-
-
Notifications
You must be signed in to change notification settings - Fork 478
/
tester.go
221 lines (168 loc) · 5.32 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
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
package tests
import (
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/gopasspw/gopass/tests/can"
"github.com/gopasspw/gopass/tests/gptest"
shellquote "github.com/kballard/go-shellquote"
"github.com/stretchr/testify/require"
)
const (
gopassConfig = `[core]
exportkeys = false
`
keyID = "BE73F104"
)
// ErrNoCommand is returned when the command is missing.
var ErrNoCommand = fmt.Errorf("no command")
type tester struct {
t *testing.T
// Binary is the path to the gopass binary used for testing
Binary string
sourceDir string
tempDir string
resetFn func()
}
func newTester(t *testing.T) *tester {
t.Helper()
sourceDir := "."
if d := os.Getenv("GOPASS_TEST_DIR"); d != "" {
sourceDir = d
}
gopassBin := ""
if b := os.Getenv("GOPASS_BINARY"); b != "" {
gopassBin = b
}
fi, err := os.Stat(gopassBin)
if err != nil {
t.Skipf("Failed to stat GOPASS_BINARY %s: %s", gopassBin, err)
}
if !strings.HasSuffix(gopassBin, ".exe") && fi.Mode()&0o111 == 0 {
t.Fatalf("GOPASS_BINARY is not executeable")
}
t.Logf("Using gopass binary: %s", gopassBin)
ts := &tester{
t: t,
sourceDir: sourceDir,
Binary: gopassBin,
}
// create tempDir
td, err := os.MkdirTemp("", "gopass-")
require.NoError(t, err)
t.Logf("Tempdir: %s", td)
ts.tempDir = td
// prepare ENVIRONMENT
ts.resetFn = gptest.UnsetVars("GNUPGHOME", "GOPASS_DEBUG", "NO_COLOR", "GOPASS_CONFIG", "GOPASS_NO_NOTIFY", "GOPASS_HOMEDIR")
require.NoError(t, os.Setenv("GNUPGHOME", ts.gpgDir()))
require.NoError(t, os.Setenv("GOPASS_DEBUG", ""))
require.NoError(t, os.Setenv("NO_COLOR", "true"))
require.NoError(t, os.Setenv("GOPASS_CONFIG_NOSYSTEM", "true"))
require.NoError(t, os.Setenv("GOPASS_CONFIG_NO_MIGRATE", "true"))
require.NoError(t, os.Setenv("GOPASS_NO_NOTIFY", "true"))
require.NoError(t, os.Setenv("GOPASS_HOMEDIR", td))
// write config
require.NoError(t, os.MkdirAll(filepath.Dir(ts.gopassConfig()), 0o700))
// we need to set the root path to something else than the root directory otherwise the mounts will show as regular entries
if err := os.WriteFile(ts.gopassConfig(), []byte(gopassConfig+"\n[mounts]\npath = "+ts.storeDir("root")+"\n"), 0o600); err != nil {
t.Fatalf("Failed to write gopass config to %s: %s", ts.gopassConfig(), err)
}
// copy gpg test files
require.NoError(t, can.WriteTo(ts.gpgDir()))
return ts
}
func (ts tester) gpgDir() string {
return filepath.Join(ts.tempDir, ".gnupg")
}
func (ts tester) gopassConfig() string {
return filepath.Join(ts.tempDir, ".config", "gopass", "config")
}
func (ts tester) storeDir(mount string) string {
return filepath.Join(ts.tempDir, ".local", "share", "gopass", "stores", mount)
}
func (ts tester) workDir() string {
return filepath.Dir(ts.tempDir)
}
func (ts tester) teardown() {
ts.resetFn() // restore env vars
if ts.tempDir == "" {
return
}
err := os.RemoveAll(ts.tempDir)
require.NoError(ts.t, err)
}
func (ts tester) runCmd(args []string, in []byte) (string, error) {
ts.t.Helper()
if len(args) < 1 {
return "", fmt.Errorf("invalid args %v: %w", args, ErrNoCommand)
}
cmd := exec.CommandContext(context.Background(), 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) {
ts.t.Helper()
if runtime.GOOS == "windows" {
arg = strings.ReplaceAll(arg, "\\", "\\\\")
}
args, err := shellquote.Split(arg)
if err != nil {
return "", fmt.Errorf("failed to split args %v: %w", arg, err)
}
cmd := exec.CommandContext(context.Background(), 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) runWithInput(arg, input string) ([]byte, error) {
ts.t.Helper()
reader := strings.NewReader(input)
return ts.runWithInputReader(arg, reader)
}
func (ts tester) runWithInputReader(arg string, input io.Reader) ([]byte, error) {
ts.t.Helper()
args, err := shellquote.Split(arg)
if err != nil {
return nil, fmt.Errorf("failed to split args %v: %w", arg, err)
}
cmd := exec.Command(ts.Binary, args...)
cmd.Dir = ts.workDir()
cmd.Stdin = input
ts.t.Logf("%+v", cmd.Args)
buf, err := cmd.CombinedOutput()
if err != nil {
return buf, fmt.Errorf("%s %v failed: %w", ts.Binary, args, err)
}
return buf, nil
}
func (ts *tester) initStore() {
out, err := ts.run("init --crypto=gpgcli --storage=fs " + keyID)
require.NoError(ts.t, err, "failed to init password store:\n%s", out)
}
func (ts *tester) initSecrets(prefix string) {
out, err := ts.run("generate -p " + prefix + "foo/bar 20")
require.NoError(ts.t, err, "failed to generate password:\n%s", out)
out, err = ts.run("generate -p " + prefix + "baz 40")
require.NoError(ts.t, err, "failed to generate password:\n%s", out)
out, err = ts.runCmd([]string{ts.Binary, "insert", prefix + "fixed/secret"}, []byte("moar"))
require.NoError(ts.t, err, "failed to insert password:\n%s", out)
out, err = ts.runCmd([]string{ts.Binary, "insert", prefix + "fixed/twoliner"}, []byte("first line\nsecond line"))
require.NoError(ts.t, err, "failed to insert password:\n%s", out)
}