mirror of
https://github.com/ReaJason/MemShellParty.git
synced 2026-09-23 23:41:52 +08:00
fix: custom listener shell generate failed
This commit is contained in:
+24
-1
@@ -5,10 +5,12 @@ import com.reajason.javaweb.asm.ClassReferenceVisitor;
|
||||
import com.reajason.javaweb.memshell.ShellType;
|
||||
import com.reajason.javaweb.memshell.config.CustomConfig;
|
||||
import com.reajason.javaweb.memshell.config.ShellConfig;
|
||||
import com.reajason.javaweb.memshell.shelltool.command.CommandListener;
|
||||
import com.reajason.javaweb.memshell.shelltool.godzilla.GodzillaValve;
|
||||
import com.reajason.javaweb.utils.CommonUtil;
|
||||
import lombok.SneakyThrows;
|
||||
import net.bytebuddy.ByteBuddy;
|
||||
import net.bytebuddy.description.type.TypeDescription;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.objectweb.asm.ClassReader;
|
||||
|
||||
@@ -23,10 +25,31 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
* @since 2025/3/19
|
||||
*/
|
||||
class CustomShellGeneratorTest {
|
||||
@Test
|
||||
@SneakyThrows
|
||||
void testListener() {
|
||||
byte[] bytes = new ByteBuddy()
|
||||
.redefine(CommandListener.class)
|
||||
.name(CommonUtil.generateShellClassName()).make().getBytes();
|
||||
String className = CommonUtil.generateShellClassName();
|
||||
ShellConfig shellConfig = ShellConfig.builder()
|
||||
.server(Server.Tomcat)
|
||||
.shellType(ShellType.LISTENER)
|
||||
.build();
|
||||
CustomConfig customConfig = CustomConfig.builder()
|
||||
.shellClassName(className)
|
||||
.shellClassBase64(Base64.getEncoder().encodeToString(bytes))
|
||||
.shellTypeDescription(TypeDescription.ForLoadedType.of(CommandListener.class))
|
||||
.build();
|
||||
byte[] bytes1 = new CustomShellGenerator(shellConfig, customConfig).getBytes();
|
||||
|
||||
ClassReader classReader = new ClassReader(bytes1);
|
||||
assertEquals(className, classReader.getClassName().replace("/", "."));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SneakyThrows
|
||||
void test() {
|
||||
void testFilter() {
|
||||
byte[] bytes = new ByteBuddy()
|
||||
.subclass(Object.class)
|
||||
.name(CommonUtil.generateShellClassName()).make().getBytes();
|
||||
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
package com.reajason.javaweb.memshell.generator;
|
||||
|
||||
import com.reajason.javaweb.GenerationException;
|
||||
import com.reajason.javaweb.memshell.server.Tomcat;
|
||||
import lombok.SneakyThrows;
|
||||
import net.bytebuddy.ByteBuddy;
|
||||
import net.bytebuddy.description.type.TypeDescription;
|
||||
import net.bytebuddy.dynamic.DynamicType;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.platform.commons.util.ReflectionUtils;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* @author ReaJason
|
||||
* @since 2025/9/16
|
||||
*/
|
||||
class ListenerGeneratorTest {
|
||||
|
||||
public static class L {
|
||||
public Object getResponseFromRequest(Object request) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static class J {
|
||||
public HttpServletResponse getResponseFromRequest(HttpServletRequest request) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static class FakeRequest {
|
||||
public Object response = "i'm a good boy";
|
||||
}
|
||||
|
||||
@Test
|
||||
void testNoGetResponseFromRequest() {
|
||||
DynamicType.Builder<?> builder = new ByteBuddy().redefine(Object.class);
|
||||
Assertions.assertThrows(GenerationException.class, () -> ListenerGenerator.build(builder, Tomcat.ListenerInterceptor.class, TypeDescription.ForLoadedType.of(Object.class), "hello.world"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetResponseFromRequestSignatureError() {
|
||||
DynamicType.Builder<?> builder = new ByteBuddy().redefine(J.class);
|
||||
Assertions.assertThrows(GenerationException.class, () -> ListenerGenerator.build(builder, Tomcat.ListenerInterceptor.class, TypeDescription.ForLoadedType.of(J.class), "hello.world"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SneakyThrows
|
||||
void test() {
|
||||
String className = "hello.world";
|
||||
DynamicType.Builder<?> build = ListenerGenerator.build(new ByteBuddy().redefine(L.class).name(className), Tomcat.ListenerInterceptor.class, TypeDescription.ForLoadedType.of(L.class), className);
|
||||
Class<?> clazz = build.make().load(getClass().getClassLoader()).getLoaded();
|
||||
Object obj = clazz.newInstance();
|
||||
Method getResponseFromRequest = clazz.getDeclaredMethod("getResponseFromRequest", Object.class);
|
||||
getResponseFromRequest.setAccessible(true);
|
||||
Object response = getResponseFromRequest.invoke(obj, new FakeRequest());
|
||||
assertEquals("i'm a good boy", response);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user