-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgenerator_test.go
139 lines (121 loc) · 3.5 KB
/
generator_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
package fireauth
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"reflect"
"strings"
"testing"
)
func TestNew(t *testing.T) {
if gen := New("foo"); gen == nil {
t.Fatal("generator should not be nil")
}
}
func TestCreateTokenData(t *testing.T) {
gen := New("foo")
data := Data{"uid": "1"}
token, err := gen.CreateToken(data, nil)
if err != nil {
t.Fatal(err)
}
tokenParts := strings.Split(token, TokenSep)
if len(tokenParts) != 3 {
t.Fatal("token is not composed correctly")
}
bytes, err := base64.URLEncoding.DecodeString(tokenParts[1] + "==")
if err != nil {
t.Fatal(err)
}
claim := struct {
Version int `json:"v"`
Data Data `json:"d"`
IssuedAt int64 `json:"iat"`
}{}
if err := json.Unmarshal(bytes, &claim); err != nil {
t.Fatal(err)
}
if claim.Version != Version {
t.Fatalf("Expected: %d\nActual: %d", Version, claim.Version)
}
if !reflect.DeepEqual(data, claim.Data) {
t.Fatalf("auth data is not the same.Expected: %s\nActual: %s", data, claim.Data)
}
}
func TestCreateTokenFailure(t *testing.T) {
if _, err := New("foo").CreateToken(nil, nil); err == nil {
t.Fatal("CreateToken without data nor option should fail")
}
if _, err := New("foo").CreateToken(Data{}, nil); err == nil {
t.Fatal("CreateToken with invalid data should fail")
}
ch := make(chan struct{})
defer close(ch)
if _, err := New("foo").CreateToken(Data{"uid": "1234", "invalid": ch}, &Option{}); err == nil {
t.Fatal("Invalid data types should make the token creation fail")
}
}
func TestCreateTokenAdminNoData(t *testing.T) {
if _, err := New("foo").CreateToken(nil, &Option{Admin: true}); err != nil {
t.Fatal(err)
}
}
func TestCreateTokenTooLong(t *testing.T) {
if _, err := New("foo").CreateToken(Data{"uid": "1", "bigKey": randData(t, 1024)}, nil); err == nil {
t.Fatal("Token too long should have failed")
}
}
func randData(t *testing.T, size int) string {
alphanum := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, size)
if _, err := rand.Read(bytes); err != nil {
t.Fatal(err)
}
for i, b := range bytes {
bytes[i] = alphanum[b%byte(len(alphanum))]
}
return string(bytes)
}
func TestValidate(t *testing.T) {
if err := validate(nil, false); err != ErrNoUIDKey {
t.Fatalf("Unexpected error. Expected: %s, Got: %v", ErrNoUIDKey, err)
}
if err := validate(Data{"uid": 42}, true); err != ErrUIDNotString {
t.Fatalf("Unexpected error. Expected: %s, Got: %v", ErrUIDNotString, err)
}
if err := validate(Data{"uid": strings.Repeat(" ", MaxUIDLen+1)}, true); err != ErrUIDTooLong {
t.Fatalf("Unexpected error. Expected: %s, Got: %v", ErrUIDTooLong, err)
}
// No uid in admin mode should not fail.
if err := validate(Data{}, true); err != nil {
t.Fatal(err)
}
}
func TestGenerateClaim(t *testing.T) {
buf, err := generateClaim(Data{"uid": "42"}, &Option{Admin: true}, 0)
if err != nil {
t.Fatal(err)
}
if expect, got := `{"admin":true,"v":0,"d":{"uid":"42"},"iat":0}`, string(buf); expect != got {
t.Fatalf("Unexpected claim\nExpect:\t%s\nGot:\t%s", expect, got)
}
}
func TestSign(t *testing.T) {
g := struct {
key string
in string
out string
}{
key: "you cannot see me",
in: "the winter is comming",
out: "ytb5HiGUKtRhJg02DXS-serVBwbxud08FFNcx6dty78",
// use the following code segmentation to generate the output
// h := hmac.New(sha256.New, []byte(key))
// h.Write([]byte(in))
// out := encode(h.Sum(nil))
}
got := sign(g.in, g.key)
if g.out != got {
t.Fatalf("expect:%v, but got:%v", g.out, got)
}
}