mirror of
https://github.com/ReaJason/MemShellParty.git
synced 2026-09-23 07:21:53 +08:00
refactor: separate generate config
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package com.reajason.javaweb.config;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* @author ReaJason
|
||||
* @since 2024/12/5
|
||||
*/
|
||||
class GodzillaShellConfigTest {
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
GodzillaConfig shellConfig = GodzillaConfig.builder()
|
||||
.pass("pass").build();
|
||||
assertEquals("key", shellConfig.getKey());
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
package com.reajason.javaweb.memsell;
|
||||
|
||||
import com.reajason.javaweb.memsell.tomcat.godzilla.GodzillaFilter;
|
||||
import com.reajason.javaweb.memsell.tomcat.godzilla.GodzillaListener;
|
||||
import com.reajason.javaweb.util.ClassUtils;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* @author ReaJason
|
||||
* @since 2024/11/23
|
||||
*/
|
||||
class GodzillaGeneratorTest {
|
||||
String pass = "pass";
|
||||
String key = "key";
|
||||
String headerName = "User-Agent";
|
||||
String headerValue = "test";
|
||||
|
||||
@Test
|
||||
@Disabled("just for generate")
|
||||
void testGenerate() throws IOException {
|
||||
String className = "org.apache.utils.CommonFilter";
|
||||
byte[] bytes = GodzillaGenerator.generate(GodzillaFilter.class, className, pass, key, headerName, headerValue);
|
||||
IOUtils.write(bytes, Files.newOutputStream(Paths.get("CommonFilter.class")));
|
||||
}
|
||||
}
|
||||
+29
-11
@@ -1,12 +1,17 @@
|
||||
package com.reajason.javaweb.memsell.tomcat.command;
|
||||
|
||||
import com.reajason.javaweb.config.Constants;
|
||||
import com.reajason.javaweb.config.CommandConfig;
|
||||
import com.reajason.javaweb.config.ShellConfig;
|
||||
import com.reajason.javaweb.memsell.CommandGenerator;
|
||||
import com.reajason.javaweb.util.ClassUtils;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.params.provider.Arguments.arguments;
|
||||
|
||||
/**
|
||||
* @author ReaJason
|
||||
@@ -14,14 +19,27 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
*/
|
||||
class CommandFilterTest {
|
||||
|
||||
@Test
|
||||
void testGenerate() {
|
||||
String className = "org.command.CommandFilter";
|
||||
String paramName = "cmd";
|
||||
byte[] bytes = CommandGenerator.generate(CommandFilter.class, className, paramName, false, Constants.DEFAULT_VERSION);
|
||||
static Stream<Arguments> casesProvider() {
|
||||
return Stream.of(
|
||||
arguments(CommandFilter.class, "org.apache.utils.CommandFilter"),
|
||||
arguments(CommandListener.class, "org.apache.utils.CommandListener"),
|
||||
arguments(CommandValve.class, "org.apache.utils.CommandValve")
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("casesProvider")
|
||||
void generate(Class<?> clazz, String className) {
|
||||
ShellConfig generateConfig = new ShellConfig();
|
||||
CommandConfig shellConfig = CommandConfig.builder()
|
||||
.clazz(clazz)
|
||||
.className(className)
|
||||
.paramName("cmd")
|
||||
.build();
|
||||
byte[] bytes = CommandGenerator.generate(generateConfig, shellConfig);
|
||||
Object obj = ClassUtils.newInstance(bytes);
|
||||
assertEquals(className, obj.getClass().getName());
|
||||
assertEquals(paramName, ClassUtils.getFieldValue(obj, "paramName"));
|
||||
System.out.println(Base64.encodeBase64String(bytes));
|
||||
assertEquals(shellConfig.getClassName(), obj.getClass().getName());
|
||||
assertEquals(shellConfig.getParamName(), ClassUtils.getFieldValue(obj, "paramName"));
|
||||
}
|
||||
}
|
||||
-38
@@ -1,38 +0,0 @@
|
||||
package com.reajason.javaweb.memsell.tomcat.godzilla;
|
||||
|
||||
import com.reajason.javaweb.memsell.GodzillaGenerator;
|
||||
import com.reajason.javaweb.util.ClassUtils;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/**
|
||||
* @author ReaJason
|
||||
* @since 2024/11/24
|
||||
*/
|
||||
class GodzillaFilterTest {
|
||||
String pass = "pass";
|
||||
String key = "key";
|
||||
String headerName = "User-Agent";
|
||||
String headerValue = "test";
|
||||
|
||||
@Test
|
||||
void generate() {
|
||||
String className = "org.apache.utils.CommonFilter";
|
||||
byte[] bytes = GodzillaGenerator.generate(GodzillaFilter.class, className, pass, key, headerName, headerValue);
|
||||
Object obj = ClassUtils.newInstance(bytes);
|
||||
assertEquals(className, obj.getClass().getName());
|
||||
assertEquals(pass, ClassUtils.getFieldValue(obj, "pass"));
|
||||
assertEquals("3c6e0b8a9c15224a", ClassUtils.getFieldValue(obj, "key"));
|
||||
assertEquals(headerName, ClassUtils.getFieldValue(obj, "headerName"));
|
||||
assertEquals(headerValue, ClassUtils.getFieldValue(obj, "headerValue"));
|
||||
System.out.println(Base64.encodeBase64String(bytes));
|
||||
}
|
||||
}
|
||||
-32
@@ -1,32 +0,0 @@
|
||||
package com.reajason.javaweb.memsell.tomcat.godzilla;
|
||||
|
||||
import com.reajason.javaweb.memsell.GodzillaGenerator;
|
||||
import com.reajason.javaweb.util.ClassUtils;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* @author ReaJason
|
||||
* @since 2024/11/24
|
||||
*/
|
||||
class GodzillaListenerTest {
|
||||
String pass = "pass";
|
||||
String key = "key";
|
||||
String headerName = "User-Agent";
|
||||
String headerValue = "test";
|
||||
|
||||
@Test
|
||||
void generate() {
|
||||
String className = "org.apache.utils.CommonListener";
|
||||
byte[] bytes = GodzillaGenerator.generate(GodzillaListener.class, className, pass, key, headerName, headerValue);
|
||||
Object obj = ClassUtils.newInstance(bytes);
|
||||
assertEquals(className, obj.getClass().getName());
|
||||
assertEquals(pass, ClassUtils.getFieldValue(obj, "pass"));
|
||||
assertEquals("3c6e0b8a9c15224a", ClassUtils.getFieldValue(obj, "key"));
|
||||
assertEquals(headerName, ClassUtils.getFieldValue(obj, "headerName"));
|
||||
assertEquals(headerValue, ClassUtils.getFieldValue(obj, "headerValue"));
|
||||
System.out.println(Base64.encodeBase64String(bytes));
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package com.reajason.javaweb.memsell.tomcat.godzilla;
|
||||
|
||||
import com.reajason.javaweb.config.ShellConfig;
|
||||
import com.reajason.javaweb.config.GodzillaConfig;
|
||||
import com.reajason.javaweb.memsell.GodzillaGenerator;
|
||||
import com.reajason.javaweb.util.ClassUtils;
|
||||
import com.reajason.javaweb.util.CommonUtil;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.params.provider.Arguments.arguments;
|
||||
|
||||
/**
|
||||
* @author ReaJason
|
||||
* @since 2024/11/24
|
||||
*/
|
||||
class GodzillaTest {
|
||||
ShellConfig config = new ShellConfig();
|
||||
|
||||
GodzillaConfig.GodzillaConfigBuilder<?, ?> shellConfigBuilder = GodzillaConfig.builder()
|
||||
.pass(CommonUtil.getRandomString(6))
|
||||
.key(CommonUtil.getRandomString(6))
|
||||
.headerName("User-Agent")
|
||||
.headerValue(CommonUtil.getRandomString(5));
|
||||
|
||||
static Stream<Arguments> casesProvider() {
|
||||
return Stream.of(
|
||||
arguments(GodzillaFilter.class, "org.apache.utils.GodzillaFilter"),
|
||||
arguments(GodzillaListener.class, "org.apache.utils.GodzillaListener"),
|
||||
arguments(GodzillaValve.class, "org.apache.utils.GodzillaValve")
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("casesProvider")
|
||||
void generate(Class<?> clazz, String className) {
|
||||
GodzillaConfig shellConfig = shellConfigBuilder
|
||||
.className(className)
|
||||
.clazz(clazz)
|
||||
.build();
|
||||
byte[] bytes = GodzillaGenerator.generate(config, shellConfig);
|
||||
Object obj = ClassUtils.newInstance(bytes);
|
||||
assertEquals(shellConfig.getClassName(), obj.getClass().getName());
|
||||
assertEquals(shellConfig.getPass(), ClassUtils.getFieldValue(obj, "pass"));
|
||||
assertEquals(shellConfig.getHeaderName(), ClassUtils.getFieldValue(obj, "headerName"));
|
||||
assertEquals(shellConfig.getHeaderValue(), ClassUtils.getFieldValue(obj, "headerValue"));
|
||||
}
|
||||
}
|
||||
-49
@@ -1,49 +0,0 @@
|
||||
package com.reajason.javaweb.memsell.tomcat.godzilla;
|
||||
|
||||
import com.reajason.javaweb.config.Constants;
|
||||
import com.reajason.javaweb.memsell.GodzillaGenerator;
|
||||
import com.reajason.javaweb.util.ClassUtils;
|
||||
import lombok.SneakyThrows;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* @author ReaJason
|
||||
* @since 2024/11/24
|
||||
*/
|
||||
class GodzillaValveTest {
|
||||
String pass = "pass";
|
||||
String key = "key";
|
||||
String headerName = "User-Agent";
|
||||
String headerValue = "test";
|
||||
|
||||
@Test
|
||||
void generate() {
|
||||
String className = "org.apache.utils.CommonValve";
|
||||
byte[] bytes = GodzillaGenerator.generate(GodzillaValve.class, className, pass, key, headerName, headerValue);
|
||||
Object obj = ClassUtils.newInstance(bytes);
|
||||
assertEquals(className, obj.getClass().getName());
|
||||
assertEquals(pass, ClassUtils.getFieldValue(obj, "pass"));
|
||||
assertEquals("3c6e0b8a9c15224a", ClassUtils.getFieldValue(obj, "key"));
|
||||
assertEquals(headerName, ClassUtils.getFieldValue(obj, "headerName"));
|
||||
assertEquals(headerValue, ClassUtils.getFieldValue(obj, "headerValue"));
|
||||
System.out.println(Base64.encodeBase64String(bytes));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SneakyThrows
|
||||
void generateJakarta() {
|
||||
String className = "org.apache.utils.CommonJakartaValve";
|
||||
byte[] bytes = GodzillaGenerator.generate(GodzillaValve.class, className, pass, key, headerName, headerValue, true, Constants.DEFAULT_VERSION);
|
||||
// Files.write(Paths.get(className + ".class"), bytes);
|
||||
Object obj = ClassUtils.newInstance(bytes);
|
||||
assertEquals(className, obj.getClass().getName());
|
||||
assertEquals(pass, ClassUtils.getFieldValue(obj, "pass"));
|
||||
assertEquals("3c6e0b8a9c15224a", ClassUtils.getFieldValue(obj, "key"));
|
||||
assertEquals(headerName, ClassUtils.getFieldValue(obj, "headerName"));
|
||||
assertEquals(headerValue, ClassUtils.getFieldValue(obj, "headerValue"));
|
||||
System.out.println(Base64.encodeBase64String(bytes));
|
||||
}
|
||||
}
|
||||
-59
File diff suppressed because one or more lines are too long
-47
File diff suppressed because one or more lines are too long
-33
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user