forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit.go
105 lines (91 loc) · 2.19 KB
/
unit.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
package gptest
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
const (
gopassConfig = `askformore: false
autoimport: true
autosync: true
cliptimeout: 45
noconfirm: true
safecontent: true`
)
var (
defaultEntries = []string{"foo"}
defaultRecipients = []string{"0xDEADBEEF"}
)
type Unit struct {
t *testing.T
Entries []string
Recipients []string
Dir string
env map[string]string
}
func (u Unit) GPConfig() string {
return filepath.Join(u.Dir, ".gopass.yml")
}
func (u Unit) GPGHome() string {
return filepath.Join(u.Dir, ".gnupg")
}
func NewUnitTester(t *testing.T) *Unit {
td, err := ioutil.TempDir("", "gopass-")
assert.NoError(t, err)
u := &Unit{
t: t,
Entries: defaultEntries,
Recipients: defaultRecipients,
Dir: td,
}
u.env = map[string]string{
"CHECKPOINT_DISABLE": "true",
"GNUPGHOME": u.GPGHome(),
"GOPASS_CONFIG": u.GPConfig(),
"GOPASS_EXPERIMENTAL_GOGIT": "",
"GOPASS_HOMEDIR": u.Dir,
"GOPASS_NOCOLOR": "true",
"GOPASS_NO_NOTIFY": "true",
"PAGER": "",
}
assert.NoError(t, setupEnv(u.env))
assert.NoError(t, os.Mkdir(u.GPGHome(), 0600))
assert.NoError(t, u.initConfig())
assert.NoError(t, u.InitStore(""))
return u
}
func (u Unit) initConfig() error {
return ioutil.WriteFile(u.GPConfig(), []byte(gopassConfig+"\npath: "+u.StoreDir("")+"\n"), 0600)
}
func (u Unit) StoreDir(mount string) string {
if mount != "" {
mount = "-" + mount
}
return filepath.Join(u.Dir, ".password-store"+mount)
}
func (u Unit) InitStore(name string) error {
dir := u.StoreDir(name)
if err := os.MkdirAll(dir, 0700); err != nil {
return err
}
if err := ioutil.WriteFile(filepath.Join(dir, ".gpg-id"), []byte(strings.Join(u.Recipients, "\n")), 0600); err != nil {
return err
}
for _, p := range AllPathsToSlash(u.Entries) {
if err := ioutil.WriteFile(filepath.Join(dir, p+".gpg"), []byte("secret"), 0600); err != nil {
return err
}
}
return nil
}
func (u *Unit) Remove() {
teardownEnv(u.env)
if u.Dir == "" {
return
}
assert.NoError(u.t, os.RemoveAll(u.Dir))
u.Dir = ""
}