tests: add OpenTruncateRead test
This is a regression test for the issue that was fixed by the last commit.
This commit is contained in:
parent
0489d08ae2
commit
1bae06a16a
@ -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))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user