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.

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