diff --git a/src/test/java/com/dispose/test/controller/AuthControllerTest.java b/src/test/java/com/dispose/test/controller/AuthControllerTest.java index e89955d4..d48e381b 100644 --- a/src/test/java/com/dispose/test/controller/AuthControllerTest.java +++ b/src/test/java/com/dispose/test/controller/AuthControllerTest.java @@ -83,8 +83,6 @@ public class AuthControllerTest extends InitTestEnvironment { .getResponse() .getContentAsString(); - - ProtocolRespDTO rspInfo = objectMapper.readValue(ret, new TypeReference>(){}); diff --git a/src/test/java/com/dispose/test/controller/DisposeTaskControllerTest.java b/src/test/java/com/dispose/test/controller/DisposeTaskControllerTest.java new file mode 100644 index 00000000..c5a79c1f --- /dev/null +++ b/src/test/java/com/dispose/test/controller/DisposeTaskControllerTest.java @@ -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 + */ +@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 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 rspInfo = objectMapper.readValue(ret, + new TypeReference>() { + }); + + 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 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 rspInfo = objectMapper.readValue(ret, + new TypeReference>() { + }); + + 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())); + } + } + +} diff --git a/src/test/java/com/dispose/test/dptech/DPTechInterfaceTestCase.java b/src/test/java/com/dispose/test/dptech/DPTechInterfaceTestCase.java index c8e7d6c8..7440d99f 100644 --- a/src/test/java/com/dispose/test/dptech/DPTechInterfaceTestCase.java +++ b/src/test/java/com/dispose/test/dptech/DPTechInterfaceTestCase.java @@ -85,7 +85,7 @@ public class DPTechInterfaceTestCase extends InitTestEnvironment { db.initDeviceEnv(url, dev.getUserName(), dev.getPassword()); MulReturnType ret = db.runDispose("192.168.3.5", DisposeCapacityType.CLEANUP, - NetflowDirection.DIRECTION_BI, DDoSAttackType.ALL_ATTACKS.getValue(), -1L); + NetflowDirection.DIRECTION_BI, DDoSAttackType.ALL_ATTACKS.getValue(), -1L); Assert.assertEquals(ret.getFirstParam(), ErrorCode.ERR_OK); } @@ -127,7 +127,7 @@ public class DPTechInterfaceTestCase extends InitTestEnvironment { Assert.assertTrue(db.getDeviceLinkStatus()); MulReturnType ret = db.stopDispose("192.168.3.5", DisposeCapacityType.CLEANUP, - NetflowDirection.DIRECTION_BI, DDoSAttackType.ALL_ATTACKS.getValue(), -1L); + NetflowDirection.DIRECTION_BI, DDoSAttackType.ALL_ATTACKS.getValue(), -1L); Assert.assertEquals(ret.getFirstParam(), ErrorCode.ERR_OK); } @@ -177,6 +177,25 @@ public class DPTechInterfaceTestCase extends InitTestEnvironment { Assert.assertEquals(ret.getSecondParam().getKernel(), "Windows"); Assert.assertEquals(ret.getSecondParam().getArch(), "x86_64"); 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); } } } diff --git a/src/test/java/com/dispose/test/haohan/HAOHanInterfaceTestCast.java b/src/test/java/com/dispose/test/haohan/HAOHanInterfaceTestCast.java index 74e38517..d97c9d7e 100644 --- a/src/test/java/com/dispose/test/haohan/HAOHanInterfaceTestCast.java +++ b/src/test/java/com/dispose/test/haohan/HAOHanInterfaceTestCast.java @@ -56,21 +56,21 @@ public class HAOHanInterfaceTestCast extends InitTestEnvironment { DisposeAbility db; DisposeDevice dev = DisposeDevice.builder() - .ipAddr("10.88.77.88") - .ipPort("18080") - .deviceType(DisposeDeviceType.HAOHAN_PLATFORM) - .areaCode(0) - .deviceName("中移杭研实验室浩瀚清洗设备") - .manufacturer("Haohan") - .model("Unknown") - .version("Unknown") - .userName("") - .password("") - .urlPath("DDoSClean/clean") - .urlType(HttpType.HTTP) - .readme("实验室测试设备") - .status(ObjectStatus.NORMAL) - .build(); + .ipAddr("10.88.77.88") + .ipPort("18080") + .deviceType(DisposeDeviceType.HAOHAN_PLATFORM) + .areaCode(0) + .deviceName("中移杭研实验室浩瀚清洗设备") + .manufacturer("Haohan") + .model("Unknown") + .version("Unknown") + .userName("") + .password("") + .urlPath("DDoSClean/clean") + .urlType(HttpType.HTTP) + .readme("实验室测试设备") + .status(ObjectStatus.NORMAL) + .build(); String httpType = dev.getUrlType() == HttpType.HTTP ? "http://" : "https://"; String addr = getAbilityMapKey(dev.getIpAddr(), dev.getIpPort()); @@ -83,8 +83,8 @@ public class HAOHanInterfaceTestCast extends InitTestEnvironment { db.initDeviceEnv(url, dev.getUserName(), dev.getPassword()); MulReturnType ret = db.runDispose("192.168.3.5", - DisposeCapacityType.CLEANUP, - null, null, -1L); + DisposeCapacityType.CLEANUP, + null, null, -1L); Assert.assertEquals(ret.getFirstParam(), ErrorCode.ERR_OK); } @@ -98,21 +98,21 @@ public class HAOHanInterfaceTestCast extends InitTestEnvironment { DisposeAbility db; DisposeDevice dev = DisposeDevice.builder() - .ipAddr("10.88.77.88") - .ipPort("18080") - .deviceType(DisposeDeviceType.HAOHAN_PLATFORM) - .areaCode(0) - .deviceName("中移杭研实验室浩瀚清洗设备") - .manufacturer("Haohan") - .model("Unknown") - .version("Unknown") - .userName("") - .password("") - .urlPath("DDoSClean/clean") - .urlType(HttpType.HTTP) - .readme("实验室测试设备") - .status(ObjectStatus.NORMAL) - .build(); + .ipAddr("10.88.77.88") + .ipPort("18080") + .deviceType(DisposeDeviceType.HAOHAN_PLATFORM) + .areaCode(0) + .deviceName("中移杭研实验室浩瀚清洗设备") + .manufacturer("Haohan") + .model("Unknown") + .version("Unknown") + .userName("") + .password("") + .urlPath("DDoSClean/clean") + .urlType(HttpType.HTTP) + .readme("实验室测试设备") + .status(ObjectStatus.NORMAL) + .build(); String httpType = dev.getUrlType() == HttpType.HTTP ? "http://" : "https://"; String addr = getAbilityMapKey(dev.getIpAddr(), dev.getIpPort()); @@ -126,8 +126,8 @@ public class HAOHanInterfaceTestCast extends InitTestEnvironment { Assert.assertTrue(db.getDeviceLinkStatus()); MulReturnType ret = db.stopDispose("192.168.3.5", - DisposeCapacityType.CLEANUP, - null, null, -1L); + DisposeCapacityType.CLEANUP, + null, null, -1L); Assert.assertEquals(ret.getFirstParam(), ErrorCode.ERR_OK); } @@ -141,21 +141,21 @@ public class HAOHanInterfaceTestCast extends InitTestEnvironment { DisposeAbility db; DisposeDevice dev = DisposeDevice.builder() - .ipAddr("10.88.77.88") - .ipPort("18080") - .deviceType(DisposeDeviceType.HAOHAN_PLATFORM) - .areaCode(0) - .deviceName("中移杭研实验室浩瀚清洗设备") - .manufacturer("Haohan") - .model("Unknown") - .version("Unknown") - .userName("") - .password("") - .urlPath("DDoSClean/clean") - .urlType(HttpType.HTTP) - .readme("实验室测试设备") - .status(ObjectStatus.NORMAL) - .build(); + .ipAddr("10.88.77.88") + .ipPort("18080") + .deviceType(DisposeDeviceType.HAOHAN_PLATFORM) + .areaCode(0) + .deviceName("中移杭研实验室浩瀚清洗设备") + .manufacturer("Haohan") + .model("Unknown") + .version("Unknown") + .userName("") + .password("") + .urlPath("DDoSClean/clean") + .urlType(HttpType.HTTP) + .readme("实验室测试设备") + .status(ObjectStatus.NORMAL) + .build(); String httpType = dev.getUrlType() == HttpType.HTTP ? "http://" : "https://"; String addr = getAbilityMapKey(dev.getIpAddr(), dev.getIpPort()); @@ -176,6 +176,26 @@ public class HAOHanInterfaceTestCast extends InitTestEnvironment { Assert.assertEquals(ret.getSecondParam().getKernel(), "Linux"); Assert.assertEquals(ret.getSecondParam().getArch(), "x86_64"); 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); } } } diff --git a/src/test/java/com/dispose/test/service/DisposeTaskServiceTest.java b/src/test/java/com/dispose/test/service/DisposeTaskServiceTest.java new file mode 100644 index 00000000..ca99adf9 --- /dev/null +++ b/src/test/java/com/dispose/test/service/DisposeTaskServiceTest.java @@ -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 + */ +@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 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 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()); + } +} +