2021-09-29 08:13:32 +08:00
|
|
|
//go:build go1.18
|
2021-10-28 16:54:46 +08:00
|
|
|
// +build go1.18
|
2021-09-29 08:13:32 +08:00
|
|
|
|
|
|
|
package bencode
|
|
|
|
|
|
|
|
import (
|
2021-09-29 10:11:17 +08:00
|
|
|
"math/big"
|
2021-09-29 08:13:32 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
qt "github.com/frankban/quicktest"
|
2021-09-29 10:11:17 +08:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2021-09-29 08:13:32 +08:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}))
|
|
|
|
|
2021-09-29 08:13:32 +08:00
|
|
|
func Fuzz(f *testing.F) {
|
2021-09-29 10:12:16 +08:00
|
|
|
for _, ret := range random_encode_tests {
|
|
|
|
f.Add([]byte(ret.expected))
|
|
|
|
}
|
2021-09-29 08:13:32 +08:00
|
|
|
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-09-29 08:13:32 +08:00
|
|
|
})
|
|
|
|
}
|
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)
|
|
|
|
})
|
|
|
|
}
|