-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactors out common Checksum concept (#399)
- Loading branch information
Showing
10 changed files
with
169 additions
and
121 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package cargo | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// Checksum represents a checksum algorithm and hash pair formatted as | ||
// algorithm:hash. | ||
type Checksum string | ||
|
||
// Algorithm returns the algorithm portion of the checksum string. If that | ||
// portion is missing, it defaults to "sha256". | ||
func (c Checksum) Algorithm() string { | ||
algorithm, _, found := strings.Cut(string(c), ":") | ||
if !found { | ||
return "sha256" | ||
} | ||
|
||
return algorithm | ||
} | ||
|
||
// Hash returns the hexidecimal encoded hash portion of the checksum string. | ||
func (c Checksum) Hash() string { | ||
_, hash, found := strings.Cut(string(c), ":") | ||
if !found { | ||
hash = string(c) | ||
} | ||
|
||
return hash | ||
} | ||
|
||
// EqualTo returns true only when the given checksum algorithms and hashes | ||
// match. | ||
func (c Checksum) Match(o Checksum) bool { | ||
return strings.EqualFold(c.Algorithm(), o.Algorithm()) && c.Hash() == o.Hash() | ||
} | ||
|
||
// EqualTo returns true only when the given checksum formatted string | ||
// algorithms and hashes match. | ||
func (c Checksum) MatchString(o string) bool { | ||
return c.Match(Checksum(o)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package cargo_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/paketo-buildpacks/packit/v2/cargo" | ||
"github.com/sclevine/spec" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func testChecksum(t *testing.T, context spec.G, it spec.S) { | ||
Expect := NewWithT(t).Expect | ||
|
||
context("Matching", func() { | ||
type testCaseType struct { | ||
c1 string | ||
c2 string | ||
result bool | ||
} | ||
|
||
for _, tc := range []testCaseType{ | ||
{"", "", true}, | ||
{"c", "c", true}, | ||
{"sha256:c", "c", true}, | ||
{"c", "sha256:c", true}, | ||
{"md5:c", "md5:c", true}, | ||
{"md5:c", ":c", false}, | ||
{":", ":", true}, | ||
{":c", ":c", true}, | ||
{"", "c", false}, | ||
{"c", "", false}, | ||
{"c", "z", false}, | ||
{"md5:c", "sha256:c", false}, | ||
{"md5:c", "md5:d", false}, | ||
{"md5:c:d", "md5:c:d", true}, | ||
{"md5:c", "md5:c:d", false}, | ||
{":", "::", false}, | ||
{":", ":::", false}, | ||
} { | ||
|
||
// NOTE: we need to keep a "loop-local" variable to use in the "it | ||
// function closure" below, otherwise the value of tc will simply be the | ||
// last element in the slice every time the test is evaluated. | ||
ca, cb, sb, result := cargo.Checksum(tc.c1), cargo.Checksum(tc.c2), tc.c2, tc.result | ||
|
||
it(fmt.Sprintf("will check result %q == %q", ca, cb), func() { | ||
Expect(ca.Match(cb)).To(Equal(result)) | ||
Expect(ca.MatchString(sb)).To(Equal(result)) | ||
}) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters