diff options
Diffstat (limited to 'internal/feed/meta_test.go')
-rw-r--r-- | internal/feed/meta_test.go | 149 |
1 files changed, 0 insertions, 149 deletions
diff --git a/internal/feed/meta_test.go b/internal/feed/meta_test.go deleted file mode 100644 index 3ea5acb..0000000 --- a/internal/feed/meta_test.go +++ /dev/null @@ -1,149 +0,0 @@ -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 -zipSize:3753799 -gzSize:3753663 -sha256:B86258D5D9861507A1894A7B92011764803D7267787B1487539E240EA2405440 -` - -// Test meta parser -func TestNewMeta(t *testing.T) { - passTests := []string { - `lastModifiedDate:2022-01-29T03:01:16-05:00 -size:73202582 -zipSize:3753799 -gzSize:3753663 -sha256:B86258D5D9861507A1894A7B92011764803D7267787B1487539E240EA2405440 -`, - } - - 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) - } - }) - } - - // build 65k token to make scanner fail - longVal := make([]byte, 65536) - for i := 0; i < cap(longVal); i++ { - longVal[i] = 'a' - } - - failTests := []struct { - val string - exp string - } { - { "asdf", "bad meta line: \"asdf\"" }, - { "lastModifiedDate:asdf", "parsing time \"asdf\" as \"2006-01-02T15:04:05Z07:00\": cannot parse \"asdf\" as \"2006\"" }, - { "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'", - }, - { string(longVal), "bufio.Scanner: token too long" }, - { "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(passTests[0]) - - // decode meta, check for error - meta, err := NewMeta(buf) - if err != nil { - t.Error(err) - } - - // create destination buffer - var dst bytes.Buffer - - // create json encoder - e := json.NewEncoder(&dst) - if err := e.Encode(meta); err != nil { - t.Error(err) - } - }) -} |