test/jdk/java/security/KeyStore/PKCS12/StoreTrustedCertAPITest.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 31718 jdk/test/java/security/KeyStore/PKCS12/StoreTrustedCertAPITest.java@c10ac6f1e391
child 51675 b487c1e914d0
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31718
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
     1
/*
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
     2
 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
     4
 *
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    10
 *
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    15
 * accompanied this code).
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    16
 *
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    20
 *
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    23
 * questions.
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    24
 */
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    25
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    26
import java.io.File;
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    27
import java.io.FileInputStream;
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    28
import java.io.FileNotFoundException;
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    29
import java.io.IOException;
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    30
import java.security.KeyStore;
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    31
import java.security.KeyStoreException;
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    32
import java.security.NoSuchAlgorithmException;
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    33
import java.security.cert.Certificate;
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    34
import java.security.cert.CertificateException;
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    35
import java.security.cert.CertificateFactory;
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    36
import static java.lang.System.err;
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    37
import static java.lang.System.out;
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    38
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    39
/**
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    40
 * @test
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    41
 * @bug 8048830
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    42
 * @summary Test imports certificate from file to PKCS12 keystore store it as
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    43
 * trusted certificate Check import errors (must be not errors) & check keystore
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    44
 * content after import
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    45
 * @library /lib/testlibrary ../
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    46
 * @run main StoreTrustedCertAPITest
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    47
 */
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    48
public class StoreTrustedCertAPITest {
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    49
    private static final char[] PASSWORD = "passwd".toCharArray();
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    50
    private static final String ALIAS = "testkey_stcapi";
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    51
    private static final String WORKING_DIRECTORY = System.getProperty(
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    52
            "test.classes", "." + File.separator);
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    53
    private static final String CERT_PATH = WORKING_DIRECTORY + File.separator
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    54
            + "cert.data";
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    55
    private static final String KEYSTORE_PATH = WORKING_DIRECTORY
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    56
            + File.separator + "ks.pkcs12";
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    57
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    58
    /**
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    59
     * Test logic (environment has set up)
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    60
     */
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    61
    private void runTest() throws FileNotFoundException, CertificateException,
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    62
            KeyStoreException, IOException, NoSuchAlgorithmException {
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    63
        Certificate cert;
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    64
        CertificateFactory cf;
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    65
        try (FileInputStream fi = new FileInputStream(CERT_PATH)) {
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    66
            cf = CertificateFactory.getInstance("X.509");
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    67
            cert = cf.generateCertificate(fi);
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    68
            KeyStore ks = KeyStore.getInstance(
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    69
                    Utils.KeyStoreType.pkcs12.name());
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    70
            ks.load(null, null);
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    71
            ks.setCertificateEntry(ALIAS, cert);
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    72
            Utils.saveKeyStore(ks, KEYSTORE_PATH, PASSWORD);
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    73
            ks = Utils.loadKeyStore(KEYSTORE_PATH, Utils.KeyStoreType.pkcs12,
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    74
                    PASSWORD);
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    75
            final Certificate ksCert = ks.getCertificate(ALIAS);
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    76
            if (!ksCert.equals(cert)) {
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    77
                err.println("Orig cert: " + cert.toString());
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    78
                err.println("Cert from keystore: " + ksCert.toString());
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    79
                throw new RuntimeException("Certificates don't match");
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    80
            }
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    81
        }
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    82
    }
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    83
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    84
    public static void main(String[] args) throws Exception {
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    85
        StoreTrustedCertAPITest test = new StoreTrustedCertAPITest();
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    86
        test.setUp();
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    87
        test.runTest();
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    88
        out.println("Test Passed");
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    89
    }
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    90
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    91
    private void setUp() {
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    92
        Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS);
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    93
        Utils.exportCert(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH,
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    94
                ALIAS, CERT_PATH);
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    95
        new File(KEYSTORE_PATH).delete();
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    96
    }
c10ac6f1e391 8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff changeset
    97
}