-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkce_test.go
945 lines (888 loc) · 23.9 KB
/
pkce_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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
package pkce
import (
"reflect"
"strings"
"testing"
)
func TestGenerateCodeChallenge(t *testing.T) {
tests := []struct {
name string
method Method
codeVerifier []byte
want string
shouldErr bool
wantErr error
}{
{
name: "should error on invalid length plain challenge",
method: Plain,
codeVerifier: []byte("yolo"),
want: "",
shouldErr: true,
wantErr: ErrVerifierLength,
},
{
name: "should error on invalid character plain challenge",
method: Plain,
codeVerifier: []byte(strings.Repeat("a", verifierMinLen) + "!"),
want: "",
shouldErr: true,
wantErr: ErrVerifierCharacters,
},
{
name: "should return valid plain code challenge",
method: Plain,
codeVerifier: []byte(strings.Repeat("a", verifierMinLen)),
want: strings.Repeat("a", verifierMinLen),
shouldErr: false,
},
{
name: "should error on invalid length S256 challenge",
method: S256,
codeVerifier: []byte("yolo"),
want: "",
shouldErr: true,
wantErr: ErrVerifierLength,
},
{
name: "should error on invalid character S256 challenge",
method: S256,
codeVerifier: []byte(strings.Repeat("a", verifierMinLen) + "!"),
want: "",
shouldErr: true,
wantErr: ErrVerifierCharacters,
},
{
name: "should return a valid, compliant computed S256 challenge (len: 43)",
method: S256,
codeVerifier: []byte("6et_m_LBa_8A-lHGANCGR0a6KATHyhr~5RU_CskUaaj"),
want: "1u1qURRaY4QPquG83Yu2fnyEYp4d0TLhXyj6AnaEcGQ",
shouldErr: false,
},
{
name: "should return a valid, compliant computed S256 challenge (len: 128)",
method: S256,
codeVerifier: []byte("-1Tumv7s3D22ko6Ejt-hHX6ly1xLrvIlLesIqJS5Nw-AiSJbSCO93FbLUVFvjkJXdD5slueEFS9ub~Oe~sIcylwuav31jLFxR~QDyPQAkgR2G1QOtIJPXQODLbTK61Hs"),
want: "EF-_M9nkOE6p88FdlYXUHkBv96MeV56C_Dsqk9DGlxw",
shouldErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GenerateCodeChallenge(tt.method, string(tt.codeVerifier))
if (err != nil) != tt.shouldErr {
t.Errorf("GenerateCodeChallenge() should error\ngot: %v, want: %v\n", err, tt.shouldErr)
}
if tt.shouldErr {
if tt.wantErr != err {
t.Errorf("GenerateCodeChallenge() error type not expected\ngot: %v, want: %v\n", err, tt.wantErr)
}
} else {
if got != tt.want {
t.Errorf("GenerateCodeChallenge() value length mismatch\ngot: %v, want: %v\n", got, tt.want)
}
}
})
}
}
func TestGenerateCodeVerifier(t *testing.T) {
tests := []struct {
name string
n int
shouldErr bool
wantErr error
}{
{
name: "should error if code verifier length is negative",
n: -10,
shouldErr: true,
wantErr: ErrVerifierLength,
},
{
name: "should error if code verifier length is less than RFC 7636's minimum",
n: verifierMinLen - 1,
shouldErr: true,
wantErr: ErrVerifierLength,
},
{
name: "should generate code verifier, minimum required length",
n: verifierMinLen,
shouldErr: false,
},
{
name: "should generate code verifier, minimum required length",
n: verifierMinLen,
shouldErr: false,
},
{
name: "should generate code verifier, length 75",
n: 75,
shouldErr: false,
},
{
name: "should generate code verifier, maximum required length",
n: verifierMaxLen,
shouldErr: false,
},
{
name: "should error if code verifier length is greater than RFC 7636's maximum",
n: verifierMaxLen + 1,
shouldErr: true,
wantErr: ErrVerifierLength,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GenerateCodeVerifier(tt.n)
if (err != nil) != tt.shouldErr {
t.Errorf("GenerateCodeVerifier() should error\ngot: %v, want: %v\n", err, tt.shouldErr)
}
if tt.shouldErr {
if tt.wantErr != err {
t.Errorf("GenerateCodeVerifier() error type not expected\ngot: %v, want: %v\n", err, tt.wantErr)
}
} else {
if len(got) != tt.n {
t.Errorf("GenerateCodeVerifier() value length mismatch\ngot: %v, want: %v\n", len(got), tt.n)
}
}
})
}
}
func TestKey_ChallengeMethod(t *testing.T) {
tests := []struct {
name string
challengeMethod Method
want Method
}{
{
name: "should return plain",
challengeMethod: Plain,
want: Plain,
},
{
name: "should return S256",
challengeMethod: S256,
want: S256,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k := &Key{
challengeMethod: tt.challengeMethod,
}
if got := k.ChallengeMethod(); got != tt.want {
t.Errorf("ChallengeMethod() = %v, want %v", got, tt.want)
}
})
}
}
type codeChallengeTest struct {
name string
method Method
codeVerifier []byte
want string
}
func codeChallengeTests() []codeChallengeTest {
return []codeChallengeTest{
{
name: "should return plain challenge",
method: Plain,
codeVerifier: []byte("yolo"),
want: "yolo",
},
{
name: "should return a valid, compliant computed S256 challenge (len: 43)",
method: S256,
codeVerifier: []byte("6et_m_LBa_8A-lHGANCGR0a6KATHyhr~5RU_CskUaaj"),
want: "1u1qURRaY4QPquG83Yu2fnyEYp4d0TLhXyj6AnaEcGQ",
},
{
name: "should return a valid, compliant computed S256 challenge (len: 128)",
method: S256,
codeVerifier: []byte("-1Tumv7s3D22ko6Ejt-hHX6ly1xLrvIlLesIqJS5Nw-AiSJbSCO93FbLUVFvjkJXdD5slueEFS9ub~Oe~sIcylwuav31jLFxR~QDyPQAkgR2G1QOtIJPXQODLbTK61Hs"),
want: "EF-_M9nkOE6p88FdlYXUHkBv96MeV56C_Dsqk9DGlxw",
},
}
}
func TestKey_CodeChallenge(t *testing.T) {
tests := codeChallengeTests()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k := &Key{
challengeMethod: tt.method,
codeVerifier: tt.codeVerifier,
}
if got := k.CodeChallenge(); got != tt.want {
t.Errorf("CodeChallenge() = %v, want %v", got, tt.want)
}
})
}
}
func TestKey_CodeVerifier(t *testing.T) {
tests := getCodeVerifierTests()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k := &Key{
codeVerifierLen: tt.codeVerifierLen,
codeVerifier: tt.codeVerifier,
}
got := k.CodeVerifier()
if tt.shouldGenerate {
if len(got) != tt.codeVerifierLen {
// test for expected generated length, as we can't perform an
// equality check on a random value!
t.Errorf(
"getCodeVerifier() expected length error\ngot: %v, want: %v",
len(got),
tt.codeVerifierLen,
)
}
} else {
if !reflect.DeepEqual(got, string(tt.wantCodeVerifier)) {
t.Errorf("getCodeVerifier() = %v, want %v", got, string(tt.wantCodeVerifier))
}
}
})
}
}
type setChallengeMethodTest struct {
name string
method Method
gotKey *Key
wantKey *Key
shouldErr bool
wantErr error
}
func setChallengeMethodTests() []setChallengeMethodTest {
return []setChallengeMethodTest{
{
name: "should set plain mode",
method: Plain,
gotKey: &Key{},
wantKey: &Key{
challengeMethod: Plain,
},
shouldErr: false,
},
{
name: "should set S256 mode",
method: S256,
gotKey: &Key{},
wantKey: &Key{
challengeMethod: S256,
},
shouldErr: false,
},
{
name: "should error on attempting to set a non-compliant method",
method: "not-a-specification-compliant-method",
gotKey: &Key{},
wantKey: &Key{},
shouldErr: true,
wantErr: ErrMethodNotSupported,
},
{
name: "should not overwrite challenge method with empty method",
method: "",
gotKey: &Key{
challengeMethod: S256,
},
wantKey: &Key{
challengeMethod: S256,
},
shouldErr: true,
wantErr: ErrMethodNotSupported,
},
}
}
func TestKey_SetChallengeMethod(t *testing.T) {
tests := setChallengeMethodTests()
tests = append(tests, setChallengeMethodTest{
name: "Should error on attempting downgrade from S256 to Plain",
method: Plain,
shouldErr: true,
wantErr: ErrMethodDowngrade,
gotKey: &Key{
challengeMethod: S256,
},
wantKey: &Key{
challengeMethod: S256,
},
})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.gotKey.SetChallengeMethod(tt.method)
if (err != nil) != tt.shouldErr {
t.Errorf("SetChallengeMethod() should error\ngot: %v, want: %v\n", err, tt.shouldErr)
}
if tt.shouldErr {
if tt.wantErr != err {
t.Errorf("SetChallengeMethod() error type not expected\ngot: %v, want: %v\n", err, tt.wantErr)
}
} else {
if !reflect.DeepEqual(tt.gotKey, tt.wantKey) {
t.Errorf("SetChallengeMethod() key\ngot: %v\nwant %v\n", tt.gotKey, tt.wantKey)
}
}
})
}
}
type verifyCodeVerifierTest struct {
name string
method Method
wantCodeVerifier string
codeVerifier string
codeChallenge string
want bool
}
func verifyCodeVerifierTests() []verifyCodeVerifierTest {
return []verifyCodeVerifierTest{
{
name: "should not verify invalid code challenge methods",
method: "not-a-method",
wantCodeVerifier: "6et_m_LBa_8A-lHGANCGR0a6KATHyhr~5RU_CskUaaj",
codeVerifier: "6et_m_LBa_8A-lHGANCGR0a6KATHyhr~5RU_CskUaaj",
codeChallenge: "6et_m_LBa_8A-lHGANCGR0a6KATHyhr~5RU_CskUaaj",
want: false,
},
{
name: "should not verify invalid plain code verifier",
method: Plain,
wantCodeVerifier: "6et_m_LBa_8A-lHGANCGR0a6KATHyhr~5RU_CskUaaj",
codeVerifier: "this-is-not-the-verifier-you-are-looking-for",
codeChallenge: "6et_m_LBa_8A-lHGANCGR0a6KATHyhr~5RU_CskUaaj",
want: false,
},
{
name: "should verify valid plain code verifier",
method: Plain,
wantCodeVerifier: "6et_m_LBa_8A-lHGANCGR0a6KATHyhr~5RU_CskUaaj",
codeVerifier: "6et_m_LBa_8A-lHGANCGR0a6KATHyhr~5RU_CskUaaj",
codeChallenge: "6et_m_LBa_8A-lHGANCGR0a6KATHyhr~5RU_CskUaaj",
want: true,
},
{
name: "should not verify non-matching S256 code verifier",
method: S256,
wantCodeVerifier: "6et_m_LBa_8A-lHGANCGR0a6KATHyhr~5RU_CskUaaj",
codeVerifier: "this-is-not-the-verifier-you-are-looking-for",
codeChallenge: "1u1qURRaY4QPquG83Yu2fnyEYp4d0TLhXyj6AnaEcGQ",
want: false,
},
{
name: "should not verify invalid length S256 code verifier",
method: S256,
wantCodeVerifier: "6et_m_LBa_8A-lHGANCGR0a6KATHyhr~5RU_CskUaaj",
codeVerifier: "not-the-verifier-you-are-looking-for",
codeChallenge: "1u1qURRaY4QPquG83Yu2fnyEYp4d0TLhXyj6AnaEcGQ",
want: false,
},
{
name: "should not verify invalid character S256 code verifier",
method: S256,
wantCodeVerifier: "6et_m_LBa_8A-lHGANCGR0a6KATHyhr~5RU_CskUaaj",
codeVerifier: "this-is-not-the-verifier-you-are-looking-for!",
codeChallenge: "1u1qURRaY4QPquG83Yu2fnyEYp4d0TLhXyj6AnaEcGQ",
want: false,
},
{
name: "should verify matching S256 code verifier",
method: S256,
wantCodeVerifier: "6et_m_LBa_8A-lHGANCGR0a6KATHyhr~5RU_CskUaaj",
codeVerifier: "6et_m_LBa_8A-lHGANCGR0a6KATHyhr~5RU_CskUaaj",
codeChallenge: "1u1qURRaY4QPquG83Yu2fnyEYp4d0TLhXyj6AnaEcGQ",
want: true,
},
}
}
func TestKey_VerifyCodeVerifier(t *testing.T) {
tests := verifyCodeVerifierTests()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k := &Key{
challengeMethod: tt.method,
codeVerifier: []byte(tt.wantCodeVerifier),
}
if got := k.VerifyCodeVerifier(tt.codeVerifier); got != tt.want {
t.Errorf("VerifyCodeVerifier() = %v, want %v", got, tt.want)
}
})
}
}
type getCodeVerifierTest struct {
name string
shouldGenerate bool
codeVerifierLen int
codeVerifier []byte
wantCodeVerifier []byte
}
func getCodeVerifierTests() []getCodeVerifierTest {
return []getCodeVerifierTest{
{
name: "should generate code verifier, if verifier is nil",
shouldGenerate: true,
codeVerifierLen: 45,
codeVerifier: nil,
wantCodeVerifier: nil,
},
{
name: "should generate code verifier, if verifier is empty",
shouldGenerate: true,
codeVerifierLen: 50,
codeVerifier: []byte{},
wantCodeVerifier: nil,
},
{
name: "should return set code verifier",
shouldGenerate: false,
codeVerifierLen: verifierMinLen,
codeVerifier: []byte(strings.Repeat("a", verifierMinLen)),
wantCodeVerifier: []byte(strings.Repeat("a", verifierMinLen)),
},
{
name: "should not generate if a code verifier if set",
shouldGenerate: false,
codeVerifierLen: 100,
codeVerifier: []byte(strings.Repeat("a", verifierMinLen)),
wantCodeVerifier: []byte(strings.Repeat("a", verifierMinLen)),
},
}
}
func TestKey_getCodeVerifier(t *testing.T) {
tests := getCodeVerifierTests()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k := &Key{
codeVerifierLen: tt.codeVerifierLen,
codeVerifier: tt.codeVerifier,
}
got := k.getCodeVerifier()
if tt.shouldGenerate {
if len(got) != tt.codeVerifierLen {
// test for expected generated length, as we can't perform an
// equality check on a random value!
t.Errorf(
"getCodeVerifier() expected length error\ngot: %v, want: %v",
len(got),
tt.codeVerifierLen,
)
}
} else {
if !reflect.DeepEqual(got, tt.wantCodeVerifier) {
t.Errorf("getCodeVerifier() = %v, want %v", got, tt.wantCodeVerifier)
}
}
})
}
}
type setCodeVerifierTest struct {
name string
codeVerifier []byte
gotKey *Key
wantKey *Key
shouldErr bool
wantErr error
}
func setCodeVerifierTests() []setCodeVerifierTest {
return []setCodeVerifierTest{
{
name: "should not set an empty verifier",
codeVerifier: []byte{},
gotKey: &Key{},
wantKey: &Key{},
shouldErr: true,
wantErr: ErrVerifierLength,
},
{
name: "should error configuring an invalid verifier length",
codeVerifier: []byte(strings.Repeat("a", verifierMinLen-1)),
gotKey: &Key{},
wantKey: &Key{},
shouldErr: true,
wantErr: ErrVerifierLength,
},
{
name: "should error configuring an invalid verifier character",
codeVerifier: []byte(strings.Repeat("a", verifierMinLen) + "!"),
gotKey: &Key{},
wantKey: &Key{},
shouldErr: true,
wantErr: ErrVerifierCharacters,
},
{
name: "should set a valid verifier",
codeVerifier: []byte(strings.Repeat("a", verifierMinLen)),
gotKey: &Key{},
wantKey: &Key{
codeVerifier: []byte(strings.Repeat("a", verifierMinLen)),
codeVerifierLen: verifierMinLen,
},
shouldErr: false,
},
{
name: "should overwrite code verifier length",
codeVerifier: []byte(strings.Repeat("a", verifierMinLen)),
gotKey: &Key{
codeVerifierLen: 100,
},
wantKey: &Key{
codeVerifier: []byte(strings.Repeat("a", verifierMinLen)),
codeVerifierLen: verifierMinLen,
},
shouldErr: false,
},
}
}
func TestKey_setCodeVerifier(t *testing.T) {
tests := setCodeVerifierTests()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.gotKey.setCodeVerifier(tt.codeVerifier)
if (err != nil) != tt.shouldErr {
t.Errorf("setCodeVerifier() should error\ngot: %v, want: %v\n", err, tt.shouldErr)
}
if tt.shouldErr {
if tt.wantErr != err {
t.Errorf("setCodeVerifier() error type not expected\ngot: %v, want: %v\n", err, tt.wantErr)
}
} else {
if !reflect.DeepEqual(tt.gotKey, tt.wantKey) {
t.Errorf("setCodeVerifier() key\ngot: %v\nwant %v\n", tt.gotKey, tt.wantKey)
}
}
})
}
}
type setCodeVerifierLengthTest struct {
name string
n int
gotKey *Key
wantKey *Key
shouldErr bool
wantErr error
}
func setCodeVerifierLengthTests() []setCodeVerifierLengthTest {
return []setCodeVerifierLengthTest{
{
name: "should not set a negative value",
n: -10,
gotKey: &Key{},
wantKey: &Key{},
shouldErr: true,
wantErr: ErrVerifierLength,
},
{
name: "should set a valid verifier length",
n: verifierMinLen,
gotKey: &Key{},
wantKey: &Key{codeVerifierLen: verifierMinLen},
shouldErr: false,
},
{
name: "should not overwrite codeVerifierLen with an invalid value",
n: -10,
gotKey: &Key{codeVerifierLen: verifierMinLen},
wantKey: &Key{codeVerifierLen: verifierMinLen},
shouldErr: true,
wantErr: ErrVerifierLength,
},
{
name: "should not overwrite code verifier length if code verifier is already set",
n: 75,
gotKey: &Key{
codeVerifier: []byte(strings.Repeat("a", verifierMinLen+1)),
codeVerifierLen: verifierMinLen + 1,
},
wantKey: &Key{
codeVerifier: []byte(strings.Repeat("a", verifierMinLen+1)),
codeVerifierLen: verifierMinLen + 1,
},
shouldErr: false,
},
}
}
func TestKey_setCodeVerifierLength(t *testing.T) {
tests := setCodeVerifierLengthTests()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.gotKey.setCodeVerifierLength(tt.n)
if (err != nil) != tt.shouldErr {
t.Errorf("setCodeVerifierLength() should error\ngot: %v, want: %v\n", err, tt.shouldErr)
}
if tt.shouldErr {
if tt.wantErr != err {
t.Errorf("setCodeVerifierLength() error type not expected\ngot: %v, want: %v\n", err, tt.wantErr)
}
} else {
if !reflect.DeepEqual(tt.gotKey, tt.wantKey) {
t.Errorf("setCodeVerifierLength() key\ngot: %v\nwant %v\n", tt.gotKey, tt.wantKey)
}
}
})
}
}
func TestMethod_String(t *testing.T) {
tests := []struct {
name string
m Method
want string
}{
{
name: "should return a methods value",
m: "not-a-spec-based-method",
want: "not-a-spec-based-method",
},
{
name: "should return plain value",
m: Plain,
want: "plain",
},
{
name: "should return S256 value",
m: S256,
want: "S256",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.m.String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
})
}
}
func TestNew(t *testing.T) {
type args struct {
opts []Option
}
tests := []struct {
name string
args args
wantKey *Key
shouldErr bool
}{
{
name: "should enforce verifier minimum length and S256 as a default",
args: args{
opts: nil,
},
wantKey: &Key{
challengeMethod: S256,
codeVerifierLen: verifierMinLen,
},
shouldErr: false,
},
{
name: "should error with invalid code challenge method via option WithChallengeMethod",
args: args{
opts: []Option{
WithChallengeMethod("not-a-method"),
},
},
wantKey: &Key{
challengeMethod: S256,
codeVerifierLen: verifierMinLen,
},
shouldErr: true,
},
{
name: "should set code challenge method via option WithChallengeMethod",
args: args{
opts: []Option{
WithChallengeMethod(Plain),
},
},
wantKey: &Key{
challengeMethod: Plain,
codeVerifierLen: verifierMinLen,
},
shouldErr: false,
},
{
name: "should error with invalid code verifier via option WithCodeVerifier",
args: args{
opts: []Option{
WithCodeVerifier([]byte("yolo")),
},
},
wantKey: &Key{
challengeMethod: S256,
codeVerifierLen: verifierMinLen,
},
shouldErr: true,
},
{
name: "should set code challenge method via option WithCodeVerifier",
args: args{
opts: []Option{
WithCodeVerifier([]byte(strings.Repeat("a", verifierMinLen))),
},
},
wantKey: &Key{
challengeMethod: S256,
codeVerifierLen: verifierMinLen,
codeVerifier: []byte(strings.Repeat("a", verifierMinLen)),
},
shouldErr: false,
},
{
name: "should error with invalid code verifier length with option WithCodeVerifierLength",
args: args{
opts: []Option{
WithCodeVerifierLength(150),
},
},
wantKey: &Key{
challengeMethod: S256,
codeVerifierLen: verifierMinLen,
},
shouldErr: true,
},
{
name: "should set code verifier length with option",
args: args{
opts: []Option{
WithCodeVerifierLength(100),
},
},
wantKey: &Key{
challengeMethod: S256,
codeVerifierLen: 100,
},
shouldErr: false,
},
{
name: "should set multiple options at once",
args: args{
opts: []Option{
WithChallengeMethod(Plain),
WithCodeVerifierLength(verifierMaxLen),
},
},
wantKey: &Key{
challengeMethod: Plain,
codeVerifierLen: verifierMaxLen,
},
shouldErr: false,
},
{
name: "should set all options at once",
args: args{
opts: []Option{
WithChallengeMethod(Plain),
WithCodeVerifierLength(verifierMaxLen),
WithCodeVerifier([]byte(strings.Repeat("a", verifierMinLen+1))),
},
},
wantKey: &Key{
challengeMethod: Plain,
codeVerifierLen: verifierMinLen + 1,
codeVerifier: []byte(strings.Repeat("a", verifierMinLen+1)),
},
shouldErr: false,
},
{
name: "should error if one of the provided options is invalid",
args: args{
opts: []Option{
WithChallengeMethod(Plain),
WithCodeVerifierLength(verifierMinLen + 20),
WithCodeVerifier([]byte(strings.Repeat("a", verifierMaxLen+1))),
},
},
wantKey: &Key{
challengeMethod: Plain,
codeVerifierLen: verifierMinLen + 20,
},
shouldErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotKey, err := New(tt.args.opts...)
if (err != nil) != tt.shouldErr {
t.Errorf("New() error = %v, shouldErr = %v", err, tt.shouldErr)
return
}
if !reflect.DeepEqual(gotKey, tt.wantKey) {
t.Errorf("New() gotKey = %v, want %v", gotKey, tt.wantKey)
}
})
}
}
func TestVerifyCodeVerifier(t *testing.T) {
tests := verifyCodeVerifierTests()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := VerifyCodeVerifier(tt.method, tt.codeVerifier, tt.codeChallenge); got != tt.want {
t.Errorf("VerifyCodeVerifier() = %v, want %v", got, tt.want)
}
})
}
}
func Test_generateCodeChallenge(t *testing.T) {
tests := codeChallengeTests()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotOut := generateCodeChallenge(tt.method, tt.codeVerifier); gotOut != tt.want {
t.Errorf("generateCodeChallenge() = %v, want %v", gotOut, tt.want)
}
})
}
}
func Test_generateCodeVerifier(t *testing.T) {
type args struct {
n int
}
tests := []struct {
name string
args args
}{
{
name: "should generate code verifier, minimum required length",
args: args{
n: verifierMinLen,
},
},
{
name: "should generate code verifier, length 75",
args: args{
n: 75,
},
},
{
name: "should generate code verifier, maximum required length",
args: args{
n: verifierMaxLen,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// since these should be truly random, we aren't able to compare
// values, but we can ensure all characters are valid and the
// requested generation length is valid
gotOut := generateCodeVerifier(tt.args.n)
if len(gotOut) != tt.args.n {
t.Errorf("generateCodeVerifier() should generate to specified length\ngot: %v\nwant: %v\n", len(gotOut), tt.args.n)
}
if err := validateCodeVerifier(gotOut); err != nil {
t.Errorf("generateCodeVerifier() should generate valid code verifiers\ngot: %s", string(gotOut))
}
})
}
}
func Test_generateCodeVerifier_randomness(t *testing.T) {
const numHashes = 10000
hashMap := map[string]struct{}{}
for i := 0; i < numHashes; i++ {
out := generateCodeVerifier(10)
v := string(out)
if _, ok := hashMap[v]; ok {
t.Error("randomness check failed")
t.FailNow()
}
hashMap[v] = struct{}{}
}
}