36511
|
1 |
/**
|
|
2 |
* Copyright (c) 2015, 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 |
* @summary Negative tests for jlink
|
|
27 |
* @bug 8130861
|
|
28 |
* @author Andrei Eremeev
|
|
29 |
* @library ../lib
|
|
30 |
* @modules java.base/jdk.internal.jimage
|
|
31 |
* java.base/jdk.internal.module
|
|
32 |
* jdk.jdeps/com.sun.tools.classfile
|
|
33 |
* jdk.jlink/jdk.tools.jlink.internal
|
|
34 |
* jdk.jlink/jdk.tools.jmod
|
|
35 |
* jdk.jlink/jdk.tools.jimage
|
|
36 |
* jdk.compiler
|
|
37 |
* @build tests.*
|
|
38 |
* @run testng JLinkNegativeTest
|
|
39 |
*/
|
|
40 |
|
|
41 |
import java.io.IOException;
|
|
42 |
import java.io.OutputStream;
|
|
43 |
import java.lang.module.ModuleDescriptor;
|
|
44 |
import java.nio.file.FileVisitResult;
|
|
45 |
import java.nio.file.Files;
|
|
46 |
import java.nio.file.Path;
|
|
47 |
import java.nio.file.Paths;
|
|
48 |
import java.nio.file.SimpleFileVisitor;
|
|
49 |
import java.nio.file.attribute.BasicFileAttributes;
|
|
50 |
import java.util.Arrays;
|
|
51 |
import java.util.Collections;
|
|
52 |
import java.util.List;
|
|
53 |
|
|
54 |
import jdk.internal.module.ModuleInfoWriter;
|
|
55 |
import org.testng.SkipException;
|
|
56 |
import org.testng.annotations.BeforeClass;
|
|
57 |
import org.testng.annotations.Test;
|
|
58 |
import tests.Helper;
|
|
59 |
import tests.JImageGenerator;
|
|
60 |
import tests.JImageGenerator.InMemoryFile;
|
|
61 |
import tests.Result;
|
|
62 |
|
|
63 |
@Test
|
|
64 |
public class JLinkNegativeTest {
|
|
65 |
|
|
66 |
private Helper helper;
|
|
67 |
|
|
68 |
@BeforeClass
|
|
69 |
public void setUp() throws IOException {
|
|
70 |
helper = Helper.newHelper();
|
|
71 |
if (helper == null) {
|
|
72 |
throw new SkipException("Not run");
|
|
73 |
}
|
|
74 |
helper.generateDefaultModules();
|
|
75 |
}
|
|
76 |
|
|
77 |
private void deleteDirectory(Path dir) throws IOException {
|
|
78 |
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
|
|
79 |
@Override
|
|
80 |
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
|
81 |
Files.delete(file);
|
|
82 |
return FileVisitResult.CONTINUE;
|
|
83 |
}
|
|
84 |
|
|
85 |
@Override
|
|
86 |
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
|
|
87 |
Files.delete(dir);
|
|
88 |
return FileVisitResult.CONTINUE;
|
|
89 |
}
|
|
90 |
});
|
|
91 |
}
|
|
92 |
|
|
93 |
public void testModuleNotExist() {
|
|
94 |
helper.generateDefaultImage("failure1").assertFailure("Error: Module failure1 not found");
|
|
95 |
}
|
|
96 |
|
|
97 |
public void testNotExistInAddMods() {
|
|
98 |
// cannot find jmod from --addmods
|
|
99 |
JImageGenerator.getJLinkTask()
|
|
100 |
.modulePath(".")
|
|
101 |
.addMods("not_exist")
|
|
102 |
.output(helper.getImageDir().resolve("failure2"))
|
|
103 |
.call().assertFailure("Error: Module not_exist not found");
|
|
104 |
}
|
|
105 |
|
|
106 |
public void test() throws IOException {
|
|
107 |
helper.generateDefaultJModule("failure3");
|
|
108 |
Path image = helper.generateDefaultImage("failure3").assertSuccess();
|
|
109 |
JImageGenerator.getJLinkTask()
|
|
110 |
.modulePath(helper.defaultModulePath())
|
|
111 |
.output(image)
|
|
112 |
.addMods("leaf1")
|
|
113 |
.limitMods("leaf1")
|
|
114 |
.call().assertFailure("Error: directory already exists: .*failure3.image(\n|\r|.)*");
|
|
115 |
}
|
|
116 |
|
|
117 |
public void testOutputIsFile() throws IOException {
|
|
118 |
// output == file
|
|
119 |
Path image = helper.createNewImageDir("failure4");
|
|
120 |
Files.createFile(image);
|
|
121 |
JImageGenerator.getJLinkTask()
|
|
122 |
.modulePath(helper.defaultModulePath())
|
|
123 |
.output(image)
|
|
124 |
.addMods("leaf1")
|
|
125 |
.call().assertFailure("Error: directory already exists: .*failure4.image(\n|\r|.)*");
|
|
126 |
}
|
|
127 |
|
|
128 |
public void testModuleNotFound() {
|
|
129 |
// limit module is not found
|
|
130 |
Path imageFile = helper.createNewImageDir("test");
|
|
131 |
JImageGenerator.getJLinkTask()
|
|
132 |
.output(imageFile)
|
|
133 |
.addMods("leaf1")
|
|
134 |
.limitMods("leaf1")
|
|
135 |
.limitMods("failure5")
|
|
136 |
.modulePath(helper.defaultModulePath())
|
|
137 |
.call().assertFailure("Error: Module failure5 not found");
|
|
138 |
}
|
|
139 |
|
|
140 |
public void testJmodIsDir() throws IOException {
|
|
141 |
Path imageFile = helper.createNewImageDir("test");
|
|
142 |
Path dirJmod = helper.createNewJmodFile("dir");
|
|
143 |
Files.createDirectory(dirJmod);
|
|
144 |
try {
|
|
145 |
JImageGenerator.getJLinkTask()
|
|
146 |
.output(imageFile)
|
|
147 |
.addMods("dir")
|
|
148 |
.modulePath(helper.defaultModulePath())
|
|
149 |
.call().assertFailure("Error: Module dir not found");
|
|
150 |
} finally {
|
|
151 |
deleteDirectory(dirJmod);
|
|
152 |
}
|
|
153 |
}
|
|
154 |
|
|
155 |
public void testJarIsDir() throws IOException {
|
|
156 |
Path imageFile = helper.createNewImageDir("test");
|
|
157 |
Path dirJar = helper.createNewJarFile("dir");
|
|
158 |
Files.createDirectory(dirJar);
|
|
159 |
try {
|
|
160 |
JImageGenerator.getJLinkTask()
|
|
161 |
.output(imageFile)
|
|
162 |
.addMods("dir")
|
|
163 |
.modulePath(helper.defaultModulePath())
|
|
164 |
.call().assertFailure("Error: Module dir not found");
|
|
165 |
} finally {
|
|
166 |
deleteDirectory(dirJar);
|
|
167 |
}
|
|
168 |
}
|
|
169 |
|
|
170 |
public void testMalformedJar() throws IOException {
|
|
171 |
Path imageFile = helper.createNewImageDir("test");
|
|
172 |
Path jar = helper.createNewJarFile("not_zip");
|
|
173 |
Files.createFile(jar);
|
|
174 |
try {
|
|
175 |
JImageGenerator.getJLinkTask()
|
|
176 |
.output(imageFile)
|
|
177 |
.addMods("not_zip")
|
|
178 |
.modulePath(helper.defaultModulePath())
|
|
179 |
.call().assertFailure("Error: java.util.zip.ZipException: zip file is empty");
|
|
180 |
} finally {
|
|
181 |
deleteDirectory(jar);
|
|
182 |
}
|
|
183 |
}
|
|
184 |
|
|
185 |
public void testMalformedJmod() throws IOException {
|
|
186 |
Path imageFile = helper.createNewImageDir("test");
|
|
187 |
Path jmod = helper.createNewJmodFile("not_zip");
|
|
188 |
Files.createFile(jmod);
|
|
189 |
try {
|
|
190 |
JImageGenerator.getJLinkTask()
|
|
191 |
.output(imageFile)
|
|
192 |
.addMods("not_zip")
|
|
193 |
.modulePath(helper.defaultModulePath())
|
|
194 |
.call().assertFailure("Error: java.util.zip.ZipException: zip file is empty");
|
|
195 |
} finally {
|
|
196 |
deleteDirectory(jmod);
|
|
197 |
}
|
|
198 |
}
|
|
199 |
|
|
200 |
// Temporarily exclude; the jmod tool can no longer be used to create a jmod
|
|
201 |
// with a class in the unnamed package. Find another way, or remove.
|
|
202 |
// public void testAddDefaultPackage() throws IOException {
|
|
203 |
// String moduleName = "hacked1";
|
|
204 |
// Path module = helper.generateModuleCompiledClasses(helper.getJmodSrcDir(), helper.getJmodClassesDir(),
|
|
205 |
// moduleName, Arrays.asList("hacked1.Main", "A", "B"), "leaf1");
|
|
206 |
// JImageGenerator
|
|
207 |
// .getJModTask()
|
|
208 |
// .addClassPath(module)
|
|
209 |
// .jmod(helper.getJmodDir().resolve(moduleName + ".jmod"))
|
|
210 |
// .create().assertSuccess();
|
|
211 |
// Path image = helper.generateDefaultImage(moduleName).assertSuccess();
|
|
212 |
// helper.checkImage(image, moduleName, null, null);
|
|
213 |
// }
|
|
214 |
|
|
215 |
public void testAddSomeTopLevelFiles() throws IOException {
|
|
216 |
String moduleName = "hacked2";
|
|
217 |
Path module = helper.generateModuleCompiledClasses(helper.getJmodSrcDir(), helper.getJmodClassesDir(),
|
|
218 |
moduleName);
|
|
219 |
Files.createFile(module.resolve("top-level-file"));
|
|
220 |
Path jmod = JImageGenerator
|
|
221 |
.getJModTask()
|
|
222 |
.addClassPath(module)
|
|
223 |
.jmod(helper.getJmodDir().resolve(moduleName + ".jmod"))
|
|
224 |
.create().assertSuccess();
|
|
225 |
try {
|
|
226 |
Path image = helper.generateDefaultImage(moduleName).assertSuccess();
|
|
227 |
helper.checkImage(image, moduleName, null, null);
|
|
228 |
} finally {
|
|
229 |
deleteDirectory(jmod);
|
|
230 |
}
|
|
231 |
}
|
|
232 |
|
|
233 |
public void testAddNonStandardSection() throws IOException {
|
|
234 |
String moduleName = "hacked3";
|
|
235 |
Path module = helper.generateDefaultJModule(moduleName).assertSuccess();
|
|
236 |
JImageGenerator.addFiles(module, new InMemoryFile("unknown/A.class", new byte[0]));
|
|
237 |
try {
|
|
238 |
Result result = helper.generateDefaultImage(moduleName);
|
|
239 |
if (result.getExitCode() != 4) {
|
|
240 |
throw new AssertionError("Crash expected");
|
|
241 |
}
|
|
242 |
if (!result.getMessage().contains("java.lang.InternalError: unexpected entry: unknown")) {
|
|
243 |
System.err.println(result.getMessage());
|
|
244 |
throw new AssertionError("InternalError expected");
|
|
245 |
}
|
|
246 |
} finally {
|
|
247 |
deleteDirectory(module);
|
|
248 |
}
|
|
249 |
}
|
|
250 |
|
|
251 |
@Test(enabled = true)
|
|
252 |
public void testSectionsAreFiles() throws IOException {
|
|
253 |
String moduleName = "module";
|
|
254 |
Path jmod = helper.generateDefaultJModule(moduleName).assertSuccess();
|
|
255 |
JImageGenerator.addFiles(jmod,
|
|
256 |
new InMemoryFile("/native", new byte[0]),
|
|
257 |
new InMemoryFile("/conf", new byte[0]),
|
|
258 |
new InMemoryFile("/bin", new byte[0]));
|
|
259 |
try {
|
|
260 |
Result result = helper.generateDefaultImage(moduleName);
|
|
261 |
if (result.getExitCode() != 4) {
|
|
262 |
throw new AssertionError("Crash expected");
|
|
263 |
}
|
|
264 |
if (!result.getMessage().contains("java.lang.InternalError: unexpected entry: ")) {
|
|
265 |
System.err.println(result.getMessage());
|
|
266 |
throw new AssertionError("InternalError expected");
|
|
267 |
}
|
|
268 |
} finally {
|
|
269 |
deleteDirectory(jmod);
|
|
270 |
}
|
|
271 |
}
|
|
272 |
|
|
273 |
public void testDuplicateModule1() throws IOException {
|
|
274 |
String moduleName1 = "dupRes1Jmod1";
|
|
275 |
String moduleName2 = "dupRes1Jmod2";
|
|
276 |
List<String> classNames = Arrays.asList("java.A", "javax.B");
|
|
277 |
Path module1 = helper.generateModuleCompiledClasses(
|
|
278 |
helper.getJmodSrcDir(), helper.getJmodClassesDir(), moduleName1, classNames);
|
|
279 |
Path module2 = helper.generateModuleCompiledClasses(
|
|
280 |
helper.getJmodSrcDir(), helper.getJmodClassesDir(), moduleName2, classNames);
|
|
281 |
|
|
282 |
try (OutputStream out = Files.newOutputStream(module2.resolve("module-info.class"))) {
|
|
283 |
ModuleInfoWriter.write(new ModuleDescriptor.Builder(moduleName1)
|
|
284 |
.requires("java.base").build(), out);
|
|
285 |
}
|
|
286 |
|
|
287 |
Path jmod1 = JImageGenerator.getJModTask()
|
|
288 |
.addClassPath(module1)
|
|
289 |
.jmod(helper.createNewJmodFile(moduleName1))
|
|
290 |
.create()
|
|
291 |
.assertSuccess();
|
|
292 |
Path jmod2 = JImageGenerator.getJModTask()
|
|
293 |
.addClassPath(module2)
|
|
294 |
.jmod(helper.createNewJmodFile(moduleName2))
|
|
295 |
.create()
|
|
296 |
.assertSuccess();
|
|
297 |
try {
|
|
298 |
helper.generateDefaultImage(moduleName1)
|
|
299 |
.assertFailure("Error: Two versions of module dupRes1Jmod1 found in");
|
|
300 |
} finally {
|
|
301 |
deleteDirectory(jmod1);
|
|
302 |
deleteDirectory(jmod2);
|
|
303 |
}
|
|
304 |
}
|
|
305 |
|
|
306 |
public void testDuplicateModule2() throws IOException {
|
|
307 |
String moduleName = "dupRes2Jmod";
|
|
308 |
List<String> classNames = Arrays.asList("java.A", "javax.B");
|
|
309 |
Path module1 = helper.generateModuleCompiledClasses(
|
|
310 |
helper.getJmodSrcDir(), helper.getJmodClassesDir(), moduleName, classNames);
|
|
311 |
Path module2 = helper.generateModuleCompiledClasses(
|
|
312 |
helper.getJarSrcDir(), helper.getJarClassesDir(), moduleName, classNames);
|
|
313 |
|
|
314 |
Path jmod = JImageGenerator.getJModTask()
|
|
315 |
.addClassPath(module1)
|
|
316 |
.jmod(helper.createNewJmodFile(moduleName))
|
|
317 |
.create()
|
|
318 |
.assertSuccess();
|
|
319 |
Path jar = JImageGenerator.createJarFile(helper.getJarDir().resolve(moduleName + ".jar"), module2);
|
|
320 |
Path newJar = helper.getJmodDir().resolve(jar.getFileName());
|
|
321 |
Files.move(jar, newJar);
|
|
322 |
try {
|
|
323 |
helper.generateDefaultImage(moduleName)
|
|
324 |
.assertFailure("Error: Two versions of module dupRes2Jmod found in");
|
|
325 |
} finally {
|
|
326 |
deleteDirectory(jmod);
|
|
327 |
deleteDirectory(newJar);
|
|
328 |
}
|
|
329 |
}
|
|
330 |
|
|
331 |
public void testDuplicateModule3() throws IOException {
|
|
332 |
String moduleName1 = "dupRes3Jar1";
|
|
333 |
String moduleName2 = "dupRes3Jar2";
|
|
334 |
List<String> classNames = Arrays.asList("java.A", "javax.B");
|
|
335 |
Path module1 = helper.generateModuleCompiledClasses(
|
|
336 |
helper.getJarSrcDir(), helper.getJarClassesDir(), moduleName1, classNames);
|
|
337 |
Path module2 = helper.generateModuleCompiledClasses(
|
|
338 |
helper.getJarSrcDir(), helper.getJarClassesDir(), moduleName2, classNames);
|
|
339 |
|
|
340 |
try (OutputStream out = Files.newOutputStream(module2.resolve("module-info.class"))) {
|
|
341 |
ModuleInfoWriter.write(new ModuleDescriptor.Builder(moduleName1)
|
|
342 |
.requires("java.base").build(), out);
|
|
343 |
}
|
|
344 |
|
|
345 |
Path jar1 = JImageGenerator.createJarFile(helper.getJarDir().resolve(moduleName1 + ".jar"), module1);
|
|
346 |
Path jar2 = JImageGenerator.createJarFile(helper.getJarDir().resolve(moduleName2 + ".jar"), module2);
|
|
347 |
try {
|
|
348 |
helper.generateDefaultImage(moduleName1)
|
|
349 |
.assertFailure("Error: Two versions of module dupRes3Jar1 found in");
|
|
350 |
} finally {
|
|
351 |
deleteDirectory(jar1);
|
|
352 |
deleteDirectory(jar2);
|
|
353 |
}
|
|
354 |
}
|
|
355 |
}
|