From 036b593356eea9982f62fdd7964c7e5ec6a636e9 Mon Sep 17 00:00:00 2001 From: Gravit Date: Sat, 18 Jan 2020 13:31:30 +0700 Subject: [PATCH] =?UTF-8?q?[TEST]=20=D0=A2=D0=B5=D1=81=D1=82=D1=8B=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20InjectClassAcceptor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../launchserver/asm/InjectClassAcceptor.java | 2 +- .../launchserver/ASMTransformersTest.java | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 LaunchServer/src/test/java/pro/gravit/launchserver/ASMTransformersTest.java diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/asm/InjectClassAcceptor.java b/LaunchServer/src/main/java/pro/gravit/launchserver/asm/InjectClassAcceptor.java index 8146dc82..7cb5621b 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/asm/InjectClassAcceptor.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/asm/InjectClassAcceptor.java @@ -22,7 +22,7 @@ public class InjectClassAcceptor { public static final String INJ_DESC = Type.getDescriptor(LauncherInject.class); public static void checkMap(Map object) { - if (!object.values().stream().allMatch(zPrimitivesList::contains)) + if (!zPrimitivesList.containsAll(object.values())) throw new IllegalArgumentException("Only primitives in values..."); } diff --git a/LaunchServer/src/test/java/pro/gravit/launchserver/ASMTransformersTest.java b/LaunchServer/src/test/java/pro/gravit/launchserver/ASMTransformersTest.java new file mode 100644 index 00000000..c776109d --- /dev/null +++ b/LaunchServer/src/test/java/pro/gravit/launchserver/ASMTransformersTest.java @@ -0,0 +1,66 @@ +package pro.gravit.launchserver; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.objectweb.asm.ClassReader; +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.FieldNode; +import pro.gravit.launcher.LauncherInject; +import pro.gravit.launchserver.asm.InjectClassAcceptor; +import pro.gravit.utils.PublicURLClassLoader; +import pro.gravit.utils.helper.LogHelper; + +import java.io.InputStream; +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; + +public class ASMTransformersTest { + public static class ASMClassLoader extends ClassLoader + { + public ASMClassLoader(ClassLoader parent) { + super(parent); + } + public void rawDefineClass(String name, byte[] bytes, int offset, int length) + { + defineClass(name, bytes, offset, length); + } + } + public static ASMClassLoader classLoader; + public static class TestClass + { + @LauncherInject(value = "testprop") + public static int test = 1; + } + @BeforeAll + public static void prepare() throws Exception { + classLoader = new ASMClassLoader(ASMTransformersTest.class.getClassLoader()); + } + InputStream getClass(Class clazz) + { + String className = clazz.getName(); + String classAsPath = className.replace('.', '/') + ".class"; + return clazz.getClassLoader().getResourceAsStream(classAsPath); + } + @Test + void testASM() throws Exception + { + ClassReader reader = new ClassReader(getClass(TestClass.class)); + ClassNode node = new ClassNode(); + reader.accept(node, ClassReader.SKIP_DEBUG); + node.name = "ASMTestClass"; + Map map = new HashMap<>(); + map.put("testprop", 1234); + InjectClassAcceptor.visit(node, map); + ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); + node.accept(writer); + byte[] bytes = writer.toByteArray(); + classLoader.rawDefineClass("ASMTestClass", bytes, 0, bytes.length); + Class clazz = classLoader.loadClass("ASMTestClass"); + Field field = clazz.getField("test"); + Object result = field.get(null); + Assertions.assertEquals(1234, (int) (Integer) result); + } +}