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
|
package integration_tests
// File reading, writing, modification, truncate
import (
"bytes"
"crypto/md5"
"encoding/hex"
"flag"
"fmt"
"io/ioutil"
"os"
"runtime"
"sync"
"syscall"
"testing"
)
var plaintextNames bool
// This is the entry point for the tests
func TestMain(m *testing.M) {
var defaultonly bool
flag.BoolVar(&defaultonly, "defaultonly", false, "Only test default configuration (openssl=true, plaintextnames=false)")
flag.Parse()
if testing.Verbose() {
fmt.Println("***** Testing with OpenSSL")
}
resetTmpDir(false) // <- this also create gocryptfs.diriv
mount(defaultCipherDir, defaultPlainDir, "--zerokey")
r := m.Run()
unmount(defaultPlainDir)
if r != 0 {
os.Exit(r)
}
if defaultonly {
os.Exit(r)
}
if testing.Verbose() {
fmt.Println("***** Testing with native Go crypto")
}
resetTmpDir(false)
mount(defaultCipherDir, defaultPlainDir, "--zerokey", "--openssl=false")
r = m.Run()
unmount(defaultPlainDir)
if r != 0 {
os.Exit(r)
}
if testing.Verbose() {
fmt.Println("***** Testing \"--plaintextnames\"")
}
resetTmpDir(true) // do not create gocryptfs.diriv
mount(defaultCipherDir, defaultPlainDir, "--zerokey", "--plaintextnames")
plaintextNames = true
r = m.Run()
unmount(defaultPlainDir)
if r != 0 {
os.Exit(r)
}
os.Exit(r)
}
// Write "n" zero bytes to filename "fn", read again, compare hash
func testWriteN(t *testing.T, fn string, n int) string {
file, err := os.Create(defaultPlainDir + fn)
if err != nil {
t.Fatal(err)
}
d := make([]byte, n)
written, err := file.Write(d)
if err != nil || written != len(d) {
t.Errorf("err=\"%s\", written=%d", err, written)
}
err = file.Close()
if err != nil {
t.Error(err)
}
verifySize(t, defaultPlainDir+fn, n)
bin := md5.Sum(d)
hashWant := hex.EncodeToString(bin[:])
hashActual := md5fn(defaultPlainDir + fn)
if hashActual != hashWant {
t.Errorf("Wrong content, hashWant=%s hashActual=%s", hashWant, hashActual)
}
return hashActual
}
func TestWrite10(t *testing.T) {
testWriteN(t, "10", 10)
}
func TestWrite100(t *testing.T) {
testWriteN(t, "100", 100)
}
func TestWrite1M(t *testing.T) {
testWriteN(t, "1M", 1024*1024)
}
func TestWrite1Mx100(t *testing.T) {
hashWant := testWriteN(t, "1Mx100", 1024*1024)
// Read and check 100 times to catch race conditions
var i int
for i = 0; i < 100; i++ {
hashActual := md5fn(defaultPlainDir + "1M")
if hashActual != hashWant {
fmt.Printf("Read corruption in loop # %d\n", i)
t.FailNow()
} else {
//fmt.Print(".")
}
}
}
func TestTruncate(t *testing.T) {
fn := defaultPlainDir + "truncate"
file, err := os.Create(fn)
if err != nil {
t.FailNow()
}
// Grow to two blocks
file.Truncate(7000)
verifySize(t, fn, 7000)
if md5fn(fn) != "95d4ec7038e3e4fdbd5f15c34c3f0b34" {
t.Errorf("wrong content")
}
// Shrink - needs RMW
file.Truncate(6999)
verifySize(t, fn, 6999)
if md5fn(fn) != "35fd15873ec6c35380064a41b9b9683b" {
t.Errorf("wrong content")
}
// Shrink to one partial block
file.Truncate(465)
verifySize(t, fn, 465)
if md5fn(fn) != "a1534d6e98a6b21386456a8f66c55260" {
t.Errorf("wrong content")
}
// Grow to exactly one block
file.Truncate(4096)
verifySize(t, fn, 4096)
if md5fn(fn) != "620f0b67a91f7f74151bc5be745b7110" {
t.Errorf("wrong content")
}
}
func TestAppend(t *testing.T) {
fn := defaultPlainDir + "append"
file, err := os.Create(fn)
if err != nil {
t.FailNow()
}
data := []byte("testdata123456789") // length 17
var buf bytes.Buffer
var hashWant string
for i := 0; i <= 500; i++ {
file.Write(data)
buf.Write(data)
bin := md5.Sum(buf.Bytes())
hashWant = hex.EncodeToString(bin[:])
hashActual := md5fn(fn)
if hashWant != hashActual {
t.FailNow()
}
}
// Overwrite with the same data
// Hash must stay the same
file.Seek(0, 0)
for i := 0; i <= 500; i++ {
file.Write(data)
hashActual := md5fn(fn)
if hashWant != hashActual {
t.FailNow()
}
}
}
// Create a file with holes by writing to offset 0 (block #0) and
// offset 4096 (block #1).
func TestFileHoles(t *testing.T) {
fn := defaultPlainDir + "fileholes"
file, err := os.Create(fn)
if err != nil {
t.Errorf("file create failed")
}
foo := []byte("foo")
file.Write(foo)
file.WriteAt(foo, 4096)
_, err = ioutil.ReadFile(fn)
if err != nil {
t.Error(err)
}
}
// sContains - does the slice of strings "haystack" contain "needle"?
func sContains(haystack []string, needle string) bool {
for _, element := range haystack {
if element == needle {
return true
}
}
return false
}
func TestRmwRace(t *testing.T) {
runtime.GOMAXPROCS(10)
fn := defaultPlainDir + "rmwrace"
f1, err := os.Create(fn)
if err != nil {
t.Fatalf("file create failed")
}
f2, err := os.Create(fn)
if err != nil {
t.Fatalf("file create failed")
}
oldBlock := bytes.Repeat([]byte("o"), 4096)
newBlock := bytes.Repeat([]byte("n"), 4096)
shortBlock := bytes.Repeat([]byte("s"), 16)
mergedBlock := make([]byte, 4096)
copy(mergedBlock, newBlock)
copy(mergedBlock[4080:], shortBlock)
goodMd5 := make(map[string]int)
for i := 0; i < 1000; i++ {
// Reset to [ooooooooo]
_, err = f1.WriteAt(oldBlock, 0)
if err != nil {
t.Fatalf("Write failed")
}
var wg sync.WaitGroup
wg.Add(2)
// Write to the end of the file, [....ssss]
go func() {
f1.WriteAt(shortBlock, 4080)
wg.Done()
}()
// Overwrite to [nnnnnnn]
go func() {
f2.WriteAt(newBlock, 0)
wg.Done()
}()
wg.Wait()
// The file should be either:
// [nnnnnnnnnn] (md5: 6c1660fdabccd448d1359f27b3db3c99) or
// [nnnnnnssss] (md5: da885006a6a284530a427c73ce1e5c32)
// but it must not be
// [oooooossss]
buf, _ := ioutil.ReadFile(fn)
m := md5hex(buf)
goodMd5[m] = goodMd5[m] + 1
/*
if m == "6c1660fdabccd448d1359f27b3db3c99" {
fmt.Println(hex.Dump(buf))
t.FailNow()
}
*/
}
if testing.Verbose() {
fmt.Println(goodMd5)
}
}
// With "--plaintextnames", the name "/gocryptfs.conf" is reserved.
// Otherwise there should be no restrictions.
func TestFiltered(t *testing.T) {
filteredFile := defaultPlainDir + "gocryptfs.conf"
file, err := os.Create(filteredFile)
if plaintextNames == true && err == nil {
t.Errorf("should have failed but didn't")
} else if plaintextNames == false && err != nil {
t.Error(err)
}
file.Close()
err = os.Remove(filteredFile)
if plaintextNames == true && err == nil {
t.Errorf("should have failed but didn't")
} else if plaintextNames == false && err != nil {
t.Error(err)
}
}
func TestFilenameEncryption(t *testing.T) {
file, err := os.Create(defaultPlainDir + "TestFilenameEncryption.txt")
file.Close()
if err != nil {
t.Fatal(err)
}
_, err = os.Stat(defaultCipherDir + "TestFilenameEncryption.txt")
if plaintextNames == true && err != nil {
t.Errorf("plaintextnames not working: %v", err)
} else if plaintextNames == false && err == nil {
t.Errorf("file name encryption not working")
}
}
// Test Mkdir and Rmdir
func TestMkdirRmdir(t *testing.T) {
testMkdirRmdir(t, defaultPlainDir)
}
// Test Rename
func TestRename(t *testing.T) {
testRename(t, defaultPlainDir)
}
// Overwrite an empty directory with another directory
func TestDirOverwrite(t *testing.T) {
dir1 := defaultPlainDir + "DirOverwrite1"
dir2 := defaultPlainDir + "DirOverwrite2"
err := os.Mkdir(dir1, 0777)
if err != nil {
t.Fatal(err)
}
err = os.Mkdir(dir2, 0777)
if err != nil {
t.Fatal(err)
}
err = os.Rename(dir1, dir2)
if err != nil {
t.Fatal(err)
}
}
func TestLongNames(t *testing.T) {
// Create
wd := defaultPlainDir
n255x := string(bytes.Repeat([]byte("x"), 255))
f, err := os.Create(wd + n255x)
if err != nil {
t.Fatalf("Could not create n255x")
}
f.Close()
if !verifyExistence(wd + n255x) {
t.Errorf("n255x is not in directory listing")
}
// Rename long to long
n255y := string(bytes.Repeat([]byte("y"), 255))
err = os.Rename(wd+n255x, wd+n255y)
if err != nil {
t.Fatalf("Could not rename n255x to n255y")
}
if !verifyExistence(wd + n255y) {
t.Errorf("n255y is not in directory listing")
}
// Rename long to short
err = os.Rename(wd+n255y, wd+"short")
if err != nil {
t.Fatalf("Could not rename n255y to short")
}
if !verifyExistence(wd + "short") {
t.Errorf("short is not in directory listing")
}
// Rename short to long
err = os.Rename(wd+"short", wd+n255x)
if err != nil {
t.Fatalf("Could not rename short to n255x")
}
if !verifyExistence(wd + n255x) {
t.Errorf("255x is not in directory listing II")
}
// Unlink
err = syscall.Unlink(wd + n255x)
if err != nil {
t.Fatalf("Could not unlink n255x")
}
if verifyExistence(wd + n255x) {
t.Errorf("n255x still there after unlink")
}
}
|