REM:
1. 更正处置任务参数校验
2. 更正单元测试校验参数
This commit is contained in:
HuangXin 2020-05-12 17:03:50 +08:00
parent c0175a1692
commit 34e9a5873e
8 changed files with 100 additions and 71 deletions

View File

@ -74,4 +74,18 @@ public enum IPAddrType {
return range.contains(addr);
}
public static boolean isIpAddress(String ipAddr) {
if (!ipAddr.contains(".") && !ipAddr.contains(":")) {
return false;
}
try {
IPAddressString str = new IPAddressString(ipAddr);
IPAddress addr = str.toAddress();
return true;
} catch (AddressStringException e) {
return false;
}
}
}

View File

@ -96,7 +96,7 @@ public class DisposeNodeInfoController {
IDArrayReq reqInfo = mr.getRequestObject(IDArrayReq.class);
LinkStatusListRsp linkStatusListRsp = new LinkStatusListRsp();
linkStatusListRsp.setItems(new ArrayList<LinkStatusRsp>());
linkStatusListRsp.setItems(new ArrayList<>());
if (reqInfo.getId().length == 0) {
List<DisposeDevice> devs = disposeNodeManager.getAllDisposeDevice();

View File

@ -38,6 +38,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
@ -93,7 +94,7 @@ public class DisposeTaskController {
@PostMapping("/start")
@ResponseBody
@ApiOperation("启动处置任务")
public ProtocolRespDTO taskStart(@RequestBody ProtocolReqDTO mr,
public ProtocolRespDTO taskStart(@RequestBody @Valid ProtocolReqDTO mr,
@RequestHeader HttpHeaders headers)
throws JsonProcessingException {
ErrorCode err = mr.verifyRequest(headers);
@ -103,6 +104,8 @@ public class DisposeTaskController {
}
StartTaskReq reqInfo = mr.getRequestObject(StartTaskReq.class);
StartTaskRsp rspInfo = new StartTaskRsp();
TaskInfoDetail task = TaskInfoDetail.builder()
@ -110,7 +113,7 @@ public class DisposeTaskController {
.accountId(userAccountService.getUserByToken(mr.getAuthToken()).getId())
.type(reqInfo.getType())
.disposeIp(reqInfo.getDisposeIp())
.planEndTime(String.valueOf(reqInfo.getDisposeTime()))
.planEndTime(String.valueOf(reqInfo.getDisposeTime() == null ? "" : reqInfo.getDisposeTime()))
.flowDirection(reqInfo.getFlowDirection() != null ? reqInfo.getFlowDirection() : FlowDirection.DIRECTION_TWOWAY.getCode())
.flowBandWidth(reqInfo.getFlowBandwidth() != null ? reqInfo.getFlowBandwidth() : 1024)
.attackType(Helper.attackArrayToString(reqInfo.getAttackType()))
@ -226,24 +229,29 @@ public class DisposeTaskController {
rspInfo.getItems().add(taskData);
} else {
ret.forEach(k -> {
ErrorCode retError = k.getFirstParam();
TaskInfoDetail taskInfo = k.getSecondParam();
taskData.setId(String.valueOf(taskInfo.getDeviceId()));
taskData.setTaskId(String.valueOf(taskInfo.getId()));
taskData.setType(taskInfo.getType());
taskData.setDisposeIp(taskInfo.getDisposeIp());
taskData.setLeftTime((Math.abs(Helper.getTimestampDiffNow(taskInfo.getBeginTime())
- Helper.getTimestampDiffNow(taskInfo.getPlanEndTime()))) / 60);
if(k.getFirstParam() == ErrorCode.ERR_OK) {
ErrorCode retError = k.getFirstParam();
TaskInfoDetail taskInfo = k.getSecondParam();
taskData.setId(String.valueOf(taskInfo.getDeviceId()));
taskData.setTaskId(String.valueOf(taskInfo.getId()));
taskData.setType(taskInfo.getType());
taskData.setDisposeIp(taskInfo.getDisposeIp());
taskData.setLeftTime((Math.abs(Helper.getTimestampDiffNow(taskInfo.getBeginTime())
- Helper.getTimestampDiffNow(taskInfo.getPlanEndTime()))) / 60);
taskData.setStatus(retError.getCode());
taskData.setMessage(retError.getMsg());
taskData.setStatus(retError.getCode());
taskData.setMessage(retError.getMsg());
} else {
taskData.setStatus(k.getFirstParam().getCode());
taskData.setMessage(k.getFirstParam().getMsg());
}
rspInfo.getItems().add(taskData);
});
}
});
return ProtocolRespDTO.result(err, rspInfo);
return ProtocolRespDTO.result(ErrorCode.ERR_OK, rspInfo);
}
/**

View File

@ -19,9 +19,7 @@ import lombok.NoArgsConstructor;
import java.util.List;
/**
* @author lsx
* @Desc
* @date
* The type Link status list rsp.
*/
@Data
@NoArgsConstructor
@ -30,13 +28,4 @@ public class LinkStatusListRsp {
* The Items.
*/
private List<LinkStatusRsp> items;
}
/**
* Revision history
* -------------------------------------------------------------------------
* <p>
* Date Author Note
* -------------------------------------------------------------------------
* lsx creat
*/
}

View File

@ -19,25 +19,14 @@ import lombok.NoArgsConstructor;
import java.util.List;
/**
* @author lsx
* @Desc
* @date
* The type Version list rsp.
*/
@Data
@NoArgsConstructor
public class VersionListRsp {
/**
* the items
* The Items.
*/
private List<VersionRsp> items;
}
/**
* Revision history
* -------------------------------------------------------------------------
* <p>
* Date Author Note
* -------------------------------------------------------------------------
* lsx creat
*/
}

View File

@ -3,6 +3,7 @@ package com.dispose.service.impl;
import com.dispose.common.DeviceCapacity;
import com.dispose.common.DisposeTaskStatus;
import com.dispose.common.ErrorCode;
import com.dispose.common.IPAddrType;
import com.dispose.dispose.DeviceRouter;
import com.dispose.dispose.DisposeEntryManager;
import com.dispose.manager.TaskCacheManager;
@ -129,6 +130,17 @@ public class TaskServiceImpl implements TaskService {
*/
@Override
public MReturnType<ErrorCode, Long> createTask(TaskInfoDetail task) {
if(task.getDisposeIp() == null || task.getDisposeIp().length() == 0
|| task.getPlanEndTime() == null || task.getPlanEndTime().length() == 0
|| task.getType() == null || !IPAddrType.isIpAddress(task.getDisposeIp())) {
log.error("Input params error: devId:{}, disposeIp:{}, type:{}",
task.getDeviceId(), task.getDisposeIp(), task.getType());
return MReturnType.<ErrorCode, Long>builder()
.firstParam(ErrorCode.ERR_PARAMEXCEPTION)
.secondParam(-1L)
.build();
}
DisposeDevice disposeDevice;
Long taskId = task.getDeviceId();
@ -434,9 +446,15 @@ public class TaskServiceImpl implements TaskService {
public List<MReturnType<ErrorCode, TaskInfoDetail>> stopTaskByDisposeIp(Long devId, int type, String ipAddr) {
List<MReturnType<ErrorCode, TaskInfoDetail>> retList = new ArrayList<>();
if(devId == null || devId == -1L || ipAddr == null || ipAddr.length() == 0 || !IPAddrType.isIpAddress(ipAddr)) {
retList.add(new MReturnType<>(ErrorCode.ERR_PARAMEXCEPTION, null));
log.error("Input params error: devId:{}, disposeIp:{}, type:{}", devId, ipAddr, type);
return retList;
}
// 根据处置IP拿出所有正在处置的任务
List<TaskInfoDetail> taskList = taskCacheManager.getAllTask().stream()
.filter(v -> ((devId == null || devId == -1L) || Objects.equals(v.getDeviceId(), devId))
.filter(v -> (Objects.equals(v.getDeviceId(), devId))
&& (Objects.equals(v.getType(), type) || type == -1)
&& Objects.equals(v.getDisposeIp(), ipAddr)
&& taskIsRunning(v))

View File

@ -100,7 +100,7 @@ public class TaskControllerQATest extends InitTestEnvironment {
.header("Authorization", "Bearer " + getLogToken())
.content(reqDataNoIp))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(521))
.andExpect(jsonPath("$.code").value(ErrorCode.ERR_OK.getHttpCode()))
.andReturn()
.getResponse()
.getContentAsString();
@ -129,7 +129,7 @@ public class TaskControllerQATest extends InitTestEnvironment {
.header("Authorization", "Bearer " + getLogToken())
.content(reqDataNullIp))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(521))
.andExpect(jsonPath("$.code").value(ErrorCode.ERR_OK.getHttpCode()))
.andReturn()
.getResponse()
.getContentAsString();
@ -172,7 +172,7 @@ public class TaskControllerQATest extends InitTestEnvironment {
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()));
Assert.assertEquals(Long.valueOf(startTaskRsp.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode()));
}
/**
@ -247,7 +247,7 @@ public class TaskControllerQATest extends InitTestEnvironment {
.header("Authorization", "Bearer " + getLogToken())
.content(reqDataNullType))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(521))
.andExpect(jsonPath("$.code").value(ErrorCode.ERR_OK.getHttpCode()))
.andReturn()
.getResponse()
.getContentAsString();
@ -268,7 +268,7 @@ public class TaskControllerQATest extends InitTestEnvironment {
public void a8_startTaskNoDisposeTimeExceptionTest() throws Exception {
//no disposeTime
String reqDataNoDisposeTime = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\":\"{\\\"id\\\":-1," +
"\\\"type\\\":null, \\\"disposeIp\\\":\\\"192.168.4.2\\\", \\\"disposeTime\\\":}\"}";
"\\\"type\\\":0, \\\"disposeIp\\\":\\\"192.168.4.2\\\", \\\"disposeTime\\\":}\"}";
String taskStart = mockMvc.perform(MockMvcRequestBuilders
.post("/task/start")
@ -305,7 +305,7 @@ public class TaskControllerQATest extends InitTestEnvironment {
.header("Authorization", "Bearer " + getLogToken())
.content(reqDataNullDisposeTime))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(521))
.andExpect(jsonPath("$.code").value(ErrorCode.ERR_OK.getHttpCode()))
.andReturn()
.getResponse()
.getContentAsString();
@ -334,7 +334,7 @@ public class TaskControllerQATest extends InitTestEnvironment {
.header("Authorization", "Bearer " + getLogToken())
.content(reqDataStringDisposeTime))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(521))
.andExpect(jsonPath("$.code").value(ErrorCode.ERR_OK.getHttpCode()))
.andReturn()
.getResponse()
.getContentAsString();
@ -487,16 +487,18 @@ public class TaskControllerQATest extends InitTestEnvironment {
.header("Authorization", "Bearer " + getLogToken())
.content(reqData))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(521))
.andExpect(jsonPath("$.code").value(ErrorCode.ERR_OK.getHttpCode()))
.andReturn()
.getResponse()
.getContentAsString();
ReturnStatus returnStatus = objectMapper.readValue(verifyResp(taskStopByIpNoDisposeIp), ReturnStatus.class);
TaskInfoRsp rspStatus = objectMapper.readValue(verifyResp(taskStopByIpNoDisposeIp), TaskInfoRsp.class);
Assert.assertNotNull(returnStatus.getStatus());
Assert.assertNotNull(returnStatus.getMessage());
Assert.assertEquals(Long.valueOf(returnStatus.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode()));
rspStatus.getItems().forEach(v -> {
Assert.assertNotNull(v.getStatus());
Assert.assertNotNull(v.getMessage());
Assert.assertEquals(Long.valueOf(v.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode()));
});
}
/**
@ -515,16 +517,18 @@ public class TaskControllerQATest extends InitTestEnvironment {
.header("Authorization", "Bearer " + getLogToken())
.content(reqData))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(521))
.andExpect(jsonPath("$.code").value(ErrorCode.ERR_OK.getHttpCode()))
.andReturn()
.getResponse()
.getContentAsString();
ReturnStatus returnStatus = objectMapper.readValue(verifyResp(taskStopByIpNullDisposeIp), ReturnStatus.class);
TaskInfoRsp rspStatus = objectMapper.readValue(verifyResp(taskStopByIpNullDisposeIp), TaskInfoRsp.class);
Assert.assertNotNull(returnStatus.getStatus());
Assert.assertNotNull(returnStatus.getMessage());
Assert.assertEquals(Long.valueOf(returnStatus.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode()));
rspStatus.getItems().forEach(v -> {
Assert.assertNotNull(v.getStatus());
Assert.assertNotNull(v.getMessage());
Assert.assertEquals(Long.valueOf(v.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode()));
});
}
/**
@ -543,16 +547,18 @@ public class TaskControllerQATest extends InitTestEnvironment {
.header("Authorization", "Bearer " + getLogToken())
.content(reqData))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(521))
.andExpect(jsonPath("$.code").value(ErrorCode.ERR_OK.getHttpCode()))
.andReturn()
.getResponse()
.getContentAsString();
ReturnStatus returnStatus = objectMapper.readValue(verifyResp(taskStopByIpErrorDisposeIp), ReturnStatus.class);
TaskInfoRsp rspStatus = objectMapper.readValue(verifyResp(taskStopByIpErrorDisposeIp), TaskInfoRsp.class);
Assert.assertNotNull(returnStatus.getStatus());
Assert.assertNotNull(returnStatus.getMessage());
Assert.assertEquals(Long.valueOf(returnStatus.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode()));
rspStatus.getItems().forEach(v -> {
Assert.assertNotNull(v.getStatus());
Assert.assertNotNull(v.getMessage());
Assert.assertEquals(Long.valueOf(v.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode()));
});
}
/**
@ -571,16 +577,18 @@ public class TaskControllerQATest extends InitTestEnvironment {
.header("Authorization", "Bearer " + getLogToken())
.content(reqData))
.andDo(print()).andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(521))
.andExpect(jsonPath("$.code").value(ErrorCode.ERR_OK.getHttpCode()))
.andReturn()
.getResponse()
.getContentAsString();
ReturnStatus returnStatus = objectMapper.readValue(verifyResp(taskStopByIpIntegerDisposeIp), ReturnStatus.class);
TaskInfoRsp rspStatus = objectMapper.readValue(verifyResp(taskStopByIpIntegerDisposeIp), TaskInfoRsp.class);
Assert.assertNotNull(returnStatus.getStatus());
Assert.assertNotNull(returnStatus.getMessage());
Assert.assertEquals(Long.valueOf(returnStatus.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode()));
rspStatus.getItems().forEach(v -> {
Assert.assertNotNull(v.getStatus());
Assert.assertNotNull(v.getMessage());
Assert.assertEquals(Long.valueOf(v.getStatus()), Long.valueOf(ErrorCode.ERR_PARAMEXCEPTION.getCode()));
});
}
/**
@ -645,7 +653,7 @@ public class TaskControllerQATest extends InitTestEnvironment {
@Test
public void d1_NormalStopTaskByNodeTest() throws Exception {
String reqData = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\": \"{\\\"items\\\":" +
"[{\\\"id\\\":\\\"472\\\",\\\"type\\\":0}]}\"}";
"[{\\\"id\\\":\\\"1\\\",\\\"type\\\":0}]}\"}";
String taskStopByNode = mockMvc.perform(MockMvcRequestBuilders
.post("/task/stop_node")
@ -695,7 +703,7 @@ public class TaskControllerQATest extends InitTestEnvironment {
@Test
public void d2_NormalStopTaskByNodesTest() throws Exception {
String reqData = "{\"ver\":2,\"cryptoType\":0,\"timeStamp\":1587604296988,\"msgContent\": \"{\\\"items\\\":" +
"[{\\\"id\\\":\\\"472\\\",\\\"type\\\":0}, {\\\"id\\\":\\\"471\\\",\\\"type\\\":0}]}\"}";
"[{\\\"id\\\":\\\"1\\\",\\\"type\\\":0}, {\\\"id\\\":\\\"471\\\",\\\"type\\\":0}]}\"}";
String taskStopByNode = mockMvc.perform(MockMvcRequestBuilders
.post("/task/stop_node")

View File

@ -3,6 +3,7 @@ package com.dispose.controller;
import com.dispose.Global.InitTestEnvironment;
import com.dispose.common.ConstValue;
import com.dispose.common.DeviceCapacity;
import com.dispose.common.IPAddrType;
import com.dispose.manager.TaskCacheManager;
import com.dispose.mapper.DisposeDeviceMapper;
import com.dispose.pojo.dto.ProtocolReqDTO;
@ -122,6 +123,8 @@ public class TaskControllerTest extends InitTestEnvironment {
.disposeTime(60)
.build();
Assert.assertTrue(IPAddrType.isIpAddress("1.1.1.1"));
Long reqTimeStamp = System.currentTimeMillis();
ProtocolReqDTO reqInfo = new ProtocolReqDTO();
reqInfo.setVer(ConstValue.Protocol.VERSION);