-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsecurecookie_test.go
1116 lines (1058 loc) · 30.8 KB
/
securecookie_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
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package securecookie
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/gorilla/securecookie"
)
func TestGenerateKeyErrors(t *testing.T) {
if _, err := GenerateRandomKey(); err != nil {
t.Errorf("unexpected error: %s", err)
}
forceError = 1
defer func() { forceError = 0 }()
if _, err := GenerateRandomKey(); err == nil {
t.Errorf("unexpected nil error")
}
}
func TestMustGenerateRandomKey(t *testing.T) {
key := MustGenerateRandomKey()
if len(key) != KeyLen {
t.Errorf("len of key is %d, expected %d", len(key), KeyLen)
}
forceError = 1
defer func() { forceError = 0 }()
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic")
}
}()
MustGenerateRandomKey()
}
func TestCheckName(t *testing.T) {
if err := checkName("toto"); err != nil {
t.Errorf("unexpected error: %s", err)
}
if err := checkName("TOTO"); err != nil {
t.Errorf("unexpected error: %s", err)
}
if err := checkName("?"); err == nil {
t.Errorf("unexpected nil error")
}
if err := checkName(""); err == nil {
t.Errorf("unexpected nil error")
}
if err := checkName("\t"); err == nil {
t.Errorf("unexpected nil error")
}
}
func TestCheckPath(t *testing.T) {
if err := checkPath("toto"); err != nil {
t.Errorf("unexpected error: %s", err)
}
if err := checkPath(";"); err == nil {
t.Errorf("unexpected nil error")
}
if err := checkPath("\t"); err == nil {
t.Errorf("unexpected nil error")
}
}
func TestCheckDomain(t *testing.T) {
if err := checkDomain(""); err != nil {
t.Errorf("unexpected error: %s", err)
}
if err := checkDomain("example.com"); err != nil {
t.Errorf("unexpected error: %s", err)
}
if err := checkDomain("EXAMPLE.com"); err != nil {
t.Errorf("unexpected error: %s", err)
}
if err := checkDomain("foo-bar.com"); err != nil {
t.Errorf("unexpected error: %s", err)
}
if err := checkDomain("www1.foo-bar.com"); err != nil {
t.Errorf("unexpected error: %s", err)
}
if err := checkDomain("192.168.1.1.example.com"); err != nil {
t.Errorf("unexpected error: %s", err)
}
if err := checkDomain(strings.Repeat("a", 300)); err == nil {
t.Errorf("unexpected nil error")
}
if err := checkDomain(strings.Repeat("a", 70) + ".com"); err == nil {
t.Errorf("unexpected nil error")
}
if err := checkDomain("example.com" + strings.Repeat("a", 70)); err == nil {
t.Errorf("unexpected nil error")
}
if err := checkDomain("?"); err == nil {
t.Errorf("unexpected nil error")
}
if err := checkDomain("\t"); err == nil {
t.Errorf("unexpected nil error")
}
if err := checkDomain("exàmple.com"); err == nil {
t.Errorf("unexpected nil error")
}
if err := checkDomain("www.\xbd\xb2.com"); err == nil {
t.Errorf("unexpected nil error")
}
if err := checkDomain("-example.com"); err == nil {
t.Errorf("unexpected nil error")
}
if err := checkDomain("example-.com"); err == nil {
t.Errorf("unexpected nil error")
}
if err := checkDomain("example.-com"); err == nil {
t.Errorf("unexpected nil error")
}
if err := checkDomain("example.com-"); err == nil {
t.Errorf("unexpected nil error")
}
if err := checkDomain("example.1com"); err == nil {
t.Errorf("unexpected nil error")
}
if err := checkDomain(".example.com"); err == nil {
t.Errorf("unexpected nil error")
}
if err := checkDomain("example..com"); err == nil {
t.Errorf("unexpected nil error")
}
if err := checkDomain("example.com."); err == nil {
t.Errorf("unexpected nil error")
}
}
func (o1 *Obj) Equal(o2 *Obj) bool {
if o1 == nil || o2 == nil {
return o1 == o2
}
return o1.name == o2.name && bytes.Equal(o1.key, o2.key) &&
o1.path == o2.path && o1.domain == o2.domain &&
o1.maxAge == o2.maxAge && o1.httpOnly == o2.httpOnly &&
o1.secure == o2.secure
}
func TestMustNew(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("expected panic")
}
}()
MustNew("test", nil, Params{})
}
func TestNew(t *testing.T) {
k := make([]byte, KeyLen)
kb := make([]byte, KeyLen*3)
n := "test"
tests := []struct {
k []byte
n string
p Params
o *Obj
fail bool
}{
{k: k, n: n, p: Params{}, o: &Obj{key: k, name: n}},
{k: k[:len(k)-1], n: n, p: Params{}, o: nil, fail: true},
{k: make([]byte, KeyLen+1), n: n, p: Params{}, o: nil, fail: true},
{k: k, n: "", p: Params{}, o: nil, fail: true},
{k: k, n: n, p: Params{Path: "path"}, o: &Obj{key: k, name: n, path: "path"}},
{k: k, n: n, p: Params{Path: ";"}, o: nil, fail: true},
{k: k, n: n, p: Params{Domain: "example.com"}, o: &Obj{key: k, name: n, domain: "example.com"}},
{k: k, n: n, p: Params{Domain: "example..com"}, o: nil, fail: true},
{k: k, n: n, p: Params{MaxAge: 3600}, o: &Obj{key: k, name: n, maxAge: 3600}},
{k: k, n: n, p: Params{MaxAge: -3600}, o: nil, fail: true},
{k: k, n: n, p: Params{HTTPOnly: true}, o: &Obj{key: k, name: n, httpOnly: true}},
{k: k, n: n, p: Params{Secure: true}, o: &Obj{key: k, name: n, secure: true}},
{k: kb, n: n, p: Params{Secure: true}, o: &Obj{key: kb, name: n, secure: true}, fail: true},
{k: k, n: n, p: Params{SameSite: None}, o: &Obj{key: k, name: n, sameSite: None}},
{k: k, n: n, p: Params{SameSite: Lax}, o: &Obj{key: k, name: n, sameSite: Lax}},
{k: k, n: n, p: Params{SameSite: Lax}, o: &Obj{key: k, name: n, sameSite: Strict}},
}
for _, test := range tests {
obj, err := New(test.n, test.k, test.p)
if err != nil && !test.fail {
t.Errorf("got error '%s', expected no error for %+v", err, test)
continue
}
if err == nil && test.fail {
t.Errorf("got nil error, expected to fail for %+v", test)
continue
}
if err != nil || test.fail {
continue
}
if !obj.Equal(test.o) {
t.Errorf("got object %+v, expected %+v for input %+v", *obj, *test.o, test)
}
}
}
func TestAccessorsMethods(t *testing.T) {
key := MustGenerateRandomKey()
name := "test"
params := Params{
Path: "path",
Domain: "example.com",
MaxAge: 3600,
HTTPOnly: true,
Secure: true,
SameSite: Strict,
}
obj, err := New(name, key, params)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if obj.Path() != params.Path {
t.Errorf("got path '%s', expected '%s'", obj.Path(), params.Path)
}
if obj.Domain() != params.Domain {
t.Errorf("got domain '%s', expected '%s'", obj.Domain(), params.Domain)
}
if obj.MaxAge() != params.MaxAge {
t.Errorf("got max age %d, expected %d", obj.MaxAge(), params.MaxAge)
}
if obj.HTTPOnly() != params.HTTPOnly {
t.Errorf("got HTTP only %t, expected %t", obj.HTTPOnly(), params.HTTPOnly)
}
if obj.Secure() != params.Secure {
t.Errorf("got secure %t, expected %t", obj.Secure(), params.Secure)
}
if obj.Name() != name {
t.Errorf("got name '%s', expected '%s'", obj.Name(), name)
}
if obj.SameSite() != Strict {
t.Errorf("got same site %d, expected Strict (%d)", obj.SameSite(), Strict)
}
}
func TestEncodeBase64(t *testing.T) {
tests := []struct {
in, out []byte
}{ // requires that len(in)%3 == 0
{in: []byte{}, out: []byte{}},
{in: []byte{0, 0, 0}, out: []byte{65, 65, 65, 65}},
{in: []byte{255, 255, 255}, out: []byte{95, 95, 95, 95}},
{in: []byte{0, 1, 2, 3, 4, 5}, out: []byte{65, 65, 69, 67, 65, 119, 81, 70}},
{in: []byte{3, 200, 254}, out: []byte{65, 56, 106, 45}},
} // out == base64.URLEncoding.WithPadding(base64.NoPadding).Encode(out, in)
for _, test := range tests {
var encLen = (len(test.in)*8 + 5) / 6
var buf = make([]byte, 0, encLen+10)
out := encodeBase64(buf, test.in)
if !bytes.Equal(out, test.out) {
t.Errorf("got base64 encoding output %v, expected %v for input %v", out, test.out, test.in)
continue
}
outStr := base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(test.in)
if !bytes.Equal([]byte(outStr), test.out) {
t.Errorf("got base64 encoding output %v, expected %v for input %v", out, test.out, test.in)
continue
}
}
}
func TestDecodeBase64(t *testing.T) {
tests := []struct {
in string
out []byte
fail bool
}{ // requires that len(in)%4 == 0
{in: "", out: []byte{}},
{in: "AAAA", out: []byte{0, 0, 0}},
{in: "____", out: []byte{255, 255, 255}},
{in: "AAECAwQF", out: []byte{0, 1, 2, 3, 4, 5}},
{in: "AAEC AwQF", out: nil, fail: true},
{in: "A8j-", out: []byte{3, 200, 254}},
{in: " A8j", out: nil, fail: true},
} // out == base64.URLEncoding.WithPadding(base64.NoPadding).Encode(out, in)
for _, test := range tests {
out, err := decodeBase64(nil, test.in)
if err != nil && !test.fail {
t.Errorf("got base64 decoding error '%s', expected no error", err)
continue
}
if err == nil && test.fail {
t.Errorf("got base64 decoding nil error, expected to fail")
continue
}
if test.fail || err != nil {
continue
}
if !bytes.Equal(out, test.out) {
t.Errorf("got base64 decoding output %v, expected %v for input '%v'", out, test.out, test.in)
}
stdOut, err := base64.URLEncoding.WithPadding(base64.NoPadding).DecodeString(test.in)
if err != nil {
t.Errorf("unexpected error for input '%s': %s", test.in, err)
continue
}
if !bytes.Equal(test.out, stdOut) {
t.Errorf("got base64 decoding output %v, expected %v for input '%v'", stdOut, test.out, test.in)
}
}
}
func bytes2Str(b []byte) string {
var out bytes.Buffer
out.Grow(len(b) * 6)
out.WriteByte('[')
for i := range b {
out.WriteString(fmt.Sprintf("0x%02X, ", b[i]))
}
res := out.Bytes()
res[len(res)-2] = ']'
return string(res[:len(res)-1])
}
func TestEncodedUint64(t *testing.T) {
tests := []struct {
in uint64
out []byte
}{
{in: 0x0000000000000001, out: []byte{0x01}},
{in: 0x0000000000000080, out: []byte{0x80, 0x01}},
{in: 0x0000000000004000, out: []byte{0x80, 0x80, 0x01}},
{in: 0x0002000000000000, out: []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01}},
{in: 0x8000000000000000, out: []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01}},
{in: 0xFFFFFFFFFFFFFFFF, out: []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01}},
}
for _, test := range tests {
out := make([]byte, 10)
out = out[:encodeUint64(out, test.in)]
if !bytes.Equal(out, test.out) {
t.Errorf("got %s, expected %s for %#X", bytes2Str(out), bytes2Str(test.out), test.in)
}
}
}
func TestDecodeUint64(t *testing.T) {
tests := []struct {
in []byte
out uint64
n int
}{
{out: 0x0000000000000001, in: []byte{0x01}, n: 1},
{out: 0x0000000000000080, in: []byte{0x80, 0x01}, n: 2},
{out: 0x0000000000004000, in: []byte{0x80, 0x80, 0x01}, n: 3},
{out: 0x0002000000000000, in: []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01}, n: 8},
{out: 0x8000000000000000, in: []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01}, n: 10},
{out: 0xFFFFFFFFFFFFFFFF, in: []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01}, n: 10},
{in: nil},
{in: []byte{}},
{in: []byte{0x80}},
{in: []byte{0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80}},
}
for _, test := range tests {
out, n := decodeUint64(test.in)
if n != test.n {
t.Errorf("got n %d, expected %d", n, test.n)
}
if out != test.out {
t.Errorf("got 0x%X, expected 0x%X for %s", out, test.out, bytes2Str(test.in))
}
}
}
func TestXorCtrAes(t *testing.T) {
var iv = make([]byte, aes.BlockSize)
var txt = []byte(strings.Repeat("test ", 10))
var ref = []byte(strings.Repeat("test ", 10))
var key = make([]byte, aes.BlockSize*2)
obj, err := New("name", key, Params{})
if err != nil {
t.Fatal("unexpected error:", err)
}
// cipher
obj.xorCtrAes(iv, txt)
// decipher
block, err := aes.NewCipher(key[len(key)/2:])
if err != nil {
t.Fatal("unexpected error:", err)
}
var stream = cipher.NewCTR(block, iv)
stream.XORKeyStream(txt, txt)
if !bytes.Equal(txt, ref) {
t.Errorf("ctr aes ciphering error")
}
}
func TestHmacSha256(t *testing.T) {
var txt = []byte(strings.Repeat("test ", 10))
var key = make([]byte, aes.BlockSize*2)
obj, err := New("name", key, Params{})
if err != nil {
t.Fatal("unexpected error:", err)
}
var mac1 = make([]byte, hmacLen)
// ipad must be prepended to the txt to hash
var tmp = make([]byte, sha256.BlockSize+len(txt))
copy(tmp[copy(tmp, obj.ipad[:]):], txt)
var n = obj.hmacSha256(mac1, tmp)
if n != len(mac1) {
t.Fatalf("got n %d, expected %d", n, len(mac1))
}
var mac = hmac.New(sha256.New, key[:len(key)/2])
mac.Write(txt)
var mac2 = mac.Sum(nil)
if !bytes.Equal(mac1, mac2) {
t.Errorf("got mac: \n%s\n expected mac: \n%s", bytes2Str(mac1), bytes2Str(mac2))
}
}
func TestEncodeDecodeValue(t *testing.T) {
tests := []struct {
in []byte
encFail, decFail bool
}{
{in: []byte{}},
{in: []byte{0}},
{in: []byte{0, 0, 0}},
{in: []byte{255, 255, 255}},
{in: []byte{0, 1, 2, 3, 4}},
{in: []byte{0, 1, 2, 3, 4, 5}},
{in: []byte{3, 200, 254}},
}
obj, err := New("test", make([]byte, KeyLen), Params{MaxAge: 3600})
if err != nil {
panic(err)
}
for _, test := range tests {
out, err := obj.encodeValue(nil, test.in)
if (err == nil) == test.encFail {
if err == nil {
t.Errorf("got nil encoding error, expected error for in: %+v", test.in)
} else {
t.Errorf("got encoding error '%s', expected nil error for in: %+v", err, test.in)
}
} else {
in, err := obj.decodeValue(nil, string(out))
if (err == nil) == test.decFail {
if err == nil {
t.Errorf("got nil decoding error, expected failure for in: %+v", test.in)
} else {
t.Errorf("got decoding error '%s', expected nil error for in: %+v", err, test.in)
}
} else {
if !bytes.Equal(in, test.in) {
t.Errorf("encoding->decoding mismatch")
}
}
}
}
}
func TestEncodeValueErrors(t *testing.T) {
obj, err := New("test", make([]byte, KeyLen), Params{})
if err != nil {
panic(err)
}
buf := make([]byte, 0, 59)
forceError = 2
defer func() { forceError = 0 }()
if _, err := obj.encodeValue(buf, []byte{1}); err == nil {
t.Errorf("unexpected nil error")
}
forceError = 1
if _, err := obj.encodeValue(buf, []byte{}); err == nil {
t.Errorf("unexpected nil error")
}
}
func purgeBufPool() {
for {
var bPtr = bufPool.Get().(*[]byte)
if len(*bPtr) == 0 {
break
}
}
}
func TestDecodeValueErrorsA(t *testing.T) {
obj, err := New("test", make([]byte, KeyLen), Params{MaxAge: 3600})
if err != nil {
t.Fatal(err)
}
if _, err := obj.decodeValue(nil, ""); err == nil {
t.Errorf("unexpected nil error")
}
if _, err := obj.decodeValue(nil, " "+strings.Repeat("A", minEncLen)); err == nil {
t.Errorf("unexpected nil error")
}
if _, err := obj.decodeValue(nil, "zA"+strings.Repeat("A", minEncLen)); err == nil {
t.Errorf("unexpected nil error")
}
buf, err := obj.encodeValue(nil, []byte{})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
buf[len(buf)-1]--
if _, err := obj.decodeValue(nil, string(buf)); err == nil {
t.Errorf("unexpected nil error")
}
buf[len(buf)-1]++
obj.maxAge -= 10000
if _, err := obj.decodeValue(nil, string(buf)); err == nil {
t.Errorf("unexpected nil error")
}
obj.maxAge += 10000
}
func TestDecodeValueErrorsB(t *testing.T) {
obj, err := New("test", make([]byte, KeyLen), Params{MaxAge: 3600})
if err != nil {
t.Fatal(err)
}
buf, err := obj.encodeValue(nil, []byte{})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
// set an invalid paddingLen value
dec, err := decodeBase64(nil, string(buf))
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
dec[0] |= 3
dec = dec[:len(dec)-hmacLen]
var hm = hmac.New(sha256.New, obj.key[:len(obj.key)/2])
hm.Write([]byte(obj.name))
hm.Write(dec)
dec = hm.Sum(dec)
buf = encodeBase64(buf[:0], dec)
if _, err := obj.decodeValue(nil, string(buf)); err == nil {
t.Errorf("unexpected nil error")
}
// set an invalid stamp encoding
buf, err = obj.encodeValue(nil, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
dec, err = decodeBase64(nil, string(buf))
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
dec = dec[:len(dec)-hmacLen]
obj.xorCtrAes(dec[1:1+ivLen], dec[1+ivLen:])
for i := 1 + ivLen; i < 1+ivLen+10; i++ {
dec[i] |= 0x80
}
obj.xorCtrAes(dec[1:1+ivLen], dec[1+ivLen:])
hm.Reset()
hm.Write([]byte(obj.name))
hm.Write(dec)
dec = hm.Sum(dec)
buf = encodeBase64(buf[:0], dec)
purgeBufPool()
if _, err := obj.decodeValue(nil, string(buf)); err == nil {
t.Errorf("unexpected nil error")
}
}
func TestDecodeValueErrorsC(t *testing.T) {
obj, err := New("test", make([]byte, KeyLen), Params{})
if err != nil {
t.Fatal(err)
}
if _, err := obj.decodeValue(nil, ""); err == nil {
t.Errorf("unexpected nil error")
}
if _, err := obj.decodeValue(nil, " "+strings.Repeat("A", minEncLen)); err == nil {
t.Errorf("unexpected nil error")
}
if _, err := obj.decodeValue(nil, "zA"+strings.Repeat("A", minEncLen)); err == nil {
t.Errorf("unexpected nil error")
}
buf, err := obj.encodeValue(nil, []byte{})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
buf[len(buf)-1]--
if _, err := obj.decodeValue(nil, string(buf)); err == nil {
t.Errorf("unexpected nil error")
}
buf[len(buf)-1]++
if _, err := obj.decodeValue(nil, string(buf)); err != nil {
t.Errorf("unexpected error: %s", err)
}
}
func TestDecodeValueAndStampErrorsA(t *testing.T) {
obj, err := New("test", make([]byte, KeyLen), Params{MaxAge: 3600})
if err != nil {
t.Fatal(err)
}
if _, _, err := obj.decodeValueAndStamp(nil, ""); err == nil {
t.Errorf("unexpected nil error")
}
if _, _, err := obj.decodeValueAndStamp(nil, " "+strings.Repeat("A", minEncLen)); err == nil {
t.Errorf("unexpected nil error")
}
if _, _, err := obj.decodeValueAndStamp(nil, "zA"+strings.Repeat("A", minEncLen)); err == nil {
t.Errorf("unexpected nil error")
}
buf, err := obj.encodeValue(nil, []byte{})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
buf[len(buf)-1]--
if _, _, err := obj.decodeValueAndStamp(nil, string(buf)); err == nil {
t.Errorf("unexpected nil error")
}
buf[len(buf)-1]++
obj.maxAge -= 10000
if _, _, err := obj.decodeValueAndStamp(nil, string(buf)); err == nil {
t.Errorf("unexpected nil error")
}
obj.maxAge += 10000
}
func TestDecodeValueAndStampErrorsB(t *testing.T) {
obj, err := New("test", make([]byte, KeyLen), Params{MaxAge: 3600})
if err != nil {
t.Fatal(err)
}
buf, err := obj.encodeValue(nil, []byte{})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
// set an invalid paddingLen value
dec, err := decodeBase64(nil, string(buf))
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
dec[0] |= 3
dec = dec[:len(dec)-hmacLen]
var hm = hmac.New(sha256.New, obj.key[:len(obj.key)/2])
hm.Write([]byte(obj.name))
hm.Write(dec)
dec = hm.Sum(dec)
buf = encodeBase64(buf[:0], dec)
if _, _, err := obj.decodeValueAndStamp(nil, string(buf)); err == nil {
t.Errorf("unexpected nil error")
}
// set an invalid stamp encoding
buf, err = obj.encodeValue(nil, []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
dec, err = decodeBase64(nil, string(buf))
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
dec = dec[:len(dec)-hmacLen]
obj.xorCtrAes(dec[1:1+ivLen], dec[1+ivLen:])
for i := 1 + ivLen; i < 1+ivLen+10; i++ {
dec[i] |= 0x80
}
obj.xorCtrAes(dec[1:1+ivLen], dec[1+ivLen:])
hm.Reset()
hm.Write([]byte(obj.name))
hm.Write(dec)
dec = hm.Sum(dec)
buf = encodeBase64(buf[:0], dec)
purgeBufPool()
if _, _, err := obj.decodeValueAndStamp(nil, string(buf)); err == nil {
t.Errorf("unexpected nil error")
}
}
func TestDecodeValueAndStampErrorsC(t *testing.T) {
obj, err := New("test", make([]byte, KeyLen), Params{})
if err != nil {
t.Fatal(err)
}
if _, _, err := obj.decodeValueAndStamp(nil, ""); err == nil {
t.Errorf("unexpected nil error")
}
if _, _, err := obj.decodeValueAndStamp(nil, " "+strings.Repeat("A", minEncLen)); err == nil {
t.Errorf("unexpected nil error")
}
if _, _, err := obj.decodeValueAndStamp(nil, "zA"+strings.Repeat("A", minEncLen)); err == nil {
t.Errorf("unexpected nil error")
}
buf, err := obj.encodeValue(nil, []byte{})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
buf[len(buf)-1]--
if _, _, err := obj.decodeValueAndStamp(nil, string(buf)); err == nil {
t.Errorf("unexpected nil error")
}
buf[len(buf)-1]++
if _, _, err := obj.decodeValueAndStamp(nil, string(buf)); err != nil {
t.Errorf("unexpected error: %s", err)
}
}
func TestSetAndGetCookie(t *testing.T) {
p := Params{
Path: "path",
Domain: "example.com",
MaxAge: 3600,
HTTPOnly: true,
Secure: true,
}
obj, err := New("test", make([]byte, KeyLen), p)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
recorder := httptest.NewRecorder()
inValue := []byte("some value")
err = obj.SetValue(recorder, inValue)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
request := &http.Request{Header: http.Header{"Cookie": recorder.Header().Values("Set-Cookie")}}
outValue, err := obj.GetValue(nil, request)
if err != nil {
t.Errorf("unexpected error: %s", err)
} else if !bytes.Equal(outValue, inValue) {
t.Errorf("got value '%s', expected '%s'", outValue, inValue)
}
// test retrieve non-existent cookie.
obj.name = "xxx"
outValue, err = obj.GetValue(nil, request)
if err == nil {
t.Errorf("unexpected nil error")
} else if outValue != nil {
t.Errorf("unexpected non-nil value")
}
// force too big cookie error
recorder = httptest.NewRecorder()
err = obj.SetValue(recorder, []byte(strings.Repeat(" ", maxCookieLen)))
if err == nil {
t.Errorf("unexpected nil error")
}
// force encoding error
forceError = 1
defer func() { forceError = 0 }()
recorder = httptest.NewRecorder()
err = obj.SetValue(recorder, []byte(strings.Repeat(" ", maxCookieLen)))
if err == nil {
t.Errorf("unexpected nil error")
}
}
func TestSetAndGetCookieWithStamp(t *testing.T) {
p := Params{
Path: "path",
Domain: "example.com",
MaxAge: 3600,
HTTPOnly: true,
Secure: true,
}
obj, err := New("test", make([]byte, KeyLen), p)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
recorder := httptest.NewRecorder()
inValue := []byte("some value")
now := time.Now().Truncate(time.Second)
err = obj.SetValue(recorder, inValue)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
request := &http.Request{Header: http.Header{"Cookie": recorder.Header().Values("Set-Cookie")}}
outValue, stamp, err := obj.GetValueAndStamp(nil, request)
if err != nil {
t.Errorf("unexpected error: %s", err)
} else if !bytes.Equal(outValue, inValue) {
t.Errorf("got value '%s', expected '%s'", outValue, inValue)
}
if stampDiff := stamp.Sub(now); stampDiff > time.Second || stampDiff < -time.Second {
t.Error("invalid stamp diff:", stampDiff)
}
// test retrieve non-existent cookie.
obj.name = "xxx"
outValue, _, err = obj.GetValueAndStamp(nil, request)
if err == nil {
t.Errorf("unexpected nil error")
} else if outValue != nil {
t.Errorf("unexpected non-nil value")
}
}
func TestSetAndGetCookiesWithStamp(t *testing.T) {
p := Params{
Path: "path",
Domain: "example.com",
MaxAge: 3600,
HTTPOnly: true,
Secure: true,
}
obj, err := New("test", make([]byte, KeyLen), p)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
recorder := httptest.NewRecorder()
inValue := []byte("some value")
now := time.Now().Truncate(time.Second)
err = obj.SetValue(recorder, inValue)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
inValue2 := []byte("other value")
err = obj.SetValue(recorder, inValue2)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
request := &http.Request{Header: http.Header{"Cookie": recorder.Header().Values("Set-Cookie")}}
outValues := obj.GetValues(request.Cookies())
if exp, got := 2, len(outValues); exp != got {
t.Fatalf("expected %d values, got %d", exp, got)
}
if !bytes.Equal(outValues[0].Value, inValue) {
t.Errorf("expected %s, got %s", string(inValue), string(outValues[0].Value))
}
if !bytes.Equal(outValues[1].Value, inValue2) {
t.Errorf("expected %s, got %s", string(inValue2), string(outValues[1].Value))
}
if stampDiff := outValues[0].Stamp.Sub(now); stampDiff > time.Second || stampDiff < -time.Second {
t.Error("invalid stamp diff:", stampDiff)
}
}
func TestDeleteCookie(t *testing.T) {
purgeBufPool()
p := Params{
Path: "path",
Domain: "example.com",
MaxAge: 3600,
HTTPOnly: true,
Secure: true,
SameSite: None,
}
obj, err := New("test", make([]byte, KeyLen), p)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
recorder := httptest.NewRecorder()
err = obj.Delete(recorder)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
request := &http.Request{Header: http.Header{"Cookie": recorder.Header().Values("Set-Cookie")}}
c, err := request.Cookie("test")
if err != nil {
t.Errorf("unexpected error: %s", err)
} else {
if len(c.Value) != 0 {
t.Errorf("got value '%s', expected empty string", c.Value)
}
}
p.SameSite = Lax
obj, err = New("test", make([]byte, KeyLen), p)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
recorder = httptest.NewRecorder()
err = obj.Delete(recorder)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
request = &http.Request{Header: http.Header{"Cookie": recorder.Header().Values("Set-Cookie")}}
c, err = request.Cookie("test")
if err != nil {
t.Errorf("unexpected error: %s", err)
} else {
if len(c.Value) != 0 {
t.Errorf("got value '%s', expected empty string", c.Value)
}
}
p.SameSite = Strict
obj, err = New("test", make([]byte, KeyLen), p)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
recorder = httptest.NewRecorder()
err = obj.Delete(recorder)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
request = &http.Request{Header: http.Header{"Cookie": recorder.Header().Values("Set-Cookie")}}
c, err = request.Cookie("test")
if err != nil {
t.Errorf("unexpected error: %s", err)
} else {
if len(c.Value) != 0 {
t.Errorf("got value '%s', expected empty string", c.Value)
}
}
}
func TestChmikeValueLen(t *testing.T) {
var name = "test"
var inValue = []byte("some value")
var recorder = httptest.NewRecorder()
var key = make([]byte, KeyLen)
obj, err := New(name, key, Params{
Path: "path",
Domain: "example.com",
MaxAge: 3600,
HTTPOnly: true,
Secure: true,
})
if err != nil {
panic(err)
}
err = obj.SetValue(recorder, inValue)
if err != nil {
panic(err)
}
request := &http.Request{Header: http.Header{"Cookie": recorder.Header().Values("Set-Cookie")}}
c, err := request.Cookie("test")
if err != nil {
t.Fatal(err)
}
fmt.Println("Chmike value:", c.Value, "len:", len(c.Value))
}
func TestGorillaValueLen(t *testing.T) {
var name = "test"
var inValue = []byte("some value")
var recorder = httptest.NewRecorder()
var hashKey = make([]byte, 16)
var blockKey = make([]byte, 16)
var s = securecookie.New(hashKey, blockKey)
var cookie = http.Cookie{Name: name}
encoded, err := s.Encode(name, inValue)
if err != nil {
panic(err)
}
cookie.Value = encoded
http.SetCookie(recorder, &cookie)
request := &http.Request{Header: http.Header{"Cookie": recorder.Header().Values("Set-Cookie")}}
c, err := request.Cookie("test")
if err != nil {
t.Fatal(err)
}
fmt.Println("Gorilla value:", c.Value, "len:", len(c.Value))
}
func TestCookieText(t *testing.T) {
key := MustGenerateRandomKey()
loginCookie, err := New("login", key, Params{
Path: "/", // cookie received only when URL starts with this path
Domain: "www.example.com", // cookie received only when URL domain matches this one
MaxAge: 15 * 24 * 3600, // cookie becomes invalid after 15 days
HTTPOnly: true, // disallow access by remote javascript code
Secure: true, // cookie received only with HTTPS, never with HTTP
SameSite: Strict, // cookie only for same site
})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
var recorder = httptest.NewRecorder()
if err := loginCookie.SetValue(recorder, []byte{}); err != nil {
t.Fatal("unexpected error:", err)
}
hdr := recorder.Header()
t.Error(hdr.Get("Set-Cookie"))
}
var buf1 = make([]byte, 512)
var buf2 = make([]byte, 512)
var val = []byte("some value")
var obj = MustNew("test", make([]byte, KeyLen), Params{MaxAge: 3600})
var enc string
func BenchmarkChmikeEncodeValue(b *testing.B) {
for n := 0; n < b.N; n++ {
obj.encodeValue(buf1[:0], val)
}
}
func init() {
b1, err := obj.encodeValue(buf1[:0], val)
if err != nil {