diff options
author | Jakob Unterwurzacher | 2017-08-10 20:55:45 +0200 |
---|---|---|
committer | Jakob Unterwurzacher | 2017-08-10 21:01:19 +0200 |
commit | f59479736bed49411bda3368f419d6605f1faa78 (patch) | |
tree | c161f865e50a8ec6a970ff1dbc2e5e62623b0859 | |
parent | e80b5f2049edb794e340da629bce9e44485a4dbb (diff) |
tests: add diriv cache race test
Passes.
-rw-r--r-- | tests/defaults/diriv_test.go | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/tests/defaults/diriv_test.go b/tests/defaults/diriv_test.go new file mode 100644 index 0000000..75f1511 --- /dev/null +++ b/tests/defaults/diriv_test.go @@ -0,0 +1,73 @@ +package defaults + +import ( + "io/ioutil" + "os" + "sync" + "syscall" + "testing" + "time" + + "github.com/rfjakob/gocryptfs/tests/test_helpers" +) + +func TestDirIVRace(t *testing.T) { + // Create "dir1" with one file in it + dir1 := test_helpers.DefaultPlainDir + "/TestDirIVRace_Dir1" + err := os.Mkdir(dir1, 0700) + if err != nil { + t.Fatal(err) + } + err = ioutil.WriteFile(dir1+"/file", nil, 0600) + if err != nil { + t.Fatal(err) + } + + // Create directory "dir2" + dir2 := test_helpers.DefaultPlainDir + "/TestDirIVRace_Dir2" + err = os.Mkdir(dir2, 0700) + if err != nil { + t.Fatal(err) + } + file2 := dir2 + "/file" + err = ioutil.WriteFile(file2, nil, 0600) + if err != nil { + t.Fatal(err) + } + + stop := false + defer func() { stop = true }() + + var wg sync.WaitGroup + wg.Add(1) + go func() { + wg.Done() + for { + // Keep dir2 in the diriv cache + fd, err2 := os.Open(file2) + if err2 != nil { + fd.Close() + } + if stop { + return + } + } + }() + wg.Wait() + time.Sleep(time.Millisecond) + + // Overwrite dir2 with dir1 + err = syscall.Unlink(file2) + if err != nil { + t.Fatal(err) + } + err = syscall.Rename(dir1, dir2) + if err != nil { + t.Fatal(err) + } + // We should be able to stat file2 + _, err = os.Stat(file2) + if err != nil { + t.Error(err) + } +} |