Path: blob/master/test/jdk/sun/security/tools/keytool/CacertsOption.java
41152 views
/*1* Copyright (c) 2016, 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*/2223/*24* @test25* @bug 816273926* @summary Create new keytool option to access cacerts file27* @modules java.base/sun.security.tools.keytool28* java.base/sun.security.tools29* @run main/othervm -Duser.language=en -Duser.country=US CacertsOption30*/3132import sun.security.tools.KeyStoreUtil;33import sun.security.tools.keytool.Main;3435import java.io.ByteArrayOutputStream;36import java.io.File;37import java.io.PrintStream;38import java.security.KeyStore;39import java.util.Collections;4041public class CacertsOption {4243public static void main(String[] args) throws Exception {4445run("-help -list");46if (!msg.contains("-cacerts")) {47throw new Exception("No cacerts in help:\n" + msg);48}4950String cacerts = KeyStoreUtil.getCacerts();5152run("-list -keystore " + cacerts);53if (!msg.contains("Warning:")) {54throw new Exception("No warning in output:\n" + msg);55}5657run("-list -cacerts");58KeyStore ks = KeyStore.getInstance(new File(cacerts), (char[])null);59for (String alias: Collections.list(ks.aliases())) {60if (!msg.contains(alias)) {61throw new Exception(alias + " not found in\n" + msg);62}63}6465try {66run("-list -cacerts -storetype jks");67throw new Exception("Should fail");68} catch (IllegalArgumentException iae) {69if (!msg.contains("cannot be used with")) {70throw new Exception("Bad error msg\n" + msg);71}72}73}7475private static String msg = null;7677private static void run(String cmd) throws Exception {78msg = null;79cmd += " -storepass changeit -debug";80ByteArrayOutputStream bout = new ByteArrayOutputStream();81PrintStream ps = new PrintStream(bout);82PrintStream oldOut = System.out;83PrintStream oldErr = System.err;84try {85System.setOut(ps);86System.setErr(ps);87Main.main(cmd.split(" "));88} finally {89System.setErr(oldErr);90System.setOut(oldOut);91msg = new String(bout.toByteArray());92}93}94}959697