test/jdk/sun/security/tools/keytool/ImportPrompt.java
author weijun
Fri, 15 Nov 2019 09:06:58 +0800
changeset 59104 046e4024e55a
parent 47216 71c04702a3d5
permissions -rw-r--r--
8214024: Remove the default keytool -keyalg value Reviewed-by: mullan
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
/*
59104
046e4024e55a 8214024: Remove the default keytool -keyalg value
weijun
parents: 47216
diff changeset
     2
 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
43238
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
45467
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 43238
diff changeset
    36
 * @build jdk.test.lib.SecurityTools
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 43238
diff changeset
    37
 *        jdk.test.lib.Utils
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 43238
diff changeset
    38
 *        jdk.test.lib.Asserts
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 43238
diff changeset
    39
 *        jdk.test.lib.JDKToolFinder
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 43238
diff changeset
    40
 *        jdk.test.lib.JDKToolLauncher
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 43238
diff changeset
    41
 *        jdk.test.lib.Platform
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 43238
diff changeset
    42
 *        jdk.test.lib.process.*
99c87a16a8e4 8181761: add explicit @build actions for jdk.test.lib classes in all :tier2 tests
iignatyev
parents: 43238
diff changeset
    43
 * @run main ImportPrompt
43238
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
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    46
public class ImportPrompt {
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
    private static final String COMMON =
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    49
            "-storetype jks -storepass changeit -keypass changeit -debug";
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
    public static void main(String[] args) throws Throwable {
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    52
59104
046e4024e55a 8214024: Remove the default keytool -keyalg value
weijun
parents: 47216
diff changeset
    53
        kt("-keystore ks1 -genkeypair -keyalg DSA -alias a -dname CN=A");
43238
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    54
        kt("-keystore ks1 -exportcert -alias a -file a.cert");
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    55
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    56
        // Just create a keystore
59104
046e4024e55a 8214024: Remove the default keytool -keyalg value
weijun
parents: 47216
diff changeset
    57
        kt("-keystore ks2 -genkeypair -keyalg DSA -alias b -dname CN=B");
43238
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    58
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    59
        // no response text, assume no
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    60
        kt("-keystore ks2 -importcert -alias a -file a.cert");
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    61
        Asserts.assertFalse(hasA());
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    62
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    63
        // no reply is no
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    64
        SecurityTools.setResponse("no");
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    65
        kt("-keystore ks2 -importcert -alias a -file a.cert");
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    66
        Asserts.assertFalse(hasA());
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    67
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    68
        // explicit yes
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    69
        SecurityTools.setResponse("yes");
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.assertTrue(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
        // remove it
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    74
        kt("-keystore ks2 -delete -alias a");
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    75
        Asserts.assertFalse(hasA());
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    76
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    77
        // the previous "yes" will not be remembered
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    78
        kt("-keystore ks2 -importcert -alias a -file a.cert");
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    79
        Asserts.assertFalse(hasA());
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    80
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    81
        // add with -noprompt
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    82
        SecurityTools.setResponse("");
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    83
        kt("-keystore ks2 -importcert -alias a -file a.cert -noprompt");
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    84
        Asserts.assertTrue(hasA());
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    85
    }
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    86
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    87
    private static OutputAnalyzer kt(String cmd) throws Throwable {
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    88
        return SecurityTools.keytool(COMMON + " " + cmd)
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    89
                .shouldHaveExitValue(0);
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    90
    }
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    91
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    92
    private static boolean hasA() throws Exception {
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    93
        return KeyStore.getInstance(new File("ks2"), "changeit".toCharArray())
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    94
                .containsAlias("a");
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    95
    }
60b2aa915d98 8172975: SecurityTools.keytool() needs to accept user input
weijun
parents:
diff changeset
    96
}