forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tester.go
174 lines (139 loc) · 3.97 KB
/
tester.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
package tests
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
shellquote "github.com/kballard/go-shellquote"
"github.com/stretchr/testify/require"
)
const (
gopassConfig = `
alwaystrust: true
autoimport: true
autopull: true
autopush: true
cliptimeout: 45
loadkeys: true
noconfirm: true
persistkeys: true
safecontent: true`
)
type tester struct {
t *testing.T
// Binary is the path to the gopass binary used for testing
Binary string
sourceDir string
tempDir string
}
func newTester(t *testing.T) *tester {
sourceDir := "."
if d := os.Getenv("GOPASS_TEST_DIR"); d != "" {
sourceDir = d
}
gopassBin := "gopass"
if b := os.Getenv("GOPASS_BINARY"); b != "" {
gopassBin = b
}
t.Logf("Using gopass binary: %s", gopassBin)
ts := &tester{
t: t,
sourceDir: sourceDir,
Binary: gopassBin,
}
// create tempDir
td, err := ioutil.TempDir("", "gopass-")
require.NoError(t, err)
t.Logf("Tempdir: %s", td)
ts.tempDir = td
// prepare ENVIRONMENT
_ = os.Setenv("GNUPGHOME", ts.gpgDir())
_ = os.Setenv("GOPASS_DEBUG", "false")
_ = os.Setenv("GOPASS_NOCOLOR", "true")
_ = os.Setenv("GOPASS_CONFIG", ts.gopassConfig())
// write config
if err := ioutil.WriteFile(ts.gopassConfig(), []byte(gopassConfig+"\npath: "+ts.storeDir()+"\n"), 0600); err != nil {
t.Fatalf("Failed to write gopass config to %s: %s", ts.gopassConfig(), err)
}
// copy gpg test files
files := map[string]string{
ts.sourceDir + "/can/gnupg/pubring.gpg": ts.gpgDir() + "/pubring.gpg",
ts.sourceDir + "/can/gnupg/random_seed": ts.gpgDir() + "/random_seed",
ts.sourceDir + "/can/gnupg/secring.gpg": ts.gpgDir() + "/secring.gpg",
ts.sourceDir + "/can/gnupg/trustdb.gpg": ts.gpgDir() + "/trustdb.gpg",
}
for from, to := range files {
buf, err := ioutil.ReadFile(from)
require.NoError(t, err, "Failed to read file %s", from)
err = os.MkdirAll(filepath.Dir(to), 0700)
require.NoError(t, err, "Failed to create dir for %s", to)
err = ioutil.WriteFile(to, buf, 0600)
require.NoError(t, err, "Failed to write file %s", to)
}
return ts
}
func (ts tester) gpgDir() string {
return filepath.Join(ts.tempDir, ".gnupg")
}
func (ts tester) gopassConfig() string {
return filepath.Join(ts.tempDir, ".gopass.yml")
}
func (ts tester) storeDir() string {
return filepath.Join(ts.tempDir, ".password-store")
}
func (ts tester) workDir() string {
return filepath.Dir(ts.tempDir)
}
func (ts tester) teardown() {
if ts.tempDir == "" {
return
}
err := os.RemoveAll(ts.tempDir)
require.NoError(ts.t, err)
}
func (ts tester) runCmd(args []string, in []byte) (string, error) {
if len(args) < 1 {
return "", fmt.Errorf("no command")
}
cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = ts.workDir()
cmd.Stdin = bytes.NewReader(in)
ts.t.Logf("%+v", cmd.Args)
out, err := cmd.CombinedOutput()
if err != nil {
return string(out), err
}
return strings.TrimSpace(string(out)), nil
}
func (ts tester) run(arg string) (string, error) {
args, err := shellquote.Split(arg)
if err != nil {
return "", err
}
cmd := exec.Command(ts.Binary, args...)
cmd.Dir = ts.workDir()
ts.t.Logf("%+v", cmd.Args)
out, err := cmd.CombinedOutput()
if err != nil {
return string(out), err
}
return strings.TrimSpace(string(out)), nil
}
func (ts *tester) initializeStore() {
out, err := ts.run("init --nogit BE73F104")
require.NoError(ts.t, err, "failed to init password store:\n%s", out)
}
func (ts *tester) initializeSecrets() {
out, err := ts.run("generate foo/bar 20")
require.NoError(ts.t, err, "failed to generate password:\n%s", out)
out, err = ts.run("generate baz 40")
require.NoError(ts.t, err, "failed to generate password:\n%s", out)
out, err = ts.runCmd([]string{ts.Binary, "insert", "fixed/secret"}, []byte("moar"))
require.NoError(ts.t, err, "failed to insert password:\n%s", out)
out, err = ts.runCmd([]string{ts.Binary, "insert", "fixed/twoliner"}, []byte("and\nmore stuff"))
require.NoError(ts.t, err, "failed to insert password:\n%s", out)
}