1 /* 2 * Copyright (c) 2016, 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 /* 25 * @test 26 * @bug 8162739 27 * @summary Create new keytool option to access cacerts file 28 * @modules java.base/sun.security.tools.keytool 29 * java.base/sun.security.tools 30 * @run main/othervm -Duser.language=en -Duser.country=US CacertsOption 31 */ 32 33 import sun.security.tools.KeyStoreUtil; 34 import sun.security.tools.keytool.Main; 35 36 import java.io.ByteArrayOutputStream; 37 import java.io.File; 38 import java.io.PrintStream; 39 import java.security.KeyStore; 40 import java.util.Collections; 41 42 public class CacertsOption { 43 main(String[] args)44 public static void main(String[] args) throws Exception { 45 46 run("-help -list"); 47 if (!msg.contains("-cacerts")) { 48 throw new Exception("No cacerts in help:\n" + msg); 49 } 50 51 String cacerts = KeyStoreUtil.getCacerts(); 52 53 run("-list -keystore " + cacerts); 54 if (!msg.contains("Warning:")) { 55 throw new Exception("No warning in output:\n" + msg); 56 } 57 58 run("-list -cacerts"); 59 KeyStore ks = KeyStore.getInstance(new File(cacerts), (char[])null); 60 for (String alias: Collections.list(ks.aliases())) { 61 if (!msg.contains(alias)) { 62 throw new Exception(alias + " not found in\n" + msg); 63 } 64 } 65 66 try { 67 run("-list -cacerts -storetype jks"); 68 throw new Exception("Should fail"); 69 } catch (IllegalArgumentException iae) { 70 if (!msg.contains("cannot be used with")) { 71 throw new Exception("Bad error msg\n" + msg); 72 } 73 } 74 } 75 76 private static String msg = null; 77 run(String cmd)78 private static void run(String cmd) throws Exception { 79 msg = null; 80 cmd += " -storepass changeit -debug"; 81 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 82 PrintStream ps = new PrintStream(bout); 83 PrintStream oldOut = System.out; 84 PrintStream oldErr = System.err; 85 try { 86 System.setOut(ps); 87 System.setErr(ps); 88 Main.main(cmd.split(" ")); 89 } finally { 90 System.setErr(oldErr); 91 System.setOut(oldOut); 92 msg = new String(bout.toByteArray()); 93 } 94 } 95 } 96