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.

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