|
25 | 25 | import org.junit.jupiter.api.io.TempDir; |
26 | 26 |
|
27 | 27 | import java.io.IOException; |
| 28 | +import java.io.InputStream; |
| 29 | +import java.nio.charset.StandardCharsets; |
| 30 | +import java.nio.file.Files; |
28 | 31 | import java.nio.file.Path; |
| 32 | +import java.security.CodeSource; |
| 33 | +import java.security.ProtectionDomain; |
| 34 | +import java.security.cert.Certificate; |
29 | 35 | import java.util.Arrays; |
30 | 36 | import java.util.Collections; |
| 37 | +import java.util.jar.JarEntry; |
| 38 | +import java.util.jar.JarOutputStream; |
31 | 39 |
|
32 | 40 | /** |
33 | 41 | * The gate as the directory loader applies it: what a real plugin jar declares in its MANIFEST decides |
|
45 | 53 | class DirectoryPluginRuntimeManagerApiVersionTest { |
46 | 54 |
|
47 | 55 | private static final ApiVersionGate GATE = DirectoryPluginRuntimeManagerMetadataTest.TEST_GATE; |
| 56 | + private static final ApiVersionGate LEGACY_GATE = ApiVersionGate.forFamilyAllowingUnversioned( |
| 57 | + "test", DirectoryPluginRuntimeManagerApiVersionTest.class, "7.0"); |
48 | 58 |
|
49 | 59 | @TempDir |
50 | 60 | Path tempDir; |
@@ -80,6 +90,70 @@ void testMalformedDeclarationIsRefused() throws IOException { |
80 | 90 | assertRefused(report, "seven-point-two", "7.2"); |
81 | 91 | } |
82 | 92 |
|
| 93 | + @Test |
| 94 | + void testReadableUnversionedLegacyPluginLoads() throws IOException { |
| 95 | + Path root = tempDir.resolve("legacy"); |
| 96 | + DirectoryPluginRuntimeManagerMetadataTest.createPluginJar( |
| 97 | + root.resolve("metadata-test").resolve("metadata-test.jar"), |
| 98 | + MetadataTestPluginFactory.class, "3.1.4", null); |
| 99 | + DirectoryPluginRuntimeManager<PluginFactory> manager = new DirectoryPluginRuntimeManager<>(); |
| 100 | + LoadReport<PluginFactory> report = manager.loadAll(Collections.singletonList(root), |
| 101 | + Thread.currentThread().getContextClassLoader(), PluginFactory.class, null, LEGACY_GATE); |
| 102 | + |
| 103 | + Assertions.assertEquals(1, report.getSuccesses().size(), () -> describe(report)); |
| 104 | + Assertions.assertEquals("3.1.4", report.getSuccesses().get(0).getVersion()); |
| 105 | + manager.discard("metadata-test"); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void testLegacyGateStillRejectsExplicitInvalidDeclarations() throws IOException { |
| 110 | + for (String version : Arrays.asList("", "seven-point-two", "6.0")) { |
| 111 | + Path root = tempDir.resolve("invalid-legacy-" + version); |
| 112 | + DirectoryPluginRuntimeManagerMetadataTest.createPluginJar( |
| 113 | + root.resolve("metadata-test").resolve("metadata-test.jar"), |
| 114 | + MetadataTestPluginFactory.class, "3.1.4", version); |
| 115 | + LoadReport<PluginFactory> report = new DirectoryPluginRuntimeManager<PluginFactory>().loadAll( |
| 116 | + Collections.singletonList(root), Thread.currentThread().getContextClassLoader(), |
| 117 | + PluginFactory.class, null, LEGACY_GATE); |
| 118 | + |
| 119 | + assertRefused(report, "Doris-Test-Plugin-Api-Version", "7.2"); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void testUnreadableDefiningJarIsRefusedInsteadOfUsingAnotherCandidate() throws IOException { |
| 125 | + Path definingJar = tempDir.resolve("unreadable.jar"); |
| 126 | + Files.write(definingJar, "not a jar".getBytes(StandardCharsets.UTF_8)); |
| 127 | + |
| 128 | + assertRefused(loadWithFactoryCodeSource(definingJar, true), |
| 129 | + "Failed to read Doris-Test-Plugin-Api-Version", "metadata-test"); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + void testUnreadableManifestIsRefusedAsApiVersionFailure() throws IOException { |
| 134 | + Path definingJar = tempDir.resolve("bad-manifest.jar"); |
| 135 | + try (JarOutputStream jar = new JarOutputStream(Files.newOutputStream(definingJar))) { |
| 136 | + jar.putNextEntry(new JarEntry("META-INF/MANIFEST.MF")); |
| 137 | + jar.write("invalid manifest header\n".getBytes(StandardCharsets.UTF_8)); |
| 138 | + jar.closeEntry(); |
| 139 | + } |
| 140 | + |
| 141 | + assertRefused(loadWithFactoryCodeSource(definingJar, false), |
| 142 | + "Failed to read Doris-Test-Plugin-Api-Version", "invalid header"); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + void testMissingDefiningJarIsRefusedInsteadOfBeingTreatedAsLegacy() throws IOException { |
| 147 | + assertRefused(loadWithFactoryCodeSource(tempDir.resolve("missing.jar"), true), |
| 148 | + "Failed to read Doris-Test-Plugin-Api-Version", "missing.jar"); |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + void testUnresolvedDefiningJarIsRefusedInsteadOfBeingTreatedAsLegacy() throws IOException { |
| 153 | + assertRefused(loadWithFactoryCodeSource(null, false), |
| 154 | + "Failed to read Doris-Test-Plugin-Api-Version", "Cannot locate defining jar"); |
| 155 | + } |
| 156 | + |
83 | 157 | @Test |
84 | 158 | void testRefusedPluginLeavesNoTraceAndRepeatsIdentically() throws IOException { |
85 | 159 | Path root = tempDir.resolve("repeat"); |
@@ -127,6 +201,51 @@ private LoadReport<PluginFactory> loadWithDeclaredVersion(String rootName, Strin |
127 | 201 | GATE); |
128 | 202 | } |
129 | 203 |
|
| 204 | + private LoadReport<PluginFactory> loadWithFactoryCodeSource(Path definingJar, boolean includeFactory) |
| 205 | + throws IOException { |
| 206 | + Path root = tempDir.resolve("lookup-failure"); |
| 207 | + Path pluginJar = root.resolve("metadata-test").resolve("metadata-test.jar"); |
| 208 | + if (includeFactory) { |
| 209 | + // This readable candidate must not hide a failure to read the actual defining jar. |
| 210 | + DirectoryPluginRuntimeManagerMetadataTest.createPluginJar( |
| 211 | + pluginJar, MetadataTestPluginFactory.class, "3.1.4", null); |
| 212 | + } else { |
| 213 | + Files.createDirectories(pluginJar.getParent()); |
| 214 | + try (JarOutputStream jar = new JarOutputStream(Files.newOutputStream(pluginJar))) { |
| 215 | + jar.putNextEntry(new JarEntry("META-INF/services/" + PluginFactory.class.getName())); |
| 216 | + jar.write((MetadataTestPluginFactory.class.getName() + "\n").getBytes(StandardCharsets.UTF_8)); |
| 217 | + jar.closeEntry(); |
| 218 | + } |
| 219 | + } |
| 220 | + // Define the factory before the separate manifest lookup, with controlled provenance. This makes |
| 221 | + // missing/unreadable metadata deterministic without racing filesystem mutation against loading. |
| 222 | + ClassLoader parent = new FactoryClassLoader(definingJar); |
| 223 | + ClassLoadingPolicy policy = new ClassLoadingPolicy( |
| 224 | + Collections.singletonList(MetadataTestPluginFactory.class.getName())); |
| 225 | + DirectoryPluginRuntimeManager<PluginFactory> manager = new DirectoryPluginRuntimeManager<>(); |
| 226 | + LoadReport<PluginFactory> report = manager.loadAll(Collections.singletonList(root), |
| 227 | + parent, PluginFactory.class, policy, LEGACY_GATE); |
| 228 | + |
| 229 | + Assertions.assertTrue(manager.list().isEmpty(), "failed verification must not publish a handle"); |
| 230 | + Assertions.assertFalse(manager.get("metadata-test").isPresent()); |
| 231 | + return report; |
| 232 | + } |
| 233 | + |
| 234 | + private static class FactoryClassLoader extends ClassLoader { |
| 235 | + FactoryClassLoader(Path definingJar) throws IOException { |
| 236 | + super(Thread.currentThread().getContextClassLoader()); |
| 237 | + String name = MetadataTestPluginFactory.class.getName(); |
| 238 | + String classEntry = name.replace('.', '/') + ".class"; |
| 239 | + try (InputStream in = MetadataTestPluginFactory.class.getResourceAsStream("/" + classEntry)) { |
| 240 | + Assertions.assertNotNull(in); |
| 241 | + byte[] bytes = in.readAllBytes(); |
| 242 | + CodeSource source = definingJar == null ? null |
| 243 | + : new CodeSource(definingJar.toUri().toURL(), (Certificate[]) null); |
| 244 | + defineClass(name, bytes, 0, bytes.length, new ProtectionDomain(source, null)); |
| 245 | + } |
| 246 | + } |
| 247 | + } |
| 248 | + |
130 | 249 | private static void assertRefused(LoadReport<PluginFactory> report, String... mustMention) { |
131 | 250 | Assertions.assertTrue(report.getSuccesses().isEmpty(), |
132 | 251 | "an incompatible plugin must never be published: " + describe(report)); |
|
0 commit comments