-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_config_test.go
More file actions
190 lines (144 loc) · 4.51 KB
/
path_config_test.go
File metadata and controls
190 lines (144 loc) · 4.51 KB
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
package kmipengine
import (
"fmt"
"testing"
"time"
"github.com/cryacry/kmip-plugins/helper/namespace"
"github.com/hashicorp/vault/sdk/logical"
"github.com/stretchr/testify/assert"
)
// TestConfig mocks the creation, read, update, and delete
// of the backend configuration for HashiCups.
func TestConfig(t *testing.T) {
b, reqStorage := getTestBackend(t)
var err error
d := map[string]interface{}{
"default_tls_client_ttl": (112 * time.Hour).String(),
"server_hostnames": []string{"localhost", "kmipserver"},
"tls_ca_key_bits": 1025,
}
d1 := map[string]interface{}{
"default_tls_client_key_bits": 2048,
"default_tls_client_key_type": rsaKeyType,
"default_tls_client_ttl": (112 * time.Hour).String(),
"listen_addrs": []string{"0.0.0.0:5696"},
"server_hostnames": []string{"localhost", "kmipserver"},
"server_ips": []string{"127.0.0.1", "::1"}, // 将拆分后的IP列表赋值给server_ips
"tls_ca_key_bits": 1024,
"tls_ca_key_type": rsaKeyType,
"tls_min_version": "tls12",
}
scopes := []string{"aaa", "bbb", "ccc"}
roles := []string{"qq", "ww", "ee"}
roleConf := map[string]interface{}{
"operation_add_attribute": true,
"operation_create": true,
}
t.Run("Test Configuration", func(t *testing.T) {
err = testConfigCreate(t, b, reqStorage, map[string]interface{}{
"listen_addrs": []string{"0.0.0.0:5696"},
})
assert.NoError(t, err)
err = testConfigRead(t, b, reqStorage, DefaultConfigMap())
assert.NoError(t, err)
err = testCARead(t, b, reqStorage, map[string]interface{}{})
assert.NoError(t, err)
// create scope role credential
err = testScopeCreate(t, b, reqStorage, scopes)
assert.NoError(t, err)
err = testRoleCreate(t, b, reqStorage, scopes, roles, roleConf)
assert.NoError(t, err)
err = testCredentialGenerate(t, b, reqStorage, scopes, roles)
assert.NoError(t, err)
// update config
err = testConfigUpdate(t, b, reqStorage, d)
assert.NoError(t, err)
err = testConfigRead(t, b, reqStorage, d1)
assert.NoError(t, err)
})
}
func testConfigCreate(t *testing.T, b logical.Backend, s logical.Storage, d map[string]interface{}) error {
ctx := namespace.RootContext(nil)
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.CreateOperation,
Path: configPath,
Data: d,
Storage: s,
})
if err != nil {
return err
}
if resp != nil && resp.IsError() {
return resp.Error()
}
return nil
}
func testConfigUpdate(t *testing.T, b logical.Backend, s logical.Storage, d map[string]interface{}) error {
ctx := namespace.RootContext(nil)
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.UpdateOperation,
Path: configPath,
Data: d,
Storage: s,
})
if err != nil {
return err
}
if resp != nil && resp.IsError() {
return resp.Error()
}
return nil
}
func testConfigRead(t *testing.T, b logical.Backend, s logical.Storage, expected map[string]interface{}) error {
ctx := namespace.RootContext(nil)
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.ReadOperation,
Path: configPath,
Storage: s,
})
if err != nil {
return err
}
if resp == nil && expected == nil {
return nil
}
if resp.IsError() {
return resp.Error()
}
//return fmt.Errorf("read data mismatch (expected %v values, got %v)", expected, resp.Data)
if len(expected) != len(resp.Data) {
return fmt.Errorf("read data mismatch (expected %v values, got %v)", expected, resp.Data)
}
//for k, expectedV := range expected {
// actualV, ok := resp.Data[k]
//
// if !ok {
// return fmt.Errorf(`expected data["%s"] = %v but was not included in read output"`, k, expectedV)
// } else if expectedV != actualV {
// return fmt.Errorf(`expected data["%s"] = %v, instead got %v"`, k, expectedV, actualV)
// }
//}
return nil
}
func testCARead(t *testing.T, b logical.Backend, s logical.Storage, expected map[string]interface{}) error {
ctx := namespace.RootContext(nil)
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.ReadOperation,
Path: caPath,
Storage: s,
})
if err != nil {
return err
}
if resp == nil && expected == nil {
return nil
}
if resp.IsError() {
return resp.Error()
}
//return fmt.Errorf("read data mismatch (expected %v values, got %v)", expected, resp.Data)
if data := resp.Data["ca_pem"].(string); len(data) <= 0 {
return fmt.Errorf("read data mismatch (expected %v values, got %v)", expected, resp.Data)
}
return nil
}