Path: blob/master/test/jdk/java/security/KeyStore/PKCS12/StoreTrustedCertKeytool.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 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.test.lib.process.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 ../41* @library /test/lib42* @run main StoreTrustedCertKeytool43*/44public class StoreTrustedCertKeytool {45private static final String PASSWORD = "passwd";46private static final String ALIAS = "testkey_stckey";47private static final String FILE_SEPARATOR = File.separator;48private static final String WORKING_DIRECTORY = System.getProperty(49"test.classes", "." + FILE_SEPARATOR);50private static final String CERT_PATH = WORKING_DIRECTORY51+ FILE_SEPARATOR52+ "cert.data";53private static final String KEYSTORE_PATH = WORKING_DIRECTORY54+ FILE_SEPARATOR + "ks.pkcs12";5556protected void run() throws IOException, KeyStoreException,57NoSuchAlgorithmException, CertificateException {58setUp();59importCert();60out.println("Import Cert test passed");61listCerts();62out.println("listCerts test passed");63printCert();64out.println("print cert test passed");65helpImportPassword();66out.println("help import test passed");67}6869private void importCert() {70final String[] command = new String[]{"-debug", "-importcert",71"-alias", ALIAS, "-file", CERT_PATH, "-noprompt", "-keystore",72KEYSTORE_PATH, "-storetype", "pkcs12", "-storepass", PASSWORD};73// If the keystore exists delete it.74File keystoreFile = new File(KEYSTORE_PATH);75if (keystoreFile.exists()) {76keystoreFile.delete();77}78Utils.executeKeytoolCommand(command);79}8081private void listCerts() throws IOException, KeyStoreException,82NoSuchAlgorithmException, CertificateException {83final String[] command = new String[]{"-debug", "-list", "-v",84"-alias", ALIAS, "-keystore", KEYSTORE_PATH, "-storetype", "pkcs12",85"-storepass", PASSWORD};86OutputAnalyzer output = Utils.executeKeytoolCommand(command);87if (output == null) {88throw new RuntimeException("Keystore print fails");89}90X509Certificate ksCert = null;91final KeyStore ks = Utils.loadKeyStore(KEYSTORE_PATH,92Utils.KeyStoreType.pkcs12, PASSWORD.toCharArray());93ksCert = (X509Certificate) ks.getCertificate(ALIAS);9495if (ksCert == null) {96throw new RuntimeException("Certificate " + ALIAS97+ " not found in Keystore " + KEYSTORE_PATH);98}99String serialNumber = ksCert.getSerialNumber().toString(16);100output.shouldContain(serialNumber);101}102103private void printCert() {104final String[] command = new String[]{"-debug", "-printcert",105"-file", CERT_PATH};106Utils.executeKeytoolCommand(command);107108}109110private void helpImportPassword() {111final String[] command = new String[]{"-debug", "-help",112"-importpassword"};113Utils.executeKeytoolCommand(command);114}115116public static void main(String[] args) throws Exception {117final StoreTrustedCertKeytool test = new StoreTrustedCertKeytool();118test.run();119out.println("Test Passed");120}121122private void setUp() {123Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS);124Utils.exportCert(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS,125CERT_PATH);126new File(KEYSTORE_PATH).delete();127}128}129130131