-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
190426a
commit cd69bd3
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
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,27 @@ | ||
package cargo | ||
|
||
import "strings" | ||
|
||
// AreChecksumsEqual returns true only when the given checksums match, including algorithm. | ||
// The algorithm is given the same way that "cargo.ConfigMetadataDependency".Checksum expects, | ||
// as "algorithm:checksum". Only exact equality is allowed, although algorithm is only checked | ||
// if present in both of the given checksums. | ||
func AreChecksumsEqual(c1, c2 string) bool { | ||
var algorithm1, algorithm2 string | ||
split1 := strings.Split(c1, ":") | ||
if len(split1) == 2 { | ||
algorithm1 = split1[0] | ||
c1 = split1[1] | ||
} else if len(split1) > 2 { | ||
return false | ||
} | ||
split2 := strings.Split(c2, ":") | ||
if len(split2) == 2 { | ||
algorithm2 = split2[0] | ||
c2 = split2[1] | ||
} | ||
areAlgorithmsEqual := func(a1, a2 string) bool { | ||
return a1 == "" || a2 == "" || a1 == a2 | ||
} | ||
return areAlgorithmsEqual(algorithm1, algorithm2) && c1 == c2 | ||
} |
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,46 @@ | ||
package cargo_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
"github.com/paketo-buildpacks/packit/v2/cargo" | ||
"github.com/sclevine/spec" | ||
) | ||
|
||
func testAreChecksumsEqual(t *testing.T, context spec.G, it spec.S) { | ||
var ( | ||
Expect = NewWithT(t).Expect | ||
) | ||
|
||
context("AreChecksumsEqual", func() { | ||
type testCaseType struct { | ||
c1 string | ||
c2 string | ||
expected bool | ||
} | ||
|
||
for _, testCase := range []testCaseType{ | ||
{"", "", true}, | ||
{"c", "c", true}, | ||
{"c", "c", true}, | ||
{"sha256:c", "c", true}, | ||
{"c", "sha256:c", true}, | ||
{"md5:c", "md5:c", true}, | ||
{":", ":", true}, | ||
{":c", ":c", true}, | ||
{"", "c", false}, | ||
{"c", "", false}, | ||
{"c", "z", false}, | ||
{"md5:c", "sha256:c", false}, | ||
{"md5:c:d", "md5:c:d", false}, | ||
{"md5:c", "md5:c:d", false}, | ||
{":", "::", false}, | ||
{":", ":::", false}, | ||
} { | ||
it("will check result", func() { | ||
Expect(cargo.AreChecksumsEqual(testCase.c1, testCase.c2)).To(Equal(testCase.expected)) | ||
}) | ||
} | ||
}) | ||
} |
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