forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevo_netinfo_tests.cpp
More file actions
649 lines (569 loc) · 29.7 KB
/
Copy pathevo_netinfo_tests.cpp
File metadata and controls
649 lines (569 loc) · 29.7 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
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
// Copyright (c) 2025 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <test/util/setup_common.h>
#include <evo/netinfo.h>
#include <util/helpers.h>
#include <chainparams.h>
#include <clientversion.h>
#include <netbase.h>
#include <streams.h>
#include <util/strencodings.h>
#include <boost/test/unit_test.hpp>
BOOST_FIXTURE_TEST_SUITE(evo_netinfo_tests, BasicTestingSetup)
struct TestEntry {
std::pair</*purpose=*/NetInfoPurpose, /*addr=*/std::string> input;
NetInfoStatus expected_ret_mn{NetInfoStatus::BadInput};
NetInfoStatus expected_ret_ext{NetInfoStatus::BadInput};
};
static const std::vector<TestEntry> addr_vals_main{
// Address and port specified
{{NetInfoPurpose::CORE_P2P, "1.1.1.1:9999"}, NetInfoStatus::Success, NetInfoStatus::Success},
// - Port should default to default P2P core with MnNetInfo
// - Ports are no longer implied with ExtNetInfo
{{NetInfoPurpose::CORE_P2P, "1.1.1.1"}, NetInfoStatus::Success, NetInfoStatus::BadPort},
// - Non-mainnet port on mainnet causes failure in MnNetInfo
// - ExtNetInfo is indifferent to choice of port unless it's a bad port which 9998 isn't
{{NetInfoPurpose::CORE_P2P, "1.1.1.1:9998"}, NetInfoStatus::BadPort, NetInfoStatus::Success},
// Internal addresses not allowed on mainnet
{{NetInfoPurpose::CORE_P2P, "127.0.0.1:9999"}, NetInfoStatus::NotRoutable, NetInfoStatus::NotRoutable},
// Valid IPv4 formatting but invalid IPv4 address
{{NetInfoPurpose::CORE_P2P, "0.0.0.0:9999"}, NetInfoStatus::BadAddress, NetInfoStatus::BadAddress},
// Port greater than uint16_t max
{{NetInfoPurpose::CORE_P2P, "1.1.1.1:99999"}, NetInfoStatus::BadInput, NetInfoStatus::BadInput},
// - Non-IPv4 addresses are prohibited in MnNetInfo
// - Any valid BIP155 address is allowed in ExtNetInfo
{{NetInfoPurpose::CORE_P2P, "[2606:4700:4700::1111]:9999"}, NetInfoStatus::BadInput, NetInfoStatus::Success},
// - MnNetInfo doesn't allow storing anything except a Core P2P address
// - Privacy network domains are allowed in ExtNetInfo but internet domains are not
{{NetInfoPurpose::CORE_P2P, "example.com:9999"}, NetInfoStatus::BadInput, NetInfoStatus::BadInput},
{{NetInfoPurpose::CORE_P2P, "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion:9999"}, NetInfoStatus::BadInput, NetInfoStatus::Success},
{{NetInfoPurpose::PLATFORM_P2P, "example.com:9999"}, NetInfoStatus::MaxLimit, NetInfoStatus::BadInput},
{{NetInfoPurpose::PLATFORM_P2P, "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion:9999"}, NetInfoStatus::MaxLimit, NetInfoStatus::Success},
// - MnNetInfo doesn't allow storing anything except a Core P2P address
// - ExtNetInfo can store Platform HTTPS addresses *as domains* alongside privacy network domains
{{NetInfoPurpose::PLATFORM_HTTPS, "example.com:9999"}, NetInfoStatus::MaxLimit, NetInfoStatus::Success},
{{NetInfoPurpose::PLATFORM_HTTPS, "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion:9999"}, NetInfoStatus::MaxLimit, NetInfoStatus::Success},
// Incorrect IPv4 address
{{NetInfoPurpose::CORE_P2P, "1.1.1.256:9999"}, NetInfoStatus::BadInput, NetInfoStatus::BadInput},
// Missing address
{{NetInfoPurpose::CORE_P2P, ":9999"}, NetInfoStatus::BadInput, NetInfoStatus::BadInput},
// Bad purpose code
{{static_cast<NetInfoPurpose>(64), "1.1.1.1:9999"}, NetInfoStatus::MaxLimit, NetInfoStatus::MaxLimit},
// - MnNetInfo doesn't allow storing anything except a Core P2P address
// - ExtNetInfo allows storing Platform P2P addresses
{{NetInfoPurpose::PLATFORM_P2P, "1.1.1.1:9999"}, NetInfoStatus::MaxLimit, NetInfoStatus::Success},
};
void ValidateGetEntries(const NetInfoList& entries, const size_t expected_size)
{
BOOST_CHECK_EQUAL(entries.size(), expected_size);
for (const NetInfoEntry& entry : entries) {
BOOST_CHECK(entry.IsTriviallyValid());
}
}
void TestMnNetInfo(const std::vector<TestEntry>& vals)
{
for (const auto& [input, expected_ret, _] : vals) {
const auto& [purpose, addr] = input;
MnNetInfo netInfo;
BOOST_CHECK_EQUAL(netInfo.AddEntry(purpose, addr), expected_ret);
if (expected_ret != NetInfoStatus::Success) {
// An empty MnNetInfo is considered malformed
BOOST_CHECK_EQUAL(netInfo.Validate(), NetInfoStatus::Malformed);
BOOST_CHECK(!netInfo.HasEntries(purpose));
BOOST_CHECK(netInfo.GetEntries().empty());
} else {
BOOST_CHECK_EQUAL(netInfo.Validate(), NetInfoStatus::Success);
BOOST_CHECK(netInfo.HasEntries(purpose));
ValidateGetEntries(netInfo.GetEntries(), /*expected_size=*/1);
}
}
}
void TestExtNetInfo(const std::vector<TestEntry>& vals)
{
for (const auto& [input, _, expected_ret] : vals) {
const auto& [purpose, addr] = input;
ExtNetInfo netInfo;
BOOST_CHECK_EQUAL(netInfo.AddEntry(purpose, addr), expected_ret);
if (expected_ret != NetInfoStatus::Success) {
// An empty ExtNetInfo is considered malformed
BOOST_CHECK_EQUAL(netInfo.Validate(), NetInfoStatus::Malformed);
BOOST_CHECK(!netInfo.HasEntries(purpose));
BOOST_CHECK(netInfo.GetEntries().empty());
} else {
BOOST_CHECK_EQUAL(netInfo.Validate(), NetInfoStatus::Success);
BOOST_CHECK(netInfo.HasEntries(purpose));
ValidateGetEntries(netInfo.GetEntries(), /*expected_size=*/1);
}
}
}
BOOST_AUTO_TEST_CASE(mnnetinfo_rules_main)
{
TestMnNetInfo(addr_vals_main);
{
// MnNetInfo only stores one value, overwriting prohibited
MnNetInfo netInfo;
BOOST_CHECK_EQUAL(netInfo.AddEntry(NetInfoPurpose::CORE_P2P, "1.1.1.1:9999"), NetInfoStatus::Success);
BOOST_CHECK_EQUAL(netInfo.AddEntry(NetInfoPurpose::CORE_P2P, "1.1.1.2:9999"), NetInfoStatus::MaxLimit);
BOOST_CHECK(netInfo.HasEntries(NetInfoPurpose::CORE_P2P));
ValidateGetEntries(netInfo.GetEntries(), /*expected_size=*/1);
}
{
// MnNetInfo only allows storing a Core P2P address
MnNetInfo netInfo;
for (const auto purpose : {NetInfoPurpose::PLATFORM_HTTPS, NetInfoPurpose::PLATFORM_P2P}) {
BOOST_CHECK_EQUAL(netInfo.AddEntry(purpose, "1.1.1.1:9999"), NetInfoStatus::MaxLimit);
BOOST_CHECK(!netInfo.HasEntries(purpose));
}
BOOST_CHECK(netInfo.GetEntries().empty());
}
}
BOOST_AUTO_TEST_CASE(extnetinfo_rules_main) { TestExtNetInfo(addr_vals_main); }
static const std::vector<TestEntry> addr_vals_reg{
// - MnNetInfo doesn't mind using port 0
// - ExtNetInfo requires non-zero ports
{{NetInfoPurpose::CORE_P2P, "1.1.1.1:0"}, NetInfoStatus::Success, NetInfoStatus::BadPort},
// - Mainnet P2P port on non-mainnet cause failure in MnNetInfo
// - ExtNetInfo is indifferent to choice of port unless it's a bad port which 9999 isn't
{{NetInfoPurpose::CORE_P2P, "1.1.1.1:9999"}, NetInfoStatus::BadPort, NetInfoStatus::Success},
// - Non-mainnet P2P port is allowed in MnNetInfo regardless of bad port status
// - Port 22 (SSH) is below the privileged ports threshold (1023) and is therefore a bad port, disallowed in ExtNetInfo
{{NetInfoPurpose::CORE_P2P, "1.1.1.1:22"}, NetInfoStatus::Success, NetInfoStatus::BadPort},
};
enum class ExpectedType : uint8_t {
CJDNS,
I2P,
Tor,
};
static const std::vector<std::tuple</*type=*/ExpectedType, /*input=*/std::string, /*expected_ret=*/NetInfoStatus>> privacy_addr_vals{
{ExpectedType::CJDNS, "[fc00:3344:5566:7788:9900:aabb:ccdd:eeff]:9998", NetInfoStatus::Success},
// ExtNetInfo can store I2P addresses as long as it uses port 0
{ExpectedType::I2P, "udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.i2p:0", NetInfoStatus::Success},
// ExtNetInfo can store onion addresses
{ExpectedType::Tor, "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion:9998", NetInfoStatus::Success},
// ExtNetInfo can store I2P addresses but non-zero ports are not allowed
{ExpectedType::I2P, "udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.i2p:9998", NetInfoStatus::BadPort},
// ExtNetInfo can store onion addresses but zero ports are not allowed
{ExpectedType::Tor, "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion:0", NetInfoStatus::BadPort},
};
BOOST_FIXTURE_TEST_CASE(mnnetinfo_rules_reg, RegTestingSetup) { TestMnNetInfo(addr_vals_reg); }
BOOST_FIXTURE_TEST_CASE(extnetinfo_rules_reg, RegTestingSetup)
{
TestExtNetInfo(addr_vals_reg);
{
// ExtNetInfo can store up to 4 entries per purpose code, check limit enforcement
ExtNetInfo netInfo;
for (size_t idx{1}; idx <= MAX_ENTRIES_EXTNETINFO; idx++) {
BOOST_CHECK_EQUAL(netInfo.AddEntry(NetInfoPurpose::CORE_P2P, strprintf("1.1.1.%d:9998", idx)),
NetInfoStatus::Success);
}
BOOST_CHECK_EQUAL(netInfo.AddEntry(NetInfoPurpose::CORE_P2P, "1.1.1.5:9998"), NetInfoStatus::MaxLimit);
BOOST_CHECK(netInfo.HasEntries(NetInfoPurpose::CORE_P2P));
// The limit applies *per purpose code* and therefore wouldn't error if the address was for a different purpose
BOOST_CHECK(!netInfo.HasEntries(NetInfoPurpose::PLATFORM_P2P));
BOOST_CHECK_EQUAL(netInfo.AddEntry(NetInfoPurpose::PLATFORM_P2P, "1.1.1.5:9998"), NetInfoStatus::Success);
BOOST_CHECK(netInfo.HasEntries(NetInfoPurpose::PLATFORM_P2P));
BOOST_CHECK_EQUAL(netInfo.Validate(), NetInfoStatus::Success);
// GetEntries() is a tally of all entries across all purpose codes
ValidateGetEntries(netInfo.GetEntries(), /*expected_size=*/MAX_ENTRIES_EXTNETINFO + 1);
}
{
// ExtNetInfo has restrictions on duplicates
ExtNetInfo netInfo;
BOOST_CHECK_EQUAL(netInfo.AddEntry(NetInfoPurpose::CORE_P2P, "1.1.1.1:9998"), NetInfoStatus::Success);
// Exact (i.e. addr:port) duplicates are prohibited *within* a list
BOOST_CHECK_EQUAL(netInfo.AddEntry(NetInfoPurpose::CORE_P2P, "1.1.1.1:9998"), NetInfoStatus::Duplicate);
// Partial (i.e. different port) duplicates are prohibited *within* a list
BOOST_CHECK_EQUAL(netInfo.AddEntry(NetInfoPurpose::CORE_P2P, "1.1.1.1:9997"), NetInfoStatus::Duplicate);
// Exact (i.e. addr:port) duplicates are prohibited *across* lists
BOOST_CHECK_EQUAL(netInfo.AddEntry(NetInfoPurpose::PLATFORM_P2P, "1.1.1.1:9998"), NetInfoStatus::Duplicate);
// Partial (i.e. different port) duplicates are allowed *across* a list
BOOST_CHECK_EQUAL(netInfo.AddEntry(NetInfoPurpose::PLATFORM_P2P, "1.1.1.1:9997"), NetInfoStatus::Success);
BOOST_CHECK_EQUAL(netInfo.Validate(), NetInfoStatus::Success);
BOOST_CHECK(netInfo.HasEntries(NetInfoPurpose::CORE_P2P));
BOOST_CHECK(netInfo.HasEntries(NetInfoPurpose::PLATFORM_P2P));
BOOST_CHECK(!netInfo.HasEntries(NetInfoPurpose::PLATFORM_HTTPS));
ValidateGetEntries(netInfo.GetEntries(), /*expected_size=*/2);
}
{
// ExtNetInfo has additional rules for domains
const std::vector<TestEntry> domain_vals{
// Port 80 (HTTP) is below the privileged ports threshold (1023), not allowed
{{NetInfoPurpose::PLATFORM_HTTPS, "example.com:80"}, NetInfoStatus::MaxLimit, NetInfoStatus::BadPort},
// Port 443 (HTTPS) is below the privileged ports threshold (1023) but still allowed
{{NetInfoPurpose::PLATFORM_HTTPS, "example.com:443"}, NetInfoStatus::MaxLimit, NetInfoStatus::Success},
// TLDs must be alphabetic to avoid ambiguation with IP addresses (per ICANN guidelines)
{{NetInfoPurpose::PLATFORM_HTTPS, "example.123:443"}, NetInfoStatus::MaxLimit, NetInfoStatus::BadInput},
// .local is a prohibited TLD
{{NetInfoPurpose::PLATFORM_HTTPS, "somebodys-macbook-pro.local:9998"}, NetInfoStatus::MaxLimit, NetInfoStatus::BadInput},
// DomainPort isn't used for storing privacy network TLDs like .onion
{{NetInfoPurpose::PLATFORM_HTTPS, "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd:9998"}, NetInfoStatus::MaxLimit, NetInfoStatus::BadInput},
};
TestExtNetInfo(domain_vals);
}
// Privacy network entry checks
for (const auto& [type, input, expected_ret] : privacy_addr_vals) {
const bool expected_success{expected_ret == NetInfoStatus::Success};
ExtNetInfo netInfo{};
BOOST_CHECK_EQUAL(netInfo.AddEntry(NetInfoPurpose::CORE_P2P, input), expected_ret);
ValidateGetEntries(netInfo.GetEntries(), /*expected_size=*/expected_success ? 1 : 0);
if (!expected_success) continue;
// Type registration check
const CService service{netInfo.GetEntries().at(0).GetAddrPort().value()};
BOOST_CHECK(service.IsValid());
switch (type) {
case ExpectedType::CJDNS:
BOOST_CHECK(service.IsCJDNS());
break;
case ExpectedType::I2P:
BOOST_CHECK(service.IsI2P());
break;
case ExpectedType::Tor:
BOOST_CHECK(service.IsTor());
break;
} // no default case, so the compiler can warn about missing cases
}
}
BOOST_AUTO_TEST_CASE(netinfo_ser)
{
{
// An empty object should only store one byte to denote it is invalid
CDataStream ds(SER_DISK, CLIENT_VERSION);
NetInfoEntry entry{};
ds << entry;
BOOST_CHECK_EQUAL(ds.size(), sizeof(uint8_t));
}
{
// Reading a nonsense byte should return an empty object
CDataStream ds(SER_DISK, CLIENT_VERSION);
NetInfoEntry entry{};
ds << 0xfe;
ds >> entry;
BOOST_CHECK(entry.IsEmpty() && !entry.IsTriviallyValid());
}
{
// Reading an invalid CService should fail trivial validation and return an empty object
CDataStream ds(SER_DISK, CLIENT_VERSION);
NetInfoEntry entry{};
ds << NetInfoEntry::NetInfoType::Service << CService{};
ds >> entry;
BOOST_CHECK(entry.IsEmpty() && !entry.IsTriviallyValid());
}
{
// Reading an unrecognized type should fail trivial validation and return an empty object
CDataStream ds(SER_DISK, CLIENT_VERSION);
NetInfoEntry entry{};
ds << NetInfoEntry::NetInfoType::Service << uint256{};
ds >> entry;
BOOST_CHECK(entry.IsEmpty() && !entry.IsTriviallyValid());
}
{
// A valid CService should be constructable, readable and pass validation
CDataStream ds(SER_DISK, CLIENT_VERSION | ADDRV2_FORMAT);
CService service{LookupNumeric("1.1.1.1", Params().GetDefaultPort())};
BOOST_CHECK(service.IsValid());
NetInfoEntry entry{service}, entry2{};
ds << NetInfoEntry::NetInfoType::Service << service;
ds >> entry2;
BOOST_CHECK(entry == entry2);
BOOST_CHECK(!entry.IsEmpty() && entry.IsTriviallyValid());
BOOST_CHECK(entry.GetAddrPort().value() == service);
}
{
// NetInfoEntry should be able to read and write ADDRV2 addresses
CService service{};
service.SetSpecial("pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion");
BOOST_CHECK(service.IsValid() && service.IsTor());
CDataStream ds(SER_DISK, CLIENT_VERSION | ADDRV2_FORMAT);
ds << NetInfoEntry::NetInfoType::Service << service;
ds.SetVersion(CLIENT_VERSION); // Drop the explicit format flag
NetInfoEntry entry{};
ds >> entry;
BOOST_CHECK(!entry.IsEmpty() && entry.IsTriviallyValid());
BOOST_CHECK(entry.GetAddrPort().value() == service);
ds.clear();
NetInfoEntry entry2{};
ds << entry;
ds >> entry2;
BOOST_CHECK(entry == entry2 && entry2.GetAddrPort().value() == service);
}
}
BOOST_AUTO_TEST_CASE(netinfo_retvals)
{
uint16_t p2p_port{Params().GetDefaultPort()};
CService service{LookupNumeric("1.1.1.1", p2p_port)}, service2{LookupNumeric("1.1.1.2", p2p_port)};
NetInfoEntry entry{service}, entry2{service2}, entry_empty{};
// Check that values are correctly recorded and pass trivial validation
BOOST_CHECK(service.IsValid());
BOOST_CHECK(!entry.IsEmpty() && entry.IsTriviallyValid());
BOOST_CHECK(entry.GetAddrPort().value() == service);
BOOST_CHECK(!entry2.IsEmpty() && entry2.IsTriviallyValid());
BOOST_CHECK(entry2.GetAddrPort().value() == service2);
// Check that dispatch returns the expected values
BOOST_CHECK_EQUAL(entry.GetPort(), service.GetPort());
BOOST_CHECK_EQUAL(entry.ToString(), strprintf("CService(addr=%s, port=%u)", service.ToStringAddr(), service.GetPort()));
BOOST_CHECK_EQUAL(entry.ToStringAddr(), service.ToStringAddr());
BOOST_CHECK_EQUAL(entry.ToStringAddrPort(), service.ToStringAddrPort());
BOOST_CHECK_EQUAL(service < service2, entry < entry2);
// Check that empty/invalid entries return error messages
BOOST_CHECK_EQUAL(entry_empty.GetPort(), 0);
BOOST_CHECK_EQUAL(entry_empty.ToString(), "[invalid entry]");
BOOST_CHECK_EQUAL(entry_empty.ToStringAddr(), "[invalid entry]");
BOOST_CHECK_EQUAL(entry_empty.ToStringAddrPort(), "[invalid entry]");
// The invalid entry type code is 0xff (highest possible value) and therefore will return as greater
// in comparison to any valid entry
BOOST_CHECK(entry < entry_empty);
}
bool CheckIfSerSame(const CService& lhs, const MnNetInfo& rhs)
{
CHashWriter ss_lhs(SER_GETHASH, 0), ss_rhs(SER_GETHASH, 0);
ss_lhs << lhs;
ss_rhs << rhs;
return ss_lhs.GetSHA256() == ss_rhs.GetSHA256();
}
BOOST_AUTO_TEST_CASE(cservice_compatible)
{
// Empty values should be the same
CService service;
MnNetInfo netInfo;
BOOST_CHECK(CheckIfSerSame(service, netInfo));
// Valid IPv4 address, valid port
service = LookupNumeric("1.1.1.1", 9999);
netInfo.Clear();
BOOST_CHECK_EQUAL(netInfo.AddEntry(NetInfoPurpose::CORE_P2P, "1.1.1.1:9999"), NetInfoStatus::Success);
BOOST_CHECK(CheckIfSerSame(service, netInfo));
// Valid IPv4 address, default P2P port implied
service = LookupNumeric("1.1.1.1", Params().GetDefaultPort());
netInfo.Clear();
BOOST_CHECK_EQUAL(netInfo.AddEntry(NetInfoPurpose::CORE_P2P, "1.1.1.1"), NetInfoStatus::Success);
BOOST_CHECK(CheckIfSerSame(service, netInfo));
// Lookup() failure (domains not allowed), MnNetInfo should remain empty if Lookup() failed
service = CService();
netInfo.Clear();
BOOST_CHECK_EQUAL(netInfo.AddEntry(NetInfoPurpose::CORE_P2P, "example.com"), NetInfoStatus::BadInput);
BOOST_CHECK(CheckIfSerSame(service, netInfo));
// Validation failure (non-IPv4 not allowed), MnNetInfo should remain empty if ValidateService() failed
service = CService();
netInfo.Clear();
BOOST_CHECK_EQUAL(netInfo.AddEntry(NetInfoPurpose::CORE_P2P, "[2606:4700:4700::1111]:9999"), NetInfoStatus::BadInput);
BOOST_CHECK(CheckIfSerSame(service, netInfo));
}
BOOST_AUTO_TEST_CASE(interface_equality)
{
// We also check for symmetry as NetInfoInterface, ExtNetInfo, MnNetInfo and NetInfoEntry
// define their operator!= as the inverse of operator==
std::shared_ptr<NetInfoInterface> ptr_lhs{nullptr}, ptr_rhs{nullptr};
// Equal initialization state (uninitialized)
BOOST_CHECK(util::shared_ptr_equal(ptr_lhs, ptr_rhs) && !util::shared_ptr_not_equal(ptr_lhs, ptr_rhs));
// Unequal initialization state (lhs initialized, rhs unchanged)
ptr_lhs = std::make_shared<MnNetInfo>();
BOOST_CHECK(!util::shared_ptr_equal(ptr_lhs, ptr_rhs) && util::shared_ptr_not_equal(ptr_lhs, ptr_rhs));
// Equal initialization state (lhs unchanged, rhs initialized), same values
ptr_rhs = std::make_shared<MnNetInfo>();
BOOST_CHECK(ptr_lhs->IsEmpty() && ptr_rhs->IsEmpty());
BOOST_CHECK(util::shared_ptr_equal(ptr_lhs, ptr_rhs) && !util::shared_ptr_not_equal(ptr_lhs, ptr_rhs));
// Equal initialization state, same type, differing values
BOOST_CHECK_EQUAL(ptr_rhs->AddEntry(NetInfoPurpose::CORE_P2P, "1.1.1.1:9999"), NetInfoStatus::Success);
BOOST_CHECK(!util::shared_ptr_equal(ptr_lhs, ptr_rhs) && util::shared_ptr_not_equal(ptr_lhs, ptr_rhs));
// Equal initialization state, different type, same values
ptr_rhs = std::make_shared<ExtNetInfo>();
BOOST_CHECK(ptr_lhs->IsEmpty() && ptr_rhs->IsEmpty());
BOOST_CHECK(!util::shared_ptr_equal(ptr_lhs, ptr_rhs) && util::shared_ptr_not_equal(ptr_lhs, ptr_rhs));
// Equal initialization state, same type, same values
ptr_lhs = std::make_shared<ExtNetInfo>();
BOOST_CHECK(ptr_lhs->IsEmpty() && ptr_rhs->IsEmpty());
BOOST_CHECK(util::shared_ptr_equal(ptr_lhs, ptr_rhs) && !util::shared_ptr_not_equal(ptr_lhs, ptr_rhs));
// Equal initialization state, same type, differing values
BOOST_CHECK_EQUAL(ptr_rhs->AddEntry(NetInfoPurpose::CORE_P2P, "1.1.1.1:9999"), NetInfoStatus::Success);
BOOST_CHECK(!util::shared_ptr_equal(ptr_lhs, ptr_rhs) && util::shared_ptr_not_equal(ptr_lhs, ptr_rhs));
}
BOOST_AUTO_TEST_CASE(domainport_rules)
{
static const std::vector<std::pair</*addr=*/std::string, /*retval=*/DomainPort::Status>> domain_vals{
// Domain name labels can be as small as one character long and remain valid
{"r.server-1.ab.cd", DomainPort::Status::Success},
// Domain names labels can trail with numbers or consist entirely of numbers due to RFC 1123
{"9998.9example7.ab", DomainPort::Status::Success},
// dotless domains prohibited
{"abcd", DomainPort::Status::BadDotless},
// no empty label (trailing delimiter)
{"abc.", DomainPort::Status::BadCharPos},
// no empty label (leading delimiter)
{".abc", DomainPort::Status::BadCharPos},
// no empty label (extra delimiters)
{"a..dot..b", DomainPort::Status::BadLabelLen},
// ' is not a valid character in domains
{"somebody's macbook pro.local", DomainPort::Status::BadChar},
// spaces are not a valid character in domains
{"somebodys macbook pro.local", DomainPort::Status::BadChar},
// trailing hyphens are not allowed
{"-a-.bc.de", DomainPort::Status::BadLabelCharPos},
// 2 (characters in domain) < 3 (minimum length)
{"ac", DomainPort::Status::BadLen},
// 278 (characters in domain) > 253 (maximum limit)
{"Loremipsumdolorsitametconsecteturadipiscingelitseddoeiusmodtempor"
"incididuntutlaboreetdoloremagnaaliquaUtenimadminimveniamquisnostrud"
"exercitationullamcolaborisnisiutaliquipexeacommodoconsequatDuisaute"
"iruredolorinreprehenderitinvoluptatevelitessecillumdoloreeufugiatnullapariat.ur", DomainPort::Status::BadLen},
// 64 (characters in label) > 63 (maximum limit)
{"loremipsumdolorsitametconsecteturadipiscingelitseddoeiusmodtempo.ri.nc", DomainPort::Status::BadLabelLen},
};
for (const auto& [addr, retval] : domain_vals) {
DomainPort domain;
ExtNetInfo netInfo;
BOOST_CHECK_EQUAL(domain.Set(addr, 443), retval);
if (retval != DomainPort::Status::Success) {
BOOST_CHECK_EQUAL(domain.Validate(), DomainPort::Status::Malformed); // Empty values report as Malformed
BOOST_CHECK_EQUAL(netInfo.AddEntry(NetInfoPurpose::PLATFORM_HTTPS, domain.ToStringAddrPort()),
NetInfoStatus::BadInput);
} else {
BOOST_CHECK_EQUAL(domain.Validate(), DomainPort::Status::Success);
BOOST_CHECK_EQUAL(netInfo.AddEntry(NetInfoPurpose::PLATFORM_HTTPS, domain.ToStringAddrPort()), NetInfoStatus::Success);
}
}
{
// DomainPort requires non-zero ports
DomainPort domain;
BOOST_CHECK_EQUAL(domain.Set("example.com", 0), DomainPort::Status::BadPort);
BOOST_CHECK_EQUAL(domain.Validate(), DomainPort::Status::Malformed);
}
{
// DomainPort stores the domain in lower-case
DomainPort lhs, rhs;
BOOST_CHECK_EQUAL(lhs.Set("example.com", 9999), DomainPort::Status::Success);
BOOST_CHECK_EQUAL(rhs.Set(ToUpper("example.com"), 9999), DomainPort::Status::Success);
BOOST_CHECK_EQUAL(lhs.ToStringAddr(), rhs.ToStringAddr());
BOOST_CHECK(lhs == rhs);
}
}
// DomainPort used a bare std::string unserialize path (bounded only by
// MAX_SIZE == 32 MiB) and NetInfoEntry::Unserialize swallowed ios_base::failure.
// That combination allowed a tiny ProTx ExtNetInfo payload to force repeated
// allocate-and-zero of multi-megabyte strings. These tests pin the serialization
// layer to reject oversized domain claims without performing the large allocation.
static CDataStream MakeOversizedDomainPortStream(const size_t claimed_len)
{
CDataStream ds(SER_DISK, CLIENT_VERSION);
WriteCompactSize(ds, claimed_len);
// Provide a full body so pre-fix code can complete the string read; the
// bound must reject based on the CompactSize alone, not on a short read.
const std::string body(claimed_len, 'a');
if (!body.empty()) {
ds.write(MakeByteSpan(body));
}
ser_writedata16be(ds, 443);
return ds;
}
BOOST_AUTO_TEST_CASE(domainport_deser_rejects_oversized_string)
{
// DOMAIN_MAX_LEN is 253; a fully-formed 1000-byte domain must be rejected at
// deserialization time (LIMITED_STRING), not accepted then failed later in
// ValidateDomain.
constexpr size_t kOversized{1000};
CDataStream ds{MakeOversizedDomainPortStream(kOversized)};
DomainPort domain;
BOOST_CHECK_THROW(ds >> domain, std::ios_base::failure);
}
BOOST_AUTO_TEST_CASE(netinfoentry_domain_deser_rejects_oversized_string)
{
// NetInfoEntry must not swallow the length-limit failure: rethrow so the
// enclosing ProTx payload deserialization aborts and the peer is penalised.
constexpr size_t kOversized{1000};
CDataStream ds(SER_DISK, CLIENT_VERSION);
ds << uint8_t{NetInfoEntry::NetInfoType::Domain};
{
CDataStream body{MakeOversizedDomainPortStream(kOversized)};
ds.write(MakeByteSpan(body));
}
NetInfoEntry entry;
BOOST_CHECK_THROW(ds >> entry, std::ios_base::failure);
}
BOOST_AUTO_TEST_CASE(domainport_deser_rejects_huge_claimed_length)
{
// CompactSize claims 1 MiB with no body. Pre-fix this would allocate 1 MiB
// then throw on short-read; post-fix LIMITED_STRING rejects before resize.
// Either way the operation must throw; the important property is that a
// declared length above DOMAIN_MAX_LEN never produces a successful DomainPort.
CDataStream ds(SER_DISK, CLIENT_VERSION);
WriteCompactSize(ds, size_t{1} << 20);
DomainPort domain;
BOOST_CHECK_THROW(ds >> domain, std::ios_base::failure);
}
BOOST_AUTO_TEST_CASE(extnetinfo_domain_amplification_rejected)
{
// Simulate the attack shape: ExtNetInfo vector of Domain entries each
// claiming a large string length from a small stream. Pre-fix,
// NetInfoEntry swallows the short-read and the vector loop continues
// (stream position does not advance on the failed body read), producing
// repeated large allocations. Post-fix the first oversized claim throws
// and aborts the whole ExtNetInfo unserialize.
constexpr size_t kClaimed{1 << 16}; // 64 KiB — large enough to prove, small enough to not OOM pre-fix
constexpr size_t kEntries{8};
CDataStream ds(SER_DISK, CLIENT_VERSION);
ds << uint8_t{1}; // ExtNetInfo version
WriteCompactSize(ds, 1); // one purpose
ds << NetInfoPurpose::PLATFORM_HTTPS;
WriteCompactSize(ds, kEntries);
for (size_t i = 0; i < kEntries; ++i) {
ds << uint8_t{NetInfoEntry::NetInfoType::Domain};
// Claim kClaimed bytes but supply none — CompactSize only.
WriteCompactSize(ds, kClaimed);
}
ExtNetInfo netInfo;
BOOST_CHECK_THROW(ds >> netInfo, std::ios_base::failure);
}
BOOST_FIXTURE_TEST_CASE(extnetinfo_validate_deser, RegTestingSetup)
{
// The in-memory builder (AddEntry/ProcessCandidate) refuses duplicates, domains
// on non-HTTPS purposes, and lists exceeding MAX_ENTRIES_EXTNETINFO. None of those
// checks run on deserialization, so Validate() must catch them itself.
const uint16_t port{9998};
auto write_header = [](CDataStream& ds, size_t n_purposes) {
ds << uint8_t{1}; // current version
WriteCompactSize(ds, n_purposes);
};
{
// Two identical entries under the same purpose
CDataStream ds(SER_DISK, CLIENT_VERSION);
write_header(ds, 1);
ds << NetInfoPurpose::CORE_P2P;
WriteCompactSize(ds, 2);
const NetInfoEntry entry{LookupNumeric("1.1.1.1", port)};
BOOST_CHECK(entry.IsTriviallyValid());
ds << entry << entry;
ExtNetInfo netInfo;
ds >> netInfo;
BOOST_CHECK_EQUAL(netInfo.Validate(), NetInfoStatus::Duplicate);
}
{
// List exceeds MAX_ENTRIES_EXTNETINFO per purpose
CDataStream ds(SER_DISK, CLIENT_VERSION);
write_header(ds, 1);
ds << NetInfoPurpose::CORE_P2P;
WriteCompactSize(ds, MAX_ENTRIES_EXTNETINFO + 1);
for (size_t i = 1; i <= MAX_ENTRIES_EXTNETINFO + 1; ++i) {
const NetInfoEntry entry{LookupNumeric(strprintf("1.1.1.%d", i), port)};
BOOST_CHECK(entry.IsTriviallyValid());
ds << entry;
}
ExtNetInfo netInfo;
ds >> netInfo;
BOOST_CHECK_EQUAL(netInfo.Validate(), NetInfoStatus::MaxLimit);
}
{
// Domain entry under a non-PLATFORM_HTTPS purpose
DomainPort domain;
BOOST_CHECK_EQUAL(domain.Set("example.com", 443), DomainPort::Status::Success);
const NetInfoEntry entry{domain};
BOOST_CHECK(entry.IsTriviallyValid());
CDataStream ds(SER_DISK, CLIENT_VERSION);
write_header(ds, 1);
ds << NetInfoPurpose::CORE_P2P;
WriteCompactSize(ds, 1);
ds << entry;
ExtNetInfo netInfo;
ds >> netInfo;
BOOST_CHECK_EQUAL(netInfo.Validate(), NetInfoStatus::BadInput);
}
}
BOOST_AUTO_TEST_SUITE_END()