Repository navigation
Expand file tree
/
Copy pathcache_test.go
More file actions
144 lines (134 loc) · 3.84 KB
/
Copy pathcache_test.go
File metadata and controls
144 lines (134 loc) · 3.84 KB
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
// Tideland Go JSON Web Token - Cache - Unit Tests
//
// Copyright (C) 2016-2025 Frank Mueller / Tideland / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package jwt_test
import (
"context"
"fmt"
"testing"
"time"
"tideland.dev/go/asserts/verify"
"tideland.dev/go/jwt"
)
// TestCachePutGet verifies the putting and getting of tokens
// to the cache.
func TestCachePutGet(t *testing.T) {
ctx := context.Background()
cache := jwt.NewCache(ctx, time.Minute, time.Minute, time.Minute, 10)
key := []byte("secret")
claims := initClaims()
jwtIn, err := jwt.Encode(claims, key, jwt.HS512)
verify.NoError(t, err)
_, err = cache.Put(jwtIn)
verify.NoError(t, err)
jwt := jwtIn.String()
jwtOut, err := cache.Get(jwt)
verify.NoError(t, err)
verify.Equal(t, jwtIn, jwtOut)
jwtOut, err = cache.Get("is.not.there")
verify.NoError(t, err)
verify.True(t, jwtOut == nil)
}
// TestCacheAccessCleanup verifies the access based cleanup
// of the JWT cache.
func TestCacheAccessCleanup(t *testing.T) {
ctx := context.Background()
cache := jwt.NewCache(ctx, time.Second, time.Second, time.Second, 10)
key := []byte("secret")
claims := initClaims()
jwtIn, err := jwt.Encode(claims, key, jwt.HS512)
verify.NoError(t, err)
_, err = cache.Put(jwtIn)
verify.NoError(t, err)
jwt := jwtIn.String()
jwtOut, err := cache.Get(jwt)
verify.NoError(t, err)
verify.Equal(t, jwtIn, jwtOut)
// Now wait a bit an try again.
time.Sleep(5 * time.Second)
jwtOut, err = cache.Get(jwt)
verify.NoError(t, err)
verify.True(t, jwtOut == nil)
}
// TestCacheValidityCleanup verifies the validity based cleanup
// of the JWT cache.
func TestCacheValidityCleanup(t *testing.T) {
ctx := context.Background()
cache := jwt.NewCache(ctx, time.Minute, time.Second, time.Second, 10)
key := []byte("secret")
now := time.Now()
nbf := now.Add(-2 * time.Second)
exp := now.Add(2 * time.Second)
claims := initClaims()
claims.SetNotBefore(nbf)
claims.SetExpiration(exp)
jwtIn, err := jwt.Encode(claims, key, jwt.HS512)
verify.NoError(t, err)
_, err = cache.Put(jwtIn)
verify.NoError(t, err)
jwt := jwtIn.String()
jwtOut, err := cache.Get(jwt)
verify.NoError(t, err)
verify.Equal(t, jwtOut, jwtIn)
// Now access until it is invalid and not
// available anymore.
var i int
for i = 0; i < 5; i++ {
time.Sleep(time.Second)
jwtOut, err = cache.Get(jwt)
verify.NoError(t, err)
if jwtOut == nil {
break
}
verify.Equal(t, jwtOut, jwtIn)
}
verify.True(t, i > 1 && i < 4)
}
// TestCacheLoad verifies the cache load based cleanup.
func TestCacheLoad(t *testing.T) {
cacheTime := 100 * time.Millisecond
ctx := context.Background()
cache := jwt.NewCache(ctx, 2*cacheTime, cacheTime, cacheTime, 4)
claims := initClaims()
// Now fill the cache and check that it doesn't
// grow too high.
var i int
for i = 0; i < 10; i++ {
time.Sleep(50 * time.Millisecond)
key := []byte(fmt.Sprintf("secret-%d", i))
jwtIn, err := jwt.Encode(claims, key, jwt.HS512)
verify.NoError(t, err)
size, err := cache.Put(jwtIn)
verify.NoError(t, err)
verify.True(t, size < 6)
}
}
// TestCacheContext verifies the cache stopping by context.
func TestCacheContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cache := jwt.NewCache(ctx, time.Minute, time.Minute, time.Minute, 10)
key := []byte("secret")
claims := initClaims()
jwtIn, err := jwt.Encode(claims, key, jwt.HS512)
verify.NoError(t, err)
_, err = cache.Put(jwtIn)
verify.NoError(t, err)
// Now cancel and test to get jwt.
cancel()
time.Sleep(10 * time.Millisecond)
jwt := jwtIn.String()
jwtOut, err := cache.Get(jwt)
verify.ErrorContains(t, err, "cache action timeout")
verify.True(t, jwtOut == nil)
}
// initClaims creates test claims.
func initClaims() jwt.Claims {
c := jwt.NewClaims()
c.SetSubject("1234567890")
c.Set("name", "John Doe")
c.Set("admin", true)
return c
}