transport_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package p2p
  17. import (
  18. "errors"
  19. "reflect"
  20. "sync"
  21. "testing"
  22. "github.com/davecgh/go-spew/spew"
  23. "github.com/ethereum/go-ethereum/crypto"
  24. "github.com/ethereum/go-ethereum/p2p/simulations/pipes"
  25. )
  26. func TestProtocolHandshake(t *testing.T) {
  27. var (
  28. prv0, _ = crypto.GenerateKey()
  29. pub0 = crypto.FromECDSAPub(&prv0.PublicKey)[1:]
  30. hs0 = &protoHandshake{Version: 3, ID: pub0, Caps: []Cap{{"a", 0}, {"b", 2}}}
  31. prv1, _ = crypto.GenerateKey()
  32. pub1 = crypto.FromECDSAPub(&prv1.PublicKey)[1:]
  33. hs1 = &protoHandshake{Version: 3, ID: pub1, Caps: []Cap{{"c", 1}, {"d", 3}}}
  34. wg sync.WaitGroup
  35. )
  36. fd0, fd1, err := pipes.TCPPipe()
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. wg.Add(2)
  41. go func() {
  42. defer wg.Done()
  43. defer fd0.Close()
  44. frame := newRLPX(fd0, &prv1.PublicKey)
  45. rpubkey, err := frame.doEncHandshake(prv0)
  46. if err != nil {
  47. t.Errorf("dial side enc handshake failed: %v", err)
  48. return
  49. }
  50. if !reflect.DeepEqual(rpubkey, &prv1.PublicKey) {
  51. t.Errorf("dial side remote pubkey mismatch: got %v, want %v", rpubkey, &prv1.PublicKey)
  52. return
  53. }
  54. phs, err := frame.doProtoHandshake(hs0)
  55. if err != nil {
  56. t.Errorf("dial side proto handshake error: %v", err)
  57. return
  58. }
  59. phs.Rest = nil
  60. if !reflect.DeepEqual(phs, hs1) {
  61. t.Errorf("dial side proto handshake mismatch:\ngot: %s\nwant: %s\n", spew.Sdump(phs), spew.Sdump(hs1))
  62. return
  63. }
  64. frame.close(DiscQuitting)
  65. }()
  66. go func() {
  67. defer wg.Done()
  68. defer fd1.Close()
  69. rlpx := newRLPX(fd1, nil)
  70. rpubkey, err := rlpx.doEncHandshake(prv1)
  71. if err != nil {
  72. t.Errorf("listen side enc handshake failed: %v", err)
  73. return
  74. }
  75. if !reflect.DeepEqual(rpubkey, &prv0.PublicKey) {
  76. t.Errorf("listen side remote pubkey mismatch: got %v, want %v", rpubkey, &prv0.PublicKey)
  77. return
  78. }
  79. phs, err := rlpx.doProtoHandshake(hs1)
  80. if err != nil {
  81. t.Errorf("listen side proto handshake error: %v", err)
  82. return
  83. }
  84. phs.Rest = nil
  85. if !reflect.DeepEqual(phs, hs0) {
  86. t.Errorf("listen side proto handshake mismatch:\ngot: %s\nwant: %s\n", spew.Sdump(phs), spew.Sdump(hs0))
  87. return
  88. }
  89. if err := ExpectMsg(rlpx, discMsg, []DiscReason{DiscQuitting}); err != nil {
  90. t.Errorf("error receiving disconnect: %v", err)
  91. }
  92. }()
  93. wg.Wait()
  94. }
  95. func TestProtocolHandshakeErrors(t *testing.T) {
  96. tests := []struct {
  97. code uint64
  98. msg interface{}
  99. err error
  100. }{
  101. {
  102. code: discMsg,
  103. msg: []DiscReason{DiscQuitting},
  104. err: DiscQuitting,
  105. },
  106. {
  107. code: 0x989898,
  108. msg: []byte{1},
  109. err: errors.New("expected handshake, got 989898"),
  110. },
  111. {
  112. code: handshakeMsg,
  113. msg: make([]byte, baseProtocolMaxMsgSize+2),
  114. err: errors.New("message too big"),
  115. },
  116. {
  117. code: handshakeMsg,
  118. msg: []byte{1, 2, 3},
  119. err: newPeerError(errInvalidMsg, "(code 0) (size 4) rlp: expected input list for p2p.protoHandshake"),
  120. },
  121. {
  122. code: handshakeMsg,
  123. msg: &protoHandshake{Version: 3},
  124. err: DiscInvalidIdentity,
  125. },
  126. }
  127. for i, test := range tests {
  128. p1, p2 := MsgPipe()
  129. go Send(p1, test.code, test.msg)
  130. _, err := readProtocolHandshake(p2)
  131. if !reflect.DeepEqual(err, test.err) {
  132. t.Errorf("test %d: error mismatch: got %q, want %q", i, err, test.err)
  133. }
  134. }
  135. }