|
@@ -0,0 +1,74 @@
|
|
|
|
|
+package com.ruoyi.app.omgpay;
|
|
|
|
|
+
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.LinkedHashMap;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
|
+
|
|
|
|
|
+class OmgCheckMacSignerTest {
|
|
|
|
|
+
|
|
|
|
|
+ private static final String HASH_KEY = "5294y06JbISpM5x9";
|
|
|
|
|
+ private static final String HASH_IV = "v77hoKGq4kWxNNIS";
|
|
|
|
|
+ private static final String EXPECTED_SIGNATURE =
|
|
|
|
|
+ "AA5842FDA7E55ACEB7118D6353E9822CA6D6FF09A0D1FC129A879DD5CAF93266";
|
|
|
|
|
+
|
|
|
|
|
+ private final OmgCheckMacSigner signer = new OmgCheckMacSigner();
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void signsOfficialAppendixVector() {
|
|
|
|
|
+ assertEquals(EXPECTED_SIGNATURE, signer.sign(officialFields(), HASH_KEY, HASH_IV));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void includesAdditionalAndEmptyFieldsInSignature() {
|
|
|
|
|
+ Map<String, String> fields = officialFields();
|
|
|
|
|
+
|
|
|
|
|
+ assertNotEquals(signer.sign(fields, HASH_KEY, HASH_IV),
|
|
|
|
|
+ signer.sign(withExtraField(fields, "NeedExtraPaidInfo", "Y"), HASH_KEY, HASH_IV));
|
|
|
|
|
+ assertNotEquals(signer.sign(fields, HASH_KEY, HASH_IV),
|
|
|
|
|
+ signer.sign(withExtraField(fields, "EmptyExtra", ""), HASH_KEY, HASH_IV));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void rejectsCallerSuppliedCheckMacValue() {
|
|
|
|
|
+ assertThrows(IllegalArgumentException.class,
|
|
|
|
|
+ () -> signer.sign(Map.of("CheckMacValue", "caller-value"), HASH_KEY, HASH_IV));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void leavesInputUnchangedAndReturnsUppercaseSha256Hex() {
|
|
|
|
|
+ Map<String, String> fields = new LinkedHashMap<>(officialFields());
|
|
|
|
|
+ Map<String, String> snapshot = new LinkedHashMap<>(fields);
|
|
|
|
|
+
|
|
|
|
|
+ String signature = signer.sign(fields, HASH_KEY, HASH_IV);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(snapshot, fields);
|
|
|
|
|
+ assertTrue(signature.matches("[0-9A-F]{64}"));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static Map<String, String> officialFields() {
|
|
|
|
|
+ Map<String, String> fields = new LinkedHashMap<>();
|
|
|
|
|
+ fields.put("TradeDesc", "促銷方案");
|
|
|
|
|
+ fields.put("PaymentType", "aio");
|
|
|
|
|
+ fields.put("MerchantTradeDate", "2013/03/12 15:30:23");
|
|
|
|
|
+ fields.put("MerchantTradeNo", "funpoint20130312153023");
|
|
|
|
|
+ fields.put("MerchantID", "2000132");
|
|
|
|
|
+ fields.put("ReturnURL", "https://www.funpoint.com.tw/receive.php");
|
|
|
|
|
+ fields.put("ItemName", "Apple iphone 7 手機殼");
|
|
|
|
|
+ fields.put("TotalAmount", "1000");
|
|
|
|
|
+ fields.put("ChoosePayment", "ALL");
|
|
|
|
|
+ fields.put("EncryptType", "1");
|
|
|
|
|
+ return fields;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static Map<String, String> withExtraField(Map<String, String> fields, String key, String value) {
|
|
|
|
|
+ Map<String, String> updated = new LinkedHashMap<>(fields);
|
|
|
|
|
+ updated.put(key, value);
|
|
|
|
|
+ return updated;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|