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.

206 lines
5.2 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
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
7 years ago
7 years ago
8 years ago
8 years ago
  1. package p2p
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "math/rand"
  6. "os"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. wire "github.com/tendermint/go-wire"
  12. cmn "github.com/tendermint/tmlibs/common"
  13. "github.com/tendermint/tmlibs/log"
  14. )
  15. func TestPEXReactorBasic(t *testing.T) {
  16. assert, require := assert.New(t), require.New(t)
  17. dir, err := ioutil.TempDir("", "pex_reactor")
  18. require.Nil(err)
  19. defer os.RemoveAll(dir) // nolint: errcheck
  20. book := NewAddrBook(dir+"addrbook.json", true)
  21. book.SetLogger(log.TestingLogger())
  22. r := NewPEXReactor(book)
  23. r.SetLogger(log.TestingLogger())
  24. assert.NotNil(r)
  25. assert.NotEmpty(r.GetChannels())
  26. }
  27. func TestPEXReactorAddRemovePeer(t *testing.T) {
  28. assert, require := assert.New(t), require.New(t)
  29. dir, err := ioutil.TempDir("", "pex_reactor")
  30. require.Nil(err)
  31. defer os.RemoveAll(dir) // nolint: errcheck
  32. book := NewAddrBook(dir+"addrbook.json", true)
  33. book.SetLogger(log.TestingLogger())
  34. r := NewPEXReactor(book)
  35. r.SetLogger(log.TestingLogger())
  36. size := book.Size()
  37. peer := createRandomPeer(false)
  38. r.AddPeer(peer)
  39. assert.Equal(size+1, book.Size())
  40. r.RemovePeer(peer, "peer not available")
  41. assert.Equal(size+1, book.Size())
  42. outboundPeer := createRandomPeer(true)
  43. r.AddPeer(outboundPeer)
  44. assert.Equal(size+1, book.Size(), "outbound peers should not be added to the address book")
  45. r.RemovePeer(outboundPeer, "peer not available")
  46. assert.Equal(size+1, book.Size())
  47. }
  48. func TestPEXReactorRunning(t *testing.T) {
  49. N := 3
  50. switches := make([]*Switch, N)
  51. dir, err := ioutil.TempDir("", "pex_reactor")
  52. require.Nil(t, err)
  53. defer os.RemoveAll(dir) // nolint: errcheck
  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(t, err)
  77. }
  78. assertSomePeersWithTimeout(t, switches, 10*time.Millisecond, 10*time.Second)
  79. // stop them
  80. for _, s := range switches {
  81. s.Stop()
  82. }
  83. }
  84. func assertSomePeersWithTimeout(t *testing.T, switches []*Switch, checkPeriod, timeout time.Duration) {
  85. ticker := time.NewTicker(checkPeriod)
  86. for {
  87. select {
  88. case <-ticker.C:
  89. // check peers are connected
  90. allGood := true
  91. for _, s := range switches {
  92. outbound, inbound, _ := s.NumPeers()
  93. if outbound+inbound == 0 {
  94. allGood = false
  95. }
  96. }
  97. if allGood {
  98. return
  99. }
  100. case <-time.After(timeout):
  101. numPeersStr := ""
  102. for i, s := range switches {
  103. outbound, inbound, _ := s.NumPeers()
  104. numPeersStr += fmt.Sprintf("%d => {outbound: %d, inbound: %d}, ", i, outbound, inbound)
  105. }
  106. t.Errorf("expected all switches to be connected to at least one peer (switches: %s)", numPeersStr)
  107. }
  108. }
  109. }
  110. func TestPEXReactorReceive(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) // nolint: errcheck
  115. book := NewAddrBook(dir+"addrbook.json", false)
  116. book.SetLogger(log.TestingLogger())
  117. r := NewPEXReactor(book)
  118. r.SetLogger(log.TestingLogger())
  119. peer := createRandomPeer(false)
  120. size := book.Size()
  121. netAddr, _ := NewNetAddressString(peer.NodeInfo().ListenAddr)
  122. addrs := []*NetAddress{netAddr}
  123. msg := wire.BinaryBytes(struct{ PexMessage }{&pexAddrsMessage{Addrs: addrs}})
  124. r.Receive(PexChannel, peer, msg)
  125. assert.Equal(size+1, book.Size())
  126. msg = wire.BinaryBytes(struct{ PexMessage }{&pexRequestMessage{}})
  127. r.Receive(PexChannel, peer, msg)
  128. }
  129. func TestPEXReactorAbuseFromPeer(t *testing.T) {
  130. assert, require := assert.New(t), require.New(t)
  131. dir, err := ioutil.TempDir("", "pex_reactor")
  132. require.Nil(err)
  133. defer os.RemoveAll(dir) // nolint: errcheck
  134. book := NewAddrBook(dir+"addrbook.json", true)
  135. book.SetLogger(log.TestingLogger())
  136. r := NewPEXReactor(book)
  137. r.SetLogger(log.TestingLogger())
  138. r.SetMaxMsgCountByPeer(5)
  139. peer := createRandomPeer(false)
  140. msg := wire.BinaryBytes(struct{ PexMessage }{&pexRequestMessage{}})
  141. for i := 0; i < 10; i++ {
  142. r.Receive(PexChannel, peer, msg)
  143. }
  144. assert.True(r.ReachedMaxMsgCountForPeer(peer.NodeInfo().ListenAddr))
  145. }
  146. func createRoutableAddr() (addr string, netAddr *NetAddress) {
  147. for {
  148. addr = cmn.Fmt("%v.%v.%v.%v:46656", rand.Int()%256, rand.Int()%256, rand.Int()%256, rand.Int()%256)
  149. netAddr, _ = NewNetAddressString(addr)
  150. if netAddr.Routable() {
  151. break
  152. }
  153. }
  154. return
  155. }
  156. func createRandomPeer(outbound bool) *peer {
  157. addr, netAddr := createRoutableAddr()
  158. p := &peer{
  159. nodeInfo: &NodeInfo{
  160. ListenAddr: addr,
  161. RemoteAddr: netAddr.String(),
  162. },
  163. outbound: outbound,
  164. mconn: &MConnection{RemoteAddress: netAddr},
  165. }
  166. p.SetLogger(log.TestingLogger().With("peer", addr))
  167. return p
  168. }