|
2 | 2 |
|
3 | 3 | import cn.binarywang.wx.miniapp.bean.xpay.WxMaXPayTeamInfo; |
4 | 4 | import cn.binarywang.wx.miniapp.constant.WxMaConstants; |
| 5 | +import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl; |
| 6 | +import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils; |
5 | 7 | import me.chanjar.weixin.common.api.WxConsts; |
| 8 | +import me.chanjar.weixin.common.error.WxRuntimeException; |
| 9 | +import me.chanjar.weixin.common.util.crypto.SHA1; |
6 | 10 | import org.testng.annotations.Test; |
7 | 11 |
|
| 12 | +import java.io.ByteArrayInputStream; |
| 13 | +import java.nio.charset.StandardCharsets; |
8 | 14 | import java.util.List; |
9 | 15 | import java.util.Map; |
10 | 16 |
|
11 | 17 | import static org.assertj.core.api.Assertions.as; |
12 | 18 | import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
13 | 20 | import static org.testng.Assert.assertEquals; |
14 | 21 | import static org.testng.Assert.assertNotNull; |
15 | 22 |
|
@@ -459,6 +466,48 @@ private void checkXPayComplaintNotifyMessage(WxMaMessage msg) { |
459 | 466 | assertEquals(msg.getRequestId(), "req_005"); |
460 | 467 | } |
461 | 468 |
|
| 469 | + @Test |
| 470 | + public void testFromEncryptedJsonWithSignature() { |
| 471 | + WxMaDefaultConfigImpl config = buildMessagePushConfig(); |
| 472 | + String plainJson = "{\"ToUserName\":\"gh_123456789abc\",\"FromUserName\":\"fromUser\",\"CreateTime\":1710000000," |
| 473 | + + "\"MsgType\":\"event\",\"Event\":\"subscribe_msg_popup_event\"}"; |
| 474 | + String encrypt = new WxMaCryptUtils(config).encrypt("1234567890abcdef", plainJson); |
| 475 | + String timestamp = "1710000000"; |
| 476 | + String nonce = "nonce123"; |
| 477 | + String msgSignature = SHA1.gen(config.getToken(), timestamp, nonce, encrypt); |
| 478 | + String encryptedJson = "{\"Encrypt\":\"" + encrypt + "\"}"; |
| 479 | + |
| 480 | + WxMaMessage wxMessage = WxMaMessage.fromEncryptedJson(new ByteArrayInputStream( |
| 481 | + encryptedJson.getBytes(StandardCharsets.UTF_8)), config, timestamp, nonce, msgSignature); |
| 482 | + |
| 483 | + assertEquals(wxMessage.getToUser(), "gh_123456789abc"); |
| 484 | + assertEquals(wxMessage.getFromUser(), "fromUser"); |
| 485 | + assertEquals(wxMessage.getCreateTime(), new Integer(1710000000)); |
| 486 | + assertEquals(wxMessage.getMsgType(), WxConsts.XmlMsgType.EVENT); |
| 487 | + assertEquals(wxMessage.getEvent(), "subscribe_msg_popup_event"); |
| 488 | + } |
| 489 | + |
| 490 | + @Test |
| 491 | + public void testFromEncryptedJsonWithSignatureInvalidMsgSignature() { |
| 492 | + WxMaDefaultConfigImpl config = buildMessagePushConfig(); |
| 493 | + String plainJson = "{\"ToUserName\":\"gh_123456789abc\",\"FromUserName\":\"fromUser\"}"; |
| 494 | + String encrypt = new WxMaCryptUtils(config).encrypt("1234567890abcdef", plainJson); |
| 495 | + String encryptedJson = "{\"Encrypt\":\"" + encrypt + "\"}"; |
| 496 | + |
| 497 | + assertThatThrownBy(() -> WxMaMessage.fromEncryptedJson(encryptedJson, config, |
| 498 | + "1710000000", "nonce123", "invalidSignature")) |
| 499 | + .isInstanceOf(WxRuntimeException.class) |
| 500 | + .hasMessageContaining("加密消息签名校验失败"); |
| 501 | + } |
| 502 | + |
| 503 | + private WxMaDefaultConfigImpl buildMessagePushConfig() { |
| 504 | + WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); |
| 505 | + config.setAppid("wx1234567890abcdef"); |
| 506 | + config.setToken("testToken"); |
| 507 | + config.setAesKey("abcdefghijklmnopqrstuvwxyz0123456789ABCDEFG"); |
| 508 | + return config; |
| 509 | + } |
| 510 | + |
462 | 511 | /** |
463 | 512 | * 虚拟支付 iOS 退款查询通知事件 xpay_subscribe_ios_refund_query_notify 测试用例(XML格式) |
464 | 513 | */ |
|
0 commit comments