2016-10-16 13:39:25 +02:00
|
|
|
// Tests run for (almost all) combinations of openssl, aessiv, plaintextnames.
|
2016-06-30 00:57:14 +02:00
|
|
|
package matrix
|
2015-09-06 11:42:01 +02:00
|
|
|
|
2021-07-31 13:23:05 +02:00
|
|
|
// File reading, writing, modification, truncate, ...
|
2016-06-30 00:57:14 +02:00
|
|
|
//
|
2021-07-31 13:23:05 +02:00
|
|
|
// Runs all tests N times, for the combinations of different flags specified
|
|
|
|
// in the `matrix` variable.
|
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-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"
|
2020-02-15 21:56:08 +01:00
|
|
|
"math/rand"
|
2015-09-06 11:42:01 +02:00
|
|
|
"os"
|
2016-09-25 16:17:26 +02:00
|
|
|
"os/exec"
|
2021-06-06 19:22:16 +02:00
|
|
|
"path/filepath"
|
2015-11-01 12:11:36 +01:00
|
|
|
"runtime"
|
2021-07-31 13:23:05 +02:00
|
|
|
"strings"
|
2015-11-01 12:11:36 +01:00
|
|
|
"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
|
|
|
|
2018-09-30 19:33:52 +02:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
2018-03-05 23:11:27 +01:00
|
|
|
"github.com/rfjakob/gocryptfs/internal/stupidgcm"
|
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
|
2016-10-08 19:18:56 +02:00
|
|
|
var testcase testcaseMatrix
|
2015-11-03 22:27:11 +01:00
|
|
|
|
2016-10-08 19:15:28 +02:00
|
|
|
type testcaseMatrix struct {
|
2016-11-01 18:39:00 +01:00
|
|
|
plaintextnames bool
|
|
|
|
openssl string
|
|
|
|
aessiv bool
|
|
|
|
raw64 bool
|
2019-09-01 12:51:42 +02:00
|
|
|
extraArgs []string
|
2016-10-08 19:15:28 +02:00
|
|
|
}
|
|
|
|
|
2016-10-19 01:12:45 +02:00
|
|
|
var matrix = []testcaseMatrix{
|
2016-10-08 19:15:28 +02:00
|
|
|
// Normal
|
2019-09-01 12:51:42 +02:00
|
|
|
{false, "auto", false, false, nil},
|
|
|
|
{false, "true", false, false, nil},
|
|
|
|
{false, "false", false, false, nil},
|
2016-10-08 19:15:28 +02:00
|
|
|
// Plaintextnames
|
2019-09-01 12:51:42 +02:00
|
|
|
{true, "true", false, false, nil},
|
|
|
|
{true, "false", false, false, nil},
|
2016-10-08 19:15:28 +02:00
|
|
|
// AES-SIV (does not use openssl, no need to test permutations)
|
2019-09-01 12:51:42 +02:00
|
|
|
{false, "auto", true, false, nil},
|
|
|
|
{true, "auto", true, false, nil},
|
2016-11-01 18:39:00 +01:00
|
|
|
// Raw64
|
2019-09-01 12:51:42 +02:00
|
|
|
{false, "auto", false, true, nil},
|
|
|
|
// -serialize_reads
|
|
|
|
{false, "auto", false, false, []string{"-serialize_reads"}},
|
tests: matrix: add -sharestorage to test matrix
Currently fails like this:
=== RUN TestRmwRace
doRead 0: corrupt block #0: cipher: message authentication failed
-wpanic turns this warning into a panic: doRead 0: corrupt block #0: cipher: message authentication failed
panic: -wpanic turns this warning into a panic: doRead 0: corrupt block #0: cipher: message authentication failed
goroutine 1293 [running]:
log.(*Logger).Panic(0xc00011c230, 0xc0003b17c8, 0x1, 0x1)
log/log.go:224 +0xac
github.com/rfjakob/gocryptfs/internal/tlog.(*toggledLogger).Printf(0xc00007a780, 0x55a821a766a1, 0x20, 0xc0003b19f0, 0x3, 0x3)
github.com/rfjakob/gocryptfs/internal/tlog/log.go:78 +0x1ef
github.com/rfjakob/gocryptfs/internal/fusefrontend.(*File).doRead(0xc0001ff420, 0x0, 0x0, 0x0, 0x0, 0x1000, 0x0, 0x1, 0xc000880000, 0x1020)
github.com/rfjakob/gocryptfs/internal/fusefrontend/file.go:201 +0x8c9
github.com/rfjakob/gocryptfs/internal/fusefrontend.(*File).doWrite(0xc0001ff420, 0xc000248428, 0x10, 0x30, 0xff0, 0x3, 0x18)
github.com/rfjakob/gocryptfs/internal/fusefrontend/file.go:291 +0xc9e
github.com/rfjakob/gocryptfs/internal/fusefrontend.(*File).Write(0xc0001ff420, 0x55a821b306a0, 0xc000fbde90, 0xc000248428, 0x10, 0x30, 0xff0, 0x7f4a00000000, 0x0)
github.com/rfjakob/gocryptfs/internal/fusefrontend/file.go:378 +0x25e
github.com/hanwen/go-fuse/v2/fs.(*rawBridge).Write(0xc000168140, 0xc000096000, 0xc0002483d8, 0xc000248428, 0x10, 0x30, 0x55a821ad40e0)
github.com/hanwen/go-fuse/v2@v2.0.4-0.20210125162859-8e0bbdb16cb7/fs/bridge.go:819 +0x26d
github.com/hanwen/go-fuse/v2/fuse.doWrite(0xc000170160, 0xc000248240)
github.com/hanwen/go-fuse/v2@v2.0.4-0.20210125162859-8e0bbdb16cb7/fuse/opcode.go:191 +0x6f
github.com/hanwen/go-fuse/v2/fuse.(*Server).handleRequest(0xc000170160, 0xc000248240, 0xc000000000)
github.com/hanwen/go-fuse/v2@v2.0.4-0.20210125162859-8e0bbdb16cb7/fuse/server.go:472 +0x2be
github.com/hanwen/go-fuse/v2/fuse.(*Server).loop(0xc000170160, 0xc000cd4101)
github.com/hanwen/go-fuse/v2@v2.0.4-0.20210125162859-8e0bbdb16cb7/fuse/server.go:445 +0x198
created by github.com/hanwen/go-fuse/v2/fuse.(*Server).readRequest
github.com/hanwen/go-fuse/v2@v2.0.4-0.20210125162859-8e0bbdb16cb7/fuse/server.go:312 +0x41d
matrix_test.go:354: Write failed
2021-03-20 18:27:34 +01:00
|
|
|
{false, "auto", false, false, []string{"-sharedstorage"}},
|
2016-10-08 19:15:28 +02: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-10-08 19:18:56 +02:00
|
|
|
for _, testcase = range matrix {
|
2018-03-05 23:11:27 +01:00
|
|
|
if testcase.openssl == "true" && stupidgcm.BuiltWithoutOpenssl {
|
|
|
|
continue
|
|
|
|
}
|
2016-10-08 19:15:28 +02:00
|
|
|
if testing.Verbose() {
|
2016-11-01 18:39:00 +01:00
|
|
|
fmt.Printf("matrix: testcase = %#v\n", testcase)
|
2016-10-08 19:15:28 +02:00
|
|
|
}
|
2016-11-01 18:39:00 +01:00
|
|
|
test_helpers.ResetTmpDir(!testcase.plaintextnames)
|
2016-10-08 19:15:28 +02:00
|
|
|
opts := []string{"-zerokey"}
|
2020-07-12 15:08:17 +02:00
|
|
|
//opts = append(opts, "-fusedebug")
|
2016-11-01 18:39:00 +01:00
|
|
|
opts = append(opts, fmt.Sprintf("-openssl=%v", testcase.openssl))
|
|
|
|
opts = append(opts, fmt.Sprintf("-plaintextnames=%v", testcase.plaintextnames))
|
|
|
|
opts = append(opts, fmt.Sprintf("-aessiv=%v", testcase.aessiv))
|
|
|
|
opts = append(opts, fmt.Sprintf("-raw64=%v", testcase.raw64))
|
2019-09-01 12:51:42 +02:00
|
|
|
opts = append(opts, testcase.extraArgs...)
|
2016-10-08 19:15:28 +02:00
|
|
|
test_helpers.MountOrExit(test_helpers.DefaultCipherDir, test_helpers.DefaultPlainDir, opts...)
|
2019-10-06 19:04:16 +02:00
|
|
|
before := test_helpers.ListFds(0, test_helpers.TmpDir)
|
2016-10-08 19:15:28 +02:00
|
|
|
r := m.Run()
|
2019-01-01 19:34:12 +01:00
|
|
|
// Catch fd leaks in the tests. NOTE: this does NOT catch leaks in
|
|
|
|
// the gocryptfs FUSE process, but only in the tests that access it!
|
2019-10-06 19:04:16 +02:00
|
|
|
// All fds that point outside TmpDir are not interesting (the Go test
|
|
|
|
// infrastucture creates temporary log files we don't care about).
|
|
|
|
after := test_helpers.ListFds(0, test_helpers.TmpDir)
|
2018-04-07 15:21:05 +02:00
|
|
|
if len(before) != len(after) {
|
2019-10-06 18:48:09 +02:00
|
|
|
fmt.Printf("fd leak in test process? before, after:\n%v\n%v\n", before, after)
|
2018-04-07 15:21:05 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2016-10-08 19:15:28 +02:00
|
|
|
test_helpers.UnmountPanic(test_helpers.DefaultPlainDir)
|
|
|
|
if r != 0 {
|
|
|
|
os.Exit(r)
|
2016-06-30 00:57:14 +02:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2020-02-15 21:56:08 +01:00
|
|
|
// Write `n` random 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)
|
2020-02-15 21:56:08 +01:00
|
|
|
for i := range d {
|
|
|
|
// Fill with pattern
|
|
|
|
d[i] = byte(rand.Int())
|
|
|
|
}
|
2019-02-17 17:25:41 +01:00
|
|
|
_, err = file.Write(d)
|
2019-02-17 17:13:20 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2015-11-27 00:03:10 +01:00
|
|
|
}
|
|
|
|
err = file.Close()
|
|
|
|
if err != nil {
|
2019-02-17 17:13:20 +01:00
|
|
|
t.Fatal(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
|
|
|
|
2016-07-06 21:06:42 +02:00
|
|
|
hashWant := test_helpers.Md5hex(d)
|
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++ {
|
2020-02-15 21:56:08 +01:00
|
|
|
hashActual := test_helpers.Md5fn(test_helpers.DefaultPlainDir + "/100x100")
|
2015-09-08 22:03:27 +02:00
|
|
|
if hashActual != hashWant {
|
2020-02-15 21:56:08 +01:00
|
|
|
fmt.Printf("Read corruption in loop #%d: have=%s want=%s\n", i, hashActual, hashWant)
|
2015-09-06 11:42:01 +02:00
|
|
|
t.FailNow()
|
|
|
|
} else {
|
|
|
|
//fmt.Print(".")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 20:27:54 +01:00
|
|
|
func TestWrite10Tight(t *testing.T) {
|
|
|
|
path := test_helpers.DefaultPlainDir + "/TestWrite10Tight"
|
|
|
|
content := make([]byte, 10)
|
|
|
|
buf := make([]byte, 100)
|
2019-04-08 20:34:24 +02:00
|
|
|
for i := 0; i < 100; i++ {
|
2019-03-05 20:27:54 +01:00
|
|
|
file, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_, err = file.Write(content)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = file.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
file, err = os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
n, err := file.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if n != 10 {
|
|
|
|
t.Fatalf("want 10 bytes, got %d", n)
|
|
|
|
}
|
|
|
|
err = file.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = os.Remove(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
2018-04-01 14:51:07 +02:00
|
|
|
defer file.Close()
|
2015-09-30 22:36:53 +02:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
2018-04-01 14:51:07 +02:00
|
|
|
defer file.Close()
|
2015-09-30 23:42:18 +02:00
|
|
|
data := []byte("testdata123456789") // length 17
|
|
|
|
var buf bytes.Buffer
|
|
|
|
var hashWant string
|
|
|
|
for i := 0; i <= 500; i++ {
|
|
|
|
file.Write(data)
|
|
|
|
buf.Write(data)
|
2016-07-06 21:06:42 +02:00
|
|
|
hashWant = test_helpers.Md5hex(buf.Bytes())
|
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")
|
|
|
|
}
|
2018-04-01 14:51:07 +02:00
|
|
|
defer file.Close()
|
2015-10-04 10:38:58 +02:00
|
|
|
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
|
|
|
}
|
2018-03-05 21:56:30 +01:00
|
|
|
defer f1.Close()
|
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
|
|
|
}
|
2018-03-05 21:56:30 +01:00
|
|
|
defer f2.Close()
|
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-11-01 18:39:00 +01:00
|
|
|
if testcase.plaintextnames && err == nil {
|
2015-11-14 17:16:17 +01:00
|
|
|
t.Errorf("should have failed but didn't")
|
2016-11-01 18:39:00 +01:00
|
|
|
} else if !testcase.plaintextnames && err != nil {
|
2015-11-03 22:27:11 +01:00
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
|
|
|
|
err = os.Remove(filteredFile)
|
2016-11-01 18:39:00 +01:00
|
|
|
if testcase.plaintextnames && err == nil {
|
2015-11-14 17:16:17 +01:00
|
|
|
t.Errorf("should have failed but didn't")
|
2016-11-01 18:39:00 +01:00
|
|
|
} else if !testcase.plaintextnames && 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")
|
2016-11-01 18:39:00 +01:00
|
|
|
if testcase.plaintextnames && err != nil {
|
2015-11-03 22:27:11 +01:00
|
|
|
t.Errorf("plaintextnames not working: %v", err)
|
2016-11-01 18:39:00 +01:00
|
|
|
} else if !testcase.plaintextnames && 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
|
|
|
|
2015-12-06 15:10:23 +01:00
|
|
|
// Test Rename
|
2019-01-20 13:41:20 +01:00
|
|
|
func TestRename(t *testing.T) {
|
2016-06-06 23:57:42 +02:00
|
|
|
test_helpers.TestRename(t, test_helpers.DefaultPlainDir)
|
2015-12-06 15:10:23 +01:00
|
|
|
}
|
2015-12-11 23:27:38 +01:00
|
|
|
|
2019-02-17 17:05:05 +01:00
|
|
|
// Test that names of all lengths work
|
|
|
|
func TestNameLengths(t *testing.T) {
|
|
|
|
f, err := os.Open(test_helpers.DefaultPlainDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
entries, err := f.Readdirnames(0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
cnt1 := len(entries)
|
|
|
|
|
|
|
|
wd := test_helpers.DefaultPlainDir + "/"
|
|
|
|
name := "x"
|
|
|
|
for len(name) < 2000 {
|
|
|
|
f, err := os.Create(wd + name + "x")
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
name = name + "x"
|
|
|
|
f.Close()
|
|
|
|
f, err = os.Open(test_helpers.DefaultPlainDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// In v1.7-rc2, we had a bug that allowed creation of too-long names.
|
|
|
|
// This threw errors in like this in READDIR:
|
|
|
|
//
|
|
|
|
// OpenDir ".": invalid entry "gocryptfs.longname.wrE-izsR9ciEkP7JSCFDrk_d_Nj4mQo1dGY6hjuixAU=":
|
|
|
|
// Could not read .name: ReadLongName: size=345 > limit=344
|
|
|
|
//
|
|
|
|
entries, err = f.Readdirnames(0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
cnt2 := len(entries)
|
|
|
|
if cnt2 != cnt1+1 {
|
|
|
|
t.Fatalf("len=%d: expected %d dir entries, have %d: %v", len(name), cnt1+1, cnt2, entries)
|
|
|
|
}
|
|
|
|
err = syscall.Unlink(wd + name)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(name) != 255 {
|
|
|
|
t.Errorf("maxlen=%d", len(name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
2020-08-15 15:39:08 +02:00
|
|
|
if !test_helpers.VerifyExistence(t, wd+n255x) {
|
2016-02-07 10:55:13 +01:00
|
|
|
t.Errorf("n255x is not in directory listing")
|
|
|
|
}
|
2017-11-25 01:56:56 +01:00
|
|
|
// Rename long to long (target does not exist)
|
2016-02-07 10:55:13 +01:00
|
|
|
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
|
|
|
}
|
2020-08-15 15:39:08 +02:00
|
|
|
if !test_helpers.VerifyExistence(t, wd+n255y) {
|
2016-02-07 10:55:13 +01:00
|
|
|
t.Errorf("n255y is not in directory listing")
|
|
|
|
}
|
2017-11-25 01:56:56 +01:00
|
|
|
// Rename long to long (target exists)
|
|
|
|
f, err = os.Create(wd + n255x)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not create n255x: %v", err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
err = os.Rename(wd+n255x, wd+n255y)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not rename n255x to n255y: %v", err)
|
|
|
|
}
|
2020-08-15 15:39:08 +02:00
|
|
|
if !test_helpers.VerifyExistence(t, wd+n255y) {
|
2017-11-25 01:56:56 +01:00
|
|
|
t.Errorf("n255y is not in directory listing")
|
|
|
|
}
|
|
|
|
// Rename long to short (target does not exist)
|
2016-02-07 10:55:13 +01:00
|
|
|
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
|
|
|
}
|
2020-08-15 15:39:08 +02:00
|
|
|
if !test_helpers.VerifyExistence(t, wd+"short") {
|
2016-02-07 10:55:13 +01:00
|
|
|
t.Errorf("short is not in directory listing")
|
|
|
|
}
|
2017-11-25 01:56:56 +01:00
|
|
|
// Rename long to short (target exists)
|
|
|
|
f, err = os.Create(wd + n255y)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not create n255y: %v", err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
err = os.Rename(wd+n255y, wd+"short")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not rename n255y to short: %v", err)
|
|
|
|
}
|
2020-08-15 15:39:08 +02:00
|
|
|
if !test_helpers.VerifyExistence(t, wd+"short") {
|
2017-11-25 01:56:56 +01:00
|
|
|
t.Errorf("short is not in directory listing")
|
|
|
|
}
|
|
|
|
// Rename short to long (target does not exist)
|
2016-02-07 10:55:13 +01:00
|
|
|
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
|
|
|
}
|
2020-08-15 15:39:08 +02:00
|
|
|
if !test_helpers.VerifyExistence(t, wd+n255x) {
|
2016-02-07 10:55:13 +01:00
|
|
|
t.Errorf("255x is not in directory listing II")
|
|
|
|
}
|
2017-11-25 01:56:56 +01:00
|
|
|
// Rename short to long (target exists)
|
|
|
|
f, err = os.Create(wd + "short")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not create short: %v", err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
err = os.Rename(wd+"short", wd+n255x)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not rename short to n255x: %v", err)
|
|
|
|
}
|
2020-08-15 15:39:08 +02:00
|
|
|
if !test_helpers.VerifyExistence(t, wd+n255x) {
|
2017-11-25 01:56:56 +01:00
|
|
|
t.Errorf("n255x is not in directory listing")
|
|
|
|
}
|
2016-02-07 10:55:13 +01:00
|
|
|
// 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
|
|
|
}
|
2020-08-15 15:39:08 +02:00
|
|
|
if test_helpers.VerifyExistence(t, 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))
|
2016-10-04 22:01:47 +02:00
|
|
|
err = os.Symlink("/", wd+n255s)
|
2016-02-07 14:02:09 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-08-15 15:39:08 +02:00
|
|
|
if !test_helpers.VerifyExistence(t, 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
|
|
|
|
2017-01-26 20:49:35 +01:00
|
|
|
// Create hard link with long name.
|
|
|
|
// This was broken up to v1.2.
|
|
|
|
func TestLongLink(t *testing.T) {
|
|
|
|
wd := test_helpers.DefaultPlainDir + "/"
|
|
|
|
target := wd + "TestLongLink.target"
|
|
|
|
f, err := os.Create(target)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%v", err)
|
|
|
|
}
|
|
|
|
f.Close()
|
|
|
|
l255 := string(bytes.Repeat([]byte("l"), 255))
|
|
|
|
err = os.Link(target, wd+l255)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2016-08-09 22:10:25 +02:00
|
|
|
|
2016-10-16 13:57:27 +02:00
|
|
|
// Set nanoseconds by path, symlink
|
2018-09-08 16:35:53 +02:00
|
|
|
func TestUtimesNanoSymlink(t *testing.T) {
|
2018-03-05 21:32:07 +01:00
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
t.Skipf("MacOS \"touch\" does not support \"--no-dereference\"")
|
|
|
|
}
|
2016-10-16 13:57:27 +02:00
|
|
|
path := test_helpers.DefaultPlainDir + "/utimesnano_symlink"
|
|
|
|
err := os.Symlink("/some/nonexisting/file", path)
|
2016-08-09 22:10:25 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-10-16 13:57:27 +02:00
|
|
|
// syscall.UtimesNano does not provide a way to pass AT_SYMLINK_NOFOLLOW,
|
|
|
|
// so we call the external utility "touch", which does.
|
|
|
|
cmd := exec.Command("touch", "--no-dereference", path)
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
err = cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-16 14:58:36 +02:00
|
|
|
type utimesTestcaseStruct struct {
|
|
|
|
// Input atime and mtime
|
|
|
|
in [2]syscall.Timespec
|
|
|
|
// Expected output atime and mtime
|
|
|
|
out [2]syscall.Timespec
|
|
|
|
}
|
|
|
|
|
2018-03-05 22:22:35 +01:00
|
|
|
// compareTimespec return true if the two passed Timespec are identical.
|
|
|
|
func compareTimespec(want syscall.Timespec, actual syscall.Timespec) bool {
|
|
|
|
if want.Sec != actual.Sec {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if want.Nsec != actual.Nsec {
|
|
|
|
return false
|
2016-10-16 14:58:36 +02:00
|
|
|
}
|
2018-03-05 22:22:35 +01:00
|
|
|
return true
|
2016-10-16 14:58:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const _UTIME_OMIT = ((1 << 30) - 2)
|
|
|
|
|
2016-10-16 13:57:27 +02:00
|
|
|
// doTestUtimesNano verifies that setting nanosecond-precision times on "path"
|
|
|
|
// works correctly. Pass "/proc/self/fd/N" to test a file descriptor.
|
|
|
|
func doTestUtimesNano(t *testing.T, path string) {
|
2016-10-16 14:58:36 +02:00
|
|
|
utimeTestcases := []utimesTestcaseStruct{
|
2016-10-19 22:25:54 +02:00
|
|
|
{
|
2018-03-05 22:45:45 +01:00
|
|
|
in: [2]syscall.Timespec{{Sec: 50, Nsec: 0}, {Sec: 51, Nsec: 0}},
|
|
|
|
out: [2]syscall.Timespec{{Sec: 50, Nsec: 0}, {Sec: 51, Nsec: 0}},
|
2016-10-19 22:25:54 +02:00
|
|
|
},
|
2016-10-16 14:58:36 +02:00
|
|
|
{
|
|
|
|
in: [2]syscall.Timespec{{Sec: 1, Nsec: 2}, {Sec: 3, Nsec: 4}},
|
|
|
|
out: [2]syscall.Timespec{{Sec: 1, Nsec: 2}, {Sec: 3, Nsec: 4}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: [2]syscall.Timespec{{Sec: 7, Nsec: 8}, {Sec: 99, Nsec: _UTIME_OMIT}},
|
2016-10-30 16:29:36 +01:00
|
|
|
out: [2]syscall.Timespec{{Sec: 7, Nsec: 8}, {Sec: 3, Nsec: 4}},
|
2016-10-16 14:58:36 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
in: [2]syscall.Timespec{{Sec: 99, Nsec: _UTIME_OMIT}, {Sec: 5, Nsec: 6}},
|
2016-10-30 16:29:36 +01:00
|
|
|
out: [2]syscall.Timespec{{Sec: 7, Nsec: 8}, {Sec: 5, Nsec: 6}},
|
2016-10-16 14:58:36 +02:00
|
|
|
},
|
|
|
|
}
|
2018-03-05 22:45:45 +01:00
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
// darwin neither supports UTIME_OMIT nor nanoseconds (!?)
|
|
|
|
utimeTestcases = utimeTestcases[:1]
|
|
|
|
}
|
2016-10-16 14:58:36 +02:00
|
|
|
for i, tc := range utimeTestcases {
|
|
|
|
err := syscall.UtimesNano(path, tc.in[:])
|
|
|
|
if err != nil {
|
2019-01-14 22:11:15 +01:00
|
|
|
t.Fatalf("%q: %v", path, err)
|
2016-10-16 14:58:36 +02:00
|
|
|
}
|
|
|
|
var st syscall.Stat_t
|
|
|
|
err = syscall.Stat(path, &st)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-03-05 22:22:35 +01:00
|
|
|
want := tc.out
|
|
|
|
have := extractAtimeMtime(st)
|
|
|
|
if !compareTimespec(want[0], have[0]) {
|
|
|
|
t.Errorf("Testcase %d: atime: want=%+v, have=%+v", i, want[0], have[0])
|
|
|
|
}
|
|
|
|
if !compareTimespec(want[1], have[1]) {
|
|
|
|
t.Errorf("Testcase %d: mtime: want=%+v, have=%+v", i, want[1], have[1])
|
2016-09-25 16:42:29 +02:00
|
|
|
}
|
2016-08-09 22:10:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-16 13:57:27 +02:00
|
|
|
// Set nanoseconds by path, normal file
|
|
|
|
func TestUtimesNano(t *testing.T) {
|
|
|
|
path := test_helpers.DefaultPlainDir + "/utimesnano"
|
|
|
|
err := ioutil.WriteFile(path, []byte("foobar"), 0600)
|
2016-09-25 16:17:26 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-10-16 13:57:27 +02:00
|
|
|
doTestUtimesNano(t, path)
|
2016-09-25 16:17:26 +02:00
|
|
|
}
|
|
|
|
|
2016-08-09 22:10:25 +02:00
|
|
|
// Set nanoseconds by fd
|
|
|
|
func TestUtimesNanoFd(t *testing.T) {
|
2018-02-28 20:48:33 +01:00
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
t.Skipf("MacOS does not have /proc")
|
|
|
|
}
|
2016-08-09 22:10:25 +02:00
|
|
|
path := test_helpers.DefaultPlainDir + "/utimesnanofd"
|
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-03-06 21:22:01 +01:00
|
|
|
defer f.Close()
|
2016-08-09 22:10:25 +02:00
|
|
|
procPath := fmt.Sprintf("/proc/self/fd/%d", f.Fd())
|
2016-10-16 13:57:27 +02:00
|
|
|
doTestUtimesNano(t, procPath)
|
2016-08-09 22:10:25 +02:00
|
|
|
}
|
2017-11-26 21:59:24 +01:00
|
|
|
|
|
|
|
// Make sure the Mknod call works by creating a fifo (named pipe)
|
|
|
|
func TestMkfifo(t *testing.T) {
|
|
|
|
path := test_helpers.DefaultPlainDir + "/fifo1"
|
|
|
|
err := syscall.Mkfifo(path, 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-11-28 00:20:42 +01:00
|
|
|
path = test_helpers.DefaultPlainDir + "/gocryptfs.longname.XXX"
|
|
|
|
err = syscall.Mkfifo(path, 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-11-28 01:22:55 +01:00
|
|
|
err = os.Remove(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-17 00:23:09 +01:00
|
|
|
// TestMagicNames verifies that "magic" names are handled correctly
|
|
|
|
// https://github.com/rfjakob/gocryptfs/issues/174
|
|
|
|
func TestMagicNames(t *testing.T) {
|
2020-07-12 20:58:38 +02:00
|
|
|
names := []string{"warmup1", "warmup2", "gocryptfs.longname.QhUr5d9FHerwEs--muUs6_80cy6JRp89c1otLwp92Cs", "gocryptfs.diriv"}
|
2018-01-17 00:23:09 +01:00
|
|
|
for _, n := range names {
|
|
|
|
t.Logf("Testing n=%q", n)
|
|
|
|
p := test_helpers.DefaultPlainDir + "/" + n
|
|
|
|
// Create file
|
|
|
|
err := ioutil.WriteFile(p, []byte("xxxxxxx"), 0200)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("creating file %q failed: %v", n, err)
|
|
|
|
}
|
|
|
|
// Rename magic to normal
|
|
|
|
err = os.Rename(p, test_helpers.DefaultPlainDir+"/x")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("rename 1 failed: %v", err)
|
|
|
|
}
|
|
|
|
// Rename normal to magic
|
|
|
|
err = os.Rename(test_helpers.DefaultPlainDir+"/x", p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("rename 2 failed: %v", err)
|
|
|
|
}
|
|
|
|
// Unlink
|
|
|
|
err = syscall.Unlink(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Mkdir
|
|
|
|
err = os.Mkdir(p, 0700)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Rmdir
|
|
|
|
err = syscall.Rmdir(p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Symlink
|
|
|
|
err = syscall.Symlink("xxxyyyyzzz", p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
syscall.Unlink(p)
|
|
|
|
// Link
|
|
|
|
target := test_helpers.DefaultPlainDir + "/linktarget"
|
2020-07-12 20:58:38 +02:00
|
|
|
err = ioutil.WriteFile(target, []byte("yyyyy"), 0600)
|
2018-01-17 00:23:09 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = syscall.Link(target, p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-12-12 14:38:00 +01:00
|
|
|
}
|
|
|
|
}
|
2018-09-08 17:00:23 +02:00
|
|
|
|
|
|
|
// Test that chmod works correctly
|
|
|
|
func TestChmod(t *testing.T) {
|
2020-05-09 15:42:57 +02:00
|
|
|
path := test_helpers.DefaultPlainDir + "/" + t.Name()
|
2018-09-08 17:00:23 +02:00
|
|
|
file, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
file.Close()
|
|
|
|
modes := []os.FileMode{0777, 0707, 0606, 0666, 0444, 0000, 0111, 0123, 0321}
|
|
|
|
for _, modeWant := range modes {
|
|
|
|
fi, err := os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-09-22 13:34:03 +02:00
|
|
|
err = syscall.Chmod(path, uint32(modeWant))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("chmod %03o -> %03o failed: %v", fi.Mode(), modeWant, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fi, err = os.Stat(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-09-08 17:00:23 +02:00
|
|
|
modeHave := fi.Mode()
|
|
|
|
if modeHave != modeWant {
|
|
|
|
t.Errorf("modeHave %#o != modeWant %#o", modeHave, modeWant)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-30 19:33:52 +02:00
|
|
|
|
|
|
|
// Test that access(2) works correctly
|
|
|
|
func TestAccess(t *testing.T) {
|
2020-05-09 15:42:57 +02:00
|
|
|
path := test_helpers.DefaultPlainDir + "/" + t.Name()
|
2018-09-30 19:33:52 +02:00
|
|
|
file, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
err = unix.Access(path, unix.F_OK)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
err = unix.Access(path, unix.R_OK)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
err = unix.Access(path, unix.X_OK)
|
|
|
|
if err == nil {
|
|
|
|
t.Error("X_OK should have failed")
|
|
|
|
}
|
|
|
|
}
|
2020-08-16 19:41:00 +02:00
|
|
|
|
|
|
|
func TestStatfs(t *testing.T) {
|
|
|
|
var st syscall.Statfs_t
|
|
|
|
syscall.Statfs(test_helpers.DefaultPlainDir, &st)
|
|
|
|
if st.Bsize == 0 {
|
|
|
|
t.Errorf("statfs reports size zero: %#v", st)
|
|
|
|
}
|
|
|
|
}
|
2021-06-06 19:22:16 +02:00
|
|
|
|
|
|
|
// gocryptfs 2.0 reported the ciphertext size on symlink creation, causing
|
|
|
|
// confusion: https://github.com/rfjakob/gocryptfs/issues/574
|
|
|
|
func TestSymlinkSize(t *testing.T) {
|
|
|
|
p := filepath.Join(test_helpers.DefaultPlainDir, t.Name())
|
|
|
|
// SYMLINK reports the size to the kernel
|
|
|
|
if err := syscall.Symlink("foo", p); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Kernel serves us this value from the attr cache
|
|
|
|
var st syscall.Stat_t
|
|
|
|
if err := syscall.Lstat(p, &st); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if st.Size != 3 {
|
|
|
|
t.Errorf("wrong size: have %d, want %d", st.Size, 3)
|
|
|
|
}
|
|
|
|
}
|
2021-07-31 13:23:05 +02:00
|
|
|
|
|
|
|
// TestPwd check that /usr/bin/pwd works inside gocryptfs.
|
|
|
|
//
|
|
|
|
// This was broken in gocryptfs v2.0 with -sharedstorage:
|
|
|
|
// https://github.com/rfjakob/gocryptfs/issues/584
|
|
|
|
func TestPwd(t *testing.T) {
|
|
|
|
dir := test_helpers.DefaultPlainDir
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
cmd := exec.Command("pwd")
|
|
|
|
cmd.Dir = dir
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
t.Log(strings.TrimSpace(string(out)))
|
|
|
|
t.Fatalf("dir %q: %v", dir, err)
|
|
|
|
}
|
|
|
|
dir = dir + "/" + t.Name()
|
|
|
|
os.Mkdir(dir, 0700)
|
|
|
|
}
|
|
|
|
}
|