forked from justusranvier/bmd
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpeer_test.go
468 lines (412 loc) · 14.9 KB
/
peer_test.go
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// Copyright (c) 2015 Monetas.
// Copyright 2016 Daniel Krawisz.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"math/rand"
"net"
"testing"
"time"
_ "github.com/DanielKrawisz/bmd/database/memdb"
"github.com/DanielKrawisz/bmd/peer"
"github.com/DanielKrawisz/bmd/objmgr/stats"
"github.com/DanielKrawisz/bmutil/wire"
"github.com/DanielKrawisz/bmutil/wire/obj"
)
// TestOutboundPeerHandshake tests the initial handshake for an outbound peer, ie,
// the real peer initiates the connection.
// Test cases:
// * Respond to a version message with a verack and then with a version.
// * Respond to a version message with a version and then a verack.
// * Error case: respond to a version with something other than verack/version.
// * Send two veracks. (not necessarily an error.)
func TestOutboundPeerHandshake(t *testing.T) {
// A channel for the mock peer to communicate with the test.
report := make(chan TestReport)
//testDone := make(chan struct{})
streams := []uint32{1}
nonce, _ := wire.RandomUint64()
var addrin, addrout *wire.NetAddress = wire.NewNetAddressIPPort(net.IPv4(5, 45, 99, 75), 8444, 1, 0),
wire.NewNetAddressIPPort(net.IPv4(5, 45, 99, 75), 8444, 1, 0)
msgAddr := wire.NewMsgAddr()
msgAddr.AddAddress(wire.NewNetAddressIPPort(net.IPv4(5, 45, 99, 75), 8444, 1, 0))
invVect := make([]*wire.InvVect, 10)
for i := 0; i < 10; i++ {
invVect[i] = (*wire.InvVect)(randomShaHash())
}
msgInv := &wire.MsgInv{InvList: invVect}
msgGetData := &wire.MsgGetData{InvList: invVect}
responses := []*PeerAction{
// Two possible ways of ordering the responses that are both valid.
&PeerAction{
Messages: []wire.Message{&wire.MsgVerAck{}, wire.NewMsgVersion(addrin, addrout, nonce, streams)},
},
&PeerAction{
Messages: []wire.Message{wire.NewMsgVersion(addrin, addrout, nonce, streams), &wire.MsgVerAck{}},
},
// Extra VerAcks are also ok.
&PeerAction{
Messages: []wire.Message{&wire.MsgVerAck{}, wire.NewMsgVersion(addrin, addrout, nonce, streams), &wire.MsgVerAck{}},
},
&PeerAction{
Messages: []wire.Message{&wire.MsgVerAck{}, &wire.MsgVerAck{}, wire.NewMsgVersion(addrin, addrout, nonce, streams)},
},
// Send a message that is not allowed at this time. Should result in a disconnect.
&PeerAction{
Messages: []wire.Message{msgAddr},
InteractionComplete: true,
DisconnectExpected: true,
},
&PeerAction{
Messages: []wire.Message{msgInv},
InteractionComplete: true,
DisconnectExpected: true,
},
&PeerAction{
Messages: []wire.Message{msgGetData},
InteractionComplete: true,
DisconnectExpected: true,
},
&PeerAction{
Messages: []wire.Message{testObj[0]},
InteractionComplete: true,
DisconnectExpected: true,
},
}
localAddr := &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8333}
remoteAddr := &net.TCPAddr{IP: net.ParseIP("192.168.0.1"), Port: 8333}
// A peer that establishes a handshake for outgoing peers.
handshakePeerBuilder := func(action *PeerAction) func(net.Addr, int64, int64) peer.Connection {
return func(addr net.Addr, maxDown, maxUp int64) peer.Connection {
return NewMockPeer(localAddr, remoteAddr, report,
NewOutboundHandshakePeerTester(action, msgAddr))
}
}
permament := []string{"5.45.99.75:8444"}
for testCase, response := range responses {
defer resetCfg(cfg)()
NewConn = handshakePeerBuilder(response)
// Create server and start it.
listeners := []string{net.JoinHostPort("", "8445")}
serv, err := newServer(listeners, getMemDb([]obj.Object{}),
MockListen([]*MockListener{
NewMockListener(localAddr, make(chan peer.Connection), make(chan struct{}, 1))}),
permament, stats.Stats{})
if err != nil {
t.Fatalf("Server failed to start: %s", err)
}
serv.Start()
msg := <-report
if msg.Err != nil {
t.Errorf("error case %d: %s", testCase, msg)
}
serv.Stop()
serv.WaitForShutdown()
}
NewConn = peer.NewConnection
}
// Test cases:
// * Send a version message and get a verack and version message in return.
// * Error cases: open with a verack and some other kind of message.
// * Error case: respond to a verack with something other than verack/version.
// * Error case: send two versions.
// * Error case: send a version message with a version < 3.
// * Send a version message with a version higher than three. The peer should not disconnect.
func TestInboundPeerHandshake(t *testing.T) {
// A channel for the mock peer to communicate with the test.
report := make(chan TestReport)
// A channel to make the fake incomming connection.
incoming := make(chan peer.Connection)
streams := []uint32{1}
nonce, _ := wire.RandomUint64()
var addrin, addrout *wire.NetAddress = wire.NewNetAddressIPPort(net.IPv4(5, 45, 99, 75), 8444, 1, 0),
wire.NewNetAddressIPPort(net.IPv4(5, 45, 99, 75), 8444, 1, 0)
localAddr := &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8333}
remoteAddr := &net.TCPAddr{IP: net.ParseIP("192.168.0.1"), Port: 8333}
msgAddr := wire.NewMsgAddr()
msgAddr.AddAddress(wire.NewNetAddressIPPort(net.IPv4(5, 45, 99, 75), 8444, 1, 0))
addrAction := &PeerAction{
Messages: []wire.Message{msgAddr},
InteractionComplete: true,
DisconnectExpected: false,
}
incorrectVersion := wire.NewMsgVersion(addrin, addrout, nonce, streams)
incorrectVersion.ProtocolVersion = int32(2)
futureVersion := wire.NewMsgVersion(addrin, addrout, nonce, streams)
futureVersion.ProtocolVersion = int32(4)
// The four test cases are all in this list.
openingMsg := []*PeerAction{
&PeerAction{
Messages: []wire.Message{wire.NewMsgVersion(addrin, addrout, nonce, streams)},
},
&PeerAction{
Messages: []wire.Message{&wire.MsgVerAck{}},
InteractionComplete: true,
DisconnectExpected: true,
},
&PeerAction{
Messages: []wire.Message{msgAddr},
InteractionComplete: true,
DisconnectExpected: true,
},
&PeerAction{
Messages: []wire.Message{wire.NewMsgVersion(addrin, addrout, nonce, streams), wire.NewMsgVersion(addrin, addrout, nonce, streams)},
InteractionComplete: true,
DisconnectExpected: true,
},
&PeerAction{
Messages: []wire.Message{incorrectVersion},
InteractionComplete: true,
DisconnectExpected: true,
},
&PeerAction{
Messages: []wire.Message{futureVersion},
InteractionComplete: true,
DisconnectExpected: false,
},
}
for testCase, open := range openingMsg {
defer resetCfg(cfg)()
// Create server and start it.
listeners := []string{net.JoinHostPort("", "8445")}
var err error
serv, err := newServer(listeners, getMemDb([]obj.Object{}),
MockListen([]*MockListener{
NewMockListener(localAddr, incoming, make(chan struct{}))}),
nil, stats.Stats{})
if err != nil {
t.Fatalf("Server failed to start: %s", err)
}
serv.Start()
// Test handshake.
incoming <- NewMockPeer(localAddr, remoteAddr, report,
NewInboundHandshakePeerTester(open, addrAction))
msg := <-report
if msg.Err != nil {
t.Errorf("error case %d: %s", testCase, msg)
}
serv.Stop()
serv.WaitForShutdown()
}
}
// Test cases
// * after a successful handshake, get an addr and receive one.
// * error case: send an addr message that is too big.
// * Give the peer no addresses to send.
func TestProcessAddr(t *testing.T) {
// Process a handshake. This should be an incoming peer.
// Send an addr and receive an addr.
// Ignore inv messages.
// A channel for the mock peer to communicate with the test.
report := make(chan TestReport)
// A channel to make the fake incomming connection.
incoming := make(chan peer.Connection)
srcAddr := &wire.NetAddress{
Timestamp: time.Now(),
Services: wire.SFNodeNetwork,
IP: net.ParseIP("173.144.173.111"),
Port: 8333,
}
// Some parameters for the test sequence.
streams := []uint32{1}
nonce, _ := wire.RandomUint64()
var addrin, addrout *wire.NetAddress = wire.NewNetAddressIPPort(net.IPv4(5, 45, 99, 75), 8444, 1, 0),
wire.NewNetAddressIPPort(net.IPv4(5, 45, 99, 75), 8444, 1, 0)
localAddr := &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8333}
remoteAddr := &net.TCPAddr{IP: net.ParseIP("192.168.0.1"), Port: 8333}
// Some addr messages to use for testing.
addrMsg := wire.NewMsgAddr()
addrMsg.AddAddress(wire.NewNetAddressIPPort(net.IPv4(5, 45, 99, 75), 8444, 1, 0))
// Maximum number of addresses allowed in an addr message is 1000, so we add 1001.
addrMsgTooLong := wire.NewMsgAddr()
for i := 0; i <= 1001; i++ {
addrMsgTooLong.AddAddress(&wire.NetAddress{
Timestamp: time.Now(),
Services: wire.SFNodeNetwork,
IP: net.IPv4(
byte(rand.Intn(256)),
byte(rand.Intn(256)),
byte(rand.Intn(256)),
byte(rand.Intn(256))),
Port: 8333,
})
}
AddrTests := []struct {
AddrAction *PeerAction // Action for the mock peer to take upon handshake completion.
NumAddrs int // Number of addresses to put in the address manager.
}{
{
&PeerAction{
Messages: []wire.Message{addrMsg},
},
25,
},
{
&PeerAction{
Messages: []wire.Message{addrMsgTooLong},
InteractionComplete: true,
DisconnectExpected: true,
},
25,
},
{
&PeerAction{InteractionComplete: true},
0,
},
}
for testCase, addrTest := range AddrTests {
defer resetCfg(cfg)()
// Add some addresses to the address manager.
addrs := make([]*wire.NetAddress, addrTest.NumAddrs)
// Create server and start it.
listeners := []string{net.JoinHostPort("", "8445")}
serv, err := newServer(listeners, getMemDb([]obj.Object{}),
MockListen([]*MockListener{
NewMockListener(localAddr, incoming, make(chan struct{}))}),
nil, stats.Stats{})
if err != nil {
t.Fatal("Server failed to start.")
}
serv.Start()
for i := 0; i < addrTest.NumAddrs; i++ {
s := fmt.Sprintf("%d.173.147.%d:8333", i/64+60, i%64+60)
addrs[i], _ = serv.addrManager.DeserializeNetAddress(s)
}
serv.addrManager.AddAddresses(addrs, srcAddr)
mockConn := NewMockPeer(localAddr, remoteAddr, report,
NewInboundHandshakePeerTester(
&PeerAction{Messages: []wire.Message{wire.NewMsgVersion(addrin, addrout, nonce, streams)}},
addrTest.AddrAction))
incoming <- mockConn
msg := <-report
if msg.Err != nil {
t.Errorf("error case %d: %s", testCase, msg)
}
serv.Stop()
serv.WaitForShutdown()
}
}
// Test cases
// * assume handshake already successful. Get an inv and receive one. request an object
// from the peer and receive a request for something that the mock peer has. Send
// and receive responses for the requests. Several cases of this scenario:
// * The peer already has everything that the mock peer has (no inv expected).
// * The peer has some of what the mock peer has, but not everything.
// * The peer needs to send more than one getData request.
// * error case: send an inv message that is too big (peer should disconnect).
// * error case: send a request for an object that the peer does not have (peer should not disconnect).
// * error case: return an object that was not requested (peer should disconnect).
func TestProcessInvAndObjectExchange(t *testing.T) {
// Send an inv and receive an inv.
// Process a request for an object.
// Send a request and receive an object.
// Ignore addr messages.
tooLongInvVect := make([]*wire.InvVect, wire.MaxInvPerMsg+1)
for i := 0; i < wire.MaxInvPerMsg+1; i++ {
tooLongInvVect[i] = (*wire.InvVect)(randomShaHash())
}
TooLongInv := &wire.MsgInv{InvList: tooLongInvVect}
tests := []struct {
peerDB []obj.Object // The messages already in the peer's db.
mockDB []obj.Object // The messages that are already in the
// Action for the mock peer to take upon receiving an inv. If this is
// nil, then an appropriate action is constructed.
invAction *PeerAction
}{
{ // Nobody has any inv in this test case!
[]obj.Object{},
[]obj.Object{},
&PeerAction{
Messages: []wire.Message{&wire.MsgVerAck{}},
InteractionComplete: true},
},
{ // Send empty inv and should be disconnected.
[]obj.Object{},
[]obj.Object{},
&PeerAction{
Messages: []wire.Message{&wire.MsgVerAck{}, wire.NewMsgInv()},
InteractionComplete: true,
DisconnectExpected: true},
},
{ // Only the real peer should request data.
[]obj.Object{},
[]obj.Object{testObj[0], testObj[2], testObj[4], testObj[6], testObj[8]},
nil,
},
{ // Neither peer should request data.
[]obj.Object{testObj[0], testObj[2], testObj[4], testObj[6], testObj[8]},
[]obj.Object{testObj[0], testObj[2], testObj[4], testObj[6], testObj[8]},
nil,
},
{ // Only the mock peer should request data.
[]obj.Object{testObj[0], testObj[2], testObj[4], testObj[6], testObj[8]},
[]obj.Object{},
nil,
},
{ // The peers have no data in common, so they should both ask for everything of the other.
[]obj.Object{testObj[1], testObj[3], testObj[5], testObj[7], testObj[9]},
[]obj.Object{testObj[0], testObj[2], testObj[4], testObj[6], testObj[8]},
nil,
},
{ // The peers have some data in common.
[]obj.Object{testObj[0], testObj[3], testObj[5], testObj[7], testObj[9]},
[]obj.Object{testObj[0], testObj[2], testObj[4], testObj[6], testObj[8]},
nil,
},
{
[]obj.Object{},
[]obj.Object{},
&PeerAction{
Messages: []wire.Message{&wire.MsgVerAck{}, TooLongInv},
DisconnectExpected: true,
InteractionComplete: true},
},
}
// A channel for the mock peer to communicate with the test.
report := make(chan TestReport)
// A channel to make the fake incomming connection.
incoming := make(chan peer.Connection)
// Some parameters for the test sequence.
localAddr := &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8333}
remoteAddr := &net.TCPAddr{IP: net.ParseIP("192.168.0.1"), Port: 8333}
addrout, _ := wire.NewNetAddress(remoteAddr, 1, 0)
for testCase, test := range tests {
defer resetCfg(cfg)()
t.Log("Test case", testCase)
// Define the objects that will go in the database.
// Create server and start it.
listeners := []string{net.JoinHostPort("", "8445")}
db := getMemDb(test.peerDB)
serv, err := newServer(listeners, db,
MockListen([]*MockListener{
NewMockListener(localAddr, incoming, make(chan struct{}))}),
nil, stats.Stats{})
if err != nil {
t.Fatal("Server failed to start.")
}
serv.Start()
mockConn := NewMockPeer(localAddr, remoteAddr, report,
NewDataExchangePeerTester(test.mockDB, test.peerDB, test.invAction))
mockSend := NewMockSend(mockConn)
inventory := peer.NewInventory()
serv.handleAddPeerMsg(peer.NewPeerHandshakeComplete(
serv, mockConn, inventory, mockSend, addrout), 0)
var msg TestReport
msg = <-report
if msg.Err != nil {
t.Errorf("error case %d: %s", testCase, msg)
}
serv.Stop()
serv.WaitForShutdown()
// Check if the data sent is actually in the peer's database.
if msg.DataSent != nil {
for _, hash := range msg.DataSent {
if ok, _ := db.ExistsObject(hash); !ok {
t.Error("test case ", testCase, ": Object", *hash, "not found in database.")
}
}
}
}
}