File: i18n.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (362 lines) | stat: -rw-r--r-- 14,461 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 * Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 4348369 8076069 8294994
 * @summary keytool i18n compliant
 * @author charlie lai
 * @modules java.base/sun.security.tools.keytool
 * @library /test/lib
 * @run main/manual/othervm -Duser.language=en i18n
 */

/*
 * @test
 * @bug 4348369 8076069 8294994
 * @summary keytool i18n compliant
 * @author charlie lai
 * @modules java.base/sun.security.tools.keytool
 * @library /test/lib
 * @run main/manual/othervm -Duser.language=de i18n
 */

/*
 * @test
 * @bug 4348369 8076069 8294994
 * @summary keytool i18n compliant
 * @author charlie lai
 * @modules java.base/sun.security.tools.keytool
 * @library /test/lib
 * @run main/manual/othervm -Duser.language=ja i18n
 */

/*
 * @test
 * @bug 4348369 8076069 8294994
 * @summary keytool i18n compliant
 * @author charlie lai
 * @modules java.base/sun.security.tools.keytool
 * @library /test/lib
 * @run main/manual/othervm -Duser.language=zh -Duser.country=CN i18n
 */

import jdk.test.lib.UIBuilder;

import javax.swing.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Locale;

public class i18n {
    private static final String[][] TABLE = new String[][]{
            {"-help", "All the output in this test should be in ${LANG}. "
                    + "Otherwise, the test failed."},

            {"-genkeypair -keyalg DSA -v -keysize 512 "
                    + "-dname cn=Name,ou=Java,o=Oracle,l=City,s=State,c=Country "
                    + "-storepass a "
                    + "-keypass a "
                    + "-keystore ./i18n.keystore",
                    "Output in ${LANG}. Check keytool error: java.lang.Exception: "
                            + "Keystore password must be at least 6 characters."},

            {"-genkeypair -keyalg DSA -v -keysize 512 "
                    + "-dname cn=Name,ou=Java,o=Oracle,l=City,s=State,c=Country "
                    + "-storepass password "
                    + "-keypass password "
                    + "-keystore ./i18n.keystore",
                    "Output in ${LANG}. Check: generated a 512 bit DSA key pair "
                            + "for CN=Name, OU=Java, O=Oracle, L=City, ST=State "
                            + "C=Country."},

            {"-list -v -storepass password -keystore ./i18n.keystore",
                    "Output in ${LANG}. Check: contains 1 keystore entry with "
                            + "512-bit DSA key algorithm for CN=Name, OU=Java, "
                            + "O=Oracle, L=City, ST=State C=Country."},

            {"-list -v -storepass a -keystore ./i18n.keystore",
                    "Output in ${LANG}. Check keytool error:java.io.IOException: "
                            + "keystore password was incorrect."},

            {"-genkey -keyalg DSA -v -keysize 512 "
                    + "-storepass password "
                    + "-keypass password "
                    + "-keystore ./i18n.keystore",
                    "Output in ${LANG}. Check keytool error: java.lang.Exception: "
                            + "alias 'mykey' already exists."},

            {"-genkeypair -keyalg DSA -v -keysize 512 "
                    + "-dname cn=Name,ou=Java,o=Oracle,l=City,s=State,c=Country "
                    + "-alias mykey2 "
                    + "-storepass password "
                    + "-keypass password "
                    + "-keystore ./i18n.keystore",
                    "Output in ${LANG}. Check: generated a 512 bit DSA key pair "
                            + "for CN=Name, OU=Java, O=Oracle, L=City, ST=State "
                            + "C=Country."},

            {"-list -v -storepass password -keystore ./i18n.keystore",
                    "Output in ${LANG}. Check: contains 2 keystore entries "
                            + "(alias name mykey & mykey2), both with 512-bit DSA"
                            + " key algorithm for CN=Name, OU=Java, O=Oracle, "
                            + "L=City, ST=State C=Country."},

            {"-keypasswd -v "
                    + "-alias mykey2 "
                    + "-storepass password "
                    + "-keypass password "
                    + "-new a "
                    + "-keystore ./i18n.keystore",
                    "Output in ${LANG}. Check keytool error: java.lang.Exception: "
                            + "New password must be at least 6 characters."},

            {"-keypasswd -v "
                    + "-alias mykey2 "
                    + "-storepass password "
                    + "-keypass password "
                    + "-new aaaaaa "
                    + "-keystore ./i18n.keystore",
                    "Output in ${LANG}. Check keytool error: -keypasswd "
                            + "commands not supported if -storetype is PKCS12."},

            {"-genkeypair -keyalg DSA -v -keysize 512 "
                    + "-dname cn=Name,ou=Java,o=Oracle,l=City,s=State,c=Country "
                    + "-storepass password "
                    + "-keypass password "
                    + "-keystore ./i18n.jks "
                    + "-storetype JKS",
                    "Output in ${LANG}. Check: generated a 512 bit DSA key pair "
                            + "with a JKS warning."},

            {"-keypasswd -v "
                    + "-storepass password "
                    + "-keypass password "
                    + "-new aaaaaa "
                    + "-keystore ./i18n.jks",
                    "Output in ${LANG}. Check: storing i18n.jks with a JKS warning."},

            {"-selfcert -v -alias mykey "
                    + "-storepass password "
                    + "-keypass password "
                    + "-keystore ./i18n.keystore",
                    "Output in ${LANG}. Check: generated a new certificate "
                            + "(self-signed)."},

            {"-list -v -storepass password -keystore ./i18n.keystore",
                    "Output in ${LANG}. Check: contains 2 keystore entries "
                            + "(alias name mykey & mykey2), both with 512-bit DSA"
                            + " key algorithm for CN=Name, OU=Java, O=Oracle, "
                            + "L=City, ST=State C=Country."},

            {"-export -v -alias mykey "
                    + "-file backup.keystore "
                    + "-storepass password "
                    + "-keystore ./i18n.keystore",
                    "Output in ${LANG}. Check: certificate stored in file <backup"
                            + ".keystore>."},

            {"-import -v "
                    + "-file backup.keystore "
                    + "-storepass password "
                    + "-keystore ./i18n.keystore",
                    "Output in ${LANG}. Check keytool error: reply and certificate "
                            + "in keystore are identical."},

            {"-printcert -file backup.keystore",
                    "Output in ${LANG}. Check: 512 bit DSA key pair for CN=Name,"
                            + " OU=Java, O=Oracle, L=City, ST=State C=Country."},

            {"-list -storepass password -keystore ./i18n.keystore "
                    + "-addprovider SUN",
                    "Output in ${LANG}. Check: contains 2 keystore entries "
                            + "(alias name mykey & mykey2)."},

            {"-storepasswd "
                    + "-storepass password "
                    + "-new a "
                    + "-keystore ./i18n.keystore",
                    "Output in ${LANG}. Check keytool error: java.lang.Exception: "
                            + "New password must be at least 6 characters."},

            {"-storepasswd "
                    + "-storetype PKCS11 "
                    + "-keystore NONE",
                    "Output in ${LANG}. Check keytool error: java.lang"
                            + ".UnsupportedOperationException: -storepasswd and "
                            + "-keypasswd commands not supported if -storetype is"
                            + " PKCS11."},

            {"-keypasswd "
                    + "-storetype PKCS11 "
                    + "-keystore NONE",
                    "Output in ${LANG}. Check keytool error: java.lang"
                            + ".UnsupportedOperationException: -storepasswd and "
                            + "-keypasswd commands not supported if -storetype is"
                            + " PKCS11."},

            {"-list -protected "
                    + "-storepass password "
                    + "-keystore ./i18n.keystore",
                    "Output in ${LANG}. Check keytool error: java.lang"
                            + ".IllegalArgumentException: if -protected is "
                            + "specified, then -storepass, -keypass, and -new "
                            + "must not be specified."},

            {"-keypasswd -protected "
                    + "-storepass password "
                    + "-keystore ./i18n.keystore",
                    "Output in ${LANG}. Check keytool error: java.lang"
                            + ".IllegalArgumentException: if -protected is "
                            + "specified, then -storepass, -keypass, and -new "
                            + "must not be specified."},

            {"-keypasswd -protected "
                    + "-storepass password "
                    + "-new aaaaaa "
                    + "-keystore ./i18n.keystore",
                    "Output in ${LANG}. Check keytool error: java.lang"
                            + ".IllegalArgumentException: if -protected is "
                            + "specified, then -storepass, -keypass, and -new "
                            + "must not be specified."},
    };
    private static String TEST_SRC = System.getProperty("test.src");
    private static int TIMEOUT_MS = 120000;
    private volatile boolean failed = false;
    private volatile boolean aborted = false;
    private Thread currentThread = null;

