aboutsummaryrefslogtreecommitdiff
path: root/main_test.go
blob: 6b68b4a5c9d042104cafdc1416bc8bd71d56b457 (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
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
package main

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

const tmpDir = "test_tmp_dir/"
const plainDir = tmpDir + "plain/"
const cipherDir = tmpDir + "cipher/"

func TestMain(m *testing.M) {

	fu := exec.Command("fusermount", "-u", plainDir)
	fu.Stdout = os.Stdout
	fu.Stderr = os.Stderr
	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")
	}

	c := exec.Command("./gocryptfs", cipherDir, plainDir)
	c.Stdout = os.Stdout
	c.Stderr = os.Stderr
	go c.Run()

	time.Sleep(3 * time.Second)

	r := m.Run()

	fu.Run()
	os.Exit(r)
}

func testWriteN(t *testing.T, fn string, n int, hashWant string) {
	file, err := os.Create(plainDir + fn)
	if err != nil {
		t.FailNow()
	}

	d := make([]byte, n)
	written, err := file.Write(d)
	if err != nil || written != len(d) {
		fmt.Printf("err=\"%s\", written=%d\n", err, written)
		t.Fail()
	}
	file.Close()

	buf, err := ioutil.ReadFile(plainDir + fn)
	if err != nil {
		t.Fail()
	}

	rawHash := md5.Sum(buf)
	hashActual := hex.EncodeToString(rawHash[:])
	if hashActual != hashWant {
		fmt.Printf("hashWant=%s hashActual=%s\n", hashWant, hashActual)
		t.Fail()
	}
}

func TestWrite10(t *testing.T) {
	testWriteN(t, "10", 10, "a63c90cc3684ad8b0a2176a6a8fe9005")
}

func TestWrite100(t *testing.T) {
	testWriteN(t, "100", 100, "6d0bb00954ceb7fbee436bb55a8397a9")
}

func TestWrite1M(t *testing.T) {
	testWriteN(t, "1M", 1024*1024, "b6d81b360a5672d80c27430f39153e2c")
}

func TestWrite1Mx100(t *testing.T) {
	testWriteN(t, "1Mx100", 1024*1024, "b6d81b360a5672d80c27430f39153e2c")
	// Read and check 100 times to catch race conditions
	var i int
	for i = 0; i < 100; i++ {
		buf, err := ioutil.ReadFile(plainDir + "1M")
		if err != nil {
			t.Fail()
		}
		rawHash := md5.Sum(buf)
		hashActual := hex.EncodeToString(rawHash[:])
		if hashActual != "b6d81b360a5672d80c27430f39153e2c" {
			fmt.Printf("Read corruption in loop # %d\n", i)
			t.FailNow()
		} else {
			//fmt.Print(".")
		}
	}
}

func BenchmarkStreamWrite(t *testing.B) {
	buf := make([]byte, 1024*1024)
	t.SetBytes(int64(len(buf)))

	file, err := os.Create(plainDir + "BenchmarkWrite")
	if err != nil {
		t.FailNow()
	}

	t.ResetTimer()
	var i int
	for i = 0; i < t.N; i++ {
		written, err := file.Write(buf)
		if err != nil {
			fmt.Printf("err=\"%s\", written=%d\n", err.Error(), written)
			t.FailNow()
		}
	}
}

func BenchmarkStreamRead(t *testing.B) {
	buf := make([]byte, 1024*1024)
	t.SetBytes(int64(len(buf)))
	file, err := os.Open(plainDir + "BenchmarkWrite")
	if err != nil {
		t.FailNow()
	}
	t.ResetTimer()
	var i int
	for i = 0; i < t.N; i++ {
		_, err := file.Read(buf)
		if err == io.EOF {
			fmt.Printf("Test file too small\n")
			t.SkipNow()
		} else if err != nil {
			fmt.Println(err)
			t.FailNow()
		}
	}
}