forked from braintree-go/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
hmac_test.go
60 lines (49 loc) · 1.77 KB
/
hmac_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package braintree
import (
"testing"
)
func TestHmacerParseSignature(t *testing.T) {
hmacer := newHmacer(testGateway)
// Happy path
realSignature, err := hmacer.parseSignature(testGateway.PublicKey + "|my_actual_signature")
if err != nil {
t.Fatal(err)
} else if realSignature != "my_actual_signature" {
t.Fatal("parseSignature returned wrong signature")
}
// Test hmacer rejects an incorrect public key
_, err = hmacer.parseSignature("some_random_public_key|my_actual_signature")
if err == nil {
t.Fatal("Did not receive an error when the wrong public key was passed")
}
// Test hmacer rejects a signature-key pair with more than one pipe
_, err = hmacer.parseSignature("some_random_public_key|some_other_stuff|my_actual_signature")
if err == nil {
t.Fatal("Did not receive an error when the wrong public key was passed")
}
// Test hmacer rejects a singature-key pair with no pipes
_, err = hmacer.parseSignature("some_random_public_key&my_actual_signature")
if err == nil {
t.Fatal("Did not receive an error when the wrong public key was passed")
}
}
func TestHmacerVerifySignature(t *testing.T) {
gateway := New(Sandbox, "my_merchant_id", "my_public_key", "my_private_key")
hmacer := newHmacer(gateway)
signatureKeyPair := gateway.PublicKey + "|fa654fa4fe5537934960c483dbb0ee575d64b6ad"
payload := "my_random_value"
// Happy path
verified, err := hmacer.verifySignature(signatureKeyPair, payload)
if err != nil {
t.Fatal(err)
} else if !verified {
t.Fatal("Did not verify correct signature")
}
// Test hmacer does not verify when the payload has been modified
verified, err = hmacer.verifySignature(signatureKeyPair, payload+"a bad man in the middle")
if err != nil {
t.Fatal(err)
} else if verified {
t.Fatal("HMACer verified invalid signature.")
}
}