test/jdk/sun/security/tools/keytool/ImportPrompt.java
changeset 47216 71c04702a3d5
parent 45467 99c87a16a8e4
child 59104 046e4024e55a
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     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 
       
    24 import jdk.test.lib.Asserts;
       
    25 import jdk.test.lib.SecurityTools;
       
    26 import jdk.test.lib.process.OutputAnalyzer;
       
    27 
       
    28 import java.io.File;
       
    29 import java.security.KeyStore;
       
    30 
       
    31 /**
       
    32  * @test
       
    33  * @bug 8172975
       
    34  * @summary SecurityTools.keytool() needs to accept user input
       
    35  * @library /test/lib
       
    36  * @build jdk.test.lib.SecurityTools
       
    37  *        jdk.test.lib.Utils
       
    38  *        jdk.test.lib.Asserts
       
    39  *        jdk.test.lib.JDKToolFinder
       
    40  *        jdk.test.lib.JDKToolLauncher
       
    41  *        jdk.test.lib.Platform
       
    42  *        jdk.test.lib.process.*
       
    43  * @run main ImportPrompt
       
    44  */
       
    45 
       
    46 public class ImportPrompt {
       
    47 
       
    48     private static final String COMMON =
       
    49             "-storetype jks -storepass changeit -keypass changeit -debug";
       
    50 
       
    51     public static void main(String[] args) throws Throwable {
       
    52 
       
    53         kt("-keystore ks1 -genkeypair -alias a -dname CN=A");
       
    54         kt("-keystore ks1 -exportcert -alias a -file a.cert");
       
    55 
       
    56         // Just create a keystore
       
    57         kt("-keystore ks2 -genkeypair -alias b -dname CN=B");
       
    58 
       
    59         // no response text, assume no
       
    60         kt("-keystore ks2 -importcert -alias a -file a.cert");
       
    61         Asserts.assertFalse(hasA());
       
    62 
       
    63         // no reply is no
       
    64         SecurityTools.setResponse("no");
       
    65         kt("-keystore ks2 -importcert -alias a -file a.cert");
       
    66         Asserts.assertFalse(hasA());
       
    67 
       
    68         // explicit yes
       
    69         SecurityTools.setResponse("yes");
       
    70         kt("-keystore ks2 -importcert -alias a -file a.cert");
       
    71         Asserts.assertTrue(hasA());
       
    72 
       
    73         // remove it
       
    74         kt("-keystore ks2 -delete -alias a");
       
    75         Asserts.assertFalse(hasA());
       
    76 
       
    77         // the previous "yes" will not be remembered
       
    78         kt("-keystore ks2 -importcert -alias a -file a.cert");
       
    79         Asserts.assertFalse(hasA());
       
    80 
       
    81         // add with -noprompt
       
    82         SecurityTools.setResponse("");
       
    83         kt("-keystore ks2 -importcert -alias a -file a.cert -noprompt");
       
    84         Asserts.assertTrue(hasA());
       
    85     }
       
    86 
       
    87     private static OutputAnalyzer kt(String cmd) throws Throwable {
       
    88         return SecurityTools.keytool(COMMON + " " + cmd)
       
    89                 .shouldHaveExitValue(0);
       
    90     }
       
    91 
       
    92     private static boolean hasA() throws Exception {
       
    93         return KeyStore.getInstance(new File("ks2"), "changeit".toCharArray())
       
    94                 .containsAlias("a");
       
    95     }
       
    96 }