From 2cf7c98574b1d7ac32381aa539d295df1e6719c5 Mon Sep 17 00:00:00 2001 From: ByMartrixx Date: Sun, 26 Feb 2023 18:11:10 -0300 Subject: [PATCH] Add test for map-specialized-methods --- .../command/CheckMappingsCommandTest.java | 16 +-- .../cuchaz/enigma/command/CommandTest.java | 31 +++++ .../command/FillClassMappingsCommandTest.java | 25 +--- .../MapSpecializedMethodsCommandTest.java | 107 ++++++++++++++++++ .../resources/mapSpecializedMethods/a.mapping | 8 ++ 5 files changed, 157 insertions(+), 30 deletions(-) create mode 100644 enigma-cli/src/test/java/cuchaz/enigma/command/CommandTest.java create mode 100644 enigma-cli/src/test/java/cuchaz/enigma/command/MapSpecializedMethodsCommandTest.java create mode 100644 enigma-cli/src/test/resources/mapSpecializedMethods/a.mapping diff --git a/enigma-cli/src/test/java/cuchaz/enigma/command/CheckMappingsCommandTest.java b/enigma-cli/src/test/java/cuchaz/enigma/command/CheckMappingsCommandTest.java index ec57a771d..60e751a5e 100644 --- a/enigma-cli/src/test/java/cuchaz/enigma/command/CheckMappingsCommandTest.java +++ b/enigma-cli/src/test/java/cuchaz/enigma/command/CheckMappingsCommandTest.java @@ -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)); } } diff --git a/enigma-cli/src/test/java/cuchaz/enigma/command/CommandTest.java b/enigma-cli/src/test/java/cuchaz/enigma/command/CommandTest.java new file mode 100644 index 000000000..0f0733099 --- /dev/null +++ b/enigma-cli/src/test/java/cuchaz/enigma/command/CommandTest.java @@ -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 mappings, Entry entry) { + if (!mappings.contains(entry)) { + return null; + } + + EntryMapping mapping = mappings.get(entry); + return mapping != null ? mapping.targetName() : null; + } +} diff --git a/enigma-cli/src/test/java/cuchaz/enigma/command/FillClassMappingsCommandTest.java b/enigma-cli/src/test/java/cuchaz/enigma/command/FillClassMappingsCommandTest.java index 4d367a081..4a04805cd 100644 --- a/enigma-cli/src/test/java/cuchaz/enigma/command/FillClassMappingsCommandTest.java +++ b/enigma-cli/src/test/java/cuchaz/enigma/command/FillClassMappingsCommandTest.java @@ -7,12 +7,10 @@ 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; @@ -20,9 +18,9 @@ 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"); @@ -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 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); - } - } } diff --git a/enigma-cli/src/test/java/cuchaz/enigma/command/MapSpecializedMethodsCommandTest.java b/enigma-cli/src/test/java/cuchaz/enigma/command/MapSpecializedMethodsCommandTest.java new file mode 100644 index 000000000..dda5e26df --- /dev/null +++ b/enigma-cli/src/test/java/cuchaz/enigma/command/MapSpecializedMethodsCommandTest.java @@ -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 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)); + } +} diff --git a/enigma-cli/src/test/resources/mapSpecializedMethods/a.mapping b/enigma-cli/src/test/resources/mapSpecializedMethods/a.mapping new file mode 100644 index 000000000..9909df501 --- /dev/null +++ b/enigma-cli/src/test/resources/mapSpecializedMethods/a.mapping @@ -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;