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.

176 lines
3.8 KiB

  1. package p2p
  2. import (
  3. golog "log"
  4. "net"
  5. "testing"
  6. "time"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. crypto "github.com/tendermint/tendermint/crypto"
  10. "github.com/tendermint/tendermint/crypto/ed25519"
  11. cmn "github.com/tendermint/tendermint/libs/common"
  12. "github.com/tendermint/tendermint/libs/log"
  13. "github.com/tendermint/tendermint/config"
  14. tmconn "github.com/tendermint/tendermint/p2p/conn"
  15. )
  16. const testCh = 0x01
  17. func TestPeerBasic(t *testing.T) {
  18. assert, require := assert.New(t), require.New(t)
  19. // simulate remote peer
  20. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
  21. rp.Start()
  22. defer rp.Stop()
  23. p, err := createOutboundPeerAndPerformHandshake(rp.Addr(), cfg, tmconn.DefaultMConnConfig())
  24. require.Nil(err)
  25. err = p.Start()
  26. require.Nil(err)
  27. defer p.Stop()
  28. assert.True(p.IsRunning())
  29. assert.True(p.IsOutbound())
  30. assert.False(p.IsPersistent())
  31. p.persistent = true
  32. assert.True(p.IsPersistent())
  33. assert.Equal(rp.Addr().DialString(), p.Addr().String())
  34. assert.Equal(rp.ID(), p.ID())
  35. }
  36. func TestPeerSend(t *testing.T) {
  37. assert, require := assert.New(t), require.New(t)
  38. config := cfg
  39. // simulate remote peer
  40. rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: config}
  41. rp.Start()
  42. defer rp.Stop()
  43. p, err := createOutboundPeerAndPerformHandshake(rp.Addr(), config, tmconn.DefaultMConnConfig())
  44. require.Nil(err)
  45. err = p.Start()
  46. require.Nil(err)
  47. defer p.Stop()
  48. assert.True(p.CanSend(testCh))
  49. assert.True(p.Send(testCh, []byte("Asylum")))
  50. }
  51. func createOutboundPeerAndPerformHandshake(
  52. addr *NetAddress,
  53. config *config.P2PConfig,
  54. mConfig tmconn.MConnConfig,
  55. ) (*peer, error) {
  56. chDescs := []*tmconn.ChannelDescriptor{
  57. {ID: testCh, Priority: 1},
  58. }
  59. reactorsByCh := map[byte]Reactor{testCh: NewTestReactor(chDescs, true)}
  60. pk := ed25519.GenPrivKey()
  61. pc, err := newOutboundPeerConn(addr, config, false, pk)
  62. if err != nil {
  63. return nil, err
  64. }
  65. nodeInfo, err := pc.HandshakeTimeout(NodeInfo{
  66. ID: addr.ID,
  67. Moniker: "host_peer",
  68. Network: "testing",
  69. Version: "123.123.123",
  70. Channels: []byte{testCh},
  71. }, 1*time.Second)
  72. if err != nil {
  73. return nil, err
  74. }
  75. p := newPeer(pc, mConfig, nodeInfo, reactorsByCh, chDescs, func(p Peer, r interface{}) {})
  76. p.SetLogger(log.TestingLogger().With("peer", addr))
  77. return p, nil
  78. }
  79. type remotePeer struct {
  80. PrivKey crypto.PrivKey
  81. Config *config.P2PConfig
  82. addr *NetAddress
  83. quit chan struct{}
  84. channels cmn.HexBytes
  85. listenAddr string
  86. }
  87. func (rp *remotePeer) Addr() *NetAddress {
  88. return rp.addr
  89. }
  90. func (rp *remotePeer) ID() ID {
  91. return PubKeyToID(rp.PrivKey.PubKey())
  92. }
  93. func (rp *remotePeer) Start() {
  94. if rp.listenAddr == "" {
  95. rp.listenAddr = "127.0.0.1:0"
  96. }
  97. l, e := net.Listen("tcp", rp.listenAddr) // any available address
  98. if e != nil {
  99. golog.Fatalf("net.Listen tcp :0: %+v", e)
  100. }
  101. rp.addr = NewNetAddress(PubKeyToID(rp.PrivKey.PubKey()), l.Addr())
  102. rp.quit = make(chan struct{})
  103. if rp.channels == nil {
  104. rp.channels = []byte{testCh}
  105. }
  106. go rp.accept(l)
  107. }
  108. func (rp *remotePeer) Stop() {
  109. close(rp.quit)
  110. }
  111. func (rp *remotePeer) accept(l net.Listener) {
  112. conns := []net.Conn{}
  113. for {
  114. conn, err := l.Accept()
  115. if err != nil {
  116. golog.Fatalf("Failed to accept conn: %+v", err)
  117. }
  118. pc, err := newInboundPeerConn(conn, rp.Config, rp.PrivKey)
  119. if err != nil {
  120. golog.Fatalf("Failed to create a peer: %+v", err)
  121. }
  122. _, err = pc.HandshakeTimeout(NodeInfo{
  123. ID: rp.Addr().ID,
  124. Moniker: "remote_peer",
  125. Network: "testing",
  126. Version: "123.123.123",
  127. ListenAddr: l.Addr().String(),
  128. Channels: rp.channels,
  129. }, 1*time.Second)
  130. if err != nil {
  131. golog.Fatalf("Failed to perform handshake: %+v", err)
  132. }
  133. conns = append(conns, conn)
  134. select {
  135. case <-rp.quit:
  136. for _, conn := range conns {
  137. if err := conn.Close(); err != nil {
  138. golog.Fatal(err)
  139. }
  140. }
  141. return
  142. default:
  143. }
  144. }
  145. }