    public static void executeKeytool(String command) throws Exception {
        sun.security.tools.keytool.Main.main(command.split("\\s+"));
    }

    public static void main(String[] args) {
        final String lang = System.getProperty("user.language");
        final String country = System.getProperty("user.country");

        if (lang != null) {
            if (country != null) {
                Locale.setDefault(Locale.of(lang, country));
            } else {
                Locale.setDefault(Locale.of(lang));
            }
        }

        final String displayName = Locale.getDefault().getDisplayName();

        boolean testFailed = false;
        i18n i18nTest = new i18n();

        for (String[] entry : TABLE) {
            String command = entry[0].replaceAll("\\$\\{TEST_SRC\\}", TEST_SRC);
            String instruction = entry[1].replaceAll("\\$\\{LANG\\}", displayName);

            ByteArrayOutputStream buffer = new ByteArrayOutputStream();

            doKeytool(command, new PrintStream(buffer, true));

            testFailed |= i18nTest.validate(command, instruction, buffer.toString());
        }

        if (testFailed) {
            throw new RuntimeException("One or more tests failed.");
        }
    }

    public static void doKeytool(String command, PrintStream dest) {
        // Backups stdout and stderr.
        PrintStream origStdOut = System.out;
        PrintStream origErrOut = System.err;

        // Redirects the system output to a custom one.
        System.setOut(dest);
        System.setErr(dest);

        try {
            executeKeytool("-debug " + command);
        } catch (Exception e) {
            // Do nothing.
        } finally {
            System.setOut(origStdOut);
            System.setErr(origErrOut);
        }
    }

