-
Notifications
You must be signed in to change notification settings - Fork 0
/
vault_test.go
224 lines (180 loc) · 4.23 KB
/
vault_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
package vault
import (
"bytes"
"os"
"testing"
)
func TestNewVault(t *testing.T) {
_, err := NewVault("/not/found")
if err == nil {
t.Error("expected error")
}
err = os.MkdirAll("/tmp/vault", 0700)
if err != nil {
t.Error(err)
}
_, err = NewVault("/tmp/vault")
if err != nil {
t.Error(err)
}
}
var Password = []byte("password")
var Secret = []byte("secret")
var KeyPath = "/tmp/vault/2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"
func setup(t *testing.T) *Vault {
s := &Vault{
Storage: Fs("/tmp/vault"),
}
err := os.MkdirAll("/tmp/vault", 0700)
if err != nil {
t.Errorf("Failed to create test directory: %s", err)
}
return s
}
//func TestLocalStorage_pathFor(t *testing.T) {
// s := &LocalStorage{
// Path: "/vault",
// }
//
// if s.pathFor("foo") != "/vault/foo" {
// t.Errorf("Expected pathFor to return /vault/foo, got %s", s.pathFor("foo"))
// }
//
// if s.pathFor("foo/bar") != "/vault/foo/bar" {
// t.Errorf("Expected pathFor to return /vault/foo/bar, got %s", s.pathFor("foo/bar"))
// }
//}
func TestLocalStorage_Delete(t *testing.T) {
s := setup(t)
defer teardown(t)
err := s.Set("foo", Secret, Password)
if err != nil {
t.Errorf("Failed to set value: %s", err)
}
if !s.Has("foo") {
t.Errorf("Key foo should exist")
}
if err = s.Delete("foo"); err != nil {
t.Error(err)
}
if s.Has("foo") {
t.Error("File should have been deleted")
}
}
func TestLocalStorage_Get(t *testing.T) {
s := setup(t)
defer teardown(t)
err := s.Set("foo", Secret, Password)
if err != nil {
t.Errorf("Failed to create test file: %s", err)
}
contents, err := s.Get("foo", Password)
if err != nil {
t.Error(err)
}
if !bytes.Equal(contents, Secret) {
t.Errorf("Expected get to return 'secret', got '%s'", contents)
}
}
func TestLocalStorage_Get2(t *testing.T) {
s := setup(t)
defer teardown(t)
_, err := s.Get("non-existent", Password)
if err == nil {
t.Error("Expected error when a key does not exist")
}
}
func TestLocalStorage_Get3(t *testing.T) {
s := setup(t)
defer teardown(t)
err := os.WriteFile(KeyPath, []byte("INVALID VAULT FILE"), 0777)
if err != nil {
t.Errorf("Failed to create test file: %s", err)
}
_, err = s.Get("foo", Password)
if err != ErrInvalidVaultFile {
t.Errorf("Expected get to return ErrInvalidVaultFile, got %s", err)
}
}
func TestLocalStorage_Get4(t *testing.T) {
s := setup(t)
defer teardown(t)
err := os.WriteFile(KeyPath, []byte("$VAULT;0.0;AES256\n0"), 0777)
if err != nil {
t.Errorf("Failed to create test file: %s", err)
}
_, err = s.Get("foo", Password)
if err != ErrInvalidVaultFile {
t.Errorf("Expected get to return ErrInvalidVaultVersion, got %s", err)
}
}
func TestLocalStorage_Get5(t *testing.T) {
s := setup(t)
defer teardown(t)
err := os.WriteFile(KeyPath, []byte("$VAULT;1.0;INVALID\n0"), 0777)
if err != nil {
t.Errorf("Failed to create test file: %s", err)
}
_, err = s.Get("foo", Password)
if err != ErrInvalidVaultFile {
t.Errorf("Expected get to return ErrInvalidCipher, got %s", err)
}
}
func TestLocalStorage_Set2(t *testing.T) {
s := setup(t)
defer teardown(t)
err := s.Set("nested.key", Secret, Password)
if err != nil {
t.Error(err)
}
if !s.Storage.Has("nested.key") {
t.Error(err)
}
}
func TestLocalStorage_Set3(t *testing.T) {
s := &Vault{
Storage: Fs("/vault"), // permission denied
}
err := s.Set("foo", Secret, Password)
if err == nil {
t.Error("Expected error, got nil")
}
}
func TestLocalStorage_Has(t *testing.T) {
s := setup(t)
defer teardown(t)
if s.Has("foo") {
t.Errorf("Expected Has(foo) to return false, got true")
}
err := s.Set("foo", []byte("bar"), []byte("password"))
if err != nil {
t.Error(err)
}
if s.Has("foo") == false {
t.Errorf("Expected Has(foo) to return true, got false")
}
}
func TestLocalStorage_Set(t *testing.T) {
s := setup(t)
defer teardown(t)
err := s.Set("foo", []byte("bar"), []byte("password"))
if err != nil {
t.Error(err)
}
if !s.Has("foo") {
t.Error(err)
}
value, err := s.Get("foo", []byte("password"))
if err != nil {
t.Error(err)
}
if string(value) != "bar" {
t.Errorf("Expected get to return 'bar', got '%s'", value)
}
}
func teardown(t *testing.T) {
err := os.RemoveAll("/tmp/vault")
if err != nil {
t.Error(err)
}
}