/
obsaddr_test.go
439 lines (372 loc) · 11.3 KB
/
obsaddr_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
package identify_test
import (
"context"
"sync"
"testing"
"time"
detectrace "github.com/ipfs/go-detect-race"
"github.com/libp2p/go-eventbus"
"github.com/libp2p/go-libp2p-core/event"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
p2putil "github.com/libp2p/go-libp2p-netutil"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
identify "github.com/libp2p/go-libp2p/p2p/protocol/identify"
ma "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
)
type harness struct {
t *testing.T
mocknet mocknet.Mocknet
host host.Host
oas *identify.ObservedAddrManager
}
func (h *harness) add(observer ma.Multiaddr) peer.ID {
// create a new fake peer.
sk, err := p2putil.RandTestBogusPrivateKey()
if err != nil {
h.t.Fatal(err)
}
h2, err := h.mocknet.AddPeer(sk, observer)
if err != nil {
h.t.Fatal(err)
}
_, err = h.mocknet.LinkPeers(h.host.ID(), h2.ID())
if err != nil {
h.t.Fatal(err)
}
return h2.ID()
}
func (h *harness) conn(observer peer.ID) network.Conn {
c, err := h.mocknet.ConnectPeers(h.host.ID(), observer)
if err != nil {
h.t.Fatal(err)
}
if c.Stat().Direction != network.DirOutbound {
h.t.Fatal("expected conn direction to be outbound")
}
return c
}
func (h *harness) connInbound(observer peer.ID) network.Conn {
c, err := h.mocknet.ConnectPeers(observer, h.host.ID())
if err != nil {
h.t.Fatal(err)
}
c = mocknet.ConnComplement(c)
if c.Stat().Direction != network.DirInbound {
h.t.Fatal("expected conn direction to be inbound")
}
return c
}
func (h *harness) observe(observed ma.Multiaddr, observer peer.ID) network.Conn {
c := h.conn(observer)
h.oas.Record(c, observed)
time.Sleep(50 * time.Millisecond) // let the worker run
return c
}
func (h *harness) observeInbound(observed ma.Multiaddr, observer peer.ID) network.Conn {
c := h.connInbound(observer)
h.oas.Record(c, observed)
time.Sleep(50 * time.Millisecond) // let the worker run
return c
}
func newHarness(ctx context.Context, t *testing.T) harness {
mn := mocknet.New(ctx)
sk, err := p2putil.RandTestBogusPrivateKey()
if err != nil {
t.Fatal(err)
}
h, err := mn.AddPeer(sk, ma.StringCast("/ip4/127.0.0.1/tcp/10086"))
if err != nil {
t.Fatal(err)
}
oas, err := identify.NewObservedAddrManager(ctx, h)
require.NoError(t, err)
return harness{
oas: oas,
mocknet: mn,
host: h,
t: t,
}
}
// TestObsAddrSet
func TestObsAddrSet(t *testing.T) {
m := func(s string) ma.Multiaddr {
m, err := ma.NewMultiaddr(s)
if err != nil {
t.Error(err)
}
return m
}
addrsMatch := func(a, b []ma.Multiaddr) bool {
if len(a) != len(b) {
return false
}
for _, aa := range a {
found := false
for _, bb := range b {
if aa.Equal(bb) {
found = true
break
}
}
if !found {
return false
}
}
return true
}
a1 := m("/ip4/1.2.3.4/tcp/1231")
a2 := m("/ip4/1.2.3.4/tcp/1232")
a3 := m("/ip4/1.2.3.4/tcp/1233")
a4 := m("/ip4/1.2.3.4/tcp/1234")
a5 := m("/ip4/1.2.3.4/tcp/1235")
b1 := m("/ip4/1.2.3.6/tcp/1236")
b2 := m("/ip4/1.2.3.7/tcp/1237")
b3 := m("/ip4/1.2.3.8/tcp/1237")
b4 := m("/ip4/1.2.3.9/tcp/1237")
b5 := m("/ip4/1.2.3.10/tcp/1237")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
harness := newHarness(ctx, t)
if !addrsMatch(harness.oas.Addrs(), nil) {
t.Error("addrs should be empty")
}
pa4 := harness.add(a4)
pa5 := harness.add(a5)
pb1 := harness.add(b1)
pb2 := harness.add(b2)
pb3 := harness.add(b3)
pb4 := harness.add(b4)
pb5 := harness.add(b5)
harness.observe(a1, pa4)
harness.observe(a2, pa4)
harness.observe(a3, pa4)
// these are all different so we should not yet get them.
if !addrsMatch(harness.oas.Addrs(), nil) {
t.Error("addrs should _still_ be empty (once)")
}
// same observer, so should not yet get them.
harness.observe(a1, pa4)
harness.observe(a2, pa4)
harness.observe(a3, pa4)
if !addrsMatch(harness.oas.Addrs(), nil) {
t.Error("addrs should _still_ be empty (same obs)")
}
// different observer, but same observer group.
harness.observe(a1, pa5)
harness.observe(a2, pa5)
harness.observe(a3, pa5)
if !addrsMatch(harness.oas.Addrs(), nil) {
t.Error("addrs should _still_ be empty (same obs group)")
}
harness.observe(a1, pb1)
harness.observe(a1, pb2)
harness.observe(a1, pb3)
if !addrsMatch(harness.oas.Addrs(), []ma.Multiaddr{a1}) {
t.Error("addrs should only have a1")
}
harness.observe(a2, pa5)
harness.observe(a1, pa5)
harness.observe(a1, pa5)
harness.observe(a2, pb1)
harness.observe(a1, pb1)
harness.observe(a1, pb1)
harness.observe(a2, pb2)
harness.observe(a1, pb2)
harness.observe(a1, pb2)
harness.observe(a2, pb4)
harness.observe(a2, pb5)
if !addrsMatch(harness.oas.Addrs(), []ma.Multiaddr{a1, a2}) {
t.Error("addrs should only have a1, a2")
}
// force a refresh.
harness.oas.SetTTL(time.Millisecond * 200)
time.Sleep(time.Millisecond * 300)
if !addrsMatch(harness.oas.Addrs(), []ma.Multiaddr{a1, a2}) {
t.Errorf("addrs should only have %s, %s; have %s", a1, a2, harness.oas.Addrs())
}
// disconnect from all but b5.
for _, p := range harness.host.Network().Peers() {
if p == pb5 {
continue
}
harness.host.Network().ClosePeer(p)
}
// wait for all other addresses to time out.
time.Sleep(time.Millisecond * 300)
// Should still have a2
if !addrsMatch(harness.oas.Addrs(), []ma.Multiaddr{a2}) {
t.Error("should only have a2, have: ", harness.oas.Addrs())
}
harness.host.Network().ClosePeer(pb5)
// wait for all addresses to timeout
time.Sleep(time.Millisecond * 400)
// Should still have a2
if !addrsMatch(harness.oas.Addrs(), nil) {
t.Error("addrs should have timed out")
}
}
func TestObservedAddrFiltering(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
harness := newHarness(ctx, t)
require.Empty(t, harness.oas.Addrs())
// IP4/TCP
it1 := ma.StringCast("/ip4/1.2.3.4/tcp/1231")
it2 := ma.StringCast("/ip4/1.2.3.4/tcp/1232")
it3 := ma.StringCast("/ip4/1.2.3.4/tcp/1233")
it4 := ma.StringCast("/ip4/1.2.3.4/tcp/1234")
it5 := ma.StringCast("/ip4/1.2.3.4/tcp/1235")
it6 := ma.StringCast("/ip4/1.2.3.4/tcp/1236")
it7 := ma.StringCast("/ip4/1.2.3.4/tcp/1237")
// observers
b1 := ma.StringCast("/ip4/1.2.3.6/tcp/1236")
b2 := ma.StringCast("/ip4/1.2.3.7/tcp/1237")
b3 := ma.StringCast("/ip4/1.2.3.8/tcp/1237")
b4 := ma.StringCast("/ip4/1.2.3.9/tcp/1237")
b5 := ma.StringCast("/ip4/1.2.3.10/tcp/1237")
b6 := ma.StringCast("/ip4/1.2.3.11/tcp/1237")
b7 := ma.StringCast("/ip4/1.2.3.12/tcp/1237")
// These are all observers in the same group.
b8 := ma.StringCast("/ip4/1.2.3.13/tcp/1237")
b9 := ma.StringCast("/ip4/1.2.3.13/tcp/1238")
b10 := ma.StringCast("/ip4/1.2.3.13/tcp/1239")
peers := []peer.ID{
harness.add(b1),
harness.add(b2),
harness.add(b3),
harness.add(b4),
harness.add(b5),
harness.add(b6),
harness.add(b7),
harness.add(b8),
harness.add(b9),
harness.add(b10),
}
for i := 0; i < 4; i++ {
harness.observe(it1, peers[i])
harness.observe(it2, peers[i])
harness.observe(it3, peers[i])
harness.observe(it4, peers[i])
harness.observe(it5, peers[i])
harness.observe(it6, peers[i])
harness.observe(it7, peers[i])
}
harness.observe(it1, peers[4])
harness.observe(it7, peers[4])
addrs := harness.oas.Addrs()
require.Len(t, addrs, 2)
require.Contains(t, addrs, it1)
require.Contains(t, addrs, it7)
// Bump the number of observations so 1 & 7 have 7 observations.
harness.observe(it1, peers[5])
harness.observe(it1, peers[6])
harness.observe(it7, peers[5])
harness.observe(it7, peers[6])
// Add an observation from IP 1.2.3.13
// 2 & 3 now have 5 observations
harness.observe(it2, peers[7])
harness.observe(it3, peers[7])
addrs = harness.oas.Addrs()
require.Len(t, addrs, 2)
require.Contains(t, addrs, it1)
require.Contains(t, addrs, it7)
// Add an inbound observation from IP 1.2.3.13, it should override the
// existing observation and it should make these addresses win even
// though we have fewer observations.
//
// 2 & 3 now have 6 observations.
harness.observeInbound(it2, peers[8])
harness.observeInbound(it3, peers[8])
addrs = harness.oas.Addrs()
require.Len(t, addrs, 2)
require.Contains(t, addrs, it2)
require.Contains(t, addrs, it3)
// Adding an outbound observation shouldn't "downgrade" it.
//
// 2 & 3 now have 7 observations.
harness.observe(it2, peers[9])
harness.observe(it3, peers[9])
addrs = harness.oas.Addrs()
require.Len(t, addrs, 2)
require.Contains(t, addrs, it2)
require.Contains(t, addrs, it3)
}
func TestEmitNATDeviceTypeSymmetric(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
harness := newHarness(ctx, t)
require.Empty(t, harness.oas.Addrs())
emitter, err := harness.host.EventBus().Emitter(new(event.EvtLocalReachabilityChanged), eventbus.Stateful)
require.NoError(t, err)
require.NoError(t, emitter.Emit(event.EvtLocalReachabilityChanged{Reachability: network.ReachabilityPrivate}))
// TCP
it1 := ma.StringCast("/ip4/1.2.3.4/tcp/1231")
it2 := ma.StringCast("/ip4/1.2.3.4/tcp/1232")
it3 := ma.StringCast("/ip4/1.2.3.4/tcp/1233")
it4 := ma.StringCast("/ip4/1.2.3.4/tcp/1234")
// observers
b1 := ma.StringCast("/ip4/1.2.3.6/tcp/1236")
b2 := ma.StringCast("/ip4/1.2.3.7/tcp/1237")
b3 := ma.StringCast("/ip4/1.2.3.8/tcp/1237")
b4 := ma.StringCast("/ip4/1.2.3.9/tcp/1237")
peers := []peer.ID{
harness.add(b1),
harness.add(b2),
harness.add(b3),
harness.add(b4),
}
harness.observe(it1, peers[0])
harness.observe(it2, peers[1])
harness.observe(it3, peers[2])
harness.observe(it4, peers[3])
sub, err := harness.host.EventBus().Subscribe(new(event.EvtNATDeviceTypeChanged))
require.NoError(t, err)
select {
case ev := <-sub.Out():
evt := ev.(event.EvtNATDeviceTypeChanged)
require.Equal(t, network.NATDeviceTypeSymmetric, evt.NatDeviceType)
require.Equal(t, network.NATTransportTCP, evt.TransportProtocol)
case <-time.After(5 * time.Second):
t.Fatal("did not get Symmetric NAT event")
}
}
func TestEmitNATDeviceTypeCone(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
harness := newHarness(ctx, t)
require.Empty(t, harness.oas.Addrs())
emitter, err := harness.host.EventBus().Emitter(new(event.EvtLocalReachabilityChanged), eventbus.Stateful)
require.NoError(t, err)
require.NoError(t, emitter.Emit(event.EvtLocalReachabilityChanged{Reachability: network.ReachabilityPrivate}))
it1 := ma.StringCast("/ip4/1.2.3.4/tcp/1231")
it2 := ma.StringCast("/ip4/1.2.3.4/tcp/1231")
it3 := ma.StringCast("/ip4/1.2.3.4/tcp/1231")
it4 := ma.StringCast("/ip4/1.2.3.4/tcp/1231")
// observers
b1 := ma.StringCast("/ip4/1.2.3.6/tcp/1236")
b2 := ma.StringCast("/ip4/1.2.3.7/tcp/1237")
b3 := ma.StringCast("/ip4/1.2.3.8/tcp/1237")
b4 := ma.StringCast("/ip4/1.2.3.9/tcp/1237")
peers := []peer.ID{
harness.add(b1),
harness.add(b2),
harness.add(b3),
harness.add(b4),
}
harness.observe(it1, peers[0])
harness.observe(it2, peers[1])
harness.observe(it3, peers[2])
harness.observe(it4, peers[3])
sub, err := harness.host.EventBus().Subscribe(new(event.EvtNATDeviceTypeChanged))
require.NoError(t, err)
select {
case ev := <-sub.Out():
evt := ev.(event.EvtNATDeviceTypeChanged)
require.Equal(t, network.NATDeviceTypeCone, evt.NatDeviceType)
case <-time.After(5 * time.Second):
t.Fatal("did not get Cone NAT event")
}
}