From 2daad6e564706c36c63b3962fc212f776cc2cf23 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 29 Sep 2021 10:13:32 +1000 Subject: [PATCH] Update fuzz to use new standard library support --- bencode/fuzz.go | 30 ---------------------------- bencode/fuzz_test.go | 26 ++++++++++++++++++++++++ metainfo/fuzz.go | 29 --------------------------- metainfo/fuzz_test.go | 46 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 59 deletions(-) delete mode 100644 bencode/fuzz.go create mode 100644 bencode/fuzz_test.go delete mode 100644 metainfo/fuzz.go create mode 100644 metainfo/fuzz_test.go diff --git a/bencode/fuzz.go b/bencode/fuzz.go deleted file mode 100644 index 7606a8cc..00000000 --- a/bencode/fuzz.go +++ /dev/null @@ -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 -} diff --git a/bencode/fuzz_test.go b/bencode/fuzz_test.go new file mode 100644 index 00000000..57c18eed --- /dev/null +++ b/bencode/fuzz_test.go @@ -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) + }) +} diff --git a/metainfo/fuzz.go b/metainfo/fuzz.go deleted file mode 100644 index f0f0dd41..00000000 --- a/metainfo/fuzz.go +++ /dev/null @@ -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 -} diff --git a/metainfo/fuzz_test.go b/metainfo/fuzz_test.go new file mode 100644 index 00000000..4b39d4fd --- /dev/null +++ b/metainfo/fuzz_test.go @@ -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) + } + }) +}