-
-
Notifications
You must be signed in to change notification settings - Fork 9k
Fix PEM format private key and certificate handling in WeChat Pay config #3712
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
Draft
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
116 changes: 116 additions & 0 deletions
...-java-pay/src/test/java/com/github/binarywang/wxpay/config/WxPayConfigPrivateKeyTest.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,116 @@ | ||
package com.github.binarywang.wxpay.config; | ||
|
||
import com.github.binarywang.wxpay.exception.WxPayException; | ||
import org.testng.annotations.Test; | ||
|
||
import static org.testng.Assert.*; | ||
|
||
/** | ||
* Test cases for private key format handling in WxPayConfig | ||
*/ | ||
public class WxPayConfigPrivateKeyTest { | ||
|
||
@Test | ||
public void testPrivateKeyStringFormat_PemFormat() { | ||
WxPayConfig config = new WxPayConfig(); | ||
|
||
// Set minimal required configuration | ||
config.setMchId("1234567890"); | ||
config.setApiV3Key("test-api-v3-key-32-characters-long"); | ||
config.setCertSerialNo("test-serial-number"); | ||
|
||
// Test with PEM format private key string that would previously fail | ||
String pemKey = "-----BEGIN PRIVATE KEY-----\n" + | ||
"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC2pK3buBufh8Vo\n" + | ||
"X4sfYbZ5CcPeGMnVQTGmj0b6\n" + | ||
"-----END PRIVATE KEY-----"; | ||
|
||
config.setPrivateKeyString(pemKey); | ||
|
||
// This should not throw a "无效的密钥格式" exception immediately | ||
// The actual key validation will happen during HTTP client initialization | ||
// but at least the format parsing should not fail | ||
|
||
try { | ||
// Try to initialize API V3 HTTP client - this might fail for other reasons | ||
// (like invalid key content) but should not fail due to format parsing | ||
config.initApiV3HttpClient(); | ||
// If we get here without InvalidKeySpecException, the format detection worked | ||
} catch (WxPayException e) { | ||
// Check that it's not the specific "无效的密钥格式" error from PemUtils | ||
if (e.getCause() != null && | ||
e.getCause().getMessage() != null && | ||
e.getCause().getMessage().contains("无效的密钥格式")) { | ||
fail("Private key format detection failed - PEM format was not handled correctly: " + e.getMessage()); | ||
} | ||
// Other exceptions are acceptable for this test since we're using a dummy key | ||
} catch (Exception e) { | ||
// Check for the specific InvalidKeySpecException that indicates format problems | ||
if (e.getCause() != null && | ||
e.getCause().getMessage() != null && | ||
e.getCause().getMessage().contains("无效的密钥格式")) { | ||
fail("Private key format detection failed - PEM format was not handled correctly: " + e.getMessage()); | ||
} | ||
// Other exceptions are acceptable for this test since we're using a dummy key | ||
} | ||
} | ||
|
||
@Test | ||
public void testPrivateKeyStringFormat_EmptyString() { | ||
WxPayConfig config = new WxPayConfig(); | ||
|
||
// Test with empty string - should not cause format errors | ||
config.setPrivateKeyString(""); | ||
|
||
// This should handle empty strings gracefully | ||
// No assertion needed, just ensuring no exceptions during object creation | ||
assertNotNull(config); | ||
} | ||
|
||
@Test | ||
public void testPrivateKeyStringFormat_NullString() { | ||
WxPayConfig config = new WxPayConfig(); | ||
|
||
// Test with null string - should not cause format errors | ||
config.setPrivateKeyString(null); | ||
|
||
// This should handle null strings gracefully | ||
assertNotNull(config); | ||
} | ||
|
||
@Test | ||
public void testPrivateCertStringFormat_PemFormat() { | ||
WxPayConfig config = new WxPayConfig(); | ||
|
||
// Set minimal required configuration | ||
config.setMchId("1234567890"); | ||
config.setApiV3Key("test-api-v3-key-32-characters-long"); | ||
|
||
// Test with PEM format certificate string that would previously fail | ||
String pemCert = "-----BEGIN CERTIFICATE-----\n" + | ||
"MIICdTCCAd4CAQAwDQYJKoZIhvcNAQEFBQAwRTELMAkGA1UEBhMCQVUxEzARBgNV\n" + | ||
"BAsKClRlc3QgQ2VydCBEYXRhMRswGQYDVQQDDBJUZXN0IENlcnRpZmljYXRlQ0Ew\n" + | ||
"-----END CERTIFICATE-----"; | ||
|
||
config.setPrivateCertString(pemCert); | ||
|
||
// This should not throw a format parsing exception immediately | ||
// The actual certificate validation will happen during HTTP client initialization | ||
// but at least the format parsing should not fail | ||
|
||
try { | ||
// Try to initialize API V3 HTTP client - this might fail for other reasons | ||
// (like invalid cert content) but should not fail due to format parsing | ||
config.initApiV3HttpClient(); | ||
// If we get here without Base64 decoding issues, the format detection worked | ||
} catch (Exception e) { | ||
// Check that it's not the specific Base64 decoding error | ||
if (e.getCause() != null && | ||
e.getCause().getMessage() != null && | ||
e.getCause().getMessage().contains("Illegal base64 character")) { | ||
fail("Certificate format detection failed - PEM format was not handled correctly: " + e.getMessage()); | ||
} | ||
// Other exceptions are acceptable for this test since we're using a dummy cert | ||
} | ||
} | ||
} |
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.