|
| 1 | +package com.github.binarywang.wxpay.v3.auth; |
| 2 | + |
| 3 | +import org.testng.annotations.BeforeMethod; |
| 4 | +import org.testng.annotations.Test; |
| 5 | + |
| 6 | +import java.nio.charset.StandardCharsets; |
| 7 | +import java.security.cert.X509Certificate; |
| 8 | + |
| 9 | +import static org.testng.Assert.*; |
| 10 | + |
| 11 | +/** |
| 12 | + * 测试公钥模式下 AutoUpdateCertificatesVerifier 的健壮性 |
| 13 | + * |
| 14 | + * @author copilot |
| 15 | + */ |
| 16 | +public class AutoUpdateCertificatesVerifierPublicKeyModeTest { |
| 17 | + |
| 18 | + private String invalidMchId; |
| 19 | + private String invalidApiV3Key; |
| 20 | + private String invalidCertSerialNo; |
| 21 | + private String payBaseUrl; |
| 22 | + private WxPayCredentials credentials; |
| 23 | + |
| 24 | + @BeforeMethod |
| 25 | + public void setUp() { |
| 26 | + // 使用无效的配置,模拟证书下载失败的场景 |
| 27 | + invalidMchId = "invalid_mch_id"; |
| 28 | + invalidApiV3Key = "invalid_api_v3_key_must_be_32_b"; |
| 29 | + invalidCertSerialNo = "invalid_serial_no"; |
| 30 | + payBaseUrl = "https://api.mch.weixin.qq.com"; |
| 31 | + |
| 32 | + credentials = new WxPayCredentials( |
| 33 | + invalidMchId, |
| 34 | + new PrivateKeySigner(invalidCertSerialNo, null) |
| 35 | + ); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * 测试当证书下载失败时,构造函数不应该抛出异常 |
| 40 | + * 这是为了支持公钥模式下的场景,在公钥模式下商户可能没有平台证书 |
| 41 | + */ |
| 42 | + @Test |
| 43 | + public void testConstructorShouldNotThrowExceptionWhenCertDownloadFails() { |
| 44 | + // 构造函数应该不抛出异常,即使证书下载失败 |
| 45 | + AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier( |
| 46 | + credentials, |
| 47 | + invalidApiV3Key.getBytes(StandardCharsets.UTF_8), |
| 48 | + 60, |
| 49 | + payBaseUrl, |
| 50 | + null |
| 51 | + ); |
| 52 | + // 如果没有抛出异常,测试通过 |
| 53 | + assertNotNull(verifier); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * 测试当没有有效证书时,verify 方法应该返回 false 而不是抛出异常 |
| 58 | + */ |
| 59 | + @Test |
| 60 | + public void testVerifyShouldReturnFalseWhenNoCertificateAvailable() { |
| 61 | + AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier( |
| 62 | + credentials, |
| 63 | + invalidApiV3Key.getBytes(StandardCharsets.UTF_8), |
| 64 | + 60, |
| 65 | + payBaseUrl, |
| 66 | + null |
| 67 | + ); |
| 68 | + |
| 69 | + // verify 方法应该返回 false,而不是抛出异常 |
| 70 | + boolean result = verifier.verify("test_serial", "test_message".getBytes(), "test_signature"); |
| 71 | + assertFalse(result, "当没有有效证书时,verify 应该返回 false"); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * 测试当没有有效证书时,getValidCertificate 方法应该抛出有意义的异常 |
| 76 | + */ |
| 77 | + @Test(expectedExceptions = me.chanjar.weixin.common.error.WxRuntimeException.class, |
| 78 | + expectedExceptionsMessageRegExp = ".*No valid certificate available.*") |
| 79 | + public void testGetValidCertificateShouldThrowMeaningfulException() { |
| 80 | + AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier( |
| 81 | + credentials, |
| 82 | + invalidApiV3Key.getBytes(StandardCharsets.UTF_8), |
| 83 | + 60, |
| 84 | + payBaseUrl, |
| 85 | + null |
| 86 | + ); |
| 87 | + |
| 88 | + // 应该抛出有意义的异常 |
| 89 | + X509Certificate certificate = verifier.getValidCertificate(); |
| 90 | + } |
| 91 | +} |
0 commit comments