forked from centrifugal/centrifuge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_verifier_jwt_test.go
159 lines (155 loc) · 3.67 KB
/
token_verifier_jwt_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
package centrifuge
import (
"reflect"
"testing"
"time"
)
func Test_tokenVerifierJWT_VerifyConnectToken(t *testing.T) {
type args struct {
token string
}
verifierJWT := newTokenVerifierJWT("secret", nil)
_time := time.Now()
tests := []struct {
name string
verifier tokenVerifier
args args
want connectToken
wantErr bool
expired bool
}{
{
name: "Valid JWT",
verifier: verifierJWT,
args: args{
token: getConnToken("user1", _time.Add(24*time.Hour).Unix()),
},
want: connectToken{
UserID: "user1",
ExpireAt: _time.Add(24 * time.Hour).Unix(),
Info: nil,
},
wantErr: false,
}, {
name: "Invalid JWT",
verifier: verifierJWT,
args: args{
token: "Invalid jwt",
},
want: connectToken{},
wantErr: true,
expired: false,
}, {
name: "Expired JWT",
verifier: verifierJWT,
args: args{
token: getConnToken("user1", _time.Add(-24*time.Hour).Unix()),
},
want: connectToken{},
wantErr: true,
expired: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.verifier.VerifyConnectToken(tt.args.token)
if tt.wantErr && err == nil {
t.Errorf("VerifyConnectToken() should return error")
}
if !tt.wantErr && err != nil {
t.Errorf("VerifyConnectToken() should not return error")
}
if tt.expired && err != errTokenExpired {
t.Errorf("VerifyConnectToken() should return token expired error")
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("VerifyConnectToken() got = %v, want %v", got, tt.want)
}
})
}
}
func Test_tokenVerifierJWT_VerifySubscribeToken(t *testing.T) {
type args struct {
token string
}
verifierJWT := newTokenVerifierJWT("secret", nil)
_time := time.Now()
tests := []struct {
name string
verifier tokenVerifier
args args
want subscribeToken
wantErr bool
expired bool
}{
{
name: "Empty JWT",
verifier: verifierJWT,
args: args{},
want: subscribeToken{},
wantErr: true,
expired: false,
}, {
name: "Invalid JWT",
verifier: verifierJWT,
args: args{
token: "randomToken",
},
want: subscribeToken{},
wantErr: true,
expired: false,
}, {
name: "Expired JWT",
verifier: verifierJWT,
args: args{
token: getSubscribeToken("channel1", "client1", _time.Add(-24*time.Hour).Unix()),
},
want: subscribeToken{},
wantErr: true,
expired: true,
}, {
name: "Valid JWT",
verifier: verifierJWT,
args: args{
token: getSubscribeToken("channel1", "client1", _time.Add(24*time.Hour).Unix()),
},
want: subscribeToken{
Client: "client1",
ExpireAt: _time.Add(24 * time.Hour).Unix(),
Info: nil,
Channel: "channel1",
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.verifier.VerifySubscribeToken(tt.args.token)
if tt.wantErr && err == nil {
t.Errorf("VerifySubscribeToken() should return error")
}
if !tt.wantErr && err != nil {
t.Errorf("VerifySubscribeToken() should not return error")
}
if tt.expired && err != errTokenExpired {
t.Errorf("VerifySubscribeToken() should return token expired error")
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("VerifySubscribeToken() got = %v, want %v", got, tt.want)
}
})
}
}
func BenchmarkConnectTokenVerify(b *testing.B) {
verifierJWT := newTokenVerifierJWT("secret", nil)
token := getConnToken("user1", time.Now().Add(24*time.Hour).Unix())
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := verifierJWT.VerifyConnectToken(token)
if err != nil {
b.Fatal(errMalformedToken)
}
}
b.StopTimer()
b.ReportAllocs()
}