Skip to content

Commit

Permalink
Improve parsing checksum file
Browse files Browse the repository at this point in the history
  • Loading branch information
nkryuchkov committed Feb 8, 2020
1 parent afc7924 commit 2a50608
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
9 changes: 8 additions & 1 deletion pkg/util/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"runtime"
"strings"
"time"
"unicode"

"github.com/SkycoinProject/skycoin/src/util/logging"

Expand Down Expand Up @@ -215,13 +216,19 @@ func isChecksumValid(filename, wantSum string) (bool, error) {
return gotSum == wantSum, nil
}

// NOTE: getChecksum does not support Unicode in checksums file.
func getChecksum(checksums, filename string) (string, error) {
idx := strings.Index(checksums, filename)
if idx == -1 {
return "", ErrNoChecksumFound
}

last := idx - 1 // space separator
// Remove space(s) separator.
last := idx
for last > 0 && unicode.IsSpace(rune(checksums[last-1])) {
last--
}

first := last - checkSumLength

if first < 0 {
Expand Down
33 changes: 30 additions & 3 deletions pkg/util/updater/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func Test_getChecksum(t *testing.T) {
wantErr error
}{
{
name: "No Error",
name: "No Error 1",
checksums: `
2f505da2a905889aa978597814f91dbe32ee46fffe44657bd7af56a942d92470 skywire-visor-0.1.0-linux-amd64
2f505da2a905889aa978597814f91dbe32ee46fffe44657bd7af56a942d92471 skywire-visor-0.1.0-linux-386
Expand All @@ -60,12 +60,39 @@ func Test_getChecksum(t *testing.T) {
wantErr: nil,
},
{
name: "ErrMalformedChecksumFile",
checksums: `skywire-visor-0.1.0-darwin-amd64`,
name: "No Error 2",
checksums: `
2f505da2a905889aa978597814f91dbe32ee46fffe44657bd7af56a942d92470 skywire-visor-0.1.0-linux-amd64
2f505da2a905889aa978597814f91dbe32ee46fffe44657bd7af56a942d92471 skywire-visor-0.1.0-linux-386
2f505da2a905889aa978597814f91dbe32ee46fffe44657bd7af56a942d92472 skywire-visor-0.1.0-darwin-amd64
2f505da2a905889aa978597814f91dbe32ee46fffe44657bd7af56a942d92473 skywire-visor-0.1.0-windows-amd64
2f505da2a905889aa978597814f91dbe32ee46fffe44657bd7af56a942d92474 skywire-visor-0.1.0-linux-arm64
`,
filename: "skywire-visor-0.1.0-darwin-amd64",
want: "2f505da2a905889aa978597814f91dbe32ee46fffe44657bd7af56a942d92472",
wantErr: nil,
},
{
name: "ErrMalformedChecksumFile 1",
checksums: "skywire-visor-0.1.0-darwin-amd64",
filename: "skywire-visor-0.1.0-darwin-amd64",
want: "",
wantErr: ErrMalformedChecksumFile,
},
{
name: "ErrMalformedChecksumFile 2",
checksums: " skywire-visor-0.1.0-darwin-amd64",
filename: " skywire-visor-0.1.0-darwin-amd64",
want: "",
wantErr: ErrMalformedChecksumFile,
},
{
name: "ErrMalformedChecksumFile 3",
checksums: " \t skywire-visor-0.1.0-darwin-amd64",
filename: " \t skywire-visor-0.1.0-darwin-amd64",
want: "",
wantErr: ErrMalformedChecksumFile,
},
{
name: "ErrNoChecksumFound",
checksums: `2f505da2a905889aa978597814f91dbe32ee46fffe44657bd7af56a942d92470 skywire-visor-0.1.0-linux-amd64`,
Expand Down

0 comments on commit 2a50608

Please sign in to comment.