-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation_test.go
288 lines (270 loc) · 6.28 KB
/
validation_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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package pkce
import (
"crypto/rand"
"fmt"
"strings"
"testing"
)
func Test_validateCodeVerifier(t *testing.T) {
type args struct {
verifier []byte
}
tests := []struct {
name string
args args
shouldErr bool
wantErr error
}{
{
name: "should error if verifier is nil",
args: args{
verifier: nil,
},
shouldErr: true,
wantErr: ErrVerifierLength,
},
{
name: "should error if verifier is empty",
args: args{
verifier: []byte{},
},
shouldErr: true,
wantErr: ErrVerifierLength,
},
{
name: "should error if verifier is too short",
args: args{
verifier: []byte(strings.Repeat("a", verifierMinLen-1)),
},
shouldErr: true,
wantErr: ErrVerifierLength,
},
{
name: "should error if verifier is too long",
args: args{
verifier: []byte(strings.Repeat("a", verifierMaxLen+1)),
},
shouldErr: true,
wantErr: ErrVerifierLength,
},
{
name: "should error with invalid characters (ascii)",
args: args{
verifier: []byte(strings.Repeat("a", verifierMinLen-1) + "!"),
},
shouldErr: true,
wantErr: ErrVerifierCharacters,
},
{
name: "should error with invalid characters (utf-8)",
args: args{
verifier: []byte("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa💩"),
},
shouldErr: true,
wantErr: ErrVerifierCharacters,
},
{
name: "should error with invalid characters (random byte array)",
args: args{
verifier: randomBytes(t, 100),
},
shouldErr: true,
wantErr: ErrVerifierCharacters,
},
{
name: "should pass with a short verifier",
args: args{
verifier: []byte(strings.Repeat("a", verifierMinLen)),
},
shouldErr: false,
},
{
name: "should pass with long verifier",
args: args{
verifier: []byte(strings.Repeat("a", verifierMaxLen)),
},
shouldErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateCodeVerifier(tt.args.verifier)
if (err != nil) != tt.shouldErr {
t.Errorf("validateCodeVerifier() should have error\ngot: %v\nwant: %v\n", err, tt.shouldErr)
}
if (err != nil) && tt.wantErr != err {
t.Errorf("validateCodeVerifier() expected error\ngot: %v\nwant: %v\n", err, tt.wantErr)
}
})
}
}
func Test_validateVerifierLen(t *testing.T) {
type args struct {
n int
}
tests := []struct {
name string
args args
shouldErr bool
wantErr error
}{
{
name: "should error if value is negative",
args: args{
n: -1,
},
shouldErr: true,
wantErr: ErrVerifierLength,
},
{
name: fmt.Sprintf("should error if length is smaller than min length (%d)", verifierMinLen),
args: args{
n: verifierMinLen - 1,
},
shouldErr: true,
wantErr: ErrVerifierLength,
},
{
name: fmt.Sprintf("should consider min length + 1 (%d) valid", verifierMinLen+1),
args: args{
n: verifierMinLen + 1,
},
shouldErr: false,
},
{
name: fmt.Sprintf("should consider max length - 1 (%d) valid", verifierMaxLen-1),
args: args{
n: verifierMinLen + 1,
},
shouldErr: false,
},
{
name: fmt.Sprintf("should error if length is greater than max length (%d)", verifierMaxLen),
args: args{
n: verifierMaxLen + 1,
},
shouldErr: true,
wantErr: ErrVerifierLength,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateVerifierLen(tt.args.n)
if (err != nil) != tt.shouldErr {
t.Errorf("validateVerifierLen() should have error\ngot: %v\nwant: %v\n", err, tt.shouldErr)
}
if (err != nil) && tt.wantErr != err {
t.Errorf("validateVerifierLen() expected error\ngot: %v\nwant: %v\n", err, tt.wantErr)
}
})
}
}
func Test_validateCodeVerifierCharacters(t *testing.T) {
type args struct {
chars []byte
}
tests := []struct {
name string
args args
shouldErr bool
wantErr error
}{
{
name: "should error with invalid characters (ascii)",
args: args{
chars: []byte("abc123!abc123"),
},
shouldErr: true,
wantErr: ErrVerifierCharacters,
},
{
name: "should error with invalid characters (utf-8)",
args: args{
chars: []byte("XÆA-Xii"),
},
shouldErr: true,
wantErr: ErrVerifierCharacters,
},
{
name: "should error with invalid characters (random byte array)",
args: args{
chars: randomBytes(t, 100),
},
shouldErr: true,
wantErr: ErrVerifierCharacters,
},
{
name: "should pass with valid characters",
args: args{
chars: []byte(unreserved),
},
shouldErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateCodeVerifierCharacters(tt.args.chars)
if (err != nil) != tt.shouldErr {
t.Errorf("validateCodeVerifierCharacters() should have error\ngot: %v\nwant: %v\n", err, tt.shouldErr)
}
if (err != nil) && tt.wantErr != err {
t.Errorf("validateCodeVerifierCharacters() expected error\ngot: %v\nwant: %v\n", err, tt.wantErr)
}
})
}
}
func Test_validVerifierChar(t *testing.T) {
type args struct {
c byte
}
type validVerifierCharTest struct {
name string
args args
want bool
}
tests := []validVerifierCharTest{}
for _, char := range []byte(unreserved) {
tests = append(tests, validVerifierCharTest{
name: fmt.Sprintf("should consider ASCII reserved character '%c' valid", char),
args: args{
c: char,
},
want: true,
})
}
for _, char := range []byte("`!@#$%^&*()+={}[]|\\/:;<>,?") {
tests = append(tests, validVerifierCharTest{
name: fmt.Sprintf("should consider ASCII character '%c' invalid", char),
args: args{
c: char,
},
want: false,
})
}
// given UTF-8 chunks overflows a byte, we can test against a possible,
// albeit extremely weird, vector.
for _, char := range []byte("💩🐶あ") {
tests = append(tests, validVerifierCharTest{
name: "should consider UTF-8 characters invalid",
args: args{
c: char,
},
want: false,
})
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := validVerifierChar(tt.args.c); got != tt.want {
t.Errorf("validVerifierChar() = %v, want %v", got, tt.want)
}
})
}
}
func randomBytes(t *testing.T, n int) (randomBytes []byte) {
randomBytes = make([]byte, n)
_, err := rand.Read(randomBytes)
if err != nil {
t.Error("unable to generate random byte stream:", err)
}
return
}