-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathautorelay_test.go
219 lines (184 loc) · 5.24 KB
/
autorelay_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
package relay_test
import (
"context"
"net"
"sync"
"testing"
"time"
libp2p "github.com/libp2p/go-libp2p"
relay "github.com/libp2p/go-libp2p/p2p/host/relay"
ggio "github.com/gogo/protobuf/io"
cid "github.com/ipfs/go-cid"
autonat "github.com/libp2p/go-libp2p-autonat"
autonatpb "github.com/libp2p/go-libp2p-autonat/pb"
circuit "github.com/libp2p/go-libp2p-circuit"
host "github.com/libp2p/go-libp2p-host"
inet "github.com/libp2p/go-libp2p-net"
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
routing "github.com/libp2p/go-libp2p-routing"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr-net"
)
// test specific parameters
func init() {
autonat.AutoNATIdentifyDelay = 100 * time.Millisecond
autonat.AutoNATBootDelay = 1 * time.Second
relay.BootDelay = 1 * time.Second
manet.Private4 = []*net.IPNet{}
}
// mock routing
type mockRoutingTable struct {
mx sync.Mutex
providers map[string]map[peer.ID]pstore.PeerInfo
}
type mockRouting struct {
h host.Host
tab *mockRoutingTable
}
func newMockRoutingTable() *mockRoutingTable {
return &mockRoutingTable{providers: make(map[string]map[peer.ID]pstore.PeerInfo)}
}
func newMockRouting(h host.Host, tab *mockRoutingTable) *mockRouting {
return &mockRouting{h: h, tab: tab}
}
func (m *mockRouting) FindPeer(ctx context.Context, p peer.ID) (pstore.PeerInfo, error) {
return pstore.PeerInfo{}, routing.ErrNotFound
}
func (m *mockRouting) Provide(ctx context.Context, cid cid.Cid, bcast bool) error {
m.tab.mx.Lock()
defer m.tab.mx.Unlock()
pmap, ok := m.tab.providers[cid.String()]
if !ok {
pmap = make(map[peer.ID]pstore.PeerInfo)
m.tab.providers[cid.String()] = pmap
}
pmap[m.h.ID()] = pstore.PeerInfo{ID: m.h.ID(), Addrs: m.h.Addrs()}
return nil
}
func (m *mockRouting) FindProvidersAsync(ctx context.Context, cid cid.Cid, limit int) <-chan pstore.PeerInfo {
ch := make(chan pstore.PeerInfo)
go func() {
defer close(ch)
m.tab.mx.Lock()
defer m.tab.mx.Unlock()
pmap, ok := m.tab.providers[cid.String()]
if !ok {
return
}
for _, pi := range pmap {
select {
case ch <- pi:
case <-ctx.Done():
return
}
}
}()
return ch
}
// mock autonat
func makeAutoNATServicePrivate(ctx context.Context, t *testing.T) host.Host {
h, err := libp2p.New(ctx)
if err != nil {
t.Fatal(err)
}
h.SetStreamHandler(autonat.AutoNATProto, sayAutoNATPrivate)
return h
}
func sayAutoNATPrivate(s inet.Stream) {
defer s.Close()
w := ggio.NewDelimitedWriter(s)
res := autonatpb.Message{
Type: autonatpb.Message_DIAL_RESPONSE.Enum(),
DialResponse: newDialResponseError(autonatpb.Message_E_DIAL_ERROR, "no dialable addresses"),
}
w.WriteMsg(&res)
}
func newDialResponseError(status autonatpb.Message_ResponseStatus, text string) *autonatpb.Message_DialResponse {
dr := new(autonatpb.Message_DialResponse)
dr.Status = status.Enum()
dr.StatusText = &text
return dr
}
// connector
func connect(t *testing.T, a, b host.Host) {
pinfo := pstore.PeerInfo{ID: a.ID(), Addrs: a.Addrs()}
err := b.Connect(context.Background(), pinfo)
if err != nil {
t.Fatal(err)
}
}
// and the actual test!
func TestAutoRelay(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
mtab := newMockRoutingTable()
makeRouting := func(h host.Host) (routing.PeerRouting, error) {
mr := newMockRouting(h, mtab)
return mr, nil
}
h1 := makeAutoNATServicePrivate(ctx, t)
_, err := libp2p.New(ctx, libp2p.EnableRelay(circuit.OptHop), libp2p.EnableAutoRelay(), libp2p.Routing(makeRouting))
if err != nil {
t.Fatal(err)
}
h3, err := libp2p.New(ctx, libp2p.EnableRelay(), libp2p.EnableAutoRelay(), libp2p.Routing(makeRouting))
if err != nil {
t.Fatal(err)
}
h4, err := libp2p.New(ctx, libp2p.EnableRelay())
// verify that we don't advertise relay addrs initially
for _, addr := range h3.Addrs() {
_, err := addr.ValueForProtocol(circuit.P_CIRCUIT)
if err == nil {
t.Fatal("relay addr advertised before auto detection")
}
}
// connect to AutoNAT and let detection/discovery work its magic
connect(t, h1, h3)
time.Sleep(3 * time.Second)
// verify that we now advertise relay addrs (but not unspecific relay addrs)
unspecificRelay, err := ma.NewMultiaddr("/p2p-circuit")
if err != nil {
t.Fatal(err)
}
haveRelay := false
for _, addr := range h3.Addrs() {
if addr.Equal(unspecificRelay) {
t.Fatal("unspecific relay addr advertised")
}
_, err := addr.ValueForProtocol(circuit.P_CIRCUIT)
if err == nil {
haveRelay = true
}
}
if !haveRelay {
t.Fatal("No relay addrs advertised")
}
// verify that we can connect through the relay
var raddrs []ma.Multiaddr
for _, addr := range h3.Addrs() {
_, err := addr.ValueForProtocol(circuit.P_CIRCUIT)
if err == nil {
raddrs = append(raddrs, addr)
}
}
err = h4.Connect(ctx, pstore.PeerInfo{ID: h3.ID(), Addrs: raddrs})
if err != nil {
t.Fatal(err)
}
// verify that we have pushed relay addrs to connected peers
haveRelay = false
for _, addr := range h1.Peerstore().Addrs(h3.ID()) {
if addr.Equal(unspecificRelay) {
t.Fatal("unspecific relay addr advertised")
}
_, err := addr.ValueForProtocol(circuit.P_CIRCUIT)
if err == nil {
haveRelay = true
}
}
if !haveRelay {
t.Fatal("No relay addrs pushed")
}
}