Quellcode durchsuchen

fix: correct OMG signer fixture and secret validation

qmj vor 2 Wochen
Ursprung
Commit
11f928ce4a

+ 11 - 2
ruoyi-admin/src/main/java/com/ruoyi/app/omgpay/OmgCheckMacSigner.java

@@ -25,8 +25,8 @@ public class OmgCheckMacSigner {
     }
 
     private static void requireSecrets(String hashKey, String hashIv) {
-        requireNonNull("hashKey", hashKey);
-        requireNonNull("hashIv", hashIv);
+        requirePresent("hashKey", hashKey);
+        requirePresent("hashIv", hashIv);
     }
 
     private static TreeMap<String, String> validatedCopy(Map<String, String> fields) {
@@ -45,6 +45,15 @@ public class OmgCheckMacSigner {
         return sorted;
     }
 
+    private static void requirePresent(String name, String value) {
+        if (value == null) {
+            throw new IllegalArgumentException(name + " must not be null");
+        }
+        if (value.isEmpty()) {
+            throw new IllegalArgumentException(name + " must not be empty");
+        }
+    }
+
     private static void requireNonNull(String name, String value) {
         if (value == null) {
             throw new IllegalArgumentException(name + " must not be null");

+ 22 - 2
ruoyi-admin/src/test/java/com/ruoyi/app/omgpay/OmgCheckMacSignerTest.java

@@ -40,6 +40,26 @@ class OmgCheckMacSignerTest {
                 () -> signer.sign(Map.of("CheckMacValue", "caller-value"), HASH_KEY, HASH_IV));
     }
 
+    @Test
+    void rejectsNullHashKey() {
+        assertThrows(IllegalArgumentException.class, () -> signer.sign(officialFields(), null, HASH_IV));
+    }
+
+    @Test
+    void rejectsNullHashIv() {
+        assertThrows(IllegalArgumentException.class, () -> signer.sign(officialFields(), HASH_KEY, null));
+    }
+
+    @Test
+    void rejectsEmptyHashKey() {
+        assertThrows(IllegalArgumentException.class, () -> signer.sign(officialFields(), "", HASH_IV));
+    }
+
+    @Test
+    void rejectsEmptyHashIv() {
+        assertThrows(IllegalArgumentException.class, () -> signer.sign(officialFields(), HASH_KEY, ""));
+    }
+
     @Test
     void leavesInputUnchangedAndReturnsUppercaseSha256Hex() {
         Map<String, String> fields = new LinkedHashMap<>(officialFields());
@@ -53,13 +73,13 @@ class OmgCheckMacSignerTest {
 
     private static Map<String, String> officialFields() {
         Map<String, String> fields = new LinkedHashMap<>();
-        fields.put("TradeDesc", "促銷方案");
+        fields.put("TradeDesc", "\u4FC3\u92B7\u65B9\u6848");
         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("ItemName", "Apple iphone 7 \u624B\u6A5F\u6BBC");
         fields.put("TotalAmount", "1000");
         fields.put("ChoosePayment", "ALL");
         fields.put("EncryptType", "1");