-
Notifications
You must be signed in to change notification settings - Fork 98
/
api_test.go
686 lines (592 loc) · 19.8 KB
/
api_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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
// Copyright 2023 - MinIO, Inc. All rights reserved.
// Use of this source code is governed by the AGPLv3
// license that can be found in the LICENSE file.
package kes
import (
"bytes"
"crypto/hmac"
"errors"
"net/http"
"runtime"
"slices"
"strconv"
"testing"
"time"
"aead.dev/mem"
"github.com/minio/kms-go/kes"
)
func TestImportKey(t *testing.T) {
t.Parallel()
ctx := testContext(t)
srv, url := startServer(ctx, nil)
defer srv.Close()
client := defaultClient(url)
const name = "my-key"
for i, test := range importKeyTests {
name := name + "-" + strconv.Itoa(i)
err := client.ImportKey(ctx, name, &kes.ImportKeyRequest{
Key: test.Key,
Cipher: test.Cipher,
})
if err == nil && test.ShouldFail {
t.Errorf("Test %d: setup: creating key '%s' should have failed", i, name)
}
if err != nil && !test.ShouldFail {
t.Errorf("Test %d: setup: failed to create key '%s': %v", i, name, err)
}
}
}
func TestAPI(t *testing.T) {
t.Parallel()
t.Run("v1/metrics", testMetrics)
t.Run("v1/api", testListAPIDefaults)
t.Run("v1/status", testStatus)
t.Run("v1/key/create", testCreateKey)
t.Run("v1/key/delete", testDeleteKey)
t.Run("v1/key/import", testImportKey)
t.Run("v1/key/describe", testDescribeKey)
t.Run("v1/key/generate", testGenerateKey)
t.Run("v1/key/hmac", testHMAC)
t.Run("v1/key/encrypt", testEncryptDecryptKey) // also tests decryption
t.Run("v1/key/list", testListKeys)
t.Run("v1/identity/describe", testDescribeIdentity)
t.Run("v1/identity/list", testListIdentities)
t.Run("v1/identity/self/describe", testSelfDescribeIdentity)
t.Run("v1/policy/describe", testDescribePolicy)
t.Run("v1/policy/read", testReadPolicy)
t.Run("v1/policy/list", testListPolicies)
}
func testMetrics(t *testing.T) {
t.Parallel()
ctx := testContext(t)
srv, url := startServer(ctx, nil)
defer srv.Close()
client := defaultClient(url)
metric, err := client.Metrics(ctx)
if err != nil {
t.Fatalf("Failed fetch server metrics: %v", err)
}
if n := metric.RequestOK + metric.RequestErr + metric.RequestFail; n != metric.RequestN() {
t.Fatalf("metrics request count differs: got %d - want %d", n, metric.RequestN())
}
if metric.CPUs == 0 {
t.Fatalf("metrics contains no number of CPUs")
}
if metric.HeapAlloc == 0 {
t.Fatalf("metrics contains no heap allocations")
}
if metric.HeapObjects == 0 {
t.Fatalf("metrics contains no heap objects")
}
if metric.StackAlloc == 0 {
t.Fatalf("metrics contains no stack allocations")
}
}
func testStatus(t *testing.T) {
t.Parallel()
ctx := testContext(t)
srv, url := startServer(ctx, nil)
defer srv.Close()
client := defaultClient(url)
stat, err := client.Status(ctx)
if err != nil {
t.Fatalf("Failed to fetch status information: %v", err)
}
if stat.Arch != runtime.GOARCH {
t.Fatalf("Invalid status: got '%s' - want '%s'", stat.Arch, runtime.GOARCH)
}
if stat.OS != runtime.GOOS {
t.Fatalf("Invalid status: got '%s' - want '%s'", stat.OS, runtime.GOOS)
}
if stat.StackAlloc == 0 {
t.Fatal("Invalid status: allocated stack memory cannot be 0")
}
if stat.HeapAlloc == 0 {
t.Fatal("Invalid status: allocated heap memory cannot be 0")
}
}
func testListAPIDefaults(t *testing.T) {
defaults := map[string]struct {
Method string
MaxBody mem.Size
Timeout time.Duration
}{
"/version": {Method: http.MethodGet, MaxBody: 0, Timeout: 10 * time.Second},
"/v1/ready": {Method: http.MethodGet, MaxBody: 0, Timeout: 15 * time.Second},
"/v1/status": {Method: http.MethodGet, MaxBody: 0, Timeout: 15 * time.Second},
"/v1/metrics": {Method: http.MethodGet, MaxBody: 0, Timeout: 15 * time.Second},
"/v1/api": {Method: http.MethodGet, MaxBody: 0, Timeout: 10 * time.Second},
"/v1/key/create/": {Method: http.MethodPut, MaxBody: 0, Timeout: 15 * time.Second},
"/v1/key/import/": {Method: http.MethodPut, MaxBody: 1 * mem.MB, Timeout: 15 * time.Second},
"/v1/key/describe/": {Method: http.MethodGet, MaxBody: 0, Timeout: 15 * time.Second},
"/v1/key/list/": {Method: http.MethodGet, MaxBody: 0, Timeout: 15 * time.Second},
"/v1/key/delete/": {Method: http.MethodDelete, MaxBody: 0, Timeout: 15 * time.Second},
"/v1/key/generate/": {Method: http.MethodPut, MaxBody: 1 * mem.MB, Timeout: 15 * time.Second},
"/v1/key/encrypt/": {Method: http.MethodPut, MaxBody: 1 * mem.MB, Timeout: 15 * time.Second},
"/v1/key/decrypt/": {Method: http.MethodPut, MaxBody: 1 * mem.MB, Timeout: 15 * time.Second},
"/v1/key/hmac/": {Method: http.MethodPut, MaxBody: 1 * mem.MB, Timeout: 15 * time.Second},
"/v1/policy/describe/": {Method: http.MethodGet, MaxBody: 0, Timeout: 15 * time.Second},
"/v1/policy/read/": {Method: http.MethodGet, MaxBody: 0, Timeout: 15 * time.Second},
"/v1/policy/list/": {Method: http.MethodGet, MaxBody: 0, Timeout: 15 * time.Second},
"/v1/identity/describe/": {Method: http.MethodGet, MaxBody: 0, Timeout: 15 * time.Second},
"/v1/identity/self/describe": {Method: http.MethodGet, MaxBody: 0, Timeout: 15 * time.Second},
"/v1/identity/list/": {Method: http.MethodGet, MaxBody: 0, Timeout: 15 * time.Second},
"/v1/log/error": {Method: http.MethodGet, MaxBody: 0, Timeout: 0},
"/v1/log/audit": {Method: http.MethodGet, MaxBody: 0, Timeout: 0},
}
t.Parallel()
ctx := testContext(t)
srv, url := startServer(ctx, nil)
defer srv.Close()
client := defaultClient(url)
routes, err := client.APIs(ctx)
if err != nil {
t.Fatalf("Failed fetch server APIs: %v", err)
}
if len(routes) != len(defaults) {
t.Fatalf("Routes mismatch: got len '%d' - want len '%d'", len(routes), len(defaults))
}
for i := range routes {
api, ok := defaults[routes[i].Path]
if !ok {
t.Fatalf("Route '%s': not found", routes[i].Path)
}
if routes[i].Method != api.Method {
t.Fatalf("Route '%s': method mismatch: got '%s' - want '%s'", routes[i].Path, routes[i].Method, api.Method)
}
if routes[i].MaxBody != int64(api.MaxBody) {
t.Fatalf("Route '%s': max body mismatch: got '%d' - want '%d'", routes[i].Path, routes[i].MaxBody, api.MaxBody)
}
if routes[i].Timeout != api.Timeout {
t.Fatalf("Route '%s': timeout mismatch: got '%v' - want '%v'", routes[i].Path, routes[i].Timeout, api.Timeout)
}
}
}
func testCreateKey(t *testing.T) {
t.Parallel()
ctx := testContext(t)
srv, url := startServer(ctx, nil)
defer srv.Close()
client := defaultClient(url)
for i, test := range validNameTests {
err := client.CreateKey(ctx, test.Name)
if err == nil && test.ShouldFail {
t.Errorf("Test %d: creating key '%s' should have failed", i, test.Name)
}
if err != nil && !test.ShouldFail {
t.Errorf("Test %d: failed to create key '%s': %v", i, test.Name, err)
}
}
}
func testDeleteKey(t *testing.T) {
t.Parallel()
ctx := testContext(t)
srv, url := startServer(ctx, nil)
defer srv.Close()
client := defaultClient(url)
for i, test := range validNameTests {
err := client.CreateKey(ctx, test.Name)
if err == nil && test.ShouldFail {
t.Errorf("Test %d: setup: creating key '%s' should have failed", i, test.Name)
}
if err != nil && !test.ShouldFail {
t.Errorf("Test %d: setup: failed to create key '%s': %v", i, test.Name, err)
}
if test.ShouldFail {
continue
}
if err := client.DeleteKey(ctx, test.Name); err != nil {
t.Errorf("Test %d: failed to delete key '%s': %v", i, test.Name, err)
}
}
}
func testImportKey(t *testing.T) {
t.Parallel()
ctx := testContext(t)
srv, url := startServer(ctx, nil)
defer srv.Close()
client := defaultClient(url)
for i, test := range validNameTests {
err := client.ImportKey(ctx, test.Name, &kes.ImportKeyRequest{
Key: make([]byte, 32),
Cipher: kes.AES256,
})
if err == nil && test.ShouldFail {
t.Errorf("Test %d: setup: creating key '%s' should have failed", i, test.Name)
}
if err != nil && !test.ShouldFail {
t.Errorf("Test %d: setup: failed to create key '%s': %v", i, test.Name, err)
}
}
}
func testDescribeKey(t *testing.T) {
t.Parallel()
ctx := testContext(t)
srv, url := startServer(ctx, nil)
defer srv.Close()
client := defaultClient(url)
for i, test := range validNameTests {
err := client.CreateKey(ctx, test.Name)
if err == nil && test.ShouldFail {
t.Errorf("Test %d: setup: creating key '%s' should have failed", i, test.Name)
}
if err != nil && !test.ShouldFail {
t.Errorf("Test %d: setup: failed to create key '%s': %v", i, test.Name, err)
}
if test.ShouldFail {
continue
}
info, err := client.DescribeKey(ctx, test.Name)
if err != nil {
t.Errorf("Test %d: failed to describe key '%s': %v", i, test.Name, err)
}
if info.Algorithm > kes.ChaCha20 {
t.Errorf("Test %d: failed to describe key '%s': invalid algorithm '%d'", i, test.Name, info.Algorithm)
}
if info.CreatedAt.IsZero() {
t.Errorf("Test %d: failed to describe key '%s': created_at is zero", i, test.Name)
}
if info.CreatedBy.IsUnknown() {
t.Errorf("Test %d: failed to describe key '%s': created_by is empty", i, test.Name)
}
}
}
func testGenerateKey(t *testing.T) {
t.Parallel()
ctx := testContext(t)
srv, url := startServer(ctx, nil)
defer srv.Close()
associatedData := make([]byte, 80)
client := defaultClient(url)
for i, test := range validNameTests {
err := client.CreateKey(ctx, test.Name)
if err == nil && test.ShouldFail {
t.Errorf("Test %d: setup: creating key '%s' should have failed", i, test.Name)
}
if err != nil && !test.ShouldFail {
t.Errorf("Test %d: setup: failed to create key '%s': %v", i, test.Name, err)
}
if test.ShouldFail {
continue
}
dek, err := client.GenerateKey(ctx, test.Name, associatedData)
if err != nil {
t.Errorf("Test %d: failed to generate DEK with key '%s': %v", i, test.Name, err)
}
plaintext, err := client.Decrypt(ctx, test.Name, dek.Ciphertext, associatedData)
if err != nil {
t.Errorf("Test %d: failed to decrypt DEK with key '%s': %v", i, test.Name, err)
}
if !bytes.Equal(plaintext, dek.Plaintext) {
t.Errorf("Test %d: plaintext mismatch: got %v - want %v", i, plaintext, dek.Plaintext)
}
dek2, err := client.GenerateKey(ctx, test.Name, associatedData)
if err != nil {
t.Errorf("Test %d: failed to generate DEK with key '%s': %v", i, test.Name, err)
}
if bytes.Equal(dek.Plaintext, dek2.Plaintext) {
t.Errorf("Test %d: generate key is deterministic and produces the same DEKs", i)
}
if bytes.Equal(dek.Ciphertext, dek2.Ciphertext) {
t.Errorf("Test %d: generate key is deterministic and produces the same DEK ciphertexts", i)
}
}
}
func testHMAC(t *testing.T) {
t.Parallel()
ctx := testContext(t)
srv, url := startServer(ctx, nil)
defer srv.Close()
message1 := []byte("Hello World")
message2 := []byte("Hello World!")
client := defaultClient(url)
for i, test := range validNameTests {
err := client.CreateKey(ctx, test.Name)
if err == nil && test.ShouldFail {
t.Errorf("Test %d: setup: creating key '%s' should have failed", i, test.Name)
}
if err != nil && !test.ShouldFail {
t.Errorf("Test %d: setup: failed to create key '%s': %v", i, test.Name, err)
}
if test.ShouldFail {
continue
}
sum1, err := client.HMAC(ctx, test.Name, message1)
if err != nil {
t.Errorf("Test %d: failed to compute HMAC with key '%s': %v", i, test.Name, err)
}
sum2, err := client.HMAC(ctx, test.Name, message2)
if err != nil {
t.Errorf("Test %d: failed to compute HMAC with key '%s': %v", i, test.Name, err)
}
if hmac.Equal(sum1, sum2) {
t.Errorf("Test %d: HMACs of different messages are equal: got '%x' and '%x'", i, sum1, sum2)
}
verifySum, err := client.HMAC(ctx, test.Name, message1)
if err != nil {
t.Errorf("Test %d: failed to compute HMAC with key '%s': %v", i, test.Name, err)
}
if !hmac.Equal(sum1, verifySum) {
t.Errorf("Test %d: HMACs of equal messages are not equal: got '%x' and '%x'", i, sum1, verifySum)
}
}
}
func testEncryptDecryptKey(t *testing.T) {
t.Parallel()
ctx := testContext(t)
srv, url := startServer(ctx, nil)
defer srv.Close()
plaintext := make([]byte, mem.KB)
associatedData := make([]byte, 80)
client := defaultClient(url)
for i, test := range validNameTests {
err := client.CreateKey(ctx, test.Name)
if err == nil && test.ShouldFail {
t.Errorf("Test %d: setup: creating key '%s' should have failed", i, test.Name)
}
if err != nil && !test.ShouldFail {
t.Errorf("Test %d: setup: failed to create key '%s': %v", i, test.Name, err)
}
if test.ShouldFail {
continue
}
ciphertext, err := client.Encrypt(ctx, test.Name, plaintext, associatedData)
if err != nil {
t.Errorf("Test %d: failed to encrypt with key '%s': %v", i, test.Name, err)
}
ptext, err := client.Decrypt(ctx, test.Name, ciphertext, associatedData)
if err != nil {
t.Errorf("Test %d: failed to decrypt with key '%s': %v", i, test.Name, err)
}
if !bytes.Equal(ptext, plaintext) {
t.Errorf("Test %d: plaintext mismatch: got %v - want %v", i, ptext, plaintext)
}
ctext, err := client.Encrypt(ctx, test.Name, plaintext, associatedData)
if err != nil {
t.Errorf("Test %d: failed to encrypt with key '%s': %v", i, test.Name, err)
}
if bytes.Equal(ctext, ciphertext) {
t.Errorf("Test %d: encryption is deterministic and produces the same ciphertexts", i)
}
}
}
func testListKeys(t *testing.T) {
t.Parallel()
ctx := testContext(t)
srv, url := startServer(ctx, nil)
defer srv.Close()
var names []string
client := defaultClient(url)
for i, test := range validNameTests {
err := client.CreateKey(ctx, test.Name)
if err == nil && test.ShouldFail {
t.Errorf("Test %d: setup: creating key '%s' should have failed", i, test.Name)
}
if err != nil && !test.ShouldFail {
t.Errorf("Test %d: setup: failed to create key '%s': %v", i, test.Name, err)
}
if !test.ShouldFail {
names = append(names, test.Name)
}
}
slices.Sort(names)
keys, _, err := client.ListKeys(ctx, "", -1)
if err != nil {
t.Fatalf("Failed to list keys: %v", err)
}
if !slices.Equal(names, keys) {
t.Fatalf("Failed to list keys: got %v - want %v", keys, names)
}
}
func testDescribeIdentity(t *testing.T) {
t.Parallel()
ctx := testContext(t)
srv, url := startServer(ctx, nil)
defer srv.Close()
var identities []kes.Identity
for _, test := range validNameTests {
if validName(test.Name) {
identities = append(identities, kes.Identity(test.Name))
}
}
if err := srv.UpdatePolicies(map[string]Policy{"policy": {Identities: identities}}); err != nil {
t.Fatalf("Failed to update server policies: %v", err)
}
client := defaultClient(url)
for i, id := range identities {
info, err := client.DescribeIdentity(ctx, id)
if err != nil {
t.Fatalf("Test %d: failed to describe identity '%s': %v", i, id, err)
}
if info.IsAdmin {
t.Errorf("Test %d: identity '%s' is admin", i, id)
}
if !info.ExpiresAt.IsZero() || info.TTL > 0 {
t.Errorf("Test %d: identity '%s' expires", i, id)
}
if info.CreatedBy != defaultIdentity {
t.Errorf("Test %d: identity '%s' was not created by '%s'", i, id, defaultIdentity)
}
}
}
func testListIdentities(t *testing.T) {
t.Parallel()
ctx := testContext(t)
srv, url := startServer(ctx, nil)
defer srv.Close()
names := []kes.Identity{
"8ed87d812abbf280ffa760080873d0d503fdfa9c41c1bf32b4cffd1dc71b1d1c",
"34d90ce76fbc40ab8354ea8c42c17b3f20e1f63a10f6cef787a94394b02141c4",
"cd0dd4c3efab6a5744d1e9b1754dbe7f612bd759062d6f17a8ef25f47fc86c54",
"59ba6afc8e844ba36edcf8ed50c23cb62626ea420e9b9ca7509ab6fa6d13ad3a",
"disabled",
}
if err := srv.UpdatePolicies(map[string]Policy{"policy": {Identities: names}}); err != nil {
t.Fatalf("Failed to update server policies: %v", err)
}
names = append(names, defaultIdentity) // Listing identities always includes the admin identity
slices.Sort(names)
client := defaultClient(url)
identities, _, err := client.ListIdentities(ctx, "", -1)
if err != nil {
t.Fatalf("Failed to list identities: %v", err)
}
if !slices.Equal(names, identities) {
t.Fatalf("Failed to list identities: got %v - want %v", identities, names)
}
}
func testSelfDescribeIdentity(t *testing.T) {
t.Parallel()
ctx := testContext(t)
srv, url := startServer(ctx, nil)
defer srv.Close()
client := defaultClient(url)
info, _, err := client.DescribeSelf(ctx)
if err != nil {
t.Fatalf("Failed to self-describe identity: %v", err)
}
if !info.IsAdmin {
t.Error("Failed to self-describe identity: not the admin")
}
if info.Identity.String() != defaultIdentity {
t.Errorf("Failed to self-describe identity: got '%s' - want '%s'", info.Identity.String(), defaultIdentity)
}
}
func testDescribePolicy(t *testing.T) {
t.Parallel()
ctx := testContext(t)
srv, url := startServer(ctx, nil)
defer srv.Close()
policies := make(map[string]Policy)
for _, test := range validNameTests {
if validName(test.Name) {
policies[test.Name] = Policy{}
}
}
if err := srv.UpdatePolicies(policies); err != nil {
t.Fatalf("Failed to update server policies: %v", err)
}
client := defaultClient(url)
for i, test := range validNameTests {
info, err := client.DescribePolicy(ctx, test.Name)
if err == nil && test.ShouldFail {
t.Errorf("Test %d: describing policy '%s' should have failed", i, test.Name)
}
if err != nil && !test.ShouldFail {
t.Errorf("Test %d: failed to describe policy '%s': %v", i, test.Name, err)
}
if !validName(test.Name) && errors.Is(err, kes.ErrPolicyNotFound) {
t.Errorf("Test %d: received %v for invalid policy name '%s'", i, err, test.Name)
}
if test.ShouldFail {
continue
}
if info.Name != test.Name {
t.Errorf("Test %d: invalid name: got '%s' - want '%s'", i, info.Name, test.Name)
}
}
}
func testReadPolicy(t *testing.T) {
t.Parallel()
policy := kes.Policy{
Allow: map[string]kes.Rule{
"/v1/status": {},
"/v1/ready": {},
"/v1/key/create/*": {},
"/v1/key/generate/*": {},
"/v1/key/decrypt/*": {},
},
Deny: map[string]kes.Rule{"/v1/key/decrypt/internal*": {}},
}
ctx := testContext(t)
srv, url := startServer(ctx, nil)
defer srv.Close()
policies := make(map[string]Policy)
for _, test := range validNameTests {
if validName(test.Name) {
policies[test.Name] = Policy{
Allow: policy.Allow,
Deny: policy.Deny,
}
}
}
if err := srv.UpdatePolicies(policies); err != nil {
t.Fatalf("Failed to update server policies: %v", err)
}
client := defaultClient(url)
for i, test := range validNameTests {
p, err := client.GetPolicy(ctx, test.Name)
if err == nil && test.ShouldFail {
t.Errorf("Test %d: reading policy '%s' should have failed", i, test.Name)
}
if err != nil && !test.ShouldFail {
t.Errorf("Test %d: failed to read policy '%s': %v", i, test.Name, err)
}
if !validName(test.Name) && errors.Is(err, kes.ErrPolicyNotFound) {
t.Errorf("Test %d: received %v for invalid policy name '%s'", i, err, test.Name)
}
if test.ShouldFail {
continue
}
if !p.IsSubset(&policy) || !policy.IsSubset(p) {
t.Errorf("Test %d: policy mismatch: got '%v' - want '%v'", i, p, policy)
}
}
}
func testListPolicies(t *testing.T) {
t.Parallel()
ctx := testContext(t)
srv, url := startServer(ctx, nil)
defer srv.Close()
var names []string
policies := make(map[string]Policy)
for _, test := range validNameTests {
if validName(test.Name) {
policies[test.Name] = Policy{}
names = append(names, test.Name)
}
}
if err := srv.UpdatePolicies(policies); err != nil {
t.Fatalf("Failed to update server policies: %v", err)
}
slices.Sort(names)
client := defaultClient(url)
list, _, err := client.ListPolicies(ctx, "", -1)
if err != nil {
t.Fatalf("Failed to list policies: %v", err)
}
if !slices.Equal(names, list) {
t.Fatalf("Failed to list policies: got %v - want %v", list, names)
}
}
var importKeyTests = []struct {
Key []byte
Cipher kes.KeyAlgorithm
ShouldFail bool
}{
{Key: make([]byte, 32), Cipher: kes.AES256}, // 0
{Key: make([]byte, 32), Cipher: kes.ChaCha20}, // 1
{Key: make([]byte, 16), Cipher: kes.AES256, ShouldFail: true}, // 2
{Key: make([]byte, 24), Cipher: kes.ChaCha20, ShouldFail: true}, // 3
{Key: make([]byte, 32), Cipher: kes.ChaCha20 + 1, ShouldFail: true}, // 4
}