-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
storage_test.go
196 lines (168 loc) · 4.45 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
package jwkset
import (
"bytes"
"context"
"errors"
"testing"
"time"
)
const (
kidMissing = "kid missing"
kidWritten = "kid written"
kidWritten2 = "kid written 2"
)
var (
hmacKey1 = []byte("hamc key 1")
hmacKey2 = []byte("hamc key 2")
)
type storageTestParams struct {
ctx context.Context
cancel context.CancelFunc
jwks Storage
}
func TestMemoryKeyDelete(t *testing.T) {
params := setupMemory()
defer params.cancel()
store := params.jwks
jwk := newStorageTestJWK(t, hmacKey1, kidWritten)
err := store.KeyWrite(params.ctx, jwk)
if err != nil {
t.Fatalf("Failed to write key. %s", err)
}
ok, err := store.KeyDelete(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.KeyDelete(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 TestMemoryKeyRead(t *testing.T) {
params := setupMemory()
defer params.cancel()
store := params.jwks
jwk := newStorageTestJWK(t, hmacKey1, kidWritten)
err := store.KeyWrite(params.ctx, jwk)
if err != nil {
t.Fatalf("Failed to write key. %s", err)
}
_, err = store.KeyRead(params.ctx, kidMissing)
if !errors.Is(err, ErrKeyNotFound) {
t.Fatalf("Should have specific error when reading missing key.\n Actual: %s\n Expected: %s", err, ErrKeyNotFound)
}
key, err := store.KeyRead(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.")
}
jwk = newStorageTestJWK(t, hmacKey2, kidWritten)
err = store.KeyWrite(params.ctx, jwk)
if err != nil {
t.Fatalf("Failed to overwrite key. %s", err)
}
key, err = store.KeyRead(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.KeyDelete(params.ctx, kidWritten)
if err != nil {
t.Fatalf("Failed to delete written key. %s", err)
}
_, err = store.KeyRead(params.ctx, kidWritten)
if !errors.Is(err, ErrKeyNotFound) {
t.Fatalf("Should have specific error when reading missing key.\n Actual: %s\n Expected: %s", err, ErrKeyNotFound)
}
}
func TestMemoryKeyReadAll(t *testing.T) {
params := setupMemory()
defer params.cancel()
store := params.jwks
jwk := newStorageTestJWK(t, hmacKey1, kidWritten)
err := store.KeyWrite(params.ctx, jwk)
if err != nil {
t.Fatalf("Failed to write key 1. %s", err)
}
jwk = newStorageTestJWK(t, hmacKey2, kidWritten2)
err = store.KeyWrite(params.ctx, jwk)
if err != nil {
t.Fatalf("Failed to write key 2. %s", err)
}
keys, err := store.KeyReadAll(params.ctx)
if err != nil {
t.Fatalf("Failed to snapshot keys. %s", err)
}
if len(keys) != 2 {
t.Fatalf("Snapshot should have 2 keys. %d", len(keys))
}
kid1Found := false
kid2Found := false
for _, jwk := range keys {
if !kid1Found && jwk.Marshal().KID == kidWritten {
kid1Found = true
if !bytes.Equal(jwk.Key().([]byte), hmacKey1) {
t.Fatalf("Snapshot key does not match written key.")
}
} else if !kid2Found && jwk.Marshal().KID == kidWritten2 {
kid2Found = true
if !bytes.Equal(jwk.Key().([]byte), hmacKey2) {
t.Fatalf("Snapshot key does not match written key.")
}
} else {
t.Fatalf("Snapshot key has unexpected key ID.")
}
}
}
func TestMemoryKeyWrite(t *testing.T) {
params := setupMemory()
defer params.cancel()
store := params.jwks
jwk := newStorageTestJWK(t, hmacKey1, kidWritten)
err := store.KeyWrite(params.ctx, jwk)
if err != nil {
t.Fatalf("Failed to write key. %s", err)
}
jwk = newStorageTestJWK(t, hmacKey2, kidWritten)
err = store.KeyWrite(params.ctx, jwk)
if err != nil {
t.Fatalf("Failed to overwrite key. %s", err)
}
}
func setupMemory() (params storageTestParams) {
jwkSet := NewMemoryStorage()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
params = storageTestParams{
ctx: ctx,
cancel: cancel,
jwks: jwkSet,
}
return params
}
func newStorageTestJWK(t *testing.T, key any, keyID string) JWK {
marshal := JWKMarshalOptions{
Private: true,
}
metadata := JWKMetadataOptions{
KID: keyID,
}
options := JWKOptions{
Marshal: marshal,
Metadata: metadata,
}
jwk, err := NewJWKFromKey(key, options)
if err != nil {
t.Fatalf("Failed to create JWK. %s", err)
}
return jwk
}