github.com/anacrolix/torrent@v1.61.0/bencode/fuzz_test.go (about)

     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/go-quicktest/qt"
    11  	"github.com/google/go-cmp/cmp"
    12  )
    13  
    14  func Fuzz(f *testing.F) {
    15  	for _, ret := range random_encode_tests {
    16  		f.Add([]byte(ret.expected))
    17  	}
    18  	f.Fuzz(func(t *testing.T, b []byte) {
    19  		var d interface{}
    20  		err := Unmarshal(b, &d)
    21  		if err != nil {
    22  			t.Skip()
    23  		}
    24  		b0, err := Marshal(d)
    25  		qt.Assert(t, qt.IsNil(err))
    26  		var d0 interface{}
    27  		err = Unmarshal(b0, &d0)
    28  		qt.Assert(t, qt.IsNil(err))
    29  		qt.Assert(t, qt.CmpEquals(d0, d, cmp.Comparer(func(a, b *big.Int) bool {
    30  			return a.Cmp(b) == 0
    31  		})))
    32  	})
    33  }
    34  
    35  func FuzzInterfaceRoundTrip(f *testing.F) {
    36  	for _, ret := range random_encode_tests {
    37  		f.Add([]byte(ret.expected))
    38  	}
    39  	f.Fuzz(func(t *testing.T, b []byte) {
    40  		var d interface{}
    41  		err := Unmarshal(b, &d)
    42  		if err != nil {
    43  			t.Skip(err)
    44  		}
    45  		b0, err := Marshal(d)
    46  		qt.Assert(t, qt.IsNil(err))
    47  		qt.Check(t, qt.DeepEquals(b0, b))
    48  	})
    49  }