Skip to content

Commit

Permalink
assertion inside test transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
EpicPlayerA10 committed Sep 11, 2024
1 parent 6af601c commit 500fbe6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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<Supplier<Transformer>> transformers, Source... sources) {
this.registeredTests.add(new RegisteredTest(testName, false, inputType, transformers, sources));
}

protected void registerThrows(String testName, InputType inputType, List<Supplier<Transformer>> transformers, Source... sources) {
this.registeredTests.add(new RegisteredTest(testName, true, inputType, transformers, sources));
this.registeredTests.add(new RegisteredTest(testName, inputType, transformers, sources));
}

@BeforeAll
Expand All @@ -77,7 +71,6 @@ public Stream<DynamicTest> testDeobfuscation() {
*/
public record RegisteredTest(
String testName,
boolean throwable,
InputType inputType,
List<Supplier<Transformer>> transformers,
Source[] sources
Expand Down Expand Up @@ -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<IContextSource> contextSources = new ArrayList<>();
Expand Down Expand Up @@ -193,6 +180,13 @@ private void assertOutput(List<IContextSource> 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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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")
);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}

0 comments on commit 500fbe6

Please sign in to comment.