diff options
Diffstat (limited to 'internal')
-rw-r--r-- | internal/feed/meta.go | 4 | ||||
-rw-r--r-- | internal/feed/meta_test.go | 112 |
2 files changed, 104 insertions, 12 deletions
diff --git a/internal/feed/meta.go b/internal/feed/meta.go index da2849d..cb7d7a5 100644 --- a/internal/feed/meta.go +++ b/internal/feed/meta.go @@ -74,7 +74,7 @@ func NewMeta(r io.Reader) (*Meta, error) { case "sha256": // check hash length if len(pair[1]) != 64 { - return nil, fmt.Errorf("invalid sha256 hash length: \"%d\"", len(pair[1])) + return nil, fmt.Errorf("invalid sha256 hash length: %d", len(pair[1])) } // decode hex, check for error @@ -86,7 +86,7 @@ func NewMeta(r io.Reader) (*Meta, error) { // save to buffer, check for error len := copy(m.Sha256[:], buf[0:32]) if len != 32 { - return nil, fmt.Errorf("invalid sha256 hash length: %d", len) + return nil, fmt.Errorf("invalid sha256 hash copy length: %d", len) } default: // return error diff --git a/internal/feed/meta_test.go b/internal/feed/meta_test.go index 04ad22b..640926c 100644 --- a/internal/feed/meta_test.go +++ b/internal/feed/meta_test.go @@ -3,9 +3,56 @@ package feed import ( "bytes" "encoding/json" + "fmt" "testing" ) +func TestParseMetaSize(t *testing.T) { + passTests := []struct { + val string + exp uint64 + } { + { "0", 0 }, + { "1024", 1024 }, + { "18446744073709551615", 18446744073709551615 }, + } + + for _, test := range(passTests) { + t.Run(test.val, func(t *testing.T) { + got, err := parseMetaSize("foo", test.val) + if err != nil { + t.Error(err) + return + } + + if got != test.exp { + t.Errorf("got %d, exp %d", got, test.exp) + return + } + }) + } + + failTests := []struct { + val string + exp string + } { + { "-1", "invalid foo: \"-1\"" }, + { "a", "invalid foo: \"a\"" }, + { "18446744073709551616", "invalid foo: \"18446744073709551616\"" }, + } + + for _, test := range(failTests) { + t.Run(test.val, func(t *testing.T) { + got, err := parseMetaSize("foo", test.val) + if err == nil { + t.Errorf("got %d, exp error", got) + } else if err.Error() != test.exp { + t.Errorf("got \"%s\", exp \"%s\"", err.Error(), test.exp) + } + }) + } +} + // test data const testMeta = `lastModifiedDate:2022-01-29T03:01:16-05:00 size:73202582 @@ -16,20 +63,65 @@ sha256:B86258D5D9861507A1894A7B92011764803D7267787B1487539E240EA2405440 // Test meta parser func TestNewMeta(t *testing.T) { - t.Run("NewMeta", func(t *testing.T) { - // create buffer - buf := bytes.NewBufferString(testMeta) + passTests := []string { + `lastModifiedDate:2022-01-29T03:01:16-05:00 +size:73202582 +zipSize:3753799 +gzSize:3753663 +sha256:B86258D5D9861507A1894A7B92011764803D7267787B1487539E240EA2405440 +`, + } - // decode meta, check for error - _, err := NewMeta(buf) - if err != nil { - t.Error(err) - } - }) + for i, val := range(passTests) { + // build test name + name := fmt.Sprintf("passTests[%d]", i) + + t.Run(name, func(t *testing.T) { + // create buffer + buf := bytes.NewBufferString(val) + + // decode meta, check for error + _, err := NewMeta(buf) + if err != nil { + t.Error(err) + } + }) + } + + failTests := []struct { + val string + exp string + } { + { "asdf", "bad meta line: \"asdf\"" }, + { "size:a", "invalid size: \"a\"" }, + { "zipSize:a", "invalid zipSize: \"a\"" }, + { "gzSize:a", "invalid gzSize: \"a\"" }, + { "sha256:a", "invalid sha256 hash length: 1" }, + { + val: "sha256:0z00000000000000000000000000000000000000000000000000000000000000", + exp: "invalid sha256 hash: encoding/hex: invalid byte: U+007A 'z'", + }, + { "foo:bar", "unknown key: \"foo\"" }, + } + + for _, test := range(failTests) { + t.Run(test.val, func(t *testing.T) { + // create buffer + buf := bytes.NewBufferString(test.val) + + // decode meta, check for error + got, err := NewMeta(buf) + if err == nil { + t.Errorf("got %v, exp error", got) + } else if err.Error() != test.exp { + t.Errorf("got \"%s\", exp \"%s\"", err.Error(), test.exp) + } + }) + } t.Run("JsonEncode", func(t *testing.T) { // create buffer - buf := bytes.NewBufferString(testMeta) + buf := bytes.NewBufferString(passTests[0]) // decode meta, check for error meta, err := NewMeta(buf) |