|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 | + * @test |
| 26 | + * @bug 8354469 |
| 27 | + * @summary keytool password does not echo in multiple cases |
| 28 | + * @library /java/awt/regtesthelpers |
| 29 | + * @modules java.base/jdk.internal.util |
| 30 | + * @build PassFailJFrame |
| 31 | + * @run main/manual/othervm EchoPassword |
| 32 | + */ |
| 33 | + |
| 34 | +import jdk.internal.util.OperatingSystem; |
| 35 | + |
| 36 | +import javax.swing.JEditorPane; |
| 37 | +import javax.swing.JLabel; |
| 38 | +import javax.swing.event.HyperlinkEvent; |
| 39 | + |
| 40 | +import java.awt.Toolkit; |
| 41 | +import java.awt.datatransfer.StringSelection; |
| 42 | +import java.io.File; |
| 43 | +import java.nio.file.Path; |
| 44 | + |
| 45 | +public class EchoPassword { |
| 46 | + |
| 47 | + static JLabel label; |
| 48 | + |
| 49 | + public static void main(String[] args) throws Exception { |
| 50 | + |
| 51 | + var ks1 = "\"" + Path.of("8354469.ks1").toAbsolutePath() + "\""; |
| 52 | + var ks2 = "\"" + Path.of("8354469.ks2").toAbsolutePath() + "\""; |
| 53 | + var ks3 = "\"" + Path.of("8354469.ks3").toAbsolutePath() + "\""; |
| 54 | + |
| 55 | + final String keytool = "\"" + System.getProperty("java.home") |
| 56 | + + File.separator + "bin" + File.separator + "keytool\""; |
| 57 | + final String nonASCII = "äöäöäöäö"; |
| 58 | + |
| 59 | + final String[][] commands = { |
| 60 | + // Input password from real Console |
| 61 | + {"First command", keytool + " -keystore " + ks1 |
| 62 | + + " -genkeypair -keyalg ec -dname cn=a -alias first"}, |
| 63 | + // Input password from limited Console (when stdout is redirected) |
| 64 | + {"Second command", keytool + " -keystore " + ks2 |
| 65 | + + " -genkeypair -keyalg ec -dname cn=b -alias second | sort"}, |
| 66 | + // Input password from System.in stream |
| 67 | + {"Third command", "echo changeit| " + keytool + " -keystore " + ks1 |
| 68 | + + " -genkeypair -keyalg ec -dname cn=c -alias third"}, |
| 69 | + // Ensure limited Console does not write a newline to System.out |
| 70 | + {"Fourth command", keytool + " -keystore " + ks1 |
| 71 | + + " -exportcert -alias first | " |
| 72 | + + keytool + " -printcert -rfc"}, |
| 73 | + // Non-ASCII password from System.in |
| 74 | + {"Fifth command", "(" |
| 75 | + // Solution 2 of https://stackoverflow.com/a/29747723 |
| 76 | + + (OperatingSystem.isWindows() |
| 77 | + ? ("echo " + nonASCII + "^&echo " + nonASCII + "^&rem.") |
| 78 | + : ("echo " + nonASCII + "; echo " + nonASCII)) |
| 79 | + + ") | " + keytool + " -keystore " + ks3 |
| 80 | + + " -genkeypair -alias a -keyalg ec -dname cn=a"}, |
| 81 | + // Non-ASCII password from Console |
| 82 | + {"Sixth command", keytool + " -keystore " + ks3 |
| 83 | + + " -exportcert -alias a -rfc"}, |
| 84 | + {"The password", nonASCII} |
| 85 | + }; |
| 86 | + |
| 87 | + final String message = String.format(""" |
| 88 | + <html>Open a terminal or Windows Command Prompt window, perform |
| 89 | + the following steps, and record the final result. Each time you |
| 90 | + click a link to copy something, make sure the status line at the |
| 91 | + bottom shows the link has been successfully clicked. |
| 92 | + <h3>Part I: Password Echoing Tests</h3> |
| 93 | + <ol> |
| 94 | + <li>Click <a href='c0'>Copy First Command</a> to copy the |
| 95 | + following command into the system clipboard. Paste it into the |
| 96 | + terminal window and execute the command. |
| 97 | + <p><code> |
| 98 | + %s |
| 99 | + </code><p> |
| 100 | + When prompted, enter "changeit" and press Enter. When prompted |
| 101 | + again, enter "changeit" again and press Enter. Verify that the |
| 102 | + two password prompts show up on different lines, both |
| 103 | + passwords are hidden, and a key pair is generated successfully. |
| 104 | +
|
| 105 | + <li>Click <a href='c1'>Copy Second Command</a> to copy the |
| 106 | + following command into the system clipboard. Paste it into the |
| 107 | + terminal window and execute the command. |
| 108 | + <p><code> |
| 109 | + %s |
| 110 | + </code><p> |
| 111 | + When prompted, enter "changeit" and press Enter. When prompted |
| 112 | + again, enter "changeit" again and press Enter. Verify that the |
| 113 | + two password prompts show up on different lines, both |
| 114 | + passwords are hidden, and a key pair is generated successfully. |
| 115 | +
|
| 116 | + <li>Click <a href='c2'>Copy Third Command</a> to copy the |
| 117 | + following command into the system clipboard. Paste it into the |
| 118 | + terminal window and execute the command. |
| 119 | + <p><code> |
| 120 | + %s |
| 121 | + </code><p> |
| 122 | + You will see a prompt but you don't need to enter anything. |
| 123 | + Verify that the password "changeit" is not shown in the command |
| 124 | + output and a key pair is generated successfully. |
| 125 | +
|
| 126 | + <li>Click <a href='c3'>Copy Fourth Command</a> to copy the |
| 127 | + following command into the system clipboard. Paste it into the |
| 128 | + terminal window and execute the command. |
| 129 | + <p><code> |
| 130 | + %s |
| 131 | + </code><p> |
| 132 | + When prompted, enter "changeit" and press Enter. Verify that the |
| 133 | + password is hidden and a PEM certificate is correctly shown. |
| 134 | + </ol> |
| 135 | + <h3>Part II: Interoperability on Non-ASCII Passwords</h3> |
| 136 | + <ol> |
| 137 | + <li>Click <a href='c4'>Copy Fifth Command</a> to copy the |
| 138 | + following command into the system clipboard. Paste it into the |
| 139 | + terminal window and execute the command. |
| 140 | + <p><code> |
| 141 | + %s |
| 142 | + </code><p> |
| 143 | + Verify that a key pair is generated successfully. |
| 144 | +
|
| 145 | + <li>Click <a href='c5'>Copy Sixth Command</a> to copy the |
| 146 | + following command into the system clipboard. Paste it into the |
| 147 | + terminal window and execute the command. |
| 148 | + <p><code> |
| 149 | + %s |
| 150 | + </code><p> |
| 151 | + When prompted, click <a href='c6'>Copy Password</a> to copy the |
| 152 | + password. Paste it into the terminal window and press Enter. |
| 153 | + Verify that the password is hidden and a PEM certificate is |
| 154 | + correctly shown. |
| 155 | + </ol> |
| 156 | + Press "pass" if the behavior matches expectations; |
| 157 | + otherwise, press "fail". |
| 158 | + """, commands[0][1], commands[1][1], commands[2][1], commands[3][1], |
| 159 | + commands[4][1], commands[5][1], commands[6][1]); |
| 160 | + |
| 161 | + PassFailJFrame.builder() |
| 162 | + .instructions(message) |
| 163 | + .rows(40).columns(100) |
| 164 | + .hyperlinkListener(e -> { |
| 165 | + if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { |
| 166 | + int pos = Integer.parseInt(e.getDescription().substring(1)); |
| 167 | + Toolkit.getDefaultToolkit().getSystemClipboard().setContents( |
| 168 | + new StringSelection(commands[pos][1]), null); |
| 169 | + label.setText(commands[pos][0] + " copied"); |
| 170 | + if (e.getSource() instanceof JEditorPane ep) { |
| 171 | + ep.getCaret().setVisible(false); |
| 172 | + } |
| 173 | + } |
| 174 | + }) |
| 175 | + .splitUIBottom(() -> { |
| 176 | + label = new JLabel("Status"); |
| 177 | + return label; |
| 178 | + }) |
| 179 | + .build() |
| 180 | + .awaitAndCheck(); |
| 181 | + } |
| 182 | +} |
0 commit comments