forked from 99designs/keyring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpass_test.go
199 lines (162 loc) · 3.84 KB
/
pass_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
// +build !windows
package keyring
import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"reflect"
"testing"
)
func setup(t *testing.T) (*passKeyring, func(t *testing.T)) {
pwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
tmpdir, err := ioutil.TempDir("", "keyring-pass-test")
if err != nil {
t.Fatal(err)
}
// Initialise a blank GPG homedir; import & trust the test key
gnupghome := filepath.Join(tmpdir, ".gnupg")
os.Mkdir(gnupghome, os.FileMode(int(0700)))
os.Setenv("GNUPGHOME", gnupghome)
os.Unsetenv("GPG_AGENT_INFO")
os.Unsetenv("GPG_TTY")
cmd := exec.Command("gpg", "--import", filepath.Join(pwd, "testdata", "test-gpg.key"))
err = cmd.Run()
if err != nil {
t.Fatal(err)
}
cmd = exec.Command("gpg", "--import-ownertrust", filepath.Join(pwd, "testdata", "test-ownertrust-gpg.txt"))
err = cmd.Run()
if err != nil {
t.Fatal(err)
}
passdir := filepath.Join(tmpdir, ".password-store")
k := &passKeyring{
dir: passdir,
passcmd: "pass",
prefix: "keyring",
}
cmd, err = k.pass("init", "[email protected]")
if err != nil {
t.Fatal(err)
}
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
t.Fatal(err)
}
return k, func(t *testing.T) {
os.RemoveAll(tmpdir)
}
}
func TestPassKeyringSetWhenEmpty(t *testing.T) {
k, teardown := setup(t)
defer teardown(t)
item := Item{Key: "llamas", Data: []byte("llamas are great")}
if err := k.Set(item); err != nil {
t.Fatal(err)
}
foundItem, err := k.Get("llamas")
if err != nil {
t.Fatal(err)
}
if string(foundItem.Data) != "llamas are great" {
t.Fatalf("Value stored was not the value retrieved: %q", foundItem.Data)
}
if foundItem.Key != "llamas" {
t.Fatalf("Key wasn't persisted: %q", foundItem.Key)
}
}
func TestPassKeyringKeysWhenEmpty(t *testing.T) {
k, teardown := setup(t)
defer teardown(t)
keys, err := k.Keys()
if err != nil {
t.Fatal(err)
}
if len(keys) != 0 {
t.Fatalf("Expected 0 keys, got %d", len(keys))
}
}
func TestPassKeyringKeysWhenNotEmpty(t *testing.T) {
k, teardown := setup(t)
defer teardown(t)
items := []Item{
{Key: "llamas", Data: []byte("llamas are great")},
{Key: "alpacas", Data: []byte("alpacas are better")},
{Key: "africa/elephants", Data: []byte("who doesn't like elephants")},
}
for _, item := range items {
if err := k.Set(item); err != nil {
t.Fatal(err)
}
}
keys, err := k.Keys()
if err != nil {
t.Fatal(err)
}
if len(keys) != len(items) {
t.Fatalf("Expected %d keys, got %d", len(items), len(keys))
}
expectedKeys := []string{
"africa/elephants",
"alpacas",
"llamas",
}
if !reflect.DeepEqual(keys, expectedKeys) {
t.Fatalf("Expected keys %v, got %v", expectedKeys, keys)
}
}
func TestPassKeyringRemoveWhenEmpty(t *testing.T) {
k, teardown := setup(t)
defer teardown(t)
err := k.Remove("no-such-key")
if err != ErrKeyNotFound {
t.Fatalf("expected ErrKeyNotFound, got: %v", err)
}
}
func TestPassKeyringRemoveWhenNotEmpty(t *testing.T) {
k, teardown := setup(t)
defer teardown(t)
item := Item{Key: "llamas", Data: []byte("llamas are great")}
if err := k.Set(item); err != nil {
t.Fatal(err)
}
if err := k.Remove(item.Key); err != nil {
t.Fatalf(err.Error())
}
keys, err := k.Keys()
if err != nil {
t.Fatal(err)
}
if len(keys) != 0 {
t.Fatalf("Expected 0 keys, got %d", len(keys))
}
}
func TestPassKeyringGetWhenEmpty(t *testing.T) {
k, teardown := setup(t)
defer teardown(t)
_, err := k.Get("no-such-key")
if err != ErrKeyNotFound {
t.Fatalf("expected ErrKeyNotFound, got: %v", err)
}
}
func TestPassKeyringGetWhenNotEmpty(t *testing.T) {
k, teardown := setup(t)
defer teardown(t)
item := Item{Key: "llamas", Data: []byte("llamas are great")}
if err := k.Set(item); err != nil {
t.Fatal(err)
}
v1, err := k.Get(item.Key)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(v1.Data, item.Data) {
t.Fatal("Expected item not returned")
}
}