-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
修复公钥模式下平台证书自动更新导致的初始化失败问题 #3854
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0b3ec25
Initial plan
Copilot 0b3c45a
修复公钥模式下自动更新证书报错问题
Copilot 77134da
Update weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/a…
binarywang 1b3ec31
Update weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/a…
binarywang 546310b
Update weixin-java-pay/src/test/java/com/github/binarywang/wxpay/v3/a…
binarywang 43c69b1
重构测试代码:提取重复的测试数据到@BeforeMethod
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
.../com/github/binarywang/wxpay/v3/auth/AutoUpdateCertificatesVerifierPublicKeyModeTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package com.github.binarywang.wxpay.v3.auth; | ||
|
|
||
| import org.testng.annotations.BeforeMethod; | ||
| import org.testng.annotations.Test; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.security.cert.X509Certificate; | ||
|
|
||
| import static org.testng.Assert.*; | ||
|
|
||
| /** | ||
| * 测试公钥模式下 AutoUpdateCertificatesVerifier 的健壮性 | ||
| * | ||
| * @author copilot | ||
| */ | ||
| public class AutoUpdateCertificatesVerifierPublicKeyModeTest { | ||
|
|
||
| private String invalidMchId; | ||
| private String invalidApiV3Key; | ||
| private String invalidCertSerialNo; | ||
| private String payBaseUrl; | ||
| private WxPayCredentials credentials; | ||
|
|
||
| @BeforeMethod | ||
| public void setUp() { | ||
| // 使用无效的配置,模拟证书下载失败的场景 | ||
| invalidMchId = "invalid_mch_id"; | ||
| invalidApiV3Key = "invalid_api_v3_key_must_be_32_b"; | ||
| invalidCertSerialNo = "invalid_serial_no"; | ||
| payBaseUrl = "https://api.mch.weixin.qq.com"; | ||
|
|
||
| credentials = new WxPayCredentials( | ||
| invalidMchId, | ||
| new PrivateKeySigner(invalidCertSerialNo, null) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * 测试当证书下载失败时,构造函数不应该抛出异常 | ||
| * 这是为了支持公钥模式下的场景,在公钥模式下商户可能没有平台证书 | ||
| */ | ||
| @Test | ||
| public void testConstructorShouldNotThrowExceptionWhenCertDownloadFails() { | ||
| // 构造函数应该不抛出异常,即使证书下载失败 | ||
| AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier( | ||
| credentials, | ||
| invalidApiV3Key.getBytes(StandardCharsets.UTF_8), | ||
| 60, | ||
| payBaseUrl, | ||
| null | ||
| ); | ||
| // 如果没有抛出异常,测试通过 | ||
| assertNotNull(verifier); | ||
| } | ||
|
|
||
| /** | ||
| * 测试当没有有效证书时,verify 方法应该返回 false 而不是抛出异常 | ||
| */ | ||
| @Test | ||
| public void testVerifyShouldReturnFalseWhenNoCertificateAvailable() { | ||
| AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier( | ||
| credentials, | ||
| invalidApiV3Key.getBytes(StandardCharsets.UTF_8), | ||
| 60, | ||
| payBaseUrl, | ||
| null | ||
| ); | ||
|
|
||
| // verify 方法应该返回 false,而不是抛出异常 | ||
| boolean result = verifier.verify("test_serial", "test_message".getBytes(), "test_signature"); | ||
| assertFalse(result, "当没有有效证书时,verify 应该返回 false"); | ||
| } | ||
|
|
||
| /** | ||
| * 测试当没有有效证书时,getValidCertificate 方法应该抛出有意义的异常 | ||
| */ | ||
| @Test(expectedExceptions = me.chanjar.weixin.common.error.WxRuntimeException.class, | ||
| expectedExceptionsMessageRegExp = ".*No valid certificate available.*") | ||
| public void testGetValidCertificateShouldThrowMeaningfulException() { | ||
| AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier( | ||
| credentials, | ||
| invalidApiV3Key.getBytes(StandardCharsets.UTF_8), | ||
| 60, | ||
| payBaseUrl, | ||
| null | ||
| ); | ||
|
Comment on lines
+60
to
+86
|
||
|
|
||
| // 应该抛出有意义的异常 | ||
| X509Certificate certificate = verifier.getValidCertificate(); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里的容错只捕获了
IOException | GeneralSecurityException,但autoUpdateCert()在 HTTP 非 200(以及签名等运行时失败)时会直接抛WxRuntimeException,仍可能导致初始化阶段异常退出(与 PR 目标的 404 场景不一致)。建议确认是否需要把该运行时异常也纳入容错范围。🤖 Was this useful? React with 👍 or 👎