    public boolean validate(String command, String instruction, String message) {
        failed = false;
        currentThread = Thread.currentThread();
        JDialog dialog = new UIBuilder.DialogBuilder()
                .setTitle("keytool " + command)
                .setInstruction(instruction)
                .setMessage(message)
                .setPassAction(e -> pass())
                .setFailAction(e -> fail())
                .setCloseAction(() -> abort())
                .build();

        SwingUtilities.invokeLater(() -> {
            try {
                dialog.setVisible(true);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });

        try {
            Thread.sleep(TIMEOUT_MS);
            //Timed out, so fail the test
            throw new RuntimeException(
                    "Timed out after " + TIMEOUT_MS / 1000 + " seconds");
        } catch (InterruptedException e) {
            if (aborted) {
                throw new RuntimeException("TEST ABORTED");
            }

            if (failed) {
                System.out.println(command + ": TEST FAILED");
                System.out.println(message);
            } else {
                System.out.println(command + ": TEST PASSED");
            }
        } finally {
            dialog.dispose();
        }

        return failed;
    }

    public void pass() {
        failed = false;
        currentThread.interrupt();
    }

    public void fail() {
        failed = true;
        currentThread.interrupt();
    }

    public void abort() {
        aborted = true;
        currentThread.interrupt();
    }
}