50 lines
1.2 KiB
Go
50 lines
1.2 KiB
Go
package payment
|
|
|
|
import "testing"
|
|
|
|
func TestSignDeterministic(t *testing.T) {
|
|
params := map[string]string{
|
|
"appid": "1000", "trade_order_id": "ORDER001", "total_fee": "29.90",
|
|
}
|
|
s1 := Sign(params, "secret123")
|
|
s2 := Sign(params, "secret123")
|
|
if s1 != s2 {
|
|
t.Fatalf("相同参数签名应一致: %s != %s", s1, s2)
|
|
}
|
|
if s1 == "" {
|
|
t.Fatal("签名不应为空")
|
|
}
|
|
}
|
|
|
|
func TestSignChangesWithSecret(t *testing.T) {
|
|
params := map[string]string{"appid": "1000", "trade_order_id": "ORDER001"}
|
|
if Sign(params, "a") == Sign(params, "b") {
|
|
t.Fatal("不同 secret 签名应不同")
|
|
}
|
|
}
|
|
|
|
func TestVerifyNotify(t *testing.T) {
|
|
params := map[string]string{
|
|
"appid": "1000", "trade_order_id": "ORDER001", "total_fee": "29.90",
|
|
"status": "OD", "hash": "",
|
|
}
|
|
hash := Sign(params, "secret123")
|
|
if !VerifyNotify(params, hash, "secret123") {
|
|
t.Fatal("正确签名应通过验签")
|
|
}
|
|
params["total_fee"] = "0.01"
|
|
if VerifyNotify(params, hash, "secret123") {
|
|
t.Fatal("篡改参数后应验签失败")
|
|
}
|
|
if VerifyNotify(params, hash, "wrong-secret") {
|
|
t.Fatal("错误 secret 应验签失败")
|
|
}
|
|
}
|
|
|
|
func TestGetConfigDisabledWhenEmpty(t *testing.T) {
|
|
cfg := GetConfig(t.Context())
|
|
if cfg.Enabled {
|
|
t.Fatal("默认配置(key 为空)应 disabled")
|
|
}
|
|
}
|