-
Notifications
You must be signed in to change notification settings - Fork 10
/
key_test.go
121 lines (102 loc) · 1.79 KB
/
key_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package gcpkms
import (
"context"
"reflect"
"testing"
"github.com/hashicorp/vault/sdk/logical"
)
func TestKey_Key(t *testing.T) {
cases := []struct {
name string
c []byte
e *Key
err bool
}{
{
"default",
nil,
nil,
true,
},
{
"saved",
[]byte(`{"name":"foo", "crypto_key_id":"bar"}`),
&Key{
Name: "foo",
CryptoKeyID: "bar",
},
false,
},
{
"invalid",
[]byte(`{x`),
nil,
true,
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
b, storage := testBackend(t)
if tc.c != nil {
if err := storage.Put(context.Background(), &logical.StorageEntry{
Key: "keys/my-key",
Value: tc.c,
}); err != nil {
t.Fatal(err)
}
}
c, err := b.Key(context.Background(), storage, "my-key")
if (err != nil) != tc.err {
t.Fatal(err)
}
if !reflect.DeepEqual(c, tc.e) {
t.Errorf("expected %#v to be %#v", c, tc.e)
}
})
}
}
func TestKey_Keys(t *testing.T) {
cases := []struct {
name string
c []byte
e []string
err bool
}{
{
"default",
nil,
nil,
false,
},
{
"saved",
[]byte(`{"name":"foo", "crypto_key_id":"bar"}`),
[]string{"my-key"},
false,
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
b, storage := testBackend(t)
if tc.c != nil {
if err := storage.Put(context.Background(), &logical.StorageEntry{
Key: "keys/my-key",
Value: tc.c,
}); err != nil {
t.Fatal(err)
}
}
c, err := b.Keys(context.Background(), storage)
if (err != nil) != tc.err {
t.Fatal(err)
}
if !reflect.DeepEqual(c, tc.e) {
t.Errorf("expected %#v to be %#v", c, tc.e)
}
})
}
}