-
Notifications
You must be signed in to change notification settings - Fork 0
/
external_ip_mapper_test.go
309 lines (261 loc) · 10 KB
/
external_ip_mapper_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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package ice
import (
"net"
"testing"
"github.com/stretchr/testify/require"
)
func TestExternalIPMapper(t *testing.T) {
t.Run("validateIPString", func(t *testing.T) {
var ip net.IP
var isIPv4 bool
var err error
ip, isIPv4, err = validateIPString("1.2.3.4")
require.NoError(t, err, "should succeed")
require.True(t, isIPv4, "should be true")
require.Equal(t, "1.2.3.4", ip.String(), "should be true")
ip, isIPv4, err = validateIPString("2601:4567::5678")
require.NoError(t, err, "should succeed")
require.False(t, isIPv4, "should be false")
require.Equal(t, "2601:4567::5678", ip.String(), "should be true")
_, _, err = validateIPString("bad.6.6.6")
require.Error(t, err, "should fail")
})
t.Run("newExternalIPMapper", func(t *testing.T) {
var m *externalIPMapper
var err error
// ips being nil should succeed but mapper will be nil also
m, err = newExternalIPMapper(CandidateTypeUnspecified, nil)
require.NoError(t, err, "should succeed")
require.Nil(t, m, "should be nil")
// ips being empty should succeed but mapper will still be nil
m, err = newExternalIPMapper(CandidateTypeUnspecified, []string{})
require.NoError(t, err, "should succeed")
require.Nil(t, m, "should be nil")
// IPv4 with no explicit local IP, defaults to CandidateTypeHost
m, err = newExternalIPMapper(CandidateTypeUnspecified, []string{
"1.2.3.4",
})
require.NoError(t, err, "should succeed")
require.NotNil(t, m, "should not be nil")
require.Equal(t, CandidateTypeHost, m.candidateType, "should match")
require.NotNil(t, m.ipv4Mapping.ipSole)
require.Nil(t, m.ipv6Mapping.ipSole)
require.Equal(t, 0, len(m.ipv4Mapping.ipMap), "should match")
require.Equal(t, 0, len(m.ipv6Mapping.ipMap), "should match")
// IPv4 with no explicit local IP, using CandidateTypeServerReflexive
m, err = newExternalIPMapper(CandidateTypeServerReflexive, []string{
"1.2.3.4",
})
require.NoError(t, err, "should succeed")
require.NotNil(t, m, "should not be nil")
require.Equal(t, CandidateTypeServerReflexive, m.candidateType, "should match")
require.NotNil(t, m.ipv4Mapping.ipSole)
require.Nil(t, m.ipv6Mapping.ipSole)
require.Equal(t, 0, len(m.ipv4Mapping.ipMap), "should match")
require.Equal(t, 0, len(m.ipv6Mapping.ipMap), "should match")
// IPv4 with no explicit local IP, defaults to CandidateTypeHost
m, err = newExternalIPMapper(CandidateTypeUnspecified, []string{
"2601:4567::5678",
})
require.NoError(t, err, "should succeed")
require.NotNil(t, m, "should not be nil")
require.Equal(t, CandidateTypeHost, m.candidateType, "should match")
require.Nil(t, m.ipv4Mapping.ipSole)
require.NotNil(t, m.ipv6Mapping.ipSole)
require.Equal(t, 0, len(m.ipv4Mapping.ipMap), "should match")
require.Equal(t, 0, len(m.ipv6Mapping.ipMap), "should match")
// IPv4 and IPv6 in the mix
m, err = newExternalIPMapper(CandidateTypeUnspecified, []string{
"1.2.3.4",
"2601:4567::5678",
})
require.NoError(t, err, "should succeed")
require.NotNil(t, m, "should not be nil")
require.Equal(t, CandidateTypeHost, m.candidateType, "should match")
require.NotNil(t, m.ipv4Mapping.ipSole)
require.NotNil(t, m.ipv6Mapping.ipSole)
require.Equal(t, 0, len(m.ipv4Mapping.ipMap), "should match")
require.Equal(t, 0, len(m.ipv6Mapping.ipMap), "should match")
// Unsupported candidate type - CandidateTypePeerReflexive
m, err = newExternalIPMapper(CandidateTypePeerReflexive, []string{
"1.2.3.4",
})
require.Error(t, err, "should fail")
require.Nil(t, m, "should be nil")
// Unsupported candidate type - CandidateTypeRelay
m, err = newExternalIPMapper(CandidateTypePeerReflexive, []string{
"1.2.3.4",
})
require.Error(t, err, "should fail")
require.Nil(t, m, "should be nil")
// Cannot duplicate mapping IPv4 family
m, err = newExternalIPMapper(CandidateTypeServerReflexive, []string{
"1.2.3.4",
"5.6.7.8",
})
require.Error(t, err, "should fail")
require.Nil(t, m, "should be nil")
// Cannot duplicate mapping IPv6 family
m, err = newExternalIPMapper(CandidateTypeServerReflexive, []string{
"2201::1",
"2201::0002",
})
require.Error(t, err, "should fail")
require.Nil(t, m, "should be nil")
// Invalide external IP string
m, err = newExternalIPMapper(CandidateTypeServerReflexive, []string{
"bad.2.3.4",
})
require.Error(t, err, "should fail")
require.Nil(t, m, "should be nil")
// Invalide local IP string
m, err = newExternalIPMapper(CandidateTypeServerReflexive, []string{
"1.2.3.4/10.0.0.bad",
})
require.Error(t, err, "should fail")
require.Nil(t, m, "should be nil")
})
t.Run("newExternalIPMapper with explicit local IP", func(t *testing.T) {
var m *externalIPMapper
var err error
// IPv4 with explicit local IP, defaults to CandidateTypeHost
m, err = newExternalIPMapper(CandidateTypeUnspecified, []string{
"1.2.3.4/10.0.0.1",
})
require.NoError(t, err, "should succeed")
require.NotNil(t, m, "should not be nil")
require.Equal(t, CandidateTypeHost, m.candidateType, "should match")
require.Nil(t, m.ipv4Mapping.ipSole)
require.Nil(t, m.ipv6Mapping.ipSole)
require.Equal(t, 1, len(m.ipv4Mapping.ipMap), "should match")
require.Equal(t, 0, len(m.ipv6Mapping.ipMap), "should match")
// Cannot assign two ext IPs for one local IPv4
m, err = newExternalIPMapper(CandidateTypeUnspecified, []string{
"1.2.3.4/10.0.0.1",
"1.2.3.5/10.0.0.1",
})
require.Error(t, err, "should fail")
require.Nil(t, m, "should be nil")
// Cannot assign two ext IPs for one local IPv6
m, err = newExternalIPMapper(CandidateTypeUnspecified, []string{
"2200::1/fe80::1",
"2200::0002/fe80::1",
})
require.Error(t, err, "should fail")
require.Nil(t, m, "should be nil")
// Cannot mix different IP family in a pair (1)
m, err = newExternalIPMapper(CandidateTypeUnspecified, []string{
"2200::1/10.0.0.1",
})
require.Error(t, err, "should fail")
require.Nil(t, m, "should be nil")
// Cannot mix different IP family in a pair (2)
m, err = newExternalIPMapper(CandidateTypeUnspecified, []string{
"1.2.3.4/fe80::1",
})
require.Error(t, err, "should fail")
require.Nil(t, m, "should be nil")
// Invalid pair
m, err = newExternalIPMapper(CandidateTypeUnspecified, []string{
"1.2.3.4/192.168.0.2/10.0.0.1",
})
require.Error(t, err, "should fail")
require.Nil(t, m, "should be nil")
})
t.Run("newExternalIPMapper with implicit and explicit local IP", func(t *testing.T) {
// Mixing implicit and explicit local IPs not allowed
_, err := newExternalIPMapper(CandidateTypeUnspecified, []string{
"1.2.3.4",
"1.2.3.5/10.0.0.1",
})
require.Error(t, err, "should fail")
// Mixing implicit and explicit local IPs not allowed
_, err = newExternalIPMapper(CandidateTypeUnspecified, []string{
"1.2.3.5/10.0.0.1",
"1.2.3.4",
})
require.Error(t, err, "should fail")
})
t.Run("findExternalIP without explicit local IP", func(t *testing.T) {
var m *externalIPMapper
var err error
var extIP net.IP
// IPv4 with explicit local IP, defaults to CandidateTypeHost
m, err = newExternalIPMapper(CandidateTypeUnspecified, []string{
"1.2.3.4",
"2200::1",
})
require.NoError(t, err, "should succeed")
require.NotNil(t, m, "should not be nil")
require.NotNil(t, m.ipv4Mapping.ipSole)
require.NotNil(t, m.ipv6Mapping.ipSole)
// Find external IPv4
extIP, err = m.findExternalIP("10.0.0.1")
require.NoError(t, err, "should succeed")
require.Equal(t, "1.2.3.4", extIP.String(), "should match")
// Find external IPv6
extIP, err = m.findExternalIP("fe80::0001") // Use '0001' instead of '1' on purpose
require.NoError(t, err, "should succeed")
require.Equal(t, "2200::1", extIP.String(), "should match")
// Bad local IP string
_, err = m.findExternalIP("really.bad")
require.Error(t, err, "should fail")
})
t.Run("findExternalIP with explicit local IP", func(t *testing.T) {
var m *externalIPMapper
var err error
var extIP net.IP
// IPv4 with explicit local IP, defaults to CandidateTypeHost
m, err = newExternalIPMapper(CandidateTypeUnspecified, []string{
"1.2.3.4/10.0.0.1",
"1.2.3.5/10.0.0.2",
"2200::1/fe80::1",
"2200::2/fe80::2",
})
require.NoError(t, err, "should succeed")
require.NotNil(t, m, "should not be nil")
// Find external IPv4
extIP, err = m.findExternalIP("10.0.0.1")
require.NoError(t, err, "should succeed")
require.Equal(t, "1.2.3.4", extIP.String(), "should match")
extIP, err = m.findExternalIP("10.0.0.2")
require.NoError(t, err, "should succeed")
require.Equal(t, "1.2.3.5", extIP.String(), "should match")
_, err = m.findExternalIP("10.0.0.3")
require.Error(t, err, "should fail")
// Find external IPv6
extIP, err = m.findExternalIP("fe80::0001") // Use '0001' instead of '1' on purpose
require.NoError(t, err, "should succeed")
require.Equal(t, "2200::1", extIP.String(), "should match")
extIP, err = m.findExternalIP("fe80::0002") // Use '0002' instead of '2' on purpose
require.NoError(t, err, "should succeed")
require.Equal(t, "2200::2", extIP.String(), "should match")
_, err = m.findExternalIP("fe80::3")
require.Error(t, err, "should fail")
// Bad local IP string
_, err = m.findExternalIP("really.bad")
require.Error(t, err, "should fail")
})
t.Run("findExternalIP with empty map", func(t *testing.T) {
var m *externalIPMapper
var err error
m, err = newExternalIPMapper(CandidateTypeUnspecified, []string{
"1.2.3.4",
})
require.NoError(t, err, "should succeed")
// Attempt to find IPv6 that does not exist in the map
extIP, err := m.findExternalIP("fe80::1")
require.NoError(t, err, "should succeed")
require.Equal(t, "fe80::1", extIP.String(), "should match")
m, err = newExternalIPMapper(CandidateTypeUnspecified, []string{
"2200::1",
})
require.NoError(t, err, "should succeed")
// Attempt to find IPv4 that does not exist in the map
extIP, err = m.findExternalIP("10.0.0.1")
require.NoError(t, err, "should succeed")
require.Equal(t, "10.0.0.1", extIP.String(), "should match")
})
}