5 #include <chainparams.h>
27 #include <boost/test/unit_test.hpp>
35 using namespace std::literals;
45 uint16_t altPort = 12345;
56 ipv4Addr.s_addr = 0xa0b0c001;
61 std::unique_ptr<CNode> pnode1 = std::make_unique<CNode>(
id++,
79 std::unique_ptr<CNode> pnode2 = std::make_unique<CNode>(
id++,
97 std::unique_ptr<CNode> pnode3 = std::make_unique<CNode>(
id++,
115 std::unique_ptr<CNode> pnode4 = std::make_unique<CNode>(
id++,
124 BOOST_CHECK(pnode4->IsFullOutboundConn() ==
false);
140 BOOST_REQUIRE(!addr.
IsValid());
141 BOOST_REQUIRE(addr.
IsIPv4());
148 addr =
LookupHost(
"255.255.255.255",
false).value();
149 BOOST_REQUIRE(!addr.
IsValid());
150 BOOST_REQUIRE(addr.
IsIPv4());
157 addr =
LookupHost(
"12.34.56.78",
false).value();
159 BOOST_REQUIRE(addr.
IsIPv4());
167 BOOST_REQUIRE(!addr.
IsValid());
168 BOOST_REQUIRE(addr.
IsIPv6());
175 addr =
LookupHost(
"1122:3344:5566:7788:9900:aabb:ccdd:eeff",
false).value();
177 BOOST_REQUIRE(addr.
IsIPv6());
188 const std::string link_local{
"fe80::1"};
189 const std::string scoped_addr{link_local +
"%32"};
190 addr =
LookupHost(scoped_addr,
false).value();
192 BOOST_REQUIRE(addr.
IsIPv6());
197 addr =
LookupHost(link_local +
"%0",
false).value();
199 BOOST_REQUIRE(addr.
IsIPv6());
207 const char* torv3_addr =
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion";
210 BOOST_REQUIRE(addr.
IsTor());
225 "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd\0wtf.onion", 66}));
234 const char* i2p_addr =
"UDHDrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.I2P";
237 BOOST_REQUIRE(addr.
IsI2P());
257 !addr.
SetSpecial(
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscsad.b32.i2p"));
264 BOOST_REQUIRE(!addr.
IsValid());
279 const std::map<std::string, std::string> canonical_representations_ipv6{
280 {
"0000:0000:0000:0000:0000:0000:0000:0000",
"::"},
281 {
"000:0000:000:00:0:00:000:0000",
"::"},
282 {
"000:000:000:000:000:000:000:000",
"::"},
283 {
"00:00:00:00:00:00:00:00",
"::"},
284 {
"0:0:0:0:0:0:0:0",
"::"},
285 {
"0:0:0:0:0:0:0:1",
"::1"},
286 {
"2001:0:0:1:0:0:0:1",
"2001:0:0:1::1"},
287 {
"2001:0db8:0:0:1:0:0:1",
"2001:db8::1:0:0:1"},
288 {
"2001:0db8:85a3:0000:0000:8a2e:0370:7334",
"2001:db8:85a3::8a2e:370:7334"},
289 {
"2001:0db8::0001",
"2001:db8::1"},
290 {
"2001:0db8::0001:0000",
"2001:db8::1:0"},
291 {
"2001:0db8::1:0:0:1",
"2001:db8::1:0:0:1"},
292 {
"2001:db8:0000:0:1::1",
"2001:db8::1:0:0:1"},
293 {
"2001:db8:0000:1:1:1:1:1",
"2001:db8:0:1:1:1:1:1"},
294 {
"2001:db8:0:0:0:0:2:1",
"2001:db8::2:1"},
295 {
"2001:db8:0:0:0::1",
"2001:db8::1"},
296 {
"2001:db8:0:0:1:0:0:1",
"2001:db8::1:0:0:1"},
297 {
"2001:db8:0:0:1::1",
"2001:db8::1:0:0:1"},
298 {
"2001:DB8:0:0:1::1",
"2001:db8::1:0:0:1"},
299 {
"2001:db8:0:0::1",
"2001:db8::1"},
300 {
"2001:db8:0:0:aaaa::1",
"2001:db8::aaaa:0:0:1"},
301 {
"2001:db8:0:1:1:1:1:1",
"2001:db8:0:1:1:1:1:1"},
302 {
"2001:db8:0::1",
"2001:db8::1"},
303 {
"2001:db8:85a3:0:0:8a2e:370:7334",
"2001:db8:85a3::8a2e:370:7334"},
304 {
"2001:db8::0:1",
"2001:db8::1"},
305 {
"2001:db8::0:1:0:0:1",
"2001:db8::1:0:0:1"},
306 {
"2001:DB8::1",
"2001:db8::1"},
307 {
"2001:db8::1",
"2001:db8::1"},
308 {
"2001:db8::1:0:0:1",
"2001:db8::1:0:0:1"},
309 {
"2001:db8::1:1:1:1:1",
"2001:db8:0:1:1:1:1:1"},
310 {
"2001:db8::aaaa:0:0:1",
"2001:db8::aaaa:0:0:1"},
311 {
"2001:db8:aaaa:bbbb:cccc:dddd:0:1",
"2001:db8:aaaa:bbbb:cccc:dddd:0:1"},
312 {
"2001:db8:aaaa:bbbb:cccc:dddd::1",
"2001:db8:aaaa:bbbb:cccc:dddd:0:1"},
313 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:0001",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
314 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:001",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
315 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:01",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
316 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
317 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
318 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:AAAA",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
319 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:AaAa",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
321 for (
const auto& [input_address, expected_canonical_representation_output] : canonical_representations_ipv6) {
322 const std::optional<CNetAddr> net_addr{
LookupHost(input_address,
false)};
323 BOOST_REQUIRE(net_addr.value().IsIPv6());
324 BOOST_CHECK_EQUAL(net_addr.value().ToStringAddr(), expected_canonical_representation_output);
334 s << ser_params(addr);
339 s << ser_params(addr);
343 addr =
LookupHost(
"1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b",
false).value();
344 s << ser_params(addr);
351 BOOST_REQUIRE(addr.
SetSpecial(
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"));
352 s << ser_params(addr);
357 s << ser_params(addr);
368 s << ser_params(addr);
373 s << ser_params(addr);
377 addr =
LookupHost(
"1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b",
false).value();
378 s << ser_params(addr);
385 BOOST_REQUIRE(addr.
SetSpecial(
"kpgvmscirrdqpekbqjsvw5teanhatztpp2gl6eee4zkowvwfxwenqaid.onion"));
386 s << ser_params(addr);
391 s << ser_params(addr);
406 s >> ser_params(addr);
411 BOOST_REQUIRE(s.empty());
417 BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
HasReason(
"end of data"));
418 BOOST_REQUIRE(!s.empty());
425 BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
426 HasReason(
"BIP155 IPv4 address with length 5 (should be 4)"));
427 BOOST_REQUIRE(!s.empty());
434 BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
435 HasReason(
"Address too long: 513 > 512"));
436 BOOST_REQUIRE(!s.empty());
442 "0102030405060708090a0b0c0d0e0f10")};
443 s >> ser_params(addr);
448 BOOST_REQUIRE(s.empty());
454 "fd6b88c08724ca978112ca1bbdcafac2")};
456 s >> ser_params(addr);
460 BOOST_REQUIRE(s.empty());
466 BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
467 HasReason(
"BIP155 IPv6 address with length 4 (should be 16)"));
468 BOOST_REQUIRE(!s.empty());
474 "00000000000000000000ffff01020304")};
475 s >> ser_params(addr);
477 BOOST_REQUIRE(s.empty());
482 "fd87d87eeb430102030405060708090a")};
483 s >> ser_params(addr);
485 BOOST_REQUIRE(s.empty());
490 "f1f2f3f4f5f6f7f8f9fa")};
491 s >> ser_params(addr);
493 BOOST_REQUIRE(s.empty());
498 "79bcc625184b05194975c28b66b66b04"
499 "69f7f6556fb1ac3189a79b40dda32f1f"
501 s >> ser_params(addr);
506 "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion");
507 BOOST_REQUIRE(s.empty());
514 BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
515 HasReason(
"BIP155 TORv3 address with length 0 (should be 32)"));
516 BOOST_REQUIRE(!s.empty());
522 "a2894dabaec08c0051a481a6dac88b64"
523 "f98232ae42d4b6fd2fa81952dfe36a87")};
524 s >> ser_params(addr);
529 "ukeu3k5oycgaauneqgtnvselmt4yemvoilkln7jpvamvfx7dnkdq.b32.i2p");
530 BOOST_REQUIRE(s.empty());
537 BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
538 HasReason(
"BIP155 I2P address with length 3 (should be 32)"));
539 BOOST_REQUIRE(!s.empty());
545 "fc000001000200030004000500060007"
547 s >> ser_params(addr);
552 BOOST_REQUIRE(s.empty());
557 "aa000001000200030004000500060007"
559 s >> ser_params(addr);
562 BOOST_REQUIRE(s.empty());
569 BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
570 HasReason(
"BIP155 CJDNS address with length 1 (should be 16)"));
571 BOOST_REQUIRE(!s.empty());
579 BOOST_CHECK_EXCEPTION(s >> ser_params(addr), std::ios_base::failure,
580 HasReason(
"Address too long: 33554432 > 512"));
581 BOOST_REQUIRE(!s.empty());
589 s >> ser_params(addr);
591 BOOST_REQUIRE(s.empty());
598 s >> ser_params(addr);
600 BOOST_REQUIRE(s.empty());
610 raw_addr.s_addr = htonl(0x7f000001);
617 mapLocalHost[mapLocalHost_entry] = lsi;
621 in_addr ipv4AddrPeer;
622 ipv4AddrPeer.s_addr = 0xa0b0c001;
624 std::unique_ptr<CNode> pnode = std::make_unique<CNode>(0,
633 pnode->fSuccessfullyConnected.store(
true);
636 in6_addr ipv6AddrLocal;
637 memset(ipv6AddrLocal.s6_addr, 0, 16);
638 ipv6AddrLocal.s6_addr[0] = 0xcc;
640 pnode->SetAddrLocal(addrLocal);
651 mapLocalHost.erase(mapLocalHost_entry);
667 const uint16_t bind_port = 20001;
671 in_addr peer_us_addr;
672 peer_us_addr.s_addr = htonl(0x02030405);
673 const CService peer_us{peer_us_addr, 20002};
676 in_addr peer_out_in_addr;
677 peer_out_in_addr.s_addr = htonl(0x01020304);
687 peer_out.fSuccessfullyConnected =
true;
688 peer_out.SetAddrLocal(peer_us);
692 BOOST_REQUIRE(chosen_local_addr);
693 const CService expected{peer_us_addr, bind_port};
697 in_addr peer_in_in_addr;
698 peer_in_in_addr.s_addr = htonl(0x05060708);
708 peer_in.fSuccessfullyConnected =
true;
709 peer_in.SetAddrLocal(peer_us);
713 BOOST_REQUIRE(chosen_local_addr);
774 unsigned char ip[] = {p1, p2, p3, p4};
776 struct sockaddr_in sa;
777 memset(&sa, 0,
sizeof(sockaddr_in));
778 memcpy(&(sa.sin_addr), &
ip,
sizeof(
ip));
824 const uint16_t bind_port = 20001;
829 in_addr peer_us_addr;
830 peer_us_addr.s_addr = htonl(0x02030405);
831 const CService peer_us{peer_us_addr, 20002};
834 in_addr peer_in_addr;
835 peer_in_addr.s_addr = htonl(0x01020304);
847 const int64_t time{0};
856 std::atomic<bool> interrupt_dummy{
false};
857 std::chrono::microseconds time_received_dummy{0};
859 const auto msg_version =
874 const CService expected{peer_us_addr, bind_port};
879 const std::string& msg_type,
881 bool is_incoming) ->
void {
882 if (!is_incoming && msg_type ==
"addr") {
884 std::vector<CAddress> addresses;
888 for (
const auto& addr : addresses) {
889 if (addr == expected) {
914 auto CreatePeer = [](
const CAddress& addr) {
915 return std::make_unique<CNode>(0,
928 BOOST_REQUIRE(addr_ipv4.IsValid());
929 BOOST_REQUIRE(addr_ipv4.IsIPv4());
932 BOOST_REQUIRE(addr_ipv6.IsValid());
933 BOOST_REQUIRE(addr_ipv6.IsIPv6());
936 BOOST_REQUIRE(addr_ipv6_tunnel.IsValid());
937 BOOST_REQUIRE(addr_ipv6_tunnel.IsIPv6());
938 BOOST_REQUIRE(addr_ipv6_tunnel.IsRFC3964());
941 BOOST_REQUIRE(addr_teredo.IsValid());
942 BOOST_REQUIRE(addr_teredo.IsIPv6());
943 BOOST_REQUIRE(addr_teredo.IsRFC4380());
946 BOOST_REQUIRE(addr_onion.
SetSpecial(
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"));
947 BOOST_REQUIRE(addr_onion.
IsValid());
948 BOOST_REQUIRE(addr_onion.
IsTor());
951 BOOST_REQUIRE(addr_i2p.
SetSpecial(
"udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.i2p"));
952 BOOST_REQUIRE(addr_i2p.
IsValid());
953 BOOST_REQUIRE(addr_i2p.
IsI2P());
957 BOOST_REQUIRE(addr_cjdns.IsValid());
958 BOOST_REQUIRE(addr_cjdns.IsCJDNS());
960 const auto peer_ipv4{CreatePeer(addr_ipv4)};
961 const auto peer_ipv6{CreatePeer(addr_ipv6)};
962 const auto peer_ipv6_tunnel{CreatePeer(addr_ipv6_tunnel)};
963 const auto peer_teredo{CreatePeer(addr_teredo)};
964 const auto peer_onion{CreatePeer(addr_onion)};
965 const auto peer_i2p{CreatePeer(addr_i2p)};
966 const auto peer_cjdns{CreatePeer(addr_cjdns)};
1018 CKey GenerateRandomTestKey() noexcept
1034 class V2TransportTester
1038 bool m_test_initiator;
1040 std::vector<uint8_t> m_sent_garbage;
1041 std::vector<uint8_t> m_recv_garbage;
1042 std::vector<uint8_t> m_to_send;
1043 std::vector<uint8_t> m_received;
1044 std::deque<CSerializedNetMsg> m_msg_to_send;
1045 bool m_sent_aad{
false};
1049 explicit V2TransportTester(
bool test_initiator)
1050 : m_transport{0, test_initiator},
1052 m_test_initiator(test_initiator) {}
1061 using InteractResult = std::optional<std::vector<std::optional<CNetMessage>>>;
1068 InteractResult Interact()
1070 std::vector<std::optional<CNetMessage>>
ret;
1072 bool progress{
false};
1074 if (!m_to_send.empty()) {
1076 size_t old_len = to_send.
size();
1078 return std::nullopt;
1080 if (old_len != to_send.
size()) {
1082 m_to_send.erase(m_to_send.begin(), m_to_send.begin() + (old_len - to_send.
size()));
1090 ret.emplace_back(std::nullopt);
1092 ret.emplace_back(std::move(
msg));
1099 m_msg_to_send.pop_front();
1104 const auto& [recv_bytes, _more, _msg_type] = m_transport.
GetBytesToSend(!m_msg_to_send.empty());
1107 m_received.insert(m_received.end(), recv_bytes.begin(), recv_bytes.begin() + to_receive);
1111 if (!progress)
break;
1122 m_to_send.insert(m_to_send.end(), data.
begin(), data.
end());
1131 m_to_send.insert(m_to_send.end(),
UCharCast(ser.data()),
UCharCast(ser.data() + ser.size()));
1144 m_sent_garbage.assign(garbage.
begin(), garbage.
end());
1146 Send(m_sent_garbage);
1150 void SendGarbage(
size_t garbage_len)
1163 void AddMessage(std::string m_type, std::vector<uint8_t> payload)
1166 msg.m_type = std::move(m_type);
1167 msg.data = std::move(payload);
1168 m_msg_to_send.push_back(std::move(
msg));
1191 std::vector<std::byte> ciphertext;
1203 void SendGarbageTerm()
1214 if (!m_sent_aad) aad = m_sent_garbage;
1215 SendPacket(version_data, aad, vers_ignore);
1224 std::vector<uint8_t> contents;
1234 contents.resize(size);
1255 void ReceiveGarbage()
1265 m_recv_garbage.assign(m_received.begin(), m_received.begin() + garblen);
1271 void ReceiveVersion()
1273 auto contents = ReceivePacket(
MakeByteSpan(m_recv_garbage));
1284 auto ret = ReceivePacket();
1294 auto ret = ReceivePacket();
1297 for (
unsigned i = 0; i < 12; ++i) {
1298 if (i < m_type.size()) {
1313 std::copy(mtype.begin(), mtype.end(),
reinterpret_cast<char*
>(contents.data() + 1));
1316 SendPacket(contents);
1324 std::vector<uint8_t> contents(1 + payload.
size());
1325 contents[0] = short_id;
1326 std::copy(payload.
begin(), payload.
end(), contents.begin() + 1);
1328 SendPacket(contents);
1332 void CompareSessionIDs()
const
1334 auto info = m_transport.
GetInfo();
1351 for (
int i = 0; i < 10; ++i) {
1352 V2TransportTester tester(
true);
1353 auto ret = tester.Interact();
1354 BOOST_REQUIRE(
ret &&
ret->empty());
1356 tester.SendGarbage();
1357 tester.ReceiveKey();
1358 tester.SendGarbageTerm();
1359 tester.SendVersion();
1360 ret = tester.Interact();
1361 BOOST_REQUIRE(
ret &&
ret->empty());
1362 tester.ReceiveGarbage();
1363 tester.ReceiveVersion();
1364 tester.CompareSessionIDs();
1367 tester.SendMessage(uint8_t(4), msg_data_1);
1368 tester.SendMessage(0, {});
1369 tester.SendMessage(
"tx", msg_data_2);
1370 ret = tester.Interact();
1371 BOOST_REQUIRE(
ret &&
ret->size() == 3);
1379 tester.SendMessage(
"bad", msg_data_1);
1382 ret = tester.Interact();
1387 tester.SendMessage(uint8_t(12), msg_data_3);
1392 for (
int i = 0; i < 10; ++i) {
1393 V2TransportTester tester(
false);
1395 tester.SendGarbage();
1396 auto ret = tester.Interact();
1397 BOOST_REQUIRE(
ret &&
ret->empty());
1398 tester.ReceiveKey();
1399 tester.SendGarbageTerm();
1400 tester.SendVersion();
1401 ret = tester.Interact();
1402 BOOST_REQUIRE(
ret &&
ret->empty());
1403 tester.ReceiveGarbage();
1404 tester.ReceiveVersion();
1405 tester.CompareSessionIDs();
1408 tester.SendMessage(uint8_t(14), msg_data_1);
1409 tester.SendMessage(uint8_t(19), msg_data_2);
1410 ret = tester.Interact();
1411 BOOST_REQUIRE(
ret &&
ret->size() == 2);
1417 tester.SendMessage(uint8_t(11), msg_data_3);
1418 ret = tester.Interact();
1423 for (
int i = 0; i < 50; ++i) {
1436 V2TransportTester tester(initiator);
1437 if (send_immediately) {
1439 tester.SendGarbage(garb_len);
1441 auto ret = tester.Interact();
1442 BOOST_REQUIRE(
ret &&
ret->empty());
1443 if (!send_immediately) {
1445 tester.SendGarbage(garb_len);
1447 tester.ReceiveKey();
1448 tester.SendGarbageTerm();
1449 for (
unsigned v = 0; v < num_ignore_version; ++v) {
1452 tester.SendVersion(ver_ign_data,
true);
1454 tester.SendVersion(ver_data,
false);
1455 ret = tester.Interact();
1456 BOOST_REQUIRE(
ret &&
ret->empty());
1457 tester.ReceiveGarbage();
1458 tester.ReceiveVersion();
1459 tester.CompareSessionIDs();
1460 for (
unsigned d = 0; d < num_decoys_1; ++d) {
1462 tester.SendPacket(decoy_data, {},
true);
1465 tester.SendMessage(uint8_t(28), msg_data_1);
1466 for (
unsigned d = 0; d < num_decoys_2; ++d) {
1468 tester.SendPacket(decoy_data, {},
true);
1471 tester.SendMessage(uint8_t(13), msg_data_2);
1474 tester.SendMessage(
"foobar", {});
1475 tester.AddMessage(
"barfoo", {});
1476 ret = tester.Interact();
1477 BOOST_REQUIRE(
ret &&
ret->size() == 4);
1482 tester.ReceiveMessage(
"barfoo", {});
1487 V2TransportTester tester(
true);
1488 auto ret = tester.Interact();
1489 BOOST_REQUIRE(
ret &&
ret->empty());
1492 tester.ReceiveKey();
1493 tester.SendGarbageTerm();
1494 ret = tester.Interact();
1500 V2TransportTester tester(
false);
1503 auto ret = tester.Interact();
1504 BOOST_REQUIRE(
ret &&
ret->empty());
1505 tester.ReceiveKey();
1506 tester.SendGarbageTerm();
1507 ret = tester.Interact();
1513 V2TransportTester tester(
true);
1514 auto ret = tester.Interact();
1515 BOOST_REQUIRE(
ret &&
ret->empty());
1517 tester.ReceiveKey();
1525 auto garb_term =
MakeUCharSpan(tester.GetCipher().GetSendGarbageTerminator());
1526 std::copy(garb_term.begin(), garb_term.begin() + 16, garbage.
begin() + len_before);
1530 tester.SendGarbage(garbage);
1531 tester.SendGarbageTerm();
1532 tester.SendVersion();
1533 ret = tester.Interact();
1534 BOOST_REQUIRE(
ret &&
ret->empty());
1535 tester.ReceiveGarbage();
1536 tester.ReceiveVersion();
1537 tester.CompareSessionIDs();
1541 tester.SendMessage(uint8_t(2), msg_data_1);
1542 tester.AddMessage(
"blocktxn", msg_data_2);
1543 ret = tester.Interact();
1544 BOOST_REQUIRE(
ret &&
ret->size() == 2);
1547 tester.ReceiveMessage(uint8_t(3), msg_data_2);
1552 V2TransportTester tester(
false);
1553 tester.SendV1Version(
Params().MessageStart());
1554 auto ret = tester.Interact();
1560 V2TransportTester tester(
false);
1562 auto ret = tester.Interact();
const CChainParams & Params()
Return the currently selected parameters.
void ForceSetArg(const std::string &strArg, const std::string &strValue)
bool SoftSetArg(const std::string &strArg, const std::string &strValue)
Set an argument if it doesn't already have a value.
The BIP324 packet cipher, encapsulating its key derivation, stream cipher, and AEAD.
Span< const std::byte > GetSendGarbageTerminator() const noexcept
Get the Garbage Terminator to send.
Span< const std::byte > GetSessionID() const noexcept
Get the Session ID.
static constexpr unsigned GARBAGE_TERMINATOR_LEN
unsigned DecryptLength(Span< const std::byte > input) noexcept
Decrypt the length of a packet.
const EllSwiftPubKey & GetOurPubKey() const noexcept
Retrieve our public key.
bool Decrypt(Span< const std::byte > input, Span< const std::byte > aad, bool &ignore, Span< std::byte > contents) noexcept
Decrypt a packet.
void Encrypt(Span< const std::byte > contents, Span< const std::byte > aad, bool ignore, Span< std::byte > output) noexcept
Encrypt a packet.
static constexpr unsigned LENGTH_LEN
static constexpr unsigned EXPANSION
void Initialize(const EllSwiftPubKey &their_pubkey, bool initiator, bool self_decrypt=false) noexcept
Initialize when the other side's public key is received.
Span< const std::byte > GetReceiveGarbageTerminator() const noexcept
Get the expected Garbage Terminator to receive.
A CService with information about it as peer.
static constexpr SerParams V1_NETWORK
static constexpr SerParams V2_NETWORK
static std::unique_ptr< const CChainParams > Main()
An encapsulated private key.
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
std::string ToStringAddr() const
bool SetSpecial(const std::string &addr)
Parse a Tor or I2P address and set this object to it.
bool SetInternal(const std::string &name)
Create an "internal" address that represents a name or FQDN.
bool IsAddrV1Compatible() const
Check if the current object can be serialized in pre-ADDRv2/BIP155 format.
Information about a peer.
A combination of a network address (CNetAddr) and a (TCP) port.
Double ended buffer combining vector and stream-like interfaces.
std::vector< B > randbytes(size_t len)
Generate random bytes.
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
static Mutex g_msgproc_mutex
Mutex for anything that is only accessed via the msg processing thread.
void Add(Network net) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
bool Contains(Network net) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
void Remove(Network net) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
void RemoveAll() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
A Span is an object that can refer to a contiguous sequence of objects.
constexpr std::size_t size() const noexcept
constexpr C * end() const noexcept
constexpr C * begin() const noexcept
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset) const noexcept
void MarkBytesSent(size_t bytes_sent) noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex)
Report how many bytes returned by the last GetBytesToSend() have been sent.
static constexpr uint32_t MAX_GARBAGE_LEN
bool ReceivedBytes(Span< const uint8_t > &msg_bytes) noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex
Feed wire bytes to the transport.
Info GetInfo() const noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex)
Retrieve information about this transport.
BytesToSend GetBytesToSend(bool have_next_message) const noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex)
Get bytes to send on the wire, if any, along with other information about it.
bool ReceivedMessageComplete() const noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex)
Returns true if the current message is complete (so GetReceivedMessage can be called).
CNetMessage GetReceivedMessage(std::chrono::microseconds time, bool &reject_message) noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex)
Retrieve a completed message from transport.
bool SetMessageToSend(CSerializedNetMsg &msg) noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_send_mutex)
Set the next message to send.
constexpr unsigned char * end()
constexpr unsigned char * begin()
@ OUTBOUND_FULL_RELAY
These are the default connections that we use to connect with the network.
@ INBOUND
Inbound connections are those initiated by a peer.
BOOST_AUTO_TEST_SUITE_END()
static CService ip(uint32_t i)
std::array< uint8_t, 4 > MessageStartChars
CSerializedNetMsg Make(std::string msg_type, Args &&... args)
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
bool IsLocal(const CService &addr)
check whether a given address is potentially local
void RemoveLocal(const CService &addr)
std::optional< CService > GetLocalAddrForPeer(CNode &node)
Returns a local address that we should advertise to this peer.
bool AddLocal(const CService &addr_, int nScore)
CService GetLocalAddress(const CNode &peer)
GlobalMutex g_maplocalhost_mutex
std::function< void(const CAddress &addr, const std::string &msg_type, Span< const unsigned char > data, bool is_incoming)> CaptureMessage
Defaults to CaptureMessageToFile(), but can be overridden by unit tests.
BOOST_AUTO_TEST_CASE(cnode_listen_port)
CNetAddr UtilBuildAddress(unsigned char p1, unsigned char p2, unsigned char p3, unsigned char p4)
@ NET_ONION
TOR (v2 or v3)
@ NET_UNROUTABLE
Addresses from these networks are not publicly routable on the global Internet.
@ NET_INTERNAL
A set of addresses that represent the hash of a string or FQDN.
std::vector< CService > Lookup(const std::string &name, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions, DNSLookupFn dns_lookup_function)
Resolve a service string to its corresponding service.
CService MaybeFlipIPv6toCJDNS(const CService &service)
If an IPv6 address belongs to the address range used by the CJDNS network and the CJDNS network is re...
ReachableNets g_reachable_nets
std::vector< CNetAddr > LookupHost(const std::string &name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
Resolve a host string to its corresponding network addresses.
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
static const int PROTOCOL_VERSION
network protocol versioning
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span{std::forward< V >(v)}))
Like the Span constructor, but for (const) unsigned char member types only.
unsigned char * UCharCast(char *c)
Span< const std::byte > MakeByteSpan(V &&v) noexcept
Span< std::byte > MakeWritableByteSpan(V &&v) noexcept
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
std::string ToString(const T &t)
Locale-independent version of std::to_string.
static constexpr size_t size()
Identical to TestingSetup, but chain set to regtest.
void JumpOutOfIbd()
Toggle IsInitialBlockDownload from true to false.
std::unique_ptr< ChainstateManager > chainman
std::unique_ptr< PeerManager > peerman
FastRandomContext g_insecure_rand_ctx
This global and the helpers that use it are not thread-safe.
static uint64_t InsecureRandRange(uint64_t range)
static uint256 InsecureRand256()
static bool InsecureRandBool()
void TestOnlyResetTimeData()
Reset the internal state of GetTimeOffset() and AddTimeData().
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.