torrent/bencode/fuzz_test.go

36 lines
661 B
Go
Raw Normal View History

//go:build go1.18
package bencode
import (
2021-09-29 02:11:17 +00:00
"math/big"
"testing"
qt "github.com/frankban/quicktest"
2021-09-29 02:11:17 +00:00
"github.com/google/go-cmp/cmp"
)
2021-09-29 02:11:17 +00:00
var bencodeInterfaceChecker = qt.CmpEquals(cmp.Comparer(func(a, b *big.Int) bool {
return a.Cmp(b) == 0
}))
func Fuzz(f *testing.F) {
2021-09-29 02:12:16 +00:00
for _, ret := range random_encode_tests {
f.Add([]byte(ret.expected))
}
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)
2021-09-29 02:11:17 +00:00
c.Assert(d0, bencodeInterfaceChecker, d)
})
}