From 4022b8b847e8767ace0da3f98ad72ab61a4c242b Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Wed, 13 Apr 2022 02:59:06 +0200 Subject: [PATCH] feat: add missing test for fields Signed-off-by: oSumAtrIX --- .../app/revanced/patcher/PatcherTest.kt | 56 +++++++++++-------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/src/test/kotlin/app/revanced/patcher/PatcherTest.kt b/src/test/kotlin/app/revanced/patcher/PatcherTest.kt index fbf83b0..2285293 100644 --- a/src/test/kotlin/app/revanced/patcher/PatcherTest.kt +++ b/src/test/kotlin/app/revanced/patcher/PatcherTest.kt @@ -6,17 +6,22 @@ import app.revanced.patcher.extensions.or import app.revanced.patcher.patch.Patch import app.revanced.patcher.patch.PatchResult import app.revanced.patcher.patch.PatchResultSuccess +import app.revanced.patcher.proxy.mutableTypes.MutableField.Companion.toMutable import app.revanced.patcher.proxy.mutableTypes.MutableMethod.Companion.toMutable import app.revanced.patcher.signature.MethodSignature +import app.revanced.patcher.smali.asInstruction import app.revanced.patcher.smali.asInstructions import com.google.common.collect.ImmutableList import org.jf.dexlib2.AccessFlags import org.jf.dexlib2.Opcode import org.jf.dexlib2.builder.instruction.BuilderInstruction11x import org.jf.dexlib2.builder.instruction.BuilderInstruction21c +import org.jf.dexlib2.immutable.ImmutableField import org.jf.dexlib2.immutable.ImmutableMethod import org.jf.dexlib2.immutable.ImmutableMethodImplementation +import org.jf.dexlib2.immutable.reference.ImmutableFieldReference import org.jf.dexlib2.immutable.reference.ImmutableStringReference +import org.jf.dexlib2.immutable.value.ImmutableFieldEncodedValue import org.junit.jupiter.api.Test import java.io.File import kotlin.test.assertTrue @@ -107,11 +112,36 @@ internal class PatcherTest { ).toMutable() ) - // Now lets create a new call to our method and print the return value! + // Add a field in the main class + // We will use this field in our method below to call println on + // The field holds the Ljava/io/PrintStream->out; field + mainClass.fields.add( + ImmutableField( + mainClass.type, + "dummyField", + "Ljava/io/PrintStream;", + AccessFlags.PRIVATE or AccessFlags.STATIC, + ImmutableFieldEncodedValue( + ImmutableFieldReference( + "Ljava/lang/System;", + "out", + "Ljava/io/PrintStream;" + ) + ), + null, + null + ).toMutable() + ) + + // store the fields initial value into the first virtual register + implementation.replaceInstruction( + 0, + "sget-object v0, LTestClass;->dummyField:Ljava/io/PrintStream;".asInstruction() + ) + + // Now let's create a new call to our method and print the return value! // You can also use the smali compiler to create instructions. - // For this sake of example I reuse the class field System.out inside the virtual register 0. - // Instead an additional instruction could be added at first to re-set this register. - // "sget-object v0, Ljava/lang/System;->out:Ljava/io/PrintStream;" + // For this sake of example I reuse the TestClass field dummyField inside the virtual register 0. // // Control flow instructions are not supported as of now. val instructions = """ @@ -121,24 +151,6 @@ internal class PatcherTest { """.trimIndent().asInstructions() implementation.addInstructions(startIndex + 2, instructions) - // TODO: check TODO of the MutableEncodedValue class - //mainClass.fields.add( - // ImmutableField( - // mainClass.type, - // "dummyField", - // "Ljava/io/PrintStream", - // AccessFlags.PRIVATE or AccessFlags.STATIC, - // ImmutableFieldEncodedValue( - // ImmutableFieldReference( - // "Ljava/lang/System;", - // "out", - // "Ljava/io/PrintStream;" - // ) - // ), - // null - // ).toMutable() - //) - // Finally, tell the patcher that this patch was a success. // You can also return PatchResultError with a message. // If an exception is thrown inside this function,