github.com/DFWallet/tendermint-cosmos@v0.0.2/test/fuzz/p2p/addrbook/fuzz.go (about)

     1  // nolint: gosec
     2  package addr
     3  
     4  import (
     5  	"encoding/json"
     6  	"fmt"
     7  	"math/rand"
     8  
     9  	"github.com/DFWallet/tendermint-cosmos/p2p"
    10  	"github.com/DFWallet/tendermint-cosmos/p2p/pex"
    11  )
    12  
    13  var addrBook = pex.NewAddrBook("./testdata/addrbook.json", true)
    14  
    15  func Fuzz(data []byte) int {
    16  	addr := new(p2p.NetAddress)
    17  	if err := json.Unmarshal(data, addr); err != nil {
    18  		return -1
    19  	}
    20  
    21  	// Fuzz AddAddress.
    22  	err := addrBook.AddAddress(addr, addr)
    23  	if err != nil {
    24  		return 0
    25  	}
    26  
    27  	// Also, make sure PickAddress always returns a non-nil address.
    28  	bias := rand.Intn(100)
    29  	if p := addrBook.PickAddress(bias); p == nil {
    30  		panic(fmt.Sprintf("picked a nil address (bias: %d, addrBook size: %v)",
    31  			bias, addrBook.Size()))
    32  	}
    33  
    34  	return 1
    35  }