Skip to content

Commit

Permalink
Add helper API ensureFileChecksum() to zip_archiver_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
64kramsystem committed Jul 12, 2019
1 parent 6a2a1fd commit 4ff3294
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions archive/zip_archiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package archive
import (
"archive/zip"
"bytes"
"crypto/md5"
"encoding/hex"
"io"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 4ff3294

Please sign in to comment.