-
Notifications
You must be signed in to change notification settings - Fork 7
/
token_test.go
74 lines (57 loc) · 1.67 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
73
74
package goalone
import (
"crypto/subtle"
"testing"
)
func TestParse(t *testing.T) {
secret := []byte(`B1nzyRateLimits`)
want := []byte(`1203981209381290.LutinRocks`)
bt := []byte(`1203981209381290.LutinRocks.ZGRsRXvTb08ld7xmJImL1ykGr8D1JmrSPGc134nBNRo`)
s := New(secret)
token := s.Parse(bt)
if subtle.ConstantTimeCompare(token.Payload, want) != 1 {
t.Logf("Got %s\n", token.Payload)
t.Logf("Want %s\n", want)
t.Fatal("token and want do not match")
}
if !token.Timestamp.IsZero() {
t.Fatal("Timestamp parsed incorrectly")
}
}
func TestParseTimestamp(t *testing.T) {
secret := []byte(`B1nzyRateLimits`)
bt := []byte(`1203981209381290.LutinRocks.3gteYe.ZGRsRXvTb08ld7xmJImL1ykGr8D1JmrSPGc134nBNRo`)
want := []byte(`1203981209381290.LutinRocks`)
s := New(secret, Timestamp)
token := s.Parse(bt)
if subtle.ConstantTimeCompare(token.Payload, want) != 1 {
t.Logf("Got %s\n", token.Payload)
t.Logf("Want %s\n", want)
t.Fatal("token and want do not match")
}
if token.Timestamp.Unix() != 1487775993 {
t.Logf("Got %d\n", token.Timestamp.Unix())
t.Logf("Want %s\n", want)
t.Fatal("Timestamp parsed incorrectly")
}
}
func BenchmarkParse(b *testing.B) {
bt := []byte(`1203981209381290.LutinRocks.ZGRsRXvTb08ld7xmJImL1ykGr8D1JmrSPGc134nBNRo`)
secret := []byte(`B1nzyRateLimits`)
s := New(secret)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
s.Parse(bt)
}
}
func BenchmarkParseTimestamp(b *testing.B) {
bt := []byte(`1203981209381290.LutinRocks.3gteYe.ZGRsRXvTb08ld7xmJImL1ykGr8D1JmrSPGc134nBNRo`)
secret := []byte(`B1nzyRateLimits`)
s := New(secret, Timestamp)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
s.Parse(bt)
}
}