Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/security/KeyStore/PKCS12/StoreTrustedCertKeytool.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 java.io.IOException;27import java.security.KeyStore;28import java.security.KeyStoreException;29import java.security.NoSuchAlgorithmException;30import java.security.cert.CertificateException;31import java.security.cert.X509Certificate;32import jdk.testlibrary.OutputAnalyzer;33import static java.lang.System.out;3435/**36* @test37* @bug 804883038* @summary Tests keytool command imports certificate , list keystore, print39* certificate and import password help.40* @library /lib/testlibrary ../41* @run main StoreTrustedCertKeytool42*/43public class StoreTrustedCertKeytool {44private static final String PASSWORD = "passwd";45private static final String ALIAS = "testkey_stckey";46private static final String FILE_SEPARATOR = File.separator;47private static final String WORKING_DIRECTORY = System.getProperty(48"test.classes", "." + FILE_SEPARATOR);49private static final String CERT_PATH = WORKING_DIRECTORY50+ FILE_SEPARATOR51+ "cert.data";52private static final String KEYSTORE_PATH = WORKING_DIRECTORY53+ FILE_SEPARATOR + "ks.pkcs12";5455protected void run() throws IOException, KeyStoreException,56NoSuchAlgorithmException, CertificateException {57setUp();58importCert();59out.println("Import Cert test passed");60listCerts();61out.println("listCerts test passed");62printCert();63out.println("print cert test passed");64helpImportPassword();65out.println("help import test passed");66}6768private void importCert() {69final String[] command = new String[]{"-debug", "-importcert",70"-alias", ALIAS, "-file", CERT_PATH, "-noprompt", "-keystore",71KEYSTORE_PATH, "-storetype", "pkcs12", "-storepass", PASSWORD};72// If the keystore exists delete it.73File keystoreFile = new File(KEYSTORE_PATH);74if (keystoreFile.exists()) {75keystoreFile.delete();76}77Utils.executeKeytoolCommand(command);78}7980private void listCerts() throws IOException, KeyStoreException,81NoSuchAlgorithmException, CertificateException {82final String[] command = new String[]{"-debug", "-list", "-v",83"-alias", ALIAS, "-keystore", KEYSTORE_PATH, "-storetype", "pkcs12",84"-storepass", PASSWORD};85OutputAnalyzer output = Utils.executeKeytoolCommand(command);86if (output == null) {87throw new RuntimeException("Keystore print fails");88}89X509Certificate ksCert = null;90final KeyStore ks = Utils.loadKeyStore(KEYSTORE_PATH,91Utils.KeyStoreType.pkcs12, PASSWORD.toCharArray());92ksCert = (X509Certificate) ks.getCertificate(ALIAS);9394if (ksCert == null) {95throw new RuntimeException("Certificate " + ALIAS96+ " not found in Keystore " + KEYSTORE_PATH);97}98String serialNumber = ksCert.getSerialNumber().toString(16);99output.shouldContain(serialNumber);100}101102private void printCert() {103final String[] command = new String[]{"-debug", "-printcert",104"-file", CERT_PATH};105Utils.executeKeytoolCommand(command);106107}108109private void helpImportPassword() {110final String[] command = new String[]{"-debug", "-help",111"-importpassword"};112Utils.executeKeytoolCommand(command);113}114115public static void main(String[] args) throws Exception {116final StoreTrustedCertKeytool test = new StoreTrustedCertKeytool();117test.run();118out.println("Test Passed");119}120121private void setUp() {122Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS);123Utils.exportCert(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS,124CERT_PATH);125new File(KEYSTORE_PATH).delete();126}127}128129130