Skip to content

Commit

Permalink
test: add fuzzing of NewCarReader
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorropo authored and masih committed Jul 6, 2022
1 parent 24feaad commit d648fc1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
6 changes: 4 additions & 2 deletions car_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ func TestRoundtrip(t *testing.T) {
}
}

// fixture is a clean single-block, single-root CAR
const fixtureStr = "3aa265726f6f747381d82a58250001711220151fe9e73c6267a7060c6f6c4cca943c236f4b196723489608edb42a8b8fa80b6776657273696f6e012c01711220151fe9e73c6267a7060c6f6c4cca943c236f4b196723489608edb42a8b8fa80ba165646f646779f5"

func TestEOFHandling(t *testing.T) {
// fixture is a clean single-block, single-root CAR
fixture, err := hex.DecodeString("3aa265726f6f747381d82a58250001711220151fe9e73c6267a7060c6f6c4cca943c236f4b196723489608edb42a8b8fa80b6776657273696f6e012c01711220151fe9e73c6267a7060c6f6c4cca943c236f4b196723489608edb42a8b8fa80ba165646f646779f5")
fixture, err := hex.DecodeString(fixtureStr)
if err != nil {
t.Fatal(err)
}
Expand Down
35 changes: 35 additions & 0 deletions fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//go:build go1.18
// +build go1.18

package car_test

import (
"bytes"
"encoding/hex"
"io"
"testing"

car "github.com/ipld/go-car"
)

func FuzzCarReader(f *testing.F) {
fixture, err := hex.DecodeString(fixtureStr)
if err != nil {
f.Fatal(err)
}
f.Add(fixture)

f.Fuzz(func(t *testing.T, data []byte) {
r, err := car.NewCarReader(bytes.NewReader(data))
if err != nil {
return
}

for {
_, err = r.Next()
if err == io.EOF {
return
}
}
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte("\xe0\xe0\xe0\xe0\xa7\x06\folLʔ<#oK\x19g#H\x96\b\xed\xb4*\x8b\x8f\xa8\vgversion\x19")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
go test fuzz v1
[]byte(":\xa2eroots\x81\xd80X%\x00\x0100 00000000000000000000000000000000gversion\x01\x010")

0 comments on commit d648fc1

Please sign in to comment.