From 1bae06a16a94950cfbb134f64a765bb34db1f167 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Mon, 7 Nov 2016 20:58:12 +0100 Subject: tests: add OpenTruncateRead test This is a regression test for the issue that was fixed by the last commit. --- tests/defaults/main_test.go | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'tests/defaults') diff --git a/tests/defaults/main_test.go b/tests/defaults/main_test.go index 95835a4..1ad5f55 100644 --- a/tests/defaults/main_test.go +++ b/tests/defaults/main_test.go @@ -2,6 +2,7 @@ package defaults import ( + "bytes" "os" "os/exec" "syscall" @@ -57,3 +58,51 @@ func TestCtlSock(t *testing.T) { t.Errorf("incorrect error handling: %+v", response) } } + +// 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)) + } +} -- cgit v1.2.3