FedP2P/bencode/fuzz_test.go

54 lines
1.0 KiB
Go
Raw Normal View History

//go:build go1.18
// +build go1.18
package bencode
import (
2021-09-29 10:11:17 +08:00
"math/big"
"testing"
qt "github.com/frankban/quicktest"
2021-09-29 10:11:17 +08:00
"github.com/google/go-cmp/cmp"
)
2021-09-29 10:11:17 +08: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 10:12:16 +08: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 10:11:17 +08:00
c.Assert(d0, bencodeInterfaceChecker, d)
})
}
2021-12-10 10:35:57 +08:00
func FuzzInterfaceRoundTrip(f *testing.F) {
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 {
c.Skip(err)
}
b0, err := Marshal(d)
c.Assert(err, qt.IsNil)
c.Check(b0, qt.DeepEquals, b)
})
}