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.

208 lines
5.3 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
  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. crypto "github.com/tendermint/go-crypto"
  12. wire "github.com/tendermint/go-wire"
  13. cmn "github.com/tendermint/tmlibs/common"
  14. "github.com/tendermint/tmlibs/log"
  15. )
  16. func TestPEXReactorBasic(t *testing.T) {
  17. assert, require := assert.New(t), require.New(t)
  18. dir, err := ioutil.TempDir("", "pex_reactor")
  19. require.Nil(err)
  20. defer os.RemoveAll(dir) // nolint: errcheck
  21. book := NewAddrBook(dir+"addrbook.json", true)
  22. book.SetLogger(log.TestingLogger())
  23. r := NewPEXReactor(book)
  24. r.SetLogger(log.TestingLogger())
  25. assert.NotNil(r)
  26. assert.NotEmpty(r.GetChannels())
  27. }
  28. func TestPEXReactorAddRemovePeer(t *testing.T) {
  29. assert, require := assert.New(t), require.New(t)
  30. dir, err := ioutil.TempDir("", "pex_reactor")
  31. require.Nil(err)
  32. defer os.RemoveAll(dir) // nolint: errcheck
  33. book := NewAddrBook(dir+"addrbook.json", true)
  34. book.SetLogger(log.TestingLogger())
  35. r := NewPEXReactor(book)
  36. r.SetLogger(log.TestingLogger())
  37. size := book.Size()
  38. peer := createRandomPeer(false)
  39. r.AddPeer(peer)
  40. assert.Equal(size+1, book.Size())
  41. r.RemovePeer(peer, "peer not available")
  42. assert.Equal(size+1, book.Size())
  43. outboundPeer := createRandomPeer(true)
  44. r.AddPeer(outboundPeer)
  45. assert.Equal(size+1, book.Size(), "outbound peers should not be added to the address book")
  46. r.RemovePeer(outboundPeer, "peer not available")
  47. assert.Equal(size+1, book.Size())
  48. }
  49. func TestPEXReactorRunning(t *testing.T) {
  50. N := 3
  51. switches := make([]*Switch, N)
  52. dir, err := ioutil.TempDir("", "pex_reactor")
  53. require.Nil(t, err)
  54. defer os.RemoveAll(dir) // nolint: errcheck
  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(t, 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) // nolint: errcheck
  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) // nolint: errcheck
  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 createRoutableAddr() (addr string, netAddr *NetAddress) {
  148. for {
  149. addr = cmn.Fmt("%v.%v.%v.%v:46656", rand.Int()%256, rand.Int()%256, rand.Int()%256, rand.Int()%256)
  150. netAddr, _ = NewNetAddressString(addr)
  151. if netAddr.Routable() {
  152. break
  153. }
  154. }
  155. return
  156. }
  157. func createRandomPeer(outbound bool) *peer {
  158. addr, netAddr := createRoutableAddr()
  159. p := &peer{
  160. nodeInfo: &NodeInfo{
  161. ListenAddr: addr,
  162. RemoteAddr: netAddr.String(),
  163. PubKey: crypto.GenPrivKeyEd25519().Wrap().PubKey(),
  164. },
  165. outbound: outbound,
  166. mconn: &MConnection{},
  167. }
  168. p.SetLogger(log.TestingLogger().With("peer", addr))
  169. return p
  170. }