github.com/klaytn/klaytn@v1.12.1/networks/p2p/transport_test.go (about) 1 // Copyright 2020 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 17 package p2p 18 19 import ( 20 "errors" 21 "reflect" 22 "sync" 23 "testing" 24 25 "github.com/davecgh/go-spew/spew" 26 "github.com/klaytn/klaytn/crypto" 27 "github.com/klaytn/klaytn/networks/p2p/discover" 28 "github.com/klaytn/klaytn/networks/p2p/simulations/pipes" 29 ) 30 31 func TestProtocolHandshake(t *testing.T) { 32 var ( 33 prv0, _ = crypto.GenerateKey() 34 pub0 = crypto.FromECDSAPub(&prv0.PublicKey)[1:] 35 id0, _ = discover.BytesID(pub0) 36 hs0 = &protoHandshake{Version: 3, ID: id0, ListenPort: []uint64{}, Caps: []Cap{{"a", 0}, {"b", 2}}} 37 38 prv1, _ = crypto.GenerateKey() 39 pub1 = crypto.FromECDSAPub(&prv1.PublicKey)[1:] 40 id1, _ = discover.BytesID(pub1) 41 hs1 = &protoHandshake{Version: 3, ID: id1, ListenPort: []uint64{}, Caps: []Cap{{"c", 1}, {"d", 3}}} 42 43 wg sync.WaitGroup 44 ) 45 46 fd0, fd1, err := pipes.TCPPipe() 47 if err != nil { 48 t.Fatal(err) 49 } 50 51 wg.Add(2) 52 go func() { 53 defer wg.Done() 54 defer fd0.Close() 55 frame := newRLPX(fd0, &prv1.PublicKey) 56 rpubkey, err := frame.doEncHandshake(prv0) 57 if err != nil { 58 t.Errorf("dial side enc handshake failed: %v", err) 59 return 60 } 61 if !reflect.DeepEqual(rpubkey, &prv1.PublicKey) { 62 t.Errorf("dial side remote pubkey mismatch: got %v, want %v", rpubkey, &prv1.PublicKey) 63 return 64 } 65 66 phs, err := frame.doProtoHandshake(hs0) 67 if err != nil { 68 t.Errorf("dial side proto handshake error: %v", err) 69 return 70 } 71 phs.Rest = nil 72 if !reflect.DeepEqual(phs, hs1) { 73 t.Errorf("dial side proto handshake mismatch:\ngot: %s\nwant: %s\n", spew.Sdump(phs), spew.Sdump(hs1)) 74 return 75 } 76 frame.close(DiscQuitting) 77 }() 78 go func() { 79 defer wg.Done() 80 defer fd1.Close() 81 rlpx := newRLPX(fd1, nil) 82 rpubkey, err := rlpx.doEncHandshake(prv1) 83 if err != nil { 84 t.Errorf("listen side enc handshake failed: %v", err) 85 return 86 } 87 if !reflect.DeepEqual(rpubkey, &prv0.PublicKey) { 88 t.Errorf("listen side remote pubkey mismatch: got %v, want %v", rpubkey, &prv0.PublicKey) 89 return 90 } 91 92 phs, err := rlpx.doProtoHandshake(hs1) 93 if err != nil { 94 t.Errorf("listen side proto handshake error: %v", err) 95 return 96 } 97 phs.Rest = nil 98 if !reflect.DeepEqual(phs, hs0) { 99 t.Errorf("listen side proto handshake mismatch:\ngot: %s\nwant: %s\n", spew.Sdump(phs), spew.Sdump(hs0)) 100 return 101 } 102 103 if err := ExpectMsg(rlpx, discMsg, []DiscReason{DiscQuitting}); err != nil { 104 t.Errorf("error receiving disconnect: %v", err) 105 } 106 }() 107 wg.Wait() 108 } 109 110 func TestProtocolHandshakeErrors(t *testing.T) { 111 tests := []struct { 112 code uint64 113 msg interface{} 114 err error 115 }{ 116 { 117 code: discMsg, 118 msg: []DiscReason{DiscQuitting}, 119 err: DiscQuitting, 120 }, 121 { 122 code: 0x989898, 123 msg: []byte{1}, 124 err: errors.New("expected handshake, got 989898"), 125 }, 126 { 127 code: handshakeMsg, 128 msg: make([]byte, baseProtocolMaxMsgSize+2), 129 err: errors.New("message too big"), 130 }, 131 { 132 code: handshakeMsg, 133 msg: []byte{1, 2, 3}, 134 err: newPeerError(errInvalidMsg, "(code 0) (size 4) rlp: expected input list for p2p.protoHandshake"), 135 }, 136 { 137 code: handshakeMsg, 138 msg: &protoHandshake{Version: 3}, 139 err: DiscInvalidIdentity, 140 }, 141 } 142 143 for i, test := range tests { 144 p1, p2 := MsgPipe() 145 go Send(p1, test.code, test.msg) 146 _, err := readProtocolHandshake(p2) 147 if !reflect.DeepEqual(err, test.err) { 148 t.Errorf("test %d: error mismatch: got %q, want %q", i, err, test.err) 149 } 150 } 151 }