libgocryptfs/tests/defaults/diriv_test.go

75 lines
1.3 KiB
Go
Raw Normal View History

package defaults
import (
"io/ioutil"
"os"
"sync"
tests: fix data race in TestDirIVRace Ironically, the test for DirIV races had a data race itself by writing to a bool without taking a lock. Found by running "./test.bash -race": WARNING: DATA RACE Write at 0x00c00001dea5 by goroutine 22: github.com/rfjakob/gocryptfs/tests/defaults.TestDirIVRace.func1() /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/defaults/diriv_test.go:39 +0x38 github.com/rfjakob/gocryptfs/tests/defaults.TestDirIVRace() /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/defaults/diriv_test.go:73 +0x65c testing.tRunner() /usr/local/go/src/testing/testing.go:865 +0x163 Previous read at 0x00c00001dea5 by goroutine 23: github.com/rfjakob/gocryptfs/tests/defaults.TestDirIVRace.func2() /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/defaults/diriv_test.go:51 +0x8b Goroutine 22 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:916 +0x699 testing.runTests.func1() /usr/local/go/src/testing/testing.go:1157 +0xa8 testing.tRunner() /usr/local/go/src/testing/testing.go:865 +0x163 testing.runTests() /usr/local/go/src/testing/testing.go:1155 +0x523 testing.(*M).Run() /usr/local/go/src/testing/testing.go:1072 +0x2eb github.com/rfjakob/gocryptfs/tests/defaults.TestMain() /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/defaults/main_test.go:21 +0xe1 main.main() _testmain.go:76 +0x222 Goroutine 23 (running) created at: github.com/rfjakob/gocryptfs/tests/defaults.TestDirIVRace() /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/defaults/diriv_test.go:43 +0x48d testing.tRunner() /usr/local/go/src/testing/testing.go:865 +0x163 ================== --- FAIL: TestDirIVRace (0.00s) testing.go:809: race detected during execution of test FAIL
2019-03-03 14:09:33 +01:00
"sync/atomic"
"syscall"
"testing"
"time"
"github.com/rfjakob/gocryptfs/v2/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)
}
tests: fix data race in TestDirIVRace Ironically, the test for DirIV races had a data race itself by writing to a bool without taking a lock. Found by running "./test.bash -race": WARNING: DATA RACE Write at 0x00c00001dea5 by goroutine 22: github.com/rfjakob/gocryptfs/tests/defaults.TestDirIVRace.func1() /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/defaults/diriv_test.go:39 +0x38 github.com/rfjakob/gocryptfs/tests/defaults.TestDirIVRace() /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/defaults/diriv_test.go:73 +0x65c testing.tRunner() /usr/local/go/src/testing/testing.go:865 +0x163 Previous read at 0x00c00001dea5 by goroutine 23: github.com/rfjakob/gocryptfs/tests/defaults.TestDirIVRace.func2() /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/defaults/diriv_test.go:51 +0x8b Goroutine 22 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:916 +0x699 testing.runTests.func1() /usr/local/go/src/testing/testing.go:1157 +0xa8 testing.tRunner() /usr/local/go/src/testing/testing.go:865 +0x163 testing.runTests() /usr/local/go/src/testing/testing.go:1155 +0x523 testing.(*M).Run() /usr/local/go/src/testing/testing.go:1072 +0x2eb github.com/rfjakob/gocryptfs/tests/defaults.TestMain() /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/defaults/main_test.go:21 +0xe1 main.main() _testmain.go:76 +0x222 Goroutine 23 (running) created at: github.com/rfjakob/gocryptfs/tests/defaults.TestDirIVRace() /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/defaults/diriv_test.go:43 +0x48d testing.tRunner() /usr/local/go/src/testing/testing.go:865 +0x163 ================== --- FAIL: TestDirIVRace (0.00s) testing.go:809: race detected during execution of test FAIL
2019-03-03 14:09:33 +01:00
var stop int32
defer func() { atomic.StoreInt32(&stop, 1) }()
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()
}
tests: fix data race in TestDirIVRace Ironically, the test for DirIV races had a data race itself by writing to a bool without taking a lock. Found by running "./test.bash -race": WARNING: DATA RACE Write at 0x00c00001dea5 by goroutine 22: github.com/rfjakob/gocryptfs/tests/defaults.TestDirIVRace.func1() /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/defaults/diriv_test.go:39 +0x38 github.com/rfjakob/gocryptfs/tests/defaults.TestDirIVRace() /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/defaults/diriv_test.go:73 +0x65c testing.tRunner() /usr/local/go/src/testing/testing.go:865 +0x163 Previous read at 0x00c00001dea5 by goroutine 23: github.com/rfjakob/gocryptfs/tests/defaults.TestDirIVRace.func2() /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/defaults/diriv_test.go:51 +0x8b Goroutine 22 (running) created at: testing.(*T).Run() /usr/local/go/src/testing/testing.go:916 +0x699 testing.runTests.func1() /usr/local/go/src/testing/testing.go:1157 +0xa8 testing.tRunner() /usr/local/go/src/testing/testing.go:865 +0x163 testing.runTests() /usr/local/go/src/testing/testing.go:1155 +0x523 testing.(*M).Run() /usr/local/go/src/testing/testing.go:1072 +0x2eb github.com/rfjakob/gocryptfs/tests/defaults.TestMain() /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/defaults/main_test.go:21 +0xe1 main.main() _testmain.go:76 +0x222 Goroutine 23 (running) created at: github.com/rfjakob/gocryptfs/tests/defaults.TestDirIVRace() /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/defaults/diriv_test.go:43 +0x48d testing.tRunner() /usr/local/go/src/testing/testing.go:865 +0x163 ================== --- FAIL: TestDirIVRace (0.00s) testing.go:809: race detected during execution of test FAIL
2019-03-03 14:09:33 +01:00
if atomic.LoadInt32(&stop) != 0 {
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)
}
}