forked from libp2p/go-nat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupnp_test.go
More file actions
242 lines (217 loc) · 6.03 KB
/
Copy pathupnp_test.go
File metadata and controls
242 lines (217 loc) · 6.03 KB
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
package nat
import (
"context"
"errors"
"maps"
"net/netip"
"net/url"
"sync"
"testing"
"time"
"github.com/huin/goupnp"
)
// testRootDevice is a device description with a reachable base URL, which
// AddPortMapping needs to resolve the local address.
func testRootDevice(t *testing.T) *goupnp.RootDevice {
t.Helper()
base, err := url.Parse("http://127.0.0.1:5000/")
if err != nil {
t.Fatal(err)
}
return &goupnp.RootDevice{URLBase: *base}
}
func TestLocationHasAddr(t *testing.T) {
gateway := netip.MustParseAddr("192.168.1.1")
tests := []struct {
name string
location string
want bool
}{
{
name: "the gateway itself",
location: "http://192.168.1.1:5000/rootDesc.xml",
want: true,
},
{
name: "the gateway without a port",
location: "http://192.168.1.1/rootDesc.xml",
want: true,
},
{
name: "another host on the link answered",
// A rogue responder steering discovery at a device it controls.
location: "http://192.168.1.99:5000/rootDesc.xml",
},
{
name: "an off-link address",
location: "http://203.0.113.7/rootDesc.xml",
},
{
name: "a host name cannot be checked without resolving it",
// A responder free to choose the name is also choosing what the
// resolver returns, so the name proves nothing.
location: "http://gateway.local:5000/rootDesc.xml",
},
{
name: "unparseable",
location: "://nonsense",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := locationHasAddr(tt.location, gateway); got != tt.want {
t.Fatalf("locationHasAddr(%q) = %v, want %v", tt.location, got, tt.want)
}
})
}
}
func TestLocationHasAddrMatchesMappedGateway(t *testing.T) {
// A gateway parsed from a net.IP may arrive as a v4-mapped v6 address.
mapped := netip.MustParseAddr("::ffff:192.168.1.1")
if !locationHasAddr("http://192.168.1.1:5000/rootDesc.xml", mapped) {
t.Fatal("a v4-mapped gateway did not match its own address")
}
}
func TestDeviceHostPort(t *testing.T) {
tests := []struct {
name string
urlBase string
want string
wantErr error
}{
{
name: "explicit port",
urlBase: "http://192.168.1.1:5000/",
want: "192.168.1.1:5000",
},
{
name: "no port defaults to HTTP",
// The UPnP Device Architecture lets a device URL omit the port.
urlBase: "http://192.168.1.1/",
want: "192.168.1.1:80",
},
{
name: "IPv6 literal keeps its brackets",
urlBase: "http://[fe80::1]:5000/",
want: "[fe80::1]:5000",
},
{
name: "IPv6 literal without a port",
urlBase: "http://[fe80::1]/",
want: "[fe80::1]:80",
},
{
name: "hostname",
urlBase: "http://gateway.local:49152/",
want: "gateway.local:49152",
},
{
name: "no base URL at all",
urlBase: "",
wantErr: errNoDeviceHost,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
base, err := url.Parse(tt.urlBase)
if err != nil {
t.Fatal(err)
}
got, err := deviceHostPort(&goupnp.RootDevice{URLBase: *base})
if !errors.Is(err, tt.wantErr) {
t.Fatalf("deviceHostPort() error = %v, want %v", err, tt.wantErr)
}
if got != tt.want {
t.Fatalf("deviceHostPort() = %q, want %q", got, tt.want)
}
})
}
}
// fakeUPNPClient records the mappings a device holds.
type fakeUPNPClient struct {
mu sync.Mutex
mappings map[string]uint16 // protocol -> external port
addErr error
delErr error
adds int
deletes int
}
func newFakeUPNPClient() *fakeUPNPClient {
return &fakeUPNPClient{mappings: make(map[string]uint16)}
}
func (c *fakeUPNPClient) GetExternalIPAddressCtx(context.Context) (string, error) {
return "203.0.113.1", nil
}
func (c *fakeUPNPClient) GetNATRSIPStatusCtx(context.Context) (bool, bool, error) {
return false, true, nil
}
func (c *fakeUPNPClient) AddPortMappingCtx(_ context.Context, _ string, extPort uint16, protocol string, _ uint16, _ string, _ bool, _ string, _ uint32) error {
c.mu.Lock()
defer c.mu.Unlock()
c.adds++
if c.addErr != nil {
return c.addErr
}
c.mappings[protocol] = extPort
return nil
}
func (c *fakeUPNPClient) DeletePortMappingCtx(_ context.Context, _ string, _ uint16, protocol string) error {
c.mu.Lock()
defer c.mu.Unlock()
c.deletes++
if c.delErr != nil {
return c.delErr
}
delete(c.mappings, protocol)
return nil
}
func (c *fakeUPNPClient) held() map[string]uint16 {
c.mu.Lock()
defer c.mu.Unlock()
return maps.Clone(c.mappings)
}
func TestUPNPKeepsTCPAndUDPMappingsApart(t *testing.T) {
client := newFakeUPNPClient()
u := newUPNPNAT(client, "test", testRootDevice(t))
ctx := context.Background()
if _, err := u.AddPortMapping(ctx, "udp", 51820, "wg", time.Hour); err != nil {
t.Fatal(err)
}
if _, err := u.AddPortMapping(ctx, "tcp", 51820, "wg", time.Hour); err != nil {
t.Fatal(err)
}
if got := len(client.held()); got != 2 {
t.Fatalf("device holds %d mappings, want 2 (TCP and UDP are independent)", got)
}
// Deleting one protocol must not make the other undeletable: keyed by port
// alone, the shared cache entry is gone and the second delete is skipped.
if err := u.DeletePortMapping(ctx, "udp", 51820); err != nil {
t.Fatal(err)
}
if err := u.DeletePortMapping(ctx, "tcp", 51820); err != nil {
t.Fatal(err)
}
if held := client.held(); len(held) != 0 {
t.Fatalf("device still holds %v, want both mappings removed", held)
}
}
func TestUPNPKeepsMappingAddressableAfterFailedDelete(t *testing.T) {
client := newFakeUPNPClient()
u := newUPNPNAT(client, "test", testRootDevice(t))
ctx := context.Background()
if _, err := u.AddPortMapping(ctx, "udp", 51820, "wg", time.Hour); err != nil {
t.Fatal(err)
}
client.delErr = errors.New("device busy")
if err := u.DeletePortMapping(ctx, "udp", 51820); err == nil {
t.Fatal("DeletePortMapping() error = nil, want the device failure")
}
// The mapping is still on the device, so a retry must still address it.
client.delErr = nil
if err := u.DeletePortMapping(ctx, "udp", 51820); err != nil {
t.Fatal(err)
}
if held := client.held(); len(held) != 0 {
t.Fatalf("device still holds %v after retry", held)
}
}