From 82a6275c11c1c4c0d46f22b692c0a63d2b93e10e Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Mon, 7 Jun 2021 00:36:26 +0800 Subject: [PATCH] =?UTF-8?q?test(work):=20=E6=96=B0=E5=A2=9E=20WxBizMsgCryp?= =?UTF-8?q?tor=20=E5=B7=A5=E5=85=B7=E7=B1=BB=E7=9A=84=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WechatWorkUtilityTests.cs | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 test/SKIT.FlurlHttpClient.Wechat.Work.UnitTests/WechatWorkUtilityTests.cs diff --git a/test/SKIT.FlurlHttpClient.Wechat.Work.UnitTests/WechatWorkUtilityTests.cs b/test/SKIT.FlurlHttpClient.Wechat.Work.UnitTests/WechatWorkUtilityTests.cs new file mode 100644 index 00000000..67450212 --- /dev/null +++ b/test/SKIT.FlurlHttpClient.Wechat.Work.UnitTests/WechatWorkUtilityTests.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Xunit; + +namespace SKIT.FlurlHttpClient.Wechat.Work.UnitTests +{ + public class WechatWorkUtilityTests + { + [Fact(DisplayName = "回调信息解析")] + public void WxBizMsgCryptorParsingTest() + { + string xml = ""; + + bool isValidXml = Utilities.WxBizMsgCryptor.TryParseXml(xml, out string toUserName, out string agentId, out string encryptedMsg); + Assert.True(isValidXml); + Assert.Equal("wx5823bf96d3bd56c7", toUserName); + Assert.Equal("218", agentId); + Assert.Equal("RypEvHKD8QQKFhvQ6QleEB4J58tiPdvo+rtK1I9qca6aM/wvqnLSV5zEPeusUiX5L5X/0lWfrf0QADHHhGd3QczcdCUpj911L3vg3W/sYYvuJTs3TUUkSUXxaccAS0qhxchrRYt66wiSpGLYL42aM6A8dTT+6k4aSknmPj48kzJs8qLjvd4Xgpue06DOdnLxAUHzM6+kDZ+HMZfJYuR+LtwGc2hgf5gsijff0ekUNXZiqATP7PF5mZxZ3Izoun1s4zG4LUMnvw2r+KqCKIw+3IQH03v+BCA9nMELNqbSf6tiWSrXJB3LAVGUcallcrw8V2t9EL4EhzJWrQUax5wLVMNS0+rUPA3k22Ncx4XXZS9o0MBH27Bo6BpNelZpS+/uh9KsNlY6bHCmJU9p8g7m3fVKn28H3KDYA5Pl/T8Z1ptDAVe0lXdQ2YoyyH2uyPIGHBZZIs2pDBS8R07+qN+E7Q==", encryptedMsg); + } + + [Fact(DisplayName = "回调信息验签")] + public void WxBizMsgCryptorSignatureTest() + { + string token = "QDG6eK"; + string timestamp = "1409659813"; + string nonce = "1372623149"; + string cipherText = "RypEvHKD8QQKFhvQ6QleEB4J58tiPdvo+rtK1I9qca6aM/wvqnLSV5zEPeusUiX5L5X/0lWfrf0QADHHhGd3QczcdCUpj911L3vg3W/sYYvuJTs3TUUkSUXxaccAS0qhxchrRYt66wiSpGLYL42aM6A8dTT+6k4aSknmPj48kzJs8qLjvd4Xgpue06DOdnLxAUHzM6+kDZ+HMZfJYuR+LtwGc2hgf5gsijff0ekUNXZiqATP7PF5mZxZ3Izoun1s4zG4LUMnvw2r+KqCKIw+3IQH03v+BCA9nMELNqbSf6tiWSrXJB3LAVGUcallcrw8V2t9EL4EhzJWrQUax5wLVMNS0+rUPA3k22Ncx4XXZS9o0MBH27Bo6BpNelZpS+/uh9KsNlY6bHCmJU9p8g7m3fVKn28H3KDYA5Pl/T8Z1ptDAVe0lXdQ2YoyyH2uyPIGHBZZIs2pDBS8R07+qN+E7Q=="; + + string actualSignText = Utilities.WxBizMsgCryptor.GenerateSignature(sToken: token, sTimestamp: timestamp, sNonce: nonce, sMsgEncrypt: cipherText); + string expectdSignText = "477715d11cdb4164915debcba66cb864d751f3e6"; + + Assert.Equal(expectdSignText, actualSignText, ignoreCase: true); + Assert.True(Utilities.WxBizMsgCryptor.VerifySignature(sToken: token, sTimestamp: timestamp, sNonce: nonce, sMsgEncrypt: cipherText, sMsgSign: expectdSignText)); + } + + [Fact(DisplayName = "回调信息解密")] + public void WxBizMsgCryptorDecryptTest() + { + string corpId = "wx5823bf96d3bd56c7"; + string encodingAESKey = "jWmYm7qr5nMoAUwZRjGtBxmz3KA1tkAj3ykkR6q2B2C"; + string cipherText = "RypEvHKD8QQKFhvQ6QleEB4J58tiPdvo+rtK1I9qca6aM/wvqnLSV5zEPeusUiX5L5X/0lWfrf0QADHHhGd3QczcdCUpj911L3vg3W/sYYvuJTs3TUUkSUXxaccAS0qhxchrRYt66wiSpGLYL42aM6A8dTT+6k4aSknmPj48kzJs8qLjvd4Xgpue06DOdnLxAUHzM6+kDZ+HMZfJYuR+LtwGc2hgf5gsijff0ekUNXZiqATP7PF5mZxZ3Izoun1s4zG4LUMnvw2r+KqCKIw+3IQH03v+BCA9nMELNqbSf6tiWSrXJB3LAVGUcallcrw8V2t9EL4EhzJWrQUax5wLVMNS0+rUPA3k22Ncx4XXZS9o0MBH27Bo6BpNelZpS+/uh9KsNlY6bHCmJU9p8g7m3fVKn28H3KDYA5Pl/T8Z1ptDAVe0lXdQ2YoyyH2uyPIGHBZZIs2pDBS8R07+qN+E7Q=="; + + string actualCipherText = Utilities.WxBizMsgCryptor.AESDecrypt(cipherText, encodingAESKey, out string actualCorpId); + string expectdCipherText = "\n\n1409659813\n\n\n4561255354251345929\n218\n"; + + Assert.Equal(corpId, actualCorpId); + Assert.Equal(expectdCipherText, actualCipherText, ignoreCase: true); + } + } +}