Update fuzz to use new standard library support

This commit is contained in:
Matt Joiner 2021-09-29 10:13:32 +10:00
parent a9aec0bb79
commit 2daad6e564
4 changed files with 72 additions and 59 deletions

View File

@ -1,30 +0,0 @@
//go:build gofuzz
// +build gofuzz
package bencode
import (
"fmt"
"reflect"
)
func Fuzz(b []byte) int {
var d interface{}
err := Unmarshal(b, &d)
if err != nil {
return 0
}
b0, err := Marshal(d)
if err != nil {
panic(err)
}
var d0 interface{}
err = Unmarshal(b0, &d0)
if err != nil {
panic(err)
}
if !reflect.DeepEqual(d, d0) {
panic(fmt.Sprintf("%s != %s", d, d0))
}
return 1
}

26
bencode/fuzz_test.go Normal file
View File

@ -0,0 +1,26 @@
//go:build go1.18
package bencode
import (
"testing"
qt "github.com/frankban/quicktest"
)
func Fuzz(f *testing.F) {
f.Fuzz(func(t *testing.T, b []byte) {
c := qt.New(t)
var d interface{}
err := Unmarshal(b, &d)
if err != nil {
t.Skip()
}
b0, err := Marshal(d)
c.Assert(err, qt.IsNil)
var d0 interface{}
err = Unmarshal(b0, &d0)
c.Assert(err, qt.IsNil)
c.Assert(d0, qt.DeepEquals, d)
})
}

View File

@ -1,29 +0,0 @@
//go:build gofuzz
// +build gofuzz
package metainfo
import (
"github.com/anacrolix/torrent/bencode"
)
func Fuzz(b []byte) int {
var mi MetaInfo
err := bencode.Unmarshal(b, &mi)
if err != nil {
return 0
}
_, err = bencode.Marshal(mi)
if err != nil {
panic(err)
}
info, err := mi.UnmarshalInfo()
if err != nil {
return 0
}
_, err = bencode.Marshal(info)
if err != nil {
panic(err)
}
return 1
}

46
metainfo/fuzz_test.go Normal file
View File

@ -0,0 +1,46 @@
//go:build go1.18
package metainfo
import (
"os"
"path/filepath"
"testing"
"github.com/anacrolix/torrent/bencode"
)
func Fuzz(f *testing.F) {
// Is there an OS-agnostic version of Glob?
matches, err := filepath.Glob(filepath.FromSlash("testdata/*.torrent"))
if err != nil {
f.Fatal(err)
}
for _, m := range matches {
b, err := os.ReadFile(m)
if err != nil {
f.Fatal(err)
}
f.Logf("adding %q", m)
f.Add(b)
}
f.Fuzz(func(t *testing.T, b []byte) {
var mi MetaInfo
err := bencode.Unmarshal(b, &mi)
if err != nil {
t.Skip(err)
}
_, err = bencode.Marshal(mi)
if err != nil {
panic(err)
}
info, err := mi.UnmarshalInfo()
if err != nil {
t.Skip(err)
}
_, err = bencode.Marshal(info)
if err != nil {
panic(err)
}
})
}