2014-10-23 18:57:54 +03:00
|
|
|
package p2p
|
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
import (
|
2014-11-25 17:01:39 +02:00
|
|
|
"bytes"
|
2015-02-05 04:07:58 +02:00
|
|
|
"fmt"
|
2014-11-25 17:01:39 +02:00
|
|
|
"io/ioutil"
|
2014-11-21 22:48:49 +02:00
|
|
|
"net"
|
2014-11-26 23:49:40 +02:00
|
|
|
"reflect"
|
2015-02-05 04:07:58 +02:00
|
|
|
"sort"
|
2014-11-21 22:48:49 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
2015-02-05 04:07:58 +02:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2014-11-21 22:48:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var discard = Protocol{
|
|
|
|
Name: "discard",
|
|
|
|
Length: 1,
|
|
|
|
Run: func(p *Peer, rw MsgReadWriter) error {
|
|
|
|
for {
|
|
|
|
msg, err := rw.ReadMsg()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = msg.Discard(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
func testPeer(handshake bool, protos []Protocol) (*frameRW, *Peer, <-chan DiscReason) {
|
2014-11-21 22:48:49 +02:00
|
|
|
conn1, conn2 := net.Pipe()
|
2015-02-05 04:07:58 +02:00
|
|
|
peer := newPeer(conn1, protos, "name", &discover.NodeID{}, &discover.NodeID{})
|
|
|
|
peer.protocolHandshakeEnabled = handshake
|
|
|
|
errc := make(chan DiscReason, 1)
|
|
|
|
go func() { errc <- peer.run() }()
|
|
|
|
return newFrameRW(conn2, msgWriteTimeout), peer, errc
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerProtoReadMsg(t *testing.T) {
|
|
|
|
defer testlog(t).detach()
|
|
|
|
|
|
|
|
done := make(chan struct{})
|
|
|
|
proto := Protocol{
|
|
|
|
Name: "a",
|
|
|
|
Length: 5,
|
|
|
|
Run: func(peer *Peer, rw MsgReadWriter) error {
|
2015-02-05 04:07:58 +02:00
|
|
|
if err := expectMsg(rw, 2, []uint{1}); err != nil {
|
|
|
|
t.Error(err)
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
if err := expectMsg(rw, 3, []uint{2}); err != nil {
|
|
|
|
t.Error(err)
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
if err := expectMsg(rw, 4, []uint{3}); err != nil {
|
|
|
|
t.Error(err)
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
close(done)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
rw, peer, errc := testPeer(false, []Protocol{proto})
|
|
|
|
defer rw.Close()
|
2014-11-21 22:48:49 +02:00
|
|
|
peer.startSubprotocols([]Cap{proto.cap()})
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
EncodeMsg(rw, baseProtocolLength+2, 1)
|
|
|
|
EncodeMsg(rw, baseProtocolLength+3, 2)
|
|
|
|
EncodeMsg(rw, baseProtocolLength+4, 3)
|
|
|
|
|
2014-11-21 22:48:49 +02:00
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case err := <-errc:
|
|
|
|
t.Errorf("peer returned: %v", err)
|
|
|
|
case <-time.After(2 * time.Second):
|
|
|
|
t.Errorf("receive timeout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerProtoReadLargeMsg(t *testing.T) {
|
|
|
|
defer testlog(t).detach()
|
|
|
|
|
|
|
|
msgsize := uint32(10 * 1024 * 1024)
|
|
|
|
done := make(chan struct{})
|
|
|
|
proto := Protocol{
|
|
|
|
Name: "a",
|
|
|
|
Length: 5,
|
|
|
|
Run: func(peer *Peer, rw MsgReadWriter) error {
|
|
|
|
msg, err := rw.ReadMsg()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("read error: %v", err)
|
|
|
|
}
|
|
|
|
if msg.Size != msgsize+4 {
|
|
|
|
t.Errorf("incorrect msg.Size, got %d, expected %d", msg.Size, msgsize)
|
|
|
|
}
|
|
|
|
msg.Discard()
|
|
|
|
close(done)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
rw, peer, errc := testPeer(false, []Protocol{proto})
|
|
|
|
defer rw.Close()
|
2014-11-21 22:48:49 +02:00
|
|
|
peer.startSubprotocols([]Cap{proto.cap()})
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
EncodeMsg(rw, 18, make([]byte, msgsize))
|
2014-11-21 22:48:49 +02:00
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case err := <-errc:
|
|
|
|
t.Errorf("peer returned: %v", err)
|
|
|
|
case <-time.After(2 * time.Second):
|
|
|
|
t.Errorf("receive timeout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPeerProtoEncodeMsg(t *testing.T) {
|
|
|
|
defer testlog(t).detach()
|
|
|
|
|
|
|
|
proto := Protocol{
|
|
|
|
Name: "a",
|
|
|
|
Length: 2,
|
|
|
|
Run: func(peer *Peer, rw MsgReadWriter) error {
|
2015-01-06 12:35:09 +02:00
|
|
|
if err := EncodeMsg(rw, 2); err == nil {
|
2014-11-21 22:48:49 +02:00
|
|
|
t.Error("expected error for out-of-range msg code, got nil")
|
|
|
|
}
|
2015-01-06 12:35:09 +02:00
|
|
|
if err := EncodeMsg(rw, 1, "foo", "bar"); err != nil {
|
2014-11-21 22:48:49 +02:00
|
|
|
t.Errorf("write error: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
rw, peer, _ := testPeer(false, []Protocol{proto})
|
|
|
|
defer rw.Close()
|
2014-11-21 22:48:49 +02:00
|
|
|
peer.startSubprotocols([]Cap{proto.cap()})
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
if err := expectMsg(rw, 17, []string{"foo", "bar"}); err != nil {
|
|
|
|
t.Error(err)
|
2015-01-05 18:10:42 +02:00
|
|
|
}
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
func TestPeerWriteForBroadcast(t *testing.T) {
|
2014-11-21 22:48:49 +02:00
|
|
|
defer testlog(t).detach()
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
rw, peer, peerErr := testPeer(false, []Protocol{discard})
|
|
|
|
defer rw.Close()
|
2014-11-21 22:48:49 +02:00
|
|
|
peer.startSubprotocols([]Cap{discard.cap()})
|
|
|
|
|
|
|
|
// test write errors
|
|
|
|
if err := peer.writeProtoMsg("b", NewMsg(3)); err == nil {
|
|
|
|
t.Errorf("expected error for unknown protocol, got nil")
|
|
|
|
}
|
|
|
|
if err := peer.writeProtoMsg("discard", NewMsg(8)); err == nil {
|
|
|
|
t.Errorf("expected error for out-of-range msg code, got nil")
|
|
|
|
} else if perr, ok := err.(*peerError); !ok || perr.Code != errInvalidMsgCode {
|
|
|
|
t.Errorf("wrong error for out-of-range msg code, got %#v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup for reading the message on the other end
|
|
|
|
read := make(chan struct{})
|
|
|
|
go func() {
|
2015-02-05 04:07:58 +02:00
|
|
|
if err := expectMsg(rw, 16, nil); err != nil {
|
|
|
|
t.Error()
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
close(read)
|
|
|
|
}()
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
// test successful write
|
2014-11-21 22:48:49 +02:00
|
|
|
if err := peer.writeProtoMsg("discard", NewMsg(0)); err != nil {
|
|
|
|
t.Errorf("expect no error for known protocol: %v", err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-read:
|
|
|
|
case err := <-peerErr:
|
|
|
|
t.Fatalf("peer stopped: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
func TestPeerPing(t *testing.T) {
|
|
|
|
defer testlog(t).detach()
|
2014-11-21 22:48:49 +02:00
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
rw, _, _ := testPeer(false, nil)
|
|
|
|
defer rw.Close()
|
|
|
|
if err := EncodeMsg(rw, pingMsg); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := expectMsg(rw, pongMsg, nil); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
2014-11-21 22:48:49 +02:00
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
func TestPeerDisconnect(t *testing.T) {
|
|
|
|
defer testlog(t).detach()
|
2014-11-21 22:48:49 +02:00
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
rw, _, disc := testPeer(false, nil)
|
|
|
|
defer rw.Close()
|
|
|
|
if err := EncodeMsg(rw, discMsg, DiscQuitting); err != nil {
|
|
|
|
t.Fatal(err)
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
if err := expectMsg(rw, discMsg, []interface{}{DiscRequested}); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
rw.Close() // make test end faster
|
|
|
|
if reason := <-disc; reason != DiscRequested {
|
|
|
|
t.Errorf("run returned wrong reason: got %v, want %v", reason, DiscRequested)
|
2014-11-21 22:48:49 +02:00
|
|
|
}
|
|
|
|
}
|
2014-11-26 23:49:40 +02:00
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
func TestPeerHandshake(t *testing.T) {
|
|
|
|
defer testlog(t).detach()
|
|
|
|
|
|
|
|
// remote has two matching protocols: a and c
|
|
|
|
remote := NewPeer(randomID(), "", []Cap{{"a", 1}, {"b", 999}, {"c", 3}})
|
|
|
|
remoteID := randomID()
|
|
|
|
remote.ourID = &remoteID
|
|
|
|
remote.ourName = "remote peer"
|
|
|
|
|
|
|
|
start := make(chan string)
|
|
|
|
stop := make(chan struct{})
|
|
|
|
run := func(p *Peer, rw MsgReadWriter) error {
|
|
|
|
name := rw.(*proto).name
|
|
|
|
if name != "a" && name != "c" {
|
|
|
|
t.Errorf("protocol %q should not be started", name)
|
|
|
|
} else {
|
|
|
|
start <- name
|
|
|
|
}
|
|
|
|
<-stop
|
|
|
|
return nil
|
2014-11-26 23:49:40 +02:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
protocols := []Protocol{
|
|
|
|
{Name: "a", Version: 1, Length: 1, Run: run},
|
|
|
|
{Name: "b", Version: 2, Length: 1, Run: run},
|
|
|
|
{Name: "c", Version: 3, Length: 1, Run: run},
|
|
|
|
{Name: "d", Version: 4, Length: 1, Run: run},
|
2014-11-26 23:49:40 +02:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
rw, p, disc := testPeer(true, protocols)
|
|
|
|
p.remoteID = remote.ourID
|
|
|
|
defer rw.Close()
|
2014-12-12 12:38:42 +02:00
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
// run the handshake
|
|
|
|
remoteProtocols := []Protocol{protocols[0], protocols[2]}
|
|
|
|
if err := writeProtocolHandshake(rw, "remote peer", remoteID, remoteProtocols); err != nil {
|
|
|
|
t.Fatalf("handshake write error: %v", err)
|
|
|
|
}
|
|
|
|
if err := readProtocolHandshake(remote, rw); err != nil {
|
|
|
|
t.Fatalf("handshake read error: %v", err)
|
|
|
|
}
|
2014-12-12 12:38:42 +02:00
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
// check that all protocols have been started
|
|
|
|
var started []string
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
select {
|
|
|
|
case name := <-start:
|
|
|
|
started = append(started, name)
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
}
|
2014-12-12 12:38:42 +02:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
sort.Strings(started)
|
|
|
|
if !reflect.DeepEqual(started, []string{"a", "c"}) {
|
|
|
|
t.Errorf("wrong protocols started: %v", started)
|
2014-12-12 12:38:42 +02:00
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
// check that metadata has been set
|
|
|
|
if p.ID() != remoteID {
|
|
|
|
t.Errorf("peer has wrong node ID: got %v, want %v", p.ID(), remoteID)
|
2014-12-12 12:38:42 +02:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
if p.Name() != remote.ourName {
|
|
|
|
t.Errorf("peer has wrong node name: got %q, want %q", p.Name(), remote.ourName)
|
2014-12-12 12:38:42 +02:00
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
close(stop)
|
|
|
|
t.Logf("disc reason: %v", <-disc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewPeer(t *testing.T) {
|
|
|
|
name := "nodename"
|
|
|
|
caps := []Cap{{"foo", 2}, {"bar", 3}}
|
|
|
|
id := randomID()
|
|
|
|
p := NewPeer(id, name, caps)
|
|
|
|
if p.ID() != id {
|
|
|
|
t.Errorf("ID mismatch: got %v, expected %v", p.ID(), id)
|
2014-12-12 12:38:42 +02:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
if p.Name() != name {
|
|
|
|
t.Errorf("Name mismatch: got %v, expected %v", p.Name(), name)
|
2014-12-12 12:38:42 +02:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
if !reflect.DeepEqual(p.Caps(), caps) {
|
|
|
|
t.Errorf("Caps mismatch: got %v, expected %v", p.Caps(), caps)
|
2014-12-12 12:38:42 +02:00
|
|
|
}
|
|
|
|
|
2015-02-05 04:07:58 +02:00
|
|
|
p.Disconnect(DiscAlreadyConnected) // Should not hang
|
|
|
|
}
|
|
|
|
|
|
|
|
// expectMsg reads a message from r and verifies that its
|
|
|
|
// code and encoded RLP content match the provided values.
|
|
|
|
// If content is nil, the payload is discarded and not verified.
|
|
|
|
func expectMsg(r MsgReader, code uint64, content interface{}) error {
|
|
|
|
msg, err := r.ReadMsg()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-12-12 12:38:42 +02:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
if msg.Code != code {
|
|
|
|
return fmt.Errorf("message code mismatch: got %d, expected %d", msg.Code, code)
|
|
|
|
}
|
|
|
|
if content == nil {
|
|
|
|
return msg.Discard()
|
|
|
|
} else {
|
|
|
|
contentEnc, err := rlp.EncodeToBytes(content)
|
|
|
|
if err != nil {
|
|
|
|
panic("content encode error: " + err.Error())
|
|
|
|
}
|
|
|
|
// skip over list header in encoded value. this is temporary.
|
|
|
|
contentEncR := bytes.NewReader(contentEnc)
|
|
|
|
if k, _, err := rlp.NewStream(contentEncR).Kind(); k != rlp.List || err != nil {
|
|
|
|
panic("content must encode as RLP list")
|
|
|
|
}
|
|
|
|
contentEnc = contentEnc[len(contentEnc)-contentEncR.Len():]
|
|
|
|
|
|
|
|
actualContent, err := ioutil.ReadAll(msg.Payload)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !bytes.Equal(actualContent, contentEnc) {
|
|
|
|
return fmt.Errorf("message payload mismatch:\ngot: %x\nwant: %x", actualContent, contentEnc)
|
|
|
|
}
|
2014-12-12 12:38:42 +02:00
|
|
|
}
|
2015-02-05 04:07:58 +02:00
|
|
|
return nil
|
2014-12-12 12:38:42 +02:00
|
|
|
}
|