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.

151 lines
3.6 KiB

  1. package p2p
  2. import (
  3. "io/ioutil"
  4. "math/rand"
  5. "os"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. cmn "github.com/tendermint/go-common"
  11. wire "github.com/tendermint/go-wire"
  12. )
  13. func TestPEXReactorBasic(t *testing.T) {
  14. dir, err := ioutil.TempDir("", "pex_reactor")
  15. require.Nil(t, err)
  16. defer os.RemoveAll(dir)
  17. book := NewAddrBook(dir+"addrbook.json", true)
  18. r := NewPEXReactor(book)
  19. assert.NotNil(t, r)
  20. assert.NotEmpty(t, r.GetChannels())
  21. }
  22. func TestPEXReactorAddRemovePeer(t *testing.T) {
  23. dir, err := ioutil.TempDir("", "pex_reactor")
  24. require.Nil(t, err)
  25. defer os.RemoveAll(dir)
  26. book := NewAddrBook(dir+"addrbook.json", true)
  27. r := NewPEXReactor(book)
  28. size := book.Size()
  29. peer := createRandomPeer(false)
  30. r.AddPeer(peer)
  31. assert.Equal(t, size+1, book.Size())
  32. r.RemovePeer(peer, "peer not available")
  33. assert.Equal(t, size, book.Size())
  34. outboundPeer := createRandomPeer(true)
  35. r.AddPeer(outboundPeer)
  36. assert.Equal(t, size, book.Size(), "size must not change")
  37. r.RemovePeer(outboundPeer, "peer not available")
  38. assert.Equal(t, size, book.Size(), "size must not change")
  39. }
  40. func TestPEXReactorRunning(t *testing.T) {
  41. N := 3
  42. switches := make([]*Switch, N)
  43. dir, err := ioutil.TempDir("", "pex_reactor")
  44. require.Nil(t, err)
  45. defer os.RemoveAll(dir)
  46. book := NewAddrBook(dir+"addrbook.json", false)
  47. // create switches
  48. for i := 0; i < N; i++ {
  49. switches[i] = makeSwitch(i, "127.0.0.1", "123.123.123", func(i int, sw *Switch) *Switch {
  50. r := NewPEXReactor(book)
  51. r.SetEnsurePeersPeriod(250 * time.Millisecond)
  52. sw.AddReactor("pex", r)
  53. return sw
  54. })
  55. }
  56. // fill the address book and add listeners
  57. for _, s := range switches {
  58. addr := NewNetAddressString(s.NodeInfo().ListenAddr)
  59. book.AddAddress(addr, addr)
  60. s.AddListener(NewDefaultListener("tcp", s.NodeInfo().ListenAddr, true))
  61. }
  62. // start switches
  63. for _, s := range switches {
  64. _, err := s.Start() // start switch and reactors
  65. require.Nil(t, err)
  66. }
  67. time.Sleep(1 * time.Second)
  68. // check peers are connected after some time
  69. for _, s := range switches {
  70. outbound, inbound, _ := s.NumPeers()
  71. if outbound+inbound == 0 {
  72. t.Errorf("%v expected to be connected to at least one peer", s.NodeInfo().ListenAddr)
  73. }
  74. }
  75. // stop them
  76. for _, s := range switches {
  77. s.Stop()
  78. }
  79. }
  80. func TestPEXReactorReceive(t *testing.T) {
  81. dir, err := ioutil.TempDir("", "pex_reactor")
  82. require.Nil(t, err)
  83. defer os.RemoveAll(dir)
  84. book := NewAddrBook(dir+"addrbook.json", true)
  85. r := NewPEXReactor(book)
  86. peer := createRandomPeer(false)
  87. size := book.Size()
  88. addrs := []*NetAddress{NewNetAddressString(peer.ListenAddr)}
  89. msg := wire.BinaryBytes(struct{ PexMessage }{&pexAddrsMessage{Addrs: addrs}})
  90. r.Receive(PexChannel, peer, msg)
  91. assert.Equal(t, size+1, book.Size())
  92. msg = wire.BinaryBytes(struct{ PexMessage }{&pexRequestMessage{}})
  93. r.Receive(PexChannel, peer, msg)
  94. }
  95. func TestPEXReactorAbuseFromPeer(t *testing.T) {
  96. dir, err := ioutil.TempDir("", "pex_reactor")
  97. require.Nil(t, err)
  98. defer os.RemoveAll(dir)
  99. book := NewAddrBook(dir+"addrbook.json", true)
  100. r := NewPEXReactor(book)
  101. r.SetMaxMsgCountByPeer(5)
  102. peer := createRandomPeer(false)
  103. msg := wire.BinaryBytes(struct{ PexMessage }{&pexRequestMessage{}})
  104. for i := 0; i < 10; i++ {
  105. r.Receive(PexChannel, peer, msg)
  106. }
  107. assert.True(t, r.ReachedMaxMsgCountForPeer(peer.ListenAddr))
  108. }
  109. func createRandomPeer(outbound bool) *Peer {
  110. addr := cmn.Fmt("%v.%v.%v.%v:46656", rand.Int()%256, rand.Int()%256, rand.Int()%256, rand.Int()%256)
  111. return &Peer{
  112. Key: cmn.RandStr(12),
  113. NodeInfo: &NodeInfo{
  114. ListenAddr: addr,
  115. },
  116. outbound: outbound,
  117. mconn: &MConnection{RemoteAddress: NewNetAddressString(addr)},
  118. }
  119. }