forked from pberkel/caddy-storage-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage_test.go
350 lines (270 loc) · 9.19 KB
/
storage_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package storageredis
import (
"context"
"encoding/json"
"errors"
"io/fs"
"strconv"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
)
const (
TestDB = 9
TestKeyPrefix = "redistlstest"
TestEncryptionKey = "1aedfs5kcM8lOZO3BDDMuwC23croDwRr"
TestCompression = true
TestKeyCertPath = "certificates"
TestKeyAcmePath = TestKeyCertPath + "/acme-v02.api.letsencrypt.org-directory"
TestKeyExamplePath = TestKeyAcmePath + "/example.com"
TestKeyExampleCrt = TestKeyExamplePath + "/example.com.crt"
TestKeyExampleKey = TestKeyExamplePath + "/example.com.key"
TestKeyExampleJson = TestKeyExamplePath + "/example.com.json"
TestKeyLock = "locks/issue_cert_example.com"
TestKeyLockIterations = 250
)
var (
TestValueCrt = []byte("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdCwgc2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFib3JlIGV0IGRvbG9yZSBtYWduYSBhbGlxdWEu")
TestValueKey = []byte("RWdlc3RhcyBlZ2VzdGFzIGZyaW5naWxsYSBwaGFzZWxsdXMgZmF1Y2lidXMgc2NlbGVyaXNxdWUgZWxlaWZlbmQgZG9uZWMgcHJldGl1bSB2dWxwdXRhdGUuIFRpbmNpZHVudCBvcm5hcmUgbWFzc2EgZWdldC4=")
TestValueJson = []byte("U2FnaXR0aXMgYWxpcXVhbSBtYWxlc3VhZGEgYmliZW5kdW0gYXJjdSB2aXRhZSBlbGVtZW50dW0uIEludGVnZXIgbWFsZXN1YWRhIG51bmMgdmVsIHJpc3VzIGNvbW1vZG8gdml2ZXJyYSBtYWVjZW5hcy4=")
)
// Emulate the Provision() Caddy function
func provisionRedisStorage(t *testing.T) (*RedisStorage, context.Context) {
ctx := context.Background()
rs := New()
logger, _ := zap.NewProduction()
rs.logger = logger.Sugar()
rs.DB = TestDB
rs.KeyPrefix = TestKeyPrefix
rs.EncryptionKey = TestEncryptionKey
rs.Compression = TestCompression
err := rs.finalizeConfiguration(ctx)
assert.NoError(t, err)
// Skip test if unable to connect to Redis server
if err != nil {
t.Skip()
return nil, nil
}
// Flush the current Redis database
err = rs.client.FlushDB(ctx).Err()
assert.NoError(t, err)
return rs, ctx
}
func TestRedisStorage_Store(t *testing.T) {
rs, ctx := provisionRedisStorage(t)
err := rs.Store(ctx, TestKeyExampleCrt, TestValueCrt)
assert.NoError(t, err)
}
func TestRedisStorage_Exists(t *testing.T) {
rs, ctx := provisionRedisStorage(t)
exists := rs.Exists(ctx, TestKeyExampleCrt)
assert.False(t, exists)
err := rs.Store(ctx, TestKeyExampleCrt, TestValueCrt)
assert.NoError(t, err)
exists = rs.Exists(ctx, TestKeyExampleCrt)
assert.True(t, exists)
}
func TestRedisStorage_Load(t *testing.T) {
rs, ctx := provisionRedisStorage(t)
err := rs.Store(ctx, TestKeyExampleCrt, TestValueCrt)
assert.NoError(t, err)
loadedValue, err := rs.Load(ctx, TestKeyExampleCrt)
assert.NoError(t, err)
assert.Equal(t, TestValueCrt, loadedValue)
}
func TestRedisStorage_Delete(t *testing.T) {
rs, ctx := provisionRedisStorage(t)
err := rs.Store(ctx, TestKeyExampleCrt, TestValueCrt)
assert.NoError(t, err)
err = rs.Delete(ctx, TestKeyExampleCrt)
assert.NoError(t, err)
exists := rs.Exists(ctx, TestKeyExampleCrt)
assert.False(t, exists)
loadedValue, err := rs.Load(ctx, TestKeyExampleCrt)
assert.Nil(t, loadedValue)
notExist := errors.Is(err, fs.ErrNotExist)
assert.True(t, notExist)
}
func TestRedisStorage_Stat(t *testing.T) {
rs, ctx := provisionRedisStorage(t)
size := int64(len(TestValueCrt))
startTime := time.Now()
err := rs.Store(ctx, TestKeyExampleCrt, TestValueCrt)
endTime := time.Now()
assert.NoError(t, err)
stat, err := rs.Stat(ctx, TestKeyExampleCrt)
assert.NoError(t, err)
assert.Equal(t, TestKeyExampleCrt, stat.Key)
assert.WithinRange(t, stat.Modified, startTime, endTime)
assert.Equal(t, size, stat.Size)
}
func TestRedisStorage_List(t *testing.T) {
rs, ctx := provisionRedisStorage(t)
// Store two key values
err := rs.Store(ctx, TestKeyExampleCrt, TestValueCrt)
assert.NoError(t, err)
err = rs.Store(ctx, TestKeyExampleKey, TestValueKey)
assert.NoError(t, err)
// List recursively from root
keys, err := rs.List(ctx, "", true)
assert.NoError(t, err)
assert.Len(t, keys, 2)
assert.Contains(t, keys, TestKeyExampleCrt)
assert.Contains(t, keys, TestKeyExampleKey)
assert.NotContains(t, keys, TestKeyExampleJson)
// List recursively from first directory
keys, err = rs.List(ctx, TestKeyCertPath, true)
assert.NoError(t, err)
assert.Len(t, keys, 2)
assert.Contains(t, keys, TestKeyExampleCrt)
assert.Contains(t, keys, TestKeyExampleKey)
assert.NotContains(t, keys, TestKeyExampleJson)
// Store third key value
err = rs.Store(ctx, TestKeyExampleJson, TestValueJson)
assert.NoError(t, err)
// List recursively from root
keys, err = rs.List(ctx, "", true)
assert.NoError(t, err)
assert.Len(t, keys, 3)
assert.Contains(t, keys, TestKeyExampleCrt)
assert.Contains(t, keys, TestKeyExampleKey)
assert.Contains(t, keys, TestKeyExampleJson)
// List recursively from first directory
keys, err = rs.List(ctx, TestKeyCertPath, true)
assert.NoError(t, err)
assert.Len(t, keys, 3)
assert.Contains(t, keys, TestKeyExampleCrt)
assert.Contains(t, keys, TestKeyExampleKey)
assert.Contains(t, keys, TestKeyExampleJson)
// Delete one key value
err = rs.Delete(ctx, TestKeyExampleCrt)
assert.NoError(t, err)
// List recursively from root
keys, err = rs.List(ctx, "", true)
assert.NoError(t, err)
assert.Len(t, keys, 2)
assert.NotContains(t, keys, TestKeyExampleCrt)
assert.Contains(t, keys, TestKeyExampleKey)
assert.Contains(t, keys, TestKeyExampleJson)
keys, err = rs.List(ctx, TestKeyCertPath, true)
assert.NoError(t, err)
assert.Len(t, keys, 2)
assert.NotContains(t, keys, TestKeyExampleCrt)
assert.Contains(t, keys, TestKeyExampleKey)
assert.Contains(t, keys, TestKeyExampleJson)
// Delete remaining two key values
err = rs.Delete(ctx, TestKeyExampleKey)
assert.NoError(t, err)
err = rs.Delete(ctx, TestKeyExampleJson)
assert.NoError(t, err)
// List recursively from root
keys, err = rs.List(ctx, "", true)
assert.NoError(t, err)
assert.Empty(t, keys)
keys, err = rs.List(ctx, TestKeyCertPath, true)
assert.NoError(t, err)
assert.Empty(t, keys)
}
func TestRedisStorage_ListNonRecursive(t *testing.T) {
rs, ctx := provisionRedisStorage(t)
// Store three key values
err := rs.Store(ctx, TestKeyExampleCrt, TestValueCrt)
assert.NoError(t, err)
err = rs.Store(ctx, TestKeyExampleKey, TestValueKey)
assert.NoError(t, err)
err = rs.Store(ctx, TestKeyExampleJson, TestValueJson)
assert.NoError(t, err)
// List non-recursively from root
keys, err := rs.List(ctx, "", false)
assert.NoError(t, err)
assert.Len(t, keys, 1)
assert.Contains(t, keys, TestKeyCertPath)
// List non-recursively from first level
keys, err = rs.List(ctx, TestKeyCertPath, false)
assert.NoError(t, err)
assert.Len(t, keys, 1)
assert.Contains(t, keys, TestKeyAcmePath)
// List non-recursively from second level
keys, err = rs.List(ctx, TestKeyAcmePath, false)
assert.NoError(t, err)
assert.Len(t, keys, 1)
assert.Contains(t, keys, TestKeyExamplePath)
// List non-recursively from third level
keys, err = rs.List(ctx, TestKeyExamplePath, false)
assert.NoError(t, err)
assert.Len(t, keys, 3)
assert.Contains(t, keys, TestKeyExampleCrt)
assert.Contains(t, keys, TestKeyExampleKey)
assert.Contains(t, keys, TestKeyExampleJson)
}
func TestRedisStorage_LockUnlock(t *testing.T) {
rs, ctx := provisionRedisStorage(t)
err := rs.Lock(ctx, TestKeyLock)
assert.NoError(t, err)
err = rs.Unlock(ctx, TestKeyLock)
assert.NoError(t, err)
}
func TestRedisStorage_MultipleLocks(t *testing.T) {
var wg sync.WaitGroup
var rsArray = make([]*RedisStorage, TestKeyLockIterations)
for i := 0; i < len(rsArray); i++ {
rsArray[i], _ = provisionRedisStorage(t)
wg.Add(1)
}
for i := 0; i < len(rsArray); i++ {
suffix := strconv.Itoa(i / 10)
go lockAndUnlock(t, &wg, rsArray[i], TestKeyLock+"-"+suffix)
}
wg.Wait()
}
func lockAndUnlock(t *testing.T, wg *sync.WaitGroup, rs *RedisStorage, key string) {
defer wg.Done()
err := rs.Lock(context.Background(), key)
assert.NoError(t, err)
err = rs.Unlock(context.Background(), key)
assert.NoError(t, err)
}
func TestRedisStorage_String(t *testing.T) {
rs := New()
redacted := `REDACTED`
t.Run("Validate password", func(t *testing.T) {
t.Run("is redacted when set", func(t *testing.T) {
testrs := New()
password := "iAmASuperSecurePassword"
rs.Password = password
err := json.Unmarshal([]byte(rs.String()), &testrs)
assert.NoError(t, err)
assert.Equal(t, redacted, testrs.Password)
assert.Equal(t, password, rs.Password)
})
rs.Password = ""
t.Run("is empty if not set", func(t *testing.T) {
err := json.Unmarshal([]byte(rs.String()), &rs)
assert.NoError(t, err)
assert.Empty(t, rs.Password)
})
})
t.Run("Validate AES key", func(t *testing.T) {
t.Run("is redacted when set", func(t *testing.T) {
testrs := New()
aeskey := "abcdefghijklmnopqrstuvwxyz123456"
rs.EncryptionKey = aeskey
err := json.Unmarshal([]byte(rs.String()), &testrs)
assert.NoError(t, err)
assert.Equal(t, redacted, testrs.EncryptionKey)
assert.Equal(t, aeskey, rs.EncryptionKey)
})
rs.EncryptionKey = ""
t.Run("is empty if not set", func(t *testing.T) {
err := json.Unmarshal([]byte(rs.String()), &rs)
assert.NoError(t, err)
assert.Empty(t, rs.EncryptionKey)
})
})
}
func TestRedisStorage_GetClient(t *testing.T) {
rs, _ := provisionRedisStorage(t)
assert.NotNil(t, rs.GetClient())
}