-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsessionid_test.go
More file actions
77 lines (62 loc) · 1.41 KB
/
sessionid_test.go
File metadata and controls
77 lines (62 loc) · 1.41 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
package sessions
import (
"crypto/rand"
"encoding/base64"
"fmt"
"testing"
)
const testSigningKey = "a very secret key"
func TestNewID(t *testing.T) {
sid, err := NewSessionID(testSigningKey)
if err != nil {
t.Fatal(err)
}
if 0 == len(sid) {
t.Errorf("Signed ID string was empty")
}
sid2, err := ValidateID(sid.String(), testSigningKey)
if nil != err {
fmt.Printf("generated: %v \n expected: %v\n", sid, sid2)
t.Fatal(err)
}
}
func TestInvalidKey(t *testing.T) {
sid, err := NewSessionID(testSigningKey)
if err != nil {
t.Fatal(err)
}
_, err = ValidateID(sid.String(), "some other signing key")
if nil == err {
t.Errorf("Was able to validate with incorrect signign key")
}
}
func TestModified(t *testing.T) {
sid, err := NewSessionID(testSigningKey)
if err != nil {
t.Fatal(err)
}
runes := []rune(sid.String())
runes[0]++
modsid := string(runes)
_, err = ValidateID(modsid, testSigningKey)
if nil == err {
t.Errorf("Was able to validate modified encoded string")
}
}
func TestEmptyID(t *testing.T) {
_, err := ValidateID("", testSigningKey)
if err == nil {
t.Error("Able to validate empty key")
}
}
func TestBadKey(t *testing.T) {
buf := make([]byte, signedLength)
if _, err := rand.Read(buf); nil != err {
t.Fatal(err)
}
badid := base64.URLEncoding.EncodeToString(buf)
_, err := ValidateID(badid, testSigningKey)
if err == nil {
t.Error("Able to validate bad key")
}
}