Path: blob/master/test/jdk/java/security/KeyStore/PKCS12/KeytoolWriteP12Test.java
41153 views
/*1* Copyright (c) 2012, 2018, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425import java.io.File;26import jdk.test.lib.process.OutputAnalyzer;27import static java.lang.System.out;2829/**30* @test31* @bug 804883032* @summary Tests for creating pkcs12 keystore with various algorithms33* @library ../34* @library /test/lib35* @run main KeytoolWriteP12Test36*/37public class KeytoolWriteP12Test {38private static final String ALIAS = "pkcs12testCA";39private static final Utils.KeyStoreType PKCS12 = Utils.KeyStoreType.pkcs12;40private static final int FAILED_EXIT_CODE = 1;41private static final String CERT_FILE_NAME = "cert.data";42private static final String DNAME = "CN=PKCS12 Test CA, OU=Security SQE, "43+ "O=JavaSoft, C=US";44private static final String WORKING_DIRECTORY = System.45getProperty("test.classes", "." + File.separator);46private enum Algorithm {47DSA, RSA, ECC48};49private void run() {50out.println("Running DSA Test");51keytoolListTest("kt_DSA.p12", Algorithm.DSA);52out.println("DSA Test passed");5354out.println("Running RSA Test");55final String rsaKeyStoreName = "kt_RSA_MD5.p12";56keytoolListTest(rsaKeyStoreName, Algorithm.RSA);57out.println("RSA Test passed");5859out.println("Running RSA and Signing Algorithm SHA1withRSA Test");60keytoolListTest("kt_RSA_SHA1.p12", Algorithm.RSA,61"-sigalg", "SHA1withRSA");62out.println("RSA and Signing Algorithm SHA1withRSA Test Passed");6364out.println("Running Keysize 256 Test");65keytoolListNegativeTest("kt_DSA_256.p12", Algorithm.DSA, "-keysize",66"256");67out.println("Keysize 256 Test Passed");6869out.println("Running Keysize 1023 Test");70keytoolListTest("kt_RSA_MD5_1023.p12", Algorithm.RSA, "-keysize",71"1023");72out.println("Keysize 1023 Test Passed");73out.println("Running Export certificate Test");74exportTest(rsaKeyStoreName);75out.println("Export certificate Test Passed");76}7778private void exportTest(String keyStore) {79final String keyStoreName = WORKING_DIRECTORY + File.separator80+ keyStore;81deleteKeyStoreFile(keyStoreName);82Utils.createKeyStore(DNAME, PKCS12, keyStore, ALIAS,83Algorithm.RSA.name());84final String certFilePath = WORKING_DIRECTORY + File.separator85+ CERT_FILE_NAME;86Utils.exportCert(PKCS12, keyStore,87ALIAS, certFilePath);88final String[] command = new String[]{"-debug", "-printcert", "-v",89"-file", certFilePath};90Utils.executeKeytoolCommand(command);91}9293private void keytoolListTest(String keyStore, Algorithm algorithm,94String ...optionalArgs) {95final String keyStoreName = WORKING_DIRECTORY + File.separator96+ keyStore;97final String[] command = new String[]{"-debug", "-list", "-v", "-alias",98ALIAS, "-keystore", keyStoreName, "-storetype", "pkcs12",99"-storepass", Utils.DEFAULT_PASSWD};100deleteKeyStoreFile(keyStoreName);101Utils.createKeyStore(DNAME, PKCS12, keyStoreName, ALIAS,102algorithm.name(), optionalArgs);103OutputAnalyzer output = Utils.executeKeytoolCommand(command);104output.shouldContain(DNAME);105}106107private void keytoolListNegativeTest(String keyStore, Algorithm algorithm,108String... optionalArgs) {109final String keyStoreName = WORKING_DIRECTORY + File.separator110+ keyStore;111deleteKeyStoreFile(keyStoreName);112Utils.createKeyStore(DNAME, PKCS12, keyStoreName, ALIAS,113algorithm.name(), optionalArgs, FAILED_EXIT_CODE);114}115116public static void main(String[] args) {117KeytoolWriteP12Test test = new KeytoolWriteP12Test();118test.run();119out.println("Test Passed");120}121122private void deleteKeyStoreFile(String fileName) {123File file = new File(fileName);124if (file.exists()) {125file.delete();126}127}128}129130131