jdk/test/sun/security/tools/keytool/KeyToolTest.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 2067 6f9db5f305cd
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Testing keytool
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author weijun.wang
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * Run through autotest.sh and manualtest.sh
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Testing non-PKCS11 keystores:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *       echo | java -Dfile KeyToolTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Testing NSS PKCS11 keystores:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *       # testing NSS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *       # make sure the NSS db files are in current directory and writable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *       echo | java -Dnss -Dnss.lib=/path/to/libsoftokn3.so KeyToolTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Testing Solaris Cryptography Framework PKCS11 keystores:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *       # make sure you've already run pktool and set test12 as pin
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *       echo | java -Dsolaris KeyToolTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * ATTENTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Exception in thread "main" java.security.ProviderException: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_KEY_SIZE_RANGE
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.security.KeyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import java.util.MissingResourceException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import sun.security.tools.KeyTool;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import sun.security.x509.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
public class KeyToolTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // The stdout and stderr outputs after a keytool run
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    String out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    String err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // the output of println() in KeyTool.run
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    String ex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    String lastInput = "", lastCommand = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final boolean debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        System.getProperty("debug") != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    static final String NSS_P11_ARG =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            "-keystore NONE -storetype PKCS11 -providerName SunPKCS11-nss -providerClass sun.security.pkcs11.SunPKCS11 -providerArg p11-nss.txt ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static final String NSS_SRC_P11_ARG =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            "-srckeystore NONE -srcstoretype PKCS11 -srcproviderName SunPKCS11-nss -providerClass sun.security.pkcs11.SunPKCS11 -providerArg p11-nss.txt ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    static final String NZZ_P11_ARG =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            "-keystore NONE -storetype PKCS11 -providerName SunPKCS11-nzz -providerClass sun.security.pkcs11.SunPKCS11 -providerArg p11-nzz.txt ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static final String NZZ_SRC_P11_ARG =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            "-srckeystore NONE -srcstoretype PKCS11 -srcproviderName SunPKCS11-nzz -providerClass sun.security.pkcs11.SunPKCS11 -providerArg p11-nzz.txt ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static final String SUN_P11_ARG = "-keystore NONE -storetype PKCS11 ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static final String SUN_SRC_P11_ARG = "-srckeystore NONE -srcstoretype PKCS11 ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    String p11Arg, srcP11Arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /** Creates a new instance of KeyToolTest */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    KeyToolTest() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        // so that there is "Warning" and not translated into other language
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        Locale.setDefault(Locale.US);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Helper, removes a file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    void remove(String filename) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            System.err.println("Removing " + filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        new File(filename).delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (new File(filename).exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            throw new RuntimeException("Error deleting " + filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Run a set of keytool command with given terminal input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param input the terminal inputs, the characters typed by human
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *        if <code>cmd</code> is running on a terminal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param cmd the argument of a keytool command line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * @throws if keytool goes wrong in some place
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    void test(String input, String cmd) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        lastInput = input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        lastCommand = cmd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        // "X" is appened so that we can precisely test how input is consumed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        HumanInputStream in = new HumanInputStream(input+"X");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        test(in, cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // make sure the input string is no more no less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if(in.read() != 'X' || in.read() != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            throw new Exception("Input not consumed exactly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    void test(InputStream in, String cmd) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        // save the original 3 streams
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            System.err.println(cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            System.err.print(".");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        PrintStream p1 = System.out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        PrintStream p2 = System.err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        InputStream i1 = System.in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        ByteArrayOutputStream b1 = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        ByteArrayOutputStream b2 = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            System.setIn(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            System.setOut(new PrintStream(b1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            System.setErr(new PrintStream(b2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            // since System.in is overrided, the KeyTool.main() method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            // never block at user input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            // use -debug so that KeyTool.main() will throw an Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            // instead of calling System.exit()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            KeyTool.main(("-debug "+cmd).split("\\s+"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            out = b1.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            err = b2.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            ex = out;   // now it goes to System.out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            System.setIn(i1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            System.setOut(p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            System.setErr(p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * Call this method if you expect test(input, cmd) should go OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    void testOK(String input, String cmd) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            test(input, cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            afterFail(input, cmd, "OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Call this method if you expect test(input, cmd) should fail and throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    void testFail(String input, String cmd) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        boolean ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            test(input, cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            if (e instanceof MissingResourceException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if(ok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            afterFail(input, cmd, "FAIL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            throw new RuntimeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Call this method if you expect test(input, cmd) should go OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    void testOK(InputStream is, String cmd) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            test(is, cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            afterFail("", cmd, "OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            throw e;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Call this method if you expect test(input, cmd) should fail and throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    void testFail(InputStream is, String cmd) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        boolean ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            test(is, cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            ok = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if(ok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            afterFail("", cmd, "FAIL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            throw new RuntimeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Call this method if you just want to run the command and does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * not care if it succeeds or fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    void testAnyway(String input, String cmd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            test(input, cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Helper method, print some output after a test does not do as expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    void afterFail(String input, String cmd, String should) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        System.err.println("\nTest fails for the command ---\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                "keytool " + cmd + "\nOr its debug version ---\n" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                "keytool -debug " + cmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        System.err.println("The command result should be " + should +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                ", but it's not. Try run the command manually and type" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                " these input into it: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        char[] inputChars = input.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        for (int i=0; i<inputChars.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            char ch = inputChars[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if (ch == '\n') System.err.print("ENTER ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            else if (ch == ' ') System.err.print("SPACE ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            else System.err.print(ch + " ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        System.err.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        System.err.println("ERR is:\n"+err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        System.err.println("OUT is:\n"+out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    void assertTrue(boolean bool, String msg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if(!bool) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            afterFail(lastInput, lastCommand, "TRUE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            throw new RuntimeException(msg);
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
     * Helper method, load a keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * @param file file for keystore, null or "NONE" for PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * @pass password for the keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @type keystore type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @returns the KeyStore object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @exception Exception if anything goes wrong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    KeyStore loadStore(String file, String pass, String type) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        KeyStore ks = KeyStore.getInstance(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        FileInputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (file != null && !file.equals("NONE")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            is = new FileInputStream(file);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        ks.load(is, pass.toCharArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        return ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * The test suite.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * Maybe it's better to put this outside the KeyToolTest class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    void testAll() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        remove("x.jceks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        remove("x.p12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        remove("x2.jceks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        remove("x2.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        remove("x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        // name changes: genkeypair, importcert, exportcert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        remove("x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -alias p1 -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        testOK("", "-keystore x.jks -storepass changeit -exportcert -alias p1 -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        assertTrue(ks.getKey("p1", "changeit".toCharArray()) != null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            "key not DSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        assertTrue(new File("x.jks.p1.cert").exists(), "p1 export err");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        testOK("", "-keystore x.jks -storepass changeit -delete -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        testOK("y\n", "-keystore x.jks -storepass changeit -importcert -alias c1 -file x.jks.p1.cert");  // importcert, prompt for Yes/No
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        testOK("", "-keystore x.jks -storepass changeit -importcert -alias c2 -file x.jks.p1.cert -noprompt"); // importcert, -noprompt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        assertTrue(ks.getCertificate("c1") != null, "import c1 err");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        // v3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        byte[] encoded = ks.getCertificate("c1").getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        X509CertImpl certImpl = new X509CertImpl(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        assertTrue(certImpl.getVersion() == 3, "Version is not 3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        // changealias and keyclone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -alias p1 -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        testOK("changeit\n", "-keystore x.jks -changealias -alias p1 -destalias p11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        testOK("changeit\n", "-keystore x.jks -changealias -alias c1 -destalias c11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        testOK("changeit\n\n", "-keystore x.jks -keyclone -alias p11 -destalias p111"); // press ENTER when prompt for p111's keypass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        assertTrue(!ks.containsAlias("p1"), "there is no p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        assertTrue(!ks.containsAlias("c1"), "there is no c1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        assertTrue(ks.containsAlias("p11"), "there is p11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        assertTrue(ks.containsAlias("c11"), "there is c11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        assertTrue(ks.containsAlias("p111"), "there is p111");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        // genSecKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        remove("x.jceks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        testOK("changeit\nchangeit\n\n", "-keystore x.jceks -storetype JCEKS -genseckey -alias s1"); // DES, no need keysize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        testFail("changeit\n\n", "-keystore x.jceks -storetype JCEKS -genseckey -alias s11 -keysize 128"); // DES, keysize cannot be 128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        testOK("changeit\n\n", "-keystore x.jceks -storetype JCEKS -genseckey -keyalg DESede -alias s2"); // DESede. no need keysize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        testFail("changeit\n\n", "-keystore x.jceks -storetype AES -genseckey -keyalg Rijndael -alias s3"); // AES, need keysize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        testOK("changeit\n\n", "-keystore x.jceks -storetype JCEKS -genseckey -keyalg AES -alias s3 -keysize 128");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                // about keypass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        testOK("\n", "-keystore x.jceks -storetype JCEKS -storepass changeit -genseckey -alias s4"); // can accept storepass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        testOK("keypass\nkeypass\n", "-keystore x.jceks -storetype JCEKS -storepass changeit -genseckey -alias s5"); // or a new one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        testOK("bad\n\bad\nkeypass\nkeypass\n", "-keystore x.jceks -storetype JCEKS -storepass changeit -genseckey -alias s6"); // keypass must be valid (prompt 3 times)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        testFail("bad\n\bad\nbad\n", "-keystore x.jceks -storetype JCEKS -storepass changeit -genseckey -alias s7"); // keypass must be valid (prompt 3 times)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        testFail("bad\n\bad\nbad\nkeypass\n", "-keystore x.jceks -storetype JCEKS -storepass changeit -genseckey -alias s7"); // keypass must be valid (prompt 3 times)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        ks = loadStore("x.jceks", "changeit", "JCEKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        assertTrue(ks.getKey("s1", "changeit".toCharArray()).getAlgorithm().equalsIgnoreCase("DES"), "s1 is DES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        assertTrue(ks.getKey("s1", "changeit".toCharArray()).getEncoded().length == 8,  "DES is 56");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        assertTrue(ks.getKey("s2", "changeit".toCharArray()).getEncoded().length == 24,  "DESede is 168");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        assertTrue(ks.getKey("s2", "changeit".toCharArray()).getAlgorithm().equalsIgnoreCase("DESede"), "s2 is DESede");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        assertTrue(ks.getKey("s3", "changeit".toCharArray()).getAlgorithm().equalsIgnoreCase("AES"), "s3 is AES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        assertTrue(ks.getKey("s4", "changeit".toCharArray()).getAlgorithm().equalsIgnoreCase("DES"), "s4 is DES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        assertTrue(ks.getKey("s5", "keypass".toCharArray()).getAlgorithm().equalsIgnoreCase("DES"), "s5 is DES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        assertTrue(ks.getKey("s6", "keypass".toCharArray()).getAlgorithm().equalsIgnoreCase("DES"), "s6 is DES");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        assertTrue(!ks.containsAlias("s7"), "s7 not created");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        // maybe we needn't test this, one day JKS will support SecretKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        //testFail("changeit\nchangeit\n", "-keystore x.jks -genseckey -keyalg AES -alias s3 -keysize 128");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        // importKeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        remove("x.jceks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        testOK("changeit\nchangeit\n\n", "-keystore x.jceks -storetype JCEKS -genkeypair -alias p1 -dname CN=Olala"); // create 2 entries...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        testOK("", "-keystore x.jceks -storetype JCEKS -storepass changeit -importcert -alias c1 -file x.jks.p1.cert -noprompt"); // ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        ks = loadStore("x.jceks", "changeit", "JCEKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        assertTrue(ks.size() == 2, "2 entries in JCEKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        // import, shouldn't mention destalias/srckeypass/destkeypass if srcalias is no given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        testFail("changeit\nchangeit\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -destalias pp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        testFail("changeit\nchangeit\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srckeypass changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        testFail("changeit\nchangeit\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -destkeypass changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        // normal import
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        testOK("changeit\nchangeit\nchangeit\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        assertTrue(ks.size() == 2, "2 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        // import again, type yes to overwrite old entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        testOK("changeit\nchangeit\ny\ny\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        // import again, specify -nopromt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        testOK("changeit\nchangeit\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -noprompt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        assertTrue(err.indexOf("Warning") != -1, "noprompt will warn");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        assertTrue(ks.size() == 2, "2 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        // import again, type into new aliases when prompted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        testOK("changeit\nchangeit\n\ns1\n\ns2\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        assertTrue(ks.size() == 4, "4 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        // importkeystore single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        testOK("changeit\nchangeit\nchangeit\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias p1"); // normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        assertTrue(ks.size() == 1, "1 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        testOK("changeit\nchangeit\ny\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias p1"); // overwrite
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        assertTrue(ks.size() == 1, "1 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        testOK("changeit\nchangeit\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias p1 -noprompt"); // noprompt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        assertTrue(ks.size() == 1, "1 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        testOK("changeit\nchangeit\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias p1 -destalias p2"); // rename
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        assertTrue(ks.size() == 2, "2 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        testOK("changeit\nchangeit\n\nnewalias\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias p1"); // another rename
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        assertTrue(ks.size() == 3, "3 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        // importkeystore single, different keypass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        testOK("changeit\nkeypass\nkeypass\n", "-keystore x.jceks -storetype JCEKS -genkeypair -alias p2 -dname CN=Olala"); // generate entry with different keypass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        testOK("changeit\nchangeit\nchangeit\nkeypass\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias p2"); // prompt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        assertTrue(ks.size() == 1, "1 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        testOK("changeit\nchangeit\nkeypass\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias p2 -destalias p3 -destkeypass keypass2"); // diff destkeypass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        assertTrue(ks.size() == 2, "2 entries in JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        assertTrue(ks.getKey("p2", "keypass".toCharArray()) != null, "p2 has old password");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        assertTrue(ks.getKey("p3", "keypass2".toCharArray()) != null, "p3 has new password");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        // importkeystore single, cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        testOK("changeit\nchangeit\nchangeit\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias c1"); // normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        testOK("changeit\n\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias c1 -destalias c2");   // in fact srcstorepass can be ignored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        assertTrue(err.indexOf("WARNING") != -1, "But will warn");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        testOK("changeit\n\ny\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias c1 -destalias c2");   // 2nd import, press y to overwrite ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        testOK("changeit\n\n\nc3\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias c1 -destalias c2");   // ... or rename
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        assertTrue(ks.size() == 3, "3 entries in JKS"); // c1, c2, c3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        // importkeystore, secretkey
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        testOK("changeit\n\n", "-keystore x.jceks -storetype JCEKS -genseckey -alias s1"); // create SecretKeyEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        testOK("changeit\n\n", "-keystore x.jceks -storetype JCEKS -genseckey -alias s2"); // create SecretKeyEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        testOK("changeit\n", "-keystore x.jceks -storetype JCEKS -delete -alias p2"); // remove the keypass!=storepass one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        ks = loadStore("x.jceks", "changeit", "JCEKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        assertTrue(ks.size() == 4, "4 entries in JCEKS");       // p1, c1, s1, s2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        testOK("changeit\nchangeit\nchangeit\n", "-importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS -srcalias s1"); // normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        assertTrue(err.indexOf("not imported") != -1, "Not imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        assertTrue(err.indexOf("Cannot store non-PrivateKeys") != -1, "Not imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        testOK("\n\n", "-srcstorepass changeit -deststorepass changeit -importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS"); // normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        assertTrue(err.indexOf("s1 not") != -1, "s1 not");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        assertTrue(err.indexOf("s2 not") != -1, "s2 not");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        assertTrue(err.indexOf("c1 success") != -1, "c1 success");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        assertTrue(err.indexOf("p1 success") != -1, "p1 success");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        testOK("yes\n", "-srcstorepass changeit -deststorepass changeit -importkeystore -srckeystore x.jceks -srcstoretype JCEKS -destkeystore x.jks -deststoretype JKS"); // normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        // maybe c1 or p1 has been imported before s1 or s2 is touched, anyway we know yesNo is only asked once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        // pkcs12
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        testFail("changeit\nchangeit\n", "-keystore x.jks -genkeypair -alias p1 -dname CN=olala"); // JKS prompt for keypass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        testOK("changeit\nchangeit\n\n", "-keystore x.jks -genkeypair -alias p1 -dname CN=olala"); // just type ENTER means keypass=storepass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        remove("x.p12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        testOK("", "-keystore x.p12 -storetype PKCS12 -storepass changeit -genkeypair -alias p0 -dname CN=olala"); // PKCS12 only need storepass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        testOK("changeit\n", "-keystore x.p12 -storetype PKCS12 -genkeypair -alias p1 -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        testOK("changeit\n", "-keystore x.p12 -keypass changeit -storetype PKCS12 -genkeypair -alias p3 -dname CN=olala"); // when specify keypass, make sure keypass==storepass...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        assertTrue(err.indexOf("Warning") == -1, "PKCS12 silent when keypass == storepass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        testOK("changeit\n", "-keystore x.p12 -keypass another -storetype PKCS12 -genkeypair -alias p2 -dname CN=olala"); // otherwise, print a warning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        assertTrue(err.indexOf("Warning") != -1, "PKCS12 warning when keypass != storepass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        testFail("", "-keystore x.p12 -storepass changeit -storetype PKCS12 -keypasswd -new changeit -alias p3"); // no -keypasswd for PKCS12
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        testOK("", "-keystore x.p12 -storepass changeit -storetype PKCS12 -changealias -alias p3 -destalias p33");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        testOK("", "-keystore x.p12 -storepass changeit -storetype PKCS12 -keyclone -alias p33 -destalias p3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        // pkcs12
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        remove("x.p12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        testOK("", "-keystore x.p12 -storetype PKCS12 -storepass changeit -genkeypair -alias p0 -dname CN=olala"); // PKCS12 only need storepass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        testOK("", "-storepass changeit -keystore x.p12 -storetype PKCS12 -genkeypair -alias p1 -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        testOK("", "-storepass changeit -keystore x.p12 -keypass changeit -storetype PKCS12 -genkeypair -alias p3 -dname CN=olala"); // when specify keypass, make sure keypass==storepass...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        assertTrue(err.indexOf("Warning") == -1, "PKCS12 silent when keypass == storepass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        testOK("", "-storepass changeit -keystore x.p12 -keypass another -storetype PKCS12 -genkeypair -alias p2 -dname CN=olala"); // otherwise, print a warning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        assertTrue(err.indexOf("Warning") != -1, "PKCS12 warning when keypass != storepass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        remove("x.jceks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        remove("x.p12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        remove("x2.jceks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        remove("x2.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        remove("x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    void testPKCS11() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        // pkcs11, the password maybe different and maybe PKCS11 is not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        // in case last test is not executed successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        testAnyway("", p11Arg + "-storepass test12 -delete -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        testAnyway("", p11Arg + "-storepass test12 -delete -alias p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        testAnyway("", p11Arg + "-storepass test12 -delete -alias p3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        testAnyway("", p11Arg + "-storepass test12 -delete -alias nss");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        testOK("", p11Arg + "-storepass test12 -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        assertTrue(out.indexOf("Your keystore contains 0 entries") != -1, "*** MAKE SURE YOU HAVE NO ENTRIES IN YOUR PKCS11 KEYSTORE BEFORE THIS TEST ***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        testOK("", p11Arg + "-storepass test12 -genkeypair -alias p1 -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        testOK("test12\n", p11Arg + "-genkeypair -alias p2 -dname CN=olala2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        testFail("test12\n", p11Arg + "-keypass test12 -genkeypair -alias p3 -dname CN=olala3"); // cannot provide keypass for PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        testFail("test12\n", p11Arg + "-keypass nonsense -genkeypair -alias p3 -dname CN=olala3"); // cannot provide keypass for PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        testOK("", p11Arg + "-storepass test12 -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        assertTrue(out.indexOf("Your keystore contains 2 entries") != -1, "2 entries in p11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        testOK("test12\n", p11Arg + "-alias p1 -changealias -destalias p3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
        testOK("", p11Arg + "-storepass test12 -list -alias p3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        testFail("", p11Arg + "-storepass test12 -list -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        testOK("test12\n", p11Arg + "-alias p3 -keyclone -destalias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        testFail("", p11Arg + "-storepass test12 -list -alias p3");   // in PKCS11, keyclone will delete old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        testOK("", p11Arg + "-storepass test12 -list -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        testFail("test12\n", p11Arg + "-alias p1 -keypasswd -new another"); // cannot change password for PKCS11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        testOK("", p11Arg + "-storepass test12 -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        assertTrue(out.indexOf("Your keystore contains 2 entries") != -1, "2 entries in p11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        testOK("", p11Arg + "-storepass test12 -delete -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        testOK("", p11Arg + "-storepass test12 -delete -alias p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        testOK("", p11Arg + "-storepass test12 -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        assertTrue(out.indexOf("Your keystore contains 0 entries") != -1, "*** MAKE SURE YOU HAVE NO ENTRIES IN YOUR PKCS11 KEYSTORE BEFORE THIS TEST ***");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    void testPKCS11ImportKeyStore() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        testOK("", p11Arg + "-storepass test12 -genkeypair -alias p1 -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        testOK("test12\n", p11Arg + "-genkeypair -alias p2 -dname CN=olala2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        // test importkeystore for pkcs11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        // pkcs11 -> jks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        testOK("changeit\nchangeit\ntest12\n", srcP11Arg + "-importkeystore -destkeystore x.jks -deststoretype JKS -srcalias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        assertTrue(err.indexOf("not imported") != -1, "cannot import key without destkeypass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        assertTrue(!ks.containsAlias("p1"), "p1 is not imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        testOK("changeit\ntest12\n", srcP11Arg + "-importkeystore -destkeystore x.jks -deststoretype JKS -srcalias p1 -destkeypass changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        testOK("changeit\ntest12\n", srcP11Arg + "-importkeystore -destkeystore x.jks -deststoretype JKS -srcalias p2 -destkeypass changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        assertTrue(ks.containsAlias("p1"), "p1 is imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        assertTrue(ks.containsAlias("p2"), "p2 is imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        // jks -> pkcs11
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        testOK("", p11Arg + "-storepass test12 -delete -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        testOK("", p11Arg + "-storepass test12 -delete -alias p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        testOK("test12\nchangeit\n", p11Arg + "-importkeystore -srckeystore x.jks -srcstoretype JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        testOK("", p11Arg + "-storepass test12 -list -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        testOK("", p11Arg + "-storepass test12 -list -alias p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        testOK("", p11Arg + "-storepass test12 -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        assertTrue(out.indexOf("Your keystore contains 2 entries") != -1, "2 entries in p11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        // clean up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        testOK("", p11Arg + "-storepass test12 -delete -alias p1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        testOK("", p11Arg + "-storepass test12 -delete -alias p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        testOK("", p11Arg + "-storepass test12 -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        assertTrue(out.indexOf("Your keystore contains 0 entries") != -1, "empty p11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
    // The sqeTest reflects the test suggested by judy.gao and bill.situ at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    // /net/sqesvr-nfs/global/nfs/sec/ws_6.0_int/security/src/SecurityTools/Keytool
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    void sqeTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        FileOutputStream fos = new FileOutputStream("badkeystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        for (int i=0; i<100; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            fos.write(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        fos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        sqeCsrTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        sqePrintcertTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        sqeDeleteTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        sqeExportTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        sqeGenkeyTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        sqeImportTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        sqeKeyclonetest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        sqeKeypasswdTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        sqeListTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        sqeSelfCertTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        sqeStorepassTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        remove("badkeystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    // Import: cacert, prompt, trusted, non-trusted, bad chain, not match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    void sqeImportTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        KeyStore ks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        testOK("", "-keystore x.jks -storepass changeit -exportcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        /* deleted */ testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        testOK("", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert -noprompt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        /* deleted */ testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        testOK("yes\n", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        assertTrue(ks.containsAlias("mykey"), "imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        /* deleted */ testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        testOK("\n", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        assertTrue(!ks.containsAlias("mykey"), "imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        testOK("no\n", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        ks = loadStore("x.jks", "changeit", "JKS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        assertTrue(!ks.containsAlias("mykey"), "imported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        testFail("no\n", "-keystore x.jks -storepass changeit -importcert -file nonexist");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        testFail("no\n", "-keystore x.jks -storepass changeit -importcert -file x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    // keyclone: exist. nonexist err, cert err, dest exist, misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    void sqeKeyclonetest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -new newpass -keyclone -dest p0"); // new pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        testOK("\n", "-keystore x.jks -storepass changeit -keypass changeit -keyclone -dest p1"); // new pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        testOK("\n", "-keystore x.jks -storepass changeit -keyclone -dest p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        testFail("\n", "-keystore x.jks -storepass changeit -keyclone -dest p2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        testFail("\n", "-keystore x.jks -storepass changeit -keyclone -dest p3 -alias noexist");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        // no cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        testOK("", "-keystore x.jks -storepass changeit -exportcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        testOK("", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert -noprompt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -new newpass -keyclone -dest p0"); // new pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    // keypasswd: exist, short, nonexist err, cert err, misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    void sqeKeypasswdTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        /*change back*/ testOK("", "-keystore x.jks -storepass changeit -keypass newpass -keypasswd -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        testOK("newpass\nnewpass\n", "-keystore x.jks -storepass changeit -keypass changeit -keypasswd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        /*change back*/ testOK("", "-keystore x.jks -storepass changeit -keypass newpass -keypasswd -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        testOK("new\nnew\nnewpass\nnewpass\n", "-keystore x.jks -storepass changeit -keypass changeit -keypasswd");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        /*change back*/ testOK("", "-keystore x.jks -storepass changeit -keypass newpass -keypasswd -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        testOK("", "-keystore x.jks -storepass changeit -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        /*change back*/ testOK("", "-keystore x.jks -storepass changeit -keypass newpass -keypasswd -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        testOK("changeit\n", "-keystore x.jks -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        /*change back*/ testOK("", "-keystore x.jks -storepass changeit -keypass newpass -keypasswd -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        testFail("", "-keystore x.jks -storepass badpass -keypass changeit -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        testFail("", "-keystore x.jks -storepass changeit -keypass bad -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        // no cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        testOK("", "-keystore x.jks -storepass changeit -exportcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        testOK("", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert -noprompt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        // diff pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        testOK("", "-keystore x.jks -storepass changeit -keypass keypass -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        testFail("", "-keystore x.jks -storepass changeit -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        testOK("keypass\n", "-keystore x.jks -storepass changeit -keypasswd -new newpass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        // i hate those misc test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    // list: -f -alias, exist, nonexist err; otherwise, check all shows, -rfc shows more, and misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    void sqeListTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        testOK("", "-keystore x.jks -storepass changeit -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        testOK("", "-keystore x.jks -storepass changeit -list -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        testFail("", "-keystore x.jks -storepass changeit -list -alias notexist");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        testFail("", "-keystore x.jks -storepass badpass -list -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        testOK("", "-keystore x.jks -storepass changeit -keypass badpass -list -alias mykey");  // keypass ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        testOK("\n", "-keystore x.jks -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        assertTrue(err.indexOf("WARNING") != -1, "no storepass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        testOK("changeit\n", "-keystore x.jks -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        assertTrue(err.indexOf("WARNING") == -1, "has storepass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        testFail("badpass\n", "-keystore x.jks -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        // misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        testFail("", "-keystore aa\\bb//cc -storepass changeit -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        testFail("", "-keystore nonexisting -storepass changeit -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        testFail("", "-keystore badkeystore -storepass changeit -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    // selfcert: exist, non-exist err, cert err, sig..., dname, wrong keypass, misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    void sqeSelfCertTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        testOK("", "-keystore x.jks -storepass changeit -selfcert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -selfcert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -selfcert -alias nonexisting"); // not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -selfcert -dname CN=NewName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -selfcert -sigalg MD5withRSA"); // sig not compatible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        testFail("", "-keystore x.jks -storepass wrong -keypass changeit -selfcert"); // bad pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        testFail("", "-keystore x.jks -storepass changeit -keypass wrong -selfcert"); // bad pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        //misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
        testFail("", "-keystore nonexist -storepass changeit -keypass changeit -selfcert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        testFail("", "-keystore aa//dd\\gg -storepass changeit -keypass changeit -selfcert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        // diff pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        testOK("", "-keystore x.jks -storepass changeit -keypass keypass -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        testFail("", "-keystore x.jks -storepass changeit -selfcert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
        testOK("keypass\n", "-keystore x.jks -storepass changeit -selfcert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        testOK("", "-keystore x.jks -storepass changeit -exportcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        testOK("", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert -noprompt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        testFail("", "-keystore x.jks -storepass changeit -selfcert");  // certentry cannot do selfcert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    // storepass: bad old, short new, misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    void sqeStorepassTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        testOK("", "-storepasswd -keystore x.jks -storepass changeit -new newstore"); // all in arg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        /* Change back */ testOK("", "-storepasswd -keystore x.jks -storepass newstore -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        testOK("changeit\nnewstore\nnewstore\n", "-storepasswd -keystore x.jks"); // all not in arg, new twice
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        /* Change back */ testOK("", "-storepasswd -keystore x.jks -storepass newstore -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        testOK("changeit\n", "-storepasswd -keystore x.jks -new newstore"); // new in arg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        /* Change back */ testOK("", "-storepasswd -keystore x.jks -storepass newstore -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        testOK("newstore\nnewstore\n", "-storepasswd -keystore x.jks -storepass changeit"); // old in arg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        /* Change back */ testOK("", "-storepasswd -keystore x.jks -storepass newstore -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        testOK("new\nnew\nnewstore\nnewstore\n", "-storepasswd -keystore x.jks -storepass changeit"); // old in arg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        /* Change back */ testOK("", "-storepasswd -keystore x.jks -storepass newstore -new changeit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        testFail("", "-storepasswd -keystore x.jks -storepass badold -new newstore"); // bad old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        testFail("", "-storepasswd -keystore x.jks -storepass changeit -new new"); // short new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        // misc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        testFail("", "-storepasswd -keystore nonexist -storepass changeit -new newstore"); // non exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        testFail("", "-storepasswd -keystore badkeystore -storepass changeit -new newstore"); // bad file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        testFail("", "-storepasswd -keystore aa\\bb//cc//dd -storepass changeit -new newstore"); // bad file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    void sqeGenkeyTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -alias newentry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -alias newentry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keyalg DSA -alias n1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keyalg RSA -alias n2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keyalg NoSuchAlg -alias n3");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keysize 56 -alias n4");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keysize 999 -alias n5");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keysize 512 -alias n6");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keysize 1024 -alias n7");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -sigalg NoSuchAlg -alias n8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keyalg RSA -sigalg MD2withRSA -alias n9");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keyalg RSA -sigalg MD5withRSA -alias n10");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keyalg RSA -sigalg SHA1withRSA -alias n11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        testFail("", "-keystore aa\\bb//cc\\dd -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keyalg RSA -sigalg NoSuchAlg -alias n12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        testFail("", "-keystore badkeystore -storepass changeit -keypass changeit -genkeypair -dname CN=olala -alias n14");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        testFail("", "-keystore x.jks -storepass badpass -keypass changeit -genkeypair -dname CN=olala -alias n16");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        testFail("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CNN=olala -alias n17");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    void sqeExportTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        testFail("", "-keystore x.jks -storepass changeit -export -file mykey.cert -alias mykey"); // nonexist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        testOK("", "-keystore x.jks -storepass changeit -export -file mykey.cert -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        testOK("", "-keystore x.jks -storepass changeit -import -file mykey.cert -noprompt -alias c1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        testOK("", "-keystore x.jks -storepass changeit -export -file mykey.cert2 -alias c1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        testFail("", "-keystore aa\\bb//cc\\dd -storepass changeit -export -file mykey.cert2 -alias c1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        testFail("", "-keystore nonexistkeystore -storepass changeit -export -file mykey.cert2 -alias c1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        testFail("", "-keystore badkeystore -storepass changeit -export -file mykey.cert2 -alias c1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        testFail("", "-keystore x.jks -storepass badpass -export -file mykey.cert2 -alias c1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        remove("mykey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        remove("mykey.cert2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    void sqeDeleteTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        testFail("", "-keystore x.jks -storepass changeit -delete -alias mykey"); // nonexist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        testFail("", "-keystore aa\\bb//cc\\dd -storepass changeit -delete -alias mykey"); // keystore name illegal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        testFail("", "-keystore nonexistkeystore -storepass changeit -delete -alias mykey"); // keystore not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        testFail("", "-keystore badkeystore -storepass changeit -delete -alias mykey"); // keystore invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        testFail("", "-keystore x.jks -storepass xxxxxxxx -delete -alias mykey"); // wrong pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    void sqeCsrTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        remove("x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        remove("csr1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        // PrivateKeyEntry can do certreq
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        testOK("", "-keystore x.jks -storepass changeit -certreq -file csr1 -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        testOK("", "-keystore x.jks -storepass changeit -certreq -file csr1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        testOK("", "-keystore x.jks -storepass changeit -certreq -file csr1 -sigalg SHA1withDSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        testFail("", "-keystore x.jks -storepass changeit -certreq -file csr1 -sigalg MD5withRSA"); // unmatched sigalg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        // misc test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        testFail("", "-keystore x.jks -storepass badstorepass -certreq -file csr1"); // bad storepass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        testOK("changeit\n", "-keystore x.jks -certreq -file csr1"); // storepass from terminal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        testFail("\n", "-keystore x.jks -certreq -file csr1"); // must provide storepass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        testFail("", "-keystore x.jks -storepass changeit -keypass badkeypass -certreq -file csr1"); // bad keypass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        testFail("", "-keystore x.jks -storepass changeit -certreq -file aa\\bb//cc\\dd");  // bad filepath
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        testFail("", "-keystore noexistks -storepass changeit -certreq -file csr1"); // non-existing keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        // Try the RSA private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala -keyalg RSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        testOK("", "-keystore x.jks -storepass changeit -certreq -file csr1 -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        testOK("", "-keystore x.jks -storepass changeit -certreq -file csr1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        testFail("", "-keystore x.jks -storepass changeit -certreq -file csr1 -sigalg SHA1withDSA"); // unmatched sigalg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        testOK("", "-keystore x.jks -storepass changeit -certreq -file csr1 -sigalg MD5withRSA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        // TrustedCertificateEntry cannot do certreq
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        testOK("", "-keystore x.jks -storepass changeit -exportcert -file x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        testOK("", "-keystore x.jks -storepass changeit -delete -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        testOK("", "-keystore x.jks -storepass changeit -importcert -file x.jks.p1.cert -noprompt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        testFail("", "-keystore x.jks -storepass changeit -certreq -file csr1 -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        testFail("", "-keystore x.jks -storepass changeit -certreq -file csr1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        remove("x.jks.p1.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        remove("csr1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    void sqePrintcertTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        remove("mykey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        testOK("", "-keystore x.jks -storepass changeit -keypass changeit -genkeypair -dname CN=olala");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        testOK("", "-keystore x.jks -storepass changeit -export -file mykey.cert -alias mykey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        testFail("", "-printcert -file badkeystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        testFail("", "-printcert -file a/b/c/d");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        testOK("", "-printcert -file mykey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        FileInputStream fin = new FileInputStream("mykey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        testOK(fin, "-printcert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        fin.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        remove("mykey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    void i18nTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        //   1.  keytool -help
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            test("", "-help");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            assertTrue(false, "Cannot come here");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        } catch(RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            assertTrue(e.getMessage().indexOf("NO ERROR, SORRY") != -1, "No error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        //   2. keytool -genkey -v -keysize 512 Enter "a" for the keystore password. Check error (password too short). Enter "password" for the keystore password. Hit 'return' for "first and last name", "organizational unit", "City", "State", and "Country Code". Type "yes" when they ask you if everything is correct. Type 'return' for new key password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        testOK("a\npassword\npassword\nMe\nHere\nNow\nPlace\nPlace\nUS\nyes\n\n", "-genkey -v -keysize 512 -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        //   3. keytool -list -v -storepass password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        testOK("", "-list -v -storepass password -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        //   4. keytool -list -v Type "a" for the keystore password. Check error (wrong keystore password).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        testFail("a\n", "-list -v -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        assertTrue(ex.indexOf("password was incorrect") != -1, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        //   5. keytool -genkey -v -keysize 512 Enter "password" as the password. Check error (alias 'mykey' already exists).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        testFail("password\n", "-genkey -v -keysize 512 -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        assertTrue(ex.indexOf("alias <mykey> already exists") != -1, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        //   6. keytool -genkey -v -keysize 512 -alias mykey2 -storepass password Hit 'return' for "first and last name", "organizational unit", "City", "State", and "Country Code". Type "yes" when they ask you if everything is correct. Type 'return' for new key password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        testOK("\n\n\n\n\n\nyes\n\n", "-genkey -v -keysize 512 -alias mykey2 -storepass password -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        //   7. keytool -list -v Type 'password' for the store password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        testOK("password\n", "-list -v -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        //   8. keytool -keypasswd -v -alias mykey2 -storepass password Type "a" for the new key password. Type "aaaaaa" for the new key password. Type "bbbbbb" when re-entering the new key password. Type "a" for the new key password. Check Error (too many failures).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        testFail("a\naaaaaa\nbbbbbb\na\n", "-keypasswd -v -alias mykey2 -storepass password -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        assertTrue(ex.indexOf("Too many failures - try later") != -1, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        //   9. keytool -keypasswd -v -alias mykey2 -storepass password Type "aaaaaa" for the new key password. Type "aaaaaa" when re-entering the new key password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        testOK("aaaaaa\naaaaaa\n", "-keypasswd -v -alias mykey2 -storepass password -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        //  10. keytool -selfcert -v -alias mykey -storepass password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        testOK("", "-selfcert -v -alias mykey -storepass password -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        //  11. keytool -list -v -storepass password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        testOK("", "-list -v -storepass password -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        //  12. keytool -export -v -alias mykey -file cert -storepass password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        remove("cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        testOK("", "-export -v -alias mykey -file cert -storepass password -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        //  13. keytool -import -v -file cert -storepass password Check error (Certificate reply and cert are the same)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        testFail("", "-import -v -file cert -storepass password -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        assertTrue(ex.indexOf("Certificate reply and certificate in keystore are identical") != -1, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        //  14. keytool -printcert -file cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        testOK("", "-printcert -file cert -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        remove("cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        //  15. keytool -list -storepass password -provider sun.security.provider.Sun
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        testOK("", "-list -storepass password -provider sun.security.provider.Sun -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        //Error tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        //   1. keytool -storepasswd -storepass password -new abc Check error (password too short)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        testFail("", "-storepasswd -storepass password -new abc");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        assertTrue(ex.indexOf("New password must be at least 6 characters") != -1, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        // Changed, no NONE needed now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        //   2. keytool -list -storetype PKCS11 Check error (-keystore must be NONE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        //testFail("", "-list -storetype PKCS11");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        //assertTrue(err.indexOf("keystore must be NONE") != -1, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        //   3. keytool -storepasswd -storetype PKCS11 -keystore NONE Check error (unsupported operation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        testFail("", "-storepasswd -storetype PKCS11 -keystore NONE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
        assertTrue(ex.indexOf("UnsupportedOperationException") != -1, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        //   4. keytool -keypasswd -storetype PKCS11 -keystore NONE Check error (unsupported operation)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        testFail("", "-keypasswd -storetype PKCS11 -keystore NONE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        assertTrue(ex.indexOf("UnsupportedOperationException") != -1, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        //   5. keytool -list -protected -storepass password Check error (password can not be specified with -protected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        testFail("", "-list -protected -storepass password -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        assertTrue(ex.indexOf("if -protected is specified, then") != -1, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        //   6. keytool -keypasswd -protected -keypass password Check error (password can not be specified with -protected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        testFail("", "-keypasswd -protected -keypass password -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        assertTrue(ex.indexOf("if -protected is specified, then") != -1, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        //   7. keytool -keypasswd -protected -new password Check error (password can not be specified with -protected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        testFail("", "-keypasswd -protected -new password -keystore x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        assertTrue(ex.indexOf("if -protected is specified, then") != -1, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        remove("x.jks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    void i18nPKCS11Test() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        //PKCS#11 tests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        //   1. sccs edit cert8.db key3.db
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        //Runtime.getRuntime().exec("/usr/ccs/bin/sccs edit cert8.db key3.db");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        testOK("", p11Arg + "-storepass test12 -genkey -alias genkey -dname cn=genkey -keysize 512 -keyalg rsa");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        testOK("", p11Arg + "-storepass test12 -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        testOK("", p11Arg + "-storepass test12 -list -alias genkey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        testOK("", p11Arg + "-storepass test12 -certreq -alias genkey -file genkey.certreq");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        testOK("", p11Arg + "-storepass test12 -export -alias genkey -file genkey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        testOK("", "-printcert -file genkey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        testOK("", p11Arg + "-storepass test12 -selfcert -alias genkey -dname cn=selfCert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        testOK("", p11Arg + "-storepass test12 -list -alias genkey -v");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        assertTrue(out.indexOf("Owner: CN=selfCert") != -1, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        //(check that cert subject DN is [cn=selfCert])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        testOK("", p11Arg + "-storepass test12 -delete -alias genkey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
        testOK("", p11Arg + "-storepass test12 -list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        assertTrue(out.indexOf("Your keystore contains 0 entries") != -1, "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        //(check for empty database listing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        //Runtime.getRuntime().exec("/usr/ccs/bin/sccs unedit cert8.db key3.db");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        remove("genkey.cert");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        remove("genkey.certreq");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        //  12. sccs unedit cert8.db key3.db
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    // tesing new option -srcProviderName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    void sszzTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        testAnyway("", NSS_P11_ARG+"-delete -alias nss -storepass test12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        testAnyway("", NZZ_P11_ARG+"-delete -alias nss -storepass test12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        testOK("", NSS_P11_ARG+"-genkeypair -dname CN=NSS -alias nss -storepass test12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        testOK("", NSS_SRC_P11_ARG + NZZ_P11_ARG +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                "-importkeystore -srcstorepass test12 -deststorepass test12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        testAnyway("", NSS_P11_ARG+"-delete -alias nss -storepass test12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        testAnyway("", NZZ_P11_ARG+"-delete -alias nss -storepass test12");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        // first test if HumanInputStream really acts like a human being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        HumanInputStream.test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        KeyToolTest t = new KeyToolTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        if (System.getProperty("file") != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            t.sqeTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            t.testAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            t.i18nTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        if (System.getProperty("nss") != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            t.srcP11Arg = NSS_SRC_P11_ARG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            t.p11Arg = NSS_P11_ARG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            t.testPKCS11();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            // FAIL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            // 1. we still don't have srcprovidername yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            // 2. cannot store privatekey into NSS keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            //    java.security.KeyStoreException: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_TEMPLATE_INCOMPLETE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            //t.testPKCS11ImportKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            t.i18nPKCS11Test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            //FAIL: currently PKCS11-NSS does not support 2 NSS KeyStores to be loaded at the same time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            //t.sszzTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        if (System.getProperty("solaris") != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            // For Solaris Cryptography Framework
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            t.srcP11Arg = SUN_SRC_P11_ARG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            t.p11Arg = SUN_P11_ARG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            t.testPKCS11();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            t.testPKCS11ImportKeyStore();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            t.i18nPKCS11Test();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        System.out.println("Test pass!!!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
class TestException extends Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    public TestException(String e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        super(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
 * HumanInputStream tries to act like a human sitting in front of a computer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
 * terminal typing on the keyboard while the keytool program is running.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
 * keytool has called InputStream.read() and BufferedReader.readLine() in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
 * various places. a call to B.readLine() will try to buffer as much input as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
 * possible. Thus, a trivial InputStream will find it impossible to feed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
 * anything to I.read() after a B.readLine() call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
 * This is why i create HumanInputStream, which will only send a single line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
 * to B.readLine(), no more, no less, and the next I.read() can have a chance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
 * to read the exact character right after "\n".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
 * I don't know why HumanInputStream works.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
class HumanInputStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    byte[] src;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
    int pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    int length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
    boolean inLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    int stopIt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
    public HumanInputStream(String input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        src = input.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        length = src.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        stopIt = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        inLine = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    // the trick: when called through read(byte[], int, int),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    // return -1 twice after "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    @Override public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
        int re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
        if(pos < length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            re = src[pos];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            if(inLine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                if(stopIt > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                    stopIt--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                    re = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                    if(re == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                        stopIt = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                    pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            re = -1;//throw new IOException("NO MORE TO READ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        //if (re < 32) System.err.printf("[%02d]", re);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        //else System.err.printf("[%c]", (char)re);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
        return re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    @Override public int read(byte[] buffer, int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
        inLine = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            int re = super.read(buffer, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            return re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            throw new RuntimeException("HumanInputStream error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            inLine = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    @Override public int available() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        if(pos < length) return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    // test part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    static void assertTrue(boolean bool) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        if(!bool)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
            throw new RuntimeException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    public static void test() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        class Tester {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            HumanInputStream is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
            BufferedReader reader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            Tester(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                is = new HumanInputStream(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                reader = new BufferedReader(new InputStreamReader(is));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            // three kinds of test method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            // 1. read byte by byte from InputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
            void testStreamReadOnce(int expection) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                assertTrue(is.read() == expection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            void testStreamReadMany(String expection) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                char[] keys = expection.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                for(int i=0; i<keys.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                    assertTrue(is.read() == keys[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            // 2. read a line with a newly created Reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            void testReaderReadline(String expection) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                String s = new BufferedReader(new InputStreamReader(is)).readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                if(s == null) assertTrue(expection == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                else assertTrue(s.equals(expection));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
            // 3. read a line with the old Reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
            void testReaderReadline2(String expection) throws Exception  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                String s = reader.readLine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                if(s == null) assertTrue(expection == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                else assertTrue(s.equals(expection));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        Tester test;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        test = new Tester("111\n222\n\n444\n\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        test.testReaderReadline("111");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        test.testReaderReadline("222");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        test.testReaderReadline("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        test.testReaderReadline("444");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        test.testReaderReadline("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        test.testReaderReadline(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
        test = new Tester("111\n222\n\n444\n\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        test.testReaderReadline2("111");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
        test.testReaderReadline2("222");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        test.testReaderReadline2("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        test.testReaderReadline2("444");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        test.testReaderReadline2("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        test.testReaderReadline2(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        test = new Tester("111\n222\n\n444\n\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        test.testReaderReadline2("111");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        test.testReaderReadline("222");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        test.testReaderReadline2("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        test.testReaderReadline2("444");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        test.testReaderReadline("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        test.testReaderReadline2(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        test = new Tester("1\n2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        test.testStreamReadMany("1\n2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        test.testStreamReadOnce(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        test = new Tester("12\n234");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        test.testStreamReadOnce('1');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        test.testReaderReadline("2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        test.testStreamReadOnce('2');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        test.testReaderReadline2("34");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        test.testReaderReadline2(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        test = new Tester("changeit\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        test.testStreamReadMany("changeit\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        test.testReaderReadline(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        test = new Tester("changeit\nName\nCountry\nYes\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        test.testStreamReadMany("changeit\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        test.testReaderReadline("Name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        test.testReaderReadline("Country");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        test.testReaderReadline("Yes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        test.testReaderReadline(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        test = new Tester("Me\nHere\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
        test.testReaderReadline2("Me");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        test.testReaderReadline2("Here");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
}