From 500fbe64a8592223588df59a448d9b08a3852cf0 Mon Sep 17 00:00:00 2001 From: EpicPlayerA10 Date: Wed, 11 Sep 2024 19:06:58 +0200 Subject: [PATCH] assertion inside test transformer --- .../deobfuscator/TestDeobfuscation.java | 2 +- .../base/TestDeobfuscationBase.java | 28 ++++++++----------- .../TestSandboxSecurityTransformer.java | 11 ++++++-- .../zkm/ZelixLongEncryptionTransformer.java | 1 + 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/deobfuscator-impl/src/test/java/uwu/narumi/deobfuscator/TestDeobfuscation.java b/deobfuscator-impl/src/test/java/uwu/narumi/deobfuscator/TestDeobfuscation.java index e1cc231..27a3e83 100644 --- a/deobfuscator-impl/src/test/java/uwu/narumi/deobfuscator/TestDeobfuscation.java +++ b/deobfuscator-impl/src/test/java/uwu/narumi/deobfuscator/TestDeobfuscation.java @@ -26,7 +26,7 @@ protected void registerAll() { register("Inline static fields with modification", InputType.JAVA_CODE, List.of(InlineStaticFieldTransformer::new), Source.of("TestInlineStaticFieldsWithModification")); // Sandbox security. Should throw - registerThrows("Sandbox security", InputType.JAVA_CODE, List.of(TestSandboxSecurityTransformer::new), Source.of("sandbox/TestSandboxSecurity")); + register("Sandbox security", InputType.JAVA_CODE, List.of(TestSandboxSecurityTransformer::new), Source.of("sandbox/TestSandboxSecurity", false)); // Samples register("Some flow obf sample", InputType.CUSTOM_CLASS, List.of(ComposedGeneralFlowTransformer::new), Source.of("FlowObfSample")); diff --git a/deobfuscator-impl/src/test/java/uwu/narumi/deobfuscator/base/TestDeobfuscationBase.java b/deobfuscator-impl/src/test/java/uwu/narumi/deobfuscator/base/TestDeobfuscationBase.java index b7804ce..5d36194 100644 --- a/deobfuscator-impl/src/test/java/uwu/narumi/deobfuscator/base/TestDeobfuscationBase.java +++ b/deobfuscator-impl/src/test/java/uwu/narumi/deobfuscator/base/TestDeobfuscationBase.java @@ -24,8 +24,6 @@ import java.util.function.Supplier; import java.util.stream.Stream; -import static org.junit.jupiter.api.Assertions.*; - @Timeout(60) public abstract class TestDeobfuscationBase { public static final Path TEST_DATA_PATH = Path.of("..", "testData"); @@ -49,11 +47,7 @@ public abstract class TestDeobfuscationBase { * @param sources You can choose one class or multiple classes for testing */ protected void register(String testName, InputType inputType, List> transformers, Source... sources) { - this.registeredTests.add(new RegisteredTest(testName, false, inputType, transformers, sources)); - } - - protected void registerThrows(String testName, InputType inputType, List> transformers, Source... sources) { - this.registeredTests.add(new RegisteredTest(testName, true, inputType, transformers, sources)); + this.registeredTests.add(new RegisteredTest(testName, inputType, transformers, sources)); } @BeforeAll @@ -77,7 +71,6 @@ public Stream testDeobfuscation() { */ public record RegisteredTest( String testName, - boolean throwable, InputType inputType, List> transformers, Source[] sources @@ -135,15 +128,9 @@ private void runTest() { .outputDir(DEOBFUSCATED_CLASSES_PATH.resolve(this.inputType.directory())); // Build and run deobfuscator! - if (this.throwable) { - assertThrows(RuntimeException.class, () -> { - Deobfuscator.from(optionsBuilder.build()).start(); - }); - // If the deobfuscator throws an exception, then there is no output. Return - return; - } else { - Deobfuscator.from(optionsBuilder.build()).start(); - } + Deobfuscator.from(optionsBuilder.build()).start(); + + if (!shouldDecompile()) return; // Init context sources List contextSources = new ArrayList<>(); @@ -193,6 +180,13 @@ private void assertOutput(List contextSources, @Nullable Path in throw new TestAbortedException("No previous decompiled code found, skipping test"); } } + + private boolean shouldDecompile() { + for (Source source : sources) { + if (source.decompile) return true; + } + return false; + } } public enum InputType { diff --git a/deobfuscator-impl/src/test/java/uwu/narumi/deobfuscator/transformer/TestSandboxSecurityTransformer.java b/deobfuscator-impl/src/test/java/uwu/narumi/deobfuscator/transformer/TestSandboxSecurityTransformer.java index 485dae2..50c6d01 100644 --- a/deobfuscator-impl/src/test/java/uwu/narumi/deobfuscator/transformer/TestSandboxSecurityTransformer.java +++ b/deobfuscator-impl/src/test/java/uwu/narumi/deobfuscator/transformer/TestSandboxSecurityTransformer.java @@ -1,19 +1,24 @@ package uwu.narumi.deobfuscator.transformer; +import dev.xdark.ssvm.execution.VMException; import dev.xdark.ssvm.mirror.type.InstanceClass; import uwu.narumi.deobfuscator.api.asm.ClassWrapper; import uwu.narumi.deobfuscator.api.context.Context; import uwu.narumi.deobfuscator.api.execution.SandBox; import uwu.narumi.deobfuscator.api.transformer.Transformer; +import static org.junit.jupiter.api.Assertions.assertThrows; + public class TestSandboxSecurityTransformer extends Transformer { @Override protected void transform(ClassWrapper scope, Context context) throws Exception { SandBox sandBox = context.getSandBox(); InstanceClass clazz = sandBox.getHelper().loadClass("sandbox.TestSandboxSecurity"); - sandBox.getInvocationUtil().invokeInt( - clazz.getMethod("test", "()I") - ); + assertThrows(VMException.class, () -> { + sandBox.getInvocationUtil().invokeInt( + clazz.getMethod("test", "()I") + ); + }); } } diff --git a/deobfuscator-transformers/src/main/java/uwu/narumi/deobfuscator/core/other/impl/zkm/ZelixLongEncryptionTransformer.java b/deobfuscator-transformers/src/main/java/uwu/narumi/deobfuscator/core/other/impl/zkm/ZelixLongEncryptionTransformer.java index 410981d..96f6914 100644 --- a/deobfuscator-transformers/src/main/java/uwu/narumi/deobfuscator/core/other/impl/zkm/ZelixLongEncryptionTransformer.java +++ b/deobfuscator-transformers/src/main/java/uwu/narumi/deobfuscator/core/other/impl/zkm/ZelixLongEncryptionTransformer.java @@ -104,6 +104,7 @@ protected void transform(ClassWrapper scope, Context context) throws Exception { } })); + // Remove decrypter classes sandBox.getUsedCustomClasses().forEach(clazz -> context.getClasses().remove(clazz.getInternalName())); } }