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.

93 lines
2.7 KiB

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