diff --git a/.gitignore b/.gitignore index e240a93..f1869e1 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ src/test/java/com/sunyard/sydapi/sample/*.* ~$* /pack/ /.logs/ +/bin/ diff --git a/src/test/java/cmbpoc/JmeterTest.java b/src/test/java/cmbpoc/JmeterTest.java index 224e19e..8a677c8 100644 --- a/src/test/java/cmbpoc/JmeterTest.java +++ b/src/test/java/cmbpoc/JmeterTest.java @@ -13,6 +13,7 @@ public class JmeterTest extends AbstractJavaSamplerClient { private byte[] publicKey; private byte[] plainData; + private int sleep = 1; @Override public Arguments getDefaultParameters() { @@ -29,6 +30,19 @@ public class JmeterTest extends AbstractJavaSamplerClient { String plainText = context.getParameter("plainText"); publicKey = Base64.getDecoder().decode(pubBase64); plainData = plainText.getBytes(StandardCharsets.UTF_8); + + // 校准测试 + sleep = context.getIntParameter("sleep", 1); + + // 线程开始时准备解密、验签数据 + String function = context.getParameter("function"); + switch (function) { + case "SYD_SM2_Sign": + break; + default: + break; + } + } @Override @@ -42,6 +56,9 @@ public class JmeterTest extends AbstractJavaSamplerClient { switch (function) { case "SYD_SM2_Sign": break; + case "sleep": + Thread.sleep(1); + break; default: break; } diff --git a/src/test/java/cmbpoc/SignatureVerifyTest.java b/src/test/java/cmbpoc/SignatureVerifyTest.java new file mode 100644 index 0000000..8bf43cd --- /dev/null +++ b/src/test/java/cmbpoc/SignatureVerifyTest.java @@ -0,0 +1,84 @@ +package cmbpoc; + +import com.sunyard.RetWrap; +import com.sunyard.SydApi; +import com.sunyard.proto.Util; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import racal.sunyard.main.SydApi4j; + +public class SignatureVerifyTest { + + private SydApi4j api; + private byte[] orgData; + private String SM2dn; + + @Before + public void setUp() { + System.setProperty("com.sunyard.sydapi4j.debug", "true"); + this.api = (SydApi4j) new SydApi4j().connect("192.168.0.100", 8889, null, 1000); + this.orgData = "Hello, World! 测试数据".getBytes(); + this.SM2dn = "C=CN,ST=北京,L=北京,O=信雅达,CN=172.1.40.188"; + } + + @After + public void tearDown() { + if (api != null) { + this.api.disconnect(); + this.api = null; + } + } + + @Test + public void testP1SignAndVerify() { + byte[] sign = this.api.SYD_NakedSign(orgData, SM2dn); + Assert.assertNotNull("P1签名结果不应为空", sign); + System.out.println("P1签名结果: " + Util.bytes2HexString(sign)); + + boolean verifyResult = this.api.SYD_NakedVerify(orgData, sign, SM2dn); + Assert.assertTrue("P1验签应通过", verifyResult); + System.out.println("P1验签结果: " + verifyResult); + } + + @Test + public void testP7DetachSignAndVerify() { + String sign = this.api.detachedSign(1, orgData, SM2dn); + Assert.assertNotNull("P7 Detach签名结果不应为空", sign); + System.out.println("P7 Detach签名结果: " + sign); + + boolean verifyResult = this.api.detachedVerify(1, orgData, sign); + Assert.assertTrue("P7 Detach验签应通过", verifyResult); + System.out.println("P7 Detach验签结果: " + verifyResult); + } + + @Test + public void testP7AttachSignAndVerify() throws Exception { + String sign = this.api.attachedSign(orgData, SM2dn); + Assert.assertNotNull("P7 Attach签名结果不应为空", sign); + System.out.println("P7 Attach签名结果: " + sign); + + boolean verifyResult = this.api.attachedVerify(sign); + Assert.assertTrue("P7 Attach验签应通过", verifyResult); + System.out.println("P7 Attach验签结果: " + verifyResult); + } + + @Test + public void testDigitalEnvelopeEncryptAndDecrypt() { + RetWrap genResult = this.api.generateDEByDn(SM2dn, orgData); + Assert.assertNotNull("数字信封生成结果不应为空", genResult); + + byte[] cipherKey = (byte[]) genResult.get(SydApi.RET_CIPHER_KEY); + Assert.assertNotNull("数字信封密文密钥不应为空", cipherKey); + System.out.println("数字信封密文密钥: " + new String(cipherKey)); + + RetWrap decryptResult = this.api.decryptDEByDN(SM2dn, cipherKey); + Assert.assertNotNull("数字信封解密结果不应为空", decryptResult); + + byte[] decryptedData = (byte[]) decryptResult.get("oData"); + Assert.assertNotNull("解密数据不应为空", decryptedData); + Assert.assertArrayEquals("解密数据应与原始数据一致", orgData, decryptedData); + System.out.println("数字信封解密结果: " + new String(decryptedData)); + } +} \ No newline at end of file