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

     1  package pex
     2  
     3  import (
     4  	"net"
     5  
     6  	"github.com/DFWallet/tendermint-cosmos/config"
     7  	"github.com/DFWallet/tendermint-cosmos/crypto/ed25519"
     8  	"github.com/DFWallet/tendermint-cosmos/libs/log"
     9  	"github.com/DFWallet/tendermint-cosmos/libs/service"
    10  	"github.com/DFWallet/tendermint-cosmos/p2p"
    11  	"github.com/DFWallet/tendermint-cosmos/p2p/pex"
    12  	"github.com/DFWallet/tendermint-cosmos/version"
    13  )
    14  
    15  var (
    16  	pexR *pex.Reactor
    17  	peer p2p.Peer
    18  )
    19  
    20  func init() {
    21  	addrB := pex.NewAddrBook("./testdata/addrbook1", false)
    22  	pexR := pex.NewReactor(addrB, &pex.ReactorConfig{SeedMode: false})
    23  	if pexR == nil {
    24  		panic("NewReactor returned nil")
    25  	}
    26  	pexR.SetLogger(log.NewNopLogger())
    27  	peer := newFuzzPeer()
    28  	pexR.AddPeer(peer)
    29  
    30  }
    31  
    32  func Fuzz(data []byte) int {
    33  	// MakeSwitch uses log.TestingLogger which can't be executed in init()
    34  	cfg := config.DefaultP2PConfig()
    35  	cfg.PexReactor = true
    36  	sw := p2p.MakeSwitch(cfg, 0, "127.0.0.1", "123.123.123", func(i int, sw *p2p.Switch) *p2p.Switch {
    37  		return sw
    38  	})
    39  	pexR.SetSwitch(sw)
    40  
    41  	pexR.Receive(pex.PexChannel, peer, data)
    42  	return 1
    43  }
    44  
    45  type fuzzPeer struct {
    46  	*service.BaseService
    47  	m map[string]interface{}
    48  }
    49  
    50  var _ p2p.Peer = (*fuzzPeer)(nil)
    51  
    52  func newFuzzPeer() *fuzzPeer {
    53  	fp := &fuzzPeer{m: make(map[string]interface{})}
    54  	fp.BaseService = service.NewBaseService(nil, "fuzzPeer", fp)
    55  	return fp
    56  }
    57  
    58  var privKey = ed25519.GenPrivKey()
    59  var nodeID = p2p.PubKeyToID(privKey.PubKey())
    60  var defaultNodeInfo = p2p.DefaultNodeInfo{
    61  	ProtocolVersion: p2p.NewProtocolVersion(
    62  		version.P2PProtocol,
    63  		version.BlockProtocol,
    64  		0,
    65  	),
    66  	DefaultNodeID: nodeID,
    67  	ListenAddr:    "0.0.0.0:98992",
    68  	Moniker:       "foo1",
    69  }
    70  
    71  func (fp *fuzzPeer) FlushStop()       {}
    72  func (fp *fuzzPeer) ID() p2p.ID       { return nodeID }
    73  func (fp *fuzzPeer) RemoteIP() net.IP { return net.IPv4(0, 0, 0, 0) }
    74  func (fp *fuzzPeer) RemoteAddr() net.Addr {
    75  	return &net.TCPAddr{IP: fp.RemoteIP(), Port: 98991, Zone: ""}
    76  }
    77  func (fp *fuzzPeer) IsOutbound() bool                  { return false }
    78  func (fp *fuzzPeer) IsPersistent() bool                { return false }
    79  func (fp *fuzzPeer) CloseConn() error                  { return nil }
    80  func (fp *fuzzPeer) NodeInfo() p2p.NodeInfo            { return defaultNodeInfo }
    81  func (fp *fuzzPeer) Status() p2p.ConnectionStatus      { var cs p2p.ConnectionStatus; return cs }
    82  func (fp *fuzzPeer) SocketAddr() *p2p.NetAddress       { return p2p.NewNetAddress(fp.ID(), fp.RemoteAddr()) }
    83  func (fp *fuzzPeer) Send(byte, []byte) bool            { return true }
    84  func (fp *fuzzPeer) TrySend(byte, []byte) bool         { return true }
    85  func (fp *fuzzPeer) Set(key string, value interface{}) { fp.m[key] = value }
    86  func (fp *fuzzPeer) Get(key string) interface{}        { return fp.m[key] }