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.

199 lines
5.0 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. "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)
  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)
  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. require := require.New(t)
  50. N := 3
  51. switches := make([]*Switch, N)
  52. dir, err := ioutil.TempDir("", "pex_reactor")
  53. require.Nil(err)
  54. defer os.RemoveAll(dir)
  55. book := NewAddrBook(dir+"addrbook.json", false)
  56. book.SetLogger(log.TestingLogger())
  57. // create switches
  58. for i := 0; i < N; i++ {
  59. switches[i] = makeSwitch(config, i, "127.0.0.1", "123.123.123", func(i int, sw *Switch) *Switch {
  60. sw.SetLogger(log.TestingLogger().With("switch", i))
  61. r := NewPEXReactor(book)
  62. r.SetLogger(log.TestingLogger())
  63. r.SetEnsurePeersPeriod(250 * time.Millisecond)
  64. sw.AddReactor("pex", r)
  65. return sw
  66. })
  67. }
  68. // fill the address book and add listeners
  69. for _, s := range switches {
  70. addr, _ := NewNetAddressString(s.NodeInfo().ListenAddr)
  71. book.AddAddress(addr, addr)
  72. s.AddListener(NewDefaultListener("tcp", s.NodeInfo().ListenAddr, true, log.TestingLogger()))
  73. }
  74. // start switches
  75. for _, s := range switches {
  76. _, err := s.Start() // start switch and reactors
  77. require.Nil(err)
  78. }
  79. assertSomePeersWithTimeout(t, switches, 10*time.Millisecond, 10*time.Second)
  80. // stop them
  81. for _, s := range switches {
  82. s.Stop()
  83. }
  84. }
  85. func assertSomePeersWithTimeout(t *testing.T, switches []*Switch, checkPeriod, timeout time.Duration) {
  86. ticker := time.NewTicker(checkPeriod)
  87. for {
  88. select {
  89. case <-ticker.C:
  90. // check peers are connected
  91. allGood := true
  92. for _, s := range switches {
  93. outbound, inbound, _ := s.NumPeers()
  94. if outbound+inbound == 0 {
  95. allGood = false
  96. }
  97. }
  98. if allGood {
  99. return
  100. }
  101. case <-time.After(timeout):
  102. numPeersStr := ""
  103. for i, s := range switches {
  104. outbound, inbound, _ := s.NumPeers()
  105. numPeersStr += fmt.Sprintf("%d => {outbound: %d, inbound: %d}, ", i, outbound, inbound)
  106. }
  107. t.Errorf("expected all switches to be connected to at least one peer (switches: %s)", numPeersStr)
  108. }
  109. }
  110. }
  111. func TestPEXReactorReceive(t *testing.T) {
  112. assert, require := assert.New(t), require.New(t)
  113. dir, err := ioutil.TempDir("", "pex_reactor")
  114. require.Nil(err)
  115. defer os.RemoveAll(dir)
  116. book := NewAddrBook(dir+"addrbook.json", false)
  117. book.SetLogger(log.TestingLogger())
  118. r := NewPEXReactor(book)
  119. r.SetLogger(log.TestingLogger())
  120. peer := createRandomPeer(false)
  121. size := book.Size()
  122. netAddr, _ := NewNetAddressString(peer.NodeInfo().ListenAddr)
  123. addrs := []*NetAddress{netAddr}
  124. msg := wire.BinaryBytes(struct{ PexMessage }{&pexAddrsMessage{Addrs: addrs}})
  125. r.Receive(PexChannel, peer, msg)
  126. assert.Equal(size+1, book.Size())
  127. msg = wire.BinaryBytes(struct{ PexMessage }{&pexRequestMessage{}})
  128. r.Receive(PexChannel, peer, msg)
  129. }
  130. func TestPEXReactorAbuseFromPeer(t *testing.T) {
  131. assert, require := assert.New(t), require.New(t)
  132. dir, err := ioutil.TempDir("", "pex_reactor")
  133. require.Nil(err)
  134. defer os.RemoveAll(dir)
  135. book := NewAddrBook(dir+"addrbook.json", true)
  136. book.SetLogger(log.TestingLogger())
  137. r := NewPEXReactor(book)
  138. r.SetLogger(log.TestingLogger())
  139. r.SetMaxMsgCountByPeer(5)
  140. peer := createRandomPeer(false)
  141. msg := wire.BinaryBytes(struct{ PexMessage }{&pexRequestMessage{}})
  142. for i := 0; i < 10; i++ {
  143. r.Receive(PexChannel, peer, msg)
  144. }
  145. assert.True(r.ReachedMaxMsgCountForPeer(peer.NodeInfo().ListenAddr))
  146. }
  147. func createRandomPeer(outbound bool) *peer {
  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. p := &peer{
  151. key: cmn.RandStr(12),
  152. nodeInfo: &NodeInfo{
  153. ListenAddr: addr,
  154. RemoteAddr: netAddr.String(),
  155. },
  156. outbound: outbound,
  157. mconn: &MConnection{RemoteAddress: netAddr},
  158. }
  159. p.SetLogger(log.TestingLogger().With("peer", addr))
  160. return p
  161. }