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.

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