-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathgating_test.go
286 lines (258 loc) · 9.86 KB
/
gating_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
package connectiongating
import (
"context"
"fmt"
"testing"
"time"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/libp2p/go-libp2p/p2p/net/swarm"
"github.com/golang/mock/gomock"
ma "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
)
//go:generate go run github.com/golang/mock/mockgen -package connectiongating -destination mock_connection_gater_test.go github.com/libp2p/go-libp2p/core/connmgr ConnectionGater
// This list should contain (at least) one address for every transport we have.
var addrs = []ma.Multiaddr{
ma.StringCast("/ip4/127.0.0.1/tcp/0"),
ma.StringCast("/ip4/127.0.0.1/tcp/0/ws"),
ma.StringCast("/ip4/127.0.0.1/udp/0/quic"),
ma.StringCast("/ip4/127.0.0.1/udp/0/quic-v1"),
ma.StringCast("/ip4/127.0.0.1/udp/0/quic-v1/webtransport"),
}
func transportName(a ma.Multiaddr) string {
_, tr := ma.SplitLast(a)
return tr.Protocol().Name
}
func stripCertHash(addr ma.Multiaddr) ma.Multiaddr {
for {
if _, err := addr.ValueForProtocol(ma.P_CERTHASH); err != nil {
break
}
addr, _ = ma.SplitLast(addr)
}
return addr
}
func TestInterceptPeerDial(t *testing.T) {
for _, a := range addrs {
t.Run(fmt.Sprintf("dialing %s", transportName(a)), func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
connGater := NewMockConnectionGater(ctrl)
h1, err := libp2p.New(libp2p.ConnectionGater(connGater))
require.NoError(t, err)
defer h1.Close()
h2, err := libp2p.New(libp2p.ListenAddrs(a))
require.NoError(t, err)
defer h2.Close()
require.Len(t, h2.Addrs(), 1)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
connGater.EXPECT().InterceptPeerDial(h2.ID())
require.ErrorIs(t, h1.Connect(ctx, peer.AddrInfo{ID: h2.ID(), Addrs: h2.Addrs()}), swarm.ErrGaterDisallowedConnection)
})
}
}
func TestInterceptAddrDial(t *testing.T) {
for _, a := range addrs {
t.Run(fmt.Sprintf("dialing %s", transportName(a)), func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
connGater := NewMockConnectionGater(ctrl)
h1, err := libp2p.New(libp2p.ConnectionGater(connGater))
require.NoError(t, err)
defer h1.Close()
h2, err := libp2p.New(libp2p.ListenAddrs(a))
require.NoError(t, err)
defer h2.Close()
require.Len(t, h2.Addrs(), 1)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
gomock.InOrder(
connGater.EXPECT().InterceptPeerDial(h2.ID()).Return(true),
connGater.EXPECT().InterceptAddrDial(h2.ID(), h2.Addrs()[0]),
)
require.ErrorIs(t, h1.Connect(ctx, peer.AddrInfo{ID: h2.ID(), Addrs: h2.Addrs()}), swarm.ErrNoGoodAddresses)
})
}
}
func TestInterceptSecuredOutgoing(t *testing.T) {
for _, a := range addrs {
t.Run(fmt.Sprintf("dialing %s", transportName(a)), func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
connGater := NewMockConnectionGater(ctrl)
h1, err := libp2p.New(libp2p.ConnectionGater(connGater))
require.NoError(t, err)
defer h1.Close()
h2, err := libp2p.New(libp2p.ListenAddrs(a))
require.NoError(t, err)
defer h2.Close()
require.Len(t, h2.Addrs(), 1)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
gomock.InOrder(
connGater.EXPECT().InterceptPeerDial(h2.ID()).Return(true),
connGater.EXPECT().InterceptAddrDial(h2.ID(), gomock.Any()).Return(true),
connGater.EXPECT().InterceptSecured(network.DirOutbound, h2.ID(), gomock.Any()).Do(func(_ network.Direction, _ peer.ID, addrs network.ConnMultiaddrs) {
// remove the certhash component from WebTransport addresses
require.Equal(t, stripCertHash(h2.Addrs()[0]).String(), addrs.RemoteMultiaddr().String())
}),
)
err = h1.Connect(ctx, peer.AddrInfo{ID: h2.ID(), Addrs: h2.Addrs()})
require.Error(t, err)
// There's a bug in the WebSocket library, making Close block for up to 5s.
// See https://github.com/nhooyr/websocket/issues/355 for details.
if _, err := a.ValueForProtocol(ma.P_WS); err == nil {
require.NotErrorIs(t, err, context.DeadlineExceeded)
}
})
}
}
func TestInterceptUpgradedOutgoing(t *testing.T) {
for _, a := range addrs {
t.Run(fmt.Sprintf("dialing %s", transportName(a)), func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
connGater := NewMockConnectionGater(ctrl)
h1, err := libp2p.New(libp2p.ConnectionGater(connGater))
require.NoError(t, err)
defer h1.Close()
h2, err := libp2p.New(libp2p.ListenAddrs(a))
require.NoError(t, err)
defer h2.Close()
require.Len(t, h2.Addrs(), 1)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
gomock.InOrder(
connGater.EXPECT().InterceptPeerDial(h2.ID()).Return(true),
connGater.EXPECT().InterceptAddrDial(h2.ID(), gomock.Any()).Return(true),
connGater.EXPECT().InterceptSecured(network.DirOutbound, h2.ID(), gomock.Any()).Return(true),
connGater.EXPECT().InterceptUpgraded(gomock.Any()).Do(func(c network.Conn) {
// remove the certhash component from WebTransport addresses
require.Equal(t, stripCertHash(h2.Addrs()[0]), c.RemoteMultiaddr())
require.Equal(t, h1.ID(), c.LocalPeer())
require.Equal(t, h2.ID(), c.RemotePeer())
}))
err = h1.Connect(ctx, peer.AddrInfo{ID: h2.ID(), Addrs: h2.Addrs()})
require.Error(t, err)
// There's a bug in the WebSocket library, making Close block for up to 5s.
// See https://github.com/nhooyr/websocket/issues/355 for details.
if _, err := a.ValueForProtocol(ma.P_WS); err == nil {
require.NotErrorIs(t, err, context.DeadlineExceeded)
}
})
}
}
func TestInterceptAccept(t *testing.T) {
for _, a := range addrs {
t.Run(fmt.Sprintf("accepting %s", transportName(a)), func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
connGater := NewMockConnectionGater(ctrl)
h1, err := libp2p.New()
require.NoError(t, err)
defer h1.Close()
h2, err := libp2p.New(
libp2p.ListenAddrs(a),
libp2p.ConnectionGater(connGater),
)
require.NoError(t, err)
defer h2.Close()
require.Len(t, h2.Addrs(), 1)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
// The basic host dials the first connection.
connGater.EXPECT().InterceptAccept(gomock.Any()).Do(func(addrs network.ConnMultiaddrs) {
// remove the certhash component from WebTransport addresses
require.Equal(t, stripCertHash(h2.Addrs()[0]), addrs.LocalMultiaddr())
})
h1.Peerstore().AddAddrs(h2.ID(), h2.Addrs(), time.Hour)
_, err = h1.NewStream(ctx, h2.ID(), protocol.TestingID)
require.Error(t, err)
// There's a bug in the WebSocket library, making Close block for up to 5s.
// See https://github.com/nhooyr/websocket/issues/355 for details.
if _, err := a.ValueForProtocol(ma.P_WS); err == nil {
require.NotErrorIs(t, err, context.DeadlineExceeded)
}
})
}
}
func TestInterceptSecuredIncoming(t *testing.T) {
for _, a := range addrs {
t.Run(fmt.Sprintf("accepting %s", transportName(a)), func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
connGater := NewMockConnectionGater(ctrl)
h1, err := libp2p.New()
require.NoError(t, err)
defer h1.Close()
h2, err := libp2p.New(
libp2p.ListenAddrs(a),
libp2p.ConnectionGater(connGater),
)
require.NoError(t, err)
defer h2.Close()
require.Len(t, h2.Addrs(), 1)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
gomock.InOrder(
connGater.EXPECT().InterceptAccept(gomock.Any()).Return(true),
connGater.EXPECT().InterceptSecured(network.DirInbound, h1.ID(), gomock.Any()).Do(func(_ network.Direction, _ peer.ID, addrs network.ConnMultiaddrs) {
// remove the certhash component from WebTransport addresses
require.Equal(t, stripCertHash(h2.Addrs()[0]), addrs.LocalMultiaddr())
}),
)
h1.Peerstore().AddAddrs(h2.ID(), h2.Addrs(), time.Hour)
_, err = h1.NewStream(ctx, h2.ID(), protocol.TestingID)
require.Error(t, err)
// There's a bug in the WebSocket library, making Close block for up to 5s.
// See https://github.com/nhooyr/websocket/issues/355 for details.
if _, err := a.ValueForProtocol(ma.P_WS); err == nil {
require.NotErrorIs(t, err, context.DeadlineExceeded)
}
})
}
}
func TestInterceptUpgradedIncoming(t *testing.T) {
for _, a := range addrs {
_, tr := ma.SplitLast(a)
t.Run(fmt.Sprintf("accepting %s", tr.Protocol().Name), func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
connGater := NewMockConnectionGater(ctrl)
h1, err := libp2p.New()
require.NoError(t, err)
defer h1.Close()
h2, err := libp2p.New(
libp2p.ListenAddrs(a),
libp2p.ConnectionGater(connGater),
)
require.NoError(t, err)
defer h2.Close()
require.Len(t, h2.Addrs(), 1)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
gomock.InOrder(
connGater.EXPECT().InterceptAccept(gomock.Any()).Return(true),
connGater.EXPECT().InterceptSecured(network.DirInbound, h1.ID(), gomock.Any()).Return(true),
connGater.EXPECT().InterceptUpgraded(gomock.Any()).Do(func(c network.Conn) {
// remove the certhash component from WebTransport addresses
require.Equal(t, stripCertHash(h2.Addrs()[0]), c.LocalMultiaddr())
require.Equal(t, h1.ID(), c.RemotePeer())
require.Equal(t, h2.ID(), c.LocalPeer())
}),
)
h1.Peerstore().AddAddrs(h2.ID(), h2.Addrs(), time.Hour)
_, err = h1.NewStream(ctx, h2.ID(), protocol.TestingID)
require.Error(t, err)
// There's a bug in the WebSocket library, making Close block for up to 5s.
// See https://github.com/nhooyr/websocket/issues/355 for details.
if _, err := a.ValueForProtocol(ma.P_WS); err == nil {
require.NotErrorIs(t, err, context.DeadlineExceeded)
}
})
}
}