|
4 | 4 |
|
5 | 5 | #include <net_permissions.h> |
6 | 6 | #include <netbase.h> |
| 7 | +#include <protocol.h> |
| 8 | +#include <serialize.h> |
| 9 | +#include <streams.h> |
7 | 10 | #include <test/util/setup_common.h> |
8 | 11 | #include <util/strencodings.h> |
9 | 12 | #include <util/translation.h> |
| 13 | +#include <version.h> |
10 | 14 |
|
11 | 15 | #include <string> |
12 | 16 |
|
@@ -443,4 +447,105 @@ BOOST_AUTO_TEST_CASE(netbase_dont_resolve_strings_with_embedded_nul_characters) |
443 | 447 | BOOST_CHECK(!LookupSubNet(std::string("5wyqrzbvrdsumnok.onion\0example.com\0", 35), ret)); |
444 | 448 | } |
445 | 449 |
|
| 450 | +// Since CNetAddr (un)ser is tested separately in net_tests.cpp here we only |
| 451 | +// try a few edge cases for port, service flags and time. |
| 452 | + |
| 453 | +static const std::vector<CAddress> fixture_addresses({ |
| 454 | + CAddress( |
| 455 | + CService(CNetAddr(in6addr_loopback), 0 /* port */), |
| 456 | + NODE_NONE, |
| 457 | + 0x4966bc61U /* Fri Jan 9 02:54:25 UTC 2009 */ |
| 458 | + ), |
| 459 | + CAddress( |
| 460 | + CService(CNetAddr(in6addr_loopback), 0x00f1 /* port */), |
| 461 | + NODE_NETWORK, |
| 462 | + 0x83766279U /* Tue Nov 22 11:22:33 UTC 2039 */ |
| 463 | + ), |
| 464 | + CAddress( |
| 465 | + CService(CNetAddr(in6addr_loopback), 0xf1f2 /* port */), |
| 466 | + static_cast<ServiceFlags>(NODE_WITNESS | NODE_COMPACT_FILTERS | NODE_NETWORK_LIMITED), |
| 467 | + 0xffffffffU /* Sun Feb 7 06:28:15 UTC 2106 */ |
| 468 | + ) |
| 469 | +}); |
| 470 | + |
| 471 | +// fixture_addresses should equal to this when serialized in V1 format. |
| 472 | +// When this is unserialized from V1 format it should equal to fixture_addresses. |
| 473 | +static constexpr const char* stream_addrv1_hex = |
| 474 | + "03" // number of entries |
| 475 | + |
| 476 | + "61bc6649" // time, Fri Jan 9 02:54:25 UTC 2009 |
| 477 | + "0000000000000000" // service flags, NODE_NONE |
| 478 | + "00000000000000000000000000000001" // address, fixed 16 bytes (IPv4 embedded in IPv6) |
| 479 | + "0000" // port |
| 480 | + |
| 481 | + "79627683" // time, Tue Nov 22 11:22:33 UTC 2039 |
| 482 | + "0100000000000000" // service flags, NODE_NETWORK |
| 483 | + "00000000000000000000000000000001" // address, fixed 16 bytes (IPv6) |
| 484 | + "00f1" // port |
| 485 | + |
| 486 | + "ffffffff" // time, Sun Feb 7 06:28:15 UTC 2106 |
| 487 | + "4804000000000000" // service flags, NODE_WITNESS | NODE_COMPACT_FILTERS | NODE_NETWORK_LIMITED |
| 488 | + "00000000000000000000000000000001" // address, fixed 16 bytes (IPv6) |
| 489 | + "f1f2"; // port |
| 490 | + |
| 491 | +// fixture_addresses should equal to this when serialized in V2 format. |
| 492 | +// When this is unserialized from V2 format it should equal to fixture_addresses. |
| 493 | +static constexpr const char* stream_addrv2_hex = |
| 494 | + "03" // number of entries |
| 495 | + |
| 496 | + "61bc6649" // time, Fri Jan 9 02:54:25 UTC 2009 |
| 497 | + "00" // service flags, COMPACTSIZE(NODE_NONE) |
| 498 | + "02" // network id, IPv6 |
| 499 | + "10" // address length, COMPACTSIZE(16) |
| 500 | + "00000000000000000000000000000001" // address |
| 501 | + "0000" // port |
| 502 | + |
| 503 | + "79627683" // time, Tue Nov 22 11:22:33 UTC 2039 |
| 504 | + "01" // service flags, COMPACTSIZE(NODE_NETWORK) |
| 505 | + "02" // network id, IPv6 |
| 506 | + "10" // address length, COMPACTSIZE(16) |
| 507 | + "00000000000000000000000000000001" // address |
| 508 | + "00f1" // port |
| 509 | + |
| 510 | + "ffffffff" // time, Sun Feb 7 06:28:15 UTC 2106 |
| 511 | + "fd4804" // service flags, COMPACTSIZE(NODE_WITNESS | NODE_COMPACT_FILTERS | NODE_NETWORK_LIMITED) |
| 512 | + "02" // network id, IPv6 |
| 513 | + "10" // address length, COMPACTSIZE(16) |
| 514 | + "00000000000000000000000000000001" // address |
| 515 | + "f1f2"; // port |
| 516 | + |
| 517 | +BOOST_AUTO_TEST_CASE(caddress_serialize_v1) |
| 518 | +{ |
| 519 | + CDataStream s(SER_NETWORK, PROTOCOL_VERSION); |
| 520 | + |
| 521 | + s << fixture_addresses; |
| 522 | + BOOST_CHECK_EQUAL(HexStr(s), stream_addrv1_hex); |
| 523 | +} |
| 524 | + |
| 525 | +BOOST_AUTO_TEST_CASE(caddress_unserialize_v1) |
| 526 | +{ |
| 527 | + CDataStream s(ParseHex(stream_addrv1_hex), SER_NETWORK, PROTOCOL_VERSION); |
| 528 | + std::vector<CAddress> addresses_unserialized; |
| 529 | + |
| 530 | + s >> addresses_unserialized; |
| 531 | + BOOST_CHECK(fixture_addresses == addresses_unserialized); |
| 532 | +} |
| 533 | + |
| 534 | +BOOST_AUTO_TEST_CASE(caddress_serialize_v2) |
| 535 | +{ |
| 536 | + CDataStream s(SER_NETWORK, PROTOCOL_VERSION | ADDRV2_FORMAT); |
| 537 | + |
| 538 | + s << fixture_addresses; |
| 539 | + BOOST_CHECK_EQUAL(HexStr(s), stream_addrv2_hex); |
| 540 | +} |
| 541 | + |
| 542 | +BOOST_AUTO_TEST_CASE(caddress_unserialize_v2) |
| 543 | +{ |
| 544 | + CDataStream s(ParseHex(stream_addrv2_hex), SER_NETWORK, PROTOCOL_VERSION | ADDRV2_FORMAT); |
| 545 | + std::vector<CAddress> addresses_unserialized; |
| 546 | + |
| 547 | + s >> addresses_unserialized; |
| 548 | + BOOST_CHECK(fixture_addresses == addresses_unserialized); |
| 549 | +} |
| 550 | + |
446 | 551 | BOOST_AUTO_TEST_SUITE_END() |
0 commit comments