Skip to content

Commit

Permalink
add Test_checksumHashAndValue + test with local files and remove Test…
Browse files Browse the repository at this point in the history
…_parseChecksumLine so we test everything from one entrypoint
  • Loading branch information
azr committed Jan 8, 2019
1 parent dd14d29 commit 46d4103
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 53 deletions.
22 changes: 19 additions & 3 deletions checksum.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"encoding/hex"
"fmt"
"hash"
"io"
"io/ioutil"
"net/url"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -107,7 +109,11 @@ func checksumsFromFile(checksumFile string, src *url.URL) (checkSums map[string]
if err != nil {
return nil, err
}
defer f.Close()
tempfile := f.Name()
f.Close()
defer func() {
os.Remove(tempfile)
}()

if err = GetFile(f.Name(), checksumFile); err != nil {
return nil, fmt.Errorf(
Expand All @@ -122,17 +128,27 @@ func checksumsFromFile(checksumFile string, src *url.URL) (checkSums map[string]
options := []string{
filename, // ubuntu-14.04.1-server-amd64.iso
"*" + filename, // *ubuntu-14.04.1-server-amd64.iso Standard checksum
"?" + filename, // ?ubuntu-14.04.1-server-amd64.iso shasum -p
relpath, // dir/ubuntu-14.04.1-server-amd64.iso
"./" + relpath, // ./dir/ubuntu-14.04.1-server-amd64.iso
absPath, // fullpath; set if local
}

f, err = os.Open(tempfile)
if err != nil {
return nil, fmt.Errorf(
"Error opening downloaded file: %s", err)
}
defer f.Close()
rd := bufio.NewReader(f)
res := map[string]string{}
for {

line, err := rd.ReadString('\n')
if err != nil && line == "" {
if err != nil {
if err != io.EOF {
return nil, fmt.Errorf(
"Error reading checksum file: %s", err)
}
break
}
checksumType, checksumValue, filename, ok := parseChecksumLine(checksumFile, line)
Expand Down
97 changes: 47 additions & 50 deletions checksum_test.go
Original file line number Diff line number Diff line change
@@ -1,75 +1,72 @@
package getter

import "testing"
import (
"encoding/hex"
"hash"
"net/url"
"reflect"
"testing"
)

func u(t *testing.T, in string) *url.URL {
u, err := url.Parse(in)
if err != nil {
t.Fatalf("cannot parse %s: %v", in, err)
}
return u
}

func Test_checksumHashAndValue(t *testing.T) {

checksums := testModule("checksum-file")

func Test_parseChecksumLine(t *testing.T) {
type args struct {
checksumFile string
line string
u *url.URL
}
tests := []struct {
name string
args args
wantChecksumType string
wantChecksumHash hash.Hash
wantChecksumValue string
wantFilename string
wantOk bool
wantErr bool
}{
{"gnu SHA256SUMS",
args{
"http://old-releases.ubuntu.com/releases/14.04.1/SHA512SUMS",
"d9a217e80fb6dc2576d5ccca4c44376c25e6016de47a48e07345678d660fac51 *ubuntu-14.04-desktop-amd64+mac.iso",
},
"sha512",
"d9a217e80fb6dc2576d5ccca4c44376c25e6016de47a48e07345678d660fac51",
"*ubuntu-14.04-desktop-amd64+mac.iso",
true,
{"shasum -a 256 -p",
args{u(t, checksums+"/content.txt?checksum=file:"+checksums+"/sha256-p.sum")},
checksummers["sha256"](),
"47afcdfff05a6e5d9db5f6c6df2140f04a6e7422d7ad7f6a7006a4f5a78570e4",
false,
},
{"bad gnu checksum",
args{
"ftp://ftp.freebsd.org/pub/FreeBSD/snapshots/VM-IMAGES/10.4-STABLE/i386/Latest/CHECKSUM.SHAz",
"d9a217e80fb6dc2576d5ccca4c44376c25e6016de47a48e07345678d660fac51 ubuntu-14.04-desktop-amd64+mac.iso",
},
{"not properly named shasum -a 256 -p",
args{u(t, checksums+"/content.txt?checksum=file:"+checksums+"/sha2FiveSixError.sum")},
nil,
"",
"d9a217e80fb6dc2576d5ccca4c44376c25e6016de47a48e07345678d660fac51",
"ubuntu-14.04-desktop-amd64+mac.iso",
true,
},
{"bsd CHECKSUM.SHA256",
args{
"ftp://ftp.freebsd.org/pub/FreeBSD/snapshots/VM-IMAGES/10.4-STABLE/i386/Latest/CHECKSUM.SHA256",
"SHA256 (FreeBSD-10.4-STABLE-i386-20181012-r339297.qcow2.xz) = cedf5203525ef1c7048631d7d26ca54b81f224fccf6b9185eab2cf4b894e8651",
},
"sha256",
"cedf5203525ef1c7048631d7d26ca54b81f224fccf6b9185eab2cf4b894e8651",
"FreeBSD-10.4-STABLE-i386-20181012-r339297.qcow2.xz",
true,
},
{"potato",
args{
"blip",
"potato chips 3 4",
},
"",
"",
"",
{"md5",
args{u(t, checksums+"/content.txt?checksum=file:"+checksums+"/sha256-p.sum")},
checksummers["sha256"](),
"47afcdfff05a6e5d9db5f6c6df2140f04a6e7422d7ad7f6a7006a4f5a78570e4",
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotChecksumType, gotChecksumValue, gotFilename, gotOk := parseChecksumLine(tt.args.checksumFile, tt.args.line)
if gotChecksumType != tt.wantChecksumType {
t.Errorf("parseChecksumLine() gotChecksumType = %v, want %v", gotChecksumType, tt.wantChecksumType)
gotChecksumHash, gotChecksumValue, err := checksumHashAndValue(tt.args.u)
if (err != nil) != tt.wantErr {
t.Errorf("checksumHashAndValue() error = %v , wantErr %v", err, tt.wantErr)
return
}
if gotChecksumValue != tt.wantChecksumValue {
t.Errorf("parseChecksumLine() gotChecksumValue = %v, want %v", gotChecksumValue, tt.wantChecksumValue)
var wantChecksumValue []byte
if tt.wantChecksumValue != "" {
if wantChecksumValue, err = hex.DecodeString(tt.wantChecksumValue); err != nil {
panic(err)
}
}
if gotFilename != tt.wantFilename {
t.Errorf("parseChecksumLine() gotFilename = %v, want %v", gotFilename, tt.wantFilename)
if !reflect.DeepEqual(gotChecksumHash, tt.wantChecksumHash) {
t.Errorf("checksumHashAndValue() gotChecksumHash = %v, want %v", gotChecksumHash, tt.wantChecksumHash)
}
if gotOk != tt.wantOk {
t.Errorf("parseChecksumLine() gotOk = %v, want %v", gotOk, tt.wantOk)
if !reflect.DeepEqual(gotChecksumValue, wantChecksumValue) {
t.Errorf("checksumHashAndValue() gotChecksumValue = %v, want %v", gotChecksumValue, tt.wantChecksumValue)
}
})
}
Expand Down
1 change: 1 addition & 0 deletions test-fixtures/checksum-file/content.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am a file with some content
1 change: 1 addition & 0 deletions test-fixtures/checksum-file/md5-bsd.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MD5 (content.txt) = 074729f0ccb41a391fb646c38f86ea54
1 change: 1 addition & 0 deletions test-fixtures/checksum-file/sha256-p.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
47afcdfff05a6e5d9db5f6c6df2140f04a6e7422d7ad7f6a7006a4f5a78570e4 ?content.txt
1 change: 1 addition & 0 deletions test-fixtures/checksum-file/sha2FiveSixError.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
47afcdfff05a6e5d9db5f6c6df2140f04a6e7422d7ad7f6a7006a4f5a78570e4 ?content.txt

0 comments on commit 46d4103

Please sign in to comment.