diff --git a/src/test/java/com/dispose/test/common/CommonEnvironment.java b/src/test/java/com/dispose/test/common/CommonEnvironment.java index 420953d6..905a2c4b 100644 --- a/src/test/java/com/dispose/test/common/CommonEnvironment.java +++ b/src/test/java/com/dispose/test/common/CommonEnvironment.java @@ -1,6 +1,7 @@ package com.dispose.test.common; import com.dispose.common.AuthConfigValue; +import com.dispose.common.DisposeConfigValue; import com.dispose.common.ErrorCode; import com.dispose.mapper.UserAccountMapper; import com.dispose.pojo.entity.UserAccount; @@ -29,7 +30,7 @@ public class CommonEnvironment { * The constant commonPriorityFilter. */ 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. @@ -65,6 +66,9 @@ public class CommonEnvironment { */ @PostConstruct private void initGlobalValue() throws NoSuchAlgorithmException { + + DisposeConfigValue.ENABLE_UTEST_MOCK = true; + Optional userAccount = userAccountMapper.selectAll().stream().findFirst(); if (userAccount.isPresent()) { diff --git a/src/test/java/com/dispose/test/common/TestPriority.java b/src/test/java/com/dispose/test/common/TestPriority.java index 92a59c99..954f5b0a 100644 --- a/src/test/java/com/dispose/test/common/TestPriority.java +++ b/src/test/java/com/dispose/test/common/TestPriority.java @@ -10,17 +10,21 @@ import com.dispose.common.BaseEnum; public enum TestPriority implements BaseEnum { /** - * P 1 priority test priority. + * The P 1 priority. */ P1_PRIORITY(1, "P1 优先级"), /** - * P 2 priority test priority. + * The P 2 priority. */ P2_PRIORITY(2, "P2 优先级"), /** - * P 3 priority test priority. + * The P 3 priority. */ P3_PRIORITY(3, "P3 优先级"), + /** + * P 4 priority test priority. + */ + P4_PRIORITY(4, "P4 优先级"), ; /** diff --git a/src/test/java/com/dispose/test/dev/Global/InitTestEnvironment.java b/src/test/java/com/dispose/test/dev/Global/InitTestEnvironment.java index f2cf7304..f95b6daf 100644 --- a/src/test/java/com/dispose/test/dev/Global/InitTestEnvironment.java +++ b/src/test/java/com/dispose/test/dev/Global/InitTestEnvironment.java @@ -14,6 +14,7 @@ import org.junit.Before; import org.junit.BeforeClass; import org.springframework.test.context.ActiveProfiles; +import javax.annotation.PostConstruct; import javax.annotation.Resource; /** @@ -47,6 +48,14 @@ public class InitTestEnvironment { @Resource private UserAccountService userAccountService; + /** + * Init global value. + */ + @PostConstruct + public void initGlobalValue() { + DisposeConfigValue.ENABLE_UTEST_MOCK = true; + } + /** * Init virtual device. */ diff --git a/src/test/java/com/dispose/test/dev/function/CryptoHelperTest.java b/src/test/java/com/dispose/test/dev/function/CryptoHelperTest.java new file mode 100644 index 00000000..09386918 --- /dev/null +++ b/src/test/java/com/dispose/test/dev/function/CryptoHelperTest.java @@ -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 + */ +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); + } +} diff --git a/src/test/java/com/dispose/test/exec/TestCaseRun.java b/src/test/java/com/dispose/test/exec/TestCaseRun.java index 4043bc5d..0c002e68 100644 --- a/src/test/java/com/dispose/test/exec/TestCaseRun.java +++ b/src/test/java/com/dispose/test/exec/TestCaseRun.java @@ -4,6 +4,8 @@ import com.dispose.mapper.DisposeDeviceMapper; import com.dispose.mapper.DisposeTaskMapper; import com.dispose.service.DisposeDeviceManagerService; 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.P2DeviceAdd; import com.dispose.test.testcase.qa.v200.P2DeviceDel; @@ -74,6 +76,11 @@ public interface TestCaseRun { Collections.addAll(tolTestCase, P2TaskStop.getTestCase()); Collections.addAll(tolTestCase, P2TaskList.getTestCase()); + + + Collections.addAll(tolTestCase, ProtocolSecurity.getTestCase()); + Collections.addAll(tolTestCase, CodeCoverage.getTestCase()); + return tolTestCase; } } diff --git a/src/test/java/com/dispose/test/exec/impl/JsonTestCaseRun.java b/src/test/java/com/dispose/test/exec/impl/JsonTestCaseRun.java index fb91f015..f87b8c06 100644 --- a/src/test/java/com/dispose/test/exec/impl/JsonTestCaseRun.java +++ b/src/test/java/com/dispose/test/exec/impl/JsonTestCaseRun.java @@ -107,6 +107,7 @@ public class JsonTestCaseRun extends CommonRestfulJson implements TestCaseRun { .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 -> Arrays.stream(CommonEnvironment.commonPriorityFilter).anyMatch(v -> k.getPriority() == v)) + //.filter(k -> k.getId() >= 20000) .collect(Collectors.toList()); } diff --git a/src/test/java/com/dispose/test/testcase/dev/v200/CodeCoverage.java b/src/test/java/com/dispose/test/testcase/dev/v200/CodeCoverage.java new file mode 100644 index 00000000..fccdf6ab --- /dev/null +++ b/src/test/java/com/dispose/test/testcase/dev/v200/CodeCoverage.java @@ -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 + */ +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) 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) 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 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()); + } +} diff --git a/src/test/java/com/dispose/test/testcase/dev/v200/ProtocolSecurity.java b/src/test/java/com/dispose/test/testcase/dev/v200/ProtocolSecurity.java new file mode 100644 index 00000000..d5fbc8ca --- /dev/null +++ b/src/test/java/com/dispose/test/testcase/dev/v200/ProtocolSecurity.java @@ -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 + */ +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) (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) (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; + } +}