jdk/test/sun/security/tools/keytool/ImportPrompt.java
author weijun
Sat, 21 Jan 2017 08:38:51 +0800
changeset 43238 60b2aa915d98
child 45467 99c87a16a8e4
permissions -rw-r--r--
8172975: SecurityTools.keytool() needs to accept user input Reviewed-by: asmotrak
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43238
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
     1
/*
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
     4
 *
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
     8
 *
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    13
 * accompanied this code).
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    14
 *
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    18
 *
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    21
 * questions.
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    22
 */
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    23
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    24
import jdk.test.lib.Asserts;
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    25
import jdk.test.lib.SecurityTools;
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    26
import jdk.test.lib.process.OutputAnalyzer;
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    27
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    28
import java.io.File;
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    29
import java.security.KeyStore;
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    30
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    31
/**
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    32
 * @test
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    33
 * @bug 8172975
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    34
 * @summary SecurityTools.keytool() needs to accept user input
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    35
 * @library /test/lib
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    36
 */
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    37
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    38
public class ImportPrompt {
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    39
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    40
    private static final String COMMON =
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    41
            "-storetype jks -storepass changeit -keypass changeit -debug";
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    42
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    43
    public static void main(String[] args) throws Throwable {
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    44
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    45
        kt("-keystore ks1 -genkeypair -alias a -dname CN=A");
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    46
        kt("-keystore ks1 -exportcert -alias a -file a.cert");
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    47
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    48
        // Just create a keystore
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    49
        kt("-keystore ks2 -genkeypair -alias b -dname CN=B");
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    50
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    51
        // no response text, assume no
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    52
        kt("-keystore ks2 -importcert -alias a -file a.cert");
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    53
        Asserts.assertFalse(hasA());
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    54
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    55
        // no reply is no
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    56
        SecurityTools.setResponse("no");
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    57
        kt("-keystore ks2 -importcert -alias a -file a.cert");
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    58
        Asserts.assertFalse(hasA());
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    59
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    60
        // explicit yes
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    61
        SecurityTools.setResponse("yes");
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    62
        kt("-keystore ks2 -importcert -alias a -file a.cert");
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    63
        Asserts.assertTrue(hasA());
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    64
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    65
        // remove it
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    66
        kt("-keystore ks2 -delete -alias a");
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    67
        Asserts.assertFalse(hasA());
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    68
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    69
        // the previous "yes" will not be remembered
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    70
        kt("-keystore ks2 -importcert -alias a -file a.cert");
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    71
        Asserts.assertFalse(hasA());
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    72
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    73
        // add with -noprompt
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    74
        SecurityTools.setResponse("");
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    75
        kt("-keystore ks2 -importcert -alias a -file a.cert -noprompt");
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    76
        Asserts.assertTrue(hasA());
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    77
    }
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    78
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    79
    private static OutputAnalyzer kt(String cmd) throws Throwable {
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    80
        return SecurityTools.keytool(COMMON + " " + cmd)
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    81
                .shouldHaveExitValue(0);
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    82
    }
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    83
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    84
    private static boolean hasA() throws Exception {
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    85
        return KeyStore.getInstance(new File("ks2"), "changeit".toCharArray())
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    86
                .containsAlias("a");
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    87
    }
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    88
}