|
| 1 | +/* |
| 2 | + * Copyright (c) Forge Development LLC |
| 3 | + * SPDX-License-Identifier: LGPL-3.0-only |
| 4 | + */ |
| 5 | + |
| 6 | +package net.minecraftforge.modlauncher.test; |
| 7 | + |
| 8 | +import cpw.mods.jarhandling.SecureJar; |
| 9 | +import cpw.mods.modlauncher.util.ModuleExceptionEnhancer; |
| 10 | +import net.minecraftforge.securemodules.SecureModuleFinder; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.BeforeAll; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.junit.jupiter.api.io.TempDir; |
| 15 | + |
| 16 | +import java.lang.module.Configuration; |
| 17 | +import java.lang.module.ModuleDescriptor; |
| 18 | +import java.lang.module.ModuleDescriptor.Requires; |
| 19 | +import java.lang.module.ModuleFinder; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.EnumSet; |
| 24 | +import java.util.List; |
| 25 | +import java.util.function.Consumer; |
| 26 | + |
| 27 | +import static org.junit.jupiter.api.Assertions.*; |
| 28 | + |
| 29 | +class ModuleExceptionEnhancerTest { |
| 30 | + private static final SecureJar[] EMPTY = new SecureJar[0]; |
| 31 | + private static SecureJar SIMPLE; |
| 32 | + private static SecureJar SIMPLE2; |
| 33 | + private static SecureJar REQUIES_A; |
| 34 | + private static SecureJar CYCLE_A; |
| 35 | + private static SecureJar CYCLE_B; |
| 36 | + private static SecureJar USES_WITHOUT_READ; |
| 37 | + private static SecureJar PACKAGE_OVERLAP; |
| 38 | + private static SecureJar PACKAGE_OVERLAP_TRANSITIVE; |
| 39 | + |
| 40 | + @BeforeAll |
| 41 | + public static void setUpBeforeClass(@TempDir Path tempDir) throws Exception { |
| 42 | + SIMPLE = makeJar(tempDir, "simple", b -> b |
| 43 | + .exports("pkg.a") |
| 44 | + ); |
| 45 | + SIMPLE2 = makeJar(tempDir, "simple2", b -> b |
| 46 | + .exports("pkg.a") |
| 47 | + ); |
| 48 | + |
| 49 | + REQUIES_A = makeJar(tempDir, "requires.simple", b -> b |
| 50 | + .requires("simple") |
| 51 | + ); |
| 52 | + |
| 53 | + CYCLE_A = makeJar(tempDir, "cycle.a", b -> b |
| 54 | + .requires(EnumSet.of(Requires.Modifier.TRANSITIVE), "cycle.b") |
| 55 | + ); |
| 56 | + |
| 57 | + CYCLE_B = makeJar(tempDir, "cycle.b", b -> b |
| 58 | + .requires("cycle.a") |
| 59 | + ); |
| 60 | + |
| 61 | + USES_WITHOUT_READ = makeJar(tempDir, "use.without.read", b -> b |
| 62 | + .provides("service.pkg.name", List.of("my.impl")) |
| 63 | + ); |
| 64 | + |
| 65 | + PACKAGE_OVERLAP = makeJar(tempDir, "pkg.overlap", b -> b |
| 66 | + .requires("simple") |
| 67 | + .exports("pkg.a") |
| 68 | + ); |
| 69 | + |
| 70 | + PACKAGE_OVERLAP_TRANSITIVE = makeJar(tempDir, "pkg.reader", b -> b |
| 71 | + .requires("simple") |
| 72 | + .requires("simple2") |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + private static SecureJar makeJar(Path path, String name, Consumer<ModuleDescriptor.Builder> consumer) throws Exception { |
| 77 | + return makeJar(path, name, name, consumer); |
| 78 | + } |
| 79 | + |
| 80 | + private static SecureJar makeJar(Path path, String folder, String name, Consumer<ModuleDescriptor.Builder> consumer) throws Exception { |
| 81 | + var builder = ModuleDescriptor.newOpenModule(name); |
| 82 | + consumer.accept(builder); |
| 83 | + var desc = builder.build(); |
| 84 | + var root = path.resolve(folder + ".jar"); |
| 85 | + Files.createDirectories(root); |
| 86 | + ModuleWriter.write(root.resolve("module-info.class"), desc); |
| 87 | + return SecureJar.from(root); |
| 88 | + } |
| 89 | + |
| 90 | + private static RuntimeException resolve(SecureJar... jars) { |
| 91 | + try { |
| 92 | + var finder = SecureModuleFinder.of(jars); |
| 93 | + var names = Arrays.stream(jars).map(SecureJar::name).toList(); |
| 94 | + Configuration.resolve(finder, List.of(Configuration.empty()), ModuleFinder.ofSystem(), names); |
| 95 | + return fail("Expected exception to be thrown"); |
| 96 | + } catch (RuntimeException e) { |
| 97 | + return e; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void not_found() { |
| 103 | + try { |
| 104 | + Configuration.resolve(SecureModuleFinder.of(), List.of(Configuration.empty()), ModuleFinder.of(), List.of("not.found")); |
| 105 | + fail("Expected exception to be thrown"); |
| 106 | + } catch (RuntimeException e) { |
| 107 | + var newException = ModuleExceptionEnhancer.enhance(e, EMPTY); |
| 108 | + assertTrue(e == newException, "Enhanced exception with no extra info: " + e.getMessage()); |
| 109 | + assertEquals("Module not.found not found", e.getMessage(), "Unexpected exception message: " + e.getMessage()); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + void not_found_required() { |
| 115 | + var jars = new SecureJar[] { REQUIES_A }; |
| 116 | + var e = resolve(jars); |
| 117 | + var newException = ModuleExceptionEnhancer.enhance(e, jars); |
| 118 | + assertFalse(e == newException, "Failed to enhance exception: " + e.getMessage()); |
| 119 | + var msg = newException.getMessage(); |
| 120 | + assertTrue(msg.contains("Module simple not found, required by requires.simple"), "Vanilla message not found in enhanced exception: " + msg); |
| 121 | + assertTrue(msg.contains("requires.simple.jar"), "Path not found in message: " + msg); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + void cycle() { |
| 126 | + var jars = new SecureJar[] { CYCLE_A, CYCLE_B }; |
| 127 | + var e = resolve(jars); |
| 128 | + var newException = ModuleExceptionEnhancer.enhance(e, jars); |
| 129 | + assertFalse(e == newException, "Failed to enhance exception: " + e.getMessage()); |
| 130 | + var msg = newException.getMessage(); |
| 131 | + assertTrue(msg.contains("Cycle detected: cycle.a -> cycle.b -> cycle.a"), "Vanilla message not found in enhanced exception: " + msg); |
| 132 | + assertTrue(msg.contains("cycle.a.jar"), "Cycle A path not found in message: " + msg); |
| 133 | + assertTrue(msg.contains("cycle.b.jar"), "Cycle B path not found in message: " + msg); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + void uses_without_read() { |
| 138 | + var jars = new SecureJar[] { USES_WITHOUT_READ }; |
| 139 | + var e = resolve(jars); |
| 140 | + var newException = ModuleExceptionEnhancer.enhance(e, jars); |
| 141 | + assertFalse(e == newException, "Failed to enhance exception: " + e.getMessage()); |
| 142 | + var msg = newException.getMessage(); |
| 143 | + assertTrue(msg.contains("Module use.without.read does not read a module that exports service.pkg"), "Vanilla message not found in enhanced exception: " + msg); |
| 144 | + assertTrue(msg.contains("use.without.read.jar"), "Path not found in message: " + msg); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + void package_overlap() { |
| 149 | + var jars = new SecureJar[] { SIMPLE, PACKAGE_OVERLAP }; |
| 150 | + var e = resolve(jars); |
| 151 | + var newException = ModuleExceptionEnhancer.enhance(e, jars); |
| 152 | + assertFalse(e == newException, "Failed to enhance exception: " + e.getMessage()); |
| 153 | + var msg = newException.getMessage(); |
| 154 | + assertTrue(msg.contains("Module pkg.overlap contains package pkg.a, module simple exports package pkg.a to pkg.overlap"), "Vanilla message not found in enhanced exception: " + msg); |
| 155 | + assertTrue(msg.contains("simple.jar"), "Simple path not found in message: " + msg); |
| 156 | + assertTrue(msg.contains("pkg.overlap.jar"), "Overlap path not found in message: " + msg); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + void reader_package_overlap() { |
| 161 | + var jars = new SecureJar[] { SIMPLE, SIMPLE2, PACKAGE_OVERLAP_TRANSITIVE }; |
| 162 | + var e = resolve(jars); |
| 163 | + var newException = ModuleExceptionEnhancer.enhance(e, jars); |
| 164 | + assertFalse(e == newException, "Failed to enhance exception: " + e.getMessage()); |
| 165 | + var msg = newException.getMessage(); |
| 166 | + // The order is random (uses a hash set with non-determinstic hashes) so we check for both |
| 167 | + assertTrue( |
| 168 | + msg.contains("Modules simple and simple2 export package pkg.a to module pkg.reader") || |
| 169 | + msg.contains("Modules simple2 and simple export package pkg.a to module pkg.reader"), |
| 170 | + "Vanilla message not found in enhanced exception: " + msg |
| 171 | + ); |
| 172 | + assertTrue(msg.contains("simple.jar"), "Simple path not found in message: " + msg); |
| 173 | + assertTrue(msg.contains("simple2.jar"), "Simple2 path not found in message: " + msg); |
| 174 | + assertTrue(msg.contains("pkg.reader.jar"), "Reader path not found in message: " + msg); |
| 175 | + } |
| 176 | + |
| 177 | + // I don't know how to trigger: |
| 178 | + // resolveFail("Module %s reads another module named %s", name1, name1); |
| 179 | + // resolveFail("Module %s reads more than one module named %s", name1, name2); |
| 180 | + |
| 181 | +} |
0 commit comments