From 4ff32942cfa54ed9d917c0d9905a93803dc3f225 Mon Sep 17 00:00:00 2001 From: Saverio Miroddi Date: Fri, 5 Jul 2019 12:22:46 +0200 Subject: [PATCH] Add helper API ensureFileChecksum() to zip_archiver_test.go --- archive/zip_archiver_test.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/archive/zip_archiver_test.go b/archive/zip_archiver_test.go index 45b3e0f3..00a5dbf3 100644 --- a/archive/zip_archiver_test.go +++ b/archive/zip_archiver_test.go @@ -3,6 +3,9 @@ package archive import ( "archive/zip" "bytes" + "crypto/md5" + "encoding/hex" + "io" "io/ioutil" "os" "path/filepath" @@ -168,3 +171,27 @@ func ensureContent(t *testing.T, wants map[string][]byte, got *zip.File) { t.Errorf("mismatched content\ngot\n%s\nwant\n%s", gotContent, wantContent) } } + +// There are different approaches to testing the functionality. Testing the checksum is a simple yet +// functional one, since, as long as we're assured that a normalized file has a fixed content (which +// a checksum guarantees), we don't need to know/test the normalization inner details. +func ensureFileChecksum(t *testing.T, zipfilepath string, expectedChecksum string) { + file, err := os.Open(zipfilepath) + if err != nil { + t.Errorf("could not open file: %s", err) + } + + defer file.Close() + + hashWriter := md5.New() + + if _, err := io.Copy(hashWriter, file); err != nil { + t.Errorf("could not open file: %s", err) + } + + fileHash := hex.EncodeToString(hashWriter.Sum(nil)[:16]) + + if expectedChecksum != fileHash { + t.Errorf("the file actual checksum (%s) didn't match the expected one (%s)", fileHash, expectedChecksum) + } +}