-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhmac_test.go
72 lines (69 loc) · 1.46 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
61
62
63
64
65
66
67
68
69
70
71
72
package hmac_test
import (
"encoding/hex"
"fmt"
"testing"
"github.com/go-utils/hmac"
)
func Test_verifier_Do(t *testing.T) {
t.Parallel()
secretKey := "this-is-sample"
mac := "66978174f0794c9a340a0b6ed3de35edf8d2b327a075d4bf576f6227d7b518a0"
id := "dab46a91-e732-4be3-acad-3ddb35295ff4"
timestamp := int64(1681327706)
type args struct {
message string
mac string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "正常",
args: args{
message: fmt.Sprintf("%s%d%d", id, 100, timestamp),
mac: mac,
},
},
{
name: "改竄済",
args: args{
message: fmt.Sprintf("%s%d%d", id, 999, timestamp),
mac: mac,
},
wantErr: true,
},
{
name: "改竄済",
args: args{
message: fmt.Sprintf("%s%d%d", "56617d18-cb1e-47ea-8d8b-e7300df89c91", 100, timestamp),
mac: mac,
},
wantErr: true,
},
{
name: "改竄済",
args: args{
message: fmt.Sprintf("%s%d%d", id, 100, 1681327707),
mac: mac,
},
wantErr: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
verifier := hmac.NewVerifier([]byte(secretKey))
decodedMac, err := hex.DecodeString(tt.args.mac)
if err != nil {
t.Fatalf("failed to hex decode mac: %v", err)
}
if err = verifier.Do([]byte(tt.args.message), decodedMac); (err != nil) != tt.wantErr {
t.Errorf("Do() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}