1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
|
/**
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @summary Negative tests for jlink
* @bug 8130861
* @bug 8174718
* @bug 8189671
* @author Andrei Eremeev
* @library ../lib
* @modules java.base/jdk.internal.jimage
* java.base/jdk.internal.module
* jdk.jdeps/com.sun.tools.classfile
* jdk.jlink/jdk.tools.jlink.internal
* jdk.jlink/jdk.tools.jmod
* jdk.jlink/jdk.tools.jimage
* jdk.compiler
* @build tests.*
* @run testng JLinkNegativeTest
*/
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.module.ModuleDescriptor;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import jdk.internal.module.ModuleInfoWriter;
import org.testng.SkipException;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import tests.Helper;
import tests.JImageGenerator;
import tests.JImageGenerator.InMemoryFile;
import tests.Result;
@Test
public class JLinkNegativeTest {
private Helper helper;
@BeforeClass
public void setUp() throws IOException {
helper = Helper.newHelper();
if (helper == null) {
throw new SkipException("Not run");
}
helper.generateDefaultModules();
}
private void deleteDirectory(Path dir) throws IOException {
Files.walkFileTree(dir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
}
public void testModuleNotExist() {
helper.generateDefaultImage("failure1").assertFailure("Error: Module failure1 not found");
}
public void testNotExistInAddMods() {
// cannot find jmod from --add-modules
JImageGenerator.getJLinkTask()
.modulePath(".")
.addMods("not_exist")
.output(helper.getImageDir().resolve("failure2"))
.call().assertFailure("Error: Module not_exist not found");
}
public void test() throws IOException {
helper.generateDefaultJModule("failure3");
Path image = helper.generateDefaultImage("failure3").assertSuccess();
JImageGenerator.getJLinkTask()
.modulePath(helper.defaultModulePath())
.output(image)
.addMods("leaf1")
.limitMods("leaf1")
.call().assertFailure("Error: directory already exists: .*failure3.image(\n|\r|.)*");
}
public void testOutputIsFile() throws IOException {
// output == file
Path image = helper.createNewImageDir("failure4");
Files.createFile(image);
JImageGenerator.getJLinkTask()
.modulePath(helper.defaultModulePath())
.output(image)
.addMods("leaf1")
.call().assertFailure("Error: directory already exists: .*failure4.image(\n|\r|.)*");
}
public void testModuleNotFound() {
// limit module is not found
Path imageFile = helper.createNewImageDir("test");
JImageGenerator.getJLinkTask()
.output(imageFile)
.addMods("leaf1")
.limitMods("leaf1")
.limitMods("failure5")
.modulePath(helper.defaultModulePath())
.call().assertFailure("Error: Module failure5 not found");
}
public void testJmodIsDir() throws IOException {
Path imageFile = helper.createNewImageDir("test");
Path dirJmod = helper.createNewJmodFile("dir");
Files.createDirectory(dirJmod);
try {
JImageGenerator.getJLinkTask()
.output(imageFile)
.addMods("dir")
.modulePath(helper.defaultModulePath())
.call().assertFailure("Error: Module dir not found");
} finally {
deleteDirectory(dirJmod);
}
}
public void testJarIsDir() throws IOException {
Path imageFile = helper.createNewImageDir("test");
Path dirJar = helper.createNewJarFile("dir");
Files.createDirectory(dirJar);
try {
JImageGenerator.getJLinkTask()
.output(imageFile)
.addMods("dir")
.modulePath(helper.defaultModulePath())
.call().assertFailure("Error: Module dir not found");
} finally {
deleteDirectory(dirJar);
}
}
public void testMalformedJar() throws IOException {
Path imageFile = helper.createNewImageDir("test");
Path jar = helper.createNewJarFile("not_zip");
Files.createFile(jar);
try {
JImageGenerator.getJLinkTask()
.output(imageFile)
.addMods("not_zip")
.modulePath(helper.defaultModulePath())
.call().assertFailure("Error: Error reading");
} finally {
deleteDirectory(jar);
}
}
public void testMalformedJmod() throws IOException {
Path imageFile = helper.createNewImageDir("test");
Path jmod = helper.createNewJmodFile("not_zip");
Files.createFile(jmod);
try {
JImageGenerator.getJLinkTask()
.output(imageFile)
.addMods("not_zip")
.modulePath(helper.defaultModulePath())
.call().assertFailure("Error: java.io.IOException: Invalid JMOD file");
} finally {
deleteDirectory(jmod);
}
}
private static File createJarFile(File dir, String filename, String pkg, String name) throws IOException {
File jarFile = new File(dir, filename + ".jar");
try (JarOutputStream output = new JarOutputStream(new FileOutputStream(jarFile))) {
JarEntry entry = new JarEntry(filename + "/" + pkg + "/" + name);
output.putNextEntry(entry);
}
return jarFile;
}
public void testAutomaticModuleAsRoot() throws IOException {
Path imageFile = helper.createNewImageDir("test");
String jarName = "myautomod";
File jarFile = createJarFile(new File("jars"), jarName, "com/acme", "Bar.class");
try {
JImageGenerator.getJLinkTask()
.output(imageFile)
.addMods(jarName)
.modulePath(helper.defaultModulePath())
.call().assertFailure("Error: automatic module cannot be used with jlink: " + jarName);
} finally {
jarFile.delete();
}
}
public void testAutomaticModuleAsDependency() throws IOException {
Path imageFile = helper.createNewImageDir("test");
String autoJarName = "myautomod";
File autoJarFile = createJarFile(new File("jars"), autoJarName, "com/acme", "Bar.class");
String rootMod = "autodepender";
helper.generateDefaultJModule(rootMod, autoJarName).assertSuccess();
try {
JImageGenerator.getJLinkTask()
.output(imageFile)
.addMods(rootMod)
.modulePath(helper.defaultModulePath())
.call().assertFailure("Error: automatic module cannot be used with jlink: " + autoJarName);
} finally {
autoJarFile.delete();
}
}
// Temporarily exclude; the jmod tool can no longer be used to create a jmod
// with a class in the unnamed package. Find another way, or remove.
// public void testAddDefaultPackage() throws IOException {
// String moduleName = "hacked1";
// Path module = helper.generateModuleCompiledClasses(helper.getJmodSrcDir(), helper.getJmodClassesDir(),
// moduleName, Arrays.asList("hacked1.Main", "A", "B"), "leaf1");
// JImageGenerator
// .getJModTask()
// .addClassPath(module)
// .jmod(helper.getJmodDir().resolve(moduleName + ".jmod"))
// .create().assertSuccess();
// Path image = helper.generateDefaultImage(moduleName).assertSuccess();
// helper.checkImage(image, moduleName, null, null);
// }
public void testAddSomeTopLevelFiles() throws IOException {
String moduleName = "hacked2";
Path module = helper.generateModuleCompiledClasses(helper.getJmodSrcDir(), helper.getJmodClassesDir(),
moduleName);
Files.createFile(module.resolve("top-level-file"));
Path jmod = JImageGenerator
.getJModTask()
.addClassPath(module)
.jmod(helper.getJmodDir().resolve(moduleName + ".jmod"))
.create().assertSuccess();
try {
Path image = helper.generateDefaultImage(moduleName).assertSuccess();
helper.checkImage(image, moduleName, null, null);
} finally {
deleteDirectory(jmod);
}
}
public void testAddNonStandardSection() throws IOException {
String moduleName = "hacked3";
Path module = helper.generateDefaultJModule(moduleName).assertSuccess();
JImageGenerator.addFiles(module, new InMemoryFile("unknown/A.class", new byte[0]));
try {
Result result = helper.generateDefaultImage(moduleName);
System.err.println(result.getMessage());
if (result.getExitCode() == 0) {
throw new AssertionError("Crash expected");
}
} finally {
deleteDirectory(module);
}
}
@Test(enabled = true)
public void testSectionsAreFiles() throws IOException {
String moduleName = "hacked4";
Path jmod = helper.generateDefaultJModule(moduleName).assertSuccess();
JImageGenerator.addFiles(jmod,
new InMemoryFile("/lib", new byte[0]),
new InMemoryFile("/conf", new byte[0]),
new InMemoryFile("/bin", new byte[0]));
try {
Result result = helper.generateDefaultImage(moduleName);
System.err.println(result.getMessage());
if (result.getExitCode() == 0) {
throw new AssertionError("Crash expected");
}
} finally {
deleteDirectory(jmod);
}
}
public void testDuplicateModule1() throws IOException {
String moduleName1 = "dupRes1Jmod1";
String moduleName2 = "dupRes1Jmod2";
List<String> classNames = Arrays.asList("java.A", "javax.B");
Path module1 = helper.generateModuleCompiledClasses(
helper.getJmodSrcDir(), helper.getJmodClassesDir(), moduleName1, classNames);
Path module2 = helper.generateModuleCompiledClasses(
helper.getJmodSrcDir(), helper.getJmodClassesDir(), moduleName2, classNames);
try (OutputStream out = Files.newOutputStream(module2.resolve("module-info.class"))) {
ModuleInfoWriter.write(ModuleDescriptor.newModule(moduleName1)
.requires("java.base").build(), out);
}
Path jmod1 = JImageGenerator.getJModTask()
.addClassPath(module1)
.jmod(helper.createNewJmodFile(moduleName1))
.create()
.assertSuccess();
Path jmod2 = JImageGenerator.getJModTask()
.addClassPath(module2)
.jmod(helper.createNewJmodFile(moduleName2))
.create()
.assertSuccess();
try {
helper.generateDefaultImage(moduleName1)
.assertFailure("Error: Two versions of module dupRes1Jmod1 found in");
} finally {
deleteDirectory(jmod1);
deleteDirectory(jmod2);
}
}
public void testDuplicateModule2() throws IOException {
String moduleName = "dupRes2Jmod";
List<String> classNames = Arrays.asList("java.A", "javax.B");
Path module1 = helper.generateModuleCompiledClasses(
helper.getJmodSrcDir(), helper.getJmodClassesDir(), moduleName, classNames);
Path module2 = helper.generateModuleCompiledClasses(
helper.getJarSrcDir(), helper.getJarClassesDir(), moduleName, classNames);
Path jmod = JImageGenerator.getJModTask()
.addClassPath(module1)
.jmod(helper.createNewJmodFile(moduleName))
.create()
.assertSuccess();
Path jar = JImageGenerator.createJarFile(helper.getJarDir().resolve(moduleName + ".jar"), module2);
Path newJar = helper.getJmodDir().resolve(jar.getFileName());
Files.move(jar, newJar);
try {
helper.generateDefaultImage(moduleName)
.assertFailure("Error: Two versions of module dupRes2Jmod found in");
} finally {
deleteDirectory(jmod);
deleteDirectory(newJar);
}
}
public void testDuplicateModule3() throws IOException {
String moduleName1 = "dupRes3Jar1";
String moduleName2 = "dupRes3Jar2";
List<String> classNames = Arrays.asList("java.A", "javax.B");
Path module1 = helper.generateModuleCompiledClasses(
helper.getJarSrcDir(), helper.getJarClassesDir(), moduleName1, classNames);
Path module2 = helper.generateModuleCompiledClasses(
helper.getJarSrcDir(), helper.getJarClassesDir(), moduleName2, classNames);
try (OutputStream out = Files.newOutputStream(module2.resolve("module-info.class"))) {
ModuleInfoWriter.write(ModuleDescriptor.newModule(moduleName1)
.requires("java.base").build(), out);
}
Path jar1 = JImageGenerator.createJarFile(helper.getJarDir().resolve(moduleName1 + ".jar"), module1);
Path jar2 = JImageGenerator.createJarFile(helper.getJarDir().resolve(moduleName2 + ".jar"), module2);
try {
helper.generateDefaultImage(moduleName1)
.assertFailure("Error: Two versions of module dupRes3Jar1 found in");
} finally {
deleteDirectory(jar1);
deleteDirectory(jar2);
}
}
public void testInconsistentModuleInfo() throws IOException {
String moduleName = "inconsistentJar";
List<String> classNames = Arrays.asList("xorg.acme.internal.B");
Path module = helper.generateModuleCompiledClasses(
helper.getJarSrcDir(), helper.getJarClassesDir(), moduleName, classNames);
try (OutputStream out = Files.newOutputStream(module.resolve("module-info.class"))) {
ModuleInfoWriter.write(ModuleDescriptor.newModule(moduleName)
.requires("java.base")
.packages(Set.of("org.acme.internal"))
.build(), out);
}
Path jar = JImageGenerator.createJarFile(helper.getJarDir().resolve(moduleName + ".jar"), module);
try {
helper.generateDefaultImage(moduleName)
.assertFailure("Module inconsistentJar's descriptor indicates the set of packages is : " +
"[org.acme.internal], but module contains packages: [xorg.acme.internal]");
} finally {
deleteDirectory(jar);
}
}
}
|