Skip to content

Commit

Permalink
Add test for map-specialized-methods
Browse files Browse the repository at this point in the history
  • Loading branch information
IotaBread committed Feb 26, 2023
1 parent 277e073 commit 2cf7c98
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.nio.file.Path;

public class CheckMappingsCommandTest {
private static final String PACKAGE_ACCESS = "../enigma/build/test-obf/packageAccess.jar";
public class CheckMappingsCommandTest extends CommandTest {
private static final Path JAR = obfJar("packageAccess");
private static final Path WRONG_MAPPINGS = getResource("/packageAccess/wrongMappings");
private static final Path CORRECT_MAPPINGS = getResource("/packageAccess/correctMappings");

@Test
public void testWrong() {
Assertions.assertThrows(IllegalStateException.class, () ->
new CheckMappingsCommand().run(new File(PACKAGE_ACCESS).getAbsolutePath(), new File("src/test/resources" +
"/packageAccess/wrongMappings").getAbsolutePath())
CheckMappingsCommand.run(JAR, WRONG_MAPPINGS)
);
}

@Test
public void testRight() throws Exception {
new CheckMappingsCommand().run(new File(PACKAGE_ACCESS).getAbsolutePath(), new File("src/test/resources" +
"/packageAccess/correctMappings").getAbsolutePath());
public void testRight() {
Assertions.assertDoesNotThrow(() -> CheckMappingsCommand.run(JAR, CORRECT_MAPPINGS));
}
}
31 changes: 31 additions & 0 deletions enigma-cli/src/test/java/cuchaz/enigma/command/CommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cuchaz.enigma.command;

import cuchaz.enigma.translation.mapping.EntryMapping;
import cuchaz.enigma.translation.mapping.tree.EntryTree;
import cuchaz.enigma.translation.representation.entry.Entry;

import java.net.URISyntaxException;
import java.nio.file.Path;

public abstract class CommandTest {
public static Path obfJar(String name) {
return Path.of("../enigma/build/test-obf/%s.jar".formatted(name)).toAbsolutePath();
}

public static Path getResource(String name) {
try {
return Path.of(CommandTest.class.getResource(name).toURI()).toAbsolutePath();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}

protected static String getName(EntryTree<EntryMapping> mappings, Entry<?> entry) {
if (!mappings.contains(entry)) {
return null;
}

EntryMapping mapping = mappings.get(entry);
return mapping != null ? mapping.targetName() : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@
import cuchaz.enigma.translation.mapping.serde.MappingSaveParameters;
import cuchaz.enigma.translation.mapping.tree.EntryTree;
import cuchaz.enigma.translation.representation.entry.ClassEntry;
import cuchaz.enigma.translation.representation.entry.Entry;
import cuchaz.enigma.translation.representation.entry.FieldEntry;
import cuchaz.enigma.translation.representation.entry.MethodEntry;
import org.junit.jupiter.api.Test;

import java.net.URISyntaxException;
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.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

public class FillClassMappingsCommandTest {
private static final Path JAR = Path.of("../enigma/build/test-obf/innerClasses.jar");
private static final Path MAPPINGS;
public class FillClassMappingsCommandTest extends CommandTest {
private static final Path JAR = obfJar("innerClasses");
private static final Path MAPPINGS = getResource("/fillClassMappings/");

private static final ClassEntry A = new ClassEntry("a");
private static final MethodEntry A_METHOD = MethodEntry.parse("a", "a", "()V");
Expand Down Expand Up @@ -94,21 +92,4 @@ public void test() throws Exception {
assertNull(getName(result, F_LEVEL_3));
assertNull(getName(result, F_LEVEL_3_FIELD));
}

private static String getName(EntryTree<EntryMapping> mappings, Entry<?> entry) {
if (!mappings.contains(entry)) {
return null;
}

EntryMapping mapping = mappings.get(entry);
return mapping != null ? mapping.targetName() : null;
}

static {
try {
MAPPINGS = Path.of(FillClassMappingsCommandTest.class.getResource("/fillClassMappings/").toURI());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package cuchaz.enigma.command;

import cuchaz.enigma.ProgressListener;
import cuchaz.enigma.translation.mapping.EntryMapping;
import cuchaz.enigma.translation.mapping.serde.MappingFileNameFormat;
import cuchaz.enigma.translation.mapping.serde.MappingFormat;
import cuchaz.enigma.translation.mapping.serde.MappingSaveParameters;
import cuchaz.enigma.translation.mapping.tree.EntryTree;
import cuchaz.enigma.translation.representation.entry.ClassEntry;
import cuchaz.enigma.translation.representation.entry.MethodEntry;
import org.junit.jupiter.api.Test;

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.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

public class MapSpecializedMethodsCommandTest extends CommandTest {
private static final Path JAR = obfJar("bridge");
private static final Path MAPPINGS = getResource("/mapSpecializedMethods/");

private static final ClassEntry BASE_CLASS = new ClassEntry("a");
private static final MethodEntry BASE_FOO_1 = MethodEntry.parse("a", "d", "()La;");
private static final MethodEntry BASE_FOO_2 = MethodEntry.parse("a", "a", "(I)La;");
private static final MethodEntry BASE_FOO_3 = MethodEntry.parse("a", "a", "(II)La;");
private static final MethodEntry BASE_BAR_1 = MethodEntry.parse("a", "e", "()La;");
private static final MethodEntry BASE_BAR_2 = MethodEntry.parse("a", "b", "(I)La;");
private static final MethodEntry BASE_BAZ_1 = MethodEntry.parse("a", "c", "(I)La;");
private static final MethodEntry BASE_BAZ_2 = MethodEntry.parse("a", "b", "(II)La;");
private static final ClassEntry OTHER_CLASS = new ClassEntry("b");
private static final MethodEntry OTHER_GET = MethodEntry.parse("b", "a", "()Ljava/lang/Integer;");
private static final MethodEntry OTHER_GET_BRIDGE = MethodEntry.parse("b", "get", "()Ljava/lang/Object;");
private static final MethodEntry OTHER_APPLY = MethodEntry.parse("b", "a", "(Ljava/lang/String;)Ljava/lang/Integer;");
private static final MethodEntry OTHER_APPLY_BRIDGE = MethodEntry.parse("b", "apply", "(Ljava/lang/Object;)Ljava/lang/Object;");
private static final ClassEntry SUB_CLASS = new ClassEntry("c");
private static final MethodEntry SUB_FOO_1 = MethodEntry.parse("c", "f", "()Lc;");
private static final MethodEntry SUB_FOO_1_BRIDGE = MethodEntry.parse("c", "d", "()La;");
private static final MethodEntry SUB_FOO_2 = MethodEntry.parse("c", "d", "(I)Lc;");
private static final MethodEntry SUB_FOO_2_BRIDGE = MethodEntry.parse("c", "a", "(I)La;");
private static final MethodEntry SUB_FOO_3 = MethodEntry.parse("c", "c", "(II)Lc;");
private static final MethodEntry SUB_FOO_3_BRIDGE = MethodEntry.parse("c", "a", "(II)La;");
private static final MethodEntry SUB_BAR_1 = MethodEntry.parse("c", "g", "()Lc;");
private static final MethodEntry SUB_BAR_1_BRIDGE = MethodEntry.parse("c", "e", "()La;");
private static final MethodEntry SUB_BAR_2 = MethodEntry.parse("c", "e", "(I)Lc;");
private static final MethodEntry SUB_BAR_2_BRIDGE = MethodEntry.parse("c", "b", "(I)La;");
private static final MethodEntry SUB_BAZ_1 = MethodEntry.parse("c", "f", "(I)Lc;");
private static final MethodEntry SUB_BAZ_1_BRIDGE = MethodEntry.parse("c", "c", "(I)La;");
private static final MethodEntry SUB_BAZ_2 = MethodEntry.parse("c", "d", "(II)Lc;");
private static final MethodEntry SUB_BAZ_2_BRIDGE = MethodEntry.parse("c", "b", "(II)La;");
private static final ClassEntry INNER_SUB_CLASS = new ClassEntry("c$a");
private static final MethodEntry INNER_SUB_FOO_1_BRIDGE = MethodEntry.parse("c$a", "d", "()La;");
private static final MethodEntry INNER_SUB_FOO_2_BRIDGE = MethodEntry.parse("c$a", "a", "(I)La;");
private static final MethodEntry INNER_SUB_FOO_3_BRIDGE = MethodEntry.parse("c$a", "a", "(II)La;");
private static final MethodEntry INNER_SUB_BAR_1_BRIDGE = MethodEntry.parse("c$a", "e", "()La;");
private static final MethodEntry INNER_SUB_BAR_2_BRIDGE = MethodEntry.parse("c$a", "b", "(I)La;");
private static final MethodEntry INNER_SUB_BAZ_1_BRIDGE = MethodEntry.parse("c$a", "c", "(I)La;");
private static final MethodEntry INNER_SUB_BAZ_2_BRIDGE = MethodEntry.parse("c$a", "b", "(II)La;");

@Test
public void test() throws Exception {
Path resultFile = Files.createTempFile("mapSpecializedMethods", ".mappings");
MapSpecializedMethodsCommand.run(JAR, MappingFormat.ENIGMA_DIRECTORY.name(), MAPPINGS, MappingFormat.ENIGMA_FILE.name(), resultFile);

EntryTree<EntryMapping> result = MappingFormat.ENIGMA_FILE.read(resultFile, ProgressListener.none(), new MappingSaveParameters(MappingFileNameFormat.BY_DEOBF));

assertNotNull(result.findNode(BASE_CLASS));
assertEquals("foo", getName(result, BASE_FOO_1));
assertEquals("foo", getName(result, BASE_FOO_2));
assertEquals("foo", getName(result, BASE_FOO_3));
assertEquals("bar", getName(result, BASE_BAR_1));
assertEquals("bar", getName(result, BASE_BAR_2));
assertEquals("baz", getName(result, BASE_BAZ_1));
assertEquals("baz", getName(result, BASE_BAZ_2));

assertNotNull(result.findNode(OTHER_CLASS));
assertEquals("get", getName(result, OTHER_GET));
assertNull(getName(result, OTHER_GET_BRIDGE));
assertEquals("apply", getName(result, OTHER_APPLY));
assertNull(getName(result, OTHER_APPLY_BRIDGE));

assertNotNull(result.findNode(SUB_CLASS));
assertEquals("foo", getName(result, SUB_FOO_1));
assertNull(getName(result, SUB_FOO_1_BRIDGE));
assertEquals("foo", getName(result, SUB_FOO_2));
assertNull(getName(result, SUB_FOO_2_BRIDGE));
assertEquals("foo", getName(result, SUB_FOO_3));
assertNull(getName(result, SUB_FOO_3_BRIDGE));
assertEquals("bar", getName(result, SUB_BAR_1));
assertNull(getName(result, SUB_BAR_1_BRIDGE));
assertEquals("bar", getName(result, SUB_BAR_2));
assertNull(getName(result, SUB_BAR_2_BRIDGE));
assertEquals("baz", getName(result, SUB_BAZ_1));
assertNull(getName(result, SUB_BAZ_1_BRIDGE));
assertEquals("baz", getName(result, SUB_BAZ_2));
assertNull(getName(result, SUB_BAZ_2_BRIDGE));

assertNull(getName(result, INNER_SUB_FOO_1_BRIDGE));
assertNull(getName(result, INNER_SUB_FOO_2_BRIDGE));
assertNull(getName(result, INNER_SUB_FOO_3_BRIDGE));
assertNull(getName(result, INNER_SUB_BAR_1_BRIDGE));
assertNull(getName(result, INNER_SUB_BAR_2_BRIDGE));
assertNull(getName(result, INNER_SUB_BAZ_1_BRIDGE));
assertNull(getName(result, INNER_SUB_BAZ_2_BRIDGE));
}
}
8 changes: 8 additions & 0 deletions enigma-cli/src/test/resources/mapSpecializedMethods/a.mapping
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CLASS a
METHOD a foo (I)La;
METHOD a foo (II)La;
METHOD b bar (I)La;
METHOD b baz (II)La;
METHOD c baz (I)La;
METHOD d foo ()La;
METHOD e bar ()La;

0 comments on commit 2cf7c98

Please sign in to comment.