]> Sergey Matveev's repositories - btrtrc.git/blob - bencode/fuzz_test.go
Fix addrPortOrZero for unix sockets on Windows
[btrtrc.git] / bencode / fuzz_test.go
1 //go:build go1.18
2 // +build go1.18
3
4 package bencode
5
6 import (
7         "math/big"
8         "testing"
9
10         qt "github.com/frankban/quicktest"
11         "github.com/google/go-cmp/cmp"
12 )
13
14 var bencodeInterfaceChecker = qt.CmpEquals(cmp.Comparer(func(a, b *big.Int) bool {
15         return a.Cmp(b) == 0
16 }))
17
18 func Fuzz(f *testing.F) {
19         for _, ret := range random_encode_tests {
20                 f.Add([]byte(ret.expected))
21         }
22         f.Fuzz(func(t *testing.T, b []byte) {
23                 c := qt.New(t)
24                 var d interface{}
25                 err := Unmarshal(b, &d)
26                 if err != nil {
27                         t.Skip()
28                 }
29                 b0, err := Marshal(d)
30                 c.Assert(err, qt.IsNil)
31                 var d0 interface{}
32                 err = Unmarshal(b0, &d0)
33                 c.Assert(err, qt.IsNil)
34                 c.Assert(d0, bencodeInterfaceChecker, d)
35         })
36 }
37
38 func FuzzInterfaceRoundTrip(f *testing.F) {
39         for _, ret := range random_encode_tests {
40                 f.Add([]byte(ret.expected))
41         }
42         f.Fuzz(func(t *testing.T, b []byte) {
43                 c := qt.New(t)
44                 var d interface{}
45                 err := Unmarshal(b, &d)
46                 if err != nil {
47                         c.Skip(err)
48                 }
49                 b0, err := Marshal(d)
50                 c.Assert(err, qt.IsNil)
51                 c.Check(b0, qt.DeepEquals, b)
52         })
53 }