-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook_test.go
More file actions
132 lines (105 loc) · 3.84 KB
/
Copy pathwebhook_test.go
File metadata and controls
132 lines (105 loc) · 3.84 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright (c) 2026 dexpace and Omar Aljarrah.
// Licensed under the MIT License. See LICENSE in the repository root for details.
package webhook_test
import (
"errors"
"strings"
"testing"
"time"
"github.com/dexpace/go-sdk/webhook"
)
func TestSignVerifyRoundTrip(t *testing.T) {
t.Parallel()
secret := []byte("s3cr3t")
payload := []byte(`{"event":"ping"}`)
sig := webhook.Sign(secret, payload)
v := webhook.NewVerifier(secret)
if err := v.Verify(payload, sig); err != nil {
t.Fatalf("Verify of a valid signature: %v", err)
}
if err := v.Verify([]byte(`{"event":"pong"}`), sig); !errors.Is(err, webhook.ErrSignatureMismatch) {
t.Fatalf("tampered payload err = %v, want ErrSignatureMismatch", err)
}
}
func TestVerifyWrongSecret(t *testing.T) {
t.Parallel()
payload := []byte("body")
sig := webhook.Sign([]byte("right"), payload)
v := webhook.NewVerifier([]byte("wrong"))
if err := v.Verify(payload, sig); !errors.Is(err, webhook.ErrSignatureMismatch) {
t.Fatalf("wrong secret err = %v, want ErrSignatureMismatch", err)
}
}
func TestVerifyBadHex(t *testing.T) {
t.Parallel()
v := webhook.NewVerifier([]byte("s"))
if err := v.Verify([]byte("body"), "not-hex!!"); !errors.Is(err, webhook.ErrSignatureMismatch) {
t.Fatalf("bad hex err = %v, want ErrSignatureMismatch (no panic)", err)
}
}
func TestVerifyTimestampWithinTolerance(t *testing.T) {
t.Parallel()
secret := []byte("s3cr3t")
body := []byte("payload")
ts := time.Unix(1_700_000_000, 0)
sig := webhook.Sign(secret, []byte("1700000000."+string(body)))
v := webhook.NewVerifier(secret)
if err := v.VerifyTimestamp(body, ts, ts, sig); err != nil {
t.Fatalf("VerifyTimestamp within tolerance: %v", err)
}
}
func TestVerifyTimestampOutsideTolerance(t *testing.T) {
t.Parallel()
secret := []byte("s3cr3t")
body := []byte("payload")
ts := time.Unix(1_700_000_000, 0)
sig := webhook.Sign(secret, []byte("1700000000."+string(body)))
v := webhook.NewVerifier(secret)
if err := v.VerifyTimestamp(body, ts, ts.Add(10*time.Minute), sig); !errors.Is(err, webhook.ErrTimestampOutsideTolerance) {
t.Fatalf("stale err = %v, want ErrTimestampOutsideTolerance", err)
}
if err := v.VerifyTimestamp(body, ts, ts.Add(-10*time.Minute), sig); !errors.Is(err, webhook.ErrTimestampOutsideTolerance) {
t.Fatalf("future err = %v, want ErrTimestampOutsideTolerance", err)
}
}
func TestVerifyTimestampZeroToleranceSkipsWindow(t *testing.T) {
t.Parallel()
secret := []byte("s3cr3t")
body := []byte("payload")
ts := time.Unix(1_700_000_000, 0)
sig := webhook.Sign(secret, []byte("1700000000."+string(body)))
v := webhook.NewVerifier(secret, webhook.WithTolerance(0))
if err := v.VerifyTimestamp(body, ts, ts.Add(48*time.Hour), sig); err != nil {
t.Fatalf("zero tolerance should skip the window: %v", err)
}
}
func TestVerifyTimestampSignatureMismatch(t *testing.T) {
t.Parallel()
secret := []byte("s3cr3t")
ts := time.Unix(1_700_000_000, 0)
sig := webhook.Sign(secret, []byte("1700000000.other"))
v := webhook.NewVerifier(secret)
if err := v.VerifyTimestamp([]byte("payload"), ts, ts, sig); !errors.Is(err, webhook.ErrSignatureMismatch) {
t.Fatalf("err = %v, want ErrSignatureMismatch", err)
}
}
func TestSignKnownVector(t *testing.T) {
t.Parallel()
// Canonical HMAC-SHA256 test vector:
// HMAC-SHA256("key", "The quick brown fox jumps over the lazy dog").
const want = "f7bc83f430538424b13298e6aa6fb143ef4d59a14946175997479dbc2d1a3cd8"
got := webhook.Sign([]byte("key"), []byte("The quick brown fox jumps over the lazy dog"))
if got != want {
t.Fatalf("Sign known vector = %q, want %q", got, want)
}
}
func TestSignOutputFormat(t *testing.T) {
t.Parallel()
sig := webhook.Sign([]byte("secret"), []byte("payload"))
if len(sig) != 64 {
t.Fatalf("Sign length = %d, want 64 hex chars", len(sig))
}
if sig != strings.ToLower(sig) {
t.Fatalf("Sign output %q is not lowercase hex", sig)
}
}