Skip to content

Commit e9aec46

Browse files
authored
Merge pull request #592 from 15911075183ma/fix-Feign-bug
Fix feign bug
2 parents 770adf1 + 9ff4aa5 commit e9aec46

File tree

11 files changed

+445
-12
lines changed

11 files changed

+445
-12
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.dongtai.iast.agent.util;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class ByteUtilsTest {
7+
@Test
8+
public void testFormatByteSize() {
9+
// 测试formatByteSize方法是否返回正确的格式化字符串
10+
11+
// 测试不足1KB的情况
12+
Assert.assertEquals("0B", ByteUtils.formatByteSize(0));
13+
Assert.assertEquals("1023B", ByteUtils.formatByteSize(1023));
14+
15+
// 测试不足1MB的情况
16+
Assert.assertEquals("1KB", ByteUtils.formatByteSize(1024));
17+
Assert. assertEquals("1.5KB", ByteUtils.formatByteSize(1536));
18+
Assert.assertEquals("1023.5KB", ByteUtils.formatByteSize(1023 * 1024 + 512));
19+
20+
// 测试不足1GB的情况
21+
Assert.assertEquals("1MB", ByteUtils.formatByteSize(1024 * 1024));
22+
Assert.assertEquals("1.5MB", ByteUtils.formatByteSize(1536 * 1024));
23+
Assert.assertEquals("1023.5MB", ByteUtils.formatByteSize(1023 * 1024 * 1024 + 512 * 1024));
24+
25+
// 测试不足1TB的情况
26+
Assert.assertEquals("1GB", ByteUtils.formatByteSize(1024 * 1024 * 1024));
27+
Assert.assertEquals("1.5GB", ByteUtils.formatByteSize(1536 * 1024 * 1024));
28+
Assert.assertEquals("1023.5GB", ByteUtils.formatByteSize(1023L * 1024 * 1024 * 1024 + 512 * 1024 * 1024));
29+
30+
// 测试不足1PB的情况
31+
Assert.assertEquals("1TB", ByteUtils.formatByteSize(1024L * 1024 * 1024 * 1024));
32+
Assert.assertEquals("1.5TB", ByteUtils.formatByteSize(1536L * 1024 * 1024 * 1024));
33+
Assert.assertEquals("1023.5TB", ByteUtils.formatByteSize(1023L * 1024 * 1024 * 1024 * 1024 + 512L * 1024 * 1024 * 1024));
34+
35+
// 测试超过1PB的情况
36+
Assert.assertEquals(">PB", ByteUtils.formatByteSize(Long.MAX_VALUE));
37+
}
38+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.dongtai.iast.agent.util;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
import java.io.IOException;
7+
8+
public class FileUtilsTest {
9+
@Test
10+
public void testGetResourceToFile() {
11+
// 测试getResourceToFile方法是否能正确地将资源文件复制到指定的文件路径
12+
13+
//获取临时文件路径
14+
String tempDirectoryPath = org.apache.commons.io.FileUtils.getTempDirectoryPath();
15+
16+
String resourceName = "iast.properties.example"; // 假设存在名为test_resource.txt的资源文件
17+
String fileName = tempDirectoryPath + "test.example"; // 替换为实际的目标文件路径
18+
19+
boolean result;
20+
try {
21+
result = FileUtils.getResourceToFile(resourceName, fileName);
22+
} catch (IOException e) {
23+
throw new RuntimeException(e);
24+
}
25+
26+
Assert.assertTrue(result); // 验证复制操作是否成功
27+
28+
// 验证目标文件是否存在
29+
java.io.File targetFile = new java.io.File(fileName);
30+
Assert.assertTrue(targetFile.exists());
31+
32+
// 清理测试产生的文件
33+
targetFile.delete();
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package io.dongtai.iast.api.gather.dubbo.convertor;
2+
3+
import io.dongtai.iast.api.openapi.convertor.OpenApiSchemaConvertorManager;
4+
import io.dongtai.iast.api.openapi.domain.DataType;
5+
import io.dongtai.iast.api.openapi.domain.MediaType;
6+
import io.dongtai.iast.api.openapi.domain.Operation;
7+
import io.dongtai.iast.api.openapi.domain.Response;
8+
import org.junit.Assert;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
import java.lang.reflect.Method;
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
16+
public class MethodConvertorTest {
17+
18+
private OpenApiSchemaConvertorManager manager;
19+
private Method reflectionMethod;
20+
private Method reflectionReturnMethod;
21+
22+
@Before
23+
public void setUp() throws NoSuchMethodException {
24+
// 创建 OpenApiSchemaConvertorManager 实例,或者使用真实的实例
25+
manager = new OpenApiSchemaConvertorManager();
26+
27+
// 创建反射方法,或者使用真实的反射方法
28+
reflectionMethod = SampleService.class.getMethod("sampleMethod",String.class,String.class);
29+
reflectionReturnMethod = SampleService.class.getMethod("stringListSampleMethod",int.class);
30+
}
31+
32+
@Test
33+
public void testConvert() {
34+
// 创建 MethodConvertor 实例
35+
MethodConvertor methodConvertor = new MethodConvertor(manager, reflectionMethod);
36+
37+
// 调用 convert 方法
38+
Operation convertedOperation = methodConvertor.convert();
39+
40+
// 进行断言,验证转换后的 Operation 对象是否符合预期
41+
Assert.assertNotNull(convertedOperation);
42+
//判断类型是否正确
43+
Assert.assertEquals(DataType.String().getType(),convertedOperation.getParameters().get(0).getSchema().getType());
44+
Assert.assertEquals(DataType.String().getType(),convertedOperation.getParameters().get(1).getSchema().getType());
45+
46+
MethodConvertor methodConvertor1 = new MethodConvertor(manager, reflectionReturnMethod);
47+
Operation convert = methodConvertor1.convert();
48+
Assert.assertNotNull(convert);
49+
//判断有返回值的情况下是否正确
50+
Assert.assertEquals(DataType.Array(DataType.String())
51+
.getType(),convert.getResponses().get(Response.CODE_OK).getContent().get(MediaType.APPLICATION_JSON)
52+
.getSchema().getType());
53+
54+
55+
}
56+
57+
// 示例服务类,用于测试
58+
static class SampleService {
59+
/**
60+
* 示例方法 无返回值
61+
* @param s1 字符串1
62+
* @param s2 字符串2
63+
*/
64+
public void sampleMethod(String s1,String s2) {
65+
// 示例方法,用于测试
66+
System.out.println(s1);
67+
System.out.println(s1+s2);
68+
}
69+
70+
public List<String> stringListSampleMethod(int int1){
71+
System.out.println(int1);
72+
return new ArrayList<>();
73+
}
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package io.dongtai.iast.api.gather.dubbo.convertor;
2+
3+
import io.dongtai.iast.api.openapi.convertor.OpenApiSchemaConvertorManager;
4+
import io.dongtai.iast.api.openapi.domain.Parameter;
5+
import io.dongtai.iast.api.openapi.domain.Path;
6+
import org.junit.Assert;
7+
import org.junit.Test;
8+
9+
import java.lang.reflect.Method;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
public class ServiceConvertorTest {
14+
@Test
15+
public void testConvert() {
16+
// 创建一个 OpenApiSchemaConvertorManager,这里可以根据需要进行配置
17+
OpenApiSchemaConvertorManager manager = new OpenApiSchemaConvertorManager();
18+
19+
// 创建一个 ServiceConvertor 实例,传入需要转换的接口类
20+
Class<?> interfaceClass = TestDubboServiceInterface.class; // 替换成实际的接口类
21+
ServiceConvertor serviceConvertor = new ServiceConvertor(manager, interfaceClass);
22+
23+
// 调用 convert 方法进行转换
24+
Map<String, Path> pathMap = serviceConvertor.convert();
25+
26+
// 打印转换结果或进行其他断言
27+
for (Map.Entry<String, Path> entry : pathMap.entrySet()) {
28+
String path = entry.getKey();
29+
Path pathData = entry.getValue();
30+
// 进行其他断言或操作
31+
Method method = AnalysisMethod(path);
32+
List<Parameter> parameters = pathData.getDubbo().getParameters();
33+
Class<?>[] parameterTypes = method.getParameterTypes();
34+
for (int i = 0, parametersSize = parameters.size(); i < parametersSize; i++) {
35+
Parameter parameter = parameters.get(i);
36+
String type = parameter.getSchema().getType();
37+
Class<?> parameterType = parameterTypes[i];
38+
String s = interceptType(parameterType);
39+
Assert.assertTrue(s.equalsIgnoreCase(type));
40+
}
41+
42+
}
43+
}
44+
45+
/**
46+
* 将JAVA类型转换为Openapi类型
47+
* @param parameterType java类型
48+
* @return Openapi类型
49+
*/
50+
private static String interceptType(Class<?> parameterType) {
51+
String[] split = parameterType.getName().split("\\.");
52+
return split[split.length - 1];
53+
}
54+
55+
/**
56+
* 通过PATH 查找方法
57+
* @param path 方法路径
58+
* @return 方法
59+
*/
60+
private Method AnalysisMethod(String path) {
61+
String[] split = path.split("/");
62+
Assert.assertEquals(3, split.length);
63+
String methodNameWithParams = split[2];
64+
65+
String[] methodParts = methodNameWithParams.split("\\(");
66+
String methodName = methodParts[0];
67+
String paramsStr = methodParts[1].replace(")", "");
68+
String[] paramTypeNames = paramsStr.split(",");
69+
Class<?>[] paramTypes = new Class[paramTypeNames.length];
70+
for (int i = 0; i < paramTypeNames.length; i++) {
71+
try {
72+
paramTypes[i] = Class.forName(paramTypeNames[i]);
73+
} catch (ClassNotFoundException e) {
74+
throw new RuntimeException(e);
75+
}
76+
}
77+
Method method;
78+
try {
79+
method = TestDubboServiceInterface.class.getMethod(methodName, paramTypes);
80+
81+
} catch (NoSuchMethodException e) {
82+
throw new RuntimeException(e);
83+
}
84+
return method;
85+
86+
}
87+
88+
// Dubbo 服务接口类
89+
private interface TestDubboServiceInterface {
90+
// 定义需要测试的 Dubbo 服务方法
91+
String sayHi(String name);
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package io.dongtai.iast.api.gather.spring.convertor;
2+
3+
import io.dongtai.iast.api.openapi.convertor.OpenApiSchemaConvertorManager;
4+
import io.dongtai.iast.api.openapi.domain.DataType;
5+
import io.dongtai.iast.api.openapi.domain.Operation;
6+
import io.dongtai.iast.api.openapi.domain.Parameter;
7+
import org.junit.Assert;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
import org.springframework.web.context.support.StaticWebApplicationContext;
11+
import org.springframework.web.method.HandlerMethod;
12+
13+
import java.util.List;
14+
15+
public class HandlerMethodConvertorTest {
16+
private HandlerMethodConvertor handlerMethodConvertor;
17+
private Operation operation;
18+
19+
@Before
20+
public void setUp() {
21+
// 在每个测试方法运行之前初始化必要的对象
22+
OpenApiSchemaConvertorManager manager = new OpenApiSchemaConvertorManager(); // 创建实际的 manager 对象
23+
operation = new Operation();
24+
HandlerMethod handlerMethod;
25+
try {
26+
handlerMethod = new HandlerMethod(new TestController(), "testMethod",String.class,int.class); // 创建实际的 HandlerMethod 对象
27+
} catch (NoSuchMethodException e) {
28+
throw new RuntimeException(e);
29+
}
30+
StaticWebApplicationContext webApplicationContext = new StaticWebApplicationContext(); // 创建一个静态的 WebApplicationContext
31+
webApplicationContext.registerSingleton("testController", TestController.class); // 注册控制器实例
32+
handlerMethodConvertor = new HandlerMethodConvertor(manager, webApplicationContext, operation, handlerMethod);
33+
}
34+
35+
@Test
36+
public void testParseParameters() {
37+
// 调用parseParameters方法
38+
handlerMethodConvertor.parse();
39+
40+
// 获取解析后的参数列表
41+
List<Parameter> parameters = operation.getParameters();
42+
//判断是否是有两个参数
43+
Assert.assertEquals(2, parameters.size());
44+
//判断第一个参数为string是否正确
45+
Assert.assertEquals(DataType.String().getType(),parameters.get(0).getSchema().getType());
46+
//判断第二个参数是否为int32
47+
Assert.assertEquals(DataType.Int32().getType(),parameters.get(1).getSchema().getType());
48+
}
49+
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.dongtai.iast.api.gather.spring.convertor;
2+
3+
/**
4+
* 因未引入servlet,无法测试
5+
*/
6+
public class RequestMappingHandlerMappingConvertorTest {
7+
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.dongtai.iast.api.gather.spring.convertor;
2+
3+
/**
4+
* 假设了一个名为 TestController 的控制器类
5+
*/
6+
public class TestController {
7+
public String testMethod( String param1,int param2) {
8+
// 实际的控制器方法逻辑
9+
return "Hello, " + param1 + ". Your number is " + param2;
10+
}
11+
}

dongtai-common/src/test/java/io/dongtai/iast/common/string/StringUtilsTest.java

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,78 @@
11
package io.dongtai.iast.common.string;
22

3-
import io.dongtai.iast.common.string.StringUtils;
43
import org.junit.Assert;
54
import org.junit.Test;
65

76
public class StringUtilsTest {
7+
8+
9+
@Test
10+
public void matchTest() {
11+
String raw = "foo";
12+
String compareValues = "foo";
13+
//完全相同比较
14+
Assert.assertTrue(StringUtils.match(raw, compareValues));
15+
16+
String objectValue = new String("foo");
17+
//对象字符串与常量字符串比较,不完全相同
18+
Assert.assertFalse(StringUtils.match(raw, objectValue));
19+
}
20+
21+
@Test
22+
public void testConvertStringToIntArray() {
23+
// 准备测试数据
24+
String input = "P1,2,3,4";
25+
// 调用被测试方法
26+
int[] result = StringUtils.convertStringToIntArray(input);
27+
// 期望的结果
28+
int[] expected = {0, 1, 2, 3}; // 输入中的每个数字减去1
29+
// 使用断言验证结果
30+
Assert.assertArrayEquals(expected, result);
31+
32+
// 测试包含负数的情况
33+
input = "P-1,-2,-3,-4";
34+
35+
result = StringUtils.convertStringToIntArray(input);
36+
37+
expected = new int[]{-2, -3, -4, -5};
38+
39+
Assert.assertArrayEquals(expected, result);
40+
}
41+
42+
@Test
43+
public void isEmptyTest() {
44+
//判断空字符串
45+
String input = "";
46+
Assert.assertTrue(StringUtils.isEmpty(input));
47+
//判断空对象
48+
input = null;
49+
Assert.assertTrue(StringUtils.isEmpty(input));
50+
//判断有值字符串
51+
input = "in";
52+
Assert.assertFalse(StringUtils.isEmpty(input));
53+
//判断空格
54+
input = " ";
55+
Assert.assertFalse(StringUtils.isEmpty(input));
56+
57+
58+
}
59+
60+
@Test
61+
public void isBlankTest(){
62+
//判断空字符串
63+
String input = "";
64+
Assert.assertTrue(StringUtils.isBlank(input));
65+
//判断空对象
66+
input = null;
67+
Assert.assertTrue(StringUtils.isBlank(input));
68+
//判断有值字符串
69+
input = "in";
70+
Assert.assertFalse(StringUtils.isBlank(input));
71+
//判断空格
72+
input = " ";
73+
Assert.assertTrue(StringUtils.isBlank(input));
74+
}
75+
876
@Test
977
public void normalize() {
1078
int maxLength = 1024;

0 commit comments

Comments
 (0)