REM:
1. 添加启动处置任务接口的单元测试
2. 添加迪普设备判断IP是否在设备范围内、获取全部防护对象等功能的单元测试
3. 添加浩瀚设备判断IP是否在设备范围内、获取全部防护对象等功能的单元测试
4. 增加service层创建任务的单元测试
This commit is contained in:
chenlinghy 2020-08-31 16:19:25 +08:00
parent 72a35f017c
commit 1a5c1eb76f
5 changed files with 406 additions and 53 deletions

View File

@ -83,8 +83,6 @@ public class AuthControllerTest extends InitTestEnvironment {
.getResponse() .getResponse()
.getContentAsString(); .getContentAsString();
ProtocolRespDTO<LoginRsp> rspInfo = objectMapper.readValue(ret, ProtocolRespDTO<LoginRsp> rspInfo = objectMapper.readValue(ret,
new TypeReference<ProtocolRespDTO<LoginRsp>>(){}); new TypeReference<ProtocolRespDTO<LoginRsp>>(){});

View File

@ -0,0 +1,189 @@
package com.dispose.test.controller;
import com.dispose.common.*;
import com.dispose.mapper.DisposeDeviceMapper;
import com.dispose.mapper.DisposeTaskMapper;
import com.dispose.pojo.dto.protocol.base.ProtocolReqDTO;
import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO;
import com.dispose.pojo.dto.protocol.task.TaskStartReq;
import com.dispose.pojo.dto.protocol.task.TaskStartResp;
import com.dispose.pojo.entity.DisposeTask;
import com.dispose.test.Global.InitTestEnvironment;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import jodd.net.HttpStatus;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* The type Auth controller test.
*
* @author <huangxin@cmhi.chinamoblie.com>
*/
@AutoConfigureMockMvc
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@Slf4j
@Transactional
@Rollback
public class DisposeTaskControllerTest extends InitTestEnvironment {
/**
* The Mock mvc.
*/
@Resource
private MockMvc mockMvc;
/**
* The Object mapper.
*/
@Resource
private ObjectMapper objectMapper;
/**
* The Dispose task manager.
*/
@Resource
private DisposeTaskMapper disposeTaskManager;
/**
* The Dispose device mapper.
*/
@Resource
private DisposeDeviceMapper disposeDeviceMapper;
/**
* A 1 add dispose device.
*
* @throws Exception the exception
*/
@Test
public void a1_startTask() throws Exception {
TaskStartReq req = TaskStartReq.builder()
.id(String.valueOf(disposeDeviceMapper.selectAll().get(0).getId()))
.type(DisposeCapacityType.CLEANUP.getValue())
.disposeIp("192.168.5.3")
.disposeTime(60)
.flowDirection(NetflowDirection.DIRECTION_IN.getValue())
.attackType(new Integer[]{DDoSAttackType.ACK_FLOOD.getValue()})
.flowBandwidth(DisposeConfigValue.DEFAULT_DISPOSE_BANDWIDTH)
.build();
ProtocolReqDTO<TaskStartReq> reqInfo = new ProtocolReqDTO<>();
reqInfo.setVer(ConstValue.Protocol.VERSION);
reqInfo.setCryptoType(ProtoCryptoType.CRYPTO_NONE.getCode());
reqInfo.setTimeStamp(System.currentTimeMillis());
reqInfo.setMsgContent(req);
String ret = mockMvc.perform(MockMvcRequestBuilders
.post("/task/start")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", ConstValue.STRING_HTTP_AUTH_HEAD + getLoginToken())
.content(objectMapper.writeValueAsString(reqInfo)))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(HttpStatus.ok().status()))
.andReturn()
.getResponse()
.getContentAsString();
ProtocolRespDTO<TaskStartResp> rspInfo = objectMapper.readValue(ret,
new TypeReference<ProtocolRespDTO<TaskStartResp>>() {
});
verifyRespProtocol(rspInfo);
log.debug(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rspInfo));
Assert.assertNotNull(rspInfo.getMsgContent().getStatus());
Assert.assertNotNull(rspInfo.getMsgContent().getMessage());
if (rspInfo.getMsgContent().getStatus() == ErrorCode.ERR_OK.getCode()) {
Assert.assertNotNull(rspInfo.getMsgContent().getTaskId());
disposeTaskManager.selectAll().forEach(v -> {
if (v.getDeviceId().equals(Long.valueOf(req.getId())) &&
v.getDisposeIp().equals(req.getDisposeIp()) &&
v.getAttackType().equals((long) DDoSAttackType.ACK_FLOOD.getValue())) {
Assert.assertEquals(Long.valueOf(rspInfo.getMsgContent().getTaskId()), v.getId());
}
});
}
}
/**
* A 2 add dispose device existed.
*
* @throws Exception the exception
*/
@Test
public void a2_startTaskExisted() throws Exception {
DisposeTask disposeTask = disposeTaskManager.selectAll().get(0);
TaskStartReq req = TaskStartReq.builder()
.id(String.valueOf(disposeTask.getId()))
.type(disposeTask.getDisposeCapacity().getValue())
.disposeIp(disposeTask.getDisposeIp())
.disposeTime(60)
.flowDirection(NetflowDirection.DIRECTION_IN.getValue())
.attackType(new Integer[]{DDoSAttackType.ACK_FLOOD.getValue()})
.flowBandwidth(DisposeConfigValue.DEFAULT_DISPOSE_BANDWIDTH)
.build();
ProtocolReqDTO<TaskStartReq> reqInfo = new ProtocolReqDTO<>();
reqInfo.setVer(ConstValue.Protocol.VERSION);
reqInfo.setCryptoType(ProtoCryptoType.CRYPTO_NONE.getCode());
reqInfo.setTimeStamp(System.currentTimeMillis());
reqInfo.setMsgContent(req);
String ret = mockMvc.perform(MockMvcRequestBuilders
.post("/task/start")
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", ConstValue.STRING_HTTP_AUTH_HEAD + getLoginToken())
.content(objectMapper.writeValueAsString(reqInfo)))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(HttpStatus.ok().status()))
.andReturn()
.getResponse()
.getContentAsString();
ProtocolRespDTO<TaskStartResp> rspInfo = objectMapper.readValue(ret,
new TypeReference<ProtocolRespDTO<TaskStartResp>>() {
});
verifyRespProtocol(rspInfo);
log.debug(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(rspInfo));
Assert.assertNotNull(rspInfo.getMsgContent().getStatus());
Assert.assertNotNull(rspInfo.getMsgContent().getMessage());
if (disposeTask.getCurrentStatus() == DisposeTaskStatus.TASK_FINISHED ||
disposeTask.getCurrentStatus() == DisposeTaskStatus.TASK_CANCELED ||
disposeTask.getCurrentStatus() == DisposeTaskStatus.TASK_EXPIRED) {
Assert.assertEquals(String.valueOf(rspInfo.getMsgContent().getStatus()), String.valueOf(ErrorCode.ERR_OK.getCode()));
} else {
Assert.assertEquals(String.valueOf(rspInfo.getMsgContent().getStatus()), String.valueOf(ErrorCode.ERR_TASKRUNNING.getCode()));
}
}
}

