-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update README.md * docs: 补充 JsapiServiceExtension/AppServiceExtension, resolve #137 * bump version to 0.2.6 * feat: 增加 `getSha1HexString(byte[])`, resolve #126 * test: 补充测试用例
- Loading branch information
Showing
8 changed files
with
123 additions
and
40 deletions.
There are no files selected for viewing
This file contains 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 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
39 changes: 36 additions & 3 deletions
39
core/src/test/java/com/wechat/pay/java/core/util/ShaUtilTest.java
This file contains 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 |
---|---|---|
@@ -1,34 +1,67 @@ | ||
package com.wechat.pay.java.core.util; | ||
|
||
import static org.mockito.ArgumentMatchers.anyString; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.Mockito; | ||
|
||
public class ShaUtilTest { | ||
|
||
private static final String MESSAGE = "message"; | ||
private static final String SHA1 = "6f9b9af3cd6e8b8a73c2cdced37fe9f59226e27d"; | ||
private static final String SHA256 = | ||
"ab530a13e45914982b79f9b7e3fba994cfd1f3fb22f71cea1afbf02b460c6d1d"; | ||
|
||
@Test | ||
public void testInputStreamGetSha1HexString() throws IOException { | ||
ByteArrayInputStream inputStream = | ||
new ByteArrayInputStream(MESSAGE.getBytes(StandardCharsets.UTF_8)); | ||
String sha = ShaUtil.getSha1HexString(inputStream); | ||
Assert.assertNotNull(sha); | ||
Assert.assertEquals(SHA1, sha); | ||
} | ||
|
||
@Test | ||
public void testInputStreamGetSha256HexString() throws IOException { | ||
ByteArrayInputStream inputStream = | ||
new ByteArrayInputStream(MESSAGE.getBytes(StandardCharsets.UTF_8)); | ||
String sha = ShaUtil.getSha256HexString(inputStream); | ||
Assert.assertNotNull(sha); | ||
Assert.assertEquals(SHA256, sha); | ||
} | ||
|
||
@Test | ||
public void testBytesGetSha256HexString() { | ||
String sha = ShaUtil.getSha256HexString(MESSAGE.getBytes(StandardCharsets.UTF_8)); | ||
Assert.assertNotNull(sha); | ||
Assert.assertEquals(SHA256, sha); | ||
} | ||
|
||
@Test | ||
public void testBytesGetSha1HexString() { | ||
String sha = ShaUtil.getSha1HexString(MESSAGE.getBytes(StandardCharsets.UTF_8)); | ||
Assert.assertEquals(SHA1, sha); | ||
} | ||
|
||
@Test | ||
public void testNoSuchAlgorithmException() { | ||
try (MockedStatic<MessageDigest> mockDigest = Mockito.mockStatic(MessageDigest.class)) { | ||
mockDigest | ||
.when(() -> MessageDigest.getInstance(anyString())) | ||
.thenThrow(new NoSuchAlgorithmException()); | ||
|
||
Assert.assertThrows( | ||
SecurityException.class, | ||
() -> ShaUtil.getSha1HexString("test".getBytes(StandardCharsets.UTF_8))); | ||
Assert.assertThrows( | ||
SecurityException.class, | ||
() -> | ||
ShaUtil.getSha1HexString( | ||
new ByteArrayInputStream("test".getBytes(StandardCharsets.UTF_8)))); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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