Debian Codegraph @

vnet/nat_test.go

raw symbols SCIP
1 package vnet
2
3 import (
4 "net"
5 "testing"
6 "time"
7
8 "github.com/pion/logging"
9 "github.com/stretchr/testify/assert"
10 )
11
12 // oic: outbound internal chunk
13 // oec: outbound external chunk
14 // iic: inbound internal chunk
15 // iec: inbound external chunk
16
17 const demoIP = "1.2.3.4"
18
19 func TestNATTypeDefaults(t *testing.T) {
20 loggerFactory := logging.NewDefaultLoggerFactory()
21 nat, err := newNAT(&natConfig{
22 natType: NATType{},
23 mappedIPs: []net.IP{net.ParseIP(demoIP)},
24 loggerFactory: loggerFactory,
25 })
26 assert.NoError(t, err, "should succeed")
27
28 assert.Equal(t, EndpointIndependent, nat.natType.MappingBehavior, "should match")
29 assert.Equal(t, EndpointIndependent, nat.natType.FilteringBehavior, "should match")
30 assert.False(t, nat.natType.Hairpinning, "should be false")
31 assert.False(t, nat.natType.PortPreservation, "should be false")
32 assert.Equal(t, defaultNATMappingLifeTime, nat.natType.MappingLifeTime, "should be false")
33 }
34
35 func TestNATMappingBehavior(t *testing.T) {
36 loggerFactory := logging.NewDefaultLoggerFactory()
37 log := loggerFactory.NewLogger("test")
38
39 t.Run("full-cone NAT", func(t *testing.T) {
40 nat, err := newNAT(&natConfig{
41 natType: NATType{
42 MappingBehavior: EndpointIndependent,
43 FilteringBehavior: EndpointIndependent,
44 Hairpinning: false,
45 MappingLifeTime: 30 * time.Second,
46 },
47 mappedIPs: []net.IP{net.ParseIP(demoIP)},
48 loggerFactory: loggerFactory,
49 })
50 assert.NoError(t, err, "should succeed")
51
52 src := &net.UDPAddr{
53 IP: net.ParseIP("192.168.0.2"),
54 Port: 1234,
55 }
56 dst := &net.UDPAddr{
57 IP: net.ParseIP("5.6.7.8"),
58 Port: 5678,
59 }
60
61 oic := newChunkUDP(src, dst)
62
63 oec, err := nat.translateOutbound(oic)
64 assert.Nil(t, err, "should succeed")
65 assert.Equal(t, 1, len(nat.outboundMap), "should match")
66 assert.Equal(t, 1, len(nat.inboundMap), "should match")
67
68 log.Debugf("o-original : %s", oic.String())
69 log.Debugf("o-translated: %s", oec.String())
70
71 //nolint:forcetypeassert
72 iec := newChunkUDP(
73 &net.UDPAddr{
74 IP: dst.IP,
75 Port: dst.Port,
76 },
77 &net.UDPAddr{
78 IP: oec.(*chunkUDP).sourceIP,
79 Port: oec.(*chunkUDP).sourcePort,
80 },
81 )
82
83 log.Debugf("i-original : %s", iec.String())
84
85 iic, err := nat.translateInbound(iec)
86 assert.Nil(t, err, "should succeed")
87
88 log.Debugf("i-translated: %s", iic.String())
89
90 //nolint:forcetypeassert
91 assert.Equal(t,
92 oic.SourceAddr().String(),
93 iic.(*chunkUDP).DestinationAddr().String(),
94 "should match")
95
96 // packet with dest addr that does not exist in the mapping table
97 // will be dropped
98 //nolint:forcetypeassert
99 iec = newChunkUDP(
100 &net.UDPAddr{
101 IP: dst.IP,
102 Port: dst.Port,
103 },
104 &net.UDPAddr{
105 IP: oec.(*chunkUDP).sourceIP,
106 Port: oec.(*chunkUDP).sourcePort + 1,
107 },
108 )
109
110 _, err = nat.translateInbound(iec)
111 log.Debug(err.Error())
112 assert.NotNil(t, err, "should fail (dropped)")
113
114 // packet from any addr will be accepted (full-cone)
115 //nolint:forcetypeassert
116 iec = newChunkUDP(
117 &net.UDPAddr{
118 IP: dst.IP,
119 Port: 7777,
120 },
121 &net.UDPAddr{
122 IP: oec.(*chunkUDP).sourceIP,
123 Port: oec.(*chunkUDP).sourcePort,
124 },
125 )
126
127 _, err = nat.translateInbound(iec)
128 assert.Nil(t, err, "should succeed")
129 })
130
131 t.Run("addr-restricted-cone NAT", func(t *testing.T) {
132 nat, err := newNAT(&natConfig{
133 natType: NATType{
134 MappingBehavior: EndpointIndependent,
135 FilteringBehavior: EndpointAddrDependent,
136 Hairpinning: false,
137 MappingLifeTime: 30 * time.Second,
138 },
139 mappedIPs: []net.IP{net.ParseIP(demoIP)},
140 loggerFactory: loggerFactory,
141 })
142 assert.NoError(t, err, "should succeed")
143
144 src := &net.UDPAddr{
145 IP: net.ParseIP("192.168.0.2"),
146 Port: 1234,
147 }
148 dst := &net.UDPAddr{
149 IP: net.ParseIP("5.6.7.8"),
150 Port: 5678,
151 }
152
153 oic := newChunkUDP(src, dst)
154 log.Debugf("o-original : %s", oic.String())
155
156 oec, err := nat.translateOutbound(oic)
157 assert.Nil(t, err, "should succeed")
158 assert.Equal(t, 1, len(nat.outboundMap), "should match")
159 assert.Equal(t, 1, len(nat.inboundMap), "should match")
160 log.Debugf("o-translated: %s", oec.String())
161
162 // sending different (IP: 5.6.7.9) won't create a new mapping
163 oic2 := newChunkUDP(&net.UDPAddr{
164 IP: net.ParseIP("192.168.0.2"),
165 Port: 1234,
166 }, &net.UDPAddr{
167 IP: net.ParseIP("5.6.7.9"),
168 Port: 9000,
169 })
170 oec2, err := nat.translateOutbound(oic2)
171 assert.Nil(t, err, "should succeed")
172 assert.Equal(t, 1, len(nat.outboundMap), "should match")
173 assert.Equal(t, 1, len(nat.inboundMap), "should match")
174 log.Debugf("o-translated: %s", oec2.String())
175
176 //nolint:forcetypeassert
177 iec := newChunkUDP(
178 &net.UDPAddr{
179 IP: dst.IP,
180 Port: dst.Port,
181 },
182 &net.UDPAddr{
183 IP: oec.(*chunkUDP).sourceIP,
184 Port: oec.(*chunkUDP).sourcePort,
185 },
186 )
187
188 log.Debugf("i-original : %s", iec.String())
189
190 iic, err := nat.translateInbound(iec)
191 if !assert.NoError(t, err, "should succeed") {
192 return
193 }
194
195 log.Debugf("i-translated: %s", iic.String())
196
197 //nolint:forcetypeassert
198 assert.Equal(t,
199 oic.SourceAddr().String(),
200 iic.(*chunkUDP).DestinationAddr().String(),
201 "should match")
202
203 // packet with dest addr that does not exist in the mapping table
204 // will be dropped
205 //nolint:forcetypeassert
206 iec = newChunkUDP(
207 &net.UDPAddr{
208 IP: dst.IP,
209 Port: dst.Port,
210 },
211 &net.UDPAddr{
212 IP: oec.(*chunkUDP).sourceIP,
213 Port: oec.(*chunkUDP).sourcePort + 1,
214 },
215 )
216
217 _, err = nat.translateInbound(iec)
218 log.Debug(err.Error())
219 assert.NotNil(t, err, "should fail (dropped)")
220
221 // packet from any port will be accepted (restricted-cone)
222 //nolint:forcetypeassert
223 iec = newChunkUDP(
224 &net.UDPAddr{
225 IP: dst.IP,
226 Port: 7777,
227 },
228 &net.UDPAddr{
229 IP: oec.(*chunkUDP).sourceIP,
230 Port: oec.(*chunkUDP).sourcePort,
231 },
232 )
233
234 _, err = nat.translateInbound(iec)
235 assert.Nil(t, err, "should succeed")
236
237 // packet from different addr will be dropped (restricted-cone)
238 //nolint:forcetypeassert
239 iec = newChunkUDP(
240 &net.UDPAddr{
241 IP: net.ParseIP("6.6.6.6"),
242 Port: dst.Port,
243 },
244 &net.UDPAddr{
245 IP: oec.(*chunkUDP).sourceIP,
246 Port: oec.(*chunkUDP).sourcePort,
247 },
248 )
249
250 _, err = nat.translateInbound(iec)
251 log.Debug(err.Error())
252 assert.NotNil(t, err, "should fail (dropped)")
253 })
254
255 t.Run("port-restricted-cone NAT", func(t *testing.T) {
256 nat, err := newNAT(&natConfig{
257 natType: NATType{
258 MappingBehavior: EndpointIndependent,
259 FilteringBehavior: EndpointAddrPortDependent,
260 Hairpinning: false,
261 MappingLifeTime: 30 * time.Second,
262 },
263 mappedIPs: []net.IP{net.ParseIP(demoIP)},
264 loggerFactory: loggerFactory,
265 })
266 assert.NoError(t, err, "should succeed")
267
268 src := &net.UDPAddr{
269 IP: net.ParseIP("192.168.0.2"),
270 Port: 1234,
271 }
272 dst := &net.UDPAddr{
273 IP: net.ParseIP("5.6.7.8"),
274 Port: 5678,
275 }
276
277 oic := newChunkUDP(src, dst)
278 log.Debugf("o-original : %s", oic.String())
279
280 oec, err := nat.translateOutbound(oic)
281 assert.Nil(t, err, "should succeed")
282 assert.Equal(t, 1, len(nat.outboundMap), "should match")
283 assert.Equal(t, 1, len(nat.inboundMap), "should match")
284
285 log.Debugf("o-translated: %s", oec.String())
286
287 // sending different (IP: 5.6.7.9) won't create a new mapping
288 oic2 := newChunkUDP(&net.UDPAddr{
289 IP: net.ParseIP("192.168.0.2"),
290 Port: 1234,
291 }, &net.UDPAddr{
292 IP: net.ParseIP("5.6.7.9"),
293 Port: 9000,
294 })
295 oec2, err := nat.translateOutbound(oic2)
296 assert.Nil(t, err, "should succeed")
297 assert.Equal(t, 1, len(nat.outboundMap), "should match")
298 assert.Equal(t, 1, len(nat.inboundMap), "should match")
299 log.Debugf("o-translated: %s", oec2.String())
300
301 //nolint:forcetypeassert
302 iec := newChunkUDP(
303 &net.UDPAddr{
304 IP: dst.IP,
305 Port: dst.Port,
306 },
307 &net.UDPAddr{
308 IP: oec.(*chunkUDP).sourceIP,
309 Port: oec.(*chunkUDP).sourcePort,
310 },
311 )
312
313 log.Debugf("i-original : %s", iec.String())
314
315 iic, err := nat.translateInbound(iec)
316 assert.Nil(t, err, "should succeed")
317
318 log.Debugf("i-translated: %s", iic.String())
319
320 //nolint:forcetypeassert
321 assert.Equal(t,
322 oic.SourceAddr().String(),
323 iic.(*chunkUDP).DestinationAddr().String(),
324 "should match")
325
326 // packet with dest addr that does not exist in the mapping table
327 // will be dropped
328 //nolint:forcetypeassert
329 iec = newChunkUDP(
330 &net.UDPAddr{
331 IP: dst.IP,
332 Port: dst.Port,
333 },
334 &net.UDPAddr{
335 IP: oec.(*chunkUDP).sourceIP,
336 Port: oec.(*chunkUDP).sourcePort + 1,
337 },
338 )
339
340 _, err = nat.translateInbound(iec)
341 assert.NotNil(t, err, "should fail (dropped)")
342
343 // packet from different port will be dropped (port-restricted-cone)
344 //nolint:forcetypeassert
345 iec = newChunkUDP(
346 &net.UDPAddr{
347 IP: dst.IP,
348 Port: 7777,
349 },
350 &net.UDPAddr{
351 IP: oec.(*chunkUDP).sourceIP,
352 Port: oec.(*chunkUDP).sourcePort,
353 },
354 )
355
356 _, err = nat.translateInbound(iec)
357 assert.NotNil(t, err, "should fail (dropped)")
358
359 // packet from different addr will be dropped (restricted-cone)
360 //nolint:forcetypeassert
361 iec = newChunkUDP(
362 &net.UDPAddr{
363 IP: net.ParseIP("6.6.6.6"),
364 Port: dst.Port,
365 },
366 &net.UDPAddr{
367 IP: oec.(*chunkUDP).sourceIP,
368 Port: oec.(*chunkUDP).sourcePort,
369 },
370 )
371
372 _, err = nat.translateInbound(iec)
373 assert.NotNil(t, err, "should fail (dropped)")
374 })
375
376 t.Run("symmetric NAT addr dependent mapping", func(t *testing.T) { //nolint:dupl
377 nat, err := newNAT(&natConfig{
378 natType: NATType{
379 MappingBehavior: EndpointAddrDependent,
380 FilteringBehavior: EndpointAddrDependent,
381 Hairpinning: false,
382 MappingLifeTime: 30 * time.Second,
383 },
384 mappedIPs: []net.IP{net.ParseIP(demoIP)},
385 loggerFactory: loggerFactory,
386 })
387 assert.NoError(t, err, "should succeed")
388
389 oic1 := newChunkUDP(
390 &net.UDPAddr{
391 IP: net.ParseIP("192.168.0.2"),
392 Port: 1234,
393 },
394 &net.UDPAddr{
395 IP: net.ParseIP("5.6.7.8"),
396 Port: 5678,
397 },
398 )
399
400 oic2 := newChunkUDP(
401 &net.UDPAddr{
402 IP: net.ParseIP("192.168.0.2"),
403 Port: 1234,
404 },
405 &net.UDPAddr{
406 IP: net.ParseIP("5.6.7.100"),
407 Port: 5678,
408 },
409 )
410
411 oic3 := newChunkUDP(
412 &net.UDPAddr{
413 IP: net.ParseIP("192.168.0.2"),
414 Port: 1234,
415 },
416 &net.UDPAddr{
417 IP: net.ParseIP("5.6.7.8"),
418 Port: 6000,
419 },
420 )
421
422 log.Debugf("o-original : %s", oic1.String())
423 log.Debugf("o-original : %s", oic2.String())
424 log.Debugf("o-original : %s", oic3.String())
425
426 oec1, err := nat.translateOutbound(oic1)
427 assert.Nil(t, err, "should succeed")
428
429 oec2, err := nat.translateOutbound(oic2)
430 assert.Nil(t, err, "should succeed")
431
432 oec3, err := nat.translateOutbound(oic3)
433 assert.Nil(t, err, "should succeed")
434
435 assert.Equal(t, 2, len(nat.outboundMap), "should match")
436 assert.Equal(t, 2, len(nat.inboundMap), "should match")
437
438 log.Debugf("o-translated: %s", oec1.String())
439 log.Debugf("o-translated: %s", oec2.String())
440 log.Debugf("o-translated: %s", oec3.String())
441
442 assert.NotEqual(t, oec1.(*chunkUDP).sourcePort, oec2.(*chunkUDP).sourcePort, "should not match") //nolint:forcetypeassert
443 assert.Equal(t, oec1.(*chunkUDP).sourcePort, oec3.(*chunkUDP).sourcePort, "should match") //nolint:forcetypeassert
444 })
445
446 t.Run("symmetric NAT port dependent mapping", func(t *testing.T) { //nolint:dupl
447 nat, err := newNAT(&natConfig{
448 natType: NATType{
449 MappingBehavior: EndpointAddrPortDependent,
450 FilteringBehavior: EndpointAddrPortDependent,
451 Hairpinning: false,
452 MappingLifeTime: 30 * time.Second,
453 },
454 mappedIPs: []net.IP{net.ParseIP(demoIP)},
455 loggerFactory: loggerFactory,
456 })
457 assert.NoError(t, err, "should succeed")
458
459 oic1 := newChunkUDP(
460 &net.UDPAddr{
461 IP: net.ParseIP("192.168.0.2"),
462 Port: 1234,
463 },
464 &net.UDPAddr{
465 IP: net.ParseIP("5.6.7.8"),
466 Port: 5678,
467 },
468 )
469
470 oic2 := newChunkUDP(
471 &net.UDPAddr{
472 IP: net.ParseIP("192.168.0.2"),
473 Port: 1234,
474 },
475 &net.UDPAddr{
476 IP: net.ParseIP("5.6.7.100"),
477 Port: 5678,
478 },
479 )
480
481 oic3 := newChunkUDP(
482 &net.UDPAddr{
483 IP: net.ParseIP("192.168.0.2"),
484 Port: 1234,
485 },
486 &net.UDPAddr{
487 IP: net.ParseIP("5.6.7.8"),
488 Port: 6000,
489 },
490 )
491
492 log.Debugf("o-original : %s", oic1.String())
493 log.Debugf("o-original : %s", oic2.String())
494 log.Debugf("o-original : %s", oic3.String())
495
496 oec1, err := nat.translateOutbound(oic1)
497 assert.Nil(t, err, "should succeed")
498
499 oec2, err := nat.translateOutbound(oic2)
500 assert.Nil(t, err, "should succeed")
501
502 oec3, err := nat.translateOutbound(oic3)
503 assert.Nil(t, err, "should succeed")
504
505 assert.Equal(t, 3, len(nat.outboundMap), "should match")
506 assert.Equal(t, 3, len(nat.inboundMap), "should match")
507
508 log.Debugf("o-translated: %s", oec1.String())
509 log.Debugf("o-translated: %s", oec2.String())
510 log.Debugf("o-translated: %s", oec3.String())
511
512 assert.NotEqual(t, oec1.(*chunkUDP).sourcePort, oec2.(*chunkUDP).sourcePort, "should not match") //nolint:forcetypeassert
513 assert.NotEqual(t, oec1.(*chunkUDP).sourcePort, oec3.(*chunkUDP).sourcePort, "should match") //nolint:forcetypeassert
514 })
515 }
516
517 func TestNATMappingTimeout(t *testing.T) {
518 loggerFactory := logging.NewDefaultLoggerFactory()
519 log := loggerFactory.NewLogger("test")
520
521 t.Run("refresh on outbound", func(t *testing.T) {
522 nat, err := newNAT(&natConfig{
523 natType: NATType{
524 MappingBehavior: EndpointIndependent,
525 FilteringBehavior: EndpointIndependent,
526 Hairpinning: false,
527 MappingLifeTime: 100 * time.Millisecond,
528 },
529 mappedIPs: []net.IP{net.ParseIP(demoIP)},
530 loggerFactory: loggerFactory,
531 })
532 assert.NoError(t, err, "should succeed")
533
534 src := &net.UDPAddr{
535 IP: net.ParseIP("192.168.0.2"),
536 Port: 1234,
537 }
538 dst := &net.UDPAddr{
539 IP: net.ParseIP("5.6.7.8"),
540 Port: 5678,
541 }
542
543 oic := newChunkUDP(src, dst)
544
545 oec, err := nat.translateOutbound(oic)
546 assert.Nil(t, err, "should succeed")
547 assert.Equal(t, 1, len(nat.outboundMap), "should match")
548 assert.Equal(t, 1, len(nat.inboundMap), "should match")
549
550 log.Debugf("o-original : %s", oic.String())
551 log.Debugf("o-translated: %s", oec.String())
552
553 // record mapped addr
554 mapped := oec.(*chunkUDP).SourceAddr().String() //nolint:forcetypeassert
555
556 time.Sleep(75 * time.Millisecond)
557
558 // refresh
559 oec, err = nat.translateOutbound(oic)
560 assert.Nil(t, err, "should succeed")
561 assert.Equal(t, 1, len(nat.outboundMap), "should match")
562 assert.Equal(t, 1, len(nat.inboundMap), "should match")
563
564 log.Debugf("o-original : %s", oic.String())
565 log.Debugf("o-translated: %s", oec.String())
566
567 assert.Equal(t, mapped, oec.(*chunkUDP).SourceAddr().String(), "mapped addr should match") //nolint:forcetypeassert
568
569 // sleep long enough for the mapping to expire
570 time.Sleep(125 * time.Millisecond)
571
572 // refresh after expiration
573 oec, err = nat.translateOutbound(oic)
574 assert.Nil(t, err, "should succeed")
575 assert.Equal(t, 1, len(nat.outboundMap), "should match")
576 assert.Equal(t, 1, len(nat.inboundMap), "should match")
577
578 log.Debugf("o-original : %s", oic.String())
579 log.Debugf("o-translated: %s", oec.String())
580
581 assert.NotEqual(t, mapped, oec.(*chunkUDP).SourceAddr().String(), "mapped addr should not match") //nolint:forcetypeassert
582 })
583
584 t.Run("outbound detects timeout", func(t *testing.T) {
585 nat, err := newNAT(&natConfig{
586 natType: NATType{
587 MappingBehavior: EndpointIndependent,
588 FilteringBehavior: EndpointIndependent,
589 Hairpinning: false,
590 MappingLifeTime: 100 * time.Millisecond,
591 },
592 mappedIPs: []net.IP{net.ParseIP(demoIP)},
593 loggerFactory: loggerFactory,
594 })
595 assert.NoError(t, err, "should succeed")
596
597 src := &net.UDPAddr{
598 IP: net.ParseIP("192.168.0.2"),
599 Port: 1234,
600 }
601 dst := &net.UDPAddr{
602 IP: net.ParseIP("5.6.7.8"),
603 Port: 5678,
604 }
605
606 oic := newChunkUDP(src, dst)
607
608 oec, err := nat.translateOutbound(oic)
609 assert.Nil(t, err, "should succeed")
610 assert.Equal(t, 1, len(nat.outboundMap), "should match")
611 assert.Equal(t, 1, len(nat.inboundMap), "should match")
612
613 log.Debugf("o-original : %s", oic.String())
614 log.Debugf("o-translated: %s", oec.String())
615
616 // sleep long enough for the mapping to expire
617 time.Sleep(125 * time.Millisecond)
618
619 //nolint:forcetypeassert
620 iec := newChunkUDP(
621 &net.UDPAddr{
622 IP: dst.IP,
623 Port: dst.Port,
624 },
625 &net.UDPAddr{
626 IP: oec.(*chunkUDP).sourceIP,
627 Port: oec.(*chunkUDP).sourcePort,
628 },
629 )
630
631 log.Debugf("i-original : %s", iec.String())
632
633 _, err = nat.translateInbound(iec)
634 assert.NotNil(t, err, "should drop")
635 assert.Equal(t, 0, len(nat.outboundMap), "should have no binding")
636 assert.Equal(t, 0, len(nat.inboundMap), "should have no binding")
637 })
638 }
639
640 func TestNAT1To1Behavior(t *testing.T) {
641 loggerFactory := logging.NewDefaultLoggerFactory()
642 log := loggerFactory.NewLogger("test")
643
644 t.Run("1:1 NAT with one mapping", func(t *testing.T) {
645 nat, err := newNAT(&natConfig{
646 natType: NATType{
647 Mode: NATModeNAT1To1,
648 },
649 mappedIPs: []net.IP{net.ParseIP(demoIP)},
650 localIPs: []net.IP{net.ParseIP("10.0.0.1")},
651 loggerFactory: loggerFactory,
652 })
653 if !assert.NoError(t, err, "should succeed") {
654 return
655 }
656
657 src := &net.UDPAddr{
658 IP: net.ParseIP("10.0.0.1"),
659 Port: 1234,
660 }
661 dst := &net.UDPAddr{
662 IP: net.ParseIP("5.6.7.8"),
663 Port: 5678,
664 }
665
666 oic := newChunkUDP(src, dst)
667
668 oec, err := nat.translateOutbound(oic)
669 assert.Nil(t, err, "should succeed")
670 assert.Equal(t, 0, len(nat.outboundMap), "should match")
671 assert.Equal(t, 0, len(nat.inboundMap), "should match")
672
673 log.Debugf("o-original : %s", oic.String())
674 log.Debugf("o-translated: %s", oec.String())
675
676 assert.Equal(t, "1.2.3.4:1234", oec.SourceAddr().String(), "should match")
677
678 //nolint:forcetypeassert
679 iec := newChunkUDP(
680 &net.UDPAddr{
681 IP: dst.IP,
682 Port: dst.Port,
683 },
684 &net.UDPAddr{
685 IP: oec.(*chunkUDP).sourceIP,
686 Port: oec.(*chunkUDP).sourcePort,
687 },
688 )
689
690 log.Debugf("i-original : %s", iec.String())
691
692 iic, err := nat.translateInbound(iec)
693 assert.Nil(t, err, "should succeed")
694
695 log.Debugf("i-translated: %s", iic.String())
696
697 assert.Equal(t,
698 oic.SourceAddr().String(),
699 iic.DestinationAddr().String(),
700 "should match")
701 })
702
703 t.Run("1:1 NAT with more than one mapping", func(t *testing.T) {
704 nat, err := newNAT(&natConfig{
705 natType: NATType{
706 Mode: NATModeNAT1To1,
707 },
708 mappedIPs: []net.IP{
709 net.ParseIP(demoIP),
710 net.ParseIP("1.2.3.5"),
711 },
712 localIPs: []net.IP{
713 net.ParseIP("10.0.0.1"),
714 net.ParseIP("10.0.0.2"),
715 },
716 loggerFactory: loggerFactory,
717 })
718 if !assert.NoError(t, err, "should succeed") {
719 return
720 }
721
722 // outbound translation
723
724 before := newChunkUDP(
725 &net.UDPAddr{
726 IP: net.ParseIP("10.0.0.1"),
727 Port: 1234,
728 },
729 &net.UDPAddr{
730 IP: net.ParseIP("5.6.7.8"),
731 Port: 5678,
732 })
733
734 after, err := nat.translateOutbound(before)
735 if !assert.NoError(t, err, "should succeed") {
736 return
737 }
738 assert.Equal(t, "1.2.3.4:1234", after.SourceAddr().String(), "should match")
739
740 before = newChunkUDP(
741 &net.UDPAddr{
742 IP: net.ParseIP("10.0.0.2"),
743 Port: 1234,
744 },
745 &net.UDPAddr{
746 IP: net.ParseIP("5.6.7.8"),
747 Port: 5678,
748 })
749
750 after, err = nat.translateOutbound(before)
751 if !assert.NoError(t, err, "should succeed") {
752 return
753 }
754 assert.Equal(t, "1.2.3.5:1234", after.SourceAddr().String(), "should match")
755
756 // inbound translation
757
758 before = newChunkUDP(
759 &net.UDPAddr{
760 IP: net.ParseIP("5.6.7.8"),
761 Port: 5678,
762 },
763 &net.UDPAddr{
764 IP: net.ParseIP(demoIP),
765 Port: 2525,
766 })
767
768 after, err = nat.translateInbound(before)
769 if !assert.NoError(t, err, "should succeed") {
770 return
771 }
772 assert.Equal(t, "10.0.0.1:2525", after.DestinationAddr().String(), "should match")
773
774 before = newChunkUDP(
775 &net.UDPAddr{
776 IP: net.ParseIP("5.6.7.8"),
777 Port: 5678,
778 },
779 &net.UDPAddr{
780 IP: net.ParseIP("1.2.3.5"),
781 Port: 9847,
782 })
783
784 after, err = nat.translateInbound(before)
785 if !assert.NoError(t, err, "should succeed") {
786 return
787 }
788 assert.Equal(t, "10.0.0.2:9847", after.DestinationAddr().String(), "should match")
789 })
790
791 t.Run("1:1 NAT failure", func(t *testing.T) {
792 // 1:1 NAT requires more than one mapping
793 _, err := newNAT(&natConfig{
794 natType: NATType{
795 Mode: NATModeNAT1To1,
796 },
797 loggerFactory: loggerFactory,
798 })
799 assert.Error(t, err, "should fail")
800
801 // 1:1 NAT requires the same number of mappedIPs and localIPs
802 _, err = newNAT(&natConfig{
803 natType: NATType{
804 Mode: NATModeNAT1To1,
805 },
806 mappedIPs: []net.IP{
807 net.ParseIP(demoIP),
808 net.ParseIP("1.2.3.5"),
809 },
810 localIPs: []net.IP{
811 net.ParseIP("10.0.0.1"),
812 },
813 loggerFactory: loggerFactory,
814 })
815 assert.Error(t, err, "should fail")
816
817 // drop outbound or inbound chunk with no route in 1:1 NAT
818
819 nat, err := newNAT(&natConfig{
820 natType: NATType{
821 Mode: NATModeNAT1To1,
822 },
823 mappedIPs: []net.IP{
824 net.ParseIP(demoIP),
825 },
826 localIPs: []net.IP{
827 net.ParseIP("10.0.0.1"),
828 },
829 loggerFactory: loggerFactory,
830 })
831 assert.NoError(t, err, "should succeed")
832
833 before := newChunkUDP(
834 &net.UDPAddr{
835 IP: net.ParseIP("10.0.0.2"), // no external mapping for this
836 Port: 1234,
837 },
838 &net.UDPAddr{
839 IP: net.ParseIP("5.6.7.8"),
840 Port: 5678,
841 })
842
843 after, err := nat.translateOutbound(before)
844 if !assert.NoError(t, err, "should succeed") {
845 return
846 }
847 if !assert.Nil(t, after, "should be nil") {
848 return
849 }
850
851 before = newChunkUDP(
852 &net.UDPAddr{
853 IP: net.ParseIP("5.6.7.8"),
854 Port: 5678,
855 },
856 &net.UDPAddr{
857 IP: net.ParseIP("10.0.0.2"), // no local mapping for this
858 Port: 1234,
859 })
860
861 _, err = nat.translateInbound(before)
862 assert.Error(t, err, "should fail")
863 })
864 }