View File

@ -177,6 +177,25 @@ public class DPTechInterfaceTestCase extends InitTestEnvironment {
Assert.assertEquals(ret.getSecondParam().getKernel(), "Windows"); Assert.assertEquals(ret.getSecondParam().getKernel(), "Windows");
Assert.assertEquals(ret.getSecondParam().getArch(), "x86_64"); Assert.assertEquals(ret.getSecondParam().getArch(), "x86_64");
Assert.assertEquals(ret.getSecondParam().getVersion(), "5.7.31"); Assert.assertEquals(ret.getSecondParam().getVersion(), "5.7.31");
//to device attack type long.
Long ddosAttackTypeMask = 10737508814L;
Long result = db.toDeviceAttackType(ddosAttackTypeMask);
log.info("ddosAttackTypeMask-->{}, result-->{}", ddosAttackTypeMask, result);
Assert.assertNotNull(result);
Assert.assertNotEquals(String.valueOf(result), "0");
//gets dispose device protect object.
db.getDisposeDeviceProtectObject();
//is carry protect ip boolean.
boolean carryResult = db.isCarryProtectIp("192.168.3.2");
Assert.assertTrue(carryResult);
carryResult = db.isCarryProtectIp("192.168.10.2");
Assert.assertFalse(carryResult);
} }
} }
} }

View File

