You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.6 KiB

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