From dff4a69f3da7d6d696da8c9fcd2c02e775232058 Mon Sep 17 00:00:00 2001 From: BenCodez <17074231+BenCodez@users.noreply.github.com> Date: Sun, 22 Nov 2026 19:20:34 +0000 Subject: [PATCH 1/5] Update compatible SQLite and NeoForge dependencies Preserve verified offline SQLite natives and native loading overrides. Use NeoForge 21.1.267 on the existing Minecraft 1.21.1 and Java 21 line. --- VotingPlugin/pom.xml | 24 +-- .../util/SqliteNativeLibrary.java | 95 ++++++++++- .../tests/packaging/PackagedArtifactTest.java | 128 ++++++++++++-- .../tests/util/SqliteNativeLibraryTest.java | 160 +++++++++++++++++- docs/jar-packaging.md | 51 +++--- 5 files changed, 397 insertions(+), 61 deletions(-) diff --git a/VotingPlugin/pom.xml b/VotingPlugin/pom.xml index e588f8a48e..d8c8fd2d8a 100644 --- a/VotingPlugin/pom.xml +++ b/VotingPlugin/pom.xml @@ -16,7 +16,7 @@ 21 21 3.4.0 - 21.1.211 + 21.1.267 @@ -225,24 +225,8 @@ META-INF/versions/** - - org.xerial:sqlite-jdbc - - - org/sqlite/native/FreeBSD/** - org/sqlite/native/Linux-Musl/** - org/sqlite/native/Linux/aarch64/** - org/sqlite/native/Linux/arm/** - org/sqlite/native/Linux/armv6/** - org/sqlite/native/Linux/armv7/** - org/sqlite/native/Linux/ppc64/** - org/sqlite/native/Linux/riscv64/** - org/sqlite/native/Linux/x86/** - org/sqlite/native/Mac/** - org/sqlite/native/Windows/** - - + *:* @@ -396,7 +380,7 @@ org.xerial sqlite-jdbc - 3.53.4.0 + 3.54.1.0 org.checkerframework diff --git a/VotingPlugin/src/main/java/com/bencodez/votingplugin/util/SqliteNativeLibrary.java b/VotingPlugin/src/main/java/com/bencodez/votingplugin/util/SqliteNativeLibrary.java index fc3513ff59..c05041dc2b 100644 --- a/VotingPlugin/src/main/java/com/bencodez/votingplugin/util/SqliteNativeLibrary.java +++ b/VotingPlugin/src/main/java/com/bencodez/votingplugin/util/SqliteNativeLibrary.java @@ -13,6 +13,7 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.HexFormat; +import java.util.Map; import java.util.Set; import java.util.UUID; import java.util.jar.JarEntry; @@ -20,13 +21,57 @@ import org.sqlite.util.OSInfo; -/** Prepares the current Xerial SQLite native without embedding every target in the plugin JAR. */ +/** Verifies the embedded Xerial SQLite native, with a pinned-driver fallback for custom packaging. */ public final class SqliteNativeLibrary { - static final String DRIVER_FILE = "sqlite-jdbc-3.53.4.0.jar"; - static final String DRIVER_SHA256 = "bcb1f51e36f940867e83342f9efbf5968ac44a6bef4d397bb4af7b17b45cd2fb"; + static final String DRIVER_FILE = "sqlite-jdbc-3.54.1.0.jar"; + static final String DRIVER_SHA256 = "f8dce420a6016278497f4f0560f65602b9db858de141b541cf91a4644ebe4639"; private static final URI DRIVER_URI = URI.create("https://maven-central.storage-download.googleapis.com/maven2/" - + "org/xerial/sqlite-jdbc/3.53.4.0/" + DRIVER_FILE); + + "org/xerial/sqlite-jdbc/3.54.1.0/" + DRIVER_FILE); private static final long MAX_DRIVER_BYTES = 16L * 1024L * 1024L; + private static final int MAX_NATIVE_BYTES = 2 * 1024 * 1024; + // Digests from the official artifact identified by DRIVER_SHA256. Never trust a resource + // supplied by a different driver or classloader merely because its path matches. + private static final Map NATIVE_SHA256 = Map.ofEntries( + Map.entry("org/sqlite/native/FreeBSD/aarch64/libsqlitejdbc.so", + "d450f90bfaf858c2e52505b4370c99ede31c07430c66c3231132a6920b26ed5c"), + Map.entry("org/sqlite/native/FreeBSD/x86/libsqlitejdbc.so", + "61f86fe5144d8965a3e64bfa48af874f6901167a8b3e4e6a370500640ac8ac1b"), + Map.entry("org/sqlite/native/FreeBSD/x86_64/libsqlitejdbc.so", + "04584ec9a87bc2d59e707949f2c9db3e206108dd8fce1cf1797f13d7e0760f30"), + Map.entry("org/sqlite/native/Linux/aarch64/libsqlitejdbc.so", + "2deb508458a39049204b0e92db5d82e53598eb4e2d34dff3ee29cc47a38f83ee"), + Map.entry("org/sqlite/native/Linux/arm/libsqlitejdbc.so", + "9b50eb59ae3a2d2a956287c53b3c6cf87cd5ffff58c00889fbd5a7a9ef220482"), + Map.entry("org/sqlite/native/Linux/armv6/libsqlitejdbc.so", + "3601ac7a21215b9dc928db447a63a28383dad419e579d77168ab6c91acbbbf3b"), + Map.entry("org/sqlite/native/Linux/armv7/libsqlitejdbc.so", + "43f0c78a6de21ce39539a0df93edf19bcef79c95358467128459d551666f098c"), + Map.entry("org/sqlite/native/Linux/ppc64/libsqlitejdbc.so", + "1a2f43080088c6230aa1554e1fc198e1961f8ef8f5df94656a612fee97ada41a"), + Map.entry("org/sqlite/native/Linux/riscv64/libsqlitejdbc.so", + "cbfb0bed9e5feca3dcec0fcd95d9e9c21f90382adc62be64fcd3300772d7c74d"), + Map.entry("org/sqlite/native/Linux/x86/libsqlitejdbc.so", + "7440830f45d7b2e32f16c27cc6d61e65070f9a7ea6ceff731a141943faf97512"), + Map.entry("org/sqlite/native/Linux/x86_64/libsqlitejdbc.so", + "9149f234a03da04caba72ce534dfa0f0e5b999ae8d03d630034be65f24c7b44c"), + Map.entry("org/sqlite/native/Linux-Musl/aarch64/libsqlitejdbc.so", + "126521645e72d05b57442583ab33b2c8494fd04d60d1be442a15536723e26861"), + Map.entry("org/sqlite/native/Linux-Musl/x86/libsqlitejdbc.so", + "cf9e993ea2a052f8ec3931a202f4fea0049e7f0276b3726c1ac87d52064c45c0"), + Map.entry("org/sqlite/native/Linux-Musl/x86_64/libsqlitejdbc.so", + "b21aac0da3a80a0c4cee035d5919021c991ee17c8284a76b38ab7b62e68bab52"), + Map.entry("org/sqlite/native/Mac/aarch64/libsqlitejdbc.dylib", + "47fa68b43f34552fbfedeed25e82875a42cfd53e81c33c7cee20ac3f729c060e"), + Map.entry("org/sqlite/native/Mac/x86_64/libsqlitejdbc.dylib", + "51d30fe476f0170cd42102baabb82c4b39d43996c9209cdc60876a4b928d85f7"), + Map.entry("org/sqlite/native/Windows/aarch64/sqlitejdbc.dll", + "59ec28822968199a7a94feb62a44357ab201c7bc3c55ebdf0e3e5ece840a45b6"), + Map.entry("org/sqlite/native/Windows/armv7/sqlitejdbc.dll", + "175095c594d0fa73e819d7b1066a4d8a19265d89bbe4bf0c629a29dc233b6e04"), + Map.entry("org/sqlite/native/Windows/x86/sqlitejdbc.dll", + "d03f852a3350e65f6ed8244e6f348d154aca10c89820e1387885dd95b4fa0445"), + Map.entry("org/sqlite/native/Windows/x86_64/sqlitejdbc.dll", + "4896f8ba8824a85f10f31383e38b9542b6043886a0c834e0d6c8606b02f82308")); private static final long STALE_NATIVE_MILLIS = 24L * 60L * 60L * 1000L; private SqliteNativeLibrary() { @@ -34,7 +79,11 @@ private SqliteNativeLibrary() { /** Ensures Xerial can load the native for this operating system and architecture. */ public static synchronized void ensureAvailable(Path directory) throws IOException { - if (System.getProperty("org.sqlite.lib.path") != null) { + String configuredName = System.getProperty("org.sqlite.lib.name"); + // A custom name alone selects a library through Xerial/java.library.path. + // The standard name still uses our verified extraction, including on noexec temp mounts. + if (System.getProperty("org.sqlite.lib.path") != null + || (configuredName != null && !configuredName.equals(System.mapLibraryName("sqlitejdbc")))) { try { if (!org.sqlite.core.NativeDB.load()) throw new IOException("Configured SQLite native library did not load"); return; @@ -53,10 +102,21 @@ static Path prepareNative(Path directory, String folder, ClassLoader resourceLoa throws IOException { String libraryName = nativeLibraryName(folder); String resource = "org/sqlite/native/" + folder + "/" + libraryName; - if (resourceLoader.getResource(resource) != null) return null; + byte[] embeddedBytes = null; + try (InputStream embedded = resourceLoader.getResourceAsStream(resource)) { + if (embedded != null) { + String expected = NATIVE_SHA256.get(resource); + byte[] bytes = embedded.readNBytes(MAX_NATIVE_BYTES + 1); + if (expected == null || bytes.length == 0 || bytes.length > MAX_NATIVE_BYTES + || !expected.equals(sha256(bytes))) { + throw new IOException("Embedded SQLite native failed SHA-256 verification: " + resource); + } + embeddedBytes = bytes; + } + } Files.createDirectories(directory); Path driver = directory.resolve(DRIVER_FILE); - if (!hasExpectedDigest(driver, DRIVER_SHA256)) { + if (embeddedBytes == null && !hasExpectedDigest(driver, DRIVER_SHA256)) { try { downloadVerified(driver, fetcher); } catch (IOException failure) { @@ -70,7 +130,18 @@ static Path prepareNative(Path directory, String folder, ClassLoader resourceLoa Path nativeDirectory = platformDirectory.resolve("load-" + UUID.randomUUID()); Files.createDirectories(nativeDirectory); Path nativeLibrary = nativeDirectory.resolve(libraryName); - extractVerifiedEntry(driver, resource, nativeLibrary); + if (embeddedBytes != null) { + // Keep loading in the plugin directory, including when the JVM temp directory + // is read-only or mounted noexec on a previously fallback-backed platform. + Path temporary = Files.createTempFile(nativeDirectory, libraryName + ".", ".extract"); + setPrivatePermissions(temporary); + try { + Files.write(temporary, embeddedBytes); + moveReplacing(temporary, nativeLibrary); + } finally { Files.deleteIfExists(temporary); } + } else { + extractVerifiedEntry(driver, resource, nativeLibrary); + } nativeLibrary.toFile().deleteOnExit(); nativeDirectory.toFile().deleteOnExit(); return nativeLibrary; @@ -187,6 +258,14 @@ private static void moveReplacing(Path source, Path target) throws IOException { } } + private static String sha256(byte[] bytes) { + try { + return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes)); + } catch (NoSuchAlgorithmException impossible) { + throw new IllegalStateException("SHA-256 is unavailable", impossible); + } + } + private static boolean hasExpectedDigest(Path file, String expected) throws IOException { if (!Files.isRegularFile(file)) return false; try (InputStream input = Files.newInputStream(file)) { diff --git a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/packaging/PackagedArtifactTest.java b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/packaging/PackagedArtifactTest.java index 4c83036010..940624e02e 100644 --- a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/packaging/PackagedArtifactTest.java +++ b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/packaging/PackagedArtifactTest.java @@ -63,19 +63,65 @@ void containsOneRelocatedRuntimeWithoutUnusedHttpCrypto() throws Exception { assertNull(artifact.getEntry("com/zaxxer/hikari/HikariDataSource.class")); assertNull(artifact.getEntry("com/tcoded/folialib/FoliaLib.class")); assertFalse(artifact.stream().anyMatch(entry -> entry.getName().contains("/bouncycastle/"))); - assertNotNull(artifact.getEntry("org/sqlite/native/Linux/x86_64/libsqlitejdbc.so"), - "The common Linux x86_64 SQLite runtime must remain available offline"); - assertFalse(artifact.stream().anyMatch(entry -> !entry.isDirectory() - && entry.getName().startsWith("org/sqlite/native/") - && !entry.getName().startsWith("org/sqlite/native/Linux/x86_64/")), - "Only the common offline SQLite native may be embedded"); + } long artifactBytes = Files.size(artifactPath); warnIfArtifactExceedsSizeTarget(artifactBytes, System.out); - System.out.printf("VotingPlugin downloadable artifact: %,d bytes; duplicate Rhino, external crypto and uncommon SQLite natives absent%n", + System.out.printf("VotingPlugin downloadable artifact: %,d bytes; duplicate Rhino and external crypto absent; all supported SQLite natives retained%n", Files.size(artifactPath)); } + @Test + void everyOfficialSqliteNativeIsPackagedOnceAndVerifiedOffline(@TempDir Path directory) throws Exception { + Path driver = Path.of(org.sqlite.JDBC.class.getProtectionDomain().getCodeSource().getLocation().toURI()); + byte[] officialBytes = Files.readAllBytes(driver); + assertEquals("f8dce420a6016278497f4f0560f65602b9db858de141b541cf91a4644ebe4639", + java.util.HexFormat.of().formatHex(java.security.MessageDigest.getInstance("SHA-256") + .digest(officialBytes)), "Expected the pinned official SQLite driver"); + Files.writeString(directory.resolve("sqlite-jdbc-3.53.4.0.jar"), "previous installation cache"); + try (JarFile official = new JarFile(driver.toFile()); + JarFile artifact = new JarFile(packagedJar().toFile()); + URLClassLoader loader = new URLClassLoader(new URL[] { packagedJar().toUri().toURL() }, + ClassLoader.getPlatformClassLoader())) { + var expected = official.stream().filter(entry -> !entry.isDirectory() + && entry.getName().startsWith("org/sqlite/native/")).map(java.util.jar.JarEntry::getName) + .collect(java.util.stream.Collectors.toSet()); + var actual = artifact.stream().filter(entry -> !entry.isDirectory() + && entry.getName().startsWith("org/sqlite/native/")).map(java.util.jar.JarEntry::getName) + .collect(java.util.stream.Collectors.toSet()); + assertEquals(20, expected.size(), "Cover every supported target, not just this host"); + assertEquals(expected, actual, "No offline target may be removed or shadowed by another dependency"); + Class nativeType = Class.forName("com.bencodez.votingplugin.util.SqliteNativeLibrary", true, loader); + Class fetcherType = Class.forName(nativeType.getName() + "$ArtifactFetcher", true, loader); + Object noNetwork = java.lang.reflect.Proxy.newProxyInstance(loader, new Class[] { fetcherType }, + (proxy, method, arguments) -> { throw new AssertionError("packaged offline native must not fetch"); }); + var prepare = nativeType.getDeclaredMethod("prepareNative", Path.class, String.class, + ClassLoader.class, fetcherType); + prepare.setAccessible(true); + for (String name : expected) { + assertEquals(1, artifact.stream().filter(entry -> entry.getName().equals(name)).count(), name); + assertArrayEquals(official.getInputStream(official.getEntry(name)).readAllBytes(), + artifact.getInputStream(artifact.getEntry(name)).readAllBytes(), name); + String folder = name.substring("org/sqlite/native/".length(), name.lastIndexOf('/')); + Path prepared = (Path) prepare.invoke(null, directory, folder, loader, noNetwork); + assertTrue(prepared.startsWith(directory.resolve("sqlite-native")), name); + assertArrayEquals(official.getInputStream(official.getEntry(name)).readAllBytes(), + Files.readAllBytes(prepared), name); + } + // Exercise integrity rejection using the packaged loader implementation, with no Maven classes. + ClassLoader corrupt = new ClassLoader(ClassLoader.getPlatformClassLoader()) { + @Override public java.io.InputStream getResourceAsStream(String name) { + return new java.io.ByteArrayInputStream(new byte[] { 1, 2, 3 }); + } + }; + var failure = org.junit.jupiter.api.Assertions.assertThrows(java.lang.reflect.InvocationTargetException.class, + () -> prepare.invoke(null, directory, "Mac/x86_64", corrupt, noNetwork)); + assertTrue(failure.getCause() instanceof java.io.IOException); + assertTrue(failure.getCause().getMessage().contains("SHA-256")); + assertFalse(Files.exists(directory.resolve("sqlite-jdbc-3.54.1.0.jar"))); + } + } + @Test void artifactSizeTargetWarnsWithoutRejectingOversizedArtifacts() { ByteArrayOutputStream output = new ByteArrayOutputStream(); @@ -101,12 +147,70 @@ private static void warnIfArtifactExceedsSizeTarget(long artifactBytes, PrintStr void packagedNeoForgeRuntimeStartsAndClosesWithoutTestDependencies(@TempDir Path directory) throws Exception { URL jar = packagedJar().toUri().toURL(); URL platformSlf4j = org.slf4j.Logger.class.getProtectionDomain().getCodeSource().getLocation(); - try (URLClassLoader loader = new URLClassLoader(new URL[] { jar, platformSlf4j }, - ClassLoader.getPlatformClassLoader())) { - Class runtime = Class.forName("com.bencodez.votingplugin.neoforge.NeoForgeRuntime", true, loader); - try (AutoCloseable instance = (AutoCloseable) runtime.getMethod("start", Path.class).invoke(null, directory)) { - assertTrue(Files.isRegularFile(directory.resolve("VotingPlugin.db"))); + Path unusableTemp = directory.resolve("not-a-directory"); + Files.writeString(unusableTemp, "synthetic unusable temp directory"); + String oldJavaTemp = System.getProperty("java.io.tmpdir"); + String oldSqliteTemp = System.getProperty("org.sqlite.tmpdir"); + try { + System.setProperty("java.io.tmpdir", unusableTemp.toString()); + System.setProperty("org.sqlite.tmpdir", unusableTemp.toString()); + try (URLClassLoader loader = new URLClassLoader(new URL[] { jar, platformSlf4j }, + ClassLoader.getPlatformClassLoader())) { + Class runtime = Class.forName("com.bencodez.votingplugin.neoforge.NeoForgeRuntime", true, loader); + try (AutoCloseable instance = (AutoCloseable) runtime.getMethod("start", Path.class).invoke(null, directory)) { + assertTrue(Files.isRegularFile(directory.resolve("VotingPlugin.db"))); + assertTrue(Files.isDirectory(directory.resolve("libraries/sqlite-native"))); + } + } + } finally { + if (oldJavaTemp == null) System.clearProperty("java.io.tmpdir"); + else System.setProperty("java.io.tmpdir", oldJavaTemp); + if (oldSqliteTemp == null) System.clearProperty("org.sqlite.tmpdir"); + else System.setProperty("org.sqlite.tmpdir", oldSqliteTemp); + } + } + + @Test + void packagedSqliteHonorsACustomNameWithoutANativePath(@TempDir Path directory) throws Exception { + URL jar = packagedJar().toUri().toURL(); + URL logging = org.slf4j.Logger.class.getProtectionDomain().getCodeSource().getLocation(); + String nativeName = System.mapLibraryName("sqlitejdbc-custom"); + String resource = "org/sqlite/native/" + org.sqlite.util.OSInfo.getNativeLibFolderPathForCurrentOS() + + "/" + System.mapLibraryName("sqlitejdbc"); + try (JarFile artifact = new JarFile(packagedJar().toFile()); + var input = artifact.getInputStream(artifact.getEntry(resource))) { + Files.copy(input, directory.resolve(nativeName)); + } + java.util.Map previous = new java.util.HashMap<>(); + for (String key : java.util.List.of("org.sqlite.lib.path", "org.sqlite.lib.name", "java.library.path")) { + previous.put(key, System.getProperty(key)); + } + try { + System.clearProperty("org.sqlite.lib.path"); + System.setProperty("org.sqlite.lib.name", nativeName); + System.setProperty("java.library.path", directory.toString()); + Path libraries = directory.resolve("plugin-libraries"); + try (URLClassLoader loader = new URLClassLoader(new URL[] { jar, logging }, + ClassLoader.getPlatformClassLoader())) { + Class.forName("com.bencodez.votingplugin.util.SqliteNativeLibrary", true, loader) + .getMethod("ensureAvailable", Path.class).invoke(null, libraries); + java.sql.Driver jdbc = (java.sql.Driver) Class.forName("org.sqlite.JDBC", true, loader) + .getConstructor().newInstance(); + try (var connection = jdbc.connect("jdbc:sqlite::memory:", new java.util.Properties()); + var statement = connection.createStatement(); + var rows = statement.executeQuery("SELECT 42")) { + assertTrue(rows.next()); + assertEquals(42, rows.getInt(1)); + } } + assertFalse(Files.exists(libraries), "custom name must select the existing library"); + assertEquals(nativeName, System.getProperty("org.sqlite.lib.name")); + assertNull(System.getProperty("org.sqlite.lib.path")); + } finally { + previous.forEach((key, value) -> { + if (value == null) System.clearProperty(key); + else System.setProperty(key, value); + }); } } diff --git a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/util/SqliteNativeLibraryTest.java b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/util/SqliteNativeLibraryTest.java index 9b9a9454f0..c374cc71ef 100644 --- a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/util/SqliteNativeLibraryTest.java +++ b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/util/SqliteNativeLibraryTest.java @@ -5,14 +5,21 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; + +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.IOException; +import java.io.ByteArrayInputStream; +import java.io.InputStream; import java.net.URL; import java.net.URLClassLoader; import java.nio.file.Files; import java.nio.file.Path; import java.util.jar.JarFile; +import java.util.stream.Stream; +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.TestFactory; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -25,7 +32,156 @@ void usesEmbeddedCommonNativeWithoutFetching() throws Exception { SqliteNativeLibrary.class.getClassLoader(), (source, target) -> { throw new AssertionError("embedded native must not download the driver"); }); - assertNull(prepared); + assertTrue(Files.isRegularFile(prepared)); + } + + @TestFactory + Stream everySupportedNativeWorksOfflineWithOnlyThePreviousDriverCached() throws Exception { + Path driver = Path.of(org.sqlite.JDBC.class.getProtectionDomain().getCodeSource().getLocation().toURI()); + Files.writeString(directory.resolve("sqlite-jdbc-3.53.4.0.jar"), "previous installation cache"); + try (JarFile jar = new JarFile(driver.toFile())) { + return jar.stream().filter(entry -> !entry.isDirectory() + && entry.getName().startsWith("org/sqlite/native/")).map(entry -> { + String resource = entry.getName(); + String folder = resource.substring("org/sqlite/native/".length(), resource.lastIndexOf('/')); + return DynamicTest.dynamicTest(folder, () -> { + Path prepared = SqliteNativeLibraryAccess.prepareNative(directory, folder, + SqliteNativeLibrary.class.getClassLoader(), (source, target) -> { + throw new AssertionError("offline upgrade must not fetch a new driver"); + }); + assertTrue(prepared.startsWith(directory.resolve("sqlite-native"))); + try (InputStream expected = SqliteNativeLibrary.class.getClassLoader().getResourceAsStream(resource)) { + assertArrayEquals(expected.readAllBytes(), Files.readAllBytes(prepared)); + } + assertTrue(Files.notExists(directory.resolve(SqliteNativeLibraryAccess.DRIVER_FILE))); + assertTrue(Files.exists(directory.resolve("sqlite-jdbc-3.53.4.0.jar"))); + }); + }).toList().stream(); + } + } + + @Test + void embeddedNativePreparationDoesNotDependOnTheJvmTempDirectory() throws Exception { + Path unusableTemp = directory.resolve("not-a-directory"); + Files.writeString(unusableTemp, "synthetic unusable temp directory"); + String oldJavaTemp = System.getProperty("java.io.tmpdir"); + String oldSqliteTemp = System.getProperty("org.sqlite.tmpdir"); + try { + System.setProperty("java.io.tmpdir", unusableTemp.toString()); + System.setProperty("org.sqlite.tmpdir", unusableTemp.toString()); + Path prepared = SqliteNativeLibraryAccess.prepareNative(directory, "Mac/x86_64", + SqliteNativeLibrary.class.getClassLoader(), (source, target) -> { + throw new AssertionError("offline native must not download"); + }); + assertTrue(prepared.startsWith(directory.resolve("sqlite-native"))); + assertTrue(Files.isRegularFile(prepared)); + } finally { + restoreProperty("java.io.tmpdir", oldJavaTemp); + restoreProperty("org.sqlite.tmpdir", oldSqliteTemp); + } + } + + private static void restoreProperty(String name, String value) { + if (value == null) System.clearProperty(name); + else System.setProperty(name, value); + } + + @TestFactory + Stream preservesExplicitNativeSelectionAcrossOverrideModes() { + return Stream.of("path-only", "name-only", "both", "standard-name").map(mode -> + DynamicTest.dynamicTest(mode, () -> { + Path caseDirectory = Files.createDirectory(directory.resolve(mode)); + Path customDirectory = Files.createDirectory(caseDirectory.resolve("custom-native")); + String standardName = System.mapLibraryName("sqlitejdbc"); + String nativeName = mode.equals("path-only") || mode.equals("standard-name") ? standardName + : System.mapLibraryName("sqlitejdbc-test-" + mode); + String resource = "org/sqlite/native/" + + org.sqlite.util.OSInfo.getNativeLibFolderPathForCurrentOS() + "/" + standardName; + try (InputStream bytes = SqliteNativeLibrary.class.getClassLoader().getResourceAsStream(resource)) { + assertNotNull(bytes); + Files.copy(bytes, customDirectory.resolve(nativeName)); + } + Path badTemp = caseDirectory.resolve("not-a-directory"); + Files.writeString(badTemp, "synthetic unusable temp directory"); + java.util.Map previous = new java.util.HashMap<>(); + for (String key : java.util.List.of("org.sqlite.lib.path", "org.sqlite.lib.name", + "java.library.path", "org.sqlite.tmpdir", "java.io.tmpdir")) { + previous.put(key, System.getProperty(key)); + } + URL code = SqliteNativeLibrary.class.getProtectionDomain().getCodeSource().getLocation(); + URL driver = org.sqlite.JDBC.class.getProtectionDomain().getCodeSource().getLocation(); + URL logging = org.slf4j.Logger.class.getProtectionDomain().getCodeSource().getLocation(); + try { + System.clearProperty("org.sqlite.lib.path"); + System.clearProperty("org.sqlite.lib.name"); + System.setProperty("java.io.tmpdir", badTemp.toString()); + System.setProperty("org.sqlite.tmpdir", badTemp.toString()); + System.setProperty("java.library.path", mode.equals("name-only") + ? customDirectory.toString() : badTemp.toString()); + if (mode.equals("path-only") || mode.equals("both")) { + System.setProperty("org.sqlite.lib.path", customDirectory.toString()); + } + if (!mode.equals("path-only")) System.setProperty("org.sqlite.lib.name", nativeName); + Path libraries = caseDirectory.resolve("plugin-libraries"); + try (URLClassLoader isolated = new URLClassLoader(new URL[] { code, driver, logging }, + ClassLoader.getPlatformClassLoader())) { + Class preparation = Class.forName(SqliteNativeLibrary.class.getName(), true, isolated); + preparation.getMethod("ensureAvailable", Path.class).invoke(null, libraries); + java.sql.Driver jdbc = (java.sql.Driver) Class.forName("org.sqlite.JDBC", true, isolated) + .getConstructor().newInstance(); + try (var connection = jdbc.connect("jdbc:sqlite::memory:", new java.util.Properties()); + var statement = connection.createStatement(); + var rows = statement.executeQuery("SELECT 42")) { + assertTrue(rows.next()); + org.junit.jupiter.api.Assertions.assertEquals(42, rows.getInt(1)); + } + } + if (mode.equals("standard-name")) assertTrue(Files.isDirectory(libraries.resolve("sqlite-native"))); + else assertTrue(Files.notExists(libraries), "explicit selection must not be replaced by embedded native"); + if (!mode.equals("path-only")) { + org.junit.jupiter.api.Assertions.assertEquals(nativeName, System.getProperty("org.sqlite.lib.name")); + } + } finally { + previous.forEach(SqliteNativeLibraryTest::restoreProperty); + } + })); + } + + @TestFactory + Stream rejectsCorruptEmptyAndOversizedEmbeddedNativesWithoutFetching() { + return Stream.of(new byte[0], new byte[] { 1, 2, 3 }, new byte[2 * 1024 * 1024 + 1]) + .map(bytes -> DynamicTest.dynamicTest("invalid native length " + bytes.length, () -> { + ClassLoader corrupt = new ClassLoader(ClassLoader.getPlatformClassLoader()) { + @Override public InputStream getResourceAsStream(String name) { + return new ByteArrayInputStream(bytes); + } + }; + IOException error = assertThrows(IOException.class, + () -> SqliteNativeLibraryAccess.prepareNative(directory, "Mac/x86_64", corrupt, + (source, target) -> { throw new AssertionError("must reject corrupt embedded native"); })); + assertTrue(error.getMessage().contains("SHA-256")); + assertTrue(Files.notExists(directory.resolve("sqlite-native"))); + })); + } + + @Test + void missingNativeAndUnverifiedCacheCannotSucceedOffline() throws Exception { + Files.writeString(directory.resolve(SqliteNativeLibraryAccess.DRIVER_FILE), "unverified cache"); + try (URLClassLoader empty = new URLClassLoader(new URL[0], ClassLoader.getPlatformClassLoader())) { + assertThrows(IOException.class, () -> SqliteNativeLibraryAccess.prepareNative(directory, + "Mac/x86_64", empty, (source, target) -> { throw new IOException("offline"); })); + assertTrue(Files.notExists(directory.resolve("sqlite-native"))); + } + } + + @Test + void rejectsUnverifiedDownloadedDriver() throws Exception { + try (URLClassLoader empty = new URLClassLoader(new URL[0], ClassLoader.getPlatformClassLoader())) { + assertThrows(IOException.class, () -> SqliteNativeLibraryAccess.prepareNative(directory, + "Mac/x86_64", empty, (source, target) -> Files.writeString(target, "unverified driver"))); + assertTrue(Files.notExists(directory.resolve(SqliteNativeLibraryAccess.DRIVER_FILE))); + assertTrue(Files.notExists(directory.resolve("sqlite-native"))); + } } @Test diff --git a/docs/jar-packaging.md b/docs/jar-packaging.md index 0dbc4cacb2..96ea514b48 100644 --- a/docs/jar-packaging.md +++ b/docs/jar-packaging.md @@ -13,27 +13,40 @@ downloadable plugin does not bundle Bouncy Castle. The package phase runs `PackagedArtifactTest` after shading. It opens the actual downloadable JAR, checks plugin resources and required relocated classes, and -rejects duplicate Rhino, raw Hikari/Folia, external crypto providers, uncommon -SQLite native targets, and Checker Framework annotations. It creates both +rejects duplicate Rhino, raw Hikari/Folia, external crypto providers and Checker +Framework annotations. It verifies every SQLite native target against the pinned +official driver artifact. It creates both server and client TLS identities from the packaged JDK-only implementation. -SQLite keeps the Linux x86_64 native in the plugin for offline startup on the -common server platform. When SQLite is selected on another supported target, -VotingPlugin downloads the pinned `sqlite-jdbc` 3.53.4.0 artifact, verifies its -SHA-256 digest, extracts only that target's native into the plugin data folder, -loads it, and restores the JVM-wide Xerial loader properties. MySQL installations -and Linux x86_64 SQLite installations do not make this request. - -For an offline Windows, macOS, ARM, musl, or FreeBSD installation, pre-provision -the official `sqlite-jdbc-3.53.4.0.jar` as -`/libraries/sqlite-jdbc-3.53.4.0.jar`. Its SHA-256 -must be `bcb1f51e36f940867e83342f9efbf5968ac44a6bef4d397bb4af7b17b45cd2fb`; -VotingPlugin rejects any other content and then extracts only the current -platform's native without network access. Operators that already provision a -native may instead set both `org.sqlite.lib.path` and `org.sqlite.lib.name` as -JVM properties. Extracted natives use a unique load directory so a replacement -plugin classloader never reuses the prior classloader's JNI path; stale copies -are removed on a best-effort basis. +SQLite retains all 20 native targets from the official `sqlite-jdbc` 3.54.1.0 +artifact in the downloadable plugin. Windows, macOS, Linux (including ARM and +musl), and FreeBSD installations can replace the plugin JAR and start offline; +no new driver download, cache deletion, or manual provisioning is required. +Each embedded native is checked against a pinned SHA-256 digest, extracted to a +unique load directory under the plugin libraries directory, and loaded with the +existing temporary Xerial properties. The JVM-wide properties are restored. +Native loading does not depend on a writable or executable JVM temp directory. +A corrupt or unexpected embedded native fails validation rather than silently +switching to a download. + +The verified-driver fallback remains available if a native resource is absent +from a custom/repackaged installation. It accepts only the official +`sqlite-jdbc-3.54.1.0.jar` (SHA-256 +`f8dce420a6016278497f4f0560f65602b9db858de141b541cf91a4644ebe4639`) +in the VotingPlugin libraries directory, or downloads and verifies that exact +artifact. The normal downloadable plugin never needs this fallback for a +supported target. Explicit `org.sqlite.lib.path` / `org.sqlite.lib.name` JVM +properties retain their existing operator-controlled behavior. Extracted natives +keep unique load directories for replacement classloaders and best-effort +stale-file cleanup. + +Retaining all supported natives adds about 11 MiB of compressed resources compared +to keeping only Linux x86_64. This is necessary for offline drop-in upgrades; +the size target remains advisory, while missing, duplicated, or mismatched natives +fail the package check. Resource verification across all targets does not claim +native execution on foreign operating systems; startup is exercised on the build +host by the isolated packaged NeoForge runtime test with an unusable JVM temp +directory. The package-phase test reports a prominent warning when the downloadable artifact exceeds the 10 MiB size target. Size alone does not fail the build; dependency From b177a57718fca8d65fe397bccad6a7e1f99018db Mon Sep 17 00:00:00 2001 From: BenCodez <17074231+BenCodez@users.noreply.github.com> Date: Sun, 22 Nov 2026 21:39:20 +0000 Subject: [PATCH 2/5] Isolate SQLite native-loading tests in child JVMs Preserve SQL and override assertions while waiting for process exit before checking native-file cleanup. Keep resource preparation tests in process. --- .../tests/packaging/PackagedArtifactTest.java | 65 +----- .../tests/util/NativeSqliteProcess.java | 188 ++++++++++++++++++ .../tests/util/SqliteNativeLibraryTest.java | 57 +----- 3 files changed, 191 insertions(+), 119 deletions(-) create mode 100644 VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/util/NativeSqliteProcess.java diff --git a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/packaging/PackagedArtifactTest.java b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/packaging/PackagedArtifactTest.java index 8bbe5a313d..ed10e1175a 100644 --- a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/packaging/PackagedArtifactTest.java +++ b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/packaging/PackagedArtifactTest.java @@ -152,73 +152,12 @@ private static void warnIfArtifactExceedsSizeTarget(long artifactBytes, PrintStr @Test void packagedNeoForgeRuntimeStartsAndClosesWithoutTestDependencies(@TempDir Path directory) throws Exception { - URL jar = packagedJar().toUri().toURL(); - URL platformSlf4j = org.slf4j.Logger.class.getProtectionDomain().getCodeSource().getLocation(); - Path unusableTemp = directory.resolve("not-a-directory"); - Files.writeString(unusableTemp, "synthetic unusable temp directory"); - String oldJavaTemp = System.getProperty("java.io.tmpdir"); - String oldSqliteTemp = System.getProperty("org.sqlite.tmpdir"); - try { - System.setProperty("java.io.tmpdir", unusableTemp.toString()); - System.setProperty("org.sqlite.tmpdir", unusableTemp.toString()); - try (URLClassLoader loader = new URLClassLoader(new URL[] { jar, platformSlf4j }, - ClassLoader.getPlatformClassLoader())) { - Class runtime = Class.forName("com.bencodez.votingplugin.neoforge.NeoForgeRuntime", true, loader); - try (AutoCloseable instance = (AutoCloseable) runtime.getMethod("start", Path.class).invoke(null, directory)) { - assertTrue(Files.isRegularFile(directory.resolve("VotingPlugin.db"))); - assertTrue(Files.isDirectory(directory.resolve("libraries/sqlite-native"))); - } - } - } finally { - if (oldJavaTemp == null) System.clearProperty("java.io.tmpdir"); - else System.setProperty("java.io.tmpdir", oldJavaTemp); - if (oldSqliteTemp == null) System.clearProperty("org.sqlite.tmpdir"); - else System.setProperty("org.sqlite.tmpdir", oldSqliteTemp); - } + com.bencodez.votingplugin.tests.util.NativeSqliteProcess.run("runtime", directory, packagedJar()); } @Test void packagedSqliteHonorsACustomNameWithoutANativePath(@TempDir Path directory) throws Exception { - URL jar = packagedJar().toUri().toURL(); - URL logging = org.slf4j.Logger.class.getProtectionDomain().getCodeSource().getLocation(); - String nativeName = System.mapLibraryName("sqlitejdbc-custom"); - String resource = "org/sqlite/native/" + org.sqlite.util.OSInfo.getNativeLibFolderPathForCurrentOS() - + "/" + System.mapLibraryName("sqlitejdbc"); - try (JarFile artifact = new JarFile(packagedJar().toFile()); - var input = artifact.getInputStream(artifact.getEntry(resource))) { - Files.copy(input, directory.resolve(nativeName)); - } - java.util.Map previous = new java.util.HashMap<>(); - for (String key : java.util.List.of("org.sqlite.lib.path", "org.sqlite.lib.name", "java.library.path")) { - previous.put(key, System.getProperty(key)); - } - try { - System.clearProperty("org.sqlite.lib.path"); - System.setProperty("org.sqlite.lib.name", nativeName); - System.setProperty("java.library.path", directory.toString()); - Path libraries = directory.resolve("plugin-libraries"); - try (URLClassLoader loader = new URLClassLoader(new URL[] { jar, logging }, - ClassLoader.getPlatformClassLoader())) { - Class.forName("com.bencodez.votingplugin.util.SqliteNativeLibrary", true, loader) - .getMethod("ensureAvailable", Path.class).invoke(null, libraries); - java.sql.Driver jdbc = (java.sql.Driver) Class.forName("org.sqlite.JDBC", true, loader) - .getConstructor().newInstance(); - try (var connection = jdbc.connect("jdbc:sqlite::memory:", new java.util.Properties()); - var statement = connection.createStatement(); - var rows = statement.executeQuery("SELECT 42")) { - assertTrue(rows.next()); - assertEquals(42, rows.getInt(1)); - } - } - assertFalse(Files.exists(libraries), "custom name must select the existing library"); - assertEquals(nativeName, System.getProperty("org.sqlite.lib.name")); - assertNull(System.getProperty("org.sqlite.lib.path")); - } finally { - previous.forEach((key, value) -> { - if (value == null) System.clearProperty(key); - else System.setProperty(key, value); - }); - } + com.bencodez.votingplugin.tests.util.NativeSqliteProcess.run("packaged-name", directory, packagedJar()); } @Test diff --git a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/util/NativeSqliteProcess.java b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/util/NativeSqliteProcess.java new file mode 100644 index 0000000000..c97d77b75c --- /dev/null +++ b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/util/NativeSqliteProcess.java @@ -0,0 +1,188 @@ +package com.bencodez.votingplugin.tests.util; + +import static org.junit.jupiter.api.Assertions.*; + +import com.bencodez.votingplugin.util.SqliteNativeLibrary; +import java.io.InputStream; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.jar.JarFile; +import java.util.concurrent.TimeUnit; + +/** Test-only process boundary: Windows releases loaded SQLite DLLs at JVM exit. */ +public final class NativeSqliteProcess { + private NativeSqliteProcess() { } + + public static void run(String mode, Path directory, Path artifact) throws Exception { + Files.createDirectories(directory); + Path output = directory.resolve("child-output.log"); + String javaExecutable = Path.of(System.getProperty("java.home"), "bin", + System.getProperty("os.name").startsWith("Windows") ? "java.exe" : "java").toString(); + Process child = new ProcessBuilder(javaExecutable, "-cp", + System.getProperty("surefire.test.class.path", System.getProperty("java.class.path")), + NativeSqliteProcess.class.getName(), mode, directory.toString(), + artifact == null ? "" : artifact.toString()).redirectErrorStream(true).redirectOutput(output.toFile()).start(); + try { + assertTrue(child.waitFor(60, TimeUnit.SECONDS), "native-loading child must exit within its bound"); + assertEquals(0, child.exitValue(), () -> { + try { return Files.readString(output); } + catch (java.io.IOException failure) { return "native-loading child failed"; } + }); + } finally { + if (child.isAlive()) { + child.destroyForcibly(); + assertTrue(child.waitFor(10, TimeUnit.SECONDS), "native-loading child must terminate"); + } + } + // Exercise deletion only after process exit; a locked DLL must fail this check. + try (var paths = Files.walk(directory)) { + for (Path path : paths.sorted(java.util.Comparator.reverseOrder()).toList()) Files.delete(path); + } + assertFalse(Files.exists(directory), "all child files, including loaded natives, must be deletable"); + } + + public static void main(String[] args) throws Exception { + String mode = args[0]; + Path directory = Path.of(args[1]); + if (mode.equals("runtime")) packagedRuntime(directory, Path.of(args[2])); + else if (mode.equals("packaged-name")) packagedName(directory, Path.of(args[2])); + else override(directory, mode); + } + + private static void restoreProperty(String name, String value) { + if (value == null) System.clearProperty(name); + else System.setProperty(name, value); + } + + private static void override(Path directory, String mode) throws Exception { + Path caseDirectory = Files.createDirectory(directory.resolve(mode)); + Path customDirectory = Files.createDirectory(caseDirectory.resolve("custom-native")); + String standardName = System.mapLibraryName("sqlitejdbc"); + String nativeName = mode.equals("path-only") || mode.equals("standard-name") ? standardName + : System.mapLibraryName("sqlitejdbc-test-" + mode); + String resource = "org/sqlite/native/" + + org.sqlite.util.OSInfo.getNativeLibFolderPathForCurrentOS() + "/" + standardName; + try (InputStream bytes = SqliteNativeLibrary.class.getClassLoader().getResourceAsStream(resource)) { + assertNotNull(bytes); + Files.copy(bytes, customDirectory.resolve(nativeName)); + } + Path badTemp = caseDirectory.resolve("not-a-directory"); + Files.writeString(badTemp, "synthetic unusable temp directory"); + java.util.Map previous = new java.util.HashMap<>(); + for (String key : java.util.List.of("org.sqlite.lib.path", "org.sqlite.lib.name", + "java.library.path", "org.sqlite.tmpdir", "java.io.tmpdir")) { + previous.put(key, System.getProperty(key)); + } + URL code = SqliteNativeLibrary.class.getProtectionDomain().getCodeSource().getLocation(); + URL driver = org.sqlite.JDBC.class.getProtectionDomain().getCodeSource().getLocation(); + URL logging = org.slf4j.Logger.class.getProtectionDomain().getCodeSource().getLocation(); + try { + System.clearProperty("org.sqlite.lib.path"); + System.clearProperty("org.sqlite.lib.name"); + System.setProperty("java.io.tmpdir", badTemp.toString()); + System.setProperty("org.sqlite.tmpdir", badTemp.toString()); + System.setProperty("java.library.path", mode.equals("name-only") + ? customDirectory.toString() : badTemp.toString()); + if (mode.equals("path-only") || mode.equals("both")) { + System.setProperty("org.sqlite.lib.path", customDirectory.toString()); + } + if (!mode.equals("path-only")) System.setProperty("org.sqlite.lib.name", nativeName); + Path libraries = caseDirectory.resolve("plugin-libraries"); + try (URLClassLoader isolated = new URLClassLoader(new URL[] { code, driver, logging }, + ClassLoader.getPlatformClassLoader())) { + Class preparation = Class.forName(SqliteNativeLibrary.class.getName(), true, isolated); + preparation.getMethod("ensureAvailable", Path.class).invoke(null, libraries); + java.sql.Driver jdbc = (java.sql.Driver) Class.forName("org.sqlite.JDBC", true, isolated) + .getConstructor().newInstance(); + try (var connection = jdbc.connect("jdbc:sqlite::memory:", new java.util.Properties()); + var statement = connection.createStatement(); + var rows = statement.executeQuery("SELECT 42")) { + assertTrue(rows.next()); + org.junit.jupiter.api.Assertions.assertEquals(42, rows.getInt(1)); + } + } + if (mode.equals("standard-name")) assertTrue(Files.isDirectory(libraries.resolve("sqlite-native"))); + else assertTrue(Files.notExists(libraries), "explicit selection must not be replaced by embedded native"); + if (!mode.equals("path-only")) { + org.junit.jupiter.api.Assertions.assertEquals(nativeName, System.getProperty("org.sqlite.lib.name")); + } + } finally { + previous.forEach(NativeSqliteProcess::restoreProperty); + } + } + + private static void packagedRuntime(Path directory, Path artifact) throws Exception { + + URL jar = artifact.toUri().toURL(); + URL platformSlf4j = org.slf4j.Logger.class.getProtectionDomain().getCodeSource().getLocation(); + Path unusableTemp = directory.resolve("not-a-directory"); + Files.writeString(unusableTemp, "synthetic unusable temp directory"); + String oldJavaTemp = System.getProperty("java.io.tmpdir"); + String oldSqliteTemp = System.getProperty("org.sqlite.tmpdir"); + try { + System.setProperty("java.io.tmpdir", unusableTemp.toString()); + System.setProperty("org.sqlite.tmpdir", unusableTemp.toString()); + try (URLClassLoader loader = new URLClassLoader(new URL[] { jar, platformSlf4j }, + ClassLoader.getPlatformClassLoader())) { + Class runtime = Class.forName("com.bencodez.votingplugin.neoforge.NeoForgeRuntime", true, loader); + try (AutoCloseable instance = (AutoCloseable) runtime.getMethod("start", Path.class).invoke(null, directory)) { + assertTrue(Files.isRegularFile(directory.resolve("VotingPlugin.db"))); + assertTrue(Files.isDirectory(directory.resolve("libraries/sqlite-native"))); + } + } + } finally { + if (oldJavaTemp == null) System.clearProperty("java.io.tmpdir"); + else System.setProperty("java.io.tmpdir", oldJavaTemp); + if (oldSqliteTemp == null) System.clearProperty("org.sqlite.tmpdir"); + else System.setProperty("org.sqlite.tmpdir", oldSqliteTemp); + } + + } + + private static void packagedName(Path directory, Path artifact) throws Exception { + + URL jar = artifact.toUri().toURL(); + URL logging = org.slf4j.Logger.class.getProtectionDomain().getCodeSource().getLocation(); + String nativeName = System.mapLibraryName("sqlitejdbc-custom"); + String resource = "org/sqlite/native/" + org.sqlite.util.OSInfo.getNativeLibFolderPathForCurrentOS() + + "/" + System.mapLibraryName("sqlitejdbc"); + try (JarFile packaged = new JarFile(artifact.toFile()); + var input = packaged.getInputStream(packaged.getEntry(resource))) { + Files.copy(input, directory.resolve(nativeName)); + } + java.util.Map previous = new java.util.HashMap<>(); + for (String key : java.util.List.of("org.sqlite.lib.path", "org.sqlite.lib.name", "java.library.path")) { + previous.put(key, System.getProperty(key)); + } + try { + System.clearProperty("org.sqlite.lib.path"); + System.setProperty("org.sqlite.lib.name", nativeName); + System.setProperty("java.library.path", directory.toString()); + Path libraries = directory.resolve("plugin-libraries"); + try (URLClassLoader loader = new URLClassLoader(new URL[] { jar, logging }, + ClassLoader.getPlatformClassLoader())) { + Class.forName("com.bencodez.votingplugin.util.SqliteNativeLibrary", true, loader) + .getMethod("ensureAvailable", Path.class).invoke(null, libraries); + java.sql.Driver jdbc = (java.sql.Driver) Class.forName("org.sqlite.JDBC", true, loader) + .getConstructor().newInstance(); + try (var connection = jdbc.connect("jdbc:sqlite::memory:", new java.util.Properties()); + var statement = connection.createStatement(); + var rows = statement.executeQuery("SELECT 42")) { + assertTrue(rows.next()); + assertEquals(42, rows.getInt(1)); + } + } + assertFalse(Files.exists(libraries), "custom name must select the existing library"); + assertEquals(nativeName, System.getProperty("org.sqlite.lib.name")); + assertNull(System.getProperty("org.sqlite.lib.path")); + } finally { + previous.forEach((key, value) -> { + if (value == null) System.clearProperty(key); + else System.setProperty(key, value); + }); + } + + } +} diff --git a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/util/SqliteNativeLibraryTest.java b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/util/SqliteNativeLibraryTest.java index c374cc71ef..050a07c2e7 100644 --- a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/util/SqliteNativeLibraryTest.java +++ b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/util/SqliteNativeLibraryTest.java @@ -89,62 +89,7 @@ private static void restoreProperty(String name, String value) { @TestFactory Stream preservesExplicitNativeSelectionAcrossOverrideModes() { return Stream.of("path-only", "name-only", "both", "standard-name").map(mode -> - DynamicTest.dynamicTest(mode, () -> { - Path caseDirectory = Files.createDirectory(directory.resolve(mode)); - Path customDirectory = Files.createDirectory(caseDirectory.resolve("custom-native")); - String standardName = System.mapLibraryName("sqlitejdbc"); - String nativeName = mode.equals("path-only") || mode.equals("standard-name") ? standardName - : System.mapLibraryName("sqlitejdbc-test-" + mode); - String resource = "org/sqlite/native/" - + org.sqlite.util.OSInfo.getNativeLibFolderPathForCurrentOS() + "/" + standardName; - try (InputStream bytes = SqliteNativeLibrary.class.getClassLoader().getResourceAsStream(resource)) { - assertNotNull(bytes); - Files.copy(bytes, customDirectory.resolve(nativeName)); - } - Path badTemp = caseDirectory.resolve("not-a-directory"); - Files.writeString(badTemp, "synthetic unusable temp directory"); - java.util.Map previous = new java.util.HashMap<>(); - for (String key : java.util.List.of("org.sqlite.lib.path", "org.sqlite.lib.name", - "java.library.path", "org.sqlite.tmpdir", "java.io.tmpdir")) { - previous.put(key, System.getProperty(key)); - } - URL code = SqliteNativeLibrary.class.getProtectionDomain().getCodeSource().getLocation(); - URL driver = org.sqlite.JDBC.class.getProtectionDomain().getCodeSource().getLocation(); - URL logging = org.slf4j.Logger.class.getProtectionDomain().getCodeSource().getLocation(); - try { - System.clearProperty("org.sqlite.lib.path"); - System.clearProperty("org.sqlite.lib.name"); - System.setProperty("java.io.tmpdir", badTemp.toString()); - System.setProperty("org.sqlite.tmpdir", badTemp.toString()); - System.setProperty("java.library.path", mode.equals("name-only") - ? customDirectory.toString() : badTemp.toString()); - if (mode.equals("path-only") || mode.equals("both")) { - System.setProperty("org.sqlite.lib.path", customDirectory.toString()); - } - if (!mode.equals("path-only")) System.setProperty("org.sqlite.lib.name", nativeName); - Path libraries = caseDirectory.resolve("plugin-libraries"); - try (URLClassLoader isolated = new URLClassLoader(new URL[] { code, driver, logging }, - ClassLoader.getPlatformClassLoader())) { - Class preparation = Class.forName(SqliteNativeLibrary.class.getName(), true, isolated); - preparation.getMethod("ensureAvailable", Path.class).invoke(null, libraries); - java.sql.Driver jdbc = (java.sql.Driver) Class.forName("org.sqlite.JDBC", true, isolated) - .getConstructor().newInstance(); - try (var connection = jdbc.connect("jdbc:sqlite::memory:", new java.util.Properties()); - var statement = connection.createStatement(); - var rows = statement.executeQuery("SELECT 42")) { - assertTrue(rows.next()); - org.junit.jupiter.api.Assertions.assertEquals(42, rows.getInt(1)); - } - } - if (mode.equals("standard-name")) assertTrue(Files.isDirectory(libraries.resolve("sqlite-native"))); - else assertTrue(Files.notExists(libraries), "explicit selection must not be replaced by embedded native"); - if (!mode.equals("path-only")) { - org.junit.jupiter.api.Assertions.assertEquals(nativeName, System.getProperty("org.sqlite.lib.name")); - } - } finally { - previous.forEach(SqliteNativeLibraryTest::restoreProperty); - } - })); + DynamicTest.dynamicTest(mode, () -> NativeSqliteProcess.run(mode, directory.resolve(mode), null))); } @TestFactory From 826db6200dc9e4ff502615bc46843e53e1b4fce9 Mon Sep 17 00:00:00 2001 From: BenCodez <17074231+BenCodez@users.noreply.github.com> Date: Sun, 29 Nov 2026 21:58:05 +0000 Subject: [PATCH 3/5] Keep test SQLite natives alive until the unit JVM exits --- .../tests/neoforge/NeoForgeRuntimeAccess.java | 27 ++++++++++++++++--- .../tests/neoforge/NeoForgeRuntimeTest.java | 11 ++++++++ .../NeoForgeVoteAccountingStoreTest.java | 6 +++-- .../tests/packaging/PackagedArtifactTest.java | 15 +++++++++++ ...aredMysqlPointSnapshotPersistenceTest.java | 1 + 5 files changed, 55 insertions(+), 5 deletions(-) diff --git a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeRuntimeAccess.java b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeRuntimeAccess.java index b8cbd438be..4c07661494 100644 --- a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeRuntimeAccess.java +++ b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeRuntimeAccess.java @@ -5,6 +5,7 @@ import com.bencodez.votingplugin.neoforge.NeoForgeRuntime.*; import java.io.IOException; import java.nio.file.Path; +import java.nio.file.Files; import java.time.Clock; /** Typed test-only access to package-private NeoForgeRuntime seams. */ @@ -12,8 +13,28 @@ public final class NeoForgeRuntimeAccess { public static final String USER_TABLE_NAME = (String) ReflectiveAccess.field("com.bencodez.votingplugin.neoforge.NeoForgeRuntime", "USER_TABLE_NAME"); private NeoForgeRuntimeAccess() { } - public static NeoForgeRuntime start(Path directory) throws IOException { return (NeoForgeRuntime) ReflectiveAccess.call("com.bencodez.votingplugin.neoforge.NeoForgeRuntime", null, "start", new String[]{"Path"}, new Object[]{directory}); } - public static NeoForgeRuntime start(Path directory, Clock clock) throws IOException { return (NeoForgeRuntime) ReflectiveAccess.call("com.bencodez.votingplugin.neoforge.NeoForgeRuntime", null, "start", new String[]{"Path", "Clock"}, new Object[]{directory, clock}); } - public static NeoForgeRuntime start(Path directory, Object server) throws IOException { return (NeoForgeRuntime) ReflectiveAccess.call("com.bencodez.votingplugin.neoforge.NeoForgeRuntime", null, "start", new String[]{"Path", "Object"}, new Object[]{directory, server}); } + private static boolean nativePrepared; + + /** Build-owned storage survives per-test TempDir cleanup while this fork owns the native. */ + public static Path nativeDirectory() throws IOException { + try { + return Path.of(NeoForgeRuntimeAccess.class.getProtectionDomain().getCodeSource().getLocation().toURI()) + .getParent().resolve("unit-test-native-libraries"); + } catch (java.net.URISyntaxException failure) { + throw new IOException("Unable to resolve test native directory", failure); + } + } + + public static synchronized void prepareNativeForTestFork() throws IOException { + if (!nativePrepared) { + com.bencodez.votingplugin.util.SqliteNativeLibrary.ensureAvailable(nativeDirectory()); + Files.writeString(nativeDirectory().resolve("owner.pid"), Long.toString(ProcessHandle.current().pid())); + nativePrepared = true; + } + } + + public static NeoForgeRuntime start(Path directory) throws IOException { prepareNativeForTestFork(); return (NeoForgeRuntime) ReflectiveAccess.call("com.bencodez.votingplugin.neoforge.NeoForgeRuntime", null, "start", new String[]{"Path"}, new Object[]{directory}); } + public static NeoForgeRuntime start(Path directory, Clock clock) throws IOException { prepareNativeForTestFork(); return (NeoForgeRuntime) ReflectiveAccess.call("com.bencodez.votingplugin.neoforge.NeoForgeRuntime", null, "start", new String[]{"Path", "Clock"}, new Object[]{directory, clock}); } + public static NeoForgeRuntime start(Path directory, Object server) throws IOException { prepareNativeForTestFork(); return (NeoForgeRuntime) ReflectiveAccess.call("com.bencodez.votingplugin.neoforge.NeoForgeRuntime", null, "start", new String[]{"Path", "Object"}, new Object[]{directory, server}); } public static RuntimeException closeAll(Runnable... steps) { return (RuntimeException) ReflectiveAccess.call("com.bencodez.votingplugin.neoforge.NeoForgeRuntime", null, "closeAll", new String[]{"Runnable[]"}, new Object[]{steps}); } } diff --git a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeRuntimeTest.java b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeRuntimeTest.java index 37b816bdb0..e034783dd5 100644 --- a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeRuntimeTest.java +++ b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeRuntimeTest.java @@ -49,6 +49,17 @@ void startsWithExistingConfigurationAndStorageThenClosesCleanly() throws IOExcep } } + @Test + void unitForkNativeIsOutsidePerTestStorage() throws IOException { + try (NeoForgeRuntime runtime = NeoForgeRuntimeAccess.start(directory)) { + Path natives = NeoForgeRuntimeAccess.nativeDirectory(); + assertFalse(natives.startsWith(directory)); + assertTrue(Files.isDirectory(natives.resolve("sqlite-native"))); + assertEquals(Long.toString(ProcessHandle.current().pid()), Files.readString(natives.resolve("owner.pid"))); + assertTrue(runtime.storage().isOpen()); + } + } + @Test void rejectsUnsupportedStorageWithoutStartingSqlite() throws IOException { Files.writeString(directory.resolve("Config.yml"), "DataStorage: MYSQL\n"); diff --git a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeVoteAccountingStoreTest.java b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeVoteAccountingStoreTest.java index 378177b5b0..6a1a5ea25e 100644 --- a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeVoteAccountingStoreTest.java +++ b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeVoteAccountingStoreTest.java @@ -24,7 +24,6 @@ import com.bencodez.votingplugin.core.vote.SharedVoteIdentity; import com.bencodez.votingplugin.core.vote.SharedVoteInput; import com.bencodez.votingplugin.core.vote.SharedVotePolicy; -import com.bencodez.votingplugin.util.SqliteNativeLibrary; class NeoForgeVoteAccountingStoreTest { @TempDir Path directory; @@ -32,6 +31,9 @@ class NeoForgeVoteAccountingStoreTest { @Test void migratesBootstrapTableAndPersistsSharedAccountingAcrossRestart() throws Exception { createBootstrapOnlyTable(); + assertFalse(Files.exists(directory.resolve("libraries")), + "bootstrap preparation must not load a native inside the per-test directory"); + assertTrue(Files.isDirectory(NeoForgeRuntimeAccess.nativeDirectory().resolve("sqlite-native"))); writeConfiguration(3, 5); UUID playerId = UUID.randomUUID(); @@ -157,7 +159,7 @@ void updatingAnotherSitePrunesRemovedLastVoteAcrossRestart() throws IOException private void createBootstrapOnlyTable() throws Exception { Files.createDirectories(directory); - SqliteNativeLibrary.ensureAvailable(directory.resolve("libraries")); + NeoForgeRuntimeAccess.prepareNativeForTestFork(); Class.forName("org.sqlite.JDBC"); try (var connection = DriverManager.getConnection("jdbc:sqlite:" + directory.resolve("VotingPlugin.db")); var statement = connection.createStatement()) { diff --git a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/packaging/PackagedArtifactTest.java b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/packaging/PackagedArtifactTest.java index ed10e1175a..08265dfee6 100644 --- a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/packaging/PackagedArtifactTest.java +++ b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/packaging/PackagedArtifactTest.java @@ -129,6 +129,21 @@ void everyOfficialSqliteNativeIsPackagedOnceAndVerifiedOffline(@TempDir Path dir } } + @Test + void unitForkNativeFilesCanBeDeletedAfterThatJvmExits() throws Exception { + // This execution runs after the separate unit-test Surefire fork has exited. + Path natives = com.bencodez.votingplugin.tests.neoforge.NeoForgeRuntimeAccess.nativeDirectory(); + if (Files.exists(natives)) { + long ownerPid = Long.parseLong(Files.readString(natives.resolve("owner.pid"))); + assertFalse(ProcessHandle.of(ownerPid).map(ProcessHandle::isAlive).orElse(false), + "the unit-test JVM must exit before native cleanup"); + try (var paths = Files.walk(natives)) { + for (Path path : paths.sorted(java.util.Comparator.reverseOrder()).toList()) Files.delete(path); + } + } + assertFalse(Files.exists(natives), "unit-fork native files must be unlocked after process exit"); + } + @Test void artifactSizeTargetWarnsWithoutRejectingOversizedArtifacts() { ByteArrayOutputStream output = new ByteArrayOutputStream(); diff --git a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/user/SharedMysqlPointSnapshotPersistenceTest.java b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/user/SharedMysqlPointSnapshotPersistenceTest.java index f90db22ef2..d6963d454c 100644 --- a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/user/SharedMysqlPointSnapshotPersistenceTest.java +++ b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/user/SharedMysqlPointSnapshotPersistenceTest.java @@ -34,6 +34,7 @@ class SharedMysqlPointSnapshotPersistenceTest { @Test void committedJdbcBalanceIsPublishedToWorkerAndMainReadsAndSurvivesReconnect() throws Exception { + com.bencodez.votingplugin.tests.neoforge.NeoForgeRuntimeAccess.prepareNativeForTestFork(); Path file = Files.createTempFile("vp-points-snapshot-", ".db"); String url = "jdbc:sqlite:" + file; UUID uuid = UUID.randomUUID(); From 87de8b54885a4c85e2d02f256ba187e531aeb8ad Mon Sep 17 00:00:00 2001 From: BenCodez <17074231+BenCodez@users.noreply.github.com> Date: Sun, 29 Nov 2026 22:47:13 +0000 Subject: [PATCH 4/5] Create test native ownership directory for configured overrides --- .../tests/neoforge/NeoForgeRuntimeAccess.java | 1 + .../tests/util/NativeSqliteProcess.java | 32 +++++++++++++++++-- .../tests/util/SqliteNativeLibraryTest.java | 3 +- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeRuntimeAccess.java b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeRuntimeAccess.java index 4c07661494..8070bf3180 100644 --- a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeRuntimeAccess.java +++ b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeRuntimeAccess.java @@ -28,6 +28,7 @@ public static Path nativeDirectory() throws IOException { public static synchronized void prepareNativeForTestFork() throws IOException { if (!nativePrepared) { com.bencodez.votingplugin.util.SqliteNativeLibrary.ensureAvailable(nativeDirectory()); + Files.createDirectories(nativeDirectory()); Files.writeString(nativeDirectory().resolve("owner.pid"), Long.toString(ProcessHandle.current().pid())); nativePrepared = true; } diff --git a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/util/NativeSqliteProcess.java b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/util/NativeSqliteProcess.java index c97d77b75c..125c5e9d7a 100644 --- a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/util/NativeSqliteProcess.java +++ b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/util/NativeSqliteProcess.java @@ -57,6 +57,8 @@ private static void restoreProperty(String name, String value) { } private static void override(Path directory, String mode) throws Exception { + boolean testHelper = mode.startsWith("helper-"); + if (testHelper) mode = mode.substring("helper-".length()); Path caseDirectory = Files.createDirectory(directory.resolve(mode)); Path customDirectory = Files.createDirectory(caseDirectory.resolve("custom-native")); String standardName = System.mapLibraryName("sqlitejdbc"); @@ -90,10 +92,36 @@ private static void override(Path directory, String mode) throws Exception { } if (!mode.equals("path-only")) System.setProperty("org.sqlite.lib.name", nativeName); Path libraries = caseDirectory.resolve("plugin-libraries"); - try (URLClassLoader isolated = new URLClassLoader(new URL[] { code, driver, logging }, + Path helperClasses = caseDirectory.resolve("test-classes"); + if (testHelper) { + String helperResource = "com/bencodez/votingplugin/tests/neoforge/NeoForgeRuntimeAccess.class"; + Path helperClass = helperClasses.resolve(helperResource); + Files.createDirectories(helperClass.getParent()); + try (InputStream bytes = NativeSqliteProcess.class.getClassLoader().getResourceAsStream(helperResource)) { + assertNotNull(bytes); + Files.copy(bytes, helperClass); + } + } + java.util.List classpath = new java.util.ArrayList<>(); + classpath.add(helperClasses.toUri().toURL()); + classpath.addAll(java.util.List.of(code, driver, logging)); + if (testHelper) { + for (String entry : System.getProperty("surefire.test.class.path", System.getProperty("java.class.path")) + .split(java.io.File.pathSeparator)) classpath.add(Path.of(entry).toUri().toURL()); + } + try (URLClassLoader isolated = new URLClassLoader(classpath.toArray(URL[]::new), ClassLoader.getPlatformClassLoader())) { Class preparation = Class.forName(SqliteNativeLibrary.class.getName(), true, isolated); - preparation.getMethod("ensureAvailable", Path.class).invoke(null, libraries); + if (testHelper) { + Path helperNatives = caseDirectory.resolve("unit-test-native-libraries"); + assertFalse(Files.exists(helperNatives)); + Class helper = Class.forName("com.bencodez.votingplugin.tests.neoforge.NeoForgeRuntimeAccess", true, isolated); + helper.getMethod("prepareNativeForTestFork").invoke(null); + helper.getMethod("prepareNativeForTestFork").invoke(null); + assertEquals(Long.toString(ProcessHandle.current().pid()), Files.readString(helperNatives.resolve("owner.pid"))); + assertFalse(Files.exists(helperNatives.resolve("sqlite-native")), + "configured native overrides must not trigger extraction"); + } else preparation.getMethod("ensureAvailable", Path.class).invoke(null, libraries); java.sql.Driver jdbc = (java.sql.Driver) Class.forName("org.sqlite.JDBC", true, isolated) .getConstructor().newInstance(); try (var connection = jdbc.connect("jdbc:sqlite::memory:", new java.util.Properties()); diff --git a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/util/SqliteNativeLibraryTest.java b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/util/SqliteNativeLibraryTest.java index 050a07c2e7..6dfb3e66a5 100644 --- a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/util/SqliteNativeLibraryTest.java +++ b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/util/SqliteNativeLibraryTest.java @@ -88,7 +88,8 @@ private static void restoreProperty(String name, String value) { @TestFactory Stream preservesExplicitNativeSelectionAcrossOverrideModes() { - return Stream.of("path-only", "name-only", "both", "standard-name").map(mode -> + return Stream.of("path-only", "name-only", "both", "standard-name", + "helper-path-only", "helper-name-only", "helper-both").map(mode -> DynamicTest.dynamicTest(mode, () -> NativeSqliteProcess.run(mode, directory.resolve(mode), null))); } From a8e28fc5da16c52c1c3bb0b45cedc85b10907c9c Mon Sep 17 00:00:00 2001 From: BenCodez <17074231+BenCodez@users.noreply.github.com> Date: Mon, 30 Nov 2026 08:58:36 +0000 Subject: [PATCH 5/5] Update compatible Jedis and aligned Mockito dependencies --- VotingPlugin/pom.xml | 6 +++--- .../votingplugin/tests/neoforge/NeoForgeRuntimeAccess.java | 7 +++++++ .../votingplugin/tests/neoforge/NeoForgeRuntimeTest.java | 5 ++++- .../tests/neoforge/NeoForgeVoteAccountingStoreTest.java | 5 ++++- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/VotingPlugin/pom.xml b/VotingPlugin/pom.xml index 97817de1be..78ade7956a 100644 --- a/VotingPlugin/pom.xml +++ b/VotingPlugin/pom.xml @@ -471,7 +471,7 @@ redis.clients jedis - 8.0.2 + 8.1.0 me.clip @@ -527,7 +527,7 @@ org.mockito mockito-core - 5.24.0 + 5.25.0 test @@ -539,7 +539,7 @@ org.mockito mockito-junit-jupiter - 5.24.0 + 5.25.0 test diff --git a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeRuntimeAccess.java b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeRuntimeAccess.java index 8070bf3180..db4a3cf8c4 100644 --- a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeRuntimeAccess.java +++ b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeRuntimeAccess.java @@ -25,6 +25,13 @@ public static Path nativeDirectory() throws IOException { } } + /** Explicit selections are loaded in place rather than extracted into test-owned storage. */ + public static boolean usesConfiguredNative() { + String name = System.getProperty("org.sqlite.lib.name"); + return System.getProperty("org.sqlite.lib.path") != null + || (name != null && !name.equals(System.mapLibraryName("sqlitejdbc"))); + } + public static synchronized void prepareNativeForTestFork() throws IOException { if (!nativePrepared) { com.bencodez.votingplugin.util.SqliteNativeLibrary.ensureAvailable(nativeDirectory()); diff --git a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeRuntimeTest.java b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeRuntimeTest.java index e034783dd5..8604c3186d 100644 --- a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeRuntimeTest.java +++ b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeRuntimeTest.java @@ -54,7 +54,10 @@ void unitForkNativeIsOutsidePerTestStorage() throws IOException { try (NeoForgeRuntime runtime = NeoForgeRuntimeAccess.start(directory)) { Path natives = NeoForgeRuntimeAccess.nativeDirectory(); assertFalse(natives.startsWith(directory)); - assertTrue(Files.isDirectory(natives.resolve("sqlite-native"))); + assertTrue(Files.isDirectory(natives)); + if (!NeoForgeRuntimeAccess.usesConfiguredNative()) { + assertTrue(Files.isDirectory(natives.resolve("sqlite-native"))); + } assertEquals(Long.toString(ProcessHandle.current().pid()), Files.readString(natives.resolve("owner.pid"))); assertTrue(runtime.storage().isOpen()); } diff --git a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeVoteAccountingStoreTest.java b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeVoteAccountingStoreTest.java index 6a1a5ea25e..8d66cd4031 100644 --- a/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeVoteAccountingStoreTest.java +++ b/VotingPlugin/src/test/java/com/bencodez/votingplugin/tests/neoforge/NeoForgeVoteAccountingStoreTest.java @@ -33,7 +33,10 @@ void migratesBootstrapTableAndPersistsSharedAccountingAcrossRestart() throws Exc createBootstrapOnlyTable(); assertFalse(Files.exists(directory.resolve("libraries")), "bootstrap preparation must not load a native inside the per-test directory"); - assertTrue(Files.isDirectory(NeoForgeRuntimeAccess.nativeDirectory().resolve("sqlite-native"))); + assertTrue(Files.isRegularFile(NeoForgeRuntimeAccess.nativeDirectory().resolve("owner.pid"))); + if (!NeoForgeRuntimeAccess.usesConfiguredNative()) { + assertTrue(Files.isDirectory(NeoForgeRuntimeAccess.nativeDirectory().resolve("sqlite-native"))); + } writeConfiguration(3, 5); UUID playerId = UUID.randomUUID();