Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/KeyStore/PKCS12/KeytoolWriteP12Test.java
38828 views
/*1* Copyright (c) 2012, 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. 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.testlibrary.OutputAnalyzer;27import static java.lang.System.out;2829/**30* @test31* @bug 804883032* @summary Tests for creating pkcs12 keystore with various algorithms33* @library /lib/testlibrary ../34* @run main KeytoolWriteP12Test35*/36public class KeytoolWriteP12Test {37private static final String ALIAS = "pkcs12testCA";38private static final Utils.KeyStoreType PKCS12 = Utils.KeyStoreType.pkcs12;39private static final int FAILED_EXIT_CODE = 1;40private static final String CERT_FILE_NAME = "cert.data";41private static final String DNAME = "CN=PKCS12 Test CA, OU=Security SQE, "42+ "O=JavaSoft, C=US";43private static final String WORKING_DIRECTORY = System.44getProperty("test.classes", "." + File.separator);45private enum Algorithm {46DSA, RSA, ECC47};48private void run() {49out.println("Running DSA Test");50keytoolListTest("kt_DSA.p12", Algorithm.DSA);51out.println("DSA Test passed");5253out.println("Running RSA Test");54final String rsaKeyStoreName = "kt_RSA_MD5.p12";55keytoolListTest(rsaKeyStoreName, Algorithm.RSA);56out.println("RSA Test passed");5758out.println("Running RSA and Signing Algorithm SHA1withRSA Test");59keytoolListTest("kt_RSA_SHA1.p12", Algorithm.RSA,60"-sigalg", "SHA1withRSA");61out.println("RSA and Signing Algorithm SHA1withRSA Test Passed");6263out.println("Running Keysize 256 Test");64keytoolListNegativeTest("kt_DSA_256.p12", Algorithm.DSA, "-keysize",65"256");66out.println("Keysize 256 Test Passed");6768out.println("Running Keysize 1023 Test");69keytoolListTest("kt_RSA_MD5_1023.p12", Algorithm.RSA, "-keysize",70"1023");71out.println("Keysize 1023 Test Passed");72out.println("Running Export certificate Test");73exportTest(rsaKeyStoreName);74out.println("Export certificate Test Passed");75}7677private void exportTest(String keyStore) {78final String keyStoreName = WORKING_DIRECTORY + File.separator79+ keyStore;80deleteKeyStoreFile(keyStoreName);81Utils.createKeyStore(DNAME, PKCS12, keyStore, ALIAS,82Algorithm.RSA.name());83final String certFilePath = WORKING_DIRECTORY + File.separator84+ CERT_FILE_NAME;85Utils.exportCert(PKCS12, keyStore,86ALIAS, certFilePath);87final String[] command = new String[]{"-debug", "-printcert", "-v",88"-file", certFilePath};89Utils.executeKeytoolCommand(command);90}9192private void keytoolListTest(String keyStore, Algorithm algorithm,93String ...optionalArgs) {94final String keyStoreName = WORKING_DIRECTORY + File.separator95+ keyStore;96final String[] command = new String[]{"-debug", "-list", "-v", "-alias",97ALIAS, "-keystore", keyStoreName, "-storetype", "pkcs12",98"-storepass", Utils.DEFAULT_PASSWD};99deleteKeyStoreFile(keyStoreName);100Utils.createKeyStore(DNAME, PKCS12, keyStoreName, ALIAS,101algorithm.name(), optionalArgs);102OutputAnalyzer output = Utils.executeKeytoolCommand(command);103output.shouldContain(DNAME);104}105106private void keytoolListNegativeTest(String keyStore, Algorithm algorithm,107String... optionalArgs) {108final String keyStoreName = WORKING_DIRECTORY + File.separator109+ keyStore;110deleteKeyStoreFile(keyStoreName);111Utils.createKeyStore(DNAME, PKCS12, keyStoreName, ALIAS,112algorithm.name(), optionalArgs, FAILED_EXIT_CODE);113}114115public static void main(String[] args) {116KeytoolWriteP12Test test = new KeytoolWriteP12Test();117test.run();118out.println("Test Passed");119}120121private void deleteKeyStoreFile(String fileName) {122File file = new File(fileName);123if (file.exists()) {124file.delete();125}126}127}128129130