summaryrefslogtreecommitdiff
path: root/integration_tests/helpers.go
blob: 9bfd9fcc34bc3729dfbf28d23109c5d565278166 (plain)
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
package integration_tests

import (
	"os"
	"os/exec"
	"fmt"
	"io/ioutil"
	"crypto/md5"
	"encoding/hex"
	"testing"
)

const tmpDir = "/tmp/gocryptfs_main_test/"
// Mountpoint
const plainDir = tmpDir + "plain/"
const cipherDir = tmpDir + "cipher/"

func resetTmpDir() {
	fu := exec.Command("fusermount", "-z", "-u", plainDir)
	fu.Run()

	os.RemoveAll(tmpDir)

	err := os.MkdirAll(plainDir, 0777)
	if err != nil {
		panic("Could not create plainDir")
	}

	err = os.MkdirAll(cipherDir, 0777)
	if err != nil {
		panic("Could not create cipherDir")
	}
}

func mount(extraArgs ...string) {
	var args []string
	args = append(args, extraArgs...)
	//args = append(args, "--fusedebug")
	args = append(args, cipherDir)
	args = append(args, plainDir)
	c := exec.Command("../gocryptfs", args...)
	// Warning messages clutter the test output. Uncomment if you want to debug
	// failures.
	//c.Stdout = os.Stdout
	//c.Stderr = os.Stderr
	err := c.Run()
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
}

func unmount() error {
	fu := exec.Command("fusermount", "-z", "-u", plainDir)
	fu.Stdout = os.Stdout
	fu.Stderr = os.Stderr
	return fu.Run()
}

// Return md5 string for file "filename"
func md5fn(filename string) string {
	buf, err := ioutil.ReadFile(filename)
	if err != nil {
		fmt.Printf("ReadFile: %v\n", err)
		return ""
	}
	return md5hex(buf)
}

// Return md5 string for "buf"
func md5hex(buf []byte) string {
	rawHash := md5.Sum(buf)
	hash := hex.EncodeToString(rawHash[:])
	return hash
}

// Verify that the file size equals "want". This checks:
// 1) Size reported by Stat()
// 2) Number of bytes returned when reading the whole file
func verifySize(t *testing.T, path string, want int) {
	buf, err := ioutil.ReadFile(path)
	if err != nil {
		t.Errorf("ReadFile failed: %v", err)
	} else if len(buf) != want {
		t.Errorf("wrong read size: got=%d want=%d", len(buf), want)
	}

	fi, err := os.Stat(path)
	if err != nil {
		t.Errorf("Stat failed: %v", err)
	} else if fi.Size() != int64(want) {
		t.Errorf("wrong stat file size, got=%d want=%d", fi.Size(), want)
	}
}