forked from 99designs/keyring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pass_test.go
156 lines (126 loc) · 2.91 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
package keyring
import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"sort"
"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: "aws-vault",
}
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 %q", len(keys))
}
}
func TestPassKeyringKeysWhenNotEmpty(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)
}
item = Item{Key: "alpacas", Data: []byte("alpacas are better")}
if err := k.Set(item); err != nil {
t.Fatal(err)
}
keys, err := k.Keys()
if err != nil {
t.Fatal(err)
}
if len(keys) != 2 {
t.Fatalf("Expected 2 keys, got %d", len(keys))
}
sort.Strings(keys)
if keys[0] != "alpacas" {
t.Fatalf("Expected alpacas")
}
if keys[1] != "llamas" {
t.Fatalf("Expected llamas")
}
}
func TestPassKeyringRemove(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))
}
}