Skip to content

Commit f4d8c00

Browse files
fgualliniwangweij
authored andcommitted
8334562: Automate com/sun/security/auth/callback/TextCallbackHandler/Default.java test
Reviewed-by: weijun
1 parent 49eb00d commit f4d8c00

File tree

5 files changed

+231
-191
lines changed

5 files changed

+231
-191
lines changed

test/jdk/ProblemList.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,6 @@ sun/security/smartcardio/TestMultiplePresent.java 8039280 generic-
609609
sun/security/smartcardio/TestPresent.java 8039280 generic-all
610610
sun/security/smartcardio/TestTransmit.java 8039280 generic-all
611611
com/sun/crypto/provider/Cipher/DES/PerformanceTest.java 8039280 generic-all
612-
com/sun/security/auth/callback/TextCallbackHandler/Default.java 8039280 generic-all
613612
com/sun/security/auth/callback/TextCallbackHandler/Password.java 8039280 generic-all
614613
com/sun/security/sasl/gsskerb/AuthOnly.java 8039280 generic-all
615614
com/sun/security/sasl/gsskerb/ConfSecurityLayer.java 8039280 generic-all

test/jdk/TEST.groups

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,6 @@ jdk_security_manual_no_input = \
624624
com/sun/crypto/provider/Cipher/DES/PerformanceTest.java \
625625
com/sun/crypto/provider/Cipher/AEAD/GCMIncrementByte4.java \
626626
com/sun/crypto/provider/Cipher/AEAD/GCMIncrementDirect4.java \
627-
com/sun/security/auth/callback/TextCallbackHandler/Default.java \
628627
com/sun/security/auth/callback/TextCallbackHandler/Password.java \
629628
com/sun/security/sasl/gsskerb/AuthOnly.java \
630629
com/sun/security/sasl/gsskerb/ConfSecurityLayer.java \
Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2001, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -23,31 +23,48 @@
2323

2424
/*
2525
* @test
26+
* @library /test/lib /java/security/testlibrary
2627
* @bug 4470717
2728
* @summary fix default handling and other misc
28-
* @run main/manual Default
29+
* @run main/othervm Default
2930
*/
3031

3132
import com.sun.security.auth.callback.TextCallbackHandler;
33+
import jdk.test.lib.Asserts;
34+
3235
import javax.security.auth.callback.*;
36+
import java.io.*;
3337

