Skip to content

Commit cd02a16

Browse files
committed
8344316: security/auth/callback/TextCallbackHandler/Password.java make runnable with JTReg and add the UI
Backport-of: 82bc0a7f8c7ee63d2f8c3db57dc22f39963ae022
1 parent a014be6 commit cd02a16

File tree

1 file changed

+105
-22
lines changed
  • test/jdk/com/sun/security/auth/callback/TextCallbackHandler

1 file changed

+105
-22
lines changed
Lines changed: 105 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2009, 2025, 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
@@ -25,35 +25,118 @@
2525
* @test
2626
* @bug 6825240 6829785
2727
* @summary Password.readPassword() echos the input when System.Console is null
28-
* @run main/manual Password
29-
*/
30-
31-
/*
32-
* This scenario cannot be automated because util/Password.java verifies the given input stream is
33-
* equal to the initialSystemIn. This prevents the test from providing a custom input stream.
34-
*
35-
* Steps to run the test:
36-
* 1) Compile the class using the JDK version being tested: '<JdkBin>/javac Password.java'
37-
* 2) Run the test using the JDK version being tested: '<JdkBin>/java -cp . Password'
38-
* 3) Type in the first password, it should not be visible in the console
39-
* 4) Type in the second password, it should be visible in the console
40-
* 5) The final output line displays the entered passwords, both should be visible
28+
* @library /test/lib
29+
* @run main/manual/othervm Password
4130
*/
4231

4332
import com.sun.security.auth.callback.TextCallbackHandler;
33+
34+
4435
import javax.security.auth.callback.*;
36+
import javax.swing.*;
37+
38+
import jdk.test.lib.UIBuilder;
39+
40+
import java.util.Arrays;
4541

4642
public class Password {
47-
public static void main(String args[]) throws Exception {
48-
TextCallbackHandler h = new TextCallbackHandler();
49-
PasswordCallback nc = new PasswordCallback("Invisible: ", false);
50-
PasswordCallback nc2 = new PasswordCallback("Visible: ", true);
5143

52-
System.out.println("Two passwords will be prompted for. The first one " +
53-
"should have echo off, the second one on. Otherwise, this test fails");
54-
Callback[] callbacks = { nc, nc2 };
44+
private static final int TIMEOUT_MS = 240000;
45+
private volatile boolean failed = false;
46+
private volatile boolean aborted = false;
47+
private Thread currentThread = null;
48+
49+
public static void password() throws Exception {
50+
51+
TextCallbackHandler h = new TextCallbackHandler();
52+
PasswordCallback nc =
53+
new PasswordCallback("Please input something, your input should be VISIBLE: ", true);
54+
PasswordCallback nc2 =
55+
new PasswordCallback("Please input something again, your input should be INVISIBLE: ", false);
56+
Callback[] callbacks = {nc, nc2};
5557
h.handle(callbacks);
5658
System.out.println("You input " + new String(nc.getPassword()) +
5759
" and " + new String(nc2.getPassword()));
58-
}
60+
}
61+
62+
public static void main(String[] args) throws Exception {
63+
if (Arrays.asList(args).contains("--password")) {
64+
password();
65+
} else {
66+
final String instructions = String.format("%s/bin/java -cp \"%s\" Password --password",
67+
System.getProperty("java.home").replace("\\","/"),
68+
System.getProperty("java.class.path").replace("\\","/")
69+
);
70+
71+
boolean testFailed = new Password().validate(
72+
"Please copy and execute the following script in the terminal / Windows Command Prompt window. " +
73+
"Two passwords will be prompted for.\n" +
74+
"Enter something at each prompt and press Enter/Return.\n" +
75+
"If the first input is visible and the second is invisible, this test PASSES. Otherwise, this test FAILS.\n" +
76+
"Once the test is complete please select whether the test has passed.\n",
77+
instructions);
78+
79+
if (testFailed) {
80+
throw new RuntimeException("Test has failed");
81+
}
82+
}
83+
}
84+
85+
public boolean validate(String instruction, String message) {
86+
failed = false;
87+
currentThread = Thread.currentThread();
88+
final JDialog dialog = new UIBuilder.DialogBuilder()
89+
.setTitle("Password")
90+
.setInstruction(instruction)
91+
.setMessage(message)
92+
.setPassAction(e -> pass())
93+
.setFailAction(e -> fail())
94+
.setCloseAction(this::abort)
95+
.build();
96+
97+
SwingUtilities.invokeLater(() -> {
98+
try {
99+
dialog.setVisible(true);
100+
} catch (Exception e) {
101+
throw new RuntimeException(e);
102+
}
103+
});
104+
105+
try {
106+
Thread.sleep(TIMEOUT_MS);
107+
//Timed out, so fail the test
108+
throw new RuntimeException(
109+
"Timed out after " + TIMEOUT_MS / 1000 + " seconds");
110+
} catch (final InterruptedException e) {
111+
if (aborted) {
112+
throw new RuntimeException("TEST ABORTED");
113+
}
114+
115+
if (failed) {
116+
System.out.println("TEST FAILED");
117+
System.out.println(message);
118+
} else {
119+
System.out.println("TEST PASSED");
120+
}
121+
} finally {
122+
dialog.dispose();
123+
}
124+
125+
return failed;
126+
}
127+
128+
public void pass() {
129+
failed = false;
130+
currentThread.interrupt();
131+
}
132+
133+
public void fail() {
134+
failed = true;
135+
currentThread.interrupt();
136+
}
137+
138+
public void abort() {
139+
aborted = true;
140+
currentThread.interrupt();
141+
}
59142
}

0 commit comments

Comments
 (0)