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.

162 lines
3.7 KiB

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