Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/tools/jlink/JLinkSigningTest.java
51214 views
1
/*
2
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 8159393
27
* @summary Test signed jars involved in image creation
28
* @modules java.base/jdk.internal.jimage
29
* java.base/sun.security.tools.keytool
30
* jdk.compiler/com.sun.tools.javac
31
* jdk.jartool/sun.security.tools.jarsigner
32
* jdk.jartool/sun.tools.jar
33
* jdk.jlink/jdk.tools.jlink.internal
34
* @run main/othervm JLinkSigningTest
35
*/
36
37
38
import java.io.File;
39
import java.io.IOException;
40
import java.nio.file.Files;
41
import java.nio.file.Path;
42
import java.nio.file.Paths;
43
import java.util.Arrays;
44
import java.util.spi.ToolProvider;
45
46
public class JLinkSigningTest {
47
private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
48
.orElseThrow(() -> new RuntimeException("jar tool not found"));
49
private static final ToolProvider JAVAC_TOOL = ToolProvider.findFirst("javac")
50
.orElseThrow(() -> new RuntimeException("javac tool not found"));
51
private static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")
52
.orElseThrow(() -> new RuntimeException("jlink tool not found"));
53
54
static final String[] MODULE_INFO = {
55
"module test {",
56
"}",
57
};
58
59
static final String[] TEST_CLASS = {
60
"package test;",
61
"public class test {",
62
" public static void main(String[] args) {",
63
" }",
64
"}",
65
};
66
67
static void report(String command, String[] args) {
68
System.out.println(command + " " + String.join(" ", Arrays.asList(args)));
69
}
70
71
static void jar(String[] args) {
72
report("jar", args);
73
JAR_TOOL.run(System.out, System.err, args);
74
}
75
76
static void jarsigner(String[] args) {
77
report("jarsigner", args);
78
79
try {
80
sun.security.tools.jarsigner.Main.main(args);
81
} catch (Exception ex) {
82
throw new RuntimeException("jarsigner not found");
83
}
84
}
85
86
static void javac(String[] args) {
87
report("javac", args);
88
JAVAC_TOOL.run(System.out, System.err, args);
89
}
90
91
static void jlink(String[] args) {
92
report("jlink", args);
93
JLINK_TOOL.run(System.out, System.err, args);
94
}
95
96
static void keytool(String[] args) {
97
report("keytool", args);
98
99
try {
100
sun.security.tools.keytool.Main.main(args);
101
} catch (Exception ex) {
102
throw new RuntimeException("keytool failed");
103
}
104
}
105
106
public static void main(String[] args) {
107
final String JAVA_HOME = System.getProperty("java.home");
108
Path moduleInfoJavaPath = Paths.get("module-info.java");
109
Path moduleInfoClassPath = Paths.get("module-info.class");
110
Path testDirectoryPath = Paths.get("test");
111
Path testJavaPath = testDirectoryPath.resolve("test.java");
112
Path testClassPath = testDirectoryPath.resolve("test.class");
113
Path testModsDirectoryPath = Paths.get("testmods");
114
Path jmodsPath = Paths.get(JAVA_HOME, "jmods");
115
Path testjarPath = testModsDirectoryPath.resolve("test.jar");
116
String modulesPath = testjarPath.toString() +
117
File.pathSeparator +
118
jmodsPath.toString();
119
120
try {
121
Files.write(moduleInfoJavaPath, Arrays.asList(MODULE_INFO));
122
Files.createDirectories(testDirectoryPath);
123
Files.write(testJavaPath, Arrays.asList(TEST_CLASS));
124
Files.createDirectories(testModsDirectoryPath);
125
} catch (IOException ex) {
126
throw new RuntimeException("file construction failed");
127
}
128
129
javac(new String[] {
130
testJavaPath.toString(),
131
moduleInfoJavaPath.toString(),
132
});
133
134
jar(new String[] {
135
"-c",
136
"-f", testjarPath.toString(),
137
"--module-path", jmodsPath.toString(),
138
testClassPath.toString(),
139
moduleInfoClassPath.toString(),
140
});
141
142
keytool(new String[] {
143
"-genkey",
144
"-keyalg", "RSA",
145
"-dname", "CN=John Doe, OU=JPG, O=Oracle, L=Santa Clara, ST=California, C=US",
146
"-alias", "examplekey",
147
"-storepass", "password",
148
"-keypass", "password",
149
"-keystore", "examplekeystore",
150
"-validity", "365",
151
});
152
153
jarsigner(new String[] {
154
"-keystore", "examplekeystore",
155
"-verbose", testjarPath.toString(),
156
"-storepass", "password",
157
"-keypass", "password",
158
"examplekey",
159
});
160
161
try {
162
jlink(new String[] {
163
"--module-path", modulesPath,
164
"--add-modules", "test",
165
"--output", "foo",
166
});
167
} catch (Throwable ex) {
168
System.out.println("Failed as should");
169
}
170
171
try {
172
jlink(new String[] {
173
"--module-path", modulesPath,
174
"--add-modules", "test",
175
"--ignore-signing-information",
176
"--output", "foo",
177
});
178
System.out.println("Suceeded as should");
179
} catch (Throwable ex) {
180
System.err.println("Should not have failed");
181
throw new RuntimeException(ex);
182
}
183
184
System.out.println("Done");
185
}
186
}
187
188
189