author | igerasim |
Wed, 16 Oct 2019 18:47:11 -0700 | |
changeset 58659 | 4113f16d5109 |
parent 51687 | 1e39953aaed8 |
permissions | -rw-r--r-- |
31718
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
1 |
/* |
51675 | 2 |
* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved. |
31718
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 |
51687
1e39953aaed8
8182404: remove jdk.testlibrary.JDKToolFinder and JDKToolLauncher
iignatyev
parents:
51675
diff
changeset
|
45 |
* @library ../ |
51675 | 46 |
* @library /test/lib |
31718
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
47 |
* @run main StoreTrustedCertAPITest |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
48 |
*/ |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
49 |
public class StoreTrustedCertAPITest { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
50 |
private static final char[] PASSWORD = "passwd".toCharArray(); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
51 |
private static final String ALIAS = "testkey_stcapi"; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
52 |
private static final String WORKING_DIRECTORY = System.getProperty( |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
53 |
"test.classes", "." + File.separator); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
54 |
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
|
55 |
+ "cert.data"; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
56 |
private static final String KEYSTORE_PATH = WORKING_DIRECTORY |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
57 |
+ File.separator + "ks.pkcs12"; |
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 |
/** |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
60 |
* Test logic (environment has set up) |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
61 |
*/ |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
62 |
private void runTest() throws FileNotFoundException, CertificateException, |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
63 |
KeyStoreException, IOException, NoSuchAlgorithmException { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
64 |
Certificate cert; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
65 |
CertificateFactory cf; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
66 |
try (FileInputStream fi = new FileInputStream(CERT_PATH)) { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
67 |
cf = CertificateFactory.getInstance("X.509"); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
68 |
cert = cf.generateCertificate(fi); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
69 |
KeyStore ks = KeyStore.getInstance( |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
70 |
Utils.KeyStoreType.pkcs12.name()); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
71 |
ks.load(null, null); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
72 |
ks.setCertificateEntry(ALIAS, cert); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
73 |
Utils.saveKeyStore(ks, KEYSTORE_PATH, PASSWORD); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
74 |
ks = Utils.loadKeyStore(KEYSTORE_PATH, Utils.KeyStoreType.pkcs12, |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
75 |
PASSWORD); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
76 |
final Certificate ksCert = ks.getCertificate(ALIAS); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
77 |
if (!ksCert.equals(cert)) { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
78 |
err.println("Orig cert: " + cert.toString()); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
79 |
err.println("Cert from keystore: " + ksCert.toString()); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
80 |
throw new RuntimeException("Certificates don't match"); |
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 |
|
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
85 |
public static void main(String[] args) throws Exception { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
86 |
StoreTrustedCertAPITest test = new StoreTrustedCertAPITest(); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
87 |
test.setUp(); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
88 |
test.runTest(); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
89 |
out.println("Test Passed"); |
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 |
|
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
92 |
private void setUp() { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
93 |
Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
94 |
Utils.exportCert(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
95 |
ALIAS, CERT_PATH); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
96 |
new File(KEYSTORE_PATH).delete(); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
97 |
} |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
98 |
} |