-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_role_test.go
244 lines (193 loc) · 5.77 KB
/
path_role_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
package kmipengine
import (
"fmt"
"reflect"
"sort"
"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 TestRole(t *testing.T) {
b, reqStorage := getTestBackend(t)
scopes := []string{"aaa"}
roles := []string{"qq", "ww", "ee"}
d := map[string]interface{}{
"operation_add_attribute": true,
"operation_create": true,
}
d1 := map[string]interface{}{
"tls_client_key_ttl": (336 * time.Hour).String(),
"tls_client_key_bits": 2048,
"tls_client_key_type": rsaKeyType,
"operation_add_attribute": true,
"operation_create": true,
}
d2 := map[string]interface{}{
"tls_client_key_ttl": (112 * time.Hour).String(),
"tls_client_key_bits": 1024,
"tls_client_key_type": rsaKeyType,
"operation_add_attribute": true,
"operation_create": true,
}
t.Run("Test Configuration", func(t *testing.T) {
var err error
err = testConfigCreate(t, b, reqStorage, map[string]interface{}{
"listen_addrs": []string{"0.0.0.0:5696"},
})
assert.NoError(t, err)
err = testScopeCreate(t, b, reqStorage, scopes)
assert.NoError(t, err)
err = testRoleCreate(t, b, reqStorage, scopes, roles, d)
assert.NoError(t, err)
err = testRoleRead(t, b, reqStorage, scopes, roles, d1)
assert.NoError(t, err)
err = testRoleWrite(t, b, reqStorage, scopes, roles, d2)
assert.NoError(t, err)
err = testRoleRead(t, b, reqStorage, scopes, roles, d2)
assert.NoError(t, err)
err = testRoleList(t, b, reqStorage, scopes, roles)
assert.NoError(t, err)
err = testRoleDelete(t, b, reqStorage, scopes[0], roles[0])
assert.NoError(t, err)
err = testRoleList(t, b, reqStorage, scopes, roles[1:])
assert.NoError(t, err)
})
}
func testRoleCreate(t *testing.T, b logical.Backend, s logical.Storage, scopes, roles []string, d map[string]interface{}) error {
ctx := namespace.RootContext(nil)
for _, scope := range scopes {
for _, role := range roles {
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.CreateOperation,
Path: "scope/" + scope + "/role/" + role,
Data: d,
Storage: s,
})
if err != nil {
return err
}
if resp != nil && resp.IsError() {
return resp.Error()
}
}
}
return nil
}
func testRoleWrite(t *testing.T, b logical.Backend, s logical.Storage, scopes, roles []string, d map[string]interface{}) error {
ctx := namespace.RootContext(nil)
for _, scope := range scopes {
for _, role := range roles {
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.UpdateOperation,
Path: "scope/" + scope + "/role/" + role,
Data: d,
Storage: s,
})
if err != nil {
return err
}
if resp != nil && resp.IsError() {
return resp.Error()
}
}
}
return nil
}
func testRoleRead(t *testing.T, b logical.Backend, s logical.Storage, scopes, roles []string, expected map[string]interface{}) error {
ctx := namespace.RootContext(nil)
for _, scope := range scopes {
for _, role := range roles {
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.ReadOperation,
Path: "scope/" + scope + "/role/" + role,
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 %d values, got %v)", len(expected), resp.Data)
if len(expected) != len(resp.Data) {
return fmt.Errorf("read data mismatch (expected %d values, got %d)", len(expected), len(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 testRoleList(t *testing.T, b logical.Backend, s logical.Storage, scopes, expected []string) error {
ctx := namespace.RootContext(nil)
for _, scope := range scopes {
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.ListOperation,
Path: "scope/" + scope + "/role",
Storage: s,
})
if err != nil {
return err
}
if resp == nil && expected == nil {
return nil
}
if resp.IsError() {
return resp.Error()
}
var resData []string
if res, ok := resp.Data["keys"]; !ok {
return fmt.Errorf("res data miss keys")
} else if resData, ok = res.([]string); !ok {
return fmt.Errorf("res data miss keys")
}
if len(expected) != len(resData) {
return fmt.Errorf("read data mismatch (expected %s values, got %s)", expected, resp.Data)
}
//
if ok := areArraysEqual(expected, resData); !ok {
return fmt.Errorf(`expected %v, instead %v"`, expected, resData)
}
}
return nil
}
func testRoleDelete(t *testing.T, b logical.Backend, s logical.Storage, scope, role string) error {
ctx := namespace.RootContext(nil)
resp, err := b.HandleRequest(ctx, &logical.Request{
Operation: logical.DeleteOperation,
Path: "scope/" + scope + "/role/" + role,
Storage: s,
})
if err != nil {
return err
}
if resp != nil && resp.IsError() {
return resp.Error()
}
return nil
}
func areArraysEqual(arr1, arr2 []string) bool {
// 克隆数组以避免修改原始数据
sortedArr1 := make([]string, len(arr1))
copy(sortedArr1, arr1)
sortedArr2 := make([]string, len(arr2))
copy(sortedArr2, arr2)
// 对数组进行排序
sort.Strings(sortedArr1)
sort.Strings(sortedArr2)
// 比较排序后的数组
return reflect.DeepEqual(sortedArr1, sortedArr2)
}