From 5b5e57151574dcfb8e2fbcbd7a67a514584adfdc Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Mon, 18 May 2020 10:16:38 +0200 Subject: [PATCH] Make checksum func public (#252) * make FileChecksum.Checksum public * add FileChecksum.String func * FileChecksum.Checksum: change var name `file` to `filePath` to avoid confusion --- checksum.go | 18 +++++++++++++----- checksum_test.go | 36 ++++++++++++++++++++++++++++++++++++ client.go | 4 ++-- 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/checksum.go b/checksum.go index e9c681580..e427e89d7 100644 --- a/checksum.go +++ b/checksum.go @@ -27,6 +27,13 @@ type FileChecksum struct { Filename string } +// String returns the hash type and the hash separated by a colon, for example: +// "md5:090992ba9fd140077b0661cb75f7ce13" +// "sha1:ebfb681885ddf1234c18094a45bbeafd91467911" +func (c *FileChecksum) String() string { + return c.Type + ":" + hex.EncodeToString(c.Value) +} + // A ChecksumError is returned when a checksum differs type ChecksumError struct { Hash hash.Hash @@ -48,10 +55,11 @@ func (cerr *ChecksumError) Error() string { ) } -// checksum is a simple method to compute the checksum of a source file -// and compare it to the given expected value. -func (c *FileChecksum) checksum(source string) error { - f, err := os.Open(source) +// Checksum computes the Checksum for filePath using the hashing algorithm from +// c.Hash and compares it to c.Value. If those values differ a ChecksumError +// will be returned. +func (c *FileChecksum) Checksum(filePath string) error { + f, err := os.Open(filePath) if err != nil { return fmt.Errorf("Failed to open file for checksum: %s", err) } @@ -67,7 +75,7 @@ func (c *FileChecksum) checksum(source string) error { Hash: c.Hash, Actual: actual, Expected: c.Value, - File: source, + File: filePath, } } diff --git a/checksum_test.go b/checksum_test.go index d80643a83..b61e7d93a 100644 --- a/checksum_test.go +++ b/checksum_test.go @@ -5,6 +5,7 @@ import ( "io" "os" "path/filepath" + "strconv" "testing" ) @@ -61,3 +62,38 @@ func TestClient_GetChecksum(t *testing.T) { t.Fatalf("bad: expecting filename ./netboot/mini.iso but was: %s", file.Filename) } } + +func TestFileChecksum_String(t *testing.T) { + type fields struct { + checksum string + } + wd, err := os.Getwd() + if err != nil { + t.Fatalf(err.Error()) + } + tests := []struct { + fields fields + want string + }{ + {fields{"090992ba9fd140077b0661cb75f7ce13"}, "md5:090992ba9fd140077b0661cb75f7ce13"}, + {fields{"ebfb681885ddf1234c18094a45bbeafd91467911"}, "sha1:ebfb681885ddf1234c18094a45bbeafd91467911"}, + {fields{"sha256:ed363350696a726b7932db864dda019bd2017365c9e299627830f06954643f93"}, "sha256:ed363350696a726b7932db864dda019bd2017365c9e299627830f06954643f93"}, + {fields{"file:" + filepath.Join(wd, fixtureDir, "checksum-file", "sha1.sum")}, "sha1:e2c7dc83ac8aa7f181314387f6dfb132cd117e3a"}, + } + + for i, tt := range tests { + t.Run(strconv.Itoa(i), func(t *testing.T) { + req := &Request{ + Src: "http://example.dev?checksum=" + tt.fields.checksum, + } + c, err := DefaultClient.GetChecksum(context.TODO(), req) + if err != nil { + t.Fatalf("GetChecksum: %v", err) + } + + if got := c.String(); got != tt.want { + t.Errorf("FileChecksum.String() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/client.go b/client.go index df9bf4de4..c5bba4743 100644 --- a/client.go +++ b/client.go @@ -181,7 +181,7 @@ func (c *Client) Get(ctx context.Context, req *Request) (*GetResult, error) { if req.Mode == ModeFile { getFile := true if checksum != nil { - if err := checksum.checksum(req.Dst); err == nil { + if err := checksum.Checksum(req.Dst); err == nil { // don't get the file if the checksum of dst is correct getFile = false } @@ -193,7 +193,7 @@ func (c *Client) Get(ctx context.Context, req *Request) (*GetResult, error) { } if checksum != nil { - if err := checksum.checksum(req.Dst); err != nil { + if err := checksum.Checksum(req.Dst); err != nil { return nil, err } }