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.

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