Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/crypto/provider/KeyProtector/IterationCount.java
41159 views
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 ([email protected])
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("--add-opens=java.base/com.sun.crypto.provider=ALL-UNNAMED");
92
cmd.add(IterationCount.class.getName());
93
cmd.add(clientStr);
94
cmd.add(expectedCount);
95
OutputAnalyzer out = ProcessTools.executeCommand(
96
cmd.toArray(new String[cmd.size()]));
97
out.shouldHaveExitValue(0);
98
}
99
100
private static void testSystem(String expectedCount, String setValue)
101
throws Throwable {
102
System.out.println("Test setting " +
103
(setValue != null ? setValue : "nothing") +
104
" as a System property");
105
List<String> cmd = getBasicCommand();
106
if (setValue != null) {
107
cmd.add("-Djdk.jceks.iterationCount=" + setValue);
108
}
109
executeCommand(cmd, expectedCount);
110
System.out.println(".............................");
111
}
112
113
private static void testSecurity(String expectedCount, String setValue)
114
throws Throwable {
115
testSecurity(expectedCount, setValue, getBasicCommand());
116
}
117
118
private static void testSecurity(String expectedCount, String setValue,
119
List<String> cmd) throws Throwable {
120
System.out.println("Test setting " +
121
(setValue != null ? setValue : "nothing") +
122
" as a Security property");
123
Path tmpDirPath = Files.createTempDirectory("tmpdir");
124
try {
125
if (setValue != null) {
126
String javaSecurityPath = tmpDirPath +
127
File.separator + "java.security";
128
writeJavaSecurityProp(javaSecurityPath, setValue);
129
cmd.add("-Djava.security.properties=" + javaSecurityPath);
130
}
131
executeCommand(cmd, expectedCount);
132
System.out.println(".............................");
133
} finally {
134
deleteDir(tmpDirPath);
135
}
136
}
137
138
private static void testSystemOverridesSecurity() throws Throwable {
139
System.out.println("Test that setting a System property overrides" +
140
" the Security one");
141
String systemValue = Integer.toString(30000);
142
System.out.println("System value: " + systemValue);
143
List<String> cmd = getBasicCommand();
144
cmd.add("-Djdk.jceks.iterationCount=" + systemValue);
145
testSecurity(systemValue, Integer.toString(40000), cmd);
146
}
147
148
private static void writeJavaSecurityProp(String javaSecurityPath,
149
String setValue) throws IOException {
150
try (FileOutputStream fos = new FileOutputStream(
151
new File(javaSecurityPath))) {
152
fos.write(("jdk.jceks.iterationCount=" + setValue).getBytes());
153
}
154
}
155
156
private static int getCurrentIterationCountValue() throws Exception {
157
Class<?> KeyProtectorClass =
158
Class.forName("com.sun.crypto.provider.KeyProtector");
159
Field iterationCountField =
160
KeyProtectorClass.getDeclaredField("ITERATION_COUNT");
161
iterationCountField.setAccessible(true);
162
return iterationCountField.getInt(KeyProtectorClass);
163
}
164
165
private static void deleteDir(Path directory) throws IOException {
166
Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
167
168
@Override
169
public FileVisitResult visitFile(Path file,
170
BasicFileAttributes attrs) throws IOException {
171
Files.delete(file);
172
return FileVisitResult.CONTINUE;
173
}
174
175
@Override
176
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
177
throws IOException {
178
Files.delete(dir);
179
return FileVisitResult.CONTINUE;
180
}
181
});
182
}
183
}
184
185