2015-11-12 21:02:44 +01:00
|
|
|
package integration_tests
|
2015-09-06 11:42:01 +02:00
|
|
|
|
2015-11-15 16:06:19 +01:00
|
|
|
// File reading, writing, modification, truncate
|
|
|
|
|
2015-09-06 11:42:01 +02:00
|
|
|
import (
|
2015-09-30 23:42:18 +02:00
|
|
|
"bytes"
|
2015-09-06 12:12:14 +02:00
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
2015-11-14 17:16:17 +01:00
|
|
|
"flag"
|
2015-09-06 11:42:01 +02:00
|
|
|
"fmt"
|
2015-09-06 12:12:14 +02:00
|
|
|
"io/ioutil"
|
2015-09-06 11:42:01 +02:00
|
|
|
"os"
|
2015-11-01 12:11:36 +01:00
|
|
|
"runtime"
|
|
|
|
"sync"
|
2015-09-06 12:12:14 +02:00
|
|
|
"testing"
|
2015-09-06 11:42:01 +02:00
|
|
|
)
|
|
|
|
|
2015-11-03 22:27:11 +01:00
|
|
|
var plaintextNames bool
|
|
|
|
|
2015-10-06 20:24:52 +02:00
|
|
|
// This is the entry point for the tests
|
2015-09-18 22:14:07 +02:00
|
|
|
func TestMain(m *testing.M) {
|
2015-11-12 21:02:44 +01:00
|
|
|
var defaultonly bool
|
|
|
|
flag.BoolVar(&defaultonly, "defaultonly", false, "Only test default configuration (openssl=true, plaintextnames=false)")
|
|
|
|
flag.Parse()
|
|
|
|
|
2015-11-03 22:27:11 +01:00
|
|
|
if testing.Verbose() {
|
2015-11-12 21:02:44 +01:00
|
|
|
fmt.Printf("***** Testing with OpenSSL\n")
|
2015-09-06 11:42:01 +02:00
|
|
|
}
|
2015-12-11 23:27:38 +01:00
|
|
|
resetTmpDir() // <- this also create gocryptfs.diriv
|
2015-11-15 15:05:15 +01:00
|
|
|
mount(defaultCipherDir, defaultPlainDir, "--zerokey")
|
2015-09-06 11:42:01 +02:00
|
|
|
r := m.Run()
|
2015-11-15 15:05:15 +01:00
|
|
|
unmount(defaultPlainDir)
|
2015-09-06 11:42:01 +02:00
|
|
|
|
2015-12-11 23:27:38 +01:00
|
|
|
if r != 0 {
|
|
|
|
os.Exit(r)
|
|
|
|
}
|
|
|
|
|
2015-11-12 21:02:44 +01:00
|
|
|
if defaultonly {
|
|
|
|
os.Exit(r)
|
|
|
|
}
|
|
|
|
|
2015-11-03 22:27:11 +01:00
|
|
|
if testing.Verbose() {
|
2015-11-12 21:02:44 +01:00
|
|
|
fmt.Printf("***** Testing with native Go crypto\n")
|
2015-11-03 22:27:11 +01:00
|
|
|
}
|
|
|
|
resetTmpDir()
|
2015-11-15 15:05:15 +01:00
|
|
|
mount(defaultCipherDir, defaultPlainDir, "--zerokey", "--openssl=false")
|
2015-10-06 20:24:52 +02:00
|
|
|
r = m.Run()
|
2015-11-15 15:05:15 +01:00
|
|
|
unmount(defaultPlainDir)
|
2015-10-06 20:24:52 +02:00
|
|
|
|
2015-12-11 23:27:38 +01:00
|
|
|
if r != 0 {
|
|
|
|
os.Exit(r)
|
|
|
|
}
|
|
|
|
|
2015-11-03 22:27:11 +01:00
|
|
|
if testing.Verbose() {
|
|
|
|
fmt.Printf("***** Testing \"--plaintextnames\"\n")
|
|
|
|
}
|
|
|
|
resetTmpDir()
|
2015-11-15 15:05:15 +01:00
|
|
|
mount(defaultCipherDir, defaultPlainDir, "--zerokey", "--plaintextnames")
|
2015-11-03 22:27:11 +01:00
|
|
|
plaintextNames = true
|
|
|
|
r = m.Run()
|
2015-11-15 15:05:15 +01:00
|
|
|
unmount(defaultPlainDir)
|
2015-11-03 22:27:11 +01:00
|
|
|
|
2015-12-11 23:27:38 +01:00
|
|
|
if r != 0 {
|
|
|
|
os.Exit(r)
|
|
|
|
}
|
|
|
|
|
2015-09-06 11:42:01 +02:00
|
|
|
os.Exit(r)
|
|
|
|
}
|
|
|
|
|
2015-10-31 23:08:40 +01:00
|
|
|
// Write "n" zero bytes to filename "fn", read again, compare hash
|
2015-09-08 22:03:27 +02:00
|
|
|
func testWriteN(t *testing.T, fn string, n int) string {
|
2015-11-15 15:05:15 +01:00
|
|
|
file, err := os.Create(defaultPlainDir + fn)
|
2015-09-06 11:42:01 +02:00
|
|
|
if err != nil {
|
2015-11-27 00:03:10 +01:00
|
|
|
t.Fatal(err)
|
2015-09-06 11:42:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
d := make([]byte, n)
|
|
|
|
written, err := file.Write(d)
|
|
|
|
if err != nil || written != len(d) {
|
2015-11-27 00:03:10 +01:00
|
|
|
t.Errorf("err=\"%s\", written=%d\n", err, written)
|
|
|
|
}
|
|
|
|
err = file.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2015-09-06 11:42:01 +02:00
|
|
|
}
|
|
|
|
|
2015-11-15 15:05:15 +01:00
|
|
|
verifySize(t, defaultPlainDir+fn, n)
|
2015-10-31 23:08:40 +01:00
|
|
|
|
2015-09-30 22:36:53 +02:00
|
|
|
bin := md5.Sum(d)
|
|
|
|
hashWant := hex.EncodeToString(bin[:])
|
2015-09-06 11:42:01 +02:00
|
|
|
|
2015-11-15 15:05:15 +01:00
|
|
|
hashActual := md5fn(defaultPlainDir + fn)
|
2015-09-08 22:03:27 +02:00
|
|
|
|
2015-09-06 11:42:01 +02:00
|
|
|
if hashActual != hashWant {
|
2015-10-31 23:17:19 +01:00
|
|
|
t.Errorf("Wrong content, hashWant=%s hashActual=%s\n", hashWant, hashActual)
|
2015-09-06 11:42:01 +02:00
|
|
|
}
|
2015-09-08 22:03:27 +02:00
|
|
|
|
|
|
|
return hashActual
|
2015-09-06 11:42:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrite10(t *testing.T) {
|
2015-09-08 22:03:27 +02:00
|
|
|
testWriteN(t, "10", 10)
|
2015-09-06 11:42:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrite100(t *testing.T) {
|
2015-09-08 22:03:27 +02:00
|
|
|
testWriteN(t, "100", 100)
|
2015-09-06 11:42:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrite1M(t *testing.T) {
|
2015-09-08 22:03:27 +02:00
|
|
|
testWriteN(t, "1M", 1024*1024)
|
2015-09-06 11:42:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestWrite1Mx100(t *testing.T) {
|
2015-09-08 22:03:27 +02:00
|
|
|
hashWant := testWriteN(t, "1Mx100", 1024*1024)
|
2015-09-06 11:42:01 +02:00
|
|
|
// Read and check 100 times to catch race conditions
|
|
|
|
var i int
|
|
|
|
for i = 0; i < 100; i++ {
|
2015-11-15 15:05:15 +01:00
|
|
|
hashActual := md5fn(defaultPlainDir + "1M")
|
2015-09-08 22:03:27 +02:00
|
|
|
if hashActual != hashWant {
|
2015-09-06 11:42:01 +02:00
|
|
|
fmt.Printf("Read corruption in loop # %d\n", i)
|
|
|
|
t.FailNow()
|
|
|
|
} else {
|
|
|
|
//fmt.Print(".")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-30 22:36:53 +02:00
|
|
|
func TestTruncate(t *testing.T) {
|
2015-11-15 15:05:15 +01:00
|
|
|
fn := defaultPlainDir + "truncate"
|
2015-09-30 22:36:53 +02:00
|
|
|
file, err := os.Create(fn)
|
|
|
|
if err != nil {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
// Grow to two blocks
|
|
|
|
file.Truncate(7000)
|
2015-11-01 00:09:08 +01:00
|
|
|
verifySize(t, fn, 7000)
|
2015-09-30 22:36:53 +02:00
|
|
|
if md5fn(fn) != "95d4ec7038e3e4fdbd5f15c34c3f0b34" {
|
2015-11-01 00:09:08 +01:00
|
|
|
t.Errorf("wrong content")
|
2015-09-30 22:36:53 +02:00
|
|
|
}
|
|
|
|
// Shrink - needs RMW
|
|
|
|
file.Truncate(6999)
|
2015-11-01 00:09:08 +01:00
|
|
|
verifySize(t, fn, 6999)
|
2015-09-30 22:36:53 +02:00
|
|
|
if md5fn(fn) != "35fd15873ec6c35380064a41b9b9683b" {
|
2015-11-01 00:09:08 +01:00
|
|
|
t.Errorf("wrong content")
|
2015-09-30 22:36:53 +02:00
|
|
|
}
|
|
|
|
// Shrink to one partial block
|
|
|
|
file.Truncate(465)
|
2015-11-01 00:09:08 +01:00
|
|
|
verifySize(t, fn, 465)
|
2015-09-30 22:36:53 +02:00
|
|
|
if md5fn(fn) != "a1534d6e98a6b21386456a8f66c55260" {
|
2015-11-01 00:09:08 +01:00
|
|
|
t.Errorf("wrong content")
|
2015-09-30 22:36:53 +02:00
|
|
|
}
|
|
|
|
// Grow to exactly one block
|
|
|
|
file.Truncate(4096)
|
2015-11-01 00:09:08 +01:00
|
|
|
verifySize(t, fn, 4096)
|
2015-09-30 22:36:53 +02:00
|
|
|
if md5fn(fn) != "620f0b67a91f7f74151bc5be745b7110" {
|
2015-11-01 00:09:08 +01:00
|
|
|
t.Errorf("wrong content")
|
2015-09-30 22:36:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-30 23:42:18 +02:00
|
|
|
func TestAppend(t *testing.T) {
|
2015-11-15 15:05:15 +01:00
|
|
|
fn := defaultPlainDir + "append"
|
2015-09-30 23:42:18 +02:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-04 10:38:58 +02:00
|
|
|
// Create a file with holes by writing to offset 0 (block #0) and
|
|
|
|
// offset 4096 (block #1).
|
|
|
|
func TestFileHoles(t *testing.T) {
|
2015-11-15 15:05:15 +01:00
|
|
|
fn := defaultPlainDir + "fileholes"
|
2015-10-04 10:38:58 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-25 22:08:07 +01:00
|
|
|
// sContains - does the slice of strings "haystack" contain "needle"?
|
2015-10-20 20:12:31 +02:00
|
|
|
func sContains(haystack []string, needle string) bool {
|
2015-11-01 12:11:36 +01:00
|
|
|
for _, element := range haystack {
|
|
|
|
if element == needle {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2015-10-20 20:12:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRmwRace(t *testing.T) {
|
|
|
|
|
|
|
|
runtime.GOMAXPROCS(10)
|
|
|
|
|
2015-11-15 15:05:15 +01:00
|
|
|
fn := defaultPlainDir + "rmwrace"
|
2015-10-20 20:12:31 +02:00
|
|
|
f1, err := os.Create(fn)
|
|
|
|
if err != nil {
|
2015-10-31 13:45:14 +01:00
|
|
|
t.Fatalf("file create failed")
|
2015-10-20 20:12:31 +02:00
|
|
|
}
|
|
|
|
f2, err := os.Create(fn)
|
|
|
|
if err != nil {
|
2015-10-31 13:45:14 +01:00
|
|
|
t.Fatalf("file create failed")
|
2015-10-20 20:12:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2015-10-31 13:45:14 +01:00
|
|
|
t.Fatalf("Write failed")
|
2015-10-20 20:12:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
/*
|
2015-11-01 12:11:36 +01:00
|
|
|
if m == "6c1660fdabccd448d1359f27b3db3c99" {
|
|
|
|
fmt.Println(hex.Dump(buf))
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2015-10-20 20:12:31 +02:00
|
|
|
*/
|
|
|
|
}
|
2015-11-03 21:11:07 +01:00
|
|
|
if testing.Verbose() {
|
|
|
|
fmt.Println(goodMd5)
|
|
|
|
}
|
2015-10-20 20:12:31 +02:00
|
|
|
}
|
2015-11-03 22:27:11 +01:00
|
|
|
|
|
|
|
// With "--plaintextnames", the name "/gocryptfs.conf" is reserved.
|
|
|
|
// Otherwise there should be no restrictions.
|
|
|
|
func TestFiltered(t *testing.T) {
|
2015-11-15 15:05:15 +01:00
|
|
|
filteredFile := defaultPlainDir + "gocryptfs.conf"
|
2015-11-03 22:27:11 +01:00
|
|
|
file, err := os.Create(filteredFile)
|
|
|
|
if plaintextNames == true && err == nil {
|
2015-11-14 17:16:17 +01:00
|
|
|
t.Errorf("should have failed but didn't")
|
2015-11-03 22:27:11 +01:00
|
|
|
} else if plaintextNames == false && err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
|
|
|
|
err = os.Remove(filteredFile)
|
|
|
|
if plaintextNames == true && err == nil {
|
2015-11-14 17:16:17 +01:00
|
|
|
t.Errorf("should have failed but didn't")
|
2015-11-03 22:27:11 +01:00
|
|
|
} else if plaintextNames == false && err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilenameEncryption(t *testing.T) {
|
2015-11-15 15:05:15 +01:00
|
|
|
file, err := os.Create(defaultPlainDir + "TestFilenameEncryption.txt")
|
2015-11-03 22:27:11 +01:00
|
|
|
file.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-11-15 15:05:15 +01:00
|
|
|
_, err = os.Stat(defaultCipherDir + "TestFilenameEncryption.txt")
|
2015-11-03 22:27:11 +01:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
2015-11-25 22:08:07 +01:00
|
|
|
|
|
|
|
// Test Mkdir and Rmdir
|
|
|
|
func TestMkdirRmdir(t *testing.T) {
|
2015-12-10 20:02:18 +01:00
|
|
|
testMkdirRmdir(t, defaultPlainDir)
|
2015-11-25 22:08:07 +01:00
|
|
|
}
|
2015-12-06 15:10:23 +01:00
|
|
|
|
|
|
|
// Test Rename
|
|
|
|
func TestRename(t *testing.T) {
|
2015-12-10 20:02:18 +01:00
|
|
|
testRename(t, defaultPlainDir)
|
2015-12-06 15:10:23 +01:00
|
|
|
}
|
2015-12-11 23:27:38 +01:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|