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.

95 lines
2.8 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/internal/p2p"
  7. "github.com/tendermint/tendermint/internal/p2p/pex"
  8. "github.com/tendermint/tendermint/libs/log"
  9. "github.com/tendermint/tendermint/libs/service"
  10. "github.com/tendermint/tendermint/types"
  11. "github.com/tendermint/tendermint/version"
  12. )
  13. var (
  14. pexR *pex.Reactor
  15. peer p2p.Peer
  16. logger = log.MustNewDefaultLogger(log.LogFormatPlain, log.LogLevelInfo, false)
  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 = types.NodeIDFromPubKey(privKey.PubKey())
  54. var defaultNodeInfo = types.NodeInfo{
  55. ProtocolVersion: types.ProtocolVersion{
  56. P2P: version.P2PProtocol,
  57. Block: version.BlockProtocol,
  58. App: 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() types.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() types.NodeInfo { return defaultNodeInfo }
  74. func (fp *fuzzPeer) Status() p2p.ConnectionStatus { var cs p2p.ConnectionStatus; return cs }
  75. func (fp *fuzzPeer) SocketAddr() *p2p.NetAddress {
  76. return types.NewNetAddress(fp.ID(), fp.RemoteAddr())
  77. }
  78. func (fp *fuzzPeer) Send(byte, []byte) bool { return true }
  79. func (fp *fuzzPeer) TrySend(byte, []byte) bool { return true }
  80. func (fp *fuzzPeer) Set(key string, value interface{}) { fp.m[key] = value }
  81. func (fp *fuzzPeer) Get(key string) interface{} { return fp.m[key] }