test/jdk/sun/security/tools/keytool/KeyToolTest.java
author weijun
Sat, 17 Nov 2018 18:11:23 +0800
changeset 52598 0379b618ec46
parent 51944 28085dba5d9a
child 52856 5f3b9b633731
permissions -rw-r--r--
8212003: Deprecating the default keytool -keyalg option Reviewed-by: mullan, xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49882
a02abc7e5536 8144806: sun/security/tools/keytool/standard.sh fails intermittently at deleting x.jks
vtewari
parents: 47216
diff changeset
     2
 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2067
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2067
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2067
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
51944
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 49882
diff changeset
    25
 * @test
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 49882
diff changeset
    26
 * @author weijun.wang
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Testing keytool
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Run through autotest.sh and manualtest.sh
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Testing non-PKCS11 keystores:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *       echo | java -Dfile KeyToolTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Testing NSS PKCS11 keystores:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *       # testing NSS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *       # make sure the NSS db files are in current directory and writable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *       echo | java -Dnss -Dnss.lib=/path/to/libsoftokn3.so KeyToolTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Testing Solaris Cryptography Framework PKCS11 keystores:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *       # make sure you've already run pktool and set test12 as pin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *       echo | java -Dsolaris KeyToolTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * ATTENTION:
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
    44
 * Exception in thread "main" java.security.ProviderException:
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
    45
 *   sun.security.pkcs11.wrapper.PKCS11Exception: CKR_KEY_SIZE_RANGE
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *       at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:420)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *       ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Caused by: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_KEY_SIZE_RANGE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *       at sun.security.pkcs11.wrapper.PKCS11.C_SignFinal(Native Method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *       at sun.security.pkcs11.P11Signature.engineSign(P11Signature.java:391)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *       ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * been observed. Possibly a Solaris bug
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * ATTENTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * NSS PKCS11 config file are changed, DSA not supported now.
51944
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 49882
diff changeset
    56
 *
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 49882
diff changeset
    57
 * @library /test/lib
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 49882
diff changeset
    58
 * @modules java.base/sun.security.tools.keytool
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 49882
diff changeset
    59
 *          java.base/sun.security.util
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 49882
diff changeset
    60
 *          java.base/sun.security.x509
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 49882
diff changeset
    61
 * @run main/othervm/timeout=600 -Dfile KeyToolTest
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
29596
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
    64
import java.nio.file.Files;
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
    65
import java.nio.file.Paths;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
import java.security.KeyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import sun.security.x509.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import java.io.*;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
    69
import java.security.KeyPairGenerator;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
    70
import java.security.NoSuchAlgorithmException;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
    71
import java.util.*;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
    72
import java.security.cert.X509Certificate;
49882
a02abc7e5536 8144806: sun/security/tools/keytool/standard.sh fails intermittently at deleting x.jks
vtewari
parents: 47216
diff changeset
    73
import jdk.test.lib.util.FileUtils;
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
    74
import sun.security.util.ObjectIdentifier;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
51944
28085dba5d9a 8209546: Make sun/security/tools/keytool/autotest.sh to support macosx
jjiang
parents: 49882
diff changeset
    76
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
public class KeyToolTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // The stdout and stderr outputs after a keytool run
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    String out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    String err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // the output of println() in KeyTool.run
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    String ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    String lastInput = "", lastCommand = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private static final boolean debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        System.getProperty("debug") != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    static final String NSS_P11_ARG =
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
    91
            "-keystore NONE -storetype PKCS11 -providerName SunPKCS11-nss " +
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 29596
diff changeset
    92
            "-addprovider SunPKCS11 " +
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
    93
            "-providerArg p11-nss.txt ";
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 29596
diff changeset
    94
    // Use -providerClass here, to confirm it still works for SunPKCS11.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    static final String NSS_SRC_P11_ARG =
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
    96
            "-srckeystore NONE -srcstoretype PKCS11 " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
    97
            "-srcproviderName SunPKCS11-nss " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
    98
            "-providerClass sun.security.pkcs11.SunPKCS11 " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
    99
            "-providerArg p11-nss.txt ";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    static final String NZZ_P11_ARG =
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   101
            "-keystore NONE -storetype PKCS11 -providerName SunPKCS11-nzz " +
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 29596
diff changeset
   102
            "-addprovider SunPKCS11 " +
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   103
            "-providerArg p11-nzz.txt ";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    static final String NZZ_SRC_P11_ARG =
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   105
            "-srckeystore NONE -srcstoretype PKCS11 " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   106
            "-srcproviderName SunPKCS11-nzz " +
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 29596
diff changeset
   107
            "-addprovider SunPKCS11 " +
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   108
            "-providerArg p11-nzz.txt ";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    static final String SUN_P11_ARG = "-keystore NONE -storetype PKCS11 ";
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   110
    static final String SUN_SRC_P11_ARG =
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   111
            "-srckeystore NONE -srcstoretype PKCS11 ";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    String p11Arg, srcP11Arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /** Creates a new instance of KeyToolTest */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    KeyToolTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        // so that there is "Warning" and not translated into other language
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        Locale.setDefault(Locale.US);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * Helper, removes a file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    void remove(String filename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            System.err.println("Removing " + filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
49882
a02abc7e5536 8144806: sun/security/tools/keytool/standard.sh fails intermittently at deleting x.jks
vtewari
parents: 47216
diff changeset
   128
        try{
a02abc7e5536 8144806: sun/security/tools/keytool/standard.sh fails intermittently at deleting x.jks
vtewari
parents: 47216
diff changeset
   129
            FileUtils.deleteFileIfExistsWithRetry(Paths.get(filename));
a02abc7e5536 8144806: sun/security/tools/keytool/standard.sh fails intermittently at deleting x.jks
vtewari
parents: 47216
diff changeset
   130
        }catch(IOException e) {
a02abc7e5536 8144806: sun/security/tools/keytool/standard.sh fails intermittently at deleting x.jks
vtewari
parents: 47216
diff changeset
   131
            throw new RuntimeException("Error deleting " + filename, e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Run a set of keytool command with given terminal input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param input the terminal inputs, the characters typed by human
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *        if <code>cmd</code> is running on a terminal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param cmd the argument of a keytool command line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @throws if keytool goes wrong in some place
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    void test(String input, String cmd) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        lastInput = input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        lastCommand = cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   146
        // "X" is appended so that we can precisely test how input is consumed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        HumanInputStream in = new HumanInputStream(input+"X");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        test(in, cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        // make sure the input string is no more no less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if(in.read() != 'X' || in.read() != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            throw new Exception("Input not consumed exactly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    void test(InputStream in, String cmd) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // save the original 3 streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            System.err.println(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            System.err.print(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        PrintStream p1 = System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        PrintStream p2 = System.err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        InputStream i1 = System.in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        ByteArrayOutputStream b1 = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        ByteArrayOutputStream b2 = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            System.setIn(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            System.setOut(new PrintStream(b1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            System.setErr(new PrintStream(b2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 12871
diff changeset
   174
            // since System.in is overrided, the
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 12871
diff changeset
   175
            // sun.security.tools.keytool.Main.main() method will
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            // never block at user input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 12871
diff changeset
   178
            // use -debug so that main() will throw an Exception
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            // instead of calling System.exit()
14182
3041082abb40 7194449: String resources for Key Tool and Policy Tool should be in their respective packages
sflores
parents: 12871
diff changeset
   180
            sun.security.tools.keytool.Main.main(("-debug "+cmd).split("\\s+"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            out = b1.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            err = b2.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            ex = out;   // now it goes to System.out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            System.setIn(i1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            System.setOut(p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            System.setErr(p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Call this method if you expect test(input, cmd) should go OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    void testOK(String input, String cmd) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        try {
27344
890be6336eab 8057810: New defaults for DSA keys in jarsigner and keytool
weijun
parents: 14182
diff changeset
   196
            // Workaround for "8057810: Make SHA256withDSA the default
890be6336eab 8057810: New defaults for DSA keys in jarsigner and keytool
weijun
parents: 14182
diff changeset
   197
            // jarsigner and keytool algorithm for DSA keys". Unfortunately
890be6336eab 8057810: New defaults for DSA keys in jarsigner and keytool
weijun
parents: 14182
diff changeset
   198
            // SunPKCS11-NSS does not support SHA256withDSA yet.
890be6336eab 8057810: New defaults for DSA keys in jarsigner and keytool
weijun
parents: 14182
diff changeset
   199
            if (cmd.contains("p11-nss.txt") && cmd.contains("-genkey")
890be6336eab 8057810: New defaults for DSA keys in jarsigner and keytool
weijun
parents: 14182
diff changeset
   200
                    && !cmd.contains("-keyalg")) {
890be6336eab 8057810: New defaults for DSA keys in jarsigner and keytool
weijun
parents: 14182
diff changeset
   201
                cmd += " -sigalg SHA1withDSA -keysize 1024";
890be6336eab 8057810: New defaults for DSA keys in jarsigner and keytool
weijun
parents: 14182
diff changeset
   202
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            test(input, cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            afterFail(input, cmd, "OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Call this method if you expect test(input, cmd) should fail and throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    void testFail(String input, String cmd) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        boolean ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            test(input, cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if (e instanceof MissingResourceException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        if(ok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            afterFail(input, cmd, "FAIL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            throw new RuntimeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Call this method if you expect test(input, cmd) should go OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    void testOK(InputStream is, String cmd) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            test(is, cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            afterFail("", cmd, "OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Call this method if you expect test(input, cmd) should fail and throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    void testFail(InputStream is, String cmd) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        boolean ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            test(is, cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if(ok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            afterFail("", cmd, "FAIL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            throw new RuntimeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Call this method if you just want to run the command and does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * not care if it succeeds or fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    void testAnyway(String input, String cmd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            test(input, cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Helper method, print some output after a test does not do as expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    void afterFail(String input, String cmd, String should) {
27344
890be6336eab 8057810: New defaults for DSA keys in jarsigner and keytool
weijun
parents: 14182
diff changeset
   278
        if (cmd.contains("p11-nss.txt")) {
890be6336eab 8057810: New defaults for DSA keys in jarsigner and keytool
weijun
parents: 14182
diff changeset
   279
            cmd = "-J-Dnss.lib=" + System.getProperty("nss.lib") + " " + cmd;
890be6336eab 8057810: New defaults for DSA keys in jarsigner and keytool
weijun
parents: 14182
diff changeset
   280
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        System.err.println("\nTest fails for the command ---\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                "keytool " + cmd + "\nOr its debug version ---\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                "keytool -debug " + cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        System.err.println("The command result should be " + should +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                ", but it's not. Try run the command manually and type" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                " these input into it: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        char[] inputChars = input.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        for (int i=0; i<inputChars.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            char ch = inputChars[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            if (ch == '\n') System.err.print("ENTER ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            else if (ch == ' ') System.err.print("SPACE ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            else System.err.print(ch + " ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        System.err.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        System.err.println("ERR is:\n"+err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        System.err.println("OUT is:\n"+out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    void assertTrue(boolean bool, String msg) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   303
        if (debug) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   304
            System.err.println("If not " + bool + ", " + msg);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   305
        } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   306
            System.err.print("v");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   307
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if(!bool) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            afterFail(lastInput, lastCommand, "TRUE");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   310
                System.err.println(msg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            throw new RuntimeException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   315
    void assertTrue(boolean bool) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   316
        assertTrue(bool, "well...");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
   317
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Helper method, load a keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @param file file for keystore, null or "NONE" for PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * @pass password for the keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @type keystore type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * @returns the KeyStore object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * @exception Exception if anything goes wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    KeyStore loadStore(String file, String pass, String type) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        KeyStore ks = KeyStore.getInstance(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        FileInputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if (file != null && !file.equals("NONE")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            is = new FileInputStream(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        ks.load(is, pass.toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        return ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * The test suite.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Maybe it's better to put this outside the KeyToolTest class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    void testAll() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        remove("x.jceks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        remove("x.p12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        remove("x2.jceks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        remove("x2.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        remove("x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        // name changes: genkeypair, importcert, exportcert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        remove("x.jks.p1.cert");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   354
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   355
                "-keypass changeit -genkeypair -alias p1 -dname CN=olala");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   356
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   357
                "-exportcert -alias p1 -file x.jks.p1.cert");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        assertTrue(ks.getKey("p1", "changeit".toCharArray()) != null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            "key not DSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        assertTrue(new File("x.jks.p1.cert").exists(), "p1 export err");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   362
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   363
                "-delete -alias p1");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   364
        // importcert, prompt for Yes/No
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   365
        testOK("y\n", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   366
                "-importcert -alias c1 -file x.jks.p1.cert");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   367
        // importcert, -noprompt
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   368
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   369
                "-importcert -alias c2 -file x.jks.p1.cert -noprompt");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        assertTrue(ks.getCertificate("c1") != null, "import c1 err");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        // v3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        byte[] encoded = ks.getCertificate("c1").getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        X509CertImpl certImpl = new X509CertImpl(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        assertTrue(certImpl.getVersion() == 3, "Version is not 3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        // changealias and keyclone
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   379
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   380
                "-keypass changeit -genkeypair -alias p1 -dname CN=olala");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   381
        testOK("changeit\n", "-keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   382
                "-changealias -alias p1 -destalias p11");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   383
        testOK("changeit\n", "-keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   384
                "-changealias -alias c1 -destalias c11");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   385
        // press ENTER when prompt for p111's keypass
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   386
        testOK("changeit\n\n", "-keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   387
                "-keyclone -alias p11 -destalias p111");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        assertTrue(!ks.containsAlias("p1"), "there is no p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        assertTrue(!ks.containsAlias("c1"), "there is no c1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        assertTrue(ks.containsAlias("p11"), "there is p11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        assertTrue(ks.containsAlias("c11"), "there is c11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        assertTrue(ks.containsAlias("p111"), "there is p111");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        // genSecKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        remove("x.jceks");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   397
        // DES, no need keysize
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   398
        testOK("changeit\nchangeit\n\n", "-keystore x.jceks -storetype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   399
                "-genseckey -alias s1");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   400
        // DES, keysize cannot be 128
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   401
        testFail("changeit\n\n", "-keystore x.jceks -storetype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   402
                "-genseckey -alias s11 -keysize 128");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   403
        // DESede. no need keysize
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   404
        testOK("changeit\n\n", "-keystore x.jceks -storetype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   405
                "-genseckey -keyalg DESede -alias s2");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   406
        // AES, need keysize
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   407
        testFail("changeit\n\n", "-keystore x.jceks -storetype AES " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   408
                "-genseckey -keyalg Rijndael -alias s3");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   409
        testOK("changeit\n\n", "-keystore x.jceks -storetype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   410
                "-genseckey -keyalg AES -alias s3 -keysize 128");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   411
        // about keypass
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   412
        // can accept storepass
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   413
        testOK("\n", "-keystore x.jceks -storetype JCEKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   414
                "-genseckey -alias s4");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   415
        // or a new one
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   416
        testOK("keypass\nkeypass\n", "-keystore x.jceks -storetype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   417
                "-storepass changeit -genseckey -alias s5");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   418
        // keypass must be valid (prompt 3 times)
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   419
        testOK("bad\n\bad\nkeypass\nkeypass\n", "-keystore x.jceks " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   420
                "-storetype JCEKS -storepass changeit -genseckey -alias s6");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   421
        // keypass must be valid (prompt 3 times)
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   422
        testFail("bad\n\bad\nbad\n", "-keystore x.jceks -storetype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   423
                "-storepass changeit -genseckey -alias s7");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   424
        // keypass must be valid (prompt 3 times)
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   425
        testFail("bad\n\bad\nbad\nkeypass\n", "-keystore x.jceks " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   426
                "-storetype JCEKS -storepass changeit -genseckey -alias s7");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        ks = loadStore("x.jceks", "changeit", "JCEKS");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   428
        assertTrue(ks.getKey("s1", "changeit".toCharArray())
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   429
                .getAlgorithm().equalsIgnoreCase("DES"), "s1 is DES");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   430
        assertTrue(ks.getKey("s1", "changeit".toCharArray())
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   431
                .getEncoded().length == 8,  "DES is 56");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   432
        assertTrue(ks.getKey("s2", "changeit".toCharArray())
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   433
                .getEncoded().length == 24,  "DESede is 168");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   434
        assertTrue(ks.getKey("s2", "changeit".toCharArray())
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   435
                .getAlgorithm().equalsIgnoreCase("DESede"), "s2 is DESede");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   436
        assertTrue(ks.getKey("s3", "changeit".toCharArray())
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   437
                .getAlgorithm().equalsIgnoreCase("AES"), "s3 is AES");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   438
        assertTrue(ks.getKey("s4", "changeit".toCharArray())
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   439
                .getAlgorithm().equalsIgnoreCase("DES"), "s4 is DES");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   440
        assertTrue(ks.getKey("s5", "keypass".toCharArray())
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   441
                .getAlgorithm().equalsIgnoreCase("DES"), "s5 is DES");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   442
        assertTrue(ks.getKey("s6", "keypass".toCharArray())
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   443
                .getAlgorithm().equalsIgnoreCase("DES"), "s6 is DES");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        assertTrue(!ks.containsAlias("s7"), "s7 not created");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        // maybe we needn't test this, one day JKS will support SecretKey
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   447
        //testFail("changeit\nchangeit\n", "-keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   448
        //        "-genseckey -keyalg AES -alias s3 -keysize 128");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        // importKeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        remove("x.jceks");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   453
        // create 2 entries...
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   454
        testOK("changeit\nchangeit\n\n", "-keystore x.jceks -storetype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   455
                "-genkeypair -alias p1 -dname CN=Olala");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   456
        testOK("", "-keystore x.jceks -storetype JCEKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   457
                "-importcert -alias c1 -file x.jks.p1.cert -noprompt");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        ks = loadStore("x.jceks", "changeit", "JCEKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        assertTrue(ks.size() == 2, "2 entries in JCEKS");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   460
        // import, shouldn't mention destalias/srckeypass/destkeypass
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   461
        // if srcalias is no given
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   462
        testFail("changeit\nchangeit\n", "-importkeystore " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   463
                "-srckeystore x.jceks -srcstoretype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   464
                "-destkeystore x.jks -deststoretype JKS -destalias pp");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   465
        testFail("changeit\nchangeit\n", "-importkeystore " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   466
                "-srckeystore x.jceks -srcstoretype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   467
                "-destkeystore x.jks -deststoretype JKS -srckeypass changeit");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   468
        testFail("changeit\nchangeit\n", "-importkeystore " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   469
                "-srckeystore x.jceks -srcstoretype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   470
                "-destkeystore x.jks -deststoretype JKS -destkeypass changeit");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        // normal import
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   472
        testOK("changeit\nchangeit\nchangeit\n", "-importkeystore " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   473
                "-srckeystore x.jceks -srcstoretype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   474
                "-destkeystore x.jks -deststoretype JKS");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        assertTrue(ks.size() == 2, "2 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        // import again, type yes to overwrite old entries
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   478
        testOK("changeit\nchangeit\ny\ny\n", "-importkeystore " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   479
                "-srckeystore x.jceks -srcstoretype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   480
                "-destkeystore x.jks -deststoretype JKS");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        // import again, specify -nopromt
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   483
        testOK("changeit\nchangeit\n", "-importkeystore " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   484
                "-srckeystore x.jceks -srcstoretype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   485
                "-destkeystore x.jks -deststoretype JKS -noprompt");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        assertTrue(err.indexOf("Warning") != -1, "noprompt will warn");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        assertTrue(ks.size() == 2, "2 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        // import again, type into new aliases when prompted
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   490
        testOK("changeit\nchangeit\n\ns1\n\ns2\n", "-importkeystore " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   491
                "-srckeystore x.jceks -srcstoretype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   492
                "-destkeystore x.jks -deststoretype JKS");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        assertTrue(ks.size() == 4, "4 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        // importkeystore single
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   497
        // normal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        remove("x.jks");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   499
        testOK("changeit\nchangeit\nchangeit\n", "-importkeystore " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   500
                "-srckeystore x.jceks -srcstoretype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   501
                "-destkeystore x.jks -deststoretype JKS -srcalias p1");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        assertTrue(ks.size() == 1, "1 entries in JKS");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   504
        // overwrite
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   505
        testOK("changeit\nchangeit\ny\n", "-importkeystore " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   506
                "-srckeystore x.jceks -srcstoretype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   507
                "-destkeystore x.jks -deststoretype JKS -srcalias p1");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        assertTrue(ks.size() == 1, "1 entries in JKS");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   510
        // noprompt
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   511
        testOK("changeit\nchangeit\n", "-importkeystore " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   512
                "-srckeystore x.jceks -srcstoretype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   513
                "-destkeystore x.jks -deststoretype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   514
                "-srcalias p1 -noprompt");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        assertTrue(ks.size() == 1, "1 entries in JKS");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   517
        // rename
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   518
        testOK("changeit\nchangeit\n", "-importkeystore " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   519
                "-srckeystore x.jceks -srcstoretype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   520
                "-destkeystore x.jks -deststoretype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   521
                "-srcalias p1 -destalias p2");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        assertTrue(ks.size() == 2, "2 entries in JKS");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   524
        // another rename
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   525
        testOK("changeit\nchangeit\n\nnewalias\n", "-importkeystore " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   526
                "-srckeystore x.jceks -srcstoretype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   527
                "-destkeystore x.jks -deststoretype JKS -srcalias p1");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        assertTrue(ks.size() == 3, "3 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        // importkeystore single, different keypass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        remove("x.jks");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   533
        // generate entry with different keypass
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   534
        testOK("changeit\nkeypass\nkeypass\n", "-keystore x.jceks " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   535
                "-storetype JCEKS -genkeypair -alias p2 -dname CN=Olala");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   536
        // prompt
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   537
        testOK("changeit\nchangeit\nchangeit\nkeypass\n", "-importkeystore " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   538
                "-srckeystore x.jceks -srcstoretype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   539
                "-destkeystore x.jks -deststoretype JKS -srcalias p2");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        assertTrue(ks.size() == 1, "1 entries in JKS");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   542
        // diff destkeypass
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   543
        testOK("changeit\nchangeit\nkeypass\n", "-importkeystore " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   544
                "-srckeystore x.jceks -srcstoretype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   545
                "-destkeystore x.jks -deststoretype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   546
                "-srcalias p2 -destalias p3 -destkeypass keypass2");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        assertTrue(ks.size() == 2, "2 entries in JKS");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   549
        assertTrue(ks.getKey("p2", "keypass".toCharArray()) != null,
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   550
                "p2 has old password");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   551
        assertTrue(ks.getKey("p3", "keypass2".toCharArray()) != null,
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   552
                "p3 has new password");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        // importkeystore single, cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        remove("x.jks");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   556
        // normal
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   557
        testOK("changeit\nchangeit\nchangeit\n", "-importkeystore " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   558
                "-srckeystore x.jceks -srcstoretype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   559
                "-destkeystore x.jks -deststoretype JKS -srcalias c1");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   560
        // in fact srcstorepass can be ignored
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   561
        testOK("changeit\n\n", "-importkeystore " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   562
                "-srckeystore x.jceks -srcstoretype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   563
                "-destkeystore x.jks -deststoretype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   564
                "-srcalias c1 -destalias c2");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        assertTrue(err.indexOf("WARNING") != -1, "But will warn");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   566
        // 2nd import, press y to overwrite ...
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   567
        testOK("changeit\n\ny\n", "-importkeystore " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   568
                "-srckeystore x.jceks -srcstoretype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   569
                "-destkeystore x.jks -deststoretype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   570
                "-srcalias c1 -destalias c2");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   571
        // ... or rename
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   572
        testOK("changeit\n\n\nc3\n", "-importkeystore " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   573
                "-srckeystore x.jceks -srcstoretype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   574
                "-destkeystore x.jks -deststoretype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   575
                "-srcalias c1 -destalias c2");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        ks = loadStore("x.jks", "changeit", "JKS");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   577
        // c1, c2, c3
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   578
        assertTrue(ks.size() == 3, "3 entries in JKS");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        // importkeystore, secretkey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        remove("x.jks");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   582
        // create SecretKeyEntry
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   583
        testOK("changeit\n\n", "-keystore x.jceks -storetype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   584
                "-genseckey -alias s1");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   585
        // create SecretKeyEntry
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   586
        testOK("changeit\n\n", "-keystore x.jceks -storetype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   587
                "-genseckey -alias s2");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   588
        // remove the keypass!=storepass one
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   589
        testOK("changeit\n", "-keystore x.jceks -storetype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   590
                "-delete -alias p2");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        ks = loadStore("x.jceks", "changeit", "JCEKS");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   592
        // p1, c1, s1, s2
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   593
        assertTrue(ks.size() == 4, "4 entries in JCEKS");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   594
        // normal
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   595
        testOK("changeit\nchangeit\nchangeit\n", "-importkeystore " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   596
                "-srckeystore x.jceks -srcstoretype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   597
                "-destkeystore x.jks -deststoretype JKS -srcalias s1");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        assertTrue(err.indexOf("not imported") != -1, "Not imported");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   599
        assertTrue(err.indexOf("Cannot store non-PrivateKeys") != -1,
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   600
                "Not imported");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   602
        // Importing a JCEKS keystore to a JKS one. Will warn
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   603
        // for the 2 SecretKey entries
12871
b583b4c82a82 7174351: test/sun/security/tools/keytool/standard.sh failed after new Hashtable
weijun
parents: 10138
diff changeset
   604
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        remove("x.jks");
12871
b583b4c82a82 7174351: test/sun/security/tools/keytool/standard.sh failed after new Hashtable
weijun
parents: 10138
diff changeset
   606
        // Two "no" answers to bypass warnings
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   607
        // normal
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   608
        testOK("\n\n", "-srcstorepass changeit -deststorepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   609
                "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   610
                "-destkeystore x.jks -deststoretype JKS");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        assertTrue(err.indexOf("s1 not") != -1, "s1 not");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        assertTrue(err.indexOf("s2 not") != -1, "s2 not");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        assertTrue(err.indexOf("c1 success") != -1, "c1 success");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        assertTrue(err.indexOf("p1 success") != -1, "p1 success");
12871
b583b4c82a82 7174351: test/sun/security/tools/keytool/standard.sh failed after new Hashtable
weijun
parents: 10138
diff changeset
   615
        remove("x.jks");
b583b4c82a82 7174351: test/sun/security/tools/keytool/standard.sh failed after new Hashtable
weijun
parents: 10138
diff changeset
   616
        // One "yes" to stop
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   617
        // normal
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   618
        testOK("yes\n", "-srcstorepass changeit -deststorepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   619
                "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   620
                "-destkeystore x.jks -deststoretype JKS");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   621
        // maybe c1 or p1 has been imported before s1 or s2 is touched,
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   622
        // anyway we know yesNo is only asked once.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        // pkcs12
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        remove("x.jks");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   626
        // JKS prompt for keypass
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   627
        testFail("changeit\nchangeit\n", "-keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   628
                "-genkeypair -alias p1 -dname CN=olala");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        remove("x.jks");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   630
        // just type ENTER means keypass=storepass
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   631
        testOK("changeit\nchangeit\n\n", "-keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   632
                "-genkeypair -alias p1 -dname CN=olala");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        remove("x.p12");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   634
        // PKCS12 only need storepass
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   635
        testOK("", "-keystore x.p12 -storetype PKCS12 -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   636
                "-genkeypair -alias p0 -dname CN=olala");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   637
        testOK("changeit\n", "-keystore x.p12 -storetype PKCS12 " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   638
                "-genkeypair -alias p1 -dname CN=olala");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   639
        // when specify keypass, make sure keypass==storepass...
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   640
        testOK("changeit\n", "-keystore x.p12 -keypass changeit " +
52598
0379b618ec46 8212003: Deprecating the default keytool -keyalg option
weijun
parents: 51944
diff changeset
   641
                "-storetype PKCS12 -genkeypair -keyalg DSA -alias p3 -dname CN=olala");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   642
        assertTrue(err.indexOf("Warning") == -1,
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   643
                "PKCS12 silent when keypass == storepass");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   644
        // otherwise, print a warning
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   645
        testOK("changeit\n", "-keystore x.p12 -keypass another" +
52598
0379b618ec46 8212003: Deprecating the default keytool -keyalg option
weijun
parents: 51944
diff changeset
   646
                " -storetype PKCS12 -genkeypair -keyalg DSA -alias p2 -dname CN=olala");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   647
        assertTrue(err.indexOf("Warning") != -1,
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   648
                "PKCS12 warning when keypass != storepass");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   649
        // no -keypasswd for PKCS12
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   650
        testFail("", "-keystore x.p12 -storepass changeit -storetype PKCS12" +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   651
                " -keypasswd -new changeit -alias p3");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   652
        testOK("", "-keystore x.p12 -storepass changeit -storetype PKCS12 " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   653
                "-changealias -alias p3 -destalias p33");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   654
        testOK("", "-keystore x.p12 -storepass changeit -storetype PKCS12 " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   655
                "-keyclone -alias p33 -destalias p3");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        // pkcs12
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        remove("x.p12");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   659
        // PKCS12 only need storepass
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   660
        testOK("", "-keystore x.p12 -storetype PKCS12 -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   661
                "-genkeypair -alias p0 -dname CN=olala");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   662
        testOK("", "-storepass changeit -keystore x.p12 -storetype PKCS12 " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   663
                "-genkeypair -alias p1 -dname CN=olala");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   664
        // when specify keypass, make sure keypass==storepass...
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   665
        testOK("", "-storepass changeit -keystore x.p12 -keypass changeit " +
52598
0379b618ec46 8212003: Deprecating the default keytool -keyalg option
weijun
parents: 51944
diff changeset
   666
                "-storetype PKCS12 -genkeypair -keyalg DSA -alias p3 -dname CN=olala");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   667
        assertTrue(err.indexOf("Warning") == -1,
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   668
                "PKCS12 silent when keypass == storepass");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   669
        // otherwise, print a warning
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   670
        testOK("", "-storepass changeit -keystore x.p12 -keypass another " +
52598
0379b618ec46 8212003: Deprecating the default keytool -keyalg option
weijun
parents: 51944
diff changeset
   671
                "-storetype PKCS12 -genkeypair -keyalg DSA -alias p2 -dname CN=olala");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   672
        assertTrue(err.indexOf("Warning") != -1,
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   673
                "PKCS12 warning when keypass != storepass");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        remove("x.jceks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        remove("x.p12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        remove("x2.jceks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        remove("x2.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        remove("x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    void testPKCS11() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        KeyStore ks;
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   685
        // pkcs11, the password maybe different and maybe PKCS11 not supported
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        // in case last test is not executed successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        testAnyway("", p11Arg + "-storepass test12 -delete -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        testAnyway("", p11Arg + "-storepass test12 -delete -alias p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        testAnyway("", p11Arg + "-storepass test12 -delete -alias p3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        testAnyway("", p11Arg + "-storepass test12 -delete -alias nss");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        testOK("", p11Arg + "-storepass test12 -list");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   694
        assertTrue(out.indexOf("Your keystore contains 0 entries") != -1,
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   695
                "*** MAKE SURE YOU HAVE NO ENTRIES IN YOUR PKCS11 KEYSTORE " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   696
                        "BEFORE THIS TEST ***");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   698
        testOK("", p11Arg +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   699
                "-storepass test12 -genkeypair -alias p1 -dname CN=olala");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        testOK("test12\n", p11Arg + "-genkeypair -alias p2 -dname CN=olala2");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   701
        // cannot provide keypass for PKCS11
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   702
        testFail("test12\n", p11Arg +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   703
                "-keypass test12 -genkeypair -alias p3 -dname CN=olala3");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   704
        // cannot provide keypass for PKCS11
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   705
        testFail("test12\n", p11Arg +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   706
                "-keypass nonsense -genkeypair -alias p3 -dname CN=olala3");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        testOK("", p11Arg + "-storepass test12 -list");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   709
        assertTrue(out.indexOf("Your keystore contains 2 entries") != -1,
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   710
                "2 entries in p11");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        testOK("test12\n", p11Arg + "-alias p1 -changealias -destalias p3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        testOK("", p11Arg + "-storepass test12 -list -alias p3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        testFail("", p11Arg + "-storepass test12 -list -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        testOK("test12\n", p11Arg + "-alias p3 -keyclone -destalias p1");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   717
        // in PKCS11, keyclone will delete old
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   718
        testFail("", p11Arg + "-storepass test12 -list -alias p3");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        testOK("", p11Arg + "-storepass test12 -list -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   721
        // cannot change password for PKCS11
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   722
        testFail("test12\n", p11Arg + "-alias p1 -keypasswd -new another");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        testOK("", p11Arg + "-storepass test12 -list");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   725
        assertTrue(out.indexOf("Your keystore contains 2 entries") != -1,
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   726
                "2 entries in p11");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        testOK("", p11Arg + "-storepass test12 -delete -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        testOK("", p11Arg + "-storepass test12 -delete -alias p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        testOK("", p11Arg + "-storepass test12 -list");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   732
        assertTrue(out.indexOf("Your keystore contains 0 entries") != -1,
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   733
                "*** MAKE SURE YOU HAVE NO ENTRIES IN YOUR PKCS11 KEYSTORE" +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   734
                        " BEFORE THIS TEST ***");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    void testPKCS11ImportKeyStore() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        KeyStore ks;
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   740
        testOK("", p11Arg +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   741
                "-storepass test12 -genkeypair -alias p1 -dname CN=olala");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        testOK("test12\n", p11Arg + "-genkeypair -alias p2 -dname CN=olala2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        // test importkeystore for pkcs11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        // pkcs11 -> jks
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   747
        testOK("changeit\nchangeit\ntest12\n", srcP11Arg +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   748
                ("-importkeystore -destkeystore x.jks -deststoretype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   749
                "-srcalias p1"));
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   750
        assertTrue(err.indexOf("not imported") != -1,
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   751
                "cannot import key without destkeypass");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        assertTrue(!ks.containsAlias("p1"), "p1 is not imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   755
        testOK("changeit\ntest12\n", srcP11Arg +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   756
                ("-importkeystore -destkeystore x.jks -deststoretype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   757
                "-srcalias p1 -destkeypass changeit"));
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   758
        testOK("changeit\ntest12\n", srcP11Arg +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   759
                ("-importkeystore -destkeystore x.jks -deststoretype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   760
                "-srcalias p2 -destkeypass changeit"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        assertTrue(ks.containsAlias("p1"), "p1 is imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        assertTrue(ks.containsAlias("p2"), "p2 is imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        // jks -> pkcs11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        testOK("", p11Arg + "-storepass test12 -delete -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        testOK("", p11Arg + "-storepass test12 -delete -alias p2");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   767
        testOK("test12\nchangeit\n", p11Arg +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   768
                "-importkeystore -srckeystore x.jks -srcstoretype JKS");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        testOK("", p11Arg + "-storepass test12 -list -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        testOK("", p11Arg + "-storepass test12 -list -alias p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        testOK("", p11Arg + "-storepass test12 -list");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   772
        assertTrue(out.indexOf("Your keystore contains 2 entries") != -1,
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   773
                "2 entries in p11");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        // clean up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        testOK("", p11Arg + "-storepass test12 -delete -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        testOK("", p11Arg + "-storepass test12 -delete -alias p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        testOK("", p11Arg + "-storepass test12 -list");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   778
        assertTrue(out.indexOf("Your keystore contains 0 entries") != -1,
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   779
                "empty p11");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   784
    // Selected sqeTest
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    void sqeTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        FileOutputStream fos = new FileOutputStream("badkeystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        for (int i=0; i<100; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            fos.write(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        fos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        sqeCsrTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        sqePrintcertTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        sqeDeleteTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        sqeExportTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        sqeGenkeyTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        sqeImportTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        sqeKeyclonetest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        sqeKeypasswdTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        sqeListTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        sqeSelfCertTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        sqeStorepassTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        remove("badkeystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    // Import: cacert, prompt, trusted, non-trusted, bad chain, not match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    void sqeImportTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        remove("x.jks");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   811
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   812
                "-keypass changeit -genkeypair -dname CN=olala");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   813
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   814
                "-exportcert -file x.jks.p1.cert");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   815
        /* deleted */ testOK("", "-keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   816
                "-storepass changeit -delete -alias mykey");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   817
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   818
                "-importcert -file x.jks.p1.cert -noprompt");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   819
        /* deleted */ testOK("", "-keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   820
                "-storepass changeit -delete -alias mykey");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   821
        testOK("yes\n", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   822
                "-importcert -file x.jks.p1.cert");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        assertTrue(ks.containsAlias("mykey"), "imported");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   825
        /* deleted */ testOK("", "-keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   826
                "-storepass changeit -delete -alias mykey");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   827
        testOK("\n", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   828
                "-importcert -file x.jks.p1.cert");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        assertTrue(!ks.containsAlias("mykey"), "imported");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   831
        testOK("no\n", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   832
                "-importcert -file x.jks.p1.cert");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        assertTrue(!ks.containsAlias("mykey"), "imported");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   835
        testFail("no\n", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   836
                "-importcert -file nonexist");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   837
        testFail("no\n", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   838
                "-importcert -file x.jks");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    // keyclone: exist. nonexist err, cert err, dest exist, misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    void sqeKeyclonetest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        remove("x.jks");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   844
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   845
                "-keypass changeit -genkeypair -dname CN=olala");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   846
        // new pass
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   847
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   848
                "-keypass changeit -new newpass -keyclone -dest p0");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   849
        // new pass
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   850
        testOK("\n", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   851
                "-keypass changeit -keyclone -dest p1");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   852
        testOK("\n", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   853
                "-keyclone -dest p2");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   854
        testFail("\n", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   855
                "-keyclone -dest p2");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   856
        testFail("\n", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   857
                "-keyclone -dest p3 -alias noexist");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        // no cert
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   859
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   860
                "-exportcert -file x.jks.p1.cert");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   861
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   862
                "-delete -alias mykey");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   863
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   864
                "-importcert -file x.jks.p1.cert -noprompt");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   865
        // new pass
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   866
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   867
                "-keypass changeit -new newpass -keyclone -dest p0");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    // keypasswd: exist, short, nonexist err, cert err, misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    void sqeKeypasswdTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        remove("x.jks");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   873
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   874
                "-keypass changeit -genkeypair -dname CN=olala");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   875
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   876
                "-keypass changeit -keypasswd -new newpass");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   877
        /*change back*/ testOK("", "-keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   878
                "-storepass changeit -keypass newpass -keypasswd -new changeit");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   879
        testOK("newpass\nnewpass\n", "-keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   880
                "-storepass changeit -keypass changeit -keypasswd");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   881
        /*change back*/ testOK("", "-keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   882
                "-storepass changeit -keypass newpass -keypasswd -new changeit");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   883
        testOK("new\nnew\nnewpass\nnewpass\n", "-keystore x.jks " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   884
                "-storetype JKS -storepass changeit -keypass changeit -keypasswd");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   885
        /*change back*/ testOK("", "-keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   886
                "-storepass changeit -keypass newpass -keypasswd -new changeit");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   887
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   888
                "-keypasswd -new newpass");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   889
        /*change back*/ testOK("", "-keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   890
                "-storepass changeit -keypass newpass -keypasswd -new changeit");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   891
        testOK("changeit\n", "-keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   892
                "-keypasswd -new newpass");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   893
        /*change back*/ testOK("", "-keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   894
                "-storepass changeit -keypass newpass -keypasswd -new changeit");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   895
        testFail("", "-keystore x.jks -storetype JKS -storepass badpass " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   896
                "-keypass changeit -keypasswd -new newpass");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   897
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   898
                "-keypass bad -keypasswd -new newpass");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        // no cert
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   900
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   901
                "-exportcert -file x.jks.p1.cert");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   902
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   903
                "-delete -alias mykey");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   904
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   905
                "-importcert -file x.jks.p1.cert -noprompt");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   906
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   907
                "-keypass changeit -keypasswd -new newpass");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        // diff pass
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   909
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   910
                "-delete -alias mykey");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   911
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   912
                "-keypass keypass -genkeypair -dname CN=olala");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   913
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   914
                "-keypasswd -new newpass");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   915
        testOK("keypass\n", "-keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   916
                "-storepass changeit -keypasswd -new newpass");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        // i hate those misc test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    }
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   920
    // list: -f -alias, exist, nonexist err;
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   921
    // otherwise, check all shows, -rfc shows more, and misc
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    void sqeListTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        remove("x.jks");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   924
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   925
                "-keypass changeit -genkeypair -dname CN=olala");
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   926
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit -list");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   927
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   928
                "-list -alias mykey");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   929
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   930
                "-list -alias notexist");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   931
        testFail("", "-keystore x.jks -storetype JKS -storepass badpass " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   932
                "-list -alias mykey");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   933
        // keypass ignore
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   934
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   935
                "-keypass badpass -list -alias mykey");
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   936
        testOK("\n", "-keystore x.jks -storetype JKS -list");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        assertTrue(err.indexOf("WARNING") != -1, "no storepass");
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   938
        testOK("changeit\n", "-keystore x.jks -storetype JKS -list");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        assertTrue(err.indexOf("WARNING") == -1, "has storepass");
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   940
        testFail("badpass\n", "-keystore x.jks -storetype JKS -list");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        // misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        testFail("", "-keystore aa\\bb//cc -storepass changeit -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        testFail("", "-keystore nonexisting -storepass changeit -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        testFail("", "-keystore badkeystore -storepass changeit -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
    }
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   947
    // selfcert: exist, non-exist err, cert err, sig, dname, wrong keypass, misc
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    void sqeSelfCertTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        remove("x.jks");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   950
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   951
                "-keypass changeit -genkeypair -dname CN=olala");
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
   952
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit -selfcert");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   953
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   954
                "-keypass changeit -selfcert");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   955
        // not exist
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   956
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   957
                "-keypass changeit -selfcert -alias nonexisting");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   958
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   959
                "-keypass changeit -selfcert -dname CN=NewName");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   960
        // sig not compatible
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   961
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   962
                "-keypass changeit -selfcert -sigalg MD5withRSA");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   963
        // bad pass
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   964
        testFail("", "-keystore x.jks -storetype JKS -storepass wrong " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   965
                "-keypass changeit -selfcert");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   966
        // bad pass
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   967
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   968
                "-keypass wrong -selfcert");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        //misc
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   970
        testFail("", "-keystore nonexist -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   971
                "-keypass changeit -selfcert");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   972
        testFail("", "-keystore aa//dd\\gg -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   973
                "-keypass changeit -selfcert");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        // diff pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        remove("x.jks");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   976
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   977
                "-keypass keypass -genkeypair -dname CN=olala");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   978
        testFail("", "-keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   979
                "-storepass changeit -selfcert");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   980
        testOK("keypass\n", "-keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   981
                "-storepass changeit -selfcert");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   983
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   984
                "-exportcert -file x.jks.p1.cert");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   985
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   986
                "-delete -alias mykey");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   987
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   988
                "-importcert -file x.jks.p1.cert -noprompt");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   989
        // certentry cannot do selfcert
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   990
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   991
                "-selfcert");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
    // storepass: bad old, short new, misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    void sqeStorepassTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        remove("x.jks");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   997
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   998
                "-keypass changeit -genkeypair -dname CN=olala");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
   999
        // all in arg
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1000
        testOK("", "-storepasswd -keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1001
                "-storepass changeit -new newstore");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1002
        /* Change back */ testOK("", "-storepasswd -keystore x.jks" +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1003
                " -storetype JKS -storepass newstore -new changeit");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1004
        // all not in arg, new twice
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1005
        testOK("changeit\nnewstore\nnewstore\n", "-storepasswd " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1006
                "-keystore x.jks -storetype JKS");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1007
        /* Change back */ testOK("", "-storepasswd -keystore x.jks " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1008
                "-storetype JKS -storepass newstore -new changeit");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1009
        // new in arg
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1010
        testOK("changeit\n", "-storepasswd -keystore x.jks " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1011
                "-storetype JKS -new newstore");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1012
        /* Change back */ testOK("", "-storepasswd -keystore x.jks " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1013
                "-storetype JKS -storepass newstore -new changeit");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1014
        // old in arg
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1015
        testOK("newstore\nnewstore\n", "-storepasswd -keystore x.jks " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1016
                "-storetype JKS -storepass changeit");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1017
        /* Change back */ testOK("", "-storepasswd -keystore x.jks " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1018
                "-storetype JKS -storepass newstore -new changeit");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1019
        // old in arg
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1020
        testOK("new\nnew\nnewstore\nnewstore\n", "-storepasswd " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1021
                "-keystore x.jks -storetype JKS -storepass changeit");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1022
        /* Change back */ testOK("", "-storepasswd -keystore x.jks " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1023
                "-storetype JKS -storepass newstore -new changeit");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1024
        // bad old
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1025
        testFail("", "-storepasswd -keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1026
                "-storepass badold -new newstore");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1027
        // short new
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1028
        testFail("", "-storepasswd -keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1029
                "-storepass changeit -new new");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        // misc
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1031
        // non exist
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1032
        testFail("", "-storepasswd -keystore nonexist " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1033
                "-storepass changeit -new newstore");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1034
        // bad file
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1035
        testFail("", "-storepasswd -keystore badkeystore " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1036
                "-storepass changeit -new newstore");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1037
        // bad file
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1038
        testFail("", "-storepasswd -keystore aa\\bb//cc//dd " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1039
                "-storepass changeit -new newstore");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
    void sqeGenkeyTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        remove("x.jks");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1046
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1047
                "-keypass changeit -genkeypair -dname CN=olala");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1048
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1049
                "-keypass changeit -genkeypair -dname CN=olala");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1050
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1051
                "-keypass changeit -genkeypair -dname CN=olala -alias newentry");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1052
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1053
                "-keypass changeit -genkeypair -dname CN=olala -alias newentry");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1054
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1055
                "-keypass changeit -genkeypair -dname CN=olala -keyalg DSA " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1056
                "-alias n1");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1057
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1058
                "-keypass changeit -genkeypair -dname CN=olala -keyalg RSA " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1059
                "-alias n2");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1060
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1061
                "-keypass changeit -genkeypair -dname CN=olala " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1062
                "-keyalg NoSuchAlg -alias n3");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1063
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1064
                "-keypass changeit -genkeypair -dname CN=olala -keysize 56 " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1065
                "-alias n4");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1066
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1067
                "-keypass changeit -genkeypair -dname CN=olala -keysize 999 " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1068
                "-alias n5");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1069
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1070
                "-keypass changeit -genkeypair -dname CN=olala -keysize 512 " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1071
                "-alias n6");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1072
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1073
                "-keypass changeit -genkeypair -dname CN=olala -keysize 1024 " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1074
                "-alias n7");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1075
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1076
                "-keypass changeit -genkeypair -dname CN=olala " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1077
                "-sigalg NoSuchAlg -alias n8");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1078
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1079
                "-keypass changeit -genkeypair -dname CN=olala -keyalg RSA " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1080
                "-sigalg MD2withRSA -alias n9");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1081
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1082
                "-keypass changeit -genkeypair -dname CN=olala -keyalg RSA " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1083
                "-sigalg MD5withRSA -alias n10");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1084
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1085
                "-keypass changeit -genkeypair -dname CN=olala -keyalg RSA " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1086
                "-sigalg SHA1withRSA -alias n11");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1087
        testFail("", "-keystore aa\\bb//cc\\dd -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1088
                "-keypass changeit -genkeypair -dname CN=olala -keyalg RSA " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1089
                "-sigalg NoSuchAlg -alias n12");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1090
        testFail("", "-keystore badkeystore -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1091
                "-keypass changeit -genkeypair -dname CN=olala " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1092
                "-alias n14");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1093
        testFail("", "-keystore x.jks -storetype JKS -storepass badpass " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1094
                "-keypass changeit -genkeypair -dname CN=olala -alias n16");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1095
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1096
                "-keypass changeit -genkeypair -dname CNN=olala -alias n17");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
    void sqeExportTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        remove("x.jks");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1102
        // nonexist
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1103
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1104
                "-export -file mykey.cert -alias mykey");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1105
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1106
                "-keypass changeit -genkeypair -dname CN=olala");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1107
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1108
                "-export -file mykey.cert -alias mykey");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1109
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1110
                "-delete -alias mykey");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1111
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1112
                "-import -file mykey.cert -noprompt -alias c1");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1113
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1114
                "-export -file mykey.cert2 -alias c1");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1115
        testFail("", "-keystore aa\\bb//cc\\dd -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1116
                "-export -file mykey.cert2 -alias c1");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1117
        testFail("", "-keystore nonexistkeystore -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1118
                "-export -file mykey.cert2 -alias c1");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1119
        testFail("", "-keystore badkeystore -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1120
                "-export -file mykey.cert2 -alias c1");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1121
        testFail("", "-keystore x.jks -storetype JKS -storepass badpass " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1122
                "-export -file mykey.cert2 -alias c1");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        remove("mykey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        remove("mykey.cert2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
    void sqeDeleteTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        remove("x.jks");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1130
        // nonexist
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1131
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1132
                "-delete -alias mykey");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1133
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1134
                "-keypass changeit -genkeypair -dname CN=olala");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1135
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1136
                "-delete -alias mykey");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1137
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1138
                "-keypass changeit -genkeypair -dname CN=olala");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1139
        // keystore name illegal
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1140
        testFail("", "-keystore aa\\bb//cc\\dd -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1141
                "-delete -alias mykey");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1142
        // keystore not exist
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1143
        testFail("", "-keystore nonexistkeystore -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1144
                "-delete -alias mykey");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1145
        // keystore invalid
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1146
        testFail("", "-keystore badkeystore -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1147
                "-delete -alias mykey");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1148
        // wrong pass
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1149
        testFail("", "-keystore x.jks -storetype JKS -storepass xxxxxxxx " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1150
                "-delete -alias mykey");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    void sqeCsrTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
        remove("x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        remove("csr1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        // PrivateKeyEntry can do certreq
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1159
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1160
                "-keypass changeit -genkeypair -dname CN=olala -keysize 1024");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1161
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1162
                "-certreq -file csr1 -alias mykey");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1163
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1164
                "-certreq -file csr1");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1165
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1166
                "-certreq -file csr1 -sigalg SHA1withDSA");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1167
        // unmatched sigalg
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1168
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1169
                "-certreq -file csr1 -sigalg MD5withRSA");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        // misc test
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1171
        // bad storepass
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1172
        testFail("", "-keystore x.jks -storetype JKS -storepass badstorepass " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1173
                "-certreq -file csr1");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1174
        // storepass from terminal
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1175
        testOK("changeit\n", "-keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1176
                "-certreq -file csr1");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1177
        // must provide storepass
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1178
        testFail("\n", "-keystore x.jks -storetype JKS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1179
                "-certreq -file csr1");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1180
        // bad keypass
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1181
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1182
                "-keypass badkeypass -certreq -file csr1");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1183
        // bad filepath
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1184
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1185
                "-certreq -file aa\\bb//cc\\dd");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1186
        // non-existing keystore
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1187
        testFail("", "-keystore noexistks -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1188
                "-certreq -file csr1");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        // Try the RSA private key
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1190
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1191
                "-delete -alias mykey");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1192
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1193
                "-keypass changeit -genkeypair -dname CN=olala -keyalg RSA");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1194
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1195
                "-certreq -file csr1 -alias mykey");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1196
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1197
                "-certreq -file csr1");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1198
        // unmatched sigalg
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1199
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1200
                "-certreq -file csr1 -sigalg SHA1withDSA");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1201
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1202
                "-certreq -file csr1 -sigalg MD5withRSA");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        // TrustedCertificateEntry cannot do certreq
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1204
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1205
                "-exportcert -file x.jks.p1.cert");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1206
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1207
                "-delete -alias mykey");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1208
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1209
                "-importcert -file x.jks.p1.cert -noprompt");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1210
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1211
                "-certreq -file csr1 -alias mykey");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1212
        testFail("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1213
                "-certreq -file csr1");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        remove("x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        remove("csr1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
    void sqePrintcertTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        remove("mykey.cert");
41960
916bb3d29d7b 8168882: keytool doesn't print certificate info if disabled algorithm was used for signing a jar
asmotrak
parents: 41107
diff changeset
  1222
        remove("myweakkey.cert");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1223
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1224
                "-keypass changeit -genkeypair -dname CN=olala");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1225
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1226
                "-export -file mykey.cert -alias mykey");
