Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/Class/forName/modules/TestDriver.java
41159 views
1
/*
2
* Copyright (c) 2015, 2018, 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
import java.io.IOException;
25
import java.io.UncheckedIOException;
26
import java.nio.file.Files;
27
import java.nio.file.Path;
28
import java.nio.file.Paths;
29
import java.util.Arrays;
30
import java.util.stream.Stream;
31
32
import jdk.test.lib.util.FileUtils;
33
import jdk.test.lib.compiler.CompilerUtils;
34
import static jdk.test.lib.process.ProcessTools.*;
35
36
import org.testng.annotations.BeforeClass;
37
import org.testng.annotations.Test;
38
import static org.testng.Assert.assertTrue;
39
40
/**
41
* @test
42
* @bug 8087335
43
* @summary Tests for Class.forName(Module,String)
44
* @library /test/lib
45
* @modules jdk.compiler
46
* @build jdk.test.lib.Platform
47
* jdk.test.lib.util.FileUtils
48
* jdk.test.lib.compiler.CompilerUtils
49
* jdk.test.lib.process.ProcessTools
50
* TestDriver TestMain TestLayer
51
* @run testng TestDriver
52
*/
53
54
public class TestDriver {
55
56
private static final String TEST_SRC =
57
Paths.get(System.getProperty("test.src")).toString();
58
private static final String TEST_CLASSES =
59
Paths.get(System.getProperty("test.classes")).toString();
60
61
private static final Path MOD_SRC_DIR = Paths.get(TEST_SRC, "src");
62
private static final Path MOD_DEST_DIR = Paths.get("mods");
63
64
private static final String[] modules = new String[] {"m1", "m2", "m3"};
65
66
/**
67
* Compiles all modules used by the test.
68
*/
69
@BeforeClass
70
public void setup() throws Exception {
71
assertTrue(CompilerUtils.compile(
72
MOD_SRC_DIR, MOD_DEST_DIR,
73
"--module-source-path",
74
MOD_SRC_DIR.toString()));
75
76
copyDirectories(MOD_DEST_DIR.resolve("m1"), Paths.get("mods1"));
77
copyDirectories(MOD_DEST_DIR.resolve("m2"), Paths.get("mods2"));
78
}
79
80
@Test
81
public void test() throws Exception {
82
String[] options = new String[] {
83
"-cp", TEST_CLASSES,
84
"--module-path", MOD_DEST_DIR.toString(),
85
"--add-modules", String.join(",", modules),
86
"-m", "m2/p2.test.Main"
87
};
88
runTest(options);
89
}
90
91
@Test
92
public void testUnnamedModule() throws Exception {
93
String[] options = new String[] {
94
"-cp", TEST_CLASSES,
95
"--module-path", MOD_DEST_DIR.toString(),
96
"--add-modules", String.join(",", modules),
97
"TestMain"
98
};
99
runTest(options);
100
}
101
102
@Test
103
public void testLayer() throws Exception {
104
String[] options = new String[] {
105
"-cp", TEST_CLASSES,
106
"TestLayer"
107
};
108
109
runTest(options);
110
}
111
112
@Test
113
public void testDeniedClassLoaderAccess() throws Exception {
114
String[] options = new String[] {
115
"--module-path", MOD_DEST_DIR.toString(),
116
"--add-modules", String.join(",", modules),
117
"-Djava.security.manager=allow",
118
"-m", "m3/p3.NoGetClassLoaderAccess"
119
};
120
assertTrue(executeTestJava(options)
121
.outputTo(System.out)
122
.errorTo(System.err)
123
.getExitValue() == 0);
124
}
125
126
@Test
127
public void testDeniedAccess() throws Exception {
128
Path policyFile = Paths.get(TEST_SRC, "policy.denied");
129
130
String[] options = new String[] {
131
"-Djava.security.manager",
132
"-Djava.security.policy=" + policyFile.toString(),
133
"--module-path", MOD_DEST_DIR.toString(),
134
"--add-modules", String.join(",", modules),
135
"-m", "m3/p3.NoAccess"
136
};
137
assertTrue(executeTestJava(options)
138
.outputTo(System.out)
139
.errorTo(System.err)
140
.getExitValue() == 0);
141
}
142
143
private String[] runWithSecurityManager(String[] options) {
144
Path policyFile = Paths.get(TEST_SRC, "policy");
145
Stream<String> opts = Stream.concat(Stream.of("-Djava.security.manager",
146
"-Djava.security.policy=" + policyFile.toString()),
147
Arrays.stream(options));
148
return opts.toArray(String[]::new);
149
}
150
151
private void runTest(String[] options) throws Exception {
152
assertTrue(executeTestJava(options)
153
.outputTo(System.out)
154
.errorTo(System.err)
155
.getExitValue() == 0);
156
157
assertTrue(executeTestJava(runWithSecurityManager(options))
158
.outputTo(System.out)
159
.errorTo(System.err)
160
.getExitValue() == 0);
161
}
162
163
private void copyDirectories(Path source, Path dest) throws IOException {
164
if (Files.exists(dest))
165
FileUtils.deleteFileTreeWithRetry(dest);
166
Files.walk(source, Integer.MAX_VALUE)
167
.filter(Files::isRegularFile)
168
.forEach(p -> {
169
try {
170
Path to = dest.resolve(source.relativize(p));
171
Files.createDirectories(to.getParent());
172
Files.copy(p, to);
173
} catch (IOException e) {
174
throw new UncheckedIOException(e);
175
}
176
});
177
}
178
}
179
180