🎨 补充单元测试
This commit is contained in:
committed by
GitHub
parent
6a4f0db39e
commit
509bce53f3
@@ -0,0 +1,100 @@
|
||||
package me.chanjar.weixin.channel.util;
|
||||
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertNotNull;
|
||||
import static org.testng.Assert.assertTrue;
|
||||
import static org.testng.Assert.expectThrows;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import me.chanjar.weixin.channel.config.impl.WxChannelDefaultConfigImpl;
|
||||
import me.chanjar.weixin.common.error.WxRuntimeException;
|
||||
import me.chanjar.weixin.common.util.crypto.SHA1;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* {@link WxChCryptUtils} 单元测试
|
||||
*/
|
||||
public class WxChCryptUtilsTest {
|
||||
|
||||
private static final String APP_ID = "wx0000000000000002";
|
||||
private static final String TOKEN = "test_channel_token";
|
||||
/** 43 位 EncodingAESKey 占位值,非真实密钥 */
|
||||
private static final String AES_KEY = "MDEyMzQ1Njc4OWFiY2RlZjAxMjM0NTY3ODlhYmNkZWY";
|
||||
|
||||
private static final Pattern ENCRYPT_PATTERN = Pattern.compile("<Encrypt><!\\[CDATA\\[(.*?)]]></Encrypt>");
|
||||
private static final Pattern SIGNATURE_PATTERN =
|
||||
Pattern.compile("<MsgSignature><!\\[CDATA\\[(.*?)]]></MsgSignature>");
|
||||
private static final Pattern TIMESTAMP_PATTERN = Pattern.compile("<TimeStamp>(.*?)</TimeStamp>");
|
||||
private static final Pattern NONCE_PATTERN = Pattern.compile("<Nonce><!\\[CDATA\\[(.*?)]]></Nonce>");
|
||||
|
||||
private WxChCryptUtils cryptUtils() {
|
||||
WxChannelDefaultConfigImpl config = new WxChannelDefaultConfigImpl();
|
||||
config.setAppid(APP_ID);
|
||||
config.setToken(TOKEN);
|
||||
config.setAesKey(AES_KEY);
|
||||
return new WxChCryptUtils(config);
|
||||
}
|
||||
|
||||
private String group(Pattern pattern, String text) {
|
||||
Matcher matcher = pattern.matcher(text);
|
||||
assertTrue(matcher.find(), "未匹配到期望的节点:" + pattern.pattern());
|
||||
return matcher.group(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncryptThenDecrypt() {
|
||||
WxChCryptUtils cryptUtils = cryptUtils();
|
||||
String plainText = "<xml><ToUserName><![CDATA[" + APP_ID + "]]></ToUserName>"
|
||||
+ "<MsgType><![CDATA[event]]></MsgType></xml>";
|
||||
|
||||
String encryptedXml = cryptUtils.encrypt(plainText);
|
||||
assertNotNull(encryptedXml);
|
||||
|
||||
String encrypt = group(ENCRYPT_PATTERN, encryptedXml);
|
||||
String signature = group(SIGNATURE_PATTERN, encryptedXml);
|
||||
String timestamp = group(TIMESTAMP_PATTERN, encryptedXml);
|
||||
String nonce = group(NONCE_PATTERN, encryptedXml);
|
||||
|
||||
assertEquals(signature, SHA1.gen(TOKEN, timestamp, nonce, encrypt));
|
||||
assertEquals(cryptUtils.decryptXml(signature, timestamp, nonce, encryptedXml), plainText);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecryptXmlWithWrongSignature() {
|
||||
WxChCryptUtils cryptUtils = cryptUtils();
|
||||
String encryptedXml = cryptUtils.encrypt("<xml><MsgType><![CDATA[event]]></MsgType></xml>");
|
||||
String timestamp = group(TIMESTAMP_PATTERN, encryptedXml);
|
||||
String nonce = group(NONCE_PATTERN, encryptedXml);
|
||||
|
||||
expectThrows(WxRuntimeException.class,
|
||||
() -> cryptUtils.decryptXml("wrong_signature", timestamp, nonce, encryptedXml));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecryptWithSessionKey() throws Exception {
|
||||
byte[] keyBytes = "0123456789abcdef".getBytes(StandardCharsets.UTF_8);
|
||||
byte[] ivBytes = "fedcba9876543210".getBytes(StandardCharsets.UTF_8);
|
||||
String plainText = "{\"openid\":\"o0000000000000000000\"}";
|
||||
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, "AES"), new IvParameterSpec(ivBytes));
|
||||
String encryptedData = Base64.encodeBase64String(cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8)));
|
||||
|
||||
String decrypted = WxChCryptUtils.decrypt(Base64.encodeBase64String(keyBytes), encryptedData,
|
||||
Base64.encodeBase64String(ivBytes));
|
||||
assertEquals(decrypted, plainText);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecryptWithInvalidSessionKey() {
|
||||
expectThrows(RuntimeException.class,
|
||||
() -> WxChCryptUtils.decrypt(Base64.encodeBase64String("short_key".getBytes(StandardCharsets.UTF_8)),
|
||||
"invalid", Base64.encodeBase64String("fedcba9876543210".getBytes(StandardCharsets.UTF_8))));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package me.chanjar.weixin.channel.util;
|
||||
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertNotNull;
|
||||
import static org.testng.Assert.assertNull;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import me.chanjar.weixin.channel.bean.base.AttrInfo;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* {@link XmlUtils} 单元测试
|
||||
*/
|
||||
public class XmlUtilsTest {
|
||||
|
||||
private static final String XML = "<AttrInfo><attr_key>这是Key</attr_key><attr_value>这是Value</attr_value></AttrInfo>";
|
||||
|
||||
@Test
|
||||
public void testEncode() {
|
||||
String xml = XmlUtils.encode(new AttrInfo("这是Key", "这是Value"));
|
||||
assertNotNull(xml);
|
||||
assertEquals(xml, XML);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEncodeWithObjectMapper() {
|
||||
String xml = XmlUtils.encode(new XmlMapper(), new AttrInfo("这是Key", "这是Value"));
|
||||
assertNotNull(xml);
|
||||
assertEquals(XmlUtils.decode(xml, AttrInfo.class).getKey(), "这是Key");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecode() {
|
||||
AttrInfo info = XmlUtils.decode(XML, AttrInfo.class);
|
||||
assertNotNull(info);
|
||||
assertEquals(info.getKey(), "这是Key");
|
||||
assertEquals(info.getValue(), "这是Value");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecodeUnknownProperty() {
|
||||
String xml = "<AttrInfo><attr_key>k</attr_key><unknown>v</unknown></AttrInfo>";
|
||||
AttrInfo info = XmlUtils.decode(xml, AttrInfo.class);
|
||||
assertNotNull(info);
|
||||
assertEquals(info.getKey(), "k");
|
||||
assertNull(info.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecodeEmptyOrInvalidXml() {
|
||||
assertNull(XmlUtils.decode((String) null, AttrInfo.class));
|
||||
assertNull(XmlUtils.decode("", AttrInfo.class));
|
||||
assertNull(XmlUtils.decode("not a xml", AttrInfo.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecodeWithTypeReference() {
|
||||
AttrInfo info = XmlUtils.decode(XML, new TypeReference<AttrInfo>() {
|
||||
});
|
||||
assertNotNull(info);
|
||||
assertEquals(info.getValue(), "这是Value");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDecodeInputStream() {
|
||||
InputStream is = new ByteArrayInputStream(XML.getBytes(StandardCharsets.UTF_8));
|
||||
AttrInfo info = XmlUtils.decode(is, AttrInfo.class);
|
||||
assertNotNull(info);
|
||||
assertEquals(info.getKey(), "这是Key");
|
||||
}
|
||||
}
|
||||
@@ -8,4 +8,12 @@
|
||||
<class name="me.chanjar.weixin.open.bean.result.WxFastMaBeenSetCategoryResultTest"/>-->
|
||||
</classes>
|
||||
</test>
|
||||
<test name="Util_Test">
|
||||
<classes>
|
||||
<class name="me.chanjar.weixin.channel.util.JsonUtilsTest"/>
|
||||
<class name="me.chanjar.weixin.channel.util.XmlUtilsTest"/>
|
||||
<class name="me.chanjar.weixin.channel.util.WxChCryptUtilsTest"/>
|
||||
<class name="me.chanjar.weixin.channel.util.ResponseUtilsTest"/>
|
||||
</classes>
|
||||
</test>
|
||||
</suite>
|
||||
|
||||
Reference in New Issue
Block a user