-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_test.go
More file actions
164 lines (152 loc) · 3.66 KB
/
Copy pathauth_test.go
File metadata and controls
164 lines (152 loc) · 3.66 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package vercelreceiver
import (
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestVerifySignature(t *testing.T) {
secret := []byte("test-secret")
body := []byte("test-body")
testCases := []struct {
name string
secret []byte
body []byte
signature string
expected bool
}{
{
name: "valid signature",
secret: secret,
body: body,
signature: createTestSignature(secret, body),
expected: true,
},
{
name: "invalid signature",
secret: secret,
body: body,
signature: "invalid-signature",
expected: false,
},
{
name: "missing signature",
secret: secret,
body: body,
signature: "",
expected: false,
},
{
name: "no secret configured",
secret: nil,
body: body,
signature: "",
expected: true, // Should pass when no secret
},
{
name: "empty secret",
secret: []byte(""),
body: body,
signature: "",
expected: true, // Should pass with empty secret
},
{
name: "wrong signature",
secret: secret,
body: body,
signature: createTestSignature([]byte("wrong-secret"), body),
expected: false,
},
{
name: "different body",
secret: secret,
body: []byte("different-body"),
signature: createTestSignature(secret, []byte("test-body")),
expected: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := verifySignature(tc.secret, tc.body, tc.signature, "sha1")
assert.Equal(t, tc.expected, result, "Signature verification result mismatch")
})
}
}
func TestVerifyRequest(t *testing.T) {
testCases := []struct {
name string
secret string
signature string
body []byte
expectedErr bool
errorMessage string
}{
{
name: "valid signature",
secret: "test-secret",
signature: createTestSignature([]byte("test-secret"), []byte("test-body")),
body: []byte("test-body"),
expectedErr: false,
},
{
name: "invalid signature",
secret: "test-secret",
signature: "invalid-signature",
body: []byte("test-body"),
expectedErr: true,
errorMessage: "invalid signature",
},
{
name: "missing signature header",
secret: "test-secret",
signature: "",
body: []byte("test-body"),
expectedErr: true,
errorMessage: "missing x-vercel-signature header",
},
{
name: "no secret configured",
secret: "",
signature: "",
body: []byte("test-body"),
expectedErr: false,
},
{
name: "empty secret skips verification",
secret: "",
signature: "",
body: []byte("test-body"),
expectedErr: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "http://localhost/test", nil)
if tc.signature != "" || tc.secret != "" {
req.Header.Set(xVercelSignatureHeader, tc.signature)
}
err := verifyRequest(req, tc.secret, tc.body, "sha1")
if tc.expectedErr {
require.Error(t, err)
if tc.errorMessage != "" {
assert.Contains(t, err.Error(), tc.errorMessage)
}
} else {
require.NoError(t, err)
}
})
}
}
func TestSignatureHeaderName(t *testing.T) {
// Test that the header name constant is correct
assert.Equal(t, "x-vercel-signature", xVercelSignatureHeader)
}
func createTestSignature(secret []byte, body []byte) string {
mac := hmac.New(sha1.New, secret)
mac.Write(body)
return hex.EncodeToString(mac.Sum(nil))
}