forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpcp_tests.cpp
699 lines (627 loc) · 27.9 KB
/
pcp_tests.cpp
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
// Copyright (c) 2024-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <common/pcp.h>
#include <netbase.h>
#include <test/util/logging.h>
#include <test/util/setup_common.h>
#include <util/time.h>
#include <boost/test/unit_test.hpp>
#include <algorithm>
#include <deque>
using namespace std::literals;
/// UDP test server operation.
struct TestOp {
std::chrono::milliseconds delay;
enum Op {
SEND, // Expect send (with optional data)
RECV, // Expect receive (with data)
NOP, // Just delay
} op;
std::vector<uint8_t> data;
//! Injected error.
//! Set this field to a non-zero value to return the errno code from send or receive operation.
int error;
TestOp(std::chrono::milliseconds delay_in, Op op_in, const std::vector<uint8_t> &data_in, int error_in):
delay(delay_in), op(op_in), data(data_in), error(error_in) {}
};
/// Save the value of CreateSock and restore when the test ends.
class PCPTestingSetup : public BasicTestingSetup
{
public:
explicit PCPTestingSetup(const ChainType chainType = ChainType::MAIN,
TestOpts opts = {})
: BasicTestingSetup{chainType, opts},
m_create_sock_orig{CreateSock}
{
const std::optional<CService> local_ipv4{Lookup("192.168.0.6", 1, false)};
const std::optional<CService> local_ipv6{Lookup("2a10:1234:5678:9abc:def0:1234:5678:9abc", 1, false)};
const std::optional<CService> gateway_ipv4{Lookup("192.168.0.1", 1, false)};
const std::optional<CService> gateway_ipv6{Lookup("2a10:1234:5678:9abc:def0:0000:0000:0000", 1, false)};
BOOST_REQUIRE(local_ipv4 && local_ipv6 && gateway_ipv4 && gateway_ipv6);
default_local_ipv4 = *local_ipv4;
default_local_ipv6 = *local_ipv6;
default_gateway_ipv4 = *gateway_ipv4;
default_gateway_ipv6 = *gateway_ipv6;
struct in_addr inaddr_any;
inaddr_any.s_addr = htonl(INADDR_ANY);
bind_any_ipv4 = CNetAddr(inaddr_any);
}
~PCPTestingSetup()
{
CreateSock = m_create_sock_orig;
MockableSteadyClock::ClearMockTime();
}
// Default testing nonce.
static constexpr PCPMappingNonce TEST_NONCE{0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc};
// Default network addresses.
CNetAddr default_local_ipv4;
CNetAddr default_local_ipv6;
CNetAddr default_gateway_ipv4;
CNetAddr default_gateway_ipv6;
// IPv4 bind
CNetAddr bind_any_ipv4;
private:
const decltype(CreateSock) m_create_sock_orig;
};
/** Simple scripted UDP server emulation for testing.
*/
class PCPTestSock final : public Sock
{
public:
// Note: we awkwardly mark all methods as const, and properties as mutable,
// because Sock expects all networking calls to be const.
explicit PCPTestSock(const CNetAddr &local_ip, const CNetAddr &gateway_ip, const std::vector<TestOp> &script)
: Sock{INVALID_SOCKET},
m_script(script),
m_local_ip(local_ip),
m_gateway_ip(gateway_ip)
{
ElapseTime(std::chrono::seconds(0)); // start mocking steady time
PrepareOp();
}
PCPTestSock& operator=(Sock&& other) override
{
assert(false && "Move of Sock into PCPTestSock not allowed.");
return *this;
}
ssize_t Send(const void* data, size_t len, int) const override {
if (!m_connected) return -1;
std::span in_pkt = std::span(static_cast<const uint8_t*>(data), len);
if (AtEndOfScript() || CurOp().op != TestOp::SEND) {
// Ignore sends after end of script, or sends when we expect a receive.
FailScript();
return len;
}
if (CurOp().error) return -1; // Inject failure
if (CurOp().data.empty() || std::ranges::equal(CurOp().data, in_pkt)) {
AdvanceOp();
} else {
// Wrong send, fail script
FailScript();
}
return len;
}
ssize_t Recv(void* buf, size_t len, int flags) const override
{
if (!m_connected || AtEndOfScript() || CurOp().op != TestOp::RECV || m_time_left != 0s) {
return -1;
}
if (CurOp().error) return -1; // Inject failure
const auto &recv_pkt = CurOp().data;
const size_t consume_bytes{std::min(len, recv_pkt.size())};
std::memcpy(buf, recv_pkt.data(), consume_bytes);
if ((flags & MSG_PEEK) == 0) {
AdvanceOp();
}
return consume_bytes;
}
int Connect(const sockaddr* sa, socklen_t sa_len) const override {
CService service;
if (service.SetSockAddr(sa, sa_len) && service == CService(m_gateway_ip, 5351)) {
if (m_bound.IsBindAny()) { // If bind-any, bind to local ip.
m_bound = CService(m_local_ip, 0);
}
if (m_bound.GetPort() == 0) { // If no port assigned, assign port 1.
m_bound = CService(m_bound, 1);
}
m_connected = true;
return 0;
}
return -1;
}
int Bind(const sockaddr* sa, socklen_t sa_len) const override {
CService service;
if (service.SetSockAddr(sa, sa_len)) {
// Can only bind to one of our local ips
if (!service.IsBindAny() && service != m_local_ip) {
return -1;
}
m_bound = service;
return 0;
}
return -1;
}
int Listen(int) const override { return -1; }
std::unique_ptr<Sock> Accept(sockaddr* addr, socklen_t* addr_len) const override
{
return nullptr;
};
int GetSockOpt(int level, int opt_name, void* opt_val, socklen_t* opt_len) const override
{
return -1;
}
int SetSockOpt(int, int, const void*, socklen_t) const override { return 0; }
int GetSockName(sockaddr* name, socklen_t* name_len) const override
{
// Return the address we've been bound to.
return m_bound.GetSockAddr(name, name_len) ? 0 : -1;
}
bool SetNonBlocking() const override { return true; }
bool IsSelectable() const override { return true; }
bool Wait(std::chrono::milliseconds timeout,
Event requested,
Event* occurred = nullptr) const override
{
// Only handles receive events.
if (AtEndOfScript() || requested != Sock::RECV) {
ElapseTime(timeout);
} else {
std::chrono::milliseconds delay = std::min(m_time_left, timeout);
ElapseTime(delay);
m_time_left -= delay;
if (CurOp().op == TestOp::RECV && m_time_left == 0s && occurred != nullptr) {
*occurred = Sock::RECV;
}
if (CurOp().op == TestOp::NOP) {
// This was a pure delay operation, move to the next op.
AdvanceOp();
}
}
return true;
}
bool WaitMany(std::chrono::milliseconds timeout, EventsPerSock& events_per_sock) const override
{
return false;
}
bool IsConnected(std::string&) const override
{
return true;
}
private:
const std::vector<TestOp> m_script;
mutable size_t m_script_ptr = 0;
mutable std::chrono::milliseconds m_time_left;
mutable std::chrono::milliseconds m_time{MockableSteadyClock::INITIAL_MOCK_TIME};
mutable bool m_connected{false};
mutable CService m_bound;
mutable CNetAddr m_local_ip;
mutable CNetAddr m_gateway_ip;
void ElapseTime(std::chrono::milliseconds duration) const
{
m_time += duration;
MockableSteadyClock::SetMockTime(m_time);
}
bool AtEndOfScript() const { return m_script_ptr == m_script.size(); }
const TestOp &CurOp() const {
BOOST_REQUIRE(m_script_ptr < m_script.size());
return m_script[m_script_ptr];
}
void PrepareOp() const {
if (AtEndOfScript()) return;
m_time_left = CurOp().delay;
}
void AdvanceOp() const
{
m_script_ptr += 1;
PrepareOp();
}
void FailScript() const { m_script_ptr = m_script.size(); }
};
BOOST_FIXTURE_TEST_SUITE(pcp_tests, PCPTestingSetup)
// NAT-PMP IPv4 good-weather scenario.
BOOST_AUTO_TEST_CASE(natpmp_ipv4)
{
const std::vector<TestOp> script{
{
0ms, TestOp::SEND,
{
0x00, 0x00, // version, opcode (request external IP)
}, 0
},
{
2ms, TestOp::RECV,
{
0x00, 0x80, 0x00, 0x00, // version, opcode (external IP), result code (success)
0x66, 0xfd, 0xa1, 0xee, // seconds sinds start of epoch
0x01, 0x02, 0x03, 0x04, // external IP address
}, 0
},
{
0ms, TestOp::SEND,
{
0x00, 0x02, 0x00, 0x00, // version, opcode (request map TCP)
0x04, 0xd2, 0x04, 0xd2, // internal port, suggested external port
0x00, 0x00, 0x03, 0xe8, // requested mapping lifetime in seconds
}, 0
},
{
2ms, TestOp::RECV,
{
0x00, 0x82, 0x00, 0x00, // version, opcode (mapped TCP)
0x66, 0xfd, 0xa1, 0xee, // seconds sinds start of epoch
0x04, 0xd2, 0x04, 0xd2, // internal port, mapped external port
0x00, 0x00, 0x01, 0xf4, // mapping lifetime in seconds
}, 0
},
};
CreateSock = [this, &script](int domain, int type, int protocol) {
if (domain == AF_INET && type == SOCK_DGRAM && protocol == IPPROTO_UDP) return std::make_unique<PCPTestSock>(default_local_ipv4, default_gateway_ipv4, script);
return std::unique_ptr<PCPTestSock>();
};
auto res = NATPMPRequestPortMap(default_gateway_ipv4, 1234, 1000, 1, 200ms);
MappingResult* mapping = std::get_if<MappingResult>(&res);
BOOST_REQUIRE(mapping);
BOOST_CHECK_EQUAL(mapping->version, 0);
BOOST_CHECK_EQUAL(mapping->internal.ToStringAddrPort(), "192.168.0.6:1234");
BOOST_CHECK_EQUAL(mapping->external.ToStringAddrPort(), "1.2.3.4:1234");
BOOST_CHECK_EQUAL(mapping->lifetime, 500);
}
// PCP IPv4 good-weather scenario.
BOOST_AUTO_TEST_CASE(pcp_ipv4)
{
const std::vector<TestOp> script{
{
0ms, TestOp::SEND,
{
0x02, 0x01, 0x00, 0x00, // version, opcode
0x00, 0x00, 0x03, 0xe8, // lifetime
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xc0, 0xa8, 0x00, 0x06, // internal IP
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, // nonce
0x06, 0x00, 0x00, 0x00, // protocol (TCP), reserved
0x04, 0xd2, 0x04, 0xd2, // internal port, suggested external port
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, // suggested external IP
}, 0
},
{
250ms, TestOp::RECV, // 250ms delay before answer
{
0x02, 0x81, 0x00, 0x00, // version, opcode, result success
0x00, 0x00, 0x01, 0xf4, // granted lifetime
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, // nonce
0x06, 0x00, 0x00, 0x00, // protocol (TCP), reserved
0x04, 0xd2, 0x04, 0xd2, // internal port, assigned external port
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03, 0x04, // assigned external IP
}, 0
},
};
CreateSock = [this, &script](int domain, int type, int protocol) {
if (domain == AF_INET && type == SOCK_DGRAM && protocol == IPPROTO_UDP) return std::make_unique<PCPTestSock>(default_local_ipv4, default_gateway_ipv4, script);
return std::unique_ptr<PCPTestSock>();
};
auto res = PCPRequestPortMap(TEST_NONCE, default_gateway_ipv4, bind_any_ipv4, 1234, 1000, 1, 1000ms);
MappingResult* mapping = std::get_if<MappingResult>(&res);
BOOST_REQUIRE(mapping);
BOOST_CHECK_EQUAL(mapping->version, 2);
BOOST_CHECK_EQUAL(mapping->internal.ToStringAddrPort(), "192.168.0.6:1234");
BOOST_CHECK_EQUAL(mapping->external.ToStringAddrPort(), "1.2.3.4:1234");
BOOST_CHECK_EQUAL(mapping->lifetime, 500);
}
// PCP IPv6 good-weather scenario.
BOOST_AUTO_TEST_CASE(pcp_ipv6)
{
const std::vector<TestOp> script{
{
0ms, TestOp::SEND,
{
0x02, 0x01, 0x00, 0x00, // version, opcode
0x00, 0x00, 0x03, 0xe8, // lifetime
0x2a, 0x10, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, // internal IP
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, // nonce
0x06, 0x00, 0x00, 0x00, // protocol (TCP), reserved
0x04, 0xd2, 0x04, 0xd2, // internal port, suggested external port
0x2a, 0x10, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, // suggested external IP
}, 0
},
{
500ms, TestOp::RECV, // 500ms delay before answer
{
0x02, 0x81, 0x00, 0x00, // version, opcode, result success
0x00, 0x00, 0x01, 0xf4, // granted lifetime
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, // nonce
0x06, 0x00, 0x00, 0x00, // protocol (TCP), reserved
0x04, 0xd2, 0x04, 0xd2, // internal port, assigned external port
0x2a, 0x10, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, // suggested external IP
}, 0
},
};
CreateSock = [this, &script](int domain, int type, int protocol) {
if (domain == AF_INET6 && type == SOCK_DGRAM && protocol == IPPROTO_UDP) return std::make_unique<PCPTestSock>(default_local_ipv6, default_gateway_ipv6, script);
return std::unique_ptr<PCPTestSock>();
};
auto res = PCPRequestPortMap(TEST_NONCE, default_gateway_ipv6, default_local_ipv6, 1234, 1000, 1, 1000ms);
MappingResult* mapping = std::get_if<MappingResult>(&res);
BOOST_REQUIRE(mapping);
BOOST_CHECK_EQUAL(mapping->version, 2);
BOOST_CHECK_EQUAL(mapping->internal.ToStringAddrPort(), "[2a10:1234:5678:9abc:def0:1234:5678:9abc]:1234");
BOOST_CHECK_EQUAL(mapping->external.ToStringAddrPort(), "[2a10:1234:5678:9abc:def0:1234:5678:9abc]:1234");
BOOST_CHECK_EQUAL(mapping->lifetime, 500);
}
// PCP timeout.
BOOST_AUTO_TEST_CASE(pcp_timeout)
{
const std::vector<TestOp> script{};
CreateSock = [this, &script](int domain, int type, int protocol) {
if (domain == AF_INET && type == SOCK_DGRAM && protocol == IPPROTO_UDP) return std::make_unique<PCPTestSock>(default_local_ipv4, default_gateway_ipv4, script);
return std::unique_ptr<PCPTestSock>();
};
ASSERT_DEBUG_LOG("pcp: Retrying (1)");
ASSERT_DEBUG_LOG("pcp: Retrying (2)");
ASSERT_DEBUG_LOG("pcp: Timeout");
auto res = PCPRequestPortMap(TEST_NONCE, default_gateway_ipv4, bind_any_ipv4, 1234, 1000, 3, 2000ms);
MappingError* err = std::get_if<MappingError>(&res);
BOOST_REQUIRE(err);
BOOST_CHECK_EQUAL(*err, MappingError::NETWORK_ERROR);
}
// PCP failure receiving (router sends ICMP port closed).
BOOST_AUTO_TEST_CASE(pcp_connrefused)
{
const std::vector<TestOp> script{
{
0ms, TestOp::SEND,
{ // May send anything.
}, 0
},
{
0ms, TestOp::RECV,
{
}, ECONNREFUSED
},
};
CreateSock = [this, &script](int domain, int type, int protocol) {
if (domain == AF_INET && type == SOCK_DGRAM && protocol == IPPROTO_UDP) return std::make_unique<PCPTestSock>(default_local_ipv4, default_gateway_ipv4, script);
return std::unique_ptr<PCPTestSock>();
};
ASSERT_DEBUG_LOG("pcp: Could not receive response");
auto res = PCPRequestPortMap(TEST_NONCE, default_gateway_ipv4, bind_any_ipv4, 1234, 1000, 3, 2000ms);
MappingError* err = std::get_if<MappingError>(&res);
BOOST_REQUIRE(err);
BOOST_CHECK_EQUAL(*err, MappingError::NETWORK_ERROR);
}
// PCP IPv6 success after one timeout.
BOOST_AUTO_TEST_CASE(pcp_ipv6_timeout_success)
{
const std::vector<TestOp> script{
{
0ms, TestOp::SEND,
{
0x02, 0x01, 0x00, 0x00, // version, opcode
0x00, 0x00, 0x03, 0xe8, // lifetime
0x2a, 0x10, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, // internal IP
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, // nonce
0x06, 0x00, 0x00, 0x00, // protocol (TCP), reserved
0x04, 0xd2, 0x04, 0xd2, // internal port, suggested external port
0x2a, 0x10, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, // suggested external IP
}, 0
},
{
2001ms, TestOp::NOP, // Takes longer to respond than timeout of 2000ms
{}, 0
},
{
0ms, TestOp::SEND, // Repeated send (try 2)
{
0x02, 0x01, 0x00, 0x00, // version, opcode
0x00, 0x00, 0x03, 0xe8, // lifetime
0x2a, 0x10, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, // internal IP
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, // nonce
0x06, 0x00, 0x00, 0x00, // protocol (TCP), reserved
0x04, 0xd2, 0x04, 0xd2, // internal port, suggested external port
0x2a, 0x10, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, // suggested external IP
}, 0
},
{
200ms, TestOp::RECV, // This time we're in time
{
0x02, 0x81, 0x00, 0x00, // version, opcode, result success
0x00, 0x00, 0x01, 0xf4, // granted lifetime
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, // nonce
0x06, 0x00, 0x00, 0x00, // protocol (TCP), reserved
0x04, 0xd2, 0x04, 0xd2, // internal port, assigned external port
0x2a, 0x10, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, // suggested external IP
}, 0
},
};
CreateSock = [this, &script](int domain, int type, int protocol) {
if (domain == AF_INET6 && type == SOCK_DGRAM && protocol == IPPROTO_UDP) return std::make_unique<PCPTestSock>(default_local_ipv6, default_gateway_ipv6, script);
return std::unique_ptr<PCPTestSock>();
};
ASSERT_DEBUG_LOG("pcp: Retrying (1)");
ASSERT_DEBUG_LOG("pcp: Timeout");
auto res = PCPRequestPortMap(TEST_NONCE, default_gateway_ipv6, default_local_ipv6, 1234, 1000, 2, 2000ms);
BOOST_CHECK(std::get_if<MappingResult>(&res));
}
// PCP IPv4 failure (no resources).
BOOST_AUTO_TEST_CASE(pcp_ipv4_fail_no_resources)
{
const std::vector<TestOp> script{
{
0ms, TestOp::SEND,
{
0x02, 0x01, 0x00, 0x00, // version, opcode
0x00, 0x00, 0x03, 0xe8, // lifetime
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xc0, 0xa8, 0x00, 0x06, // internal IP
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, // nonce
0x06, 0x00, 0x00, 0x00, // protocol (TCP), reserved
0x04, 0xd2, 0x04, 0xd2, // internal port, suggested external port
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, // suggested external IP
}, 0
},
{
500ms, TestOp::RECV,
{
0x02, 0x81, 0x00, 0x08, // version, opcode, result 0x08: no resources
0x00, 0x00, 0x00, 0x00, // granted lifetime
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, // nonce
0x06, 0x00, 0x00, 0x00, // protocol (TCP), reserved
0x04, 0xd2, 0x00, 0x00, // internal port, assigned external port
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // assigned external IP
}, 0
},
};
CreateSock = [this, &script](int domain, int type, int protocol) {
if (domain == AF_INET && type == SOCK_DGRAM && protocol == IPPROTO_UDP) return std::make_unique<PCPTestSock>(default_local_ipv4, default_gateway_ipv4, script);
return std::unique_ptr<PCPTestSock>();
};
auto res = PCPRequestPortMap(TEST_NONCE, default_gateway_ipv4, bind_any_ipv4, 1234, 1000, 3, 1000ms);
MappingError* err = std::get_if<MappingError>(&res);
BOOST_REQUIRE(err);
BOOST_CHECK_EQUAL(*err, MappingError::NO_RESOURCES);
}
// PCP IPv4 failure (test NATPMP downgrade scenario).
BOOST_AUTO_TEST_CASE(pcp_ipv4_fail_unsupported_version)
{
const std::vector<TestOp> script{
{
0ms, TestOp::SEND,
{
0x02, 0x01, 0x00, 0x00, // version, opcode
0x00, 0x00, 0x03, 0xe8, // lifetime
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xc0, 0xa8, 0x00, 0x06, // internal IP
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, // nonce
0x06, 0x00, 0x00, 0x00, // protocol (TCP), reserved
0x04, 0xd2, 0x04, 0xd2, // internal port, suggested external port
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, // suggested external IP
}, 0
},
{
500ms, TestOp::RECV,
{
0x00, 0x81, 0x00, 0x01, // version, opcode, result 0x01: unsupported version
0x00, 0x00, 0x00, 0x00,
}, 0
},
};
CreateSock = [this, &script](int domain, int type, int protocol) {
if (domain == AF_INET && type == SOCK_DGRAM && protocol == IPPROTO_UDP) return std::make_unique<PCPTestSock>(default_local_ipv4, default_gateway_ipv4, script);
return std::unique_ptr<PCPTestSock>();
};
auto res = PCPRequestPortMap(TEST_NONCE, default_gateway_ipv4, bind_any_ipv4, 1234, 1000, 3, 1000ms);
MappingError* err = std::get_if<MappingError>(&res);
BOOST_REQUIRE(err);
BOOST_CHECK_EQUAL(*err, MappingError::UNSUPP_VERSION);
}
// NAT-PMP IPv4 protocol error scenarii.
BOOST_AUTO_TEST_CASE(natpmp_protocol_error)
{
// First scenario: non-0 result code when requesting external IP.
std::vector<TestOp> script{
{
0ms, TestOp::SEND,
{
0x00, 0x00, // version, opcode (request external IP)
}, 0
},
{
2ms, TestOp::RECV,
{
0x00, 0x80, 0x00, 0x42, // version, opcode (external IP), result code (*NOT* success)
0x66, 0xfd, 0xa1, 0xee, // seconds sinds start of epoch
0x01, 0x02, 0x03, 0x04, // external IP address
}, 0
},
};
CreateSock = [this, &script](int domain, int type, int protocol) {
if (domain == AF_INET && type == SOCK_DGRAM && protocol == IPPROTO_UDP) return std::make_unique<PCPTestSock>(default_local_ipv4, default_gateway_ipv4, script);
return std::unique_ptr<PCPTestSock>();
};
auto res = NATPMPRequestPortMap(default_gateway_ipv4, 1234, 1000, 1, 200ms);
MappingError* err = std::get_if<MappingError>(&res);
BOOST_REQUIRE(err);
BOOST_CHECK_EQUAL(*err, MappingError::PROTOCOL_ERROR);
// First scenario: non-0 result code when requesting port mapping.
script = {
{
0ms, TestOp::SEND,
{
0x00, 0x00, // version, opcode (request external IP)
}, 0
},
{
2ms, TestOp::RECV,
{
0x00, 0x80, 0x00, 0x00, // version, opcode (external IP), result code (success)
0x66, 0xfd, 0xa1, 0xee, // seconds sinds start of epoch
0x01, 0x02, 0x03, 0x04, // external IP address
}, 0
},
{
0ms, TestOp::SEND,
{
0x00, 0x02, 0x00, 0x00, // version, opcode (request map TCP)
0x04, 0xd2, 0x04, 0xd2, // internal port, suggested external port
0x00, 0x00, 0x03, 0xe8, // requested mapping lifetime in seconds
}, 0
},
{
2ms, TestOp::RECV,
{
0x00, 0x82, 0x00, 0x43, // version, opcode (mapped TCP)
0x66, 0xfd, 0xa1, 0xee, // seconds sinds start of epoch
0x04, 0xd2, 0x04, 0xd2, // internal port, mapped external port
0x00, 0x00, 0x01, 0xf4, // mapping lifetime in seconds
}, 0
},
};
CreateSock = [this, &script](int domain, int type, int protocol) {
if (domain == AF_INET && type == SOCK_DGRAM && protocol == IPPROTO_UDP) return std::make_unique<PCPTestSock>(default_local_ipv4, default_gateway_ipv4, script);
return std::unique_ptr<PCPTestSock>();
};
res = NATPMPRequestPortMap(default_gateway_ipv4, 1234, 1000, 1, 200ms);
err = std::get_if<MappingError>(&res);
BOOST_REQUIRE(err);
BOOST_CHECK_EQUAL(*err, MappingError::PROTOCOL_ERROR);
}
// PCP IPv4 protocol error scenario.
BOOST_AUTO_TEST_CASE(pcp_protocol_error)
{
const std::vector<TestOp> script{
{
0ms, TestOp::SEND,
{
0x02, 0x01, 0x00, 0x00, // version, opcode
0x00, 0x00, 0x03, 0xe8, // lifetime
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xc0, 0xa8, 0x00, 0x06, // internal IP
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, // nonce
0x06, 0x00, 0x00, 0x00, // protocol (TCP), reserved
0x04, 0xd2, 0x04, 0xd2, // internal port, suggested external port
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, // suggested external IP
}, 0
},
{
250ms, TestOp::RECV, // 250ms delay before answer
{
0x02, 0x81, 0x00, 0x42, // version, opcode, result error
0x00, 0x00, 0x01, 0xf4, // granted lifetime
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // reserved
0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, // nonce
0x06, 0x00, 0x00, 0x00, // protocol (TCP), reserved
0x04, 0xd2, 0x04, 0xd2, // internal port, assigned external port
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03, 0x04, // assigned external IP
}, 0
},
};
CreateSock = [this, &script](int domain, int type, int protocol) {
if (domain == AF_INET && type == SOCK_DGRAM && protocol == IPPROTO_UDP) return std::make_unique<PCPTestSock>(default_local_ipv4, default_gateway_ipv4, script);
return std::unique_ptr<PCPTestSock>();
};
auto res = PCPRequestPortMap(TEST_NONCE, default_gateway_ipv4, bind_any_ipv4, 1234, 1000, 1, 1000ms);
MappingError* err = std::get_if<MappingError>(&res);
BOOST_REQUIRE(err);
BOOST_CHECK_EQUAL(*err, MappingError::PROTOCOL_ERROR);
}
BOOST_AUTO_TEST_SUITE_END()