-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemount_test.go
482 lines (407 loc) · 11.5 KB
/
emount_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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"math/rand"
"os"
"strings"
"testing"
)
const testDirPrefix = "gotest_emount_"
// gocryptConfig is config file - we check for this to confirm vol was created
const gocryptConfig = "/gocryptfs.conf"
func TestInitCryptVolEmptyPw(t *testing.T) {
// empty password attempts to prompt for new password
err := initCryptVol("", "")
// expect "Input error: inappropriate ioctl for device"
if err == nil {
t.Errorf("expected prompt for password, err == nil\n")
}
if !strings.Contains(err.Error(), "Input error") {
t.Errorf("unexpected error for empty password: %v", err)
}
}
func TestInitCryptVol(t *testing.T) {
// generate random password of about 31-32 hex chars
password := fmt.Sprintf("%x%x", rand.Int63(), rand.Int63())
os.Setenv("EMOUNT_PASSWORD", password)
folder, err := ioutil.TempDir("", testDirPrefix)
okf(t, err)
defer func() {
_ = os.RemoveAll(folder)
}()
err = initCryptVol(folder, "")
okf(t, err)
fs, err := os.Stat(folder)
okf(t, err)
if fs.Mode()&0777 != 0700 {
t.Errorf("mode mismatch expected %o got %o", 0700, fs.Mode()&0777)
}
fs, err = os.Stat(folder + gocryptConfig)
okf(t, err)
assert(t, fs.Size() > 0, "crypt config", fs.Size())
}
type testFileInfo struct {
dirPath string
name string
contents []byte
perm os.FileMode
}
func getTestFileInfo() []testFileInfo {
return []testFileInfo{
// the names and file contents in this array are opaque to the test,
// since we iterate through the array, however there is an assumption
// in TestDecryptAndRun that the [0]th item is a file in the root directory.
{
dirPath: "",
name: "/abc.txt",
contents: []byte("hello_world"),
perm: 0600,
},
{
dirPath: "/sub",
name: "/xyz.txt",
contents: []byte("1234"),
perm: 0640,
},
{
dirPath: "/bin",
name: "/prog.exe",
contents: []byte{0, 1, 2, 3},
perm: 0755,
},
}
}
func createTestData() (string, error) {
folder, err := ioutil.TempDir("", testDirPrefix)
if err != nil {
return "", err
}
for _, tf := range getTestFileInfo() {
if tf.dirPath != "" {
if err = os.Mkdir(folder+tf.dirPath, 0700); err != nil {
return "", fmt.Errorf("creating tmp file %v", err)
}
}
if err = ioutil.WriteFile(folder+tf.name, tf.contents, tf.perm); err != nil {
return "", err
}
}
return folder, nil
}
func confirmTestData(t *testing.T, path string) error {
t.Helper()
fs, err := os.Stat(path)
ok(t, err)
assertf(t, fs.IsDir(), "tet folder is dir", fs.IsDir())
for _, tf := range getTestFileInfo() {
fs, err = os.Stat(path + tf.name)
ok(t, err)
mode := fs.Mode() & 0777
assert(t, tf.perm == mode,
fmt.Sprintf("mode mismatch %s expected %o", tf.name, tf.perm), mode)
data, err := ioutil.ReadFile(path + tf.name)
if err != nil {
t.Errorf("reading file %s: %v", tf.name, err)
}
if !bytes.Equal(data, tf.contents) {
t.Logf("expected: %v\nactual:%v\n", tf.contents, data)
t.Errorf("file contents mismatch")
}
}
return nil
}
func TestInitCryptVolCopy(t *testing.T) {
// generate random password of about 31-32 hex chars
password := fmt.Sprintf("%x%x", rand.Int63(), rand.Int63())
os.Setenv("EMOUNT_PASSWORD", password)
newCrypt, err := ioutil.TempDir("", testDirPrefix)
okf(t, err)
defer func() {
_ = os.RemoveAll(newCrypt)
}()
// create test data
dataFolder, err := createTestData()
okf(t, err)
defer func() {
_ = os.RemoveAll(dataFolder)
}()
err = initCryptVol(newCrypt, dataFolder)
okf(t, err)
// mount volume and confirm
// when second param is empty it creates temp mount, so we need to remove later
mp, err := mountCrypt(newCrypt, "", password)
okf(t, err)
err = confirmTestData(t, mp)
ok(t, err)
unmountVol(mp)
ok(t, os.RemoveAll(mp))
}
func TestDecryptAndRun(t *testing.T) {
sav := flag.CommandLine
defer func() {
flag.CommandLine = sav
}()
// generate random password of about 31-32 hex chars
password := fmt.Sprintf("%x%x", rand.Int63(), rand.Int63())
os.Setenv("EMOUNT_PASSWORD", password)
newCrypt, err := ioutil.TempDir("", testDirPrefix)
okf(t, err)
defer func() {
_ = os.RemoveAll(newCrypt)
}()
// create test data
dataFolder, err := createTestData()
okf(t, err)
defer func() {
_ = os.RemoveAll(dataFolder)
}()
// init & copy from test data
err = initCryptVol(newCrypt, dataFolder)
okf(t, err)
mountPoint, err := ioutil.TempDir("", testDirPrefix)
okf(t, err)
defer func() {
_ = os.RemoveAll(mountPoint)
}()
destDir, err := ioutil.TempDir("", testDirPrefix)
okf(t, err)
defer func() {
_ = os.RemoveAll(destDir)
}()
// use /bin/cp to copy file. Need to use abs path because path lookup is in parseArgs
// (path lookup was already tested with bash in TestParseArgs)
// use the 0th file in the test array, a file at the volume root
// so we don't need to do mkdir
tf := getTestFileInfo()[0]
assert(t, tf.dirPath == "", "test file[0] should be at root", tf.dirPath)
flag.CommandLine = flag.NewFlagSet("prog", flag.ExitOnError)
os.Args = []string{"prog", "-r", newCrypt, "-m", mountPoint,
"/bin/cp", mountPoint + tf.name, destDir + tf.name}
main()
// confirm that copy occurred
data, err := ioutil.ReadFile(destDir + tf.name)
ok(t, err)
assert(t, bytes.Equal(data, tf.contents), "cp", data)
// confirm that it's been unmounted
err = checkEmptyDir(mountPoint)
ok(t, err)
}
func TestInvalidPath(t *testing.T) {
_, err := mountCrypt("/usr/bin", "", "abc")
if err == nil || !strings.Contains(err.Error(), "no such file or directory") {
t.Errorf("expect 'no such file or directory', got %v", err)
}
}
func TestInvalidPassword(t *testing.T) {
// generate random password of about 31-32 hex chars
password := fmt.Sprintf("%x%x", rand.Int63(), rand.Int63())
os.Setenv("EMOUNT_PASSWORD", password)
newCrypt, err := ioutil.TempDir("", testDirPrefix)
okf(t, err)
defer func() {
_ = os.RemoveAll(newCrypt)
}()
err = initCryptVol(newCrypt, "")
ok(t, err)
_, err = mountCrypt(newCrypt, "", "abc")
if err == nil || !strings.Contains(err.Error(), "Invalid password") {
t.Errorf("expected Invalid password error, got: %v", err)
}
}
func TestUnmountWarning(t *testing.T) {
fmt.Printf("Ignore the following warning about unmount failure: ")
printUnmountWarning("")
}
func TestCheckEmptyDir(t *testing.T) {
path, err := ioutil.TempDir("", testDirPrefix)
okf(t, err)
defer func() {
_ = os.RemoveAll(path)
}()
err = checkEmptyDir(path)
ok(t, err)
err = ioutil.WriteFile(path+"/abc.txt", []byte("hello"), 0600)
okf(t, err)
err = checkEmptyDir(path)
if err == nil {
t.Errorf("expected checkEmptyDir to fail with file")
}
// check invalid path
err = checkEmptyDir(path)
if err == nil {
t.Errorf("expected error for invalid path")
}
// check file but not dir
tmpf, err := ioutil.TempFile("", testDirPrefix)
okf(t, err)
err = checkEmptyDir(tmpf.Name())
if err == nil {
t.Errorf("checkEmptyDir should have failed with file param")
}
ok(t, tmpf.Close())
ok(t, os.Remove(tmpf.Name()))
}
func TestCheckFolder(t *testing.T) {
cf := checkFolder("")
if cf != invalidPath {
t.Errorf("empty path expected invalidPath, got %v", cf)
}
cf = checkFolder(os.TempDir())
if cf != isDir {
t.Errorf("expected isDir for %s, got %v", os.TempDir(), cf)
}
tmpf, err := ioutil.TempFile("", testDirPrefix)
okf(t, err)
fname := tmpf.Name()
cf = checkFolder(fname)
if cf != isNotDir {
t.Errorf("expected notDir for temp file, got %v", cf)
}
// test invalid path
ok(t, tmpf.Close())
ok(t, os.Remove(fname))
cf = checkFolder(fname)
if cf != invalidPath {
t.Errorf("invalid fname, got %v", cf)
}
}
func TestParseArgs(t *testing.T) {
// we overwrite CommandLine a few times so we'll restore it in case
// the test framework depends on it
sav := flag.CommandLine
defer func() {
flag.CommandLine = sav
}()
dir, err := ioutil.TempDir("", testDirPrefix)
okf(t, err)
defer func() {
_ = os.RemoveAll(dir)
}()
// -i dir valid
t.Logf("-i dir (valid)")
os.Args = []string{"prog", "-i", dir}
opt := &options{}
flag.CommandLine = flag.NewFlagSet("prog", flag.ExitOnError)
err = parseArgs(opt)
ok(t, err)
if opt.init != dir {
t.Errorf("init flag")
}
file, err := ioutil.TempFile("", testDirPrefix)
okf(t, err)
fname := file.Name()
defer func() {
_ = file.Close()
_ = os.Remove(fname)
}()
// -i dir (invalid dir)
t.Logf("-i dir (invalid dir)")
os.Args = []string{"prog", "-i", fname}
opt = &options{}
flag.CommandLine = flag.NewFlagSet("prog", flag.ExitOnError)
err = parseArgs(opt)
if err == nil {
t.Errorf("expected arg fail with invalid dir")
}
// -i dir non-existent
t.Logf("-i dir (non-existent)")
os.Args = []string{"prog", "-i", dir + "/a/b/c"}
opt = &options{}
flag.CommandLine = flag.NewFlagSet("prog", flag.ExitOnError)
err = parseArgs(opt)
ok(t, err)
_ = os.RemoveAll(dir + "/a")
// -i dir -r dir (error: both)
t.Logf("-i dir -r dir")
os.Args = []string{"prog", "-i", "folder", "-r", "other"}
opt = &options{}
flag.CommandLine = flag.NewFlagSet("prog", flag.ExitOnError)
err = parseArgs(opt)
if err == nil {
t.Errorf("expected error for both -i and -r")
}
// -i dir non-empty
t.Logf("-i dir (non-empty)")
os.Args = []string{"prog", "-i", dir}
err = ioutil.WriteFile(dir+"/file.x", []byte{1}, 0600)
okf(t, err)
opt = &options{}
flag.CommandLine = flag.NewFlagSet("prog", flag.ExitOnError)
err = parseArgs(opt)
if err == nil {
t.Errorf("init non-empty dir expected error")
}
_ = os.Remove(dir + "/file.x")
// -r dir
t.Logf("-r dir bash echo hello")
os.Args = []string{"prog", "-r", dir, "bash", "echo", "hello"}
opt = &options{}
flag.CommandLine = flag.NewFlagSet("prog", flag.ExitOnError)
err = parseArgs(opt)
ok(t, err)
if opt.run != dir {
t.Errorf("run arg missing")
}
// this also tests the PATH lookup
// string.Contains should work for any of /bin/bash /usr/bin/bash, /usr/local/bin/bash
if !strings.Contains(opt.runCmd[0], "/bin/bash") {
t.Errorf("opt path search failed")
}
if opt.runCmd[1] != "echo" || opt.runCmd[2] != "hello" || len(opt.runCmd) != 3 {
t.Errorf("runCmd not packed correctly: %v", opt.runCmd)
}
// -r dir -m mountpoint
t.Logf("-r dir -m mountpoint")
mp, err := ioutil.TempDir("", testDirPrefix)
okf(t, err)
defer func() {
_ = os.RemoveAll(mp)
}()
os.Args = []string{"prog", "-r", dir, "-m", mp, "bash", "echo", "hello"}
opt = &options{}
flag.CommandLine = flag.NewFlagSet("prog", flag.ExitOnError)
err = parseArgs(opt)
okf(t, err)
if opt.mountPoint != mp {
t.Errorf("mountpoint arg")
}
// -r dir != -m mp
t.Logf("-r dir != mp")
os.Args = []string{"prog", "-r", dir, "-m", dir, "bash", "echo", "hello"}
opt = &options{}
flag.CommandLine = flag.NewFlagSet("prog", flag.ExitOnError)
err = parseArgs(opt)
if err == nil {
t.Errorf("expected err for run dir == mountpoint")
}
// -r dir no cmd
t.Logf("-r dir no cmd")
os.Args = []string{"prog", "-r", dir}
opt = &options{}
flag.CommandLine = flag.NewFlagSet("prog", flag.ExitOnError)
err = parseArgs(opt)
if err == nil {
t.Errorf("expected err for run with no command")
}
// -r dir cmd non-executable
t.Logf("-r dir cmd non-executable")
os.Args = []string{"prog", "-r", dir, mp}
opt = &options{}
flag.CommandLine = flag.NewFlagSet("prog", flag.ExitOnError)
err = parseArgs(opt)
if err == nil {
t.Errorf("expected err for run with non-executable cmd")
}
}
func TestShowUsage(t *testing.T) {
t.Logf("Ignore the following usage statement: ")
showUsage()
}
func TestUsageError(t *testing.T) {
e := newUsageErr("hello")
assert(t, e.Error() == "hello", "Error()", e.Error())
assert(t, isUsageErr(e), "usage", e)
}