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.IOException; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
28 |
import java.security.KeyStore; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
29 |
import java.security.KeyStoreException; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
30 |
import java.security.NoSuchAlgorithmException; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
31 |
import java.security.cert.CertificateException; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
32 |
import java.security.cert.X509Certificate; |
51675 | 33 |
import jdk.test.lib.process.OutputAnalyzer; |
31718
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
34 |
import static java.lang.System.out; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
35 |
|
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
36 |
/** |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
37 |
* @test |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
38 |
* @bug 8048830 |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
39 |
* @summary Tests keytool command imports certificate , list keystore, print |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
40 |
* certificate and import password help. |
51687
1e39953aaed8
8182404: remove jdk.testlibrary.JDKToolFinder and JDKToolLauncher
iignatyev
parents:
51675
diff
changeset
|
41 |
* @library ../ |
51675 | 42 |
* @library /test/lib |
31718
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
43 |
* @run main StoreTrustedCertKeytool |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
44 |
*/ |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
45 |
public class StoreTrustedCertKeytool { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
46 |
private static final String PASSWORD = "passwd"; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
47 |
private static final String ALIAS = "testkey_stckey"; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
48 |
private static final String FILE_SEPARATOR = File.separator; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
49 |
private static final String WORKING_DIRECTORY = System.getProperty( |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
50 |
"test.classes", "." + FILE_SEPARATOR); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
51 |
private static final String CERT_PATH = WORKING_DIRECTORY |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
52 |
+ FILE_SEPARATOR |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
53 |
+ "cert.data"; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
54 |
private static final String KEYSTORE_PATH = WORKING_DIRECTORY |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
55 |
+ FILE_SEPARATOR + "ks.pkcs12"; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
56 |
|
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
57 |
protected void run() throws IOException, KeyStoreException, |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
58 |
NoSuchAlgorithmException, CertificateException { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
59 |
setUp(); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
60 |
importCert(); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
61 |
out.println("Import Cert test passed"); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
62 |
listCerts(); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
63 |
out.println("listCerts test passed"); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
64 |
printCert(); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
65 |
out.println("print cert test passed"); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
66 |
helpImportPassword(); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
67 |
out.println("help import test passed"); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
68 |
} |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
69 |
|
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
70 |
private void importCert() { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
71 |
final String[] command = new String[]{"-debug", "-importcert", |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
72 |
"-alias", ALIAS, "-file", CERT_PATH, "-noprompt", "-keystore", |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
73 |
KEYSTORE_PATH, "-storetype", "pkcs12", "-storepass", PASSWORD}; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
74 |
// If the keystore exists delete it. |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
75 |
File keystoreFile = new File(KEYSTORE_PATH); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
76 |
if (keystoreFile.exists()) { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
77 |
keystoreFile.delete(); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
78 |
} |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
79 |
Utils.executeKeytoolCommand(command); |
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 |
private void listCerts() throws IOException, KeyStoreException, |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
83 |
NoSuchAlgorithmException, CertificateException { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
84 |
final String[] command = new String[]{"-debug", "-list", "-v", |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
85 |
"-alias", ALIAS, "-keystore", KEYSTORE_PATH, "-storetype", "pkcs12", |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
86 |
"-storepass", PASSWORD}; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
87 |
OutputAnalyzer output = Utils.executeKeytoolCommand(command); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
88 |
if (output == null) { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
89 |
throw new RuntimeException("Keystore print fails"); |
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 |
X509Certificate ksCert = null; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
92 |
final KeyStore ks = Utils.loadKeyStore(KEYSTORE_PATH, |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
93 |
Utils.KeyStoreType.pkcs12, PASSWORD.toCharArray()); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
94 |
ksCert = (X509Certificate) ks.getCertificate(ALIAS); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
95 |
|
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
96 |
if (ksCert == null) { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
97 |
throw new RuntimeException("Certificate " + ALIAS |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
98 |
+ " not found in Keystore " + KEYSTORE_PATH); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
99 |
} |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
100 |
String serialNumber = ksCert.getSerialNumber().toString(16); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
101 |
output.shouldContain(serialNumber); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
102 |
} |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
103 |
|
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
104 |
private void printCert() { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
105 |
final String[] command = new String[]{"-debug", "-printcert", |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
106 |
"-file", CERT_PATH}; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
107 |
Utils.executeKeytoolCommand(command); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
108 |
|
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
109 |
} |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
110 |
|
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
111 |
private void helpImportPassword() { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
112 |
final String[] command = new String[]{"-debug", "-help", |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
113 |
"-importpassword"}; |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
114 |
Utils.executeKeytoolCommand(command); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
115 |
} |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
116 |
|
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
117 |
public static void main(String[] args) throws Exception { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
118 |
final StoreTrustedCertKeytool test = new StoreTrustedCertKeytool(); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
119 |
test.run(); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
120 |
out.println("Test Passed"); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
121 |
} |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
122 |
|
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
123 |
private void setUp() { |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
124 |
Utils.createKeyStore(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
125 |
Utils.exportCert(Utils.KeyStoreType.pkcs12, KEYSTORE_PATH, ALIAS, |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
126 |
CERT_PATH); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
127 |
new File(KEYSTORE_PATH).delete(); |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
128 |
} |
c10ac6f1e391
8048830: Implement tests for new functionality provided in JEP 166
vinnie
parents:
diff
changeset
|
129 |
} |