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