Add ModCheckTest to test runtime mod validations and entry point superclass checks. Include Gradle verification metadata for dependency integrity.

This commit is contained in:
Overlord committed 2026-10-03 09:59:47 +02:00
1 parent 18ff32b5ce
commit 6761f2d9ea
29 files changed
+1306 -72

No files matched your search

@@ -174,7 +174,7 @@ public final class ModCheck
if (descriptor == null) return new Result(null, null, problems);
ModCheck.checkDefaultConfig(arguments.jar(), problems);
ModCheck.checkEntrypoint(arguments.jar(), descriptor, problems);
ModCheck.checkEntrypoint(arguments, descriptor, problems);
List<ModDiscovery.Candidate> candidates = new ArrayList<>();
@@ -283,9 +283,14 @@ public final class ModCheck
}
}
/** The entry point and its superclasses inside the jar must lead to {@code Mod}. */
private static void checkEntrypoint(Path jar, ModDescriptor descriptor, List<String> problems)
/**
* The entry point must be in the jar, and its superclasses must lead to {@code Mod}. As in the
* server's class loader, superclasses come from the jar, its libraries, and then its
* dependencies.
*/
private static void checkEntrypoint(Arguments arguments, ModDescriptor descriptor, List<String> problems)
{
Path jar = arguments.jar();
String entrypoint = descriptor.entrypoint();
ClassNode type = ModCheck.classNode(jar, entrypoint.replace('.', '/'), problems);
@@ -300,6 +305,11 @@ public final class ModCheck
boolean constructor = type.methods.stream().anyMatch(ModCheck::isPublicNoArgConstructor);
if (!constructor || (type.access & Opcodes.ACC_PUBLIC) == 0) problems.add("The entry point '" + entrypoint + "' needs to be public, with a public constructor without parameters.");
List<Path> visible = new ArrayList<>();
visible.add(jar);
visible.addAll(arguments.libraries());
visible.addAll(arguments.dependencies());
ClassNode current = type;
for (int depth = 0; depth < 32; depth++)
@@ -309,7 +319,13 @@ public final class ModCheck
if (ModCheck.MOD_CLASS.equals(superName)) return;
if (superName == null) break;
ClassNode parent = ModCheck.classNode(jar, superName, problems);
ClassNode parent = ModCheck.classNode(visible, superName, problems);
if (parent == null && !ModCheck.isJdkClass(superName)) {
problems.add("The entry point '" + entrypoint + "' extends '" + superName.replace('/', '.') + "', which is not in '" + jar.getFileName() + "', its libraries, or its dependencies.");
return;
}
if (parent == null) break;
current = parent;
@@ -344,6 +360,21 @@ public final class ModCheck
}
}
/** Whether {@code internalName} is a JDK class; none of them leads to {@code Mod}. */
private static boolean isJdkClass(String internalName) { return ClassLoader.getPlatformClassLoader().getResource(internalName + ".class") != null; }
/** The class from the first of {@code jars} that contains it. */
private static @Nullable ClassNode classNode(List<Path> jars, String internalName, List<String> problems)
{
for (Path jar : jars)
{
ClassNode node = ModCheck.classNode(jar, internalName, problems);
if (node != null) return node;
}
return null;
}
private static @Nullable ClassNode classNode(Path jar, String internalName, List<String> problems)
{
byte[] bytes = ModCheck.bytes(jar, internalName + ".class", problems);
@@ -0,0 +1,108 @@
package dev.overlord.zomboidforge.tools;
import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
import static org.objectweb.asm.Opcodes.ALOAD;
import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
import static org.objectweb.asm.Opcodes.RETURN;
import static org.objectweb.asm.Opcodes.V25;
class ModCheckTest
{
private static final String MOD = "dev/overlord/zomboidforge/api/Mod";
private static final String TOML = """
id = "shop"
version = "1.0.0"
entrypoint = "a.Main"
api = "0.3"
""";
@TempDir
Path directory;
@Test
void findsEntryPointSuperclassesInLibraries() throws IOException
{
Path mod = this.jar("shop.jar", ModCheckTest.TOML, Map.of("a/Main", ModCheckTest.type("a/Main", "b/Base")));
Path library = this.jar("base.jar", null, Map.of("b/Base", ModCheckTest.type("b/Base", ModCheckTest.MOD)));
ModCheck.Result result = ModCheck.check(new ModCheck.Arguments(mod, null, List.of(library), List.of(), null));
assertEquals(List.of(), result.problems());
}
@Test
void reportsSuperclassesThatAreNowhere() throws IOException
{
Path mod = this.jar("shop.jar", ModCheckTest.TOML, Map.of("a/Main", ModCheckTest.type("a/Main", "b/Base")));
ModCheck.Result result = ModCheck.check(new ModCheck.Arguments(mod, null, List.of(), List.of(), null));
assertEquals(List.of("The entry point 'a.Main' extends 'b.Base', which is not in 'shop.jar', its libraries, or its dependencies."), result.problems());
}
@Test
void reportsEntryPointsThatDoNotExtendMod() throws IOException
{
Path mod = this.jar("shop.jar", ModCheckTest.TOML, Map.of("a/Main", ModCheckTest.type("a/Main", "java/lang/Thread")));
ModCheck.Result result = ModCheck.check(new ModCheck.Arguments(mod, null, List.of(), List.of(), null));
assertEquals(List.of("The entry point 'a.Main' does not extend 'dev.overlord.zomboidforge.api.Mod'."), result.problems());
}
/** A public class extending {@code superName} with a public constructor without parameters. */
private static byte[] type(String name, String superName)
{
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
writer.visit(V25, ACC_PUBLIC, name, null, superName, null);
MethodVisitor init = writer.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
init.visitVarInsn(ALOAD, 0);
init.visitMethodInsn(INVOKESPECIAL, superName, "<init>", "()V", false);
init.visitInsn(RETURN);
init.visitMaxs(0, 0);
writer.visitEnd();
return writer.toByteArray();
}
/** A jar with {@code mod.toml} (unless {@code null}) and {@code classes} (internal name to bytes). */
private Path jar(String name, @Nullable String toml, Map<String, byte[]> classes) throws IOException
{
Path jar = this.directory.resolve(name);
try (OutputStream file = Files.newOutputStream(jar); JarOutputStream out = new JarOutputStream(file))
{
if (toml != null) {
out.putNextEntry(new JarEntry("META-INF/zomboidforge/mod.toml"));
out.write(toml.getBytes(StandardCharsets.UTF_8));
out.closeEntry();
}
for (Map.Entry<String, byte[]> entry : classes.entrySet()) {
out.putNextEntry(new JarEntry(entry.getKey() + ".class"));
out.write(entry.getValue());
out.closeEntry();
}
}
return jar;
}
}