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
|
// Tests and benchmarks performed with default settings only.
package defaults
import (
"bytes"
"os"
"os/exec"
"syscall"
"testing"
"github.com/rfjakob/gocryptfs/internal/ctlsock"
"github.com/rfjakob/gocryptfs/tests/test_helpers"
)
func TestMain(m *testing.M) {
test_helpers.ResetTmpDir(true)
test_helpers.MountOrExit(test_helpers.DefaultCipherDir, test_helpers.DefaultPlainDir, "-zerokey")
r := m.Run()
test_helpers.UnmountPanic(test_helpers.DefaultPlainDir)
os.Exit(r)
}
// Test that we get the right timestamp when extracting a tarball.
func Test1980Tar(t *testing.T) {
c := exec.Command("tar", "xzf", "1980.tar.gz", "-C", test_helpers.DefaultPlainDir)
c.Stderr = os.Stderr
c.Stdout = os.Stdout
err := c.Run()
if err != nil {
t.Fatal(err)
}
fi, err := os.Stat(test_helpers.DefaultPlainDir + "/1980.txt")
if err != nil {
t.Fatal(err)
}
m := fi.ModTime().Unix()
if m != 315619323 {
t.Errorf("Wrong mtime: %d", m)
}
}
func TestCtlSock(t *testing.T) {
cDir := test_helpers.InitFS(t)
pDir := cDir + ".mnt"
sock := cDir + ".sock"
test_helpers.MountOrFatal(t, cDir, pDir, "-ctlsock="+sock, "-extpass", "echo test")
defer test_helpers.UnmountPanic(pDir)
req := ctlsock.RequestStruct{
EncryptPath: "foobar",
}
response := test_helpers.QueryCtlSock(t, sock, req)
if response.Result == "" || response.ErrNo != 0 {
t.Errorf("got an error reply: %+v", response)
}
req.EncryptPath = "not-existing-dir/xyz"
response = test_helpers.QueryCtlSock(t, sock, req)
if response.ErrNo != int32(syscall.ENOENT) || response.Result != "" {
t.Errorf("incorrect error handling: %+v", response)
}
// Strange paths should not cause a crash
crashers := []string{"/foo", "foo/", "/foo/", ".", "/////", "/../../."}
for _, c := range crashers {
req.EncryptPath = c
// QueryCtlSock calls t.Fatal if it gets EOF when gocryptfs panics
response = test_helpers.QueryCtlSock(t, sock, req)
if response.WarnText == "" {
t.Errorf("We should get a warning about non-canonical paths here")
}
}
}
// In gocryptfs before v1.2, the file header was only read once for each
// open. But truncating a file to zero will generate a new random file ID.
// The sequence below caused an I/O error to be returned.
func TestOpenTruncateRead(t *testing.T) {
fn := test_helpers.DefaultPlainDir + "/TestTruncateWrite"
// First FD is used for write and trucate.
writeFd, err := os.Create(fn)
if err != nil {
t.Fatal(err)
}
abc := []byte("abc")
_, err = writeFd.WriteAt(abc, 0)
if err != nil {
t.Fatal(err)
}
// Second FD is just for reading.
readFd, err := os.Open(fn)
if err != nil {
t.Fatal(err)
}
content := make([]byte, 3)
_, err = readFd.ReadAt(content, 0)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(content, abc) {
t.Fatalf("wrong content: %s", string(content))
}
// Truncate to zero to generate a new file ID and write new content.
err = writeFd.Truncate(0)
if err != nil {
t.Fatal(err)
}
xyz := []byte("xyz")
_, err = writeFd.WriteAt(xyz, 0)
if err != nil {
t.Fatal(err)
}
// Try to read from the other FD.
_, err = readFd.ReadAt(content, 0)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(content, xyz) {
t.Fatalf("wrong content: %s", string(content))
}
}
// TestWORead tries to read from a write-only file.
func TestWORead(t *testing.T) {
fn := test_helpers.DefaultPlainDir + "/TestWORead"
fd, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY, 0600)
if err != nil {
t.Fatal(err)
}
defer fd.Close()
buf := make([]byte, 10)
_, err = fd.Read(buf)
if err == nil {
t.Error("Reading from write-only file should fail, but did not")
}
}
|