58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
package fadada
|
|
|
|
import "testing"
|
|
|
|
func TestVerifyCallback(t *testing.T) {
|
|
secret := "test-secret"
|
|
c := NewClient(&Config{
|
|
AppID: "80005307",
|
|
AppSecret: secret,
|
|
ServerURL: "https://uat-api.fadada.com/api/v5/",
|
|
})
|
|
|
|
biz := `{"signTaskId":"st-1","signTaskStatus":"task_finished"}`
|
|
timestamp := "1720000000000"
|
|
nonce := "nonce-1"
|
|
headMap := map[string]string{
|
|
"X-FASC-App-Id": "80005307",
|
|
"X-FASC-Sign-Type": "HMAC-SHA256",
|
|
"X-FASC-Timestamp": timestamp,
|
|
"X-FASC-Nonce": nonce,
|
|
"X-FASC-Event": "sign-task-finished",
|
|
"bizContent": biz,
|
|
}
|
|
sign := SignByMap(headMap, timestamp, secret)
|
|
|
|
headers := map[string]string{
|
|
"X-FASC-App-Id": "80005307",
|
|
"X-FASC-Sign-Type": "HMAC-SHA256",
|
|
"X-FASC-Timestamp": timestamp,
|
|
"X-FASC-Nonce": nonce,
|
|
"X-FASC-Event": "sign-task-finished",
|
|
"X-FASC-Sign": sign,
|
|
}
|
|
if err := c.VerifyCallback(headers, biz); err != nil {
|
|
t.Fatalf("verify failed: %v", err)
|
|
}
|
|
|
|
headers["X-FASC-Sign"] = "deadbeef"
|
|
if err := c.VerifyCallback(headers, biz); err == nil {
|
|
t.Fatal("expected signature failure")
|
|
}
|
|
}
|
|
|
|
func TestSignByMapStable(t *testing.T) {
|
|
m := map[string]string{
|
|
"X-FASC-App-Id": "app",
|
|
"X-FASC-Nonce": "n1",
|
|
"X-FASC-Sign-Type": "HMAC-SHA256",
|
|
"X-FASC-Timestamp": "1720000000000",
|
|
"bizContent": `{"a":1}`,
|
|
}
|
|
s1 := SignByMap(m, "1720000000000", "secret")
|
|
s2 := SignByMap(m, "1720000000000", "secret")
|
|
if s1 == "" || s1 != s2 {
|
|
t.Fatalf("signature unstable: %s vs %s", s1, s2)
|
|
}
|
|
}
|