diff options
-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)) + } +} |