Add unit tests for patcher logic, version parsing, and core functionality in ZomboidForge.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
015e9bb2c8
commit
896d7ed8e1
4 files changed
+300
No files matched your search
@@ -0,0 +1,61 @@
|
||||
package dev.overlord.zomboidforge.patcher;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.objectweb.asm.tree.ClassNode;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class ApplicablePatchesTest {
|
||||
private static ClassPatch patch(String name, int sinceMinor) {
|
||||
return new ClassPatch() {
|
||||
@Override
|
||||
public String target() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean appliesTo(GameVersion version) {
|
||||
return version.isAtLeast(42, sinceMinor);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(ClassNode node) {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static PatchSet build42(List<ClassPatch> patches) {
|
||||
return new PatchSet() {
|
||||
@Override
|
||||
public int build() {
|
||||
return 42;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ClassPatch> patches() {
|
||||
return patches;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
void keepsOrderAndSkipsPatchesForOtherVersions() {
|
||||
ClassPatch old = patch("t/Old", 0);
|
||||
ClassPatch fromTwenty = patch("t/New", 20);
|
||||
ClassPatch fromTwentyTwo = patch("t/Newer", 22);
|
||||
PatchSet patchSet = build42(List.of(old, fromTwenty, fromTwentyTwo));
|
||||
|
||||
assertEquals(List.of(old, fromTwenty), JarPatcher.applicablePatches(patchSet, new GameVersion(42, 21, 0)));
|
||||
assertEquals(List.of(old), JarPatcher.applicablePatches(patchSet, new GameVersion(42, 19, 2)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsPatchSetOfAnotherBuild() {
|
||||
PatchSet patchSet = build42(List.of());
|
||||
assertThrows(IllegalStateException.class,
|
||||
() -> JarPatcher.applicablePatches(patchSet, new GameVersion(43, 0, 0)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package dev.overlord.zomboidforge.patcher;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
import static org.objectweb.asm.Opcodes.ACC_FINAL;
|
||||
import static org.objectweb.asm.Opcodes.ACC_PRIVATE;
|
||||
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
|
||||
import static org.objectweb.asm.Opcodes.ACC_STATIC;
|
||||
import static org.objectweb.asm.Opcodes.BIPUSH;
|
||||
import static org.objectweb.asm.Opcodes.DUP;
|
||||
import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
|
||||
import static org.objectweb.asm.Opcodes.NEW;
|
||||
import static org.objectweb.asm.Opcodes.PUTSTATIC;
|
||||
import static org.objectweb.asm.Opcodes.RETURN;
|
||||
import static org.objectweb.asm.Opcodes.V25;
|
||||
|
||||
class GameVersionReaderTest {
|
||||
@Test
|
||||
void readsVersionFromCoreStaticInitializer() {
|
||||
assertEquals(new GameVersion(42, 21, 2), GameVersionReader.read(coreClass(42, 21, 2)));
|
||||
}
|
||||
|
||||
/** Runs when the Gradle build passes the server jar (pz.jar) and targeted version (pz.version). */
|
||||
@Test
|
||||
void readsTargetedVersionFromServerJar() throws Exception {
|
||||
String jar = System.getProperty("pz.jar", "");
|
||||
assumeTrue(!jar.isEmpty() && Files.isRegularFile(Path.of(jar)), "server jar not available");
|
||||
|
||||
GameVersion version = JarPatcher.readGameVersion(Path.of(jar));
|
||||
assertTrue(version.matches(System.getProperty("pz.version")), "server jar is " + version);
|
||||
}
|
||||
|
||||
/** Mirrors Core: {@code gameVersion = new GameVersion(major, minor, "")} plus a buildVersion constant. */
|
||||
private static byte[] coreClass(int major, int minor, int build) {
|
||||
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
|
||||
cw.visit(V25, ACC_PUBLIC, GameVersionReader.CORE_CLASS, null, "java/lang/Object", null);
|
||||
cw.visitField(ACC_PRIVATE | ACC_STATIC | ACC_FINAL, "gameVersion", "Lzombie/core/GameVersion;", null, null);
|
||||
cw.visitField(ACC_PRIVATE | ACC_STATIC | ACC_FINAL, "buildVersion", "I", null, build);
|
||||
|
||||
MethodVisitor clinit = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
|
||||
clinit.visitTypeInsn(NEW, "zombie/core/GameVersion");
|
||||
clinit.visitInsn(DUP);
|
||||
clinit.visitIntInsn(BIPUSH, major);
|
||||
clinit.visitIntInsn(BIPUSH, minor);
|
||||
clinit.visitLdcInsn("");
|
||||
clinit.visitMethodInsn(INVOKESPECIAL, "zombie/core/GameVersion", "<init>", "(IILjava/lang/String;)V", false);
|
||||
clinit.visitFieldInsn(PUTSTATIC, GameVersionReader.CORE_CLASS, "gameVersion", "Lzombie/core/GameVersion;");
|
||||
clinit.visitInsn(RETURN);
|
||||
clinit.visitMaxs(0, 0);
|
||||
cw.visitEnd();
|
||||
return cw.toByteArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package dev.overlord.zomboidforge.patcher;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class GameVersionTest {
|
||||
@Test
|
||||
void parsesTwoAndThreePartVersions() {
|
||||
assertEquals(new GameVersion(42, 21, 0), GameVersion.parse("42.21"));
|
||||
assertEquals(new GameVersion(42, 21, 3), GameVersion.parse("42.21.3"));
|
||||
assertThrows(IllegalArgumentException.class, () -> GameVersion.parse("42"));
|
||||
assertThrows(IllegalArgumentException.class, () -> GameVersion.parse("b42.21"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void twoPartExpectationMatchesEveryBuild() {
|
||||
GameVersion version = new GameVersion(42, 21, 3);
|
||||
assertTrue(version.matches("42.21"));
|
||||
assertTrue(version.matches("42.21.3"));
|
||||
assertFalse(version.matches("42.21.0"));
|
||||
assertFalse(version.matches("42.20"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void comparesNumerically() {
|
||||
GameVersion version = new GameVersion(42, 21, 0);
|
||||
assertTrue(version.isAtLeast(42, 9));
|
||||
assertTrue(version.isAtLeast(42, 21));
|
||||
assertFalse(version.isAtLeast(42, 22));
|
||||
assertTrue(version.isBefore(43, 0));
|
||||
assertFalse(version.isBefore(42, 21));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package dev.overlord.zomboidforge.patcher;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
import org.objectweb.asm.Label;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
import org.objectweb.asm.tree.ClassNode;
|
||||
import org.objectweb.asm.tree.MethodNode;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
|
||||
import static org.objectweb.asm.Opcodes.ACC_STATIC;
|
||||
import static org.objectweb.asm.Opcodes.ALOAD;
|
||||
import static org.objectweb.asm.Opcodes.ARETURN;
|
||||
import static org.objectweb.asm.Opcodes.DUP;
|
||||
import static org.objectweb.asm.Opcodes.GOTO;
|
||||
import static org.objectweb.asm.Opcodes.IFEQ;
|
||||
import static org.objectweb.asm.Opcodes.ILOAD;
|
||||
import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
|
||||
import static org.objectweb.asm.Opcodes.NEW;
|
||||
import static org.objectweb.asm.Opcodes.RETURN;
|
||||
import static org.objectweb.asm.Opcodes.V25;
|
||||
|
||||
class JarPatcherTest {
|
||||
@TempDir
|
||||
Path dir;
|
||||
|
||||
/** Adds {@code static Object pick(boolean)} returning a new A or B; frame computation at the merge point requires Base as common superclass. */
|
||||
private static final ClassPatch ADD_PICK = new ClassPatch() {
|
||||
@Override
|
||||
public String target() {
|
||||
return "t/Target";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(ClassNode node) {
|
||||
MethodNode m = new MethodNode(ACC_PUBLIC | ACC_STATIC, "pick", "(Z)Ljava/lang/Object;", null, null);
|
||||
Label elseBranch = new Label();
|
||||
Label end = new Label();
|
||||
m.visitVarInsn(ILOAD, 0);
|
||||
m.visitJumpInsn(IFEQ, elseBranch);
|
||||
newInstance(m, "t/A");
|
||||
m.visitJumpInsn(GOTO, end);
|
||||
m.visitLabel(elseBranch);
|
||||
newInstance(m, "t/B");
|
||||
m.visitLabel(end);
|
||||
m.visitInsn(ARETURN);
|
||||
node.methods.add(m);
|
||||
}
|
||||
};
|
||||
|
||||
@Test
|
||||
void patchesTargetAndCopiesEverythingElse() throws Exception {
|
||||
Path in = jar(Map.of(
|
||||
"t/Base.class", classBytes("t/Base", "java/lang/Object"),
|
||||
"t/A.class", classBytes("t/A", "t/Base"),
|
||||
"t/B.class", classBytes("t/B", "t/Base"),
|
||||
"t/Target.class", classBytes("t/Target", "java/lang/Object"),
|
||||
"media/readme.txt", "untouched".getBytes()));
|
||||
Path out = dir.resolve("out/patched.jar");
|
||||
|
||||
assertEquals(1, new JarPatcher(List.of(ADD_PICK)).patch(in, out));
|
||||
|
||||
try (JarFile original = new JarFile(in.toFile()); JarFile patched = new JarFile(out.toFile())) {
|
||||
for (String name : List.of("t/Base.class", "t/A.class", "t/B.class", "media/readme.txt")) {
|
||||
assertArrayEquals(read(original, name), read(patched, name), name);
|
||||
}
|
||||
}
|
||||
try (URLClassLoader loader = new URLClassLoader(new URL[]{out.toUri().toURL()}, null)) {
|
||||
Class<?> target = loader.loadClass("t.Target");
|
||||
Object picked = target.getMethod("pick", boolean.class).invoke(null, true);
|
||||
assertEquals("t.A", picked.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void failsWhenTargetIsMissing() throws IOException {
|
||||
Path in = jar(Map.of("t/Other.class", classBytes("t/Other", "java/lang/Object")));
|
||||
IllegalStateException e = assertThrows(IllegalStateException.class,
|
||||
() -> new JarPatcher(List.of(ADD_PICK)).patch(in, dir.resolve("out.jar")));
|
||||
assertTrue(e.getMessage().contains("t/Target.class not found"), e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void failsWhenTargetHasMultiReleaseOverride() throws IOException {
|
||||
byte[] target = classBytes("t/Target", "java/lang/Object");
|
||||
Path in = jar(Map.of("t/Target.class", target, "META-INF/versions/21/t/Target.class", target));
|
||||
IllegalStateException e = assertThrows(IllegalStateException.class,
|
||||
() -> new JarPatcher(List.of(ADD_PICK)).patch(in, dir.resolve("out.jar")));
|
||||
assertTrue(e.getMessage().contains("overrides a patched class"), e.getMessage());
|
||||
}
|
||||
|
||||
private static void newInstance(MethodVisitor m, String type) {
|
||||
m.visitTypeInsn(NEW, type);
|
||||
m.visitInsn(DUP);
|
||||
m.visitMethodInsn(INVOKESPECIAL, type, "<init>", "()V", false);
|
||||
}
|
||||
|
||||
private static byte[] classBytes(String name, String superName) {
|
||||
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
|
||||
cw.visit(V25, ACC_PUBLIC, name, null, superName, null);
|
||||
MethodVisitor init = cw.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);
|
||||
cw.visitEnd();
|
||||
return cw.toByteArray();
|
||||
}
|
||||
|
||||
private Path jar(Map<String, byte[]> entries) throws IOException {
|
||||
Path jar = Files.createTempFile(dir, "in", ".jar");
|
||||
try (ZipOutputStream zip = new ZipOutputStream(Files.newOutputStream(jar))) {
|
||||
for (Map.Entry<String, byte[]> e : entries.entrySet()) {
|
||||
zip.putNextEntry(new ZipEntry(e.getKey()));
|
||||
zip.write(e.getValue());
|
||||
zip.closeEntry();
|
||||
}
|
||||
}
|
||||
return jar;
|
||||
}
|
||||
|
||||
private static byte[] read(JarFile jar, String name) throws IOException {
|
||||
return jar.getInputStream(jar.getEntry(name)).readAllBytes();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user