mirror of
https://github.com/xiaozhou233/JuiceAgent.git
synced 2026-12-05 21:15:56 +08:00
Test: add bytecode API integration tests
- add Java tests for getClassBytes(ByName), redefineClass(ByName), retransformClass(ByName) and defineClass - ignore tests/tmp/ directory for captured bytecode output
This commit is contained in:
1 parent
63dd69abc6
commit
4833b512f5
8 files changed
+182
-1
No files matched your search
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user