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.

153 lines
3.7 KiB

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