41960
916bb3d29d7b 8168882: keytool doesn't print certificate info if disabled algorithm was used for signing a jar
asmotrak
parents: 41107
diff changeset
  1227
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
916bb3d29d7b 8168882: keytool doesn't print certificate info if disabled algorithm was used for signing a jar
asmotrak
parents: 41107
diff changeset
  1228
                "-keypass changeit -genkeypair -dname CN=weak -keyalg rsa " +
916bb3d29d7b 8168882: keytool doesn't print certificate info if disabled algorithm was used for signing a jar
asmotrak
parents: 41107
diff changeset
  1229
                "-keysize 512 -sigalg MD5withRSA -alias myweakkey");
916bb3d29d7b 8168882: keytool doesn't print certificate info if disabled algorithm was used for signing a jar
asmotrak
parents: 41107
diff changeset
  1230
        testOK("", "-keystore x.jks -storetype JKS -storepass changeit " +
916bb3d29d7b 8168882: keytool doesn't print certificate info if disabled algorithm was used for signing a jar
asmotrak
parents: 41107
diff changeset
  1231
                "-export -file myweakkey.cert -alias myweakkey");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        testFail("", "-printcert -file badkeystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        testFail("", "-printcert -file a/b/c/d");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        testOK("", "-printcert -file mykey.cert");
41960
916bb3d29d7b 8168882: keytool doesn't print certificate info if disabled algorithm was used for signing a jar
asmotrak
parents: 41107
diff changeset
  1235
        testOK("", "-printcert -file myweakkey.cert");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        FileInputStream fin = new FileInputStream("mykey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        testOK(fin, "-printcert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        fin.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        remove("mykey.cert");
41960
916bb3d29d7b 8168882: keytool doesn't print certificate info if disabled algorithm was used for signing a jar
asmotrak
parents: 41107
diff changeset
  1241
        remove("myweakkey.cert");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
29596
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  1244
    // 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  1245
    static void checkPem(String file) throws Exception {
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  1246
        boolean maybeLast = false;
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  1247
        for (String s: Files.readAllLines(Paths.get(file))) {
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  1248
            if (s.isEmpty()) continue;
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  1249
            if (s.startsWith("---")) continue;
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  1250
            if (maybeLast) {
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  1251
                throw new Exception("Last line already seen");
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  1252
            }
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  1253
            if (s.length() > 64) {
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  1254
                throw new Exception(s);
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  1255
            }
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  1256
            if (s.length() < 64) {
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  1257
                maybeLast = true;
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  1258
            }
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  1259
        }
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  1260
    }
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  1261
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1262
    void v3extTest(String keyAlg) throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1263
        KeyStore ks;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1264
        remove("x.jks");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1265
        String simple = "-keystore x.jks -storetype JKS -storepass changeit " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1266
                "-keypass changeit -noprompt -keyalg " + keyAlg + " ";
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1267
        String pre = simple + "-genkeypair -dname CN=Olala -alias ";
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1268
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1269
        // Version and SKID
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1270
        testOK("", pre + "o1");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1271
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1272
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1273
        assertTrue(((X509Certificate)ks.getCertificate("o1")).getVersion() == 3);
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1274
        assertTrue(((X509CertImpl)ks.getCertificate("o1"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1275
                .getSubjectKeyIdentifierExtension() != null);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1276
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1277
        // BC
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1278
        testOK("", pre + "b1 -ext BC:critical");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1279
        testOK("", pre + "b2 -ext BC");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1280
        testOK("", pre + "b3 -ext bc");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1281
        testOK("", pre + "b4 -ext BasicConstraints");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1282
        testOK("", pre + "b5 -ext basicconstraints");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1283
        testOK("", pre + "b6 -ext BC=ca:true,pathlen:12");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1284
        testOK("", pre + "b7 -ext BC=ca:false");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1285
        testOK("", pre + "b8 -ext BC:critical=ca:false");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1286
        testOK("", pre + "b9 -ext BC=12");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1287
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1288
        ks = loadStore("x.jks", "changeit", "JKS");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1289
        assertTrue(((X509CertImpl)ks.getCertificate("b1"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1290
                .getBasicConstraintsExtension().isCritical());
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1291
        assertTrue(!((X509CertImpl)ks.getCertificate("b2"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1292
                .getBasicConstraintsExtension().isCritical());
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1293
        assertTrue(((X509CertImpl)ks.getCertificate("b8"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1294
                .getBasicConstraintsExtension().isCritical());
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1295
        assertTrue(((X509Certificate)ks.getCertificate("b1"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1296
                .getBasicConstraints() == Integer.MAX_VALUE);
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1297
        assertTrue(((X509Certificate)ks.getCertificate("b2"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1298
                .getBasicConstraints() == Integer.MAX_VALUE);
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1299
        assertTrue(((X509Certificate)ks.getCertificate("b3"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1300
                .getBasicConstraints() == Integer.MAX_VALUE);
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1301
        assertTrue(((X509Certificate)ks.getCertificate("b4"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1302
                .getBasicConstraints() == Integer.MAX_VALUE);
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1303
        assertTrue(((X509Certificate)ks.getCertificate("b5"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1304
                .getBasicConstraints() == Integer.MAX_VALUE);
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1305
        assertTrue(((X509Certificate)ks.getCertificate("b6"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1306
                .getBasicConstraints() == 12);
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1307
        assertTrue(((X509Certificate)ks.getCertificate("b7"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1308
                .getBasicConstraints() == -1);
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1309
        assertTrue(((X509Certificate)ks.getCertificate("b9"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1310
                .getBasicConstraints() == 12);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1311
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1312
        // KU
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1313
        testOK("", pre + "ku1 -ext KeyUsage:critical=digitalsignature");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1314
        testOK("", pre + "ku2 -ext KU=digitalSignature");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1315
        testOK("", pre + "ku3 -ext KU=ds");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1316
        testOK("", pre + "ku4 -ext KU=dig");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1317
        // ambigous value
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1318
        testFail("", pre + "ku5 -ext KU=d");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1319
        // cRLSign cannot be cs
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1320
        testFail("", pre + "ku6 -ext KU=cs");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1321
        testOK("", pre + "ku11 -ext KU=nr");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1322
        // ke also means keyAgreement
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1323
        testFail("", pre + "ku12 -ext KU=ke");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1324
        testOK("", pre + "ku12 -ext KU=keyE");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1325
        // de also means decipherOnly
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1326
        testFail("", pre + "ku13 -ext KU=de");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1327
        testOK("", pre + "ku13 -ext KU=dataE");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1328
        testOK("", pre + "ku14 -ext KU=ka");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1329
        testOK("", pre + "ku15 -ext KU=kcs");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1330
        testOK("", pre + "ku16 -ext KU=crls");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1331
        testOK("", pre + "ku17 -ext KU=eo");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1332
        testOK("", pre + "ku18 -ext KU=do");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1333
        testOK("", pre + "ku19 -ext KU=cc");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1334
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1335
        testOK("", pre + "ku017 -ext KU=ds,cc,eo");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1336
        testOK("", pre + "ku135 -ext KU=nr,dataEncipherment,keyCertSign");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1337
        testOK("", pre + "ku246 -ext KU=keyEnc,cRL,keyA");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1338
        testOK("", pre + "ku1234 -ext KU=ka,da,keyE,nonR");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1339
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1340
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1341
        class CheckKU {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1342
            void check(KeyStore ks, String alias, int... pos) throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1343
                System.err.print("x");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1344
                boolean[] bs = ((X509Certificate)ks.getCertificate(alias))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1345
                        .getKeyUsage();
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1346
                bs = Arrays.copyOf(bs, 9);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1347
                for (int i=0; i<bs.length; i++) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1348
                    boolean found = false;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1349
                    for (int p: pos) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1350
                        if (p == i) found = true;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1351
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1352
                    if (!found ^ bs[i]) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1353
                        // OK
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1354
                    } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1355
                        throw new RuntimeException("KU not match at " + i +
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1356
                                ": " + found + " vs " + bs[i]);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1357
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1358
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1359
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1360
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1361
        CheckKU c = new CheckKU();
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1362
        assertTrue(((X509CertImpl)ks.getCertificate("ku1"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1363
                .getExtension(PKIXExtensions.KeyUsage_Id).isCritical());
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1364
        assertTrue(!((X509CertImpl)ks.getCertificate("ku2"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1365
                .getExtension(PKIXExtensions.KeyUsage_Id).isCritical());
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1366
        c.check(ks, "ku1", 0);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1367
        c.check(ks, "ku2", 0);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1368
        c.check(ks, "ku3", 0);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1369
        c.check(ks, "ku4", 0);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1370
        c.check(ks, "ku11", 1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1371
        c.check(ks, "ku12", 2);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1372
        c.check(ks, "ku13", 3);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1373
        c.check(ks, "ku14", 4);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1374
        c.check(ks, "ku15", 5);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1375
        c.check(ks, "ku16", 6);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1376
        c.check(ks, "ku17", 7);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1377
        c.check(ks, "ku18", 8);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1378
        c.check(ks, "ku19", 1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1379
        c.check(ks, "ku11", 1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1380
        c.check(ks, "ku11", 1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1381
        c.check(ks, "ku11", 1);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1382
        c.check(ks, "ku017", 0, 1, 7);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1383
        c.check(ks, "ku135", 1, 3, 5);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1384
        c.check(ks, "ku246", 6, 2, 4);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1385
        c.check(ks, "ku1234", 1, 2, 3, 4);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1386
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1387
        // EKU
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1388
        testOK("", pre + "eku1 -ext EKU:critical=sa");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1389
        testOK("", pre + "eku2 -ext ExtendedKeyUsage=ca");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1390
        testOK("", pre + "eku3 -ext EKU=cs");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1391
        testOK("", pre + "eku4 -ext EKU=ep");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1392
        testOK("", pre + "eku8 -ext EKU=ts");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1393
        testFail("", pre + "eku9 -ext EKU=os");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1394
        testOK("", pre + "eku9 -ext EKU=ocsps");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1395
        testOK("", pre + "eku10 -ext EKU=any");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1396
        testOK("", pre + "eku11 -ext EKU=1.2.3.4,1.3.5.7,ep");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1397
        testFail("", pre + "eku12 -ext EKU=c");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1398
        testFail("", pre + "eku12 -ext EKU=nothing");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1399
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1400
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1401
        class CheckEKU {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1402
            void check(KeyStore ks, String alias, String... pos) throws Exception {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1403
                System.err.print("x");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1404
                List<String> bs = ((X509Certificate)ks.getCertificate(alias))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1405
                        .getExtendedKeyUsage();
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1406
                int found = 0;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1407
                for (String p: pos) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1408
                    if (bs.contains(p)) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1409
                        found++;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1410
                    } else {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1411
                        throw new RuntimeException("EKU: not included " + p);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1412
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1413
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1414
                if (found != bs.size()) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1415
                    throw new RuntimeException("EKU: more items than expected");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1416
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1417
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1418
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1419
        CheckEKU cx = new CheckEKU();
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1420
        assertTrue(((X509CertImpl)ks.getCertificate("eku1"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1421
                .getExtension(PKIXExtensions.ExtendedKeyUsage_Id).isCritical());
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1422
        assertTrue(!((X509CertImpl)ks.getCertificate("eku2"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1423
                .getExtension(PKIXExtensions.ExtendedKeyUsage_Id).isCritical());
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1424
        cx.check(ks, "eku1", "1.3.6.1.5.5.7.3.1");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1425
        cx.check(ks, "eku2", "1.3.6.1.5.5.7.3.2");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1426
        cx.check(ks, "eku3", "1.3.6.1.5.5.7.3.3");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1427
        cx.check(ks, "eku4", "1.3.6.1.5.5.7.3.4");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1428
        cx.check(ks, "eku8", "1.3.6.1.5.5.7.3.8");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1429
        cx.check(ks, "eku9", "1.3.6.1.5.5.7.3.9");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1430
        cx.check(ks, "eku10", "2.5.29.37.0");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1431
        cx.check(ks, "eku11", "1.3.6.1.5.5.7.3.4", "1.2.3.4", "1.3.5.7");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1432
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1433
        // SAN
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1434
        testOK("", pre+"san1 -ext san:critical=email:me@me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1435
        testOK("", pre+"san2 -ext san=uri:http://me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1436
        testOK("", pre+"san3 -ext san=dns:me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1437
        testOK("", pre+"san4 -ext san=ip:192.168.0.1");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1438
        testOK("", pre+"san5 -ext san=oid:1.2.3.4");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1439
        testOK("", pre+"san235 -ext san=uri:http://me.org,dns:me.org,oid:1.2.3.4");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1440
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1441
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1442
        class CheckSAN {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1443
            // Please sort items with name type
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1444
            void check(KeyStore ks, String alias, int type, Object... items)
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1445
                    throws Exception {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1446
                int pos = 0;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1447
                System.err.print("x");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1448
                Object[] names = null;
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1449
                if (type == 0) names = ((X509Certificate)ks.getCertificate(alias))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1450
                        .getSubjectAlternativeNames().toArray();
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1451
                else names = ((X509Certificate)ks.getCertificate(alias))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1452
                        .getIssuerAlternativeNames().toArray();
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1453
                Arrays.sort(names, new Comparator() {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1454
                    public int compare(Object o1, Object o2) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1455
                        int i1 = (Integer)((List)o1).get(0);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1456
                        int i2 = (Integer)((List)o2).get(0);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1457
                        return i1 - i2;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1458
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1459
                });
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1460
                for (Object o: names) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1461
                    List l = (List)o;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1462
                    for (Object o2: l) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1463
                        if (!items[pos++].equals(o2)) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1464
                            throw new RuntimeException("Not equals at " + pos
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1465
                                    + ": " + items[pos-1] + " vs " + o2);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1466
                        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1467
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1468
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1469
                if (pos != items.length) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1470
                    throw new RuntimeException("Extra items, pos is " + pos);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1471
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1472
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1473
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1474
        CheckSAN csan = new CheckSAN();
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1475
        assertTrue(((X509CertImpl)ks.getCertificate("san1"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1476
                .getSubjectAlternativeNameExtension().isCritical());
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1477
        assertTrue(!((X509CertImpl)ks.getCertificate("san2"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1478
                .getSubjectAlternativeNameExtension().isCritical());
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1479
        csan.check(ks, "san1", 0, 1, "me@me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1480
        csan.check(ks, "san2", 0, 6, "http://me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1481
        csan.check(ks, "san3", 0, 2, "me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1482
        csan.check(ks, "san4", 0, 7, "192.168.0.1");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1483
        csan.check(ks, "san5", 0, 8, "1.2.3.4");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1484
        csan.check(ks, "san235", 0, 2, "me.org", 6, "http://me.org", 8, "1.2.3.4");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1485
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1486
        // IAN
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1487
        testOK("", pre+"ian1 -ext ian:critical=email:me@me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1488
        testOK("", pre+"ian2 -ext ian=uri:http://me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1489
        testOK("", pre+"ian3 -ext ian=dns:me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1490
        testOK("", pre+"ian4 -ext ian=ip:192.168.0.1");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1491
        testOK("", pre+"ian5 -ext ian=oid:1.2.3.4");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1492
        testOK("", pre+"ian235 -ext ian=uri:http://me.org,dns:me.org,oid:1.2.3.4");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1493
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1494
        ks = loadStore("x.jks", "changeit", "JKS");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1495
        assertTrue(((X509CertImpl)ks.getCertificate("ian1"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1496
                .getIssuerAlternativeNameExtension().isCritical());
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1497
        assertTrue(!((X509CertImpl)ks.getCertificate("ian2"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1498
                .getIssuerAlternativeNameExtension().isCritical());
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1499
        csan.check(ks, "ian1", 1, 1, "me@me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1500
        csan.check(ks, "ian2", 1, 6, "http://me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1501
        csan.check(ks, "ian3", 1, 2, "me.org");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1502
        csan.check(ks, "ian4", 1, 7, "192.168.0.1");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1503
        csan.check(ks, "ian5", 1, 8, "1.2.3.4");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1504
        csan.check(ks, "ian235", 1, 2, "me.org", 6, "http://me.org", 8, "1.2.3.4");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1505
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1506
        // SIA
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1507
        testOK("", pre+"sia1 -ext sia=care:uri:ldap://ca.com/cn=CA");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1508
        testOK("", pre+"sia2 -ext sia=ts:email:ts@ca.com");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1509
        testFail("SIA never critical", pre +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1510
                "sia3 -ext sia:critical=ts:email:ts@ca.com");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1511
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1512
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1513
        class CheckSia {
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1514
            void check(KeyStore ks, String alias, int type, Object... items)
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1515
                    throws Exception {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1516
                int pos = 0;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1517
                System.err.print("x");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1518
                AccessDescription[] ads = null;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1519
                if (type == 0) {
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1520
                    SubjectInfoAccessExtension siae = (SubjectInfoAccessExtension)
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1521
                            ((X509CertImpl)ks.getCertificate(alias))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1522
                            .getExtension(PKIXExtensions.SubjectInfoAccess_Id);
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1523
                    ads = siae.getAccessDescriptions()
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1524
                            .toArray(new AccessDescription[0]);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1525
                } else {
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1526
                    AuthorityInfoAccessExtension aiae =
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1527
                            (AuthorityInfoAccessExtension)
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1528
                            ((X509CertImpl)ks.getCertificate(alias))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1529
                            .getExtension(PKIXExtensions.AuthInfoAccess_Id);
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1530
                    ads = aiae.getAccessDescriptions()
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1531
                            .toArray(new AccessDescription[0]);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1532
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1533
                Arrays.sort(ads, new Comparator<AccessDescription>() {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1534
                    @Override
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1535
                    public int compare(AccessDescription o1,
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1536
                                       AccessDescription o2) {
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1537
                        return o1.getAccessMethod().toString()
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1538
                                .compareTo(o2.getAccessMethod().toString());
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1539
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1540
                });
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1541
                for (AccessDescription ad: ads) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1542
                    if (!ad.getAccessMethod().equals(items[pos++]) ||
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1543
                            !new Integer(ad.getAccessLocation().getType())
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1544
                                    .equals(items[pos++])) {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1545
                        throw new RuntimeException("Not same type at " + pos);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1546
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1547
                    String name = null;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1548
                    switch (ad.getAccessLocation().getType()) {
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1549
                        case 1:
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1550
                            name = ((RFC822Name)ad.getAccessLocation()
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1551
                                    .getName()).getName();
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1552
                            break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1553
                        case 6:
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1554
                            name = ((URIName)ad.getAccessLocation()
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1555
                                    .getName()).getURI().toString();
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1556
                            break;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1557
                        default:
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1558
                            throw new RuntimeException("Not implemented: " + ad);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1559
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1560
                    if (!name.equals(items[pos++])) {
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1561
                        throw new Exception("Name not same for " + ad +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1562
                                " at pos " + pos);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1563
                    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1564
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1565
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1566
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1567
        CheckSia csia = new CheckSia();
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1568
        assertTrue(!((X509CertImpl)ks.getCertificate("sia1"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1569
                .getExtension(PKIXExtensions.SubjectInfoAccess_Id).isCritical());
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1570
        csia.check(ks, "sia1", 0,
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1571
                AccessDescription.Ad_CAREPOSITORY_Id, 6, "ldap://ca.com/cn=CA");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1572
        csia.check(ks, "sia2",
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1573
                0, AccessDescription.Ad_TIMESTAMPING_Id, 1, "ts@ca.com");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1574
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1575
        // AIA
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1576
        testOK("", pre+"aia1 -ext aia=cai:uri:ldap://ca.com/cn=CA");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1577
        testOK("", pre+"aia2 -ext aia=ocsp:email:ocsp@ca.com");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1578
        testFail("AIA never critical", pre +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1579
                "aia3 -ext aia:critical=ts:email:ts@ca.com");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1580
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1581
        ks = loadStore("x.jks", "changeit", "JKS");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1582
        assertTrue(!((X509CertImpl)ks.getCertificate("aia1"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1583
                .getExtension(PKIXExtensions.AuthInfoAccess_Id).isCritical());
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1584
        csia.check(ks, "aia1", 1,
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1585
                AccessDescription.Ad_CAISSUERS_Id, 6, "ldap://ca.com/cn=CA");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1586
        csia.check(ks, "aia2", 1,
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1587
                AccessDescription.Ad_OCSP_Id, 1, "ocsp@ca.com");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1588
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1589
        // OID
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1590
        testOK("", pre+"oid1 -ext 1.2.3:critical=0102");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1591
        testOK("", pre+"oid2 -ext 1.2.3");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1592
        testOK("", pre+"oid12 -ext 1.2.3 -ext 1.2.4=01:02:03");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1593
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1594
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1595
        class CheckOid {
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1596
            void check(KeyStore ks, String alias, String oid, byte[] value)
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1597
                    throws Exception {
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1598
                int pos = 0;
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1599
                System.err.print("x");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1600
                Extension ex = ((X509CertImpl)ks.getCertificate(alias))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1601
                        .getExtension(new ObjectIdentifier(oid));
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1602
                if (!Arrays.equals(value, ex.getValue())) {
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1603
                    throw new RuntimeException("Not same content in " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1604
                            alias + " for " + oid);
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1605
                }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1606
            }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1607
        }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1608
        CheckOid coid = new CheckOid();
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1609
        assertTrue(((X509CertImpl)ks.getCertificate("oid1"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1610
                .getExtension(new ObjectIdentifier("1.2.3")).isCritical());
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1611
        assertTrue(!((X509CertImpl)ks.getCertificate("oid2"))
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1612
                .getExtension(new ObjectIdentifier("1.2.3")).isCritical());
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1613
        coid.check(ks, "oid1", "1.2.3", new byte[]{1,2});
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1614
        coid.check(ks, "oid2", "1.2.3", new byte[]{});
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1615
        coid.check(ks, "oid12", "1.2.3", new byte[]{});
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1616
        coid.check(ks, "oid12", "1.2.4", new byte[]{1,2,3});
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1617
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1618
        // honored
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1619
        testOK("", pre+"ca");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1620
        testOK("", pre+"a");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1621
        // request: BC,KU,1.2.3,1.2.4,1.2.5
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1622
        testOK("", simple+"-alias a -certreq " +
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1623
                "-ext BC=1 -ext KU=crl " +
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1624
                "-ext 1.2.3=01 -ext 1.2.4:critical=0102 -ext 1.2.5=010203 " +
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1625
                "-rfc -file test.req");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1626
        // printcertreq
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1627
        testOK("", "-printcertreq -file test.req");
29596
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  1628
        checkPem("test.req");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1629
        // issue: deny KU, change criticality of 1.2.3 and 1.2.4,
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1630
        // change content of BC, add 2.3.4
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1631
        testOK("", simple+"-gencert -alias ca -infile test.req -ext " +
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1632
                "honored=all,-KU,1.2.3:critical,1.2.4:non-critical " +
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1633
                "-ext BC=2 -ext 2.3.4=01020304 " +
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1634
                "-debug -rfc -outfile test.cert");
29596
70399c7a7f5a 8074935: jdk8 keytool doesn't validate pem files for RFC 1421 correctness, as jdk7 did
weijun
parents: 29225
diff changeset
  1635
        checkPem("test.cert");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1636
        testOK("", simple+"-importcert -file test.cert -alias a");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1637
        ks = loadStore("x.jks", "changeit", "JKS");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1638
        X509CertImpl a = (X509CertImpl)ks.getCertificate("a");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1639
        assertTrue(a.getAuthorityKeyIdentifierExtension() != null);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1640
        assertTrue(a.getSubjectKeyIdentifierExtension() != null);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1641
        assertTrue(a.getKeyUsage() == null);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1642
        assertTrue(a.getExtension(new ObjectIdentifier("1.2.3")).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1643
        assertTrue(!a.getExtension(new ObjectIdentifier("1.2.4")).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1644
        assertTrue(!a.getExtension(new ObjectIdentifier("1.2.5")).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1645
        assertTrue(a.getExtensionValue("1.2.3").length == 3);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1646
        assertTrue(a.getExtensionValue("1.2.4").length == 4);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1647
        assertTrue(a.getExtensionValue("1.2.5").length == 5);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1648
        assertTrue(a.getBasicConstraints() == 2);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1649
        assertTrue(!a.getExtension(new ObjectIdentifier("2.3.4")).isCritical());
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1650
        assertTrue(a.getExtensionValue("2.3.4").length == 6);
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1651
29110
ea89fdd8a5d5 8073181: keytool -ext honored not working correctly
weijun
parents: 28243
diff changeset
  1652
        // 8073181: keytool -ext honored not working correctly
ea89fdd8a5d5 8073181: keytool -ext honored not working correctly
weijun
parents: 28243
diff changeset
  1653
        testOK("", simple+"-gencert -alias ca -infile test.req -ext " +
29225
fb5b4b9d12f5 8074018: Named extension not recognized in keytool -ext honored after 8073182
weijun
parents: 29120
diff changeset
  1654
                "honored=1.2.3,KU,1.2.4:critical " +
29110
ea89fdd8a5d5 8073181: keytool -ext honored not working correctly
weijun
parents: 28243
diff changeset
  1655
                "-debug -rfc -outfile test2.cert");
ea89fdd8a5d5 8073181: keytool -ext honored not working correctly
weijun
parents: 28243
diff changeset
  1656
        testOK("", simple+"-importcert -file test2.cert -alias b");
ea89fdd8a5d5 8073181: keytool -ext honored not working correctly
weijun
parents: 28243
diff changeset
  1657
        ks = loadStore("x.jks", "changeit", "JKS");
ea89fdd8a5d5 8073181: keytool -ext honored not working correctly
weijun
parents: 28243
diff changeset
  1658
        X509CertImpl b = (X509CertImpl)ks.getCertificate("b");
ea89fdd8a5d5 8073181: keytool -ext honored not working correctly
weijun
parents: 28243
diff changeset
  1659
        assertTrue(!b.getExtension(new ObjectIdentifier("1.2.3")).isCritical());
ea89fdd8a5d5 8073181: keytool -ext honored not working correctly
weijun
parents: 28243
diff changeset
  1660
        assertTrue(b.getExtension(new ObjectIdentifier("1.2.4")).isCritical());
ea89fdd8a5d5 8073181: keytool -ext honored not working correctly
weijun
parents: 28243
diff changeset
  1661
29111
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  1662
        // 8073182: keytool may generate duplicate extensions
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  1663
        testOK("", pre+"dup -ext bc=2 -ext 2.5.29.19=30030101FF -ext bc=3");
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  1664
        ks = loadStore("x.jks", "changeit", "JKS");
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  1665
        X509CertImpl dup = (X509CertImpl)ks.getCertificate("dup");
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  1666
        assertTrue(dup.getBasicConstraints() == 3);
e9103f166a4a 8073182: keytool may generate duplicate extensions
weijun
parents: 29110
diff changeset
  1667
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1668
        remove("x.jks");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1669
        remove("test.req");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1670
        remove("test.cert");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1671
    }
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1672
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
    void i18nTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        //   1.  keytool -help
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        remove("x.jks");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1676
        testOK("", "-help");
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1677
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1678
        //   2. keytool -genkey -v -keysize 512 Enter "a" for the keystore
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1679
        // password. Check error (password too short). Enter "password" for
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1680
        // the keystore password. Hit 'return' for "first and last name",
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1681
        // "organizational unit", "City", "State", and "Country Code".
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1682
        // Type "yes" when they ask you if everything is correct.
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1683
        // Type 'return' for new key password.
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1684
        testOK("a\npassword\npassword\nMe\nHere\nNow\nPlace\nPlace\nUS\nyes\n\n",
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1685
                "-genkey -v -keysize 512 -keystore x.jks -storetype JKS");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        //   3. keytool -list -v -storepass password
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
  1687
        testOK("", "-list -v -storepass password -keystore x.jks -storetype JKS");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1688
        //   4. keytool -list -v Type "a" for the keystore password.
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1689
        // Check error (wrong keystore password).
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
  1690
        testFail("a\n", "-list -v -keystore x.jks -storetype JKS");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1691
        assertTrue(ex.indexOf("password was incorrect") != -1);
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1692
        //   5. keytool -genkey -v -keysize 512 Enter "password" as the password.
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1693
        // Check error (alias 'mykey' already exists).
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1694
        testFail("password\n", "-genkey -v -keysize 512" +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1695
                " -keystore x.jks -storetype JKS");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1696
        assertTrue(ex.indexOf("alias <mykey> already exists") != -1);
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1697
        //   6. keytool -genkey -v -keysize 512 -alias mykey2 -storepass password
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1698
        // Hit 'return' for "first and last name", "organizational unit", "City",
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1699
        // "State", and "Country Code". Type "yes" when they ask you if
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1700
        // everything is correct. Type 'return' for new key password.
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1701
        testOK("\n\n\n\n\n\nyes\n\n", "-genkey -v -keysize 512 -alias mykey2" +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1702
                " -storepass password -keystore x.jks -storetype JKS");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
        //   7. keytool -list -v Type 'password' for the store password.
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
  1704
        testOK("password\n", "-list -v -keystore x.jks -storetype JKS");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1705
        //   8. keytool -keypasswd -v -alias mykey2 -storepass password
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1706
        // Type "a" for the new key password. Type "aaaaaa" for the new key
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1707
        // password. Type "bbbbbb" when re-entering the new key password.
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1708
        // Type "a" for the new key password. Check Error (too many failures).
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1709
        testFail("a\naaaaaa\nbbbbbb\na\n", "-keypasswd -v -alias mykey2" +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1710
                " -storepass password -keystore x.jks -storetype JKS");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1711
        assertTrue(ex.indexOf("Too many failures - try later") != -1);
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1712
        //   9. keytool -keypasswd -v -alias mykey2 -storepass password
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1713
        // Type "aaaaaa" for the new key password. Type "aaaaaa"
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1714
        // when re-entering the new key password.
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1715
        testOK("aaaaaa\naaaaaa\n", "-keypasswd -v -alias mykey2 " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1716
                "-storepass password -keystore x.jks -storetype JKS");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
        //  10. keytool -selfcert -v -alias mykey -storepass password
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1718
        testOK("", "-selfcert -v -alias mykey -storepass password " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1719
                "-keystore x.jks -storetype JKS");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
        //  11. keytool -list -v -storepass password
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
  1721
        testOK("", "-list -v -storepass password -keystore x.jks -storetype JKS");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
        //  12. keytool -export -v -alias mykey -file cert -storepass password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
        remove("cert");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1724
        testOK("", "-export -v -alias mykey -file cert -storepass password " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1725
                "-keystore x.jks -storetype JKS");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1726
        //  13. keytool -import -v -file cert -storepass password
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1727
        // Check error (Certificate reply and cert are the same)
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1728
        testFail("", "-import -v -file cert -storepass password" +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1729
                " -keystore x.jks -storetype JKS");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1730
        assertTrue(ex.indexOf("Certificate reply and certificate" +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1731
                " in keystore are identical") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        //  14. keytool -printcert -file cert
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 27344
diff changeset
  1733
        testOK("", "-printcert -file cert -keystore x.jks -storetype JKS");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        remove("cert");
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 29596
diff changeset
  1735
        //  15. keytool -list -storepass password -addprovider SUN
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1736
        testOK("", "-list -storepass password" +
39633
9dc7586be5f0 8130302: jarsigner and keytool -providerClass needs be re-examined for modules
weijun
parents: 29596
diff changeset
  1737
                " -addprovider SUN" +
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1738
                " -keystore x.jks -storetype JKS");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
        //Error tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1742
        //   1. keytool -storepasswd -storepass password -new abc
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1743
        // Check error (password too short)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        testFail("", "-storepasswd -storepass password -new abc");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1745
        assertTrue(ex.indexOf("New password must be at least 6 characters") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
        // Changed, no NONE needed now
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
        //   2. keytool -list -storetype PKCS11 Check error (-keystore must be NONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
        //testFail("", "-list -storetype PKCS11");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1749
        //assertTrue(err.indexOf("keystore must be NONE") != -1);
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1750
        //   3. keytool -storepasswd -storetype PKCS11 -keystore NONE
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1751
        // Check error (unsupported operation)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
        testFail("", "-storepasswd -storetype PKCS11 -keystore NONE");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1753
        assertTrue(ex.indexOf("UnsupportedOperationException") != -1);
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1754
        //   4. keytool -keypasswd -storetype PKCS11 -keystore NONE
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1755
        // Check error (unsupported operation)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
        testFail("", "-keypasswd -storetype PKCS11 -keystore NONE");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1757
        assertTrue(ex.indexOf("UnsupportedOperationException") != -1);
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1758
        //   5. keytool -list -protected -storepass password
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1759
        // Check error (password can not be specified with -protected)
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1760
        testFail("", "-list -protected -storepass password " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1761
                "-keystore x.jks -storetype JKS");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1762
        assertTrue(ex.indexOf("if -protected is specified, then") != -1);
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1763
        //   6. keytool -keypasswd -protected -keypass password
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1764
        // Check error (password can not be specified with -protected)
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1765
        testFail("", "-keypasswd -protected -keypass password " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1766
                "-keystore x.jks -storetype JKS");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1767
        assertTrue(ex.indexOf("if -protected is specified, then") != -1);
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1768
        //   7. keytool -keypasswd -protected -new password
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1769
        // Check error (password can not be specified with -protected)
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1770
        testFail("", "-keypasswd -protected -new password " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1771
                "-keystore x.jks -storetype JKS");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1772
        assertTrue(ex.indexOf("if -protected is specified, then") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
    void i18nPKCS11Test() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        //PKCS#11 tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        //   1. sccs edit cert8.db key3.db
41107
567f832618d7 8165161: Solaris: /usr/ccs /opt/sfw and /opt/csw are dead, references should be expunged
alanbur
parents: 39633
diff changeset
  1780
        //Runtime.getRuntime().exec("/usr/bin/sccs edit cert8.db key3.db");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1781
        testOK("", p11Arg + ("-storepass test12 -genkey -alias genkey" +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1782
                " -dname cn=genkey -keysize 512 -keyalg rsa"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
        testOK("", p11Arg + "-storepass test12 -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
        testOK("", p11Arg + "-storepass test12 -list -alias genkey");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1785
        testOK("", p11Arg +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1786
                "-storepass test12 -certreq -alias genkey -file genkey.certreq");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1787
        testOK("", p11Arg +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1788
                "-storepass test12 -export -alias genkey -file genkey.cert");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
        testOK("", "-printcert -file genkey.cert");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1790
        testOK("", p11Arg +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1791
                "-storepass test12 -selfcert -alias genkey -dname cn=selfCert");
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1792
        testOK("", p11Arg +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1793
                "-storepass test12 -list -alias genkey -v");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1794
        assertTrue(out.indexOf("Owner: CN=selfCert") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
        //(check that cert subject DN is [cn=selfCert])
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        testOK("", p11Arg + "-storepass test12 -delete -alias genkey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        testOK("", p11Arg + "-storepass test12 -list");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1798
        assertTrue(out.indexOf("Your keystore contains 0 entries") != -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
        //(check for empty database listing)
41107
567f832618d7 8165161: Solaris: /usr/ccs /opt/sfw and /opt/csw are dead, references should be expunged
alanbur
parents: 39633
diff changeset
  1800
        //Runtime.getRuntime().exec("/usr/bin/sccs unedit cert8.db key3.db");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        remove("genkey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
        remove("genkey.certreq");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
        //  12. sccs unedit cert8.db key3.db
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
    // tesing new option -srcProviderName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
    void sszzTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
        testAnyway("", NSS_P11_ARG+"-delete -alias nss -storepass test12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        testAnyway("", NZZ_P11_ARG+"-delete -alias nss -storepass test12");
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1810
        testOK("", NSS_P11_ARG+"-genkeypair -dname CN=NSS " +
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1811
                "-alias nss -storepass test12");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
        testOK("", NSS_SRC_P11_ARG + NZZ_P11_ARG +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
                "-importkeystore -srcstorepass test12 -deststorepass test12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        testAnyway("", NSS_P11_ARG+"-delete -alias nss -storepass test12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
        testAnyway("", NZZ_P11_ARG+"-delete -alias nss -storepass test12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
    public static void main(String[] args) throws Exception {
10138
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1819
        Locale reservedLocale = Locale.getDefault();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1820
        try {
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1821
            // first test if HumanInputStream really acts like a human being
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1822
            HumanInputStream.test();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1823
            KeyToolTest t = new KeyToolTest();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
10138
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1825
            if (System.getProperty("file") != null) {
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1826
                t.sqeTest();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1827
                t.testAll();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1828
                t.i18nTest();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1829
                t.v3extTest("RSA");
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1830
                t.v3extTest("DSA");
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1831
                boolean testEC = true;
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1832
                try {
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1833
                    KeyPairGenerator.getInstance("EC");
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1834
                } catch (NoSuchAlgorithmException nae) {
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1835
                    testEC = false;
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1836
                }
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1837
                if (testEC) t.v3extTest("EC");
2067
6f9db5f305cd 6780416: New keytool commands/options: -gencert, -printcertreq, -ext
weijun
parents: 2
diff changeset
  1838
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
10138
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1840
            if (System.getProperty("nss") != null) {
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1841
                t.srcP11Arg = NSS_SRC_P11_ARG;
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1842
                t.p11Arg = NSS_P11_ARG;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
10138
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1844
                t.testPKCS11();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1845
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1846
                // FAIL:
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1847
                // 1. we still don't have srcprovidername yet
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1848
                // 2. cannot store privatekey into NSS keystore
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1849
                //    java.security.KeyStoreException: sun.security.pkcs11
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1850
                //      .wrapper.PKCS11Exception: CKR_TEMPLATE_INCOMPLETE.
10138
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1851
                //t.testPKCS11ImportKeyStore();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1852
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1853
                t.i18nPKCS11Test();
29120
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1854
                //FAIL: currently PKCS11-NSS does not support
db85cf043b4f 8073853: KeyToolTest.java has too many too long lines
weijun
parents: 29111
diff changeset
  1855
                // 2 NSS KeyStores to be loaded at the same time
10138
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1856
                //t.sszzTest();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1857
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
10138
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1859
            if (System.getProperty("solaris") != null) {
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1860
                // For Solaris Cryptography Framework
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1861
                t.srcP11Arg = SUN_SRC_P11_ARG;
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1862
                t.p11Arg = SUN_P11_ARG;
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1863
                t.testPKCS11();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1864
                t.testPKCS11ImportKeyStore();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1865
                t.i18nPKCS11Test();
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1866
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
10138
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1868
            System.out.println("Test pass!!!");
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1869
        } finally {
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1870
            // restore the reserved locale
b7572da25d15 7068662: Reserve and restore the default locale
xuelei
parents: 5506
diff changeset
  1871
            Locale.setDefault(reservedLocale);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
class TestException extends Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
    public TestException(String e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
        super(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
 * HumanInputStream tries to act like a human sitting in front of a computer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
 * terminal typing on the keyboard while the keytool program is running.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
 * keytool has called InputStream.read() and BufferedReader.readLine() in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
 * various places. a call to B.readLine() will try to buffer as much input as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
 * possible. Thus, a trivial InputStream will find it impossible to feed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
 * anything to I.read() after a B.readLine() call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
 * This is why i create HumanInputStream, which will only send a single line
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
 * to B.readLine(), no more, no less, and the next I.read() can have a chance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
 * to read the exact character right after "\n".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
 * I don't know why HumanInputStream works.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
class HumanInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
    byte[] src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
    int pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
    int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
    boolean inLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
    int stopIt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
    public HumanInputStream(String input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
        src = input.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
        pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
        length = src.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
        stopIt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        inLine = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
    // the trick: when called through read(byte[], int, int),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
    // return -1 twice after "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
    @Override public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
        int re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
        if(pos < length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
            re = src[pos];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
            if(inLine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
                if(stopIt > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
                    stopIt--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
                    re = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
                    if(re == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
                        stopIt = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
                    pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
                pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
            re = -1;//throw new IOException("NO MORE TO READ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
        //if (re < 32) System.err.printf("[%02d]", re);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
        //else System.err.printf("[%c]", (char)re);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
        return re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
    @Override public int read(byte[] buffer, int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
        inLine = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
            int re = super.read(buffer, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
            return re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
            throw new RuntimeException("HumanInputStream error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
            inLine = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
    @Override public int available() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
        if(pos < length) return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
    // test part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
    static void assertTrue(boolean bool) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
        if(!bool)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
            throw new RuntimeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
    public static void test() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
        class Tester {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
            HumanInputStream is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
            BufferedReader reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            Tester(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                is = new HumanInputStream(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
                reader = new BufferedReader(new InputStreamReader(is));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
            // three kinds of test method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
            // 1. read byte by byte from InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
            void testStreamReadOnce(int expection) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                assertTrue(is.read() == expection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
            void testStreamReadMany(String expection) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
                char[] keys = expection.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
                for(int i=0; i<keys.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
                    assertTrue(is.read() == keys[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
            // 2. read a line with a newly created Reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
            void testReaderReadline(String expection) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                String s = new BufferedReader(new InputStreamReader(is)).readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                if(s == null) assertTrue(expection == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                else assertTrue(s.equals(expection));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
            // 3. read a line with the old Reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
            void testReaderReadline2(String expection) throws Exception  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
                String s = reader.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
                if(s == null) assertTrue(expection == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                else assertTrue(s.equals(expection));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
        Tester test;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
        test = new Tester("111\n222\n\n444\n\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        test.testReaderReadline("111");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        test.testReaderReadline("222");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
        test.testReaderReadline("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        test.testReaderReadline("444");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
        test.testReaderReadline("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        test.testReaderReadline(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
        test = new Tester("111\n222\n\n444\n\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        test.testReaderReadline2("111");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
        test.testReaderReadline2("222");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
        test.testReaderReadline2("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
        test.testReaderReadline2("444");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
        test.testReaderReadline2("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
        test.testReaderReadline2(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
        test = new Tester("111\n222\n\n444\n\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
        test.testReaderReadline2("111");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
        test.testReaderReadline("222");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
        test.testReaderReadline2("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
        test.testReaderReadline2("444");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
        test.testReaderReadline("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
        test.testReaderReadline2(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
        test = new Tester("1\n2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
        test.testStreamReadMany("1\n2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
        test.testStreamReadOnce(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
        test = new Tester("12\n234");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
        test.testStreamReadOnce('1');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
        test.testReaderReadline("2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
        test.testStreamReadOnce('2');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
        test.testReaderReadline2("34");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
        test.testReaderReadline2(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
        test = new Tester("changeit\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
        test.testStreamReadMany("changeit\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        test.testReaderReadline(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
        test = new Tester("changeit\nName\nCountry\nYes\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
        test.testStreamReadMany("changeit\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
        test.testReaderReadline("Name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
        test.testReaderReadline("Country");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
        test.testReaderReadline("Yes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
        test.testReaderReadline(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
        test = new Tester("Me\nHere\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
        test.testReaderReadline2("Me");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
        test.testReaderReadline2("Here");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
}