aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/peer_test.go
blob: 76d856d3e0efa5f29424b7221f28996a91bb00d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package p2p

import (
    "bytes"
    "fmt"
    "io/ioutil"
    "net"
    "reflect"
    "sort"
    "testing"
    "time"

    "github.com/ethereum/go-ethereum/p2p/discover"
    "github.com/ethereum/go-ethereum/rlp"
)

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
            }
        }
    },
}

func testPeer(handshake bool, protos []Protocol) (*frameRW, *Peer, <-chan DiscReason) {
    conn1, conn2 := net.Pipe()
    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
}

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 {
            if err := expectMsg(rw, 2, []uint{1}); err != nil {
                t.Error(err)
            }
            if err := expectMsg(rw, 3, []uint{2}); err != nil {
                t.Error(err)
            }
            if err := expectMsg(rw, 4, []uint{3}); err != nil {
                t.Error(err)
            }
            close(done)
            return nil
        },
    }

    rw, peer, errc := testPeer(false, []Protocol{proto})
    defer rw.Close()
    peer.startSubprotocols([]Cap{proto.cap()})

    EncodeMsg(rw, baseProtocolLength+2, 1)
    EncodeMsg(rw, baseProtocolLength+3, 2)
    EncodeMsg(rw, baseProtocolLength+4, 3)

    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
        },
    }

    rw, peer, errc := testPeer(false, []Protocol{proto})
    defer rw.Close()
    peer.startSubprotocols([]Cap{proto.cap()})

    EncodeMsg(rw, 18, make([]byte, msgsize))
    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 {
            if err := EncodeMsg(rw, 2); err == nil {
                t.Error("expected error for out-of-range msg code, got nil")
            }
            if err := EncodeMsg(rw, 1, "foo", "bar"); err != nil {
                t.Errorf("write error: %v", err)
            }
            return nil
        },
    }
    rw, peer, _ := testPeer(false, []Protocol{proto})
    defer rw.Close()
    peer.startSubprotocols([]Cap{proto.cap()})

    if err := expectMsg(rw, 17, []string{"foo", "bar"}); err != nil {
        t.Error(err)
    }
}

func TestPeerWriteForBroadcast(t *testing.T) {
    defer testlog(t).detach()

    rw, peer, peerErr := testPeer(false, []Protocol{discard})
    defer rw.Close()
    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() {
        if err := expectMsg(rw, 16, nil); err != nil {
            t.Error()
        }
        close(read)
    }()

    // test successful write
    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)
    }
}

func TestPeerPing(t *testing.T) {
    defer testlog(t).detach()

    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)
    }
}

func TestPeerDisconnect(t *testing.T) {
    defer testlog(t).detach()

    rw, _, disc := testPeer(false, nil)
    defer rw.Close()
    if err := EncodeMsg(rw, discMsg, DiscQuitting); err != nil {
        t.Fatal(err)
    }
    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)
    }
}

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
    }
    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},
    }
    rw, p, disc := testPeer(true, protocols)
    p.remoteID = remote.ourID
    defer rw.Close()

    // 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)
    }

    // 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):
        }
    }
    sort.Strings(started)
    if !reflect.DeepEqual(started, []string{"a", "c"}) {
        t.Errorf("wrong protocols started: %v", started)
    }

    // check that metadata has been set
    if p.ID() != remoteID {
        t.Errorf("peer has wrong node ID: got %v, want %v", p.ID(), remoteID)
    }
    if p.Name() != remote.ourName {
        t.Errorf("peer has wrong node name: got %q, want %q", p.Name(), remote.ourName)
    }

    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)
    }
    if p.Name() != name {
        t.Errorf("Name mismatch: got %v, expected %v", p.Name(), name)
    }
    if !reflect.DeepEqual(p.Caps(), caps) {
        t.Errorf("Caps mismatch: got %v, expected %v", p.Caps(), caps)
    }

    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
    }
    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)
        }
    }
    return nil
}