1 package jwt
2
3 import (
4 "testing"
5 "time"
6 )
7
8 func TestVerifyAud(t *testing.T) {
9 var nilInterface interface{}
10 var nilListInterface []interface{}
11 var intListInterface interface{} = []int{1, 2, 3}
12 type test struct {
13 Name string
14 MapClaims MapClaims
15 Expected bool
16 Comparison string
17 Required bool
18 }
19 tests := []test{
20
21
22 {Name: "String Aud matching required", MapClaims: MapClaims{"aud": "example.com"}, Expected: true, Required: true, Comparison: "example.com"},
23 {Name: "[]String Aud with match required", MapClaims: MapClaims{"aud": []string{"example.com", "example.example.com"}}, Expected: true, Required: true, Comparison: "example.com"},
24
25
26 {Name: "String Aud with match not required", MapClaims: MapClaims{"aud": "example.com"}, Expected: true, Required: false, Comparison: "example.com"},
27 {Name: "Empty String Aud with match not required", MapClaims: MapClaims{}, Expected: true, Required: false, Comparison: "example.com"},
28 {Name: "Empty String Aud with match not required", MapClaims: MapClaims{"aud": ""}, Expected: true, Required: false, Comparison: "example.com"},
29 {Name: "Nil String Aud with match not required", MapClaims: MapClaims{"aud": nil}, Expected: true, Required: false, Comparison: "example.com"},
30
31 {Name: "[]String Aud with match not required", MapClaims: MapClaims{"aud": []string{"example.com", "example.example.com"}}, Expected: true, Required: false, Comparison: "example.com"},
32 {Name: "Empty []String Aud with match not required", MapClaims: MapClaims{"aud": []string{}}, Expected: true, Required: false, Comparison: "example.com"},
33
34
35
36 {Name: "String Aud without match required", MapClaims: MapClaims{"aud": "not.example.com"}, Expected: false, Required: true, Comparison: "example.com"},
37 {Name: "Empty String Aud without match required", MapClaims: MapClaims{"aud": ""}, Expected: false, Required: true, Comparison: "example.com"},
38 {Name: "[]String Aud without match required", MapClaims: MapClaims{"aud": []string{"not.example.com", "example.example.com"}}, Expected: false, Required: true, Comparison: "example.com"},
39 {Name: "Empty []String Aud without match required", MapClaims: MapClaims{"aud": []string{""}}, Expected: false, Required: true, Comparison: "example.com"},
40 {Name: "String Aud without match not required", MapClaims: MapClaims{"aud": "not.example.com"}, Expected: false, Required: true, Comparison: "example.com"},
41 {Name: "Empty String Aud without match not required", MapClaims: MapClaims{"aud": ""}, Expected: false, Required: true, Comparison: "example.com"},
42 {Name: "[]String Aud without match not required", MapClaims: MapClaims{"aud": []string{"not.example.com", "example.example.com"}}, Expected: false, Required: true, Comparison: "example.com"},
43
44
45 {Name: "Empty []String Aud without match required", MapClaims: MapClaims{"aud": []string{""}}, Expected: false, Required: true, Comparison: "example.com"},
46
47
48 {Name: "Empty []interface{} Aud without match required", MapClaims: MapClaims{"aud": nilListInterface}, Expected: true, Required: false, Comparison: "example.com"},
49 {Name: "[]interface{} Aud wit match required", MapClaims: MapClaims{"aud": []interface{}{"a", "foo", "example.com"}}, Expected: true, Required: true, Comparison: "example.com"},
50 {Name: "[]interface{} Aud wit match but invalid types", MapClaims: MapClaims{"aud": []interface{}{"a", 5, "example.com"}}, Expected: false, Required: true, Comparison: "example.com"},
51 {Name: "[]interface{} Aud int wit match required", MapClaims: MapClaims{"aud": intListInterface}, Expected: false, Required: true, Comparison: "example.com"},
52
53
54 {Name: "Empty interface{} Aud without match not required", MapClaims: MapClaims{"aud": nilInterface}, Expected: true, Required: false, Comparison: "example.com"},
55 }
56
57 for _, test := range tests {
58 t.Run(test.Name, func(t *testing.T) {
59 got := test.MapClaims.VerifyAudience(test.Comparison, test.Required)
60
61 if got != test.Expected {
62 t.Errorf("Expected %v, got %v", test.Expected, got)
63 }
64 })
65 }
66 }
67
68 func TestMapclaimsVerifyIssuedAtInvalidTypeString(t *testing.T) {
69 mapClaims := MapClaims{
70 "iat": "foo",
71 }
72 want := false
73 got := mapClaims.VerifyIssuedAt(0, false)
74 if want != got {
75 t.Fatalf("Failed to verify claims, wanted: %v got %v", want, got)
76 }
77 }
78
79 func TestMapclaimsVerifyNotBeforeInvalidTypeString(t *testing.T) {
80 mapClaims := MapClaims{
81 "nbf": "foo",
82 }
83 want := false
84 got := mapClaims.VerifyNotBefore(0, false)
85 if want != got {
86 t.Fatalf("Failed to verify claims, wanted: %v got %v", want, got)
87 }
88 }
89
90 func TestMapclaimsVerifyExpiresAtInvalidTypeString(t *testing.T) {
91 mapClaims := MapClaims{
92 "exp": "foo",
93 }
94 want := false
95 got := mapClaims.VerifyExpiresAt(0, false)
96
97 if want != got {
98 t.Fatalf("Failed to verify claims, wanted: %v got %v", want, got)
99 }
100 }
101
102 func TestMapClaimsVerifyExpiresAtExpire(t *testing.T) {
103 exp := time.Now().Unix()
104 mapClaims := MapClaims{
105 "exp": float64(exp),
106 }
107 want := false
108 got := mapClaims.VerifyExpiresAt(exp, true)
109 if want != got {
110 t.Fatalf("Failed to verify claims, wanted: %v got %v", want, got)
111 }
112
113 got = mapClaims.VerifyExpiresAt(exp+1, true)
114 if want != got {
115 t.Fatalf("Failed to verify claims, wanted: %v got %v", want, got)
116 }
117
118 want = true
119 got = mapClaims.VerifyExpiresAt(exp-1, true)
120 if want != got {
121 t.Fatalf("Failed to verify claims, wanted: %v got %v", want, got)
122 }
123 }
124
View as plain text