2016-06-30 00:57:14 +02:00
|
|
|
package matrix
|
2015-09-06 11:42:01 +02:00
|
|
|
|
2015-11-15 16:06:19 +01:00
|
|
|
// File reading, writing, modification, truncate
|
2016-06-30 00:57:14 +02:00
|
|
|
//
|
|
|
|
// Runs everything four times, for all combinations of
|
|
|
|
// "-plaintextnames" and "-openssl".
|
|
|
|
//
|
|
|
|
// Test Matrix:
|
|
|
|
// openssl=true openssl=false
|
|
|
|
// plaintextnames=false X X
|
|
|
|
// plaintextnames=true X X
|
2015-11-15 16:06:19 +01:00
|
|
|
|
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"
|
2016-02-07 13:28:55 +01:00
|
|
|
"syscall"
|
2015-09-06 12:12:14 +02:00
|
|
|
"testing"
|
2016-06-06 23:57:42 +02:00
|
|
|
|
|
|
|
"github.com/rfjakob/gocryptfs/tests/test_helpers"
|
2015-09-06 11:42:01 +02:00
|
|
|
)
|
|
|
|
|
2016-06-30 00:57:14 +02:00
|
|
|
// Several tests need to be aware if plaintextnames is active or not, so make this
|
|
|
|
// a global variable
|
|
|
|
var plaintextnames bool
|
2015-11-03 22:27:11 +01:00
|
|
|
|
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) {
|
2016-06-30 00:57:14 +02:00
|
|
|
// Make "testing.Verbose()" return the correct value
|
2015-11-12 21:02:44 +01:00
|
|
|
flag.Parse()
|
2016-06-30 00:57:14 +02:00
|
|
|
for _, openssl := range []bool{true, false} {
|
|
|
|
for _, plaintextnames = range []bool{true, false} {
|
|
|
|
if testing.Verbose() {
|
|
|
|
fmt.Printf("Testing openssl=%v plaintextnames=%v\n", openssl, plaintextnames)
|
2015-11-12 21:02:44 +01:00
|
|
|
|
2016-06-30 00:57:14 +02:00
|
|
|
}
|
|
|
|
test_helpers.ResetTmpDir(plaintextnames)
|
|
|
|
opts := []string{"--zerokey"}
|
|
|
|
opts = append(opts, fmt.Sprintf("-openssl=%v", openssl))
|
|
|
|
opts = append(opts, fmt.Sprintf("-plaintextnames=%v", plaintextnames))
|
|
|
|
test_helpers.MountOrExit(test_helpers.DefaultCipherDir, test_helpers.DefaultPlainDir, opts...)
|
|
|
|
r := m.Run()
|
|
|
|
test_helpers.Unmount(test_helpers.DefaultPlainDir)
|
|
|
|
if r != 0 {
|
|
|
|
os.Exit(r)
|
|
|
|
}
|
|
|
|
}
|
2015-12-11 23:27:38 +01:00
|
|
|
}
|
2016-06-30 00:57:14 +02:00
|
|
|
os.Exit(0)
|
2015-09-06 11:42:01 +02:00
|
|
|
}
|
|
|
|
|
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 {
|
2016-06-30 00:57:14 +02:00
|
|
|
file, err := os.Create(test_helpers.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) {
|
2016-01-20 20:55:56 +01:00
|
|
|
t.Errorf("err=\"%s\", written=%d", err, written)
|
2015-11-27 00:03:10 +01:00
|
|
|
}
|
|
|
|
err = file.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2015-09-06 11:42:01 +02:00
|
|
|
}
|
|
|
|
|
2016-06-30 00:57:14 +02:00
|
|
|
test_helpers.VerifySize(t, test_helpers.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
|
|
|
|
2016-06-30 00:57:14 +02:00
|
|
|
hashActual := test_helpers.Md5fn(test_helpers.DefaultPlainDir + "/" + fn)
|
2015-09-08 22:03:27 +02:00
|
|
|
|
2015-09-06 11:42:01 +02:00
|
|
|
if hashActual != hashWant {
|
2016-01-20 20:55:56 +01:00
|
|
|
t.Errorf("Wrong content, hashWant=%s hashActual=%s", 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
|
|
|
}
|
|
|
|
|
2016-06-30 00:57:14 +02:00
|
|
|
func TestWrite100x100(t *testing.T) {
|
|
|
|
hashWant := testWriteN(t, "100x100", 100)
|
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++ {
|
2016-06-30 00:57:14 +02:00
|
|
|
hashActual := test_helpers.Md5fn(test_helpers.DefaultPlainDir + "/100")
|
2015-09-08 22:03:27 +02:00
|
|
|
if hashActual != hashWant {
|
2016-06-30 00:57:14 +02:00
|
|
|
fmt.Printf("Read corruption in loop #%d\n", i)
|
2015-09-06 11:42:01 +02:00
|
|
|
t.FailNow()
|
|
|
|
} else {
|
|
|
|
//fmt.Print(".")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 23:29:31 +02:00
|
|
|
// Hint for calculating reference md5sums:
|
|
|
|
// dd if=/dev/zero count=1 bs=XYZ | md5sum
|
2015-09-30 22:36:53 +02:00
|
|
|
func TestTruncate(t *testing.T) {
|
2016-06-30 00:57:14 +02:00
|
|
|
fn := test_helpers.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)
|
2016-06-06 23:57:42 +02:00
|
|
|
test_helpers.VerifySize(t, fn, 7000)
|
2016-07-02 19:43:57 +02:00
|
|
|
if md5 := test_helpers.Md5fn(fn); md5 != "95d4ec7038e3e4fdbd5f15c34c3f0b34" {
|
|
|
|
t.Errorf("Wrong md5 %s", md5)
|
2015-09-30 22:36:53 +02:00
|
|
|
}
|
|
|
|
// Shrink - needs RMW
|
|
|
|
file.Truncate(6999)
|
2016-06-06 23:57:42 +02:00
|
|
|
test_helpers.VerifySize(t, fn, 6999)
|
2016-07-02 19:43:57 +02:00
|
|
|
if md5 := test_helpers.Md5fn(fn); md5 != "35fd15873ec6c35380064a41b9b9683b" {
|
|
|
|
t.Errorf("Wrong md5 %s", md5)
|
2015-09-30 22:36:53 +02:00
|
|
|
}
|
|
|
|
// Shrink to one partial block
|
|
|
|
file.Truncate(465)
|
2016-06-06 23:57:42 +02:00
|
|
|
test_helpers.VerifySize(t, fn, 465)
|
2016-07-02 19:43:57 +02:00
|
|
|
if md5 := test_helpers.Md5fn(fn); md5 != "a1534d6e98a6b21386456a8f66c55260" {
|
|
|
|
t.Errorf("Wrong md5 %s", md5)
|
2015-09-30 22:36:53 +02:00
|
|
|
}
|
|
|
|
// Grow to exactly one block
|
|
|
|
file.Truncate(4096)
|
2016-06-06 23:57:42 +02:00
|
|
|
test_helpers.VerifySize(t, fn, 4096)
|
2016-07-02 19:43:57 +02:00
|
|
|
if md5 := test_helpers.Md5fn(fn); md5 != "620f0b67a91f7f74151bc5be745b7110" {
|
|
|
|
t.Errorf("Wrong md5 %s", md5)
|
2015-09-30 22:36:53 +02:00
|
|
|
}
|
2016-07-01 23:29:31 +02:00
|
|
|
// Truncate to zero
|
|
|
|
file.Truncate(0)
|
|
|
|
test_helpers.VerifySize(t, fn, 0)
|
|
|
|
// Grow to 10MB (creates file holes)
|
|
|
|
var sz int
|
|
|
|
sz = 10 * 1024 * 1024
|
|
|
|
file.Truncate(int64(sz))
|
|
|
|
test_helpers.VerifySize(t, fn, sz)
|
2016-07-02 19:43:57 +02:00
|
|
|
if md5 := test_helpers.Md5fn(fn); md5 != "f1c9645dbc14efddc7d8a322685f26eb" {
|
|
|
|
t.Errorf("Wrong md5 %s", md5)
|
2016-07-01 23:29:31 +02:00
|
|
|
}
|
|
|
|
// Grow to 10MB + 100B (partial block on the end)
|
|
|
|
sz = 10*1024*1024 + 100
|
|
|
|
file.Truncate(int64(sz))
|
|
|
|
test_helpers.VerifySize(t, fn, sz)
|
2016-07-02 19:43:57 +02:00
|
|
|
if md5 := test_helpers.Md5fn(fn); md5 != "c23ea79b857b91a7ff07c6ecf185f1ca" {
|
|
|
|
t.Errorf("Wrong md5 %s", md5)
|
2016-07-01 23:29:31 +02:00
|
|
|
}
|
|
|
|
// Grow to 20MB (creates file holes, partial block on the front)
|
|
|
|
sz = 20 * 1024 * 1024
|
|
|
|
file.Truncate(int64(sz))
|
|
|
|
test_helpers.VerifySize(t, fn, sz)
|
2016-07-02 19:43:57 +02:00
|
|
|
if md5 := test_helpers.Md5fn(fn); md5 != "8f4e33f3dc3e414ff94e5fb6905cba8c" {
|
|
|
|
t.Errorf("Wrong md5 %s", md5)
|
2016-07-01 23:29:31 +02:00
|
|
|
}
|
2015-09-30 22:36:53 +02:00
|
|
|
}
|
|
|
|
|
2016-07-02 19:43:57 +02:00
|
|
|
const FALLOC_DEFAULT = 0x00
|
|
|
|
const FALLOC_FL_KEEP_SIZE = 0x01
|
|
|
|
|
|
|
|
func TestFallocate(t *testing.T) {
|
|
|
|
fn := test_helpers.DefaultPlainDir + "/fallocate"
|
|
|
|
file, err := os.Create(fn)
|
|
|
|
if err != nil {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
var nBlocks int64
|
|
|
|
fd := int(file.Fd())
|
|
|
|
_, nBlocks = test_helpers.Du(t, fd)
|
|
|
|
if nBlocks != 0 {
|
|
|
|
t.Fatalf("Empty file has %d blocks", nBlocks)
|
|
|
|
}
|
|
|
|
// Allocate 30 bytes, keep size
|
|
|
|
// gocryptfs || (0 blocks)
|
|
|
|
// ext4 | d | (1 block)
|
|
|
|
err = syscall.Fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, 30)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
_, nBlocks = test_helpers.Du(t, fd)
|
|
|
|
if want := 1; nBlocks/8 != int64(want) {
|
|
|
|
t.Errorf("Expected %d 4k block(s), got %d", want, nBlocks/8)
|
|
|
|
}
|
|
|
|
test_helpers.VerifySize(t, fn, 0)
|
|
|
|
// Three ciphertext blocks. The middle one should be a file hole.
|
|
|
|
// gocryptfs | h | h | d| (1 block)
|
|
|
|
// ext4 | d | h | d | (2 blocks)
|
|
|
|
// (Note that gocryptfs blocks are slightly bigger than the ext4 blocks,
|
|
|
|
// but the last one is partial)
|
|
|
|
err = file.Truncate(9000)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, nBlocks = test_helpers.Du(t, fd)
|
|
|
|
if want := 2; nBlocks/8 != int64(want) {
|
|
|
|
t.Errorf("Expected %d 4k block(s), got %d", want, nBlocks/8)
|
|
|
|
}
|
|
|
|
if md5 := test_helpers.Md5fn(fn); md5 != "5420afa22f6423a9f59e669540656bb4" {
|
|
|
|
t.Errorf("Wrong md5 %s", md5)
|
|
|
|
}
|
|
|
|
// Allocate the whole file space
|
|
|
|
// gocryptfs | h | h | d| (1 block)
|
|
|
|
// ext4 | d | d | d | (3 blocks
|
|
|
|
err = syscall.Fallocate(fd, FALLOC_DEFAULT, 0, 9000)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, nBlocks = test_helpers.Du(t, fd)
|
|
|
|
if want := 3; nBlocks/8 != int64(want) {
|
|
|
|
t.Errorf("Expected %d 4k block(s), got %d", want, nBlocks/8)
|
|
|
|
}
|
|
|
|
// Neither apparent size nor content should have changed
|
|
|
|
test_helpers.VerifySize(t, fn, 9000)
|
|
|
|
if md5 := test_helpers.Md5fn(fn); md5 != "5420afa22f6423a9f59e669540656bb4" {
|
|
|
|
t.Errorf("Wrong md5 %s", md5)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Partial block on the end. The first ext4 block is dirtied by the header.
|
|
|
|
// gocryptfs | h | h | d| (1 block)
|
|
|
|
// ext4 | d | h | d | (2 blocks)
|
|
|
|
file.Truncate(0)
|
|
|
|
file.Truncate(9000)
|
|
|
|
_, nBlocks = test_helpers.Du(t, fd)
|
|
|
|
if want := 2; nBlocks/8 != int64(want) {
|
|
|
|
t.Errorf("Expected %d 4k block(s), got %d", want, nBlocks/8)
|
|
|
|
}
|
|
|
|
// Allocate 10 bytes in the second block
|
|
|
|
// gocryptfs | h | h | d| (1 block)
|
|
|
|
// ext4 | d | d | d | (2 blocks)
|
|
|
|
syscall.Fallocate(fd, FALLOC_DEFAULT, 5000, 10)
|
|
|
|
_, nBlocks = test_helpers.Du(t, fd)
|
|
|
|
if want := 3; nBlocks/8 != int64(want) {
|
|
|
|
t.Errorf("Expected %d 4k block(s), got %d", want, nBlocks/8)
|
|
|
|
}
|
|
|
|
// Neither apparent size nor content should have changed
|
|
|
|
test_helpers.VerifySize(t, fn, 9000)
|
|
|
|
if md5 := test_helpers.Md5fn(fn); md5 != "5420afa22f6423a9f59e669540656bb4" {
|
|
|
|
t.Errorf("Wrong md5 %s", md5)
|
|
|
|
}
|
|
|
|
// Grow the file to 4 blocks
|
|
|
|
// gocryptfs | h | h | d |d| (2 blocks)
|
|
|
|
// ext4 | d | d | d | d | (3 blocks)
|
|
|
|
syscall.Fallocate(fd, FALLOC_DEFAULT, 15000, 10)
|
|
|
|
_, nBlocks = test_helpers.Du(t, fd)
|
|
|
|
if want := 4; nBlocks/8 != int64(want) {
|
|
|
|
t.Errorf("Expected %d 4k block(s), got %d", want, nBlocks/8)
|
|
|
|
}
|
|
|
|
test_helpers.VerifySize(t, fn, 15010)
|
|
|
|
if md5 := test_helpers.Md5fn(fn); md5 != "c4c44c7a41ab7798a79d093eb44f99fc" {
|
|
|
|
t.Errorf("Wrong md5 %s", md5)
|
|
|
|
}
|
|
|
|
// Shrinking a file using fallocate should have no effect
|
|
|
|
for _, off := range []int64{0, 10, 2000, 5000} {
|
|
|
|
for _, sz := range []int64{0, 1, 42, 6000} {
|
|
|
|
syscall.Fallocate(fd, FALLOC_DEFAULT, off, sz)
|
|
|
|
test_helpers.VerifySize(t, fn, 15010)
|
|
|
|
if md5 := test_helpers.Md5fn(fn); md5 != "c4c44c7a41ab7798a79d093eb44f99fc" {
|
|
|
|
t.Errorf("Wrong md5 %s", md5)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Cleanup
|
|
|
|
syscall.Unlink(fn)
|
|
|
|
}
|
|
|
|
|
2015-09-30 23:42:18 +02:00
|
|
|
func TestAppend(t *testing.T) {
|
2016-06-30 00:57:14 +02:00
|
|
|
fn := test_helpers.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[:])
|
2016-06-06 23:57:42 +02:00
|
|
|
hashActual := test_helpers.Md5fn(fn)
|
2015-09-30 23:42:18 +02:00
|
|
|
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)
|
2016-06-06 23:57:42 +02:00
|
|
|
hashActual := test_helpers.Md5fn(fn)
|
2015-09-30 23:42:18 +02:00
|
|
|
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) {
|
2016-06-30 00:57:14 +02:00
|
|
|
fn := test_helpers.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)
|
|
|
|
|
2016-06-30 00:57:14 +02:00
|
|
|
fn := test_helpers.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)
|
2016-06-06 23:57:42 +02:00
|
|
|
m := test_helpers.Md5hex(buf)
|
2015-10-20 20:12:31 +02:00
|
|
|
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 22:27:11 +01:00
|
|
|
|
|
|
|
// With "--plaintextnames", the name "/gocryptfs.conf" is reserved.
|
|
|
|
// Otherwise there should be no restrictions.
|
|
|
|
func TestFiltered(t *testing.T) {
|
2016-06-30 00:57:14 +02:00
|
|
|
filteredFile := test_helpers.DefaultPlainDir + "/gocryptfs.conf"
|
2015-11-03 22:27:11 +01:00
|
|
|
file, err := os.Create(filteredFile)
|
2016-06-30 00:57:14 +02:00
|
|
|
if plaintextnames == true && err == nil {
|
2015-11-14 17:16:17 +01:00
|
|
|
t.Errorf("should have failed but didn't")
|
2016-06-30 00:57:14 +02:00
|
|
|
} else if plaintextnames == false && err != nil {
|
2015-11-03 22:27:11 +01:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
|
|
|
|
err = os.Remove(filteredFile)
|
2016-06-30 00:57:14 +02:00
|
|
|
if plaintextnames == true && err == nil {
|
2015-11-14 17:16:17 +01:00
|
|
|
t.Errorf("should have failed but didn't")
|
2016-06-30 00:57:14 +02:00
|
|
|
} else if plaintextnames == false && err != nil {
|
2015-11-03 22:27:11 +01:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFilenameEncryption(t *testing.T) {
|
2016-06-30 00:57:14 +02:00
|
|
|
file, err := os.Create(test_helpers.DefaultPlainDir + "/TestFilenameEncryption.txt")
|
2015-11-03 22:27:11 +01:00
|
|
|
file.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-06-30 00:57:14 +02:00
|
|
|
_, err = os.Stat(test_helpers.DefaultCipherDir + "/TestFilenameEncryption.txt")
|
|
|
|
if plaintextnames == true && err != nil {
|
2015-11-03 22:27:11 +01:00
|
|
|
t.Errorf("plaintextnames not working: %v", err)
|
2016-06-30 00:57:14 +02:00
|
|
|
} else if plaintextnames == false && err == nil {
|
2015-11-03 22:27:11 +01:00
|
|
|
t.Errorf("file name encryption not working")
|
|
|
|
}
|
|
|
|
}
|
2015-11-25 22:08:07 +01:00
|
|
|
|
|
|
|
// Test Mkdir and Rmdir
|
2016-06-06 23:57:42 +02:00
|
|
|
func testMkdirRmdir(t *testing.T) {
|
|
|
|
test_helpers.TestMkdirRmdir(t, test_helpers.DefaultPlainDir)
|
2015-11-25 22:08:07 +01:00
|
|
|
}
|
2015-12-06 15:10:23 +01:00
|
|
|
|
|
|
|
// Test Rename
|
2016-06-06 23:57:42 +02:00
|
|
|
func testRename(t *testing.T) {
|
|
|
|
test_helpers.TestRename(t, test_helpers.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) {
|
2016-06-30 00:57:14 +02:00
|
|
|
dir1 := test_helpers.DefaultPlainDir + "/DirOverwrite1"
|
|
|
|
dir2 := test_helpers.DefaultPlainDir + "/DirOverwrite2"
|
2015-12-11 23:27:38 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2016-02-07 10:55:13 +01:00
|
|
|
|
|
|
|
func TestLongNames(t *testing.T) {
|
2016-06-06 23:57:42 +02:00
|
|
|
fi, err := ioutil.ReadDir(test_helpers.DefaultCipherDir)
|
2016-02-07 14:02:09 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cnt1 := len(fi)
|
2016-06-30 00:57:14 +02:00
|
|
|
wd := test_helpers.DefaultPlainDir + "/"
|
2016-02-07 14:02:09 +01:00
|
|
|
// Create file with long name
|
2016-02-07 10:55:13 +01:00
|
|
|
n255x := string(bytes.Repeat([]byte("x"), 255))
|
2016-02-07 13:28:55 +01:00
|
|
|
f, err := os.Create(wd + n255x)
|
2016-02-07 10:55:13 +01:00
|
|
|
if err != nil {
|
2016-02-07 14:02:09 +01:00
|
|
|
t.Fatalf("Could not create n255x: %v", err)
|
2016-02-07 10:55:13 +01:00
|
|
|
}
|
|
|
|
f.Close()
|
2016-06-06 23:57:42 +02:00
|
|
|
if !test_helpers.VerifyExistence(wd + n255x) {
|
2016-02-07 10:55:13 +01:00
|
|
|
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 {
|
2016-02-07 14:02:09 +01:00
|
|
|
t.Fatalf("Could not rename n255x to n255y: %v", err)
|
2016-02-07 10:55:13 +01:00
|
|
|
}
|
2016-06-06 23:57:42 +02:00
|
|
|
if !test_helpers.VerifyExistence(wd + n255y) {
|
2016-02-07 10:55:13 +01:00
|
|
|
t.Errorf("n255y is not in directory listing")
|
|
|
|
}
|
|
|
|
// Rename long to short
|
|
|
|
err = os.Rename(wd+n255y, wd+"short")
|
|
|
|
if err != nil {
|
2016-02-07 14:02:09 +01:00
|
|
|
t.Fatalf("Could not rename n255y to short: %v", err)
|
2016-02-07 10:55:13 +01:00
|
|
|
}
|
2016-06-06 23:57:42 +02:00
|
|
|
if !test_helpers.VerifyExistence(wd + "short") {
|
2016-02-07 10:55:13 +01:00
|
|
|
t.Errorf("short is not in directory listing")
|
|
|
|
}
|
|
|
|
// Rename short to long
|
|
|
|
err = os.Rename(wd+"short", wd+n255x)
|
|
|
|
if err != nil {
|
2016-02-07 14:02:09 +01:00
|
|
|
t.Fatalf("Could not rename short to n255x: %v", err)
|
2016-02-07 10:55:13 +01:00
|
|
|
}
|
2016-06-06 23:57:42 +02:00
|
|
|
if !test_helpers.VerifyExistence(wd + n255x) {
|
2016-02-07 10:55:13 +01:00
|
|
|
t.Errorf("255x is not in directory listing II")
|
|
|
|
}
|
|
|
|
// Unlink
|
2016-02-07 13:28:55 +01:00
|
|
|
err = syscall.Unlink(wd + n255x)
|
2016-02-07 10:55:13 +01:00
|
|
|
if err != nil {
|
2016-02-07 14:02:09 +01:00
|
|
|
t.Fatalf("Could not unlink n255x: %v", err)
|
2016-02-07 10:55:13 +01:00
|
|
|
}
|
2016-06-06 23:57:42 +02:00
|
|
|
if test_helpers.VerifyExistence(wd + n255x) {
|
2016-02-07 10:55:13 +01:00
|
|
|
t.Errorf("n255x still there after unlink")
|
|
|
|
}
|
2016-02-07 14:02:09 +01:00
|
|
|
// Long symlink
|
|
|
|
n255s := string(bytes.Repeat([]byte("s"), 255))
|
|
|
|
err = os.Symlink("/etc/motd", wd+n255s)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-06-06 23:57:42 +02:00
|
|
|
if !test_helpers.VerifyExistence(wd + n255s) {
|
2016-02-07 14:02:09 +01:00
|
|
|
t.Errorf("n255s is not in directory listing")
|
|
|
|
}
|
|
|
|
err = syscall.Unlink(wd + n255s)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
// Long dir
|
|
|
|
n255d := string(bytes.Repeat([]byte("d"), 255))
|
|
|
|
err = os.Mkdir(wd+n255d, 0777)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = syscall.Rmdir(wd + n255d)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
// Check for orphaned files
|
2016-06-06 23:57:42 +02:00
|
|
|
fi, err = ioutil.ReadDir(test_helpers.DefaultCipherDir)
|
2016-02-07 14:02:09 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cnt2 := len(fi)
|
|
|
|
if cnt1 != cnt2 {
|
|
|
|
t.Errorf("Leftover files, cnt1=%d cnt2=%d", cnt1, cnt2)
|
|
|
|
}
|
2016-02-07 10:55:13 +01:00
|
|
|
}
|
2016-06-08 00:17:18 +02:00
|
|
|
|
|
|
|
func TestLchown(t *testing.T) {
|
2016-06-30 00:57:14 +02:00
|
|
|
name := test_helpers.DefaultPlainDir + "/symlink"
|
2016-06-08 00:17:18 +02:00
|
|
|
err := os.Symlink("/target/does/not/exist", name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = os.Chown(name, os.Getuid(), os.Getgid())
|
|
|
|
if err == nil {
|
|
|
|
t.Error("Chown on dangling symlink should fail")
|
|
|
|
}
|
|
|
|
err = os.Lchown(name, os.Getuid(), os.Getgid())
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|