diff options
author | Jakob Unterwurzacher | 2016-11-07 20:58:12 +0100 |
---|---|---|
committer | Jakob Unterwurzacher | 2016-11-17 22:48:50 +0100 |
commit | 1bae06a16a94950cfbb134f64a765bb34db1f167 (patch) | |
tree | bbccfbd879fa852612fa6a0a3a59f3caf7665184 /tests | |
parent | 0489d08ae21107990d0efd0685443293aa26b35f (diff) |
tests: add OpenTruncateRead test
This is a regression test for the issue that was fixed by the
last commit.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/defaults/main_test.go | 49 |
1 files changed, 49 insertions, 0 deletions
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)) + } +} |