Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:添加测试用例 #588

Merged
merged 4 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.dongtai.iast.agent.util;

import org.junit.Assert;
import org.junit.Test;

public class ByteUtilsTest {
@Test
public void testFormatByteSize() {
// 测试formatByteSize方法是否返回正确的格式化字符串

// 测试不足1KB的情况
Assert.assertEquals("0B", ByteUtils.formatByteSize(0));
Assert.assertEquals("1023B", ByteUtils.formatByteSize(1023));

// 测试不足1MB的情况
Assert.assertEquals("1KB", ByteUtils.formatByteSize(1024));
Assert. assertEquals("1.5KB", ByteUtils.formatByteSize(1536));
Assert.assertEquals("1023.5KB", ByteUtils.formatByteSize(1023 * 1024 + 512));

// 测试不足1GB的情况
Assert.assertEquals("1MB", ByteUtils.formatByteSize(1024 * 1024));
Assert.assertEquals("1.5MB", ByteUtils.formatByteSize(1536 * 1024));
Assert.assertEquals("1023.5MB", ByteUtils.formatByteSize(1023 * 1024 * 1024 + 512 * 1024));

// 测试不足1TB的情况
Assert.assertEquals("1GB", ByteUtils.formatByteSize(1024 * 1024 * 1024));
Assert.assertEquals("1.5GB", ByteUtils.formatByteSize(1536 * 1024 * 1024));
Assert.assertEquals("1023.5GB", ByteUtils.formatByteSize(1023L * 1024 * 1024 * 1024 + 512 * 1024 * 1024));

// 测试不足1PB的情况
Assert.assertEquals("1TB", ByteUtils.formatByteSize(1024L * 1024 * 1024 * 1024));
Assert.assertEquals("1.5TB", ByteUtils.formatByteSize(1536L * 1024 * 1024 * 1024));
Assert.assertEquals("1023.5TB", ByteUtils.formatByteSize(1023L * 1024 * 1024 * 1024 * 1024 + 512L * 1024 * 1024 * 1024));

// 测试超过1PB的情况
Assert.assertEquals(">PB", ByteUtils.formatByteSize(Long.MAX_VALUE));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.dongtai.iast.agent.util;

import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;

public class FileUtilsTest {
@Test
public void testGetResourceToFile() {
// 测试getResourceToFile方法是否能正确地将资源文件复制到指定的文件路径

//获取临时文件路径
String tempDirectoryPath = org.apache.commons.io.FileUtils.getTempDirectoryPath();

String resourceName = "iast.properties.example"; // 假设存在名为test_resource.txt的资源文件
String fileName = tempDirectoryPath + "test.example"; // 替换为实际的目标文件路径

boolean result;
try {
result = FileUtils.getResourceToFile(resourceName, fileName);
} catch (IOException e) {
throw new RuntimeException(e);
}

Assert.assertTrue(result); // 验证复制操作是否成功

// 验证目标文件是否存在
java.io.File targetFile = new java.io.File(fileName);
Assert.assertTrue(targetFile.exists());

// 清理测试产生的文件
targetFile.delete();
}
}
43 changes: 43 additions & 0 deletions dongtai-log/src/test/java/io.dongtai.log/IastPropertiesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
public class IastPropertiesTest {
private final String oldTmpPath = System.getProperty("java.io.tmpdir.dongtai");
private final String oldLogPath = System.getProperty("dongtai.log.path");
private final String LogLevel = System.getProperty("dongtai.log.level");

private final String switchSign = System.getProperty("dongtai.log");



@Before
public void setUp() {
Expand All @@ -22,11 +27,35 @@ public void tearDown() {
if (oldLogPath != null) {
System.setProperty("dongtai.log.path", oldLogPath);
}
if (LogLevel != null) {
System.setProperty("dongtai.log.level", LogLevel);
}
if (switchSign != null) {
System.setProperty("dongtai.log", switchSign);
}
}

private void clear() {
System.clearProperty("java.io.tmpdir.dongtai");
System.clearProperty("dongtai.log.path");
System.clearProperty("dongtai.log.level");
System.clearProperty("dongtai.log");
}


@Test
public void isEnabledTest(){
boolean enabled = IastProperties.isEnabled();
//默认开启
Assert.assertTrue("isEnabled:" + enabled, enabled);


//修改为关闭
System.setProperty("dongtai.log", "false");
enabled = IastProperties.isEnabled();
Assert.assertFalse("isEnabled:" + enabled,enabled);


}

@Test
Expand All @@ -49,4 +78,18 @@ public void getLogPathTest() {
path = IastProperties.getLogDir();
Assert.assertEquals(File.separator + "foo", path);
}

@Test
public void getLogLevelTest() {
//默认使用info级别
String logLevel = IastProperties.getLogLevel();
Assert.assertEquals("log level:" + logLevel, "info", logLevel);
clear();
//修改为debug级别
System.setProperty("dongtai.log.level", "debug");
logLevel = IastProperties.getLogLevel();
Assert.assertEquals("log level:" + logLevel, "debug", logLevel);


}
}