Skip to content

Commit

Permalink
chore: add header error tests
Browse files Browse the repository at this point in the history
Ref: ipld/js-car#28

the primary difference with js-car is that go-car requires a non-empty roots
array whereas js-car is fine with empty roots array, hence the test fixture
differences


This commit was moved from ipld/go-car@2876c18
  • Loading branch information
rvagg committed Jun 3, 2021
1 parent 873f8f2 commit b81b803
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 5 deletions.
3 changes: 3 additions & 0 deletions ipld/car/.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 ipld/car/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
78 changes: 78 additions & 0 deletions ipld/car/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,80 @@ func TestEOFHandling(t *testing.T) {
}
})
}

func TestBadHeaders(t *testing.T) {
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)
}
})

t.Run("{version:2}", func(t *testing.T) {
err := makeCar(t, "0aa16776657273696f6e02")
if err.Error() != "invalid car version: 2" {
t.Fatalf("bad error: %v", err)
}
})

// an unfortunate error because we don't use a pointer
t.Run("{roots:[baeaaaa3bmjrq]}", func(t *testing.T) {
err := makeCar(t, "13a165726f6f747381d82a480001000003616263")
if err.Error() != "invalid car version: 0" {
t.Fatalf("bad error: %v", err)
}
})

t.Run("{version:\"1\",roots:[baeaaaa3bmjrq]}", func(t *testing.T) {
err := makeCar(t, "1da265726f6f747381d82a4800010000036162636776657273696f6e6131")
if !strings.HasPrefix(err.Error(), "invalid header: ") {
t.Fatalf("bad error: %v", err)
}
})

t.Run("{version:1}", func(t *testing.T) {
err := makeCar(t, "0aa16776657273696f6e01")
if err.Error() != "empty car, no roots" {
t.Fatalf("bad error: %v", err)
}
})

t.Run("{version:1,roots:{cid:baeaaaa3bmjrq}}", func(t *testing.T) {
err := makeCar(t, "20a265726f6f7473a163636964d82a4800010000036162636776657273696f6e01")
if !strings.HasPrefix(err.Error(), "invalid header: ") {
t.Fatalf("bad error: %v", err)
}
})

t.Run("{version:1,roots:[baeaaaa3bmjrq],blip:true}", func(t *testing.T) {
err := makeCar(t, "22a364626c6970f565726f6f747381d82a4800010000036162636776657273696f6e01")
if !strings.HasPrefix(err.Error(), "invalid header: ") {
t.Fatalf("bad error: %v", err)
}
})

t.Run("[1,[]]", func(t *testing.T) {
err := makeCar(t, "03820180")
if !strings.HasPrefix(err.Error(), "invalid header: ") {
t.Fatalf("bad error: %v", err)
}
})

// 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
t.Run("null", func(t *testing.T) {
err := makeCar(t, "01f6")
if !strings.HasPrefix(err.Error(), "invalid car version: 0") {
t.Fatalf("bad error: %v", err)
}
})
}

0 comments on commit b81b803

Please sign in to comment.