-
Notifications
You must be signed in to change notification settings - Fork 19
/
expected_test.go
149 lines (136 loc) · 4.37 KB
/
expected_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
package jwt
import (
"errors"
"fmt"
"testing"
)
func TestExpected(t *testing.T) {
expected := Expected{
NotBefore: 2019,
IssuedAt: 1193,
Expiry: 2020,
ID: "my-jti",
Issuer: "my-iss",
Subject: "1194",
Audience: []string{"aud1", "aud2"},
}
err := fmt.Errorf("test err")
if got := expected.ValidateToken(nil, Claims{}, err); err != got {
t.Fatalf("expected to return the previous error but got: %v", got)
}
// Test all OK.
err = expected.ValidateToken(nil, Claims{
NotBefore: 2019,
IssuedAt: 1193,
Expiry: 2020,
ID: "my-jti",
Issuer: "my-iss",
Subject: "1194",
Audience: []string{"aud1", "aud2"},
}, nil)
if err != nil {
t.Fatalf("expected nil error but got: %v", err)
}
// Test failures one by one, should stop on the first error.
var getExpectedErr = func(field string) error {
return fmt.Errorf("%w: %s", ErrExpected, field)
}
expectedErr := getExpectedErr("nbf")
gotErr := expected.ValidateToken(nil, Claims{NotBefore: 1}, nil)
if !errors.Is(gotErr, ErrExpected) {
t.Fatalf("expected error to be ErrExpired but got: %#+v", gotErr)
}
if expectedErr.Error() != gotErr.Error() {
t.Fatalf("expected error: %v but got: %v", expectedErr, gotErr)
}
expectedErr = getExpectedErr("iat")
gotErr = expected.ValidateToken(nil, Claims{
NotBefore: expected.NotBefore,
IssuedAt: 1}, nil)
if !errors.Is(gotErr, ErrExpected) {
t.Fatalf("expected error to be ErrExpired but got: %#+v", gotErr)
}
if expectedErr.Error() != gotErr.Error() {
t.Fatalf("expected error: %v but got: %v", expectedErr, gotErr)
}
expectedErr = getExpectedErr("exp")
gotErr = expected.ValidateToken(nil, Claims{
NotBefore: expected.NotBefore,
IssuedAt: expected.IssuedAt,
Expiry: 1}, nil)
if !errors.Is(gotErr, ErrExpected) {
t.Fatalf("expected error to be ErrExpired but got: %#+v", gotErr)
}
if expectedErr.Error() != gotErr.Error() {
t.Fatalf("expected error: %v but got: %v", expectedErr, gotErr)
}
expectedErr = getExpectedErr("jti")
gotErr = expected.ValidateToken(nil, Claims{
NotBefore: expected.NotBefore,
IssuedAt: expected.IssuedAt,
Expiry: expected.Expiry,
ID: "unmatched"}, nil)
if !errors.Is(gotErr, ErrExpected) {
t.Fatalf("expected error to be ErrExpired but got: %#+v", gotErr)
}
if expectedErr.Error() != gotErr.Error() {
t.Fatalf("expected error: %v but got: %v", expectedErr, gotErr)
}
expectedErr = getExpectedErr("iss")
gotErr = expected.ValidateToken(nil, Claims{
NotBefore: expected.NotBefore,
IssuedAt: expected.IssuedAt,
Expiry: expected.Expiry,
ID: expected.ID,
Issuer: "unmatched"}, nil)
if !errors.Is(gotErr, ErrExpected) {
t.Fatalf("expected error to be ErrExpired but got: %#+v", gotErr)
}
if expectedErr.Error() != gotErr.Error() {
t.Fatalf("expected error: %v but got: %v", expectedErr, gotErr)
}
expectedErr = getExpectedErr("sub")
gotErr = expected.ValidateToken(nil, Claims{
NotBefore: expected.NotBefore,
IssuedAt: expected.IssuedAt,
Expiry: expected.Expiry,
ID: expected.ID,
Issuer: expected.Issuer,
Subject: "unmatched"}, nil)
if !errors.Is(gotErr, ErrExpected) {
t.Fatalf("expected error to be ErrExpired but got: %#+v", gotErr)
}
if expectedErr.Error() != gotErr.Error() {
t.Fatalf("expected error: %v but got: %v", expectedErr, gotErr)
}
expectedErr = getExpectedErr("aud (length)")
gotErr = expected.ValidateToken(nil, Claims{
NotBefore: expected.NotBefore,
IssuedAt: expected.IssuedAt,
Expiry: expected.Expiry,
ID: expected.ID,
Issuer: expected.Issuer,
Subject: expected.Subject,
Audience: []string{"aud1", "aud2", "aud3"}}, nil)
if !errors.Is(gotErr, ErrExpected) {
t.Fatalf("expected error to be ErrExpired but got: %#+v", gotErr)
}
if expectedErr.Error() != gotErr.Error() {
t.Fatalf("expected error: %v but got: %v", expectedErr, gotErr)
}
expectedErr = getExpectedErr(`aud ("aud2")`)
gotErr = expected.ValidateToken(nil, Claims{
NotBefore: expected.NotBefore,
IssuedAt: expected.IssuedAt,
Expiry: expected.Expiry,
ID: expected.ID,
Issuer: expected.Issuer,
Subject: expected.Subject,
Audience: []string{"aud1", "aud3"}}, nil)
if !errors.Is(gotErr, ErrExpected) {
t.Fatalf("expected error to be ErrExpired but got: %#+v", gotErr)
}
if expectedErr.Error() != gotErr.Error() {
t.Fatalf("expected error: %v but got: %v", expectedErr, gotErr)
}
}