-
Notifications
You must be signed in to change notification settings - Fork 369
/
integration_test.go
343 lines (289 loc) · 8.62 KB
/
integration_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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/StackExchange/blackbox/v2/pkg/bblog"
_ "github.com/StackExchange/blackbox/v2/pkg/bblog"
_ "github.com/StackExchange/blackbox/v2/pkg/vcs/_all"
)
var vcsToTest = flag.String("testvcs", "GIT", "VCS to test")
var longTests = flag.Bool("long", false, "Run long version of tests")
//var crypterToTest = flag.String("crypter", "GnuPG", "crypter to test")
func init() {
testing.Init()
flag.Parse()
op, err := os.Getwd()
if err != nil {
panic(err)
}
originPath = op
}
func compile(t *testing.T) {
if PathToBlackBox() != "" {
// It's been compiled already.
return
}
// Make sure we have the latest binary
fmt.Println("========== Compiling")
cmd := exec.Command("go", "build", "-o", "../bbintegration", "../cmd/blackbox")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
t.Fatalf("setup_compile: %v", err)
}
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
SetPathToBlackBox(filepath.Join(cwd, "../bbintegration"))
}
func setup(t *testing.T) {
logDebug := bblog.GetDebug(*verbose)
logDebug.Printf("flag.testvcs is %v", *vcsToTest)
vh := getVcs(t, *vcsToTest)
logDebug.Printf("Using BLACKBOX_VCS=%v", vh.Name())
os.Setenv("BLACKBOX_VCS", vh.Name())
}
func TestInit(t *testing.T) {
if !*longTests {
return
}
compile(t)
makeHomeDir(t, "init")
// Only zero or one args are permitted.
invalidArgs(t, "init", "one", "two")
invalidArgs(t, "init", "one", "two", "three")
runBB(t, "init", "yes")
assertFileEmpty(t, ".blackbox/blackbox-admins.txt")
assertFileEmpty(t, ".blackbox/blackbox-files.txt")
assertFilePerms(t, ".blackbox/blackbox-admins.txt", 0o640)
assertFilePerms(t, ".blackbox/blackbox-files.txt", 0o640)
}
func TestList(t *testing.T) {
if !*longTests {
return
}
compile(t)
makeHomeDir(t, "init")
runBB(t, "init", "yes")
createDummyFilesAdmin(t)
checkOutput("000-admin-list.txt", t, "admin", "list")
checkOutput("000-file-list.txt", t, "file", "list")
invalidArgs(t, "file", "list", "extra")
invalidArgs(t, "admin", "list", "extra")
}
func TestStatus(t *testing.T) {
if !*longTests {
return
}
compile(t)
makeHomeDir(t, "init")
runBB(t, "init", "yes")
createFilesStatus(t)
checkOutput("000-status.txt", t, "status")
}
func TestShred(t *testing.T) {
if !*longTests {
return
}
compile(t)
makeHomeDir(t, "shred")
runBB(t, "init", "yes")
makeFile(t, "shredme.txt", "File with SHREDME in it.\n")
assertFileExists(t, "shredme.txt")
runBB(t, "shred", "shredme.txt")
assertFileMissing(t, "shredme.txt")
}
func TestStatus_notreg(t *testing.T) {
if !*longTests {
return
}
compile(t)
makeHomeDir(t, "init")
runBB(t, "init", "yes")
createFilesStatus(t)
checkOutput("status-noreg.txt", t, "status", "status-ENCRYPTED.txt", "blah.txt")
}
// TestHard tests the functions using a fake homedir and repo.
func TestHard(t *testing.T) {
if !*longTests {
return
}
// These are basic tests that work on a fake repo.
// The repo has mostly real data, except any .gpg file
// is just garbage.
compile(t)
setup(t)
for _, cx := range []struct{ subname, prefix string }{
//{subname: ".", prefix: "."},
{subname: "mysub", prefix: ".."},
} {
subname := cx.subname
prefix := cx.prefix
_ = prefix
phase("========== SUBDIR = " + subname + " ==========")
makeHomeDir(t, "BasicAlice")
plaintextFoo := "I am the foo.txt file!\n"
plainAltered := "I am the altered file!\n"
runBB(t, "testing_init") // Runs "git init" or equiv
assertFileExists(t, ".git")
runBB(t, "init", "yes") // Creates .blackbox or equiv
if subname != "." {
err := os.Mkdir(subname, 0770)
if err != nil {
t.Fatal(fmt.Errorf("hard-mk-home %q: %v", subname, err))
}
}
olddir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
os.Chdir(subname)
os.Chdir(olddir)
phase("Alice creates a GPG key")
gpgdir := makeAdmin(t, "alice", "Alice Example", "alice@example.com")
become(t, "alice")
phase("Alice enrolls as an admin")
//os.Chdir(subname)
runBB(t, "admin", "add", "alice@example.com", gpgdir)
//os.Chdir(olddir)
// encrypt
phase("Alice registers foo.txt")
makeFile(t, "foo.txt", plaintextFoo)
//os.Chdir(subname)
//runBB(t, "file", "add", "--shred", filepath.Join(prefix, "foo.txt"))
runBB(t, "file", "add", "--shred", "foo.txt")
//os.Chdir(olddir)
// "file add" encrypts the file.
// We shred the plaintext so that we are sure that when Decrypt runs,
// we can verify the contents wasn't just sitting there all the time.
assertFileMissing(t, "foo.txt")
assertFileExists(t, "foo.txt.gpg")
phase("Alice decrypts foo.txt")
// decrypt
//os.Chdir(subname)
runBB(t, "decrypt", "foo.txt")
//runBB(t, "decrypt", filepath.Join(prefix, "foo.txt"))
//os.Chdir(olddir)
assertFileExists(t, "foo.txt")
assertFileExists(t, "foo.txt.gpg")
assertFileContents(t, "foo.txt", plaintextFoo)
// encrypts (without shredding)
phase("Alice encrypts foo.txt (again)")
runBB(t, "encrypt", "foo.txt")
assertFileExists(t, "foo.txt")
assertFileExists(t, "foo.txt.gpg")
assertFileContents(t, "foo.txt", plaintextFoo)
// reencrypt
phase("Alice reencrypts")
checkOutput("basic-status.txt", t, "status")
runBB(t, "reencrypt", "--overwrite", "foo.txt")
// Test variations of cat
// foo.txt=plain result=plain
phase("Alice cats plain:plain")
makeFile(t, "foo.txt", plaintextFoo)
assertFileExists(t, "foo.txt")
runBB(t, "encrypt", "foo.txt")
assertFileExists(t, "foo.txt")
assertFileExists(t, "foo.txt.gpg")
checkOutput("alice-cat-plain.txt", t, "cat", "foo.txt")
assertFileExists(t, "foo.txt")
assertFileExists(t, "foo.txt.gpg")
// foo.txt=altered result=plain
phase("Alice cats altered:plain")
makeFile(t, "foo.txt", plainAltered)
assertFileExists(t, "foo.txt")
assertFileExists(t, "foo.txt.gpg")
checkOutput("alice-cat-plain.txt", t, "cat", "foo.txt")
assertFileExists(t, "foo.txt")
assertFileExists(t, "foo.txt.gpg")
// foo.txt=missing result=plain
phase("Alice cats missing:plain")
removeFile(t, "foo.txt")
assertFileMissing(t, "foo.txt")
assertFileMissing(t, "foo.txt")
assertFileExists(t, "foo.txt.gpg")
checkOutput("alice-cat-plain.txt", t, "cat", "foo.txt")
assertFileMissing(t, "foo.txt")
assertFileExists(t, "foo.txt.gpg")
// Chapter 2: Bob
// Alice adds Bob.
// Bob encrypts a file.
// Bob makes sure he can decrypt alice's file.
// Bob removes Alice.
// Alice verifies she CAN'T decrypt files.
// Bob adds Alice back.
// Alice verifies she CAN decrypt files.
// Bob adds an encrypted file by mistake, "bb add" and fixes it.
// Bob corrupts the blackbox-admins.txt file, verifies that commands fail.
}
}
// TestEvilFilenames verifies commands work with "difficult" file names
func TestEvilFilenames(t *testing.T) {
if !*longTests {
return
}
compile(t)
setup(t)
makeHomeDir(t, "Mallory")
runBB(t, "testing_init") // Runs "git init" or equiv
assertFileExists(t, ".git")
runBB(t, "init", "yes") // Creates .blackbox or equiv
phase("Malory creates a GPG key")
gpgdir := makeAdmin(t, "mallory", "Mallory Evil", "mallory@example.com")
become(t, "mallory")
phase("Mallory enrolls as an admin")
runBB(t, "admin", "add", "mallory@example.com", gpgdir)
_ = os.MkdirAll("my/path/to", 0o770)
_ = os.Mkdir("other", 0o770)
for i, name := range []string{
"!important!.txt",
"#andpounds.txt",
"stars*bars?.txt",
"space space.txt",
"tab\ttab.txt",
"ret\rret.txt",
"smile😁eyes",
"¡que!",
"thé",
"pound£",
"*.go",
"rm -f erase ; echo done",
`smile☺`,
`dub𝓦`,
"my/path/to/relsecrets.txt",
//"my/../my/path/../path/to/myother.txt", // Not permitted yet
//"other/../my//path/../path/to/otherother.txt", // Not permitted yet
//"new\nnew.txt", // \n not permitted
//"two\n", // \n not permitted (yet)
//"four\U0010FFFF", // Illegal byte sequence. git won't accept.
} {
phase(fmt.Sprintf("Mallory tries %02d: %q", i, name))
contents := "the name of this file is the talking heads... i mean, " + name
makeFile(t, name, contents)
assertFileExists(t, name)
assertFileMissing(t, name+".gpg")
assertFileContents(t, name, contents)
runBB(t, "file", "add", name)
assertFileMissing(t, name)
assertFileExists(t, name+".gpg")
runBB(t, "decrypt", name)
assertFileExists(t, name)
assertFileExists(t, name+".gpg")
assertFileContents(t, name, contents)
runBB(t, "encrypt", name)
assertFileExists(t, name)
assertFileExists(t, name+".gpg")
assertFileContents(t, name, contents)
runBB(t, "shred", name)
assertFileMissing(t, name)
assertFileExists(t, name+".gpg")
}
}
// More tests to implement.
// 1. Verify that the --gid works (blackbox decrypt --gid)