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.

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