From 3f8b22d6ac2f70ad3dc8a7c936b2ebbeef4d36de Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Sat, 31 Oct 2015 23:17:19 +0100 Subject: [PATCH] tests: additionally verify the file size by reading the whole file --- main_test.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/main_test.go b/main_test.go index 2d4f578..0927a58 100644 --- a/main_test.go +++ b/main_test.go @@ -41,6 +41,7 @@ func unmount() error { return fu.Run() } +// Return md5 string for file "filename" func md5fn(filename string) string { buf, err := ioutil.ReadFile(filename) if err != nil { @@ -50,12 +51,22 @@ func md5fn(filename string) string { return md5hex(buf) } +// Return md5 string for "buf" func md5hex(buf []byte) string { rawHash := md5.Sum(buf) hash := hex.EncodeToString(rawHash[:]) return hash } +// Read the whole file and return number of bytes read +func readSize(fn string) int { + buf, err := ioutil.ReadFile(fn) + if err != nil { + fmt.Printf("ReadFile: %v\n", err) + } + return len(buf) +} + // This is the entry point for the tests func TestMain(m *testing.M) { @@ -105,10 +116,15 @@ func testWriteN(t *testing.T, fn string, n int) string { t.Errorf("Stat on file %s failed: %v", fn, err) } else { if fi.Size() != int64(n) { - t.Errorf("Wrong file fize, got=%d want=%d", fi.Size(), n) + t.Errorf("Wrong stat file size, got=%d want=%d", fi.Size(), n) } } + rs := readSize(plainDir + fn) + if rs != n { + t.Errorf("Wrong read file fize, got=%d want=%d", rs, n) + } + bin := md5.Sum(d) hashWant := hex.EncodeToString(bin[:]) @@ -116,8 +132,7 @@ func testWriteN(t *testing.T, fn string, n int) string { hashActual := md5fn(plainDir + fn) if hashActual != hashWant { - fmt.Printf("Content corruption in file %s: hashWant=%s hashActual=%s\n", fn, hashWant, hashActual) - t.Fail() + t.Errorf("Wrong content, hashWant=%s hashActual=%s\n", hashWant, hashActual) } return hashActual