|
1 /* |
|
2 * Copyright (c) 2019, Red Hat, Inc. |
|
3 * |
|
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
5 * |
|
6 * This code is free software; you can redistribute it and/or modify it |
|
7 * under the terms of the GNU General Public License version 2 only, as |
|
8 * published by the Free Software Foundation. |
|
9 * |
|
10 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 * version 2 for more details (a copy is included in the LICENSE file that |
|
14 * accompanied this code). |
|
15 * |
|
16 * You should have received a copy of the GNU General Public License version |
|
17 * 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 USA |
|
21 * or visit www.oracle.com if you need additional information or have any |
|
22 * questions. |
|
23 */ |
|
24 |
|
25 /* |
|
26 * @test |
|
27 * @bug 8233404 |
|
28 * @library /test/lib |
|
29 * @run main/othervm/timeout=30 IterationCount HOST 200000 |
|
30 * @run main/othervm/timeout=30 IterationCount HOST 200000 1 |
|
31 * @run main/othervm/timeout=30 IterationCount HOST 200000 6000000 |
|
32 * @run main/othervm/timeout=30 IterationCount HOST 200000 invalid |
|
33 * @run main/othervm/timeout=30 IterationCount HOST 30000 30000 |
|
34 * @run main/othervm/timeout=30 IterationCount OVERRIDE |
|
35 * @author Martin Balao (mbalao@redhat.com) |
|
36 */ |
|
37 |
|
38 import java.io.File; |
|
39 import java.io.FileOutputStream; |
|
40 import java.io.IOException; |
|
41 import java.lang.reflect.Field; |
|
42 import java.nio.file.FileVisitResult; |
|
43 import java.nio.file.Files; |
|
44 import java.nio.file.Path; |
|
45 import java.nio.file.SimpleFileVisitor; |
|
46 import java.nio.file.attribute.BasicFileAttributes; |
|
47 import java.util.ArrayList; |
|
48 import java.util.List; |
|
49 |
|
50 import jdk.test.lib.process.OutputAnalyzer; |
|
51 import jdk.test.lib.process.ProcessTools; |
|
52 |
|
53 public class IterationCount { |
|
54 private static final String clientStr = "CLIENT"; |
|
55 private static final String javaBinPath = |
|
56 System.getProperty("java.home", ".") + File.separator + "bin" + |
|
57 File.separator + "java"; |
|
58 |
|
59 public static void main(String[] args) throws Throwable { |
|
60 if (args[0].equals("HOST")) { |
|
61 String setValue = null; |
|
62 if (args.length > 2) { |
|
63 setValue = args[2]; |
|
64 } |
|
65 testSystem(args[1], setValue); |
|
66 testSecurity(args[1], setValue); |
|
67 } else if (args[0].equals(clientStr)) { |
|
68 int expectedIterationCount = Integer.parseInt(args[1]); |
|
69 int currentIterationCount = getCurrentIterationCountValue(); |
|
70 System.out.println("Expected value: " + expectedIterationCount); |
|
71 System.out.println("Current value: " + currentIterationCount); |
|
72 if (currentIterationCount != expectedIterationCount) { |
|
73 throw new Exception("Expected value different than current"); |
|
74 } |
|
75 } else if (args[0].equals("OVERRIDE")) { |
|
76 testSystemOverridesSecurity(); |
|
77 } |
|
78 System.out.println("TEST PASS - OK"); |
|
79 } |
|
80 |
|
81 private static List<String> getBasicCommand() { |
|
82 List<String> cmd = new ArrayList<>(); |
|
83 cmd.add(javaBinPath); |
|
84 cmd.add("-cp"); |
|
85 cmd.add(System.getProperty("test.classes", ".")); |
|
86 return cmd; |
|
87 } |
|
88 |
|
89 private static void executeCommand(List<String> cmd, String expectedCount) |
|
90 throws Throwable { |
|
91 cmd.add(IterationCount.class.getName()); |
|
92 cmd.add(clientStr); |
|
93 cmd.add(expectedCount); |
|
94 OutputAnalyzer out = ProcessTools.executeCommand( |
|
95 cmd.toArray(new String[cmd.size()])); |
|
96 out.shouldHaveExitValue(0); |
|
97 } |
|
98 |
|
99 private static void testSystem(String expectedCount, String setValue) |
|
100 throws Throwable { |
|
101 System.out.println("Test setting " + |
|
102 (setValue != null ? setValue : "nothing") + |
|
103 " as a System property"); |
|
104 List<String> cmd = getBasicCommand(); |
|
105 if (setValue != null) { |
|
106 cmd.add("-Djdk.jceks.iterationCount=" + setValue); |
|
107 } |
|
108 executeCommand(cmd, expectedCount); |
|
109 System.out.println("............................."); |
|
110 } |
|
111 |
|
112 private static void testSecurity(String expectedCount, String setValue) |
|
113 throws Throwable { |
|
114 testSecurity(expectedCount, setValue, getBasicCommand()); |
|
115 } |
|
116 |
|
117 private static void testSecurity(String expectedCount, String setValue, |
|
118 List<String> cmd) throws Throwable { |
|
119 System.out.println("Test setting " + |
|
120 (setValue != null ? setValue : "nothing") + |
|
121 " as a Security property"); |
|
122 Path tmpDirPath = Files.createTempDirectory("tmpdir"); |
|
123 try { |
|
124 if (setValue != null) { |
|
125 String javaSecurityPath = tmpDirPath + |
|
126 File.separator + "java.security"; |
|
127 writeJavaSecurityProp(javaSecurityPath, setValue); |
|
128 cmd.add("-Djava.security.properties=" + javaSecurityPath); |
|
129 } |
|
130 executeCommand(cmd, expectedCount); |
|
131 System.out.println("............................."); |
|
132 } finally { |
|
133 deleteDir(tmpDirPath); |
|
134 } |
|
135 } |
|
136 |
|
137 private static void testSystemOverridesSecurity() throws Throwable { |
|
138 System.out.println("Test that setting a System property overrides" + |
|
139 " the Security one"); |
|
140 String systemValue = Integer.toString(30000); |
|
141 System.out.println("System value: " + systemValue); |
|
142 List<String> cmd = getBasicCommand(); |
|
143 cmd.add("-Djdk.jceks.iterationCount=" + systemValue); |
|
144 testSecurity(systemValue, Integer.toString(40000), cmd); |
|
145 } |
|
146 |
|
147 private static void writeJavaSecurityProp(String javaSecurityPath, |
|
148 String setValue) throws IOException { |
|
149 try (FileOutputStream fos = new FileOutputStream( |
|
150 new File(javaSecurityPath))) { |
|
151 fos.write(("jdk.jceks.iterationCount=" + setValue).getBytes()); |
|
152 } |
|
153 } |
|
154 |
|
155 private static int getCurrentIterationCountValue() throws Exception { |
|
156 Class<?> KeyProtectorClass = |
|
157 Class.forName("com.sun.crypto.provider.KeyProtector"); |
|
158 Field iterationCountField = |
|
159 KeyProtectorClass.getDeclaredField("ITERATION_COUNT"); |
|
160 iterationCountField.setAccessible(true); |
|
161 return iterationCountField.getInt(KeyProtectorClass); |
|
162 } |
|
163 |
|
164 private static void deleteDir(Path directory) throws IOException { |
|
165 Files.walkFileTree(directory, new SimpleFileVisitor<Path>() { |
|
166 |
|
167 @Override |
|
168 public FileVisitResult visitFile(Path file, |
|
169 BasicFileAttributes attrs) throws IOException { |
|
170 Files.delete(file); |
|
171 return FileVisitResult.CONTINUE; |
|
172 } |
|
173 |
|
174 @Override |
|
175 public FileVisitResult postVisitDirectory(Path dir, IOException exc) |
|
176 throws IOException { |
|
177 Files.delete(dir); |
|
178 return FileVisitResult.CONTINUE; |
|
179 } |
|
180 }); |
|
181 } |
|
182 } |