-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsecret_store_test.go
More file actions
131 lines (109 loc) · 3.25 KB
/
Copy pathsecret_store_test.go
File metadata and controls
131 lines (109 loc) · 3.25 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
package fastlike
import (
"testing"
)
func TestSecretStore(t *testing.T) {
// Create a simple secret lookup function
secretLookup := func(key string) ([]byte, bool) {
secrets := map[string][]byte{
"api_key": []byte("secret-api-key-12345"),
"db_password": []byte("super-secret-password"),
}
value, found := secrets[key]
return value, found
}
// Create minimal wasm bytes (just a valid wasm module header)
// This is enough to test instance creation without running it
wasmBytes := []byte{
0x00, 0x61, 0x73, 0x6d, // wasm magic number
0x01, 0x00, 0x00, 0x00, // version
}
// Create an instance with a secret store
instance := NewInstance(
wasmBytes,
WithSecretStore("my_secrets", secretLookup),
)
if instance == nil {
t.Fatal("Failed to create instance")
}
// Verify the secret store was registered
if len(instance.secretStores) != 1 {
t.Fatalf("Expected 1 secret store, got %d", len(instance.secretStores))
}
if instance.secretStores[0].name != "my_secrets" {
t.Errorf("Expected secret store name 'my_secrets', got '%s'", instance.secretStores[0].name)
}
// Test secret lookup directly
value, found := instance.secretStores[0].lookup("api_key")
if !found {
t.Error("Expected to find 'api_key' secret")
}
if string(value) != "secret-api-key-12345" {
t.Errorf("Expected 'secret-api-key-12345', got '%s'", string(value))
}
// Test non-existent secret
_, found = instance.secretStores[0].lookup("nonexistent")
if found {
t.Error("Should not find 'nonexistent' secret")
}
}
func TestSecretHandles(t *testing.T) {
handles := &SecretHandles{}
// Test creating secrets
plaintext1 := []byte("secret-value-1")
handle1 := handles.New(plaintext1)
plaintext2 := []byte("secret-value-2")
handle2 := handles.New(plaintext2)
if handle1 == handle2 {
t.Error("Expected different handles for different secrets")
}
// Test retrieving secrets
secret1 := handles.Get(handle1)
if secret1 == nil {
t.Fatal("Expected to retrieve secret1")
}
if string(secret1.Plaintext()) != string(plaintext1) {
t.Errorf("Expected '%s', got '%s'", string(plaintext1), string(secret1.Plaintext()))
}
secret2 := handles.Get(handle2)
if secret2 == nil {
t.Fatal("Expected to retrieve secret2")
}
if string(secret2.Plaintext()) != string(plaintext2) {
t.Errorf("Expected '%s', got '%s'", string(plaintext2), string(secret2.Plaintext()))
}
// Test invalid handle
invalid := handles.Get(999)
if invalid != nil {
t.Error("Expected nil for invalid handle")
}
}
func TestSecretStoreHandles(t *testing.T) {
handles := &SecretStoreHandles{}
// Test creating store handles
handle1 := handles.New("store1")
handle2 := handles.New("store2")
if handle1 == handle2 {
t.Error("Expected different handles for different stores")
}
// Test retrieving store handles
store1 := handles.Get(handle1)
if store1 == nil {
t.Fatal("Expected to retrieve store1")
}
if store1.name != "store1" {
t.Errorf("Expected 'store1', got '%s'", store1.name)
}
store2 := handles.Get(handle2)
if store2 == nil {
t.Fatal("Expected to retrieve store2")
}
if store2.name != "store2" {
t.Errorf("Expected 'store2', got '%s'", store2.name)
}
// Test invalid handle
invalid := handles.Get(999)
if invalid != nil {
t.Error("Expected nil for invalid handle")
}
}