Path: blob/master/test/jdk/sun/security/tools/keytool/ImportPrompt.java
41152 views
/*1* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import jdk.test.lib.Asserts;24import jdk.test.lib.SecurityTools;25import jdk.test.lib.process.OutputAnalyzer;2627import java.io.File;28import java.security.KeyStore;2930/**31* @test32* @bug 817297533* @summary SecurityTools.keytool() needs to accept user input34* @library /test/lib35* @build jdk.test.lib.SecurityTools36* jdk.test.lib.Utils37* jdk.test.lib.Asserts38* jdk.test.lib.JDKToolFinder39* jdk.test.lib.JDKToolLauncher40* jdk.test.lib.Platform41* jdk.test.lib.process.*42* @run main ImportPrompt43*/4445public class ImportPrompt {4647private static final String COMMON =48"-storetype jks -storepass changeit -keypass changeit -debug";4950public static void main(String[] args) throws Throwable {5152kt("-keystore ks1 -genkeypair -keyalg DSA -alias a -dname CN=A");53kt("-keystore ks1 -exportcert -alias a -file a.cert");5455// Just create a keystore56kt("-keystore ks2 -genkeypair -keyalg DSA -alias b -dname CN=B");5758// no response text, assume no59kt("-keystore ks2 -importcert -alias a -file a.cert");60Asserts.assertFalse(hasA());6162// no reply is no63SecurityTools.setResponse("no");64kt("-keystore ks2 -importcert -alias a -file a.cert");65Asserts.assertFalse(hasA());6667// explicit yes68SecurityTools.setResponse("yes");69kt("-keystore ks2 -importcert -alias a -file a.cert");70Asserts.assertTrue(hasA());7172// remove it73kt("-keystore ks2 -delete -alias a");74Asserts.assertFalse(hasA());7576// the previous "yes" will not be remembered77kt("-keystore ks2 -importcert -alias a -file a.cert");78Asserts.assertFalse(hasA());7980// add with -noprompt81SecurityTools.setResponse("");82kt("-keystore ks2 -importcert -alias a -file a.cert -noprompt");83Asserts.assertTrue(hasA());84}8586private static OutputAnalyzer kt(String cmd) throws Throwable {87return SecurityTools.keytool(COMMON + " " + cmd)88.shouldHaveExitValue(0);89}9091private static boolean hasA() throws Exception {92return KeyStore.getInstance(new File("ks2"), "changeit".toCharArray())93.containsAlias("a");94}95}969798