4
4
"context"
5
5
"fmt"
6
6
"reflect"
7
+ "sync/atomic"
7
8
"testing"
8
9
"time"
9
10
@@ -12,6 +13,7 @@ import (
12
13
"github.com/libp2p/go-libp2p/core/peer"
13
14
"github.com/libp2p/go-libp2p/core/peerstore"
14
15
bhost "github.com/libp2p/go-libp2p/p2p/host/blank"
16
+ "github.com/libp2p/go-libp2p/p2p/host/eventbus"
15
17
swarmt "github.com/libp2p/go-libp2p/p2p/net/swarm/testing"
16
18
"github.com/libp2p/go-libp2p/p2p/protocol/autonatv2/pbv2"
17
19
@@ -22,14 +24,17 @@ import (
22
24
23
25
func newAutoNAT (t * testing.T , dialer host.Host , opts ... AutoNATOption ) * AutoNAT {
24
26
t .Helper ()
25
- h := bhost .NewBlankHost (swarmt .GenSwarm (t ))
27
+ b := eventbus .NewBus ()
28
+ h := bhost .NewBlankHost (swarmt .GenSwarm (t , swarmt .EventBus (b )), bhost .WithEventBus (b ))
26
29
if dialer == nil {
27
30
dialer = bhost .NewBlankHost (swarmt .GenSwarm (t ))
28
31
}
29
32
an , err := New (h , dialer , opts ... )
30
33
if err != nil {
31
34
t .Error (err )
32
35
}
36
+ an .srv .Enable ()
37
+ an .cli .Register ()
33
38
return an
34
39
}
35
40
@@ -47,31 +52,28 @@ func parseAddrs(t *testing.T, msg *pbv2.Message) []ma.Multiaddr {
47
52
return addrs
48
53
}
49
54
50
- func TestValidPeer (t * testing.T ) {
51
- an := newAutoNAT (t , nil )
52
- require .Equal (t , an .validPeer (), peer .ID ("" ))
53
- an .host .Peerstore ().AddAddr ("peer1" , ma .StringCast ("/ip4/127.0.0.1/tcp/1" ), peerstore .PermanentAddrTTL )
54
- an .host .Peerstore ().AddAddr ("peer2" , ma .StringCast ("/ip4/127.0.0.1/tcp/2" ), peerstore .PermanentAddrTTL )
55
- require .NoError (t , an .host .Peerstore ().AddProtocols ("peer1" , DialProtocol ))
56
- require .NoError (t , an .host .Peerstore ().AddProtocols ("peer2" , DialProtocol ))
57
-
58
- var got1 , got2 bool
59
- for i := 0 ; i < 100 ; i ++ {
60
- p := an .validPeer ()
61
- switch p {
62
- case peer .ID ("peer1" ):
63
- got1 = true
64
- case peer .ID ("peer2" ):
65
- got2 = true
66
- default :
67
- t .Fatalf ("invalid peer: %s" , p )
68
- }
69
- if got1 && got2 {
70
- break
71
- }
72
- }
73
- require .True (t , got1 )
74
- require .True (t , got2 )
55
+ func idAndConnect (t * testing.T , a , b host.Host ) {
56
+ a .Peerstore ().AddAddrs (b .ID (), b .Addrs (), peerstore .PermanentAddrTTL )
57
+ a .Peerstore ().AddProtocols (b .ID (), DialProtocol )
58
+
59
+ err := a .Connect (context .Background (), peer.AddrInfo {ID : b .ID ()})
60
+ require .NoError (t , err )
61
+ }
62
+
63
+ // waitForPeer waits for a to process all peer events
64
+ func waitForPeer (t * testing.T , a * AutoNAT ) {
65
+ t .Helper ()
66
+ require .Eventually (t , func () bool {
67
+ a .mx .Lock ()
68
+ defer a .mx .Unlock ()
69
+ return len (a .peers ) > 0
70
+ }, 5 * time .Second , 100 * time .Millisecond )
71
+ }
72
+
73
+ // identify provides server address and protocol to client
74
+ func identify (t * testing.T , cli * AutoNAT , srv * AutoNAT ) {
75
+ idAndConnect (t , cli .host , srv .host )
76
+ waitForPeer (t , cli )
75
77
}
76
78
77
79
func TestAutoNATPrivateAddr (t * testing.T ) {
@@ -82,19 +84,24 @@ func TestAutoNATPrivateAddr(t *testing.T) {
82
84
}
83
85
84
86
func TestClientRequest (t * testing.T ) {
85
- an := newAutoNAT (t , nil )
87
+ an := newAutoNAT (t , nil , allowAll )
86
88
87
89
addrs := an .host .Addrs ()
88
90
91
+ var gotReq atomic.Bool
89
92
p := bhost .NewBlankHost (swarmt .GenSwarm (t ))
90
93
p .SetStreamHandler (DialProtocol , func (s network.Stream ) {
94
+ gotReq .Store (true )
91
95
r := pbio .NewDelimitedReader (s , maxMsgSize )
92
96
var msg pbv2.Message
93
- err := r .ReadMsg (& msg )
94
- if err != nil {
97
+ if err := r .ReadMsg (& msg ); err != nil {
95
98
t .Error (err )
99
+ return
100
+ }
101
+ if msg .GetDialRequest () == nil {
102
+ t .Errorf ("expected message to be of type DialRequest, got %T" , msg .Msg )
103
+ return
96
104
}
97
- require .NotNil (t , msg .GetDialRequest ())
98
105
addrsb := make ([][]byte , len (addrs ))
99
106
for i := 0 ; i < len (addrs ); i ++ {
100
107
addrsb [i ] = addrs [i ].Bytes ()
@@ -105,20 +112,23 @@ func TestClientRequest(t *testing.T) {
105
112
s .Reset ()
106
113
})
107
114
108
- an .host .Peerstore ().AddAddrs (p .ID (), p .Addrs (), peerstore .TempAddrTTL )
109
- an .host .Peerstore ().AddProtocols (p .ID (), DialProtocol )
115
+ idAndConnect (t , an .host , p )
116
+ waitForPeer (t , an )
117
+
110
118
res , err := an .CheckReachability (context .Background (), addrs [:1 ], addrs [1 :])
111
119
require .Nil (t , res )
112
120
require .NotNil (t , err )
121
+ require .True (t , gotReq .Load ())
113
122
}
114
123
115
124
func TestClientServerError (t * testing.T ) {
116
125
an := newAutoNAT (t , nil , allowAll )
117
126
addrs := an .host .Addrs ()
118
127
119
128
p := bhost .NewBlankHost (swarmt .GenSwarm (t ))
120
- an .host .Peerstore ().AddAddrs (p .ID (), p .Addrs (), peerstore .PermanentAddrTTL )
121
- an .host .Peerstore ().AddProtocols (p .ID (), DialProtocol )
129
+ idAndConnect (t , an .host , p )
130
+ waitForPeer (t , an )
131
+
122
132
done := make (chan bool )
123
133
tests := []struct {
124
134
handler func (network.Stream )
@@ -163,8 +173,9 @@ func TestClientDataRequest(t *testing.T) {
163
173
addrs := an .host .Addrs ()
164
174
165
175
p := bhost .NewBlankHost (swarmt .GenSwarm (t ))
166
- an .host .Peerstore ().AddAddrs (p .ID (), p .Addrs (), peerstore .PermanentAddrTTL )
167
- an .host .Peerstore ().AddProtocols (p .ID (), DialProtocol )
176
+ idAndConnect (t , an .host , p )
177
+ waitForPeer (t , an )
178
+
168
179
done := make (chan bool )
169
180
tests := []struct {
170
181
handler func (network.Stream )
@@ -234,9 +245,8 @@ func TestClientDialAttempts(t *testing.T) {
234
245
addrs := an .host .Addrs ()
235
246
236
247
p := bhost .NewBlankHost (swarmt .GenSwarm (t ))
237
- an .host .Peerstore ().AddAddrs (p .ID (), p .Addrs (), peerstore .PermanentAddrTTL )
238
- an .host .Peerstore ().AddProtocols (p .ID (), DialProtocol )
239
- an .cli .Register ()
248
+ idAndConnect (t , an .host , p )
249
+ waitForPeer (t , an )
240
250
241
251
tests := []struct {
242
252
handler func (network.Stream )
@@ -419,3 +429,41 @@ func TestClientDialAttempts(t *testing.T) {
419
429
})
420
430
}
421
431
}
432
+
433
+ func TestEventSubscription (t * testing.T ) {
434
+ an := newAutoNAT (t , nil )
435
+ defer an .host .Close ()
436
+
437
+ b := bhost .NewBlankHost (swarmt .GenSwarm (t ))
438
+ defer b .Close ()
439
+ c := bhost .NewBlankHost (swarmt .GenSwarm (t ))
440
+ defer c .Close ()
441
+
442
+ idAndConnect (t , an .host , b )
443
+ require .Eventually (t , func () bool {
444
+ an .mx .Lock ()
445
+ defer an .mx .Unlock ()
446
+ return len (an .peers ) == 1
447
+ }, 5 * time .Second , 100 * time .Millisecond )
448
+
449
+ idAndConnect (t , an .host , c )
450
+ require .Eventually (t , func () bool {
451
+ an .mx .Lock ()
452
+ defer an .mx .Unlock ()
453
+ return len (an .peers ) == 2
454
+ }, 5 * time .Second , 100 * time .Millisecond )
455
+
456
+ an .host .Network ().ClosePeer (b .ID ())
457
+ require .Eventually (t , func () bool {
458
+ an .mx .Lock ()
459
+ defer an .mx .Unlock ()
460
+ return len (an .peers ) == 1
461
+ }, 5 * time .Second , 100 * time .Millisecond )
462
+
463
+ an .host .Network ().ClosePeer (c .ID ())
464
+ require .Eventually (t , func () bool {
465
+ an .mx .Lock ()
466
+ defer an .mx .Unlock ()
467
+ return len (an .peers ) == 0
468
+ }, 5 * time .Second , 100 * time .Millisecond )
469
+ }
0 commit comments