Skip to content

Commit

Permalink
checksum from file: error managment + tests + fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
azr committed Jan 8, 2019
1 parent a67abad commit ee889a1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
16 changes: 11 additions & 5 deletions checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ func checksumHashAndValue(u *url.URL) (checksumHash hash.Hash, checksumValue []b
return nil, nil, fmt.Errorf(
"unsupported checksum type: %s", checksumType)
}
file := v[idx+1:]

checkSums, err := checksumsFromFile(v[idx+1:], u)
checkSums, err := checksumsFromFile(file, u)
if err != nil {
return nil, nil, err
}
Expand All @@ -60,11 +61,12 @@ func checksumHashAndValue(u *url.URL) (checksumHash hash.Hash, checksumValue []b
if fn, found := checksummers[checksumType]; found {
checksumHash = fn()
checksumValue, err = hex.DecodeString(checksumValueString)
return
return checksumHash, checksumValue, err
}
}

return
return nil, nil, fmt.Errorf(
"Could not find a supported checksum in %s: %s", file, checksumType)
}

func checksumsFromFile(checksumFile string, src *url.URL) (checkSums map[string]string, err error) {
Expand Down Expand Up @@ -114,11 +116,15 @@ func checksumsFromFile(checksumFile string, src *url.URL) (checkSums map[string]
// filename matches src ?
if filename == option {
res[checksumType] = checksumValue
break
}
}

}
return
if len(res) == 0 {
err = fmt.Errorf("Could not find a checksum for %s in %s", filename, checksumFile)
}
return res, err
}

// parseChecksumLine takes a line from a checksum file and returns
Expand Down Expand Up @@ -150,7 +156,7 @@ func parseChecksumLine(checksumFilename, line string) (checksumType, checksumVal

for ctype := range checksummers {
// guess checksum type from filename
if strings.ContainsAny(checksumFilename, ctype) {
if strings.Contains(strings.ToLower(checksumFilename), ctype) {
checksumType = ctype
}
}
Expand Down
2 changes: 1 addition & 1 deletion checksum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func Test_parseChecksumLine(t *testing.T) {
}{
{"gnu SHA256SUMS",
args{
"http://old-releases.ubuntu.com/releases/14.04.1/SHA256SUMS",
"http://old-releases.ubuntu.com/releases/14.04.1/SHA512SUMS",
"d9a217e80fb6dc2576d5ccca4c44376c25e6016de47a48e07345678d660fac51 *ubuntu-14.04-desktop-amd64+mac.iso",
},
"sha512",
Expand Down
16 changes: 11 additions & 5 deletions source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,30 @@ import (
"strings"
)

// SourceDirSubdir takes a source and returns a tuple of the URL without
// the subdir and the URL with the subdir.
func SourceDirSubdir(src string) (string, string) {
// SourceDirSubdir takes a source and returns the URL without
// the subdir and the subdir.
func SourceDirSubdir(src string) (url string, subdir string) {
// Calcaulate an offset to avoid accidentally marking the scheme
// as the dir.
var offset int
if idx := strings.Index(src, "://"); idx > -1 {
offset = idx + 3
}

// URL might contains another url in query parameters
stop := len(src)
if idx := strings.Index(src, "?"); idx > -1 {
stop = idx
}

// First see if we even have an explicit subdir
idx := strings.Index(src[offset:], "//")
idx := strings.Index(src[offset:stop], "//")
if idx == -1 {
return src, ""
}

idx += offset
subdir := src[idx+2:]
subdir = src[idx+2:]
src = src[:idx]

// Next, check if we have query parameters and push them onto the
Expand Down
8 changes: 8 additions & 0 deletions source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ func TestSourceDirSubdir(t *testing.T) {
"https://hashicorp.com/path//*?archive=foo",
"https://hashicorp.com/path?archive=foo", "*",
},
{
"https://hashicorp.com/path?checksum=file:http://url.com/....iso.sha256",
"https://hashicorp.com/path?checksum=file:http://url.com/....iso.sha256", "",
},
{
"https://hashicorp.com/path//*?checksum=file:http://url.com/....iso.sha256",
"https://hashicorp.com/path?checksum=file:http://url.com/....iso.sha256", "*",
},
{
"file://foo//bar",
"file://foo", "bar",
Expand Down

0 comments on commit ee889a1

Please sign in to comment.