REM:
1. 增加加密解密算法单元测试
2. 增加测试用例提高代码覆盖率
3. 增加协议安全测试用例
This commit is contained in:
HuangXin 2020-09-15 14:31:35 +08:00
parent 20424c9fc9
commit 2726180356
8 changed files with 270 additions and 4 deletions

View File

@ -1,6 +1,7 @@
package com.dispose.test.common; package com.dispose.test.common;
import com.dispose.common.AuthConfigValue; import com.dispose.common.AuthConfigValue;
import com.dispose.common.DisposeConfigValue;
import com.dispose.common.ErrorCode; import com.dispose.common.ErrorCode;
import com.dispose.mapper.UserAccountMapper; import com.dispose.mapper.UserAccountMapper;
import com.dispose.pojo.entity.UserAccount; import com.dispose.pojo.entity.UserAccount;
@ -29,7 +30,7 @@ public class CommonEnvironment {
* The constant commonPriorityFilter. * The constant commonPriorityFilter.
*/ */
public static TestPriority[] commonPriorityFilter = new TestPriority[] {TestPriority.P1_PRIORITY, public static TestPriority[] commonPriorityFilter = new TestPriority[] {TestPriority.P1_PRIORITY,
TestPriority.P2_PRIORITY, TestPriority.P3_PRIORITY}; TestPriority.P2_PRIORITY, TestPriority.P3_PRIORITY, TestPriority.P4_PRIORITY};
/** /**
* The constant commonIdFilter. * The constant commonIdFilter.
@ -65,6 +66,9 @@ public class CommonEnvironment {
*/ */
@PostConstruct @PostConstruct
private void initGlobalValue() throws NoSuchAlgorithmException { private void initGlobalValue() throws NoSuchAlgorithmException {
DisposeConfigValue.ENABLE_UTEST_MOCK = true;
Optional<UserAccount> userAccount = userAccountMapper.selectAll().stream().findFirst(); Optional<UserAccount> userAccount = userAccountMapper.selectAll().stream().findFirst();
if (userAccount.isPresent()) { if (userAccount.isPresent()) {

View File

@ -10,17 +10,21 @@ import com.dispose.common.BaseEnum;
public enum TestPriority implements BaseEnum { public enum TestPriority implements BaseEnum {
/** /**
* P 1 priority test priority. * The P 1 priority.
*/ */
P1_PRIORITY(1, "P1 优先级"), P1_PRIORITY(1, "P1 优先级"),
/** /**
* P 2 priority test priority. * The P 2 priority.
*/ */
P2_PRIORITY(2, "P2 优先级"), P2_PRIORITY(2, "P2 优先级"),
/** /**
* P 3 priority test priority. * The P 3 priority.
*/ */
P3_PRIORITY(3, "P3 优先级"), P3_PRIORITY(3, "P3 优先级"),
/**
* P 4 priority test priority.
*/
P4_PRIORITY(4, "P4 优先级"),
; ;
/** /**

View File

@ -14,6 +14,7 @@ import org.junit.Before;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.ActiveProfiles;
import javax.annotation.PostConstruct;
import javax.annotation.Resource; import javax.annotation.Resource;
/** /**
@ -47,6 +48,14 @@ public class InitTestEnvironment {
@Resource @Resource
private UserAccountService userAccountService; private UserAccountService userAccountService;
/**
* Init global value.
*/
@PostConstruct
public void initGlobalValue() {
DisposeConfigValue.ENABLE_UTEST_MOCK = true;
}
/** /**
* Init virtual device. * Init virtual device.
*/ */

View File

@ -0,0 +1,80 @@
package com.dispose.test.dev.function;
import com.security.arithmetic.CryptoHelper;
import org.junit.Assert;
import org.junit.Test;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
/**
* The type Crypto helper test.
*
* @author <huangxin@cmhi.chinamoblie.com>
*/
public class CryptoHelperTest {
/**
* T 1 sha 256 test.
*
* @throws NoSuchAlgorithmException the no such algorithm exception
*/
@Test
public void t1_sha256Test() throws NoSuchAlgorithmException {
String srcTest = "hello world";
byte[] shaCode = CryptoHelper.sha256Encryption(srcTest);
String showText = String.format("%064x", new BigInteger(1, shaCode));
Assert.assertEquals(showText, "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9");
}
/**
* T 2 aes 128 test.
*
* @throws IllegalBlockSizeException the illegal block size exception
* @throws InvalidKeyException the invalid key exception
* @throws BadPaddingException the bad padding exception
* @throws NoSuchAlgorithmException the no such algorithm exception
* @throws NoSuchPaddingException the no such padding exception
*/
@Test
public void t2_aes128Test() throws IllegalBlockSizeException, InvalidKeyException, BadPaddingException,
NoSuchAlgorithmException, NoSuchPaddingException {
String srcTest = "hello word";
String key = "hkoUV5ZWh0q1jSxMnpjovVn19Qg99HY6DD40";
byte[] aesCode = CryptoHelper.aes128Encryption(srcTest.getBytes(StandardCharsets.UTF_8), key);
String showText = CryptoHelper.base64Encryption(aesCode);
Assert.assertEquals(showText, "jHCJYYPX6509Dn5vj9IzZA==");
byte[] aesDecode = CryptoHelper.aes128Decryption(aesCode, key);
Assert.assertEquals(new String(aesDecode), srcTest);
}
/**
* T 2 des test.
*
* @throws NoSuchPaddingException the no such padding exception
* @throws NoSuchAlgorithmException the no such algorithm exception
* @throws IllegalBlockSizeException the illegal block size exception
* @throws BadPaddingException the bad padding exception
* @throws InvalidKeyException the invalid key exception
*/
@Test
public void t2_desTest() throws NoSuchPaddingException, NoSuchAlgorithmException, IllegalBlockSizeException,
BadPaddingException, InvalidKeyException {
String srcTest = "hello word";
String key = "hkoUV5ZWh0q1jSxMnpjovVn19Qg99HY6DD40";
byte[] aesCode = CryptoHelper.desEncryption(srcTest.getBytes(StandardCharsets.UTF_8), key);
String showText = CryptoHelper.base64Encryption(aesCode);
Assert.assertEquals(showText, "jdlS5EvGYhnPlyUc1vYizg==");
byte[] aesDecode = CryptoHelper.desDecryption(aesCode, key);
Assert.assertEquals(new String(aesDecode), srcTest);
}
}

View File

@ -4,6 +4,8 @@ import com.dispose.mapper.DisposeDeviceMapper;
import com.dispose.mapper.DisposeTaskMapper; import com.dispose.mapper.DisposeTaskMapper;
import com.dispose.service.DisposeDeviceManagerService; import com.dispose.service.DisposeDeviceManagerService;
import com.dispose.test.common.QATestItem; import com.dispose.test.common.QATestItem;
import com.dispose.test.testcase.dev.v200.CodeCoverage;
import com.dispose.test.testcase.dev.v200.ProtocolSecurity;
import com.dispose.test.testcase.qa.v200.P1All; import com.dispose.test.testcase.qa.v200.P1All;
import com.dispose.test.testcase.qa.v200.P2DeviceAdd; import com.dispose.test.testcase.qa.v200.P2DeviceAdd;
import com.dispose.test.testcase.qa.v200.P2DeviceDel; import com.dispose.test.testcase.qa.v200.P2DeviceDel;
@ -74,6 +76,11 @@ public interface TestCaseRun {
Collections.addAll(tolTestCase, P2TaskStop.getTestCase()); Collections.addAll(tolTestCase, P2TaskStop.getTestCase());
Collections.addAll(tolTestCase, P2TaskList.getTestCase()); Collections.addAll(tolTestCase, P2TaskList.getTestCase());
Collections.addAll(tolTestCase, ProtocolSecurity.getTestCase());
Collections.addAll(tolTestCase, CodeCoverage.getTestCase());
return tolTestCase; return tolTestCase;
} }
} }

View File

@ -107,6 +107,7 @@ public class JsonTestCaseRun extends CommonRestfulJson implements TestCaseRun {
.filter(k -> k.getCaseJsonValue() != null && k.getCaseJsonValue().length() > 0) .filter(k -> k.getCaseJsonValue() != null && k.getCaseJsonValue().length() > 0)
.filter(k -> usedId.size() == 0 || usedId.stream().anyMatch(v -> Objects.equals(v, k.getId()))) .filter(k -> usedId.size() == 0 || usedId.stream().anyMatch(v -> Objects.equals(v, k.getId())))
.filter(k -> Arrays.stream(CommonEnvironment.commonPriorityFilter).anyMatch(v -> k.getPriority() == v)) .filter(k -> Arrays.stream(CommonEnvironment.commonPriorityFilter).anyMatch(v -> k.getPriority() == v))
//.filter(k -> k.getId() >= 20000)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }

View File

@ -0,0 +1,87 @@
package com.dispose.test.testcase.dev.v200;
import com.dispose.common.ErrorCode;
import com.dispose.common.ProtoCryptoType;
import com.dispose.common.SecurityConfigValue;
import com.dispose.pojo.dto.protocol.base.BaseRespStatus;
import com.dispose.pojo.dto.protocol.base.ProtocolRespDTO;
import com.dispose.pojo.dto.protocol.task.GetTaskRsp;
import com.dispose.pojo.dto.protocol.task.TaskStartMulRsp;
import com.dispose.test.common.QATestItem;
import com.dispose.test.common.TestPriority;
import com.dispose.test.common.VerifyProtoRespCallback;
import com.dispose.test.exec.TestCaseRun;
import org.junit.Assert;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* The type Code coverage.
*
* @author <huangxin@cmhi.chinamoblie.com>
*/
public class CodeCoverage {
/**
* The constant BASE_CODE_COVERAGE_ID.
*/
public static final int BASE_CODE_COVERAGE_ID = 20000;
/**
* The constant testItemArray.
*/
private static final QATestItem[] testItemArray = new QATestItem[]{
QATestItem.builder()
.id(BASE_CODE_COVERAGE_ID)
.name("IP地址不符合规范")
.priority(TestPriority.P4_PRIORITY)
.urlPath("/task/startMulIp")
.method(RequestMethod.POST)
.caseJsonValue("{\"ver\":3,\"cryptoType\":0,\"timeStamp\":1598597142580,\"msgContent\":{\"type\":0," +
"\"mulDisposeIp\":[\"192.168.50.\",\"192.168.50\"],\"disposeTime\":10," +
"\"flowDirection\":2,\"attackType\":[1,2,3,4,5]}}")
.rspClass(TaskStartMulRsp.class)
.rspCode(ErrorCode.ERR_PARAMEXCEPTION)
.autoLogin(true)
.verifyCallback((VerifyProtoRespCallback<TaskStartMulRsp>) CodeCoverage::verifyJsonExceptionResp)
.prepareCallback(c -> SecurityConfigValue.SECURITY_PROTOCOL_TYPE = ProtoCryptoType.CRYPTO_BASE64.getCode())
.build(),
QATestItem.builder()
.id(BASE_CODE_COVERAGE_ID + 1)
.name("分页大小不符合规范")
.priority(TestPriority.P4_PRIORITY)
.urlPath("/task/taskList")
.method(RequestMethod.POST)
.caseJsonValue("{\"ver\":3,\"cryptoType\":0,\"timeStamp\":1598596065234,\"msgContent\":{\"startPage\":1," +
"\"pageSize\":4}}")
.rspClass(GetTaskRsp.class)
.rspCode(ErrorCode.ERR_PARAMEXCEPTION)
.autoLogin(true)
.verifyCallback((VerifyProtoRespCallback<GetTaskRsp>) CodeCoverage::verifyJsonExceptionResp)
.prepareCallback(c -> SecurityConfigValue.SECURITY_PROTOCOL_TYPE = ProtoCryptoType.CRYPTO_AES256.getCode())
.build(),
};
/**
* Get test case qa test item [ ].
*
* @return the qa test item [ ]
*/
public static QATestItem[] getTestCase() {
return testItemArray;
}
/**
* Verify json exception resp.
*
* @param v the v
* @param e the e
* @param c the c
*/
private static void verifyJsonExceptionResp(ProtocolRespDTO<? extends BaseRespStatus> v, ErrorCode e,
TestCaseRun c) {
Assert.assertNotNull(v);
Assert.assertNotNull(v.getMsgContent());
Assert.assertEquals((long) v.getCode(), e.getHttpCode());
Assert.assertEquals((long) v.getMsgContent().getStatus(), e.getCode());
}
}

View File

@ -0,0 +1,74 @@
package com.dispose.test.testcase.dev.v200;
import com.dispose.common.ErrorCode;
import com.dispose.pojo.dto.protocol.auth.LoginRsp;
import com.dispose.test.common.QATestItem;
import com.dispose.test.common.TestPriority;
import com.dispose.test.common.VerifyProtoRespCallback;
import org.junit.Assert;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* The type Protocol security.
*
* @author <huangxin@cmhi.chinamoblie.com>
*/
public class ProtocolSecurity {
/**
* The constant BASE_PROTOCOL_SECURITY_ID.
*/
public static final int BASE_PROTOCOL_SECURITY_ID = 10000;
/**
* The constant testItemArray.
*/
private static final QATestItem[] testItemArray = new QATestItem[]{
QATestItem.builder()
.id(BASE_PROTOCOL_SECURITY_ID)
.name("BASE64正常编码")
.priority(TestPriority.P4_PRIORITY)
.urlPath("/auth/login")
.method(RequestMethod.POST)
.caseJsonValue("{\"ver\":3,\"cryptoType\":1,\"timeStamp\":1598580612302," +
"\"msgContent" +
"\":\"eyJwYXNzd29yZCI6ImMzODU1ZTZiNmJiMTIwNDUwZjE2MGJhOTExMzQ1MjI4NjhmODlkMzYwNjJmMjA2MWViZWVmZDgwODE3ZTFkNTgiLCJ1c2VyTmFtZSI6ImFkbWluIn0=\"}")
.rspClass(LoginRsp.class)
.rspCode(ErrorCode.ERR_OK)
.autoLogin(true)
.verifyCallback((VerifyProtoRespCallback<CodeCoverage>) (v, e, c) -> {
Assert.assertNotNull(v);
Assert.assertNotNull(v.getMsgContent());
Assert.assertEquals((long)v.getCode(), e.getHttpCode());
})
.build(),
QATestItem.builder()
.id(BASE_PROTOCOL_SECURITY_ID)
.name("BASE64异常编码")
.priority(TestPriority.P4_PRIORITY)
.urlPath("/auth/login")
.method(RequestMethod.POST)
.caseJsonValue("{\"ver\":3,\"cryptoType\":1,\"timeStamp\":1598580612302," +
"\"msgContent" +
"\":\"eyJwYXNzd29yZCI6ImMzODU1ZTZiNmJiMTIwwZjE2MGJhOTExMzQ1MjI4NjhmODlkMzYwNjJmMjA2MWWVmZDgwODE3ZTFkNTgiLCJ1c2VyTmFtZSFkbWluIn0=\"}")
.rspClass(LoginRsp.class)
.rspCode(ErrorCode.ERR_PARAMEXCEPTION)
.autoLogin(true)
.verifyCallback((VerifyProtoRespCallback<LoginRsp>) (v, e, c) -> {
Assert.assertNotNull(v);
Assert.assertNotNull(v.getMsgContent());
Assert.assertEquals((long)v.getCode(), e.getHttpCode());
Assert.assertEquals((long)v.getMsgContent().getStatus(), e.getCode());
})
.build(),
};
/**
* Get test case qa test item [ ].
*
* @return the qa test item [ ]
*/
public static QATestItem[] getTestCase() {
return testItemArray;
}
}