forked from MicahParks/jwkset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_test.go
173 lines (145 loc) · 4.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
package jwkset_test
import (
"bytes"
"context"
"errors"
"testing"
"time"
"github.com/MicahParks/jwkset"
)
const (
kidMissing = "kid missing"
kidWritten = "kid written"
kidWritten2 = "kid written 2"
)
var (
hmacKey1 = []byte("hamc key 1")
hmacKey2 = []byte("hamc key 2")
)
type storageTestParams[CustomKeyMeta any] struct {
ctx context.Context
cancel context.CancelFunc
jwks jwkset.JWKSet[CustomKeyMeta]
}
func TestMemoryDeleteKey(t *testing.T) {
params := setupMemory[any]()
defer params.cancel()
store := params.jwks.Store
err := store.WriteKey(params.ctx, jwkset.NewKey[any](hmacKey1, kidWritten))
if err != nil {
t.Fatalf("Failed to write key. %s", err)
}
ok, err := store.DeleteKey(params.ctx, kidMissing)
if err != nil {
t.Fatalf("Failed to delete missing key. %s", err)
}
if ok {
t.Fatalf("Deleted missing key.")
}
ok, err = store.DeleteKey(params.ctx, kidWritten)
if err != nil {
t.Fatalf("Failed to delete written key. %s", err)
}
if !ok {
t.Fatalf("Failed to delete written key.")
}
}
func TestMemoryReadKey(t *testing.T) {
params := setupMemory[any]()
defer params.cancel()
store := params.jwks.Store
err := store.WriteKey(params.ctx, jwkset.NewKey[any](hmacKey1, kidWritten))
if err != nil {
t.Fatalf("Failed to write key. %s", err)
}
_, err = store.ReadKey(params.ctx, kidMissing)
if !errors.Is(err, jwkset.ErrKeyNotFound) {
t.Fatalf("Should have specific error when reading missing key.\n Actual: %s\n Expected: %s", err, jwkset.ErrKeyNotFound)
}
key, err := store.ReadKey(params.ctx, kidWritten)
if err != nil {
t.Fatalf("Failed to read written key. %s", err)
}
if !bytes.Equal(key.Key.([]byte), hmacKey1) {
t.Fatalf("Read key does not match written key.")
}
err = store.WriteKey(params.ctx, jwkset.NewKey[any](hmacKey2, kidWritten))
if err != nil {
t.Fatalf("Failed to overwrite key. %s", err)
}
key, err = store.ReadKey(params.ctx, kidWritten)
if err != nil {
t.Fatalf("Failed to read written key. %s", err)
}
if !bytes.Equal(key.Key.([]byte), hmacKey2) {
t.Fatalf("Read key does not match written key.")
}
_, err = store.DeleteKey(params.ctx, kidWritten)
if err != nil {
t.Fatalf("Failed to delete written key. %s", err)
}
_, err = store.ReadKey(params.ctx, kidWritten)
if !errors.Is(err, jwkset.ErrKeyNotFound) {
t.Fatalf("Should have specific error when reading missing key.\n Actual: %s\n Expected: %s", err, jwkset.ErrKeyNotFound)
}
}
func TestMemorySnapshotKeys(t *testing.T) {
params := setupMemory[any]()
defer params.cancel()
store := params.jwks.Store
err := store.WriteKey(params.ctx, jwkset.NewKey[any](hmacKey1, kidWritten))
if err != nil {
t.Fatalf("Failed to write key 1. %s", err)
}
err = store.WriteKey(params.ctx, jwkset.NewKey[any](hmacKey2, kidWritten2))
if err != nil {
t.Fatalf("Failed to write key 2. %s", err)
}
meta, err := store.SnapshotKeys(params.ctx)
if err != nil {
t.Fatalf("Failed to snapshot keys. %s", err)
}
if len(meta) != 2 {
t.Fatalf("Snapshot should have 2 keys. %d", len(meta))
}
kid1Found := false
kid2Found := false
for _, m := range meta {
if !kid1Found && m.KeyID == kidWritten {
kid1Found = true
if !bytes.Equal(m.Key.([]byte), hmacKey1) {
t.Fatalf("Snapshot key does not match written key.")
}
} else if !kid2Found && m.KeyID == kidWritten2 {
kid2Found = true
if !bytes.Equal(m.Key.([]byte), hmacKey2) {
t.Fatalf("Snapshot key does not match written key.")
}
} else {
t.Fatalf("Snapshot key has unexpected key ID.")
}
}
}
func TestMemoryWriteKey(t *testing.T) {
params := setupMemory[any]()
defer params.cancel()
store := params.jwks.Store
err := store.WriteKey(params.ctx, jwkset.NewKey[any](hmacKey1, kidWritten))
if err != nil {
t.Fatalf("Failed to write key. %s", err)
}
err = store.WriteKey(params.ctx, jwkset.NewKey[any](hmacKey2, kidWritten))
if err != nil {
t.Fatalf("Failed to overwrite key. %s", err)
}
}
func setupMemory[CustomKeyMeta any]() (params storageTestParams[CustomKeyMeta]) {
jwkSet := jwkset.NewMemory[CustomKeyMeta]()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
params = storageTestParams[CustomKeyMeta]{
ctx: ctx,
cancel: cancel,
jwks: jwkSet,
}
return params
}