forked from dgrijalva/jwt-go
-
Notifications
You must be signed in to change notification settings - Fork 3
/
parser_test.go
120 lines (111 loc) · 2.76 KB
/
parser_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
package h256only_test
import (
"encoding/json"
"reflect"
"testing"
"time"
"github.com/kevinburke/h256only"
"github.com/kevinburke/h256only/test"
)
var testData = []struct {
name string
tokenString string
claims interface{}
err string
}{
{
"basic",
"eyJ0eXAiOiJoMjU2b25seSJ9.eyJmb28iOiJiYXIifQ.Rw_hYjKT1nbhuvLfPJknTXv7P-QJFwEKWroE3NNZWRo",
h256only.MapClaims{"foo": "bar"},
"",
},
{
"basic wrongkey",
"eyJ0eXAiOiJoMjU2b25seSJ9.eyJmb28iOiJiYXIifQ.wrongsignature",
h256only.MapClaims{"foo": "bar"},
"invalid signature",
},
{
"valid signing method",
"",
h256only.MapClaims{"foo": "bar"},
"",
},
{
"JSON Number",
"",
h256only.MapClaims{"foo": json.Number("123.4")},
"",
},
{
"Standard Claims",
"",
&h256only.StandardClaims{
ExpiresAt: time.Now().Add(time.Second * 10).Unix(),
},
"",
},
{
"Standard Expired Claims",
"",
&h256only.StandardClaims{
ExpiresAt: time.Now().Add(-1 * time.Second * 10).Unix(),
},
"",
},
}
func TestParser_Parse(t *testing.T) {
// Iterate over test data set and run tests
for _, data := range testData {
// If the token string is blank, use helper function to generate string
if data.tokenString == "" {
data.tokenString = test.MakeSampleToken(data.claims, &hmacTestKey)
}
// Parse the token
var token *h256only.Token
var err error
// Figure out correct claims type
switch data.claims.(type) {
case h256only.MapClaims:
token, err = h256only.ParseWithClaims(data.tokenString, h256only.MapClaims{}, &hmacTestKey)
case *h256only.StandardClaims:
token, err = h256only.ParseWithClaims(data.tokenString, &h256only.StandardClaims{}, &hmacTestKey)
default:
panic("unknown type")
}
if data.err == "" {
if err != nil {
t.Errorf("[%v] Error while verifying token: %T:%v", data.name, err, err)
}
if (token == nil || token.Claims == nil) && data.claims != nil {
t.Errorf("[%v]: nil token | token.Claims but data.claims was not nil: %v", data.name, data.claims)
continue
}
// Verify result matches expectation
if !reflect.DeepEqual(data.claims, token.Claims) {
t.Errorf("[%v] Claims mismatch. Expecting: %#v Got: %#v", data.name, data.claims, token.Claims)
}
if token == nil || token.Signature == "" {
t.Errorf("[%v] Signature is left unpopulated after parsing", data.name)
}
} else {
if err == nil {
t.Errorf("[%v] Expecting error. Didn't get one.", data.name)
} else {
if err.Error() != data.err {
t.Errorf("[%v]: err: got %q, want %q", data.name, err.Error(), data.err)
}
}
}
}
}
func BenchmarkHS256Signing(b *testing.B) {
t := h256only.New()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if _, err := t.SignedString(&hmacTestKey); err != nil {
b.Fatal(err)
}
}
})
}