From 6eca07e36e2e58ff7495305ed7cb0fdcc36436e6 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Sat, 15 Feb 2020 21:56:08 +0100 Subject: [PATCH] tests: randomize data in testWriteN Just writing zeros carries the risk of not detecting wrongly created file holes. Write random data instead. --- tests/matrix/matrix_test.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/matrix/matrix_test.go b/tests/matrix/matrix_test.go index af4769a..2215ff6 100644 --- a/tests/matrix/matrix_test.go +++ b/tests/matrix/matrix_test.go @@ -16,6 +16,7 @@ import ( "flag" "fmt" "io/ioutil" + "math/rand" "os" "os/exec" "runtime" @@ -96,7 +97,7 @@ func TestMain(m *testing.M) { os.Exit(0) } -// Write "n" zero bytes to filename "fn", read again, compare hash +// Write `n` random bytes to filename `fn`, read again, compare hash func testWriteN(t *testing.T, fn string, n int) string { file, err := os.Create(test_helpers.DefaultPlainDir + "/" + fn) if err != nil { @@ -104,6 +105,10 @@ func testWriteN(t *testing.T, fn string, n int) string { } d := make([]byte, n) + for i := range d { + // Fill with pattern + d[i] = byte(rand.Int()) + } _, err = file.Write(d) if err != nil { t.Fatal(err) @@ -143,9 +148,9 @@ func TestWrite100x100(t *testing.T) { // Read and check 100 times to catch race conditions var i int for i = 0; i < 100; i++ { - hashActual := test_helpers.Md5fn(test_helpers.DefaultPlainDir + "/100") + hashActual := test_helpers.Md5fn(test_helpers.DefaultPlainDir + "/100x100") if hashActual != hashWant { - fmt.Printf("Read corruption in loop #%d\n", i) + fmt.Printf("Read corruption in loop #%d: have=%s want=%s\n", i, hashActual, hashWant) t.FailNow() } else { //fmt.Print(".")