forked from justinas/nosurf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoken_test.go
72 lines (59 loc) · 1.7 KB
/
token_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
package nosurf
import (
"crypto/rand"
"testing"
)
func TestChecksForPRNG(t *testing.T) {
// Monkeypatch crypto/rand with an always-failing reader
oldReader := rand.Reader
rand.Reader = failReader{}
// Restore it later for other tests
defer func() {
rand.Reader = oldReader
}()
defer func() {
r := recover()
if r == nil {
t.Errorf("Expected checkForPRNG() to panic")
}
}()
checkForPRNG()
}
func TestGeneratesAValidToken(t *testing.T) {
// We can't test much with any certainity here,
// since we generate tokens randomly
// Basically we check that the length of the
// token is what it should be
token := generateToken()
l := len(token)
if l != tokenLength {
t.Errorf("Bad decoded token length: expected %d, got %d", tokenLength, l)
}
}
func TestVerifyTokenChecksLengthCorrectly(t *testing.T) {
for i := 0; i < 64; i++ {
slice := make([]byte, i)
result := verifyToken(slice, slice)
if result != false {
t.Errorf("verifyToken should've returned false with slices of length %d", i)
}
}
slice := make([]byte, 64)
result := verifyToken(slice[:32], slice)
if result != true {
t.Errorf("verifyToken should've returned true on a zeroed slice of length 64")
}
}
func TestVerifiesMaskedTokenCorrectly(t *testing.T) {
realToken := []byte("qwertyuiopasdfghjklzxcvbnm123456")
sentToken := []byte("qwertyuiopasdfghjklzxcvbnm123456" +
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
if !verifyToken(realToken, sentToken) {
t.Errorf("verifyToken returned a false negative")
}
realToken[0] = 'x'
if verifyToken(realToken, sentToken) {
t.Errorf("verifyToken returned a false positive")
}
}