forked from Shopify/ecfg
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathecfg_test.go
218 lines (197 loc) · 5.91 KB
/
ecfg_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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package ecfg
import (
"io/ioutil"
"os"
"reflect"
"regexp"
"strings"
"testing"
)
func TestGenerateKeypair(t *testing.T) {
pub, priv, err := GenerateKeypair()
assertNoError(t, err)
if pub == priv {
t.Errorf("pub == priv")
}
if strings.Contains(pub, "00000") {
t.Errorf("pubkey looks sketchy")
}
if strings.Contains(priv, "00000") {
t.Errorf("privkey looks sketchy")
}
}
func assertNoError(t *testing.T, err error) {
if err != nil {
t.Errorf("unexpected error: %v", err)
}
}
func TestEncryptFileInPlace(t *testing.T) {
getMode = func(p string) (os.FileMode, error) {
return 0400, nil
}
defer func() { getMode = _getMode }()
_, err := EncryptFileInPlace("/does/not/exist", FileTypeJSON)
if !os.IsNotExist(err) {
t.Errorf("expected IsNotExist, got %v", err)
}
// invalid json file
readFile = func(p string) ([]byte, error) {
return []byte(`{"a": "b"]`), nil
}
_, err = EncryptFileInPlace("/doesnt/matter", FileTypeJSON)
readFile = ioutil.ReadFile
if err == nil {
t.Errorf("expected error, but none was received")
} else {
if !strings.Contains(err.Error(), "invalid character") {
t.Errorf("wanted json error, but got %v", err)
}
}
// invalid key
readFile = func(p string) ([]byte, error) {
return []byte(`{"_public_key": "invalid"}`), nil
}
_, err = EncryptFileInPlace("/doesnt/matter", FileTypeJSON)
readFile = ioutil.ReadFile
if err == nil {
t.Errorf("expected error, but none was received")
} else {
if !strings.Contains(err.Error(), "public key has invalid format") {
t.Errorf("wanted key error, but got %v", err)
}
}
// valid keypair
readFile = func(p string) ([]byte, error) {
return []byte(`{"_public_key": "8d8647e2eeb6d2e31228e6df7da3df921ec3b799c3f66a171cd37a1ed3004e7d", "a": "b"}`), nil
}
var output []byte
writeFile = func(path string, data []byte, mode os.FileMode) error {
output = data
return nil
}
_, err = EncryptFileInPlace("/doesnt/matter", FileTypeJSON)
readFile = ioutil.ReadFile
writeFile = ioutil.WriteFile
assertNoError(t, err)
match := regexp.MustCompile(`{"_public_key": "8d8.*", "a": "EJ.*"}`)
if match.Find(output) == nil {
t.Errorf("unexpected output: %s", output)
}
}
func TestDecryptFile(t *testing.T) {
_, err := DecryptFile("/does/not/exist", []string{"/doesnt/matter"}, FileTypeJSON)
if !os.IsNotExist(err) {
t.Errorf("expected IsNotExist, but got %v", err)
}
// invalid json file
readFile = func(p string) ([]byte, error) {
return []byte(`{"a": "b"]`), nil
}
_, err = DecryptFile("/doesnt/matter", []string{"/doesnt/matter"}, FileTypeJSON)
readFile = ioutil.ReadFile
if err == nil {
t.Errorf("expected error, but none was received")
} else {
if !strings.Contains(err.Error(), "invalid character") {
t.Errorf("wanted json error, but got %v", err)
}
}
readFile = func(p string) ([]byte, error) {
return []byte(`{"_public_key": "invalid"}`), nil
}
_, err = DecryptFile("/doesnt/matter", []string{"/doesnt/matter"}, FileTypeJSON)
readFile = ioutil.ReadFile
if err == nil {
t.Errorf("expected error, but none was received")
} else {
if !strings.Contains(err.Error(), "public key has invalid format") {
t.Errorf("wanted key error, but got %v", err)
}
}
// valid keypair but no corresponding entry in keydir
readFile = func(p string) ([]byte, error) {
if p == "a" {
return []byte(`{"_public_key": "8d8647e2eeb6d2e31228e6df7da3df921ec3b799c3f66a171cd37a1ed3004e7d", "a": "b"}`), nil
}
return ioutil.ReadFile("/does/not/exist")
}
_, err = DecryptFile("a", []string{"b"}, FileTypeJSON)
readFile = ioutil.ReadFile
if err == nil {
t.Errorf("expected error, but none was received")
} else {
if !strings.Contains(err.Error(), "private key not found") {
t.Errorf("wanted key file error, but got %v", err)
}
}
// valid keypair and a corresponding entry in keydir
readFile = func(p string) ([]byte, error) {
if p == "a" {
return []byte(`{"_public_key": "8d8647e2eeb6d2e31228e6df7da3df921ec3b799c3f66a171cd37a1ed3004e7d", "a": "EJ[1:KR1IxNZnTZQMP3OR1NdOpDQ1IcLD83FSuE7iVNzINDk=:XnYW1HOxMthBFMnxWULHlnY4scj5mNmX:ls1+kvwwu2ETz5C6apgWE7Q=]"}`), nil
}
return []byte("c5caa31a5b8cb2be0074b37c56775f533b368b81d8fd33b94181f79bd6e47f87"), nil
}
out, err := DecryptFile("a", []string{"b"}, FileTypeJSON)
readFile = ioutil.ReadFile
assertNoError(t, err)
if string(out) != `{"_public_key": "8d8647e2eeb6d2e31228e6df7da3df921ec3b799c3f66a171cd37a1ed3004e7d", "a": "b"}` {
t.Errorf("unexpected output")
}
}
func stubKeypathStuff(uid int, xdgConfigHome, home string) func() {
getuid = func() int { return uid }
getenv = func(k string) string {
if k == "XDG_CONFIG_HOME" {
return xdgConfigHome
}
return home
}
return func() {
getuid = os.Getuid
getenv = os.Getenv
}
}
func TestKeypathsForNonRootWithXDGConfigHomeSet(t *testing.T) {
defer stubKeypathStuff(501, "/Users/me/.config", "/Users/me")()
{
expected := []string{"/Users/me/.config/ecfg/keys", "/Users/me/.ecfg/keys"}
if !reflect.DeepEqual(expected, UserKeypath()) {
t.Error("invalid keypath")
}
}
{
expected := []string{"/Users/me/.config/ecfg/keys", "/Users/me/.ecfg/keys", "/etc/ecfg/keys", "/opt/ejson/keys"}
if !reflect.DeepEqual(expected, DefaultKeypath()) {
t.Error("invalid keypath")
}
}
}
func TestKeypathsForNonRootWithoutXDGConfigHomeSet(t *testing.T) {
defer stubKeypathStuff(501, "", "/Users/me")()
{
expected := []string{"/Users/me/.ecfg/keys"}
if !reflect.DeepEqual(expected, UserKeypath()) {
t.Error("invalid keypath")
}
}
{
expected := []string{"/Users/me/.ecfg/keys", "/etc/ecfg/keys", "/opt/ejson/keys"}
if !reflect.DeepEqual(expected, DefaultKeypath()) {
t.Error("invalid keypath")
}
}
}
func TestKeypathsForRoot(t *testing.T) {
defer stubKeypathStuff(0, "/root/.config", "/root")()
{
if len(UserKeypath()) > 0 {
t.Error("invalid keypath")
}
}
{
expected := []string{"/etc/ecfg/keys", "/opt/ejson/keys"}
if !reflect.DeepEqual(expected, DefaultKeypath()) {
t.Error("invalid keypath")
}
}
}