Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make checksum func public #252

Merged
merged 3 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -67,7 +75,7 @@ func (c *FileChecksum) checksum(source string) error {
Hash: c.Hash,
Actual: actual,
Expected: c.Value,
File: source,
File: filePath,
}
}

Expand Down
36 changes: 36 additions & 0 deletions checksum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"os"
"path/filepath"
"strconv"
"testing"
)

Expand Down Expand Up @@ -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)
}
})
}
}
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
}
Expand Down