Debian Codegraph @

internal/tokenstorage/inmemory_test.go

raw
1 package tokenstorage_test
2
3 import (
4 "context"
5 "testing"
6 "time"
7
8 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/test/testsuite"
9 "github.com/jkroepke/openvpn-auth-oauth2/v2/internal/tokenstorage"
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 )
13
14 func newInMemory(tb testing.TB, encryptionKey string, expires time.Duration) *tokenstorage.InMemory {
15 tb.Helper()
16
17 storage, err := tokenstorage.NewInMemory(encryptionKey, expires)
18 require.NoError(tb, err)
19
20 return storage
21 }
22
23 func newInMemoryWithGC(
24 tb testing.TB,
25 expires, gcInterval time.Duration,
26 ) *tokenstorage.InMemory {
27 tb.Helper()
28
29 storage, err := tokenstorage.NewInMemoryWithGC(testsuite.Secret, expires, gcInterval)
30 require.NoError(tb, err)
31
32 return storage
33 }
34
35 func TestStorageInMemory(t *testing.T) {
36 t.Parallel()
37
38 ctx := context.Background()
39 tokenStorage := newInMemory(t, testsuite.Secret, time.Millisecond*400)
40
41 t.Cleanup(func() {
42 require.NoError(t, tokenStorage.Close())
43 })
44
45 require.NoError(t, tokenStorage.Set(ctx, "0", "TEST0"))
46 require.NoError(t, tokenStorage.Set(ctx, "1", "TEST1"))
47
48 token, err := tokenStorage.Get(ctx, "0")
49
50 require.NoError(t, err)
51 assert.Equal(t, "TEST0", token)
52
53 token, err = tokenStorage.Get(ctx, "1")
54
55 require.NoError(t, err)
56 assert.Equal(t, "TEST1", token)
57
58 token, err = tokenStorage.Get(ctx, "1")
59
60 require.NoError(t, err)
61 assert.Equal(t, "TEST1", token)
62
63 _, err = tokenStorage.Get(ctx, "2")
64 require.ErrorIs(t, err, tokenstorage.ErrNotExists)
65
66 err = tokenStorage.Delete(ctx, "1")
67 require.NoError(t, err)
68
69 _, err = tokenStorage.Get(ctx, "1")
70 require.Error(t, err)
71 require.ErrorIs(t, err, tokenstorage.ErrNotExists)
72 }
73
74 func TestStorageInMemory_Consume(t *testing.T) {
75 t.Parallel()
76
77 const consumers = 32
78
79 ctx := t.Context()
80 tokenStorage := newInMemory(t, testsuite.Secret, time.Hour)
81
82 t.Cleanup(func() {
83 require.NoError(t, tokenStorage.Close())
84 })
85
86 require.NoError(t, tokenStorage.Set(ctx, "client", "token"))
87
88 type result struct {
89 token string
90 err error
91 }
92
93 start := make(chan struct{})
94 results := make(chan result, consumers)
95
96 for range consumers {
97 go func() {
98 <-start
99
100 token, err := tokenStorage.Consume(ctx, "client")
101 results <- result{token: token, err: err}
102 }()
103 }
104
105 close(start)
106
107 consumed := 0
108
109 for range consumers {
110 outcome := <-results
111 if outcome.err != nil {
112 require.ErrorIs(t, outcome.err, tokenstorage.ErrNotExists)
113
114 continue
115 }
116
117 consumed++
118
119 require.Equal(t, "token", outcome.token)
120 }
121
122 require.Equal(t, 1, consumed)
123 }
124
125 func TestStorageInMemory_Expire(t *testing.T) {
126 t.Parallel()
127
128 ctx := context.Background()
129 tokenStorage := newInMemory(t, testsuite.Secret, 0)
130
131 t.Cleanup(func() {
132 require.NoError(t, tokenStorage.Close())
133 })
134
135 require.NoError(t, tokenStorage.Set(ctx, "0", "TEST0"))
136 require.NoError(t, tokenStorage.Set(ctx, "1", "TEST1"))
137
138 _, err := tokenStorage.Get(ctx, "0")
139 require.ErrorIs(t, err, tokenstorage.ErrNotExists)
140
141 _, err = tokenStorage.Get(ctx, "1")
142 require.ErrorIs(t, err, tokenstorage.ErrNotExists)
143 }
144
145 func TestStorageInMemory_InvalidSecret(t *testing.T) {
146 t.Parallel()
147
148 ctx := context.Background()
149 key := "invalid"
150
151 tokenStorage := newInMemory(t, key, time.Second)
152
153 t.Cleanup(func() {
154 require.NoError(t, tokenStorage.Close())
155 })
156
157 // HKDF derives the fixed-size AEAD key from any input length.
158 // Test that encryption/decryption works even with short keys.
159 require.NoError(t, tokenStorage.Set(ctx, "0", "TEST0"))
160 token, err := tokenStorage.Get(ctx, "0")
161 require.NoError(t, err)
162 require.Equal(t, "TEST0", token)
163 }
164
165 func TestStorageInMemory_InvalidData(t *testing.T) {
166 t.Parallel()
167
168 tokenStorage := newInMemory(t, testsuite.Secret, 0)
169
170 t.Cleanup(func() {
171 require.NoError(t, tokenStorage.Close())
172 })
173
174 require.ErrorIs(t, tokenStorage.SetStorage(nil), tokenstorage.ErrNilData)
175 }
176
177 func TestStorageInMemory_GarbageCollection(t *testing.T) {
178 t.Parallel()
179
180 ctx := context.Background()
181 // Use short expiration and GC interval for testing
182 tokenStorage := newInMemoryWithGC(t, 50*time.Millisecond, 100*time.Millisecond)
183
184 t.Cleanup(func() {
185 require.NoError(t, tokenStorage.Close())
186 })
187
188 // Add tokens
189 require.NoError(t, tokenStorage.Set(ctx, "0", "TEST0"))
190 require.NoError(t, tokenStorage.Set(ctx, "1", "TEST1"))
191
192 // After expiration and GC, tokens should not be retrievable
193 require.EventuallyWithT(t, func(collect *assert.CollectT) {
194 _, err := tokenStorage.Get(ctx, "0")
195 assert.ErrorIs(collect, err, tokenstorage.ErrNotExists)
196
197 _, err = tokenStorage.Get(ctx, "1")
198 assert.ErrorIs(collect, err, tokenstorage.ErrNotExists)
199 }, time.Second, 25*time.Millisecond)
200 }
201
202 func TestStorageInMemory_NoGC(t *testing.T) {
203 t.Parallel()
204
205 ctx := context.Background()
206 // Disable GC by passing 0 interval
207 tokenStorage := newInMemoryWithGC(t, 50*time.Millisecond, 0)
208
209 t.Cleanup(func() {
210 require.NoError(t, tokenStorage.Close())
211 })
212
213 require.NoError(t, tokenStorage.Set(ctx, "0", "TEST0"))
214
215 // Access should trigger removal
216 require.EventuallyWithT(t, func(collect *assert.CollectT) {
217 _, err := tokenStorage.Get(ctx, "0")
218 assert.ErrorIs(collect, err, tokenstorage.ErrNotExists)
219 }, time.Second, 25*time.Millisecond)
220 }