diff --git a/src/main/java/com/dispose/controller/DisposeTaskController.java b/src/main/java/com/dispose/controller/DisposeTaskController.java index 20263282..133a191e 100644 --- a/src/main/java/com/dispose/controller/DisposeTaskController.java +++ b/src/main/java/com/dispose/controller/DisposeTaskController.java @@ -103,6 +103,20 @@ public class DisposeTaskController { } StartTaskReq reqInfo = mr.getRequestObject(StartTaskReq.class); + + if(reqInfo.getDisposeIp().length() == 0 || reqInfo.getDisposeIp().equals("null")){ + return ProtocolRespDTO.result(ErrorCode.ERR_PARAMEXCEPTION); + } + + if(reqInfo.getId() == null || reqInfo.getType() == null || String.valueOf(reqInfo.getType()).length() == 0){ + return ProtocolRespDTO.result(ErrorCode.ERR_PARAMEXCEPTION); + } + + if(reqInfo.getDisposeTime() == null || reqInfo.getDisposeTime() < 0 || String.valueOf(reqInfo.getDisposeTime()).length() == 0) { + return ProtocolRespDTO.result(ErrorCode.ERR_PARAMEXCEPTION); + } + + StartTaskRsp rspInfo = new StartTaskRsp(); TaskInfoDetail task = TaskInfoDetail.builder() @@ -112,7 +126,7 @@ public class DisposeTaskController { .disposeIp(reqInfo.getDisposeIp()) .planEndTime(String.valueOf(reqInfo.getDisposeTime())) .flowDirection(reqInfo.getFlowDirection() != null ? reqInfo.getFlowDirection() : FlowDirection.DIRECTION_TWOWAY.getCode()) - .flowBandWidth(reqInfo.getFlowBandWidth() != null ? reqInfo.getFlowBandWidth() : 1024) + .flowBandWidth(reqInfo.getFlowBandwidth() != null ? reqInfo.getFlowBandwidth() : 1024) .attackType(Helper.attackArrayToString(reqInfo.getAttackType())) .build(); diff --git a/src/main/java/com/dispose/pojo/vo/task/StartTaskReq.java b/src/main/java/com/dispose/pojo/vo/task/StartTaskReq.java index 8d1a636e..78d64f72 100644 --- a/src/main/java/com/dispose/pojo/vo/task/StartTaskReq.java +++ b/src/main/java/com/dispose/pojo/vo/task/StartTaskReq.java @@ -23,7 +23,7 @@ public class StartTaskReq { /** * The Type. */ - private int type; + private Integer type; /** * The Dispose ip. */ @@ -31,7 +31,7 @@ public class StartTaskReq { /** * The Dispose time. */ - private int disposeTime; + private Integer disposeTime; /** * The Flow direction. */ @@ -46,5 +46,5 @@ public class StartTaskReq { * The Flow band width. */ @Nullable - private Integer flowBandWidth; + private Integer flowBandwidth; } diff --git a/src/test/java/com/dispose/controller/TaskControllerExceptionSmokeTest.java b/src/test/java/com/dispose/controller/TaskControllerExceptionSmokeTest.java new file mode 100644 index 00000000..daf09b0c --- /dev/null +++ b/src/test/java/com/dispose/controller/TaskControllerExceptionSmokeTest.java @@ -0,0 +1,352 @@ +package com.dispose.controller; + +import com.dispose.Global.InitTestEnvironment; +import com.dispose.common.ErrorCode; +import com.dispose.pojo.vo.auth.LoginRsp; +import com.dispose.pojo.vo.common.TaskInfoData; +import com.dispose.pojo.vo.task.StartTaskRsp; +import com.dispose.pojo.vo.task.TaskInfoRsp; +import com.fasterxml.jackson.databind.ObjectMapper; +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.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import javax.annotation.Resource; + +import java.util.List; + +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; + +@AutoConfigureMockMvc +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) +@FixMethodOrder(MethodSorters.NAME_ASCENDING) +@Slf4j +public class TaskControllerExceptionSmokeTest extends InitTestEnvironment { + /** + * The Mock mvc. + */ + @Resource + private MockMvc mockMvc; + /** + * The Object mapper. + */ + @Resource + private ObjectMapper objectMapper; + + @Test + public void t1_NormalTaskTest() throws Exception { + String reqData = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988," + + "\"msgContent\":\"{\\\"id\\\":-1," + + "\\\"type\\\":0, " + + "\\\"disposeIp\\\":\\\"192.168.5.5\\\", " + + "\\\"disposeTime\\\":120}\"}"; + + String taskStart = mockMvc.perform(MockMvcRequestBuilders + .post("/task/start") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(reqData)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(200)) + .andReturn() + .getResponse() + .getContentAsString(); + + StartTaskRsp startTaskRsp = objectMapper.readValue(verifyResp(taskStart), StartTaskRsp.class); + + Assert.assertNotNull(startTaskRsp); + Assert.assertNotNull(String.valueOf(startTaskRsp.getExpireTime())); + Assert.assertNotNull(startTaskRsp.getId()); + Assert.assertNotNull(startTaskRsp.getTaskId()); + Assert.assertNotNull(startTaskRsp.getStatus()); + Assert.assertNotNull(startTaskRsp.getMessage()); + + if(startTaskRsp.getStatus() == 0){ + Assert.assertEquals(Long.valueOf(startTaskRsp.getStatus()), Long.valueOf(ErrorCode.ERR_OK.getCode())); + }else if(startTaskRsp.getStatus() == 26){ + Assert.assertEquals(Long.valueOf(startTaskRsp.getStatus()), Long.valueOf(ErrorCode.ERR_TASKRUNNING.getCode())); + } + + } + + + + @Test + public void t2_NormalContainsNonEmptyTaskTest() throws Exception { + String reqData = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\":\"{\\\"id\\\":-1," + + "\\\"type\\\":0, \\\"disposeIp\\\":\\\"192.168.5.4\\\", " + + "\\\"disposeTime\\\":120, \\\"flowDirection\\\":2, \\\"attackType\\\":[0], " + + "\\\"flowBandwidth\\\":1024}\"}"; + + String taskStart = mockMvc.perform(MockMvcRequestBuilders + .post("/task/start") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(reqData)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(200)) + .andReturn() + .getResponse() + .getContentAsString(); + + StartTaskRsp startTaskRsp = objectMapper.readValue(verifyResp(taskStart), StartTaskRsp.class); + + Assert.assertNotNull(startTaskRsp); + Assert.assertNotNull(String.valueOf(startTaskRsp.getExpireTime())); + Assert.assertNotNull(startTaskRsp.getId()); + Assert.assertNotNull(startTaskRsp.getTaskId()); + Assert.assertNotNull(startTaskRsp.getStatus()); + Assert.assertNotNull(startTaskRsp.getMessage()); + + if(startTaskRsp.getStatus() == 0){ + Assert.assertEquals(Long.valueOf(startTaskRsp.getStatus()), Long.valueOf(ErrorCode.ERR_OK.getCode())); + }else if(startTaskRsp.getStatus() == 26){ + Assert.assertEquals(Long.valueOf(startTaskRsp.getStatus()), Long.valueOf(ErrorCode.ERR_TASKRUNNING.getCode())); + } + } + + @Test + public void t3_startTaskNoDisPoseIpExceptionTest() throws Exception { + String reqDataNoIp = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\":\"{\\\"id\\\":-1," + + "\\\"type\\\":0, \\\"disposeIp\\\":\\\"\\\", \\\"disposeTime\\\":120}\"}"; + + String taskStart = mockMvc.perform(MockMvcRequestBuilders + .post("/task/start") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(reqDataNoIp)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(521)) + .andReturn() + .getResponse() + .getContentAsString(); + + StartTaskRsp startTaskRsp = objectMapper.readValue(verifyResp(taskStart), StartTaskRsp.class); + + Assert.assertNotNull(startTaskRsp); + Assert.assertNotNull(startTaskRsp.getStatus()); + Assert.assertNotNull(startTaskRsp.getMessage()); + Assert.assertEquals(Long.valueOf(startTaskRsp.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode())); + } + + + @Test + public void t4_startTaskNullDisposeIpExceptionTest() throws Exception { + //disposeIp null + String reqDataNullIp = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\":\"{\\\"id\\\":-1," + + "\\\"type\\\":0, \\\"disposeIp\\\":\\\"null\\\", \\\"disposeTime\\\":120}\"}"; + + String taskStart = mockMvc.perform(MockMvcRequestBuilders + .post("/task/start") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(reqDataNullIp)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(521)) + .andReturn() + .getResponse() + .getContentAsString(); + + StartTaskRsp startTaskRsp = objectMapper.readValue(verifyResp(taskStart), StartTaskRsp.class); + + Assert.assertNotNull(startTaskRsp); + Assert.assertNotNull(startTaskRsp.getStatus()); + Assert.assertNotNull(startTaskRsp.getMessage()); + Assert.assertEquals(Long.valueOf(startTaskRsp.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode())); + } + + + @Test + public void t5_startTaskIntegerDisposeIpExceptionTest() throws Exception { + String reqDataIntegerIp = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\":\"{\\\"id\\\":-1," + + "\\\"type\\\":0, \\\"disposeIp\\\":192345, \\\"disposeTime\\\":120}\"}"; + + String taskStart = mockMvc.perform(MockMvcRequestBuilders + .post("/task/start") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(reqDataIntegerIp)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(200)) + .andReturn() + .getResponse() + .getContentAsString(); + + StartTaskRsp startTaskRsp = objectMapper.readValue(verifyResp(taskStart), StartTaskRsp.class); + + Assert.assertNotNull(startTaskRsp); + Assert.assertNotNull(String.valueOf(startTaskRsp.getExpireTime())); + Assert.assertNotNull(startTaskRsp.getId()); + Assert.assertEquals(startTaskRsp.getId(), "-1"); + Assert.assertNotNull(startTaskRsp.getTaskId()); + Assert.assertEquals(startTaskRsp.getTaskId(), "-1"); + Assert.assertNotNull(startTaskRsp.getStatus()); + Assert.assertNotNull(startTaskRsp.getMessage()); + Assert.assertEquals(Long.valueOf(startTaskRsp.getStatus()), Long.valueOf(ErrorCode.ERR_NOSUCHDEVICE.getCode())); + } + + @Test + public void t6_startTaskNullIdExceptionTest() throws Exception { + //Id null + String reqDataNullId = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\":\"{\\\"id\\\":null," + + "\\\"type\\\":0, \\\"disposeIp\\\":\\\"192.168.4.2\\\", \\\"disposeTime\\\":120}\"}"; + + String taskStart = mockMvc.perform(MockMvcRequestBuilders + .post("/task/start") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(reqDataNullId)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(521)) + .andReturn() + .getResponse() + .getContentAsString(); + + StartTaskRsp startTaskRsp = objectMapper.readValue(verifyResp(taskStart), StartTaskRsp.class); + + Assert.assertNotNull(startTaskRsp); + Assert.assertNotNull(startTaskRsp.getStatus()); + Assert.assertNotNull(startTaskRsp.getMessage()); + Assert.assertEquals(Long.valueOf(startTaskRsp.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode())); + } + + @Test + public void t7_startTaskNoTypeExceptionTest() throws Exception { + //no type + String reqDataNoType = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\":\"{\\\"id\\\":-1," + + "\\\"type\\\":, \\\"disposeIp\\\":\\\"192.168.4.2\\\", \\\"disposeTime\\\":120}\"}"; + + String taskStart = mockMvc.perform(MockMvcRequestBuilders + .post("/task/start") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(reqDataNoType)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(521)) + .andReturn() + .getResponse() + .getContentAsString(); + + StartTaskRsp startTaskRsp = objectMapper.readValue(verifyResp(taskStart), StartTaskRsp.class); + + Assert.assertNotNull(startTaskRsp); + Assert.assertNotNull(startTaskRsp.getStatus()); + Assert.assertNotNull(startTaskRsp.getMessage()); + Assert.assertEquals(Long.valueOf(startTaskRsp.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode())); + } + + + @Test + public void t8_startTaskNullTypeExceptionTest() throws Exception { //null type + String reqDataNullType = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\":\"{\\\"id\\\":-1," + + "\\\"type\\\":null, \\\"disposeIp\\\":\\\"192.168.4.2\\\", \\\"disposeTime\\\":120}\"}"; + + String taskStart = mockMvc.perform(MockMvcRequestBuilders + .post("/task/start") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(reqDataNullType)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(521)) + .andReturn() + .getResponse() + .getContentAsString(); + + StartTaskRsp startTaskRsp = objectMapper.readValue(verifyResp(taskStart), StartTaskRsp.class); + + Assert.assertNotNull(startTaskRsp); + Assert.assertNotNull(startTaskRsp.getStatus()); + Assert.assertNotNull(startTaskRsp.getMessage()); + Assert.assertEquals(Long.valueOf(startTaskRsp.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode())); + } + + @Test + public void t9_startTaskNoDisposeTimeExceptionTest() throws Exception { + //no disposeTime + String reqDataNoDisposeTime = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\":\"{\\\"id\\\":-1," + + "\\\"type\\\":null, \\\"disposeIp\\\":\\\"192.168.4.2\\\", \\\"disposeTime\\\":}\"}"; + + String taskStart = mockMvc.perform(MockMvcRequestBuilders + .post("/task/start") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(reqDataNoDisposeTime)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(521)) + .andReturn() + .getResponse() + .getContentAsString(); + + StartTaskRsp startTaskRsp = objectMapper.readValue(verifyResp(taskStart), StartTaskRsp.class); + + Assert.assertNotNull(startTaskRsp); + Assert.assertNotNull(startTaskRsp.getStatus()); + Assert.assertNotNull(startTaskRsp.getMessage()); + Assert.assertEquals(Long.valueOf(startTaskRsp.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode())); + } + + + @Test + public void t10_startTaskNullDisposeTimeExceptionTest() throws Exception { + //null disposeTime + String reqDataNullDisposeTime = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\":\"{\\\"id\\\":-1," + + "\\\"type\\\":0, \\\"disposeIp\\\":\\\"192.168.4.2\\\", \\\"disposeTime\\\":null}\"}"; + + String taskStart = mockMvc.perform(MockMvcRequestBuilders + .post("/task/start") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(reqDataNullDisposeTime)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(521)) + .andReturn() + .getResponse() + .getContentAsString(); + + StartTaskRsp startTaskRsp = objectMapper.readValue(verifyResp(taskStart), StartTaskRsp.class); + + Assert.assertNotNull(startTaskRsp); + Assert.assertNotNull(startTaskRsp.getStatus()); + Assert.assertNotNull(startTaskRsp.getMessage()); + Assert.assertEquals(Long.valueOf(startTaskRsp.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode())); + } + + @Test + public void t11_startTaskStringDisposeTimeExceptionTest() throws Exception { + //string disposeTime + String reqDataStringDisposeTime = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\":\"{\\\"id\\\":-1," + + "\\\"type\\\":null, \\\"disposeIp\\\":\\\"192.168.4.2\\\", \\\"disposeTime\\\":\\\"120\\\"}\"}"; + + String taskStart = mockMvc.perform(MockMvcRequestBuilders + .post("/task/start") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Bearer " + getLogToken()) + .content(reqDataStringDisposeTime)) + .andDo(print()).andExpect(status().isOk()) + .andExpect(jsonPath("$.code").value(521)) + .andReturn() + .getResponse() + .getContentAsString(); + + StartTaskRsp startTaskRsp = objectMapper.readValue(verifyResp(taskStart), StartTaskRsp.class); + + Assert.assertNotNull(startTaskRsp); + Assert.assertNotNull(startTaskRsp.getStatus()); + Assert.assertNotNull(startTaskRsp.getMessage()); + Assert.assertEquals(Long.valueOf(startTaskRsp.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode())); + } +}