This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
path_ejson_identity_test.go
80 lines (68 loc) · 2.03 KB
/
path_ejson_identity_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
package secretsejson
import (
"context"
"crypto/rand"
"fmt"
"testing"
"github.com/hashicorp/vault/sdk/logical"
)
func TestEJSON_Identity_default(t *testing.T) {
b, storage := getTestBackend(t)
dataInput := map[string]interface{}{
"plaintext": "a",
}
req := &logical.Request{
Operation: logical.CreateOperation,
Path: "identity",
Storage: storage,
Data: dataInput,
}
resp, err := b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err:%s resp:%#v\n", err, resp)
}
expected := "fcc648ebfb10143d55ccf1a80eb40071f94e86f4650b51d4c52cf26eba6474cc"
if resp.Data["identity"] != expected {
t.Fatalf("Bad identity string with default salt: \nGot: %#v\nWant: %#v", resp.Data["identity"], expected)
}
}
func TestEJSON_Identity_non_default(t *testing.T) {
b, storage := getTestBackend(t)
randomSalt := make([]byte, 32)
_, err := rand.Read(randomSalt)
if err != nil {
t.Fatalf("test failed due to rand.Read eror: %s", err)
}
// First set `__secret_salt`
dataInput := map[string]interface{}{
"private": fmt.Sprintf("%x", randomSalt),
}
req := &logical.Request{
Operation: logical.CreateOperation,
Path: "keys/__secret_salt",
Storage: storage,
Data: dataInput,
}
resp, err := b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err:%s resp:%#v\n", err, resp)
}
// Then generate identity string with salt set
dataInput = map[string]interface{}{
"plaintext": "a",
}
req = &logical.Request{
Operation: logical.CreateOperation,
Path: "identity",
Storage: storage,
Data: dataInput,
}
resp, err = b.HandleRequest(context.Background(), req)
if err != nil || (resp != nil && resp.IsError()) {
t.Fatalf("err:%s resp:%#v\n", err, resp)
}
expected := "fcc648ebfb10143d55ccf1a80eb40071f94e86f4650b51d4c52cf26eba6474cc"
if resp.Data["identity"] == expected {
t.Fatalf("Bad identity string with set salt: \nGot: %#v\nDid not want: %#v", resp.Data["identity"], expected)
}
}