Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Header error tests #76

Merged
merged 3 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
car/car
main
coverage.txt
10 changes: 5 additions & 5 deletions car.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func ReadHeader(br *bufio.Reader) (*CarHeader, error) {

var ch CarHeader
if err := cbor.DecodeInto(hb, &ch); err != nil {
return nil, err
return nil, fmt.Errorf("invalid header: %v", err)
}

return &ch, nil
Expand Down Expand Up @@ -130,14 +130,14 @@ func NewCarReader(r io.Reader) (*CarReader, error) {
return nil, err
}

if len(ch.Roots) == 0 {
return nil, fmt.Errorf("empty car")
}

if ch.Version != 1 {
return nil, fmt.Errorf("invalid car version: %d", ch.Version)
}

if len(ch.Roots) == 0 {
return nil, fmt.Errorf("empty car, no roots")
}

return &CarReader{
br: br,
Header: ch,
Expand Down
89 changes: 89 additions & 0 deletions car_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/hex"
"io"
"strings"
"testing"

cid "github.com/ipfs/go-cid"
Expand Down Expand Up @@ -234,3 +235,91 @@ func TestEOFHandling(t *testing.T) {
}
})
}

func TestBadHeaders(t *testing.T) {
testCases := []struct {
name string
hex string
errStr string // either the whole error string
errPfx string // or just the prefix
}{
{
"{version:2}",
"0aa16776657273696f6e02",
"invalid car version: 2",
"",
},
{
// an unfortunate error because we don't use a pointer
"{roots:[baeaaaa3bmjrq]}",
"13a165726f6f747381d82a480001000003616263",
"invalid car version: 0",
"",
}, {
"{version:\"1\",roots:[baeaaaa3bmjrq]}",
"1da265726f6f747381d82a4800010000036162636776657273696f6e6131",
"", "invalid header: ",
}, {
"{version:1}",
"0aa16776657273696f6e01",
"empty car, no roots",
"",
}, {
"{version:1,roots:{cid:baeaaaa3bmjrq}}",
"20a265726f6f7473a163636964d82a4800010000036162636776657273696f6e01",
"",
"invalid header: ",
}, {
"{version:1,roots:[baeaaaa3bmjrq],blip:true}",
"22a364626c6970f565726f6f747381d82a4800010000036162636776657273696f6e01",
"",
"invalid header: ",
}, {
"[1,[]]",
"03820180",
"",
"invalid header: ",
}, {
// this is an unfortunate error, it'd be nice to catch it better but it's
// very unlikely we'd ever see this in practice
"null",
"01f6",
"",
"invalid car version: 0",
},
}

makeCar := func(t *testing.T, byts string) error {
fixture, err := hex.DecodeString(byts)
if err != nil {
t.Fatal(err)
}
_, err = NewCarReader(bytes.NewReader(fixture))
return err
}

t.Run("Sanity check {version:1,roots:[baeaaaa3bmjrq]}", func(t *testing.T) {
err := makeCar(t, "1ca265726f6f747381d82a4800010000036162636776657273696f6e01")
if err != nil {
t.Fatal(err)
}
})

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := makeCar(t, tc.hex)
rvagg marked this conversation as resolved.
Show resolved Hide resolved
if err == nil {
t.Fatal("expected error from bad header, didn't get one")
}
if tc.errStr != "" {
if err.Error() != tc.errStr {
t.Fatalf("bad error: %v", err)
}
} else {
if !strings.HasPrefix(err.Error(), tc.errPfx) {
t.Fatalf("bad error: %v", err)
}
}
})
}
}
rvagg marked this conversation as resolved.
Show resolved Hide resolved