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