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.

179 lines
4.5 KiB

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