diff --git a/tests/.gitignore b/tests/.gitignore index 09b96e8..9029fed 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -32,4 +32,5 @@ bin/ *.log ### JuiceAgent ### -resources/ \ No newline at end of file +resources/ +tmp/ \ No newline at end of file diff --git a/tests/src/DefineClass.java b/tests/src/DefineClass.java new file mode 100644 index 0000000..fc2bab6 --- /dev/null +++ b/tests/src/DefineClass.java @@ -0,0 +1,34 @@ +import cn.xiaozhou233.juiceagent.api.JuiceAgent; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +public class DefineClass { + public static void main(String[] args) throws IOException { + System.load(new File("resources/libagent.dll").getAbsolutePath()); + JuiceAgent.init(""); + + ClassLoader cl = new ClassLoader() {}; + + invokeTest(cl); + + byte[] bytes = Files.readAllBytes(new File("out/production/injection/Injection.class").toPath()); + System.out.println("Read bytes: " + bytes.length); + + JuiceAgent.defineClass(cl, bytes); + + invokeTest(cl); + + } + + public static void invokeTest(ClassLoader cl) { + try { + Class clazz = cl.loadClass("Injection"); + clazz.getMethod("test").invoke(null); + System.out.println("ClassLoader: " + clazz.getClassLoader().toString()); + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/tests/src/GetClassBytes.java b/tests/src/GetClassBytes.java new file mode 100644 index 0000000..ffbcdef --- /dev/null +++ b/tests/src/GetClassBytes.java @@ -0,0 +1,29 @@ +import cn.xiaozhou233.juiceagent.api.JuiceAgent; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +public class GetClassBytes { + public static void main(String[] args) throws IOException { + System.load(new File("resources/libagent.dll").getAbsolutePath()); + JuiceAgent.init(""); + + byte[] bytes = JuiceAgent.getClassBytes(Target.class); + + File output = new File("tmp", Target.class.getName().replace('.', '/') + ".class"); + + File parent = output.getParentFile(); + if (parent != null && !parent.exists()) { + parent.mkdirs(); + } + + try (FileOutputStream fos = new FileOutputStream(output)) { + fos.write(bytes); + } + + System.out.println("Class: " + Target.class.getName()); + System.out.println("Bytes: " + bytes.length); + System.out.println("Saved: " + output.getAbsolutePath()); + } +} \ No newline at end of file diff --git a/tests/src/GetClassBytesByName.java b/tests/src/GetClassBytesByName.java new file mode 100644 index 0000000..2535fc2 --- /dev/null +++ b/tests/src/GetClassBytesByName.java @@ -0,0 +1,37 @@ +import cn.xiaozhou233.juiceagent.api.JuiceAgent; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +public class GetClassBytesByName { + public static void main(String[] args) throws IOException { + System.load(new File("resources/libagent.dll").getAbsolutePath()); + JuiceAgent.init(""); + + String className = "Target"; + byte[] bytes = JuiceAgent.getClassBytesByName(className); + + if (bytes == null || bytes.length == 0) { + System.out.println("Failed to get class bytes: " + className); + return; + } + + // Convert JVM internal class name to a file path. + String fileName = className.replace('.', '/').replace('$', '_') + ".class"; + File output = new File("tmp", fileName); + + File parent = output.getParentFile(); + if (parent != null && !parent.exists()) { + parent.mkdirs(); + } + + try (FileOutputStream out = new FileOutputStream(output)) { + out.write(bytes); + } + + System.out.println("Class: " + className); + System.out.println("Bytes: " + bytes.length); + System.out.println("Saved: " + output.getAbsolutePath()); + } +} \ No newline at end of file diff --git a/tests/src/RedefineClass.java b/tests/src/RedefineClass.java new file mode 100644 index 0000000..71ef349 --- /dev/null +++ b/tests/src/RedefineClass.java @@ -0,0 +1,20 @@ +import cn.xiaozhou233.juiceagent.api.JuiceAgent; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +public class RedefineClass { + public static void main(String[] args) throws IOException { + System.load(new File("resources/libagent.dll").getAbsolutePath()); + JuiceAgent.init(""); + + Target.targetTest(); + + byte[] bytes = Files.readAllBytes(new File("out/production/injection/Target.class").toPath()); + JuiceAgent.redefineClass(Target.class, bytes, bytes.length); + + Target.targetTest(); + + } +} diff --git a/tests/src/RedefineClassByName.java b/tests/src/RedefineClassByName.java new file mode 100644 index 0000000..1fc7007 --- /dev/null +++ b/tests/src/RedefineClassByName.java @@ -0,0 +1,20 @@ +import cn.xiaozhou233.juiceagent.api.JuiceAgent; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +public class RedefineClassByName { + public static void main(String[] args) throws IOException { + System.load(new File("resources/libagent.dll").getAbsolutePath()); + JuiceAgent.init(""); + + Target.targetTest(); + + byte[] bytes = Files.readAllBytes(new File("out/production/injection/Target.class").toPath()); + JuiceAgent.redefineClassByName("Target", bytes, bytes.length); + + Target.targetTest(); + + } +} diff --git a/tests/src/RetransformClass.java b/tests/src/RetransformClass.java new file mode 100644 index 0000000..741b99c --- /dev/null +++ b/tests/src/RetransformClass.java @@ -0,0 +1,20 @@ +import cn.xiaozhou233.juiceagent.api.JuiceAgent; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +public class RetransformClass { + public static void main(String[] args) throws IOException { + System.load(new File("resources/libagent.dll").getAbsolutePath()); + JuiceAgent.init(""); + + Target.targetTest(); + + byte[] bytes = Files.readAllBytes(new File("out/production/injection/Target.class").toPath()); + JuiceAgent.retransformClass(Target.class, bytes, bytes.length); + + Target.targetTest(); + + } +} diff --git a/tests/src/RetransformClassByName.java b/tests/src/RetransformClassByName.java new file mode 100644 index 0000000..c06a791 --- /dev/null +++ b/tests/src/RetransformClassByName.java @@ -0,0 +1,20 @@ +import cn.xiaozhou233.juiceagent.api.JuiceAgent; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; + +public class RetransformClassByName { + public static void main(String[] args) throws IOException { + System.load(new File("resources/libagent.dll").getAbsolutePath()); + JuiceAgent.init(""); + + Target.targetTest(); + + byte[] bytes = Files.readAllBytes(new File("out/production/injection/Target.class").toPath()); + JuiceAgent.retransformClassByName("Target", bytes, bytes.length); + + Target.targetTest(); + + } +}