tests: randomize data in testWriteN
Just writing zeros carries the risk of not detecting wrongly created file holes. Write random data instead.
This commit is contained in:
parent
97743858ce
commit
6eca07e36e
@ -16,6 +16,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -96,7 +97,7 @@ func TestMain(m *testing.M) {
|
|||||||
os.Exit(0)
|
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 {
|
func testWriteN(t *testing.T, fn string, n int) string {
|
||||||
file, err := os.Create(test_helpers.DefaultPlainDir + "/" + fn)
|
file, err := os.Create(test_helpers.DefaultPlainDir + "/" + fn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -104,6 +105,10 @@ func testWriteN(t *testing.T, fn string, n int) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
d := make([]byte, n)
|
d := make([]byte, n)
|
||||||
|
for i := range d {
|
||||||
|
// Fill with pattern
|
||||||
|
d[i] = byte(rand.Int())
|
||||||
|
}
|
||||||
_, err = file.Write(d)
|
_, err = file.Write(d)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@ -143,9 +148,9 @@ func TestWrite100x100(t *testing.T) {
|
|||||||
// Read and check 100 times to catch race conditions
|
// Read and check 100 times to catch race conditions
|
||||||
var i int
|
var i int
|
||||||
for i = 0; i < 100; i++ {
|
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 {
|
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()
|
t.FailNow()
|
||||||
} else {
|
} else {
|
||||||
//fmt.Print(".")
|
//fmt.Print(".")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user