author | weijun |
Wed, 10 Aug 2016 11:30:53 +0800 | |
changeset 40253 | 08809866c0bc |
permissions | -rw-r--r-- |
40253
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
1 |
/* |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
2 |
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
4 |
* |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
8 |
* |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
13 |
* accompanied this code). |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
14 |
* |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
18 |
* |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
21 |
* questions. |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
22 |
*/ |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
23 |
|
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
24 |
/* |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
25 |
* @test |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
26 |
* @bug 8162739 |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
27 |
* @summary Create new keytool option to access cacerts file |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
28 |
* @modules java.base/sun.security.tools.keytool |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
29 |
* java.base/sun.security.tools |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
30 |
* @run main/othervm -Duser.language=en -Duser.country=US CacertsOption |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
31 |
*/ |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
32 |
|
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
33 |
import sun.security.tools.KeyStoreUtil; |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
34 |
import sun.security.tools.keytool.Main; |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
35 |
|
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
36 |
import java.io.ByteArrayOutputStream; |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
37 |
import java.io.File; |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
38 |
import java.io.PrintStream; |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
39 |
import java.security.KeyStore; |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
40 |
import java.util.Collections; |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
41 |
|
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
42 |
public class CacertsOption { |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
43 |
|
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
44 |
public static void main(String[] args) throws Exception { |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
45 |
|
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
46 |
run("-help -list"); |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
47 |
if (!msg.contains("-cacerts")) { |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
48 |
throw new Exception("No cacerts in help:\n" + msg); |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
49 |
} |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
50 |
|
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
51 |
String cacerts = KeyStoreUtil.getCacerts(); |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
52 |
|
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
53 |
run("-list -keystore " + cacerts); |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
54 |
if (!msg.contains("Warning:")) { |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
55 |
throw new Exception("No warning in output:\n" + msg); |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
56 |
} |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
57 |
|
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
58 |
run("-list -cacerts"); |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
59 |
KeyStore ks = KeyStore.getInstance(new File(cacerts), (char[])null); |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
60 |
for (String alias: Collections.list(ks.aliases())) { |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
61 |
if (!msg.contains(alias)) { |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
62 |
throw new Exception(alias + " not found in\n" + msg); |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
63 |
} |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
64 |
} |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
65 |
|
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
66 |
try { |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
67 |
run("-list -cacerts -storetype jks"); |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
68 |
throw new Exception("Should fail"); |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
69 |
} catch (IllegalArgumentException iae) { |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
70 |
if (!msg.contains("cannot be used with")) { |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
71 |
throw new Exception("Bad error msg\n" + msg); |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
72 |
} |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
73 |
} |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
74 |
} |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
75 |
|
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
76 |
private static String msg = null; |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
77 |
|
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
78 |
private static void run(String cmd) throws Exception { |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
79 |
msg = null; |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
80 |
cmd += " -storepass changeit -debug"; |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
81 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
82 |
PrintStream ps = new PrintStream(bout); |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
83 |
PrintStream oldOut = System.out; |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
84 |
PrintStream oldErr = System.err; |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
85 |
try { |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
86 |
System.setOut(ps); |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
87 |
System.setErr(ps); |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
88 |
Main.main(cmd.split(" ")); |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
89 |
} finally { |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
90 |
System.setErr(oldErr); |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
91 |
System.setOut(oldOut); |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
92 |
msg = new String(bout.toByteArray()); |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
93 |
} |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
94 |
} |
08809866c0bc
8162739: Create new keytool option to access cacerts file
weijun
parents:
diff
changeset
|
95 |
} |