@ -176,6 +176,26 @@ public class HAOHanInterfaceTestCast extends InitTestEnvironment {
Assert.assertEquals(ret.getSecondParam().getKernel(), "Linux"); Assert.assertEquals(ret.getSecondParam().getKernel(), "Linux");
Assert.assertEquals(ret.getSecondParam().getArch(), "x86_64"); Assert.assertEquals(ret.getSecondParam().getArch(), "x86_64");
Assert.assertEquals(ret.getSecondParam().getVersion(), "Unknown"); Assert.assertEquals(ret.getSecondParam().getVersion(), "Unknown");
//to device attack type long.
Long ddosAttackTypeMask = 10737508814L;
Long result = db.toDeviceAttackType(ddosAttackTypeMask);
log.info("ddosAttackTypeMask-->{}, result-->{}", ddosAttackTypeMask, result);
Assert.assertNotNull(result);
Assert.assertEquals(String.valueOf(ddosAttackTypeMask), String.valueOf(result));
//gets dispose device protect object.
db.getDisposeDeviceProtectObject();
//is carry protect ip boolean. 所有IP都可执行清洗命令
boolean carryResult = db.isCarryProtectIp("192.168.5.2");
Assert.assertTrue(carryResult);
carryResult = db.isCarryProtectIp("");
Assert.assertTrue(carryResult);
} }
} }
} }

View File

@ -0,0 +1,127 @@
package com.dispose.test.service;
import com.dispose.common.*;
import com.dispose.manager.DisposeTaskManager;
import com.dispose.mapper.DisposeDeviceMapper;
import com.dispose.mapper.UserAccountMapper;
import com.dispose.pojo.entity.DisposeTask;
import com.dispose.pojo.po.MulReturnType;
import com.dispose.service.DisposeTaskService;
import com.dispose.service.UserAccountService;
import com.dispose.test.Global.InitTestEnvironment;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.security.NoSuchAlgorithmException;
/**
* The type User account service test.
*
* @author <huangxin@cmhi.chinamoblie.com>
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@Transactional
@Rollback
public class DisposeTaskServiceTest extends InitTestEnvironment {
/**
* The constant token.
*/
private static String token = "";
/**
* The User account service.
*/
@Resource
private UserAccountService userAccountService;
/**
* The Object mapper.
*/
@Resource
private ObjectMapper objectMapper;
/**
* The user account mapper.
*/
@Resource
private UserAccountMapper userAccountMapper;
/**
* The Dispose device mapper.
*/
@Resource
private DisposeDeviceMapper disposeDeviceMapper;
/**
* The Dispose task manager.
*/
@Resource
private DisposeTaskManager disposeTaskManager;
/**
* The Dispose task service.
*/
@Resource
private DisposeTaskService disposeTaskService;
/**
* User login test.
*
* @throws NoSuchAlgorithmException the no such algorithm exception
*/
@Before
public void userLoginTest() throws NoSuchAlgorithmException {
MulReturnType<ErrorCode, String> ret = userAccountService.loginService(getUSER_NAME(),
getPASSWORD());
if (ret.getFirstParam() == ErrorCode.ERR_OK) {
DisposeTaskServiceTest.token = ret.getSecondParam();
}
}
/**
* creat task mul return type.
*/
@Test
public void a1_createTask() throws JsonProcessingException {
// 构成处置任务参数
DisposeTask newTask = DisposeTask.builder()
.deviceId(disposeDeviceMapper.selectAll().get(0).getId())
.accountId(userAccountMapper.selectAll().get(0).getId())
.disposeCapacity(DisposeCapacityType.CLEANUP)
.disposeIp("192.168.5.2")
.planEndTime("30")
.flowDirection(NetflowDirection.DIRECTION_IN)
.attackType(DDoSAttackType.getTypeMaskFromAttackType(new Integer[]{DDoSAttackType.ALL_ATTACKS.getValue()}))
.flowBandWidth(DisposeConfigValue.DEFAULT_DISPOSE_BANDWIDTH)
.build();
log.info("creat new task: {}", objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(newTask));
MulReturnType<ErrorCode, Long> ret = disposeTaskService.createTask(newTask);
if (ret.getFirstParam() == ErrorCode.ERR_OK) {
Assert.assertNotNull(ret.getSecondParam());
} else {
Assert.assertNotEquals(ret.getFirstParam().getCode(), ErrorCode.ERR_OK.getCode());
}
Assert.assertEquals(ret.getSecondParam(), disposeTaskManager.getDisposeTask(newTask.getDeviceId(),
newTask.getDisposeIp(), newTask.getDisposeCapacity()).getId());
ret = disposeTaskService.createTask(newTask);
Assert.assertEquals(ret.getFirstParam().getCode(), ErrorCode.ERR_TASKRUNNING.getCode());
Assert.assertEquals(ret.getSecondParam(), disposeTaskManager.getDisposeTask(newTask.getDeviceId(),
newTask.getDisposeIp(), newTask.getDisposeCapacity()).getId());
}
}