forked from libp2p/go-libp2p-autonat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autonat_test.go
164 lines (136 loc) · 4.16 KB
/
autonat_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
package autonat
import (
"context"
"testing"
"time"
pb "github.com/libp2p/go-libp2p-autonat/pb"
"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"
ggio "github.com/gogo/protobuf/io"
bhost "github.com/libp2p/go-libp2p-blankhost"
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
ma "github.com/multiformats/go-multiaddr"
)
func init() {
AutoNATBootDelay = 1 * time.Second
AutoNATRefreshInterval = 1 * time.Second
AutoNATRetryInterval = 1 * time.Second
AutoNATIdentifyDelay = 100 * time.Millisecond
}
// these are mock service implementations for testing
func makeAutoNATServicePrivate(ctx context.Context, t *testing.T) host.Host {
h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
h.SetStreamHandler(AutoNATProto, sayAutoNATPrivate)
return h
}
func makeAutoNATServicePublic(ctx context.Context, t *testing.T) host.Host {
h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
h.SetStreamHandler(AutoNATProto, sayAutoNATPublic)
return h
}
func sayAutoNATPrivate(s network.Stream) {
defer s.Close()
w := ggio.NewDelimitedWriter(s)
res := pb.Message{
Type: pb.Message_DIAL_RESPONSE.Enum(),
DialResponse: newDialResponseError(pb.Message_E_DIAL_ERROR, "no dialable addresses"),
}
w.WriteMsg(&res)
}
func sayAutoNATPublic(s network.Stream) {
defer s.Close()
w := ggio.NewDelimitedWriter(s)
res := pb.Message{
Type: pb.Message_DIAL_RESPONSE.Enum(),
DialResponse: newDialResponseOK(s.Conn().RemoteMultiaddr()),
}
w.WriteMsg(&res)
}
func newDialResponseOK(addr ma.Multiaddr) *pb.Message_DialResponse {
dr := new(pb.Message_DialResponse)
dr.Status = pb.Message_OK.Enum()
dr.Addr = addr.Bytes()
return dr
}
func newDialResponseError(status pb.Message_ResponseStatus, text string) *pb.Message_DialResponse {
dr := new(pb.Message_DialResponse)
dr.Status = status.Enum()
dr.StatusText = &text
return dr
}
func makeAutoNAT(ctx context.Context, t *testing.T, ash host.Host) (host.Host, AutoNAT) {
h := bhost.NewBlankHost(swarmt.GenSwarm(t, ctx))
h.Peerstore().AddAddrs(ash.ID(), ash.Addrs(), time.Minute)
a := NewAutoNAT(ctx, h)
return h, a
}
func connect(t *testing.T, a, b host.Host) {
pinfo := peer.AddrInfo{ID: a.ID(), Addrs: a.Addrs()}
err := b.Connect(context.Background(), pinfo)
if err != nil {
t.Fatal(err)
}
}
// tests
func TestAutoNATPrivate(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
hs := makeAutoNATServicePrivate(ctx, t)
hc, an := makeAutoNAT(ctx, t, hs)
// subscribe to AutoNat events
s, err := hc.EventBus().Subscribe(&event.EvtLocalRoutabilityPrivate{})
if err != nil {
t.Fatalf("failed to subscribe to event EvtLocalRoutabilityPrivate, err=%s", err)
}
status := an.Status()
if status != NATStatusUnknown {
t.Fatalf("unexpected NAT status: %d", status)
}
connect(t, hs, hc)
time.Sleep(2 * time.Second)
status = an.Status()
if status != NATStatusPrivate {
t.Fatalf("unexpected NAT status: %d", status)
}
select {
case e := <-s.Out():
_, ok := e.(event.EvtLocalRoutabilityPrivate)
if !ok {
t.Fatal("got wrong event type from the bus")
}
case <-time.After(1 * time.Second):
t.Fatal("failed to get the EvtLocalRoutabilityPrivate event from the bus")
}
}
func TestAutoNATPublic(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
hs := makeAutoNATServicePublic(ctx, t)
hc, an := makeAutoNAT(ctx, t, hs)
// subscribe to AutoNat events
s, err := hc.EventBus().Subscribe(&event.EvtLocalRoutabilityPublic{})
if err != nil {
t.Fatalf("failed to subscribe to event EvtLocalRoutabilityPublic, err=%s", err)
}
status := an.Status()
if status != NATStatusUnknown {
t.Fatalf("unexpected NAT status: %d", status)
}
connect(t, hs, hc)
time.Sleep(2 * time.Second)
status = an.Status()
if status != NATStatusPublic {
t.Fatalf("unexpected NAT status: %d", status)
}
select {
case e := <-s.Out():
_, ok := e.(event.EvtLocalRoutabilityPublic)
if !ok {
t.Fatal("got wrong event type from the bus")
}
case <-time.After(1 * time.Second):
t.Fatal("failed to get the EvtLocalRoutabilityPublic event from the bus")
}
}