3438
public class Default {
35-
public static void main(String args[]) throws Exception {
36-
TextCallbackHandler h = new TextCallbackHandler();
37-
NameCallback nc = new NameCallback("Name: ", "charlie");
38-
ConfirmationCallback cc = new ConfirmationCallback
39-
("Correct?",
40-
ConfirmationCallback.INFORMATION,
41-
ConfirmationCallback.YES_NO_OPTION,
42-
ConfirmationCallback.NO);
43-
44-
Callback[] callbacks = { nc, cc };
45-
h.handle(callbacks);
46-
47-
if (cc.getSelectedIndex() == ConfirmationCallback.YES) {
48-
System.out.println("yes");
49-
} else {
50-
System.out.println("no");
39+
public static void main(String args[]) throws Exception {
40+
InputStream in = System.in;
41+
PrintStream err = System.err;
42+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
43+
final String defaultName = "charlie";
44+
final String simulatedInput = "-1\n-1\n";
45+
HumanInputStream humanInputStream = new HumanInputStream(simulatedInput);
46+
47+
try (PrintStream prints = new PrintStream(baos)) {
48+
System.setIn(humanInputStream);
49+
System.setErr(prints);
50+
NameCallback nameCallback = new NameCallback("Name: ", defaultName);
51+
ConfirmationCallback confirmationCallback = new ConfirmationCallback(
52+
"Correct?",
53+
ConfirmationCallback.INFORMATION,
54+
ConfirmationCallback.YES_NO_OPTION,
55+
ConfirmationCallback.NO);
56+
new TextCallbackHandler().handle(new Callback[]{nameCallback, confirmationCallback});
57+
58+
Asserts.assertEquals(nameCallback.getDefaultName(), defaultName);
59+
Asserts.assertEquals(confirmationCallback.getSelectedIndex(), ConfirmationCallback.NO);
60+
61+
} finally {
62+
System.setIn(in);
63+
System.setErr(err);
5164
}
52-
}
65+
66+
// check that the default name and confirmation were visible in the output
67+
Asserts.assertTrue(baos.toString().contains(String.format("Name: [%s]", defaultName)));
68+
Asserts.assertTrue(baos.toString().contains("1. No [default]"));
69+
}
5370
}
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
25+
import java.io.BufferedReader;
26+
import java.io.IOException;
27+
import java.io.InputStream;
28+
import java.io.InputStreamReader;
29+
30+
/**
31+
* HumanInputStream tries to act like a human sitting in front of a computer
32+
* terminal typing on the keyboard while a program is running.
33+
* <p>
34+
* The program may call InputStream.read() and BufferedReader.readLine() in
35+
* various places. a call to B.readLine() will try to buffer as much input as
36+
* possible. Thus, a trivial InputStream will find it impossible to feed
37+
* anything to I.read() after a B.readLine() call.
38+
* <p>
39+
* This is why HumanInputStream was created, which will only send a single line
40+
* to B.readLine(), no more, no less, and the next I.read() can have a chance
41+
* to read the exact character right after "\n".
42+
*
43+
*/
44+
45+
public class HumanInputStream extends InputStream {
46+
byte[] src;
47+
int pos;
48+
int length;
49+
boolean inLine;
50+
int stopIt;
51+
52+
public HumanInputStream(String input) {
53+
src = input.getBytes();
54+
pos = 0;
55+
length = src.length;
56+
stopIt = 0;
57+
inLine = false;
58+
}
59+
60+
// the trick: when called through read(byte[], int, int),
61+
// return -1 twice after "\n"
62+
63+
@Override public int read() throws IOException {
64+
int re;
65+
if(pos < length) {
66+
re = src[pos];
67+
if(inLine) {
68+
if(stopIt > 0) {
69+
stopIt--;
70+
re = -1;
71+
} else {
72+
if(re == '\n') {
73+
stopIt = 2;
74+
}
75+
pos++;
76+
}
77+
} else {
78+
pos++;
79+
}
80+
} else {
81+
re = -1; //throws new IOException("NO MORE TO READ");
82+
}
83+
return re;
84+
}
85+
@Override public int read(byte[] buffer, int offset, int len) {
86+
inLine = true;
87+
try {
88+
return super.read(buffer, offset, len);
89+
} catch(Exception e) {
90+
throw new RuntimeException("HumanInputStream error");
91+
} finally {
92+
inLine = false;
93+
}
94+
}
95+
@Override public int available() {
96+
if (pos < length) return 1;
97+
return 0;
98+
}
99+
100+
// test part
101+
static void assertTrue(boolean bool) {
102+
if (!bool)
103+
throw new RuntimeException();
104+
}
105+
106+
public static void test() throws Exception {
107+
class Tester {
108+
HumanInputStream is;
109+
BufferedReader reader;
110+
Tester(String s) {
111+
is = new HumanInputStream(s);
112+
reader = new BufferedReader(new InputStreamReader(is));
113+
}
114+
115+
// three kinds of test method
116+
// 1. read byte by byte from InputStream
117+
void testStreamReadOnce(int expection) throws Exception {
118+
assertTrue(is.read() == expection);
119+
}
120+
void testStreamReadMany(String expectation) throws Exception {
121+
char[] keys = expectation.toCharArray();
122+
for (char key : keys) {
123+
assertTrue(is.read() == key);
124+
}
125+
}
126+
// 2. read a line with a newly created Reader
127+
void testReaderReadline(String expectation) throws Exception {
128+
String s = new BufferedReader(new InputStreamReader(is)).readLine();
129+
if(s == null) assertTrue(expectation == null);
130+
else assertTrue(s.equals(expectation));
131+
}
132+
// 3. read a line with the old Reader
133+
void testReaderReadline2(String expectation) throws Exception {
134+
String s = reader.readLine();
135+
if(s == null) assertTrue(expectation == null);
136+
else assertTrue(s.equals(expectation));
137+
}
138+
}
139+
140+
Tester test;
141+
142+
test = new Tester("111\n222\n\n444\n\n");
143+
test.testReaderReadline("111");
144+
test.testReaderReadline("222");
145+
test.testReaderReadline("");
146+
test.testReaderReadline("444");
147+
test.testReaderReadline("");
148+
test.testReaderReadline(null);
149+
150+
test = new Tester("111\n222\n\n444\n\n");
151+
test.testReaderReadline2("111");
152+
test.testReaderReadline2("222");
153+
test.testReaderReadline2("");
154+
test.testReaderReadline2("444");
155+
test.testReaderReadline2("");
156+
test.testReaderReadline2(null);
157+
158+
test = new Tester("111\n222\n\n444\n\n");
159+
test.testReaderReadline2("111");
160+
test.testReaderReadline("222");
161+
test.testReaderReadline2("");
162+
test.testReaderReadline2("444");
163+
test.testReaderReadline("");
164+
test.testReaderReadline2(null);
165+
166+
test = new Tester("1\n2");
167+
test.testStreamReadMany("1\n2");
168+
test.testStreamReadOnce(-1);
169+
170+
test = new Tester("12\n234");
171+
test.testStreamReadOnce('1');
172+
test.testReaderReadline("2");
173+
test.testStreamReadOnce('2');
174+
test.testReaderReadline2("34");
175+
test.testReaderReadline2(null);
176+
177+
test = new Tester("changeit\n");
178+
test.testStreamReadMany("changeit\n");
179+
test.testReaderReadline(null);
180+
181+
test = new Tester("changeit\nName\nCountry\nYes\n");
182+
test.testStreamReadMany("changeit\n");
183+
test.testReaderReadline("Name");
184+
test.testReaderReadline("Country");
185+
test.testReaderReadline("Yes");
186+
test.testReaderReadline(null);
187+
188+
test = new Tester("Me\nHere\n");
189+
test.testReaderReadline2("Me");
190+
test.testReaderReadline2("Here");
191+
}
192+
}

0 commit comments

Comments
 (0)