7#include <chainparams.h>
29#include <boost/test/unit_test.hpp>
38using namespace std::literals;
50 uint16_t altPort = 12345;
61 ipv4Addr.s_addr = 0xa0b0c001;
66 std::unique_ptr<CNode> pnode1 = std::make_unique<CNode>(
id++,
85 std::unique_ptr<CNode> pnode2 = std::make_unique<CNode>(
id++,
104 std::unique_ptr<CNode> pnode3 = std::make_unique<CNode>(
id++,
123 std::unique_ptr<CNode> pnode4 = std::make_unique<CNode>(
id++,
133 BOOST_CHECK(pnode4->IsFullOutboundConn() ==
false);
149 BOOST_REQUIRE(!addr.
IsValid());
150 BOOST_REQUIRE(addr.
IsIPv4());
157 addr =
LookupHost(
"255.255.255.255",
false).value();
158 BOOST_REQUIRE(!addr.
IsValid());
159 BOOST_REQUIRE(addr.
IsIPv4());
166 addr =
LookupHost(
"12.34.56.78",
false).value();
168 BOOST_REQUIRE(addr.
IsIPv4());
176 BOOST_REQUIRE(!addr.
IsValid());
177 BOOST_REQUIRE(addr.
IsIPv6());
184 addr =
LookupHost(
"1122:3344:5566:7788:9900:aabb:ccdd:eeff",
false).value();
186 BOOST_REQUIRE(addr.
IsIPv6());
197 const std::string link_local{
"fe80::1"};
198 const std::string scoped_addr{link_local +
"%32"};
199 addr =
LookupHost(scoped_addr,
false).value();
201 BOOST_REQUIRE(addr.
IsIPv6());
209 const char* torv3_addr =
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion";
212 BOOST_REQUIRE(addr.
IsTor());
227 "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd\0wtf.onion", 66}));
236 const char* i2p_addr =
"UDHDrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.I2P";
239 BOOST_REQUIRE(addr.
IsI2P());
259 !addr.
SetSpecial(
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscsad.b32.i2p"));
266 BOOST_REQUIRE(!addr.
IsValid());
281 const std::map<std::string, std::string> canonical_representations_ipv6{
282 {
"0000:0000:0000:0000:0000:0000:0000:0000",
"::"},
283 {
"000:0000:000:00:0:00:000:0000",
"::"},
284 {
"000:000:000:000:000:000:000:000",
"::"},
285 {
"00:00:00:00:00:00:00:00",
"::"},
286 {
"0:0:0:0:0:0:0:0",
"::"},
287 {
"0:0:0:0:0:0:0:1",
"::1"},
288 {
"2001:0:0:1:0:0:0:1",
"2001:0:0:1::1"},
289 {
"2001:0db8:0:0:1:0:0:1",
"2001:db8::1:0:0:1"},
290 {
"2001:0db8:85a3:0000:0000:8a2e:0370:7334",
"2001:db8:85a3::8a2e:370:7334"},
291 {
"2001:0db8::0001",
"2001:db8::1"},
292 {
"2001:0db8::0001:0000",
"2001:db8::1:0"},
293 {
"2001:0db8::1:0:0:1",
"2001:db8::1:0:0:1"},
294 {
"2001:db8:0000:0:1::1",
"2001:db8::1:0:0:1"},
295 {
"2001:db8:0000:1:1:1:1:1",
"2001:db8:0:1:1:1:1:1"},
296 {
"2001:db8:0:0:0:0:2:1",
"2001:db8::2:1"},
297 {
"2001:db8:0:0:0::1",
"2001:db8::1"},
298 {
"2001:db8:0:0:1:0:0:1",
"2001:db8::1:0:0:1"},
299 {
"2001:db8:0:0:1::1",
"2001:db8::1:0:0:1"},
300 {
"2001:DB8:0:0:1::1",
"2001:db8::1:0:0:1"},
301 {
"2001:db8:0:0::1",
"2001:db8::1"},
302 {
"2001:db8:0:0:aaaa::1",
"2001:db8::aaaa:0:0:1"},
303 {
"2001:db8:0:1:1:1:1:1",
"2001:db8:0:1:1:1:1:1"},
304 {
"2001:db8:0::1",
"2001:db8::1"},
305 {
"2001:db8:85a3:0:0:8a2e:370:7334",
"2001:db8:85a3::8a2e:370:7334"},
306 {
"2001:db8::0:1",
"2001:db8::1"},
307 {
"2001:db8::0:1:0:0:1",
"2001:db8::1:0:0:1"},
308 {
"2001:DB8::1",
"2001:db8::1"},
309 {
"2001:db8::1",
"2001:db8::1"},
310 {
"2001:db8::1:0:0:1",
"2001:db8::1:0:0:1"},
311 {
"2001:db8::1:1:1:1:1",
"2001:db8:0:1:1:1:1:1"},
312 {
"2001:db8::aaaa:0:0:1",
"2001:db8::aaaa:0:0:1"},
313 {
"2001:db8:aaaa:bbbb:cccc:dddd:0:1",
"2001:db8:aaaa:bbbb:cccc:dddd:0:1"},
314 {
"2001:db8:aaaa:bbbb:cccc:dddd::1",
"2001:db8:aaaa:bbbb:cccc:dddd:0:1"},
315 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:0001",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
316 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:001",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
317 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:01",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
318 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
319 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
320 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:AAAA",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
321 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:AaAa",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
323 for (
const auto& [input_address, expected_canonical_representation_output] : canonical_representations_ipv6) {
324 const std::optional<CNetAddr> net_addr{
LookupHost(input_address,
false)};
325 BOOST_REQUIRE(net_addr.value().IsIPv6());
326 BOOST_CHECK_EQUAL(net_addr.value().ToStringAddr(), expected_canonical_representation_output);
336 s << ser_params(addr);
341 s << ser_params(addr);
345 addr =
LookupHost(
"1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b",
false).value();
346 s << ser_params(addr);
353 BOOST_REQUIRE(addr.
SetSpecial(
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"));
354 s << ser_params(addr);
359 s << ser_params(addr);
370 s << ser_params(addr);
375 s << ser_params(addr);
379 addr =
LookupHost(
"1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b",
false).value();
380 s << ser_params(addr);
387 BOOST_REQUIRE(addr.
SetSpecial(
"kpgvmscirrdqpekbqjsvw5teanhatztpp2gl6eee4zkowvwfxwenqaid.onion"));
388 s << ser_params(addr);
393 s << ser_params(addr);
408 s >> ser_params(addr);
413 BOOST_REQUIRE(
s.empty());
420 BOOST_REQUIRE(!
s.empty());
428 HasReason(
"BIP155 IPv4 address with length 5 (should be 4)"));
429 BOOST_REQUIRE(!
s.empty());
437 HasReason(
"Address too long: 513 > 512"));
438 BOOST_REQUIRE(!
s.empty());
444 "0102030405060708090a0b0c0d0e0f10"_hex;
445 s >> ser_params(addr);
450 BOOST_REQUIRE(
s.empty());
455 "fd6b88c08724ca978112ca1bbdcafac2"_hex;
457 s >> ser_params(addr);
461 BOOST_REQUIRE(
s.empty());
468 HasReason(
"BIP155 IPv6 address with length 4 (should be 16)"));
469 BOOST_REQUIRE(!
s.empty());
475 "00000000000000000000ffff01020304"_hex;
476 s >> ser_params(addr);
478 BOOST_REQUIRE(
s.empty());
483 "fd87d87eeb430102030405060708090a"_hex;
484 s >> ser_params(addr);
486 BOOST_REQUIRE(
s.empty());
491 "f1f2f3f4f5f6f7f8f9fa"_hex;
492 s >> ser_params(addr);
494 BOOST_REQUIRE(
s.empty());
499 "79bcc625184b05194975c28b66b66b04"
500 "69f7f6556fb1ac3189a79b40dda32f1f"_hex;
501 s >> ser_params(addr);
506 "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion");
507 BOOST_REQUIRE(
s.empty());
514 HasReason(
"BIP155 TORv3 address with length 0 (should be 32)"));
515 BOOST_REQUIRE(!
s.empty());
521 "a2894dabaec08c0051a481a6dac88b64"
522 "f98232ae42d4b6fd2fa81952dfe36a87"_hex;
523 s >> ser_params(addr);
528 "ukeu3k5oycgaauneqgtnvselmt4yemvoilkln7jpvamvfx7dnkdq.b32.i2p");
529 BOOST_REQUIRE(
s.empty());
536 HasReason(
"BIP155 I2P address with length 3 (should be 32)"));
537 BOOST_REQUIRE(!
s.empty());
543 "fc000001000200030004000500060007"_hex;
544 s >> ser_params(addr);
549 BOOST_REQUIRE(
s.empty());
554 "aa000001000200030004000500060007"_hex;
555 s >> ser_params(addr);
558 BOOST_REQUIRE(
s.empty());
565 HasReason(
"BIP155 CJDNS address with length 1 (should be 16)"));
566 BOOST_REQUIRE(!
s.empty());
572 "01020304050607"_hex;
574 HasReason(
"Address too long: 33554432 > 512"));
575 BOOST_REQUIRE(!
s.empty());
582 s >> ser_params(addr);
584 BOOST_REQUIRE(
s.empty());
590 s >> ser_params(addr);
592 BOOST_REQUIRE(
s.empty());
602 raw_addr.s_addr = htonl(0x7f000001);
609 mapLocalHost[mapLocalHost_entry] = lsi;
613 in_addr ipv4AddrPeer;
614 ipv4AddrPeer.s_addr = 0xa0b0c001;
616 std::unique_ptr<CNode> pnode = std::make_unique<CNode>(0,
626 pnode->fSuccessfullyConnected.store(
true);
629 in6_addr ipv6AddrLocal;
630 memset(ipv6AddrLocal.s6_addr, 0, 16);
631 ipv6AddrLocal.s6_addr[0] = 0xcc;
633 pnode->SetAddrLocal(addrLocal);
644 mapLocalHost.erase(mapLocalHost_entry);
660 const uint16_t bind_port = 20001;
664 in_addr peer_us_addr;
665 peer_us_addr.s_addr = htonl(0x02030405);
666 const CService peer_us{peer_us_addr, 20002};
669 in_addr peer_out_in_addr;
670 peer_out_in_addr.s_addr = htonl(0x01020304);
681 peer_out.fSuccessfullyConnected =
true;
682 peer_out.SetAddrLocal(peer_us);
686 BOOST_REQUIRE(chosen_local_addr);
687 const CService expected{peer_us_addr, bind_port};
691 in_addr peer_in_in_addr;
692 peer_in_in_addr.s_addr = htonl(0x05060708);
703 peer_in.fSuccessfullyConnected =
true;
704 peer_in.SetAddrLocal(peer_us);
708 BOOST_REQUIRE(chosen_local_addr);
769 unsigned char ip[] = {p1, p2, p3, p4};
771 struct sockaddr_in sa;
772 memset(&sa, 0,
sizeof(sockaddr_in));
773 memcpy(&(sa.sin_addr), &
ip,
sizeof(
ip));
811 const auto get_score = [](
const CService& service) ->
int {
813 const auto it = mapLocalHost.find(service);
814 return it != mapLocalHost.end() ? it->second.nScore : 0;
817 const int initial_score = 1000;
818 BOOST_REQUIRE(
AddLocal(addr, initial_score));
827 BOOST_REQUIRE(
AddLocal(addr, std::numeric_limits<int>::max()));
855 const uint16_t bind_port = 20001;
860 in_addr peer_us_addr;
861 peer_us_addr.s_addr = htonl(0x02030405);
862 const CService peer_us{peer_us_addr, 20002};
865 in_addr peer_in_addr;
866 peer_in_addr.s_addr = htonl(0x01020304);
879 const int64_t time{0};
889 connman.FlushSendBuffer(peer);
891 auto msg_version_receive =
893 Assert(connman.ReceiveMsgFrom(peer, std::move(msg_version_receive)));
894 peer.fPauseSend =
false;
895 bool more_work{connman.ProcessMessagesOnce(peer)};
899 connman.FlushSendBuffer(peer);
902 peer.fPauseSend =
false;
904 more_work = connman.ProcessMessagesOnce(peer);
908 const CService expected{peer_us_addr, bind_port};
913 const std::string& msg_type,
914 std::span<const unsigned char>
data,
915 bool is_incoming) ->
void {
916 if (!is_incoming && msg_type ==
"addr") {
917 std::vector<CAddress> addresses;
921 for (
const auto& addr : addresses) {
922 if (addr == expected) {
943 auto CreatePeer = [](
const CAddress& addr) {
944 return std::make_unique<CNode>(0,
958 BOOST_REQUIRE(addr_ipv4.IsValid());
959 BOOST_REQUIRE(addr_ipv4.IsIPv4());
962 BOOST_REQUIRE(addr_ipv6.IsValid());
963 BOOST_REQUIRE(addr_ipv6.IsIPv6());
966 BOOST_REQUIRE(addr_ipv6_tunnel.IsValid());
967 BOOST_REQUIRE(addr_ipv6_tunnel.IsIPv6());
968 BOOST_REQUIRE(addr_ipv6_tunnel.IsRFC3964());
971 BOOST_REQUIRE(addr_teredo.IsValid());
972 BOOST_REQUIRE(addr_teredo.IsIPv6());
973 BOOST_REQUIRE(addr_teredo.IsRFC4380());
976 BOOST_REQUIRE(addr_onion.
SetSpecial(
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion"));
977 BOOST_REQUIRE(addr_onion.
IsValid());
978 BOOST_REQUIRE(addr_onion.
IsTor());
981 BOOST_REQUIRE(addr_i2p.
SetSpecial(
"udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.i2p"));
982 BOOST_REQUIRE(addr_i2p.
IsValid());
983 BOOST_REQUIRE(addr_i2p.
IsI2P());
987 BOOST_REQUIRE(addr_cjdns.IsValid());
988 BOOST_REQUIRE(addr_cjdns.IsCJDNS());
990 const auto peer_ipv4{CreatePeer(addr_ipv4)};
991 const auto peer_ipv6{CreatePeer(addr_ipv6)};
992 const auto peer_ipv6_tunnel{CreatePeer(addr_ipv6_tunnel)};
993 const auto peer_teredo{CreatePeer(addr_teredo)};
994 const auto peer_onion{CreatePeer(addr_onion)};
995 const auto peer_i2p{CreatePeer(addr_i2p)};
996 const auto peer_cjdns{CreatePeer(addr_cjdns)};
1064class V2TransportTester
1069 bool m_test_initiator;
1071 std::vector<uint8_t> m_sent_garbage;
1072 std::vector<uint8_t> m_recv_garbage;
1073 std::vector<uint8_t> m_to_send;
1074 std::vector<uint8_t> m_received;
1075 std::deque<CSerializedNetMsg> m_msg_to_send;
1076 bool m_sent_aad{
false};
1082 m_transport{0, test_initiator},
1083 m_cipher{GenerateRandomTestKey(m_rng),
MakeByteSpan(m_rng.rand256())},
1084 m_test_initiator(test_initiator) {}
1093 using InteractResult = std::optional<std::vector<std::optional<CNetMessage>>>;
1100 InteractResult Interact()
1102 std::vector<std::optional<CNetMessage>>
ret;
1104 bool progress{
false};
1106 if (!m_to_send.empty()) {
1107 std::span<const uint8_t> to_send = std::span{m_to_send}.first(1 + m_rng.
randrange(m_to_send.size()));
1108 size_t old_len = to_send.size();
1110 return std::nullopt;
1112 if (old_len != to_send.size()) {
1114 m_to_send.erase(m_to_send.begin(), m_to_send.begin() + (old_len - to_send.size()));
1122 ret.emplace_back(std::nullopt);
1124 ret.emplace_back(std::move(
msg));
1129 if (!m_msg_to_send.empty() && (!progress || m_rng.
randbool())) {
1131 m_msg_to_send.pop_front();
1136 const auto& [recv_bytes, _more, _msg_type] = m_transport.
GetBytesToSend(!m_msg_to_send.empty());
1137 if (!recv_bytes.empty() && (!progress || m_rng.
randbool())) {
1138 size_t to_receive = 1 + m_rng.
randrange(recv_bytes.size());
1139 m_received.insert(m_received.end(), recv_bytes.begin(), recv_bytes.begin() + to_receive);
1143 if (!progress)
break;
1152 void Send(std::span<const uint8_t>
data)
1154 m_to_send.insert(m_to_send.end(),
data.begin(),
data.end());
1163 m_to_send.insert(m_to_send.end(),
UCharCast(ser.data()),
UCharCast(ser.data() + ser.size()));
1173 void SendGarbage(std::span<const uint8_t> garbage)
1176 m_sent_garbage.assign(garbage.begin(), garbage.end());
1178 Send(m_sent_garbage);
1182 void SendGarbage(
size_t garbage_len)
1185 SendGarbage(m_rng.
randbytes<uint8_t>(garbage_len));
1195 void AddMessage(std::string m_type, std::vector<uint8_t> payload)
1198 msg.m_type = std::move(m_type);
1199 msg.data = std::move(payload);
1200 m_msg_to_send.push_back(std::move(
msg));
1220 void SendPacket(std::span<const uint8_t> content, std::span<const uint8_t> aad = {},
bool ignore =
false)
1223 std::vector<std::byte> ciphertext;
1235 void SendGarbageTerm()
1242 void SendVersion(std::span<const uint8_t> version_data = {},
bool vers_ignore =
false)
1244 std::span<const std::uint8_t> aad;
1246 if (!m_sent_aad) aad = m_sent_garbage;
1247 SendPacket(version_data, aad, vers_ignore);
1254 std::vector<uint8_t> ReceivePacket(std::span<const std::byte> aad = {})
1256 std::vector<uint8_t> contents;
1266 contents.resize(size);
1287 void ReceiveGarbage()
1297 m_recv_garbage.assign(m_received.begin(), m_received.begin() + garblen);
1303 void ReceiveVersion()
1305 auto contents = ReceivePacket(
MakeByteSpan(m_recv_garbage));
1314 void ReceiveMessage(uint8_t short_id, std::span<const uint8_t> payload)
1316 auto ret = ReceivePacket();
1319 BOOST_CHECK(std::ranges::equal(std::span{
ret}.subspan(1), payload));
1324 void ReceiveMessage(
const std::string& m_type, std::span<const uint8_t> payload)
1326 auto ret = ReceivePacket();
1329 for (
unsigned i = 0; i < 12; ++i) {
1330 if (i < m_type.size()) {
1341 void SendMessage(std::string mtype, std::span<const uint8_t> payload)
1345 std::copy(mtype.begin(), mtype.end(), contents.begin() + 1);
1348 SendPacket(contents);
1353 void SendMessage(uint8_t short_id, std::span<const uint8_t> payload)
1356 std::vector<uint8_t> contents(1 + payload.size());
1357 contents[0] = short_id;
1358 std::copy(payload.begin(), payload.end(), contents.begin() + 1);
1360 SendPacket(contents);
1364 void CompareSessionIDs()
const
1366 auto info = m_transport.
GetInfo();
1383 for (
int i = 0; i < 10; ++i) {
1384 V2TransportTester tester(m_rng,
true);
1385 auto ret = tester.Interact();
1386 BOOST_REQUIRE(
ret &&
ret->empty());
1388 tester.SendGarbage();
1389 tester.ReceiveKey();
1390 tester.SendGarbageTerm();
1391 tester.SendVersion();
1392 ret = tester.Interact();
1393 BOOST_REQUIRE(
ret &&
ret->empty());
1394 tester.ReceiveGarbage();
1395 tester.ReceiveVersion();
1396 tester.CompareSessionIDs();
1399 tester.SendMessage(uint8_t(4), msg_data_1);
1400 tester.SendMessage(0, {});
1401 tester.SendMessage(
"tx", msg_data_2);
1402 ret = tester.Interact();
1403 BOOST_REQUIRE(
ret &&
ret->size() == 3);
1411 tester.SendMessage(
"bad", msg_data_1);
1414 ret = tester.Interact();
1419 tester.SendMessage(uint8_t(12), msg_data_3);
1424 for (
int i = 0; i < 10; ++i) {
1425 V2TransportTester tester(m_rng,
false);
1427 tester.SendGarbage();
1428 auto ret = tester.Interact();
1429 BOOST_REQUIRE(
ret &&
ret->empty());
1430 tester.ReceiveKey();
1431 tester.SendGarbageTerm();
1432 tester.SendVersion();
1433 ret = tester.Interact();
1434 BOOST_REQUIRE(
ret &&
ret->empty());
1435 tester.ReceiveGarbage();
1436 tester.ReceiveVersion();
1437 tester.CompareSessionIDs();
1440 tester.SendMessage(uint8_t(14), msg_data_1);
1441 tester.SendMessage(uint8_t(19), msg_data_2);
1442 ret = tester.Interact();
1443 BOOST_REQUIRE(
ret &&
ret->size() == 2);
1448 auto msg_data_3 = m_rng.
randbytes<uint8_t>(4005000);
1449 tester.SendMessage(uint8_t(11), msg_data_3);
1450 ret = tester.Interact();
1455 for (
int i = 0; i < 50; ++i) {
1461 unsigned num_ignore_version = m_rng.
randrange(10);
1465 bool send_immediately = !initiator || m_rng.
randbool();
1468 V2TransportTester tester(m_rng, initiator);
1469 if (send_immediately) {
1471 tester.SendGarbage(garb_len);
1473 auto ret = tester.Interact();
1474 BOOST_REQUIRE(
ret &&
ret->empty());
1475 if (!send_immediately) {
1477 tester.SendGarbage(garb_len);
1479 tester.ReceiveKey();
1480 tester.SendGarbageTerm();
1481 for (
unsigned v = 0; v < num_ignore_version; ++v) {
1483 auto ver_ign_data = m_rng.
randbytes<uint8_t>(ver_ign_data_len);
1484 tester.SendVersion(ver_ign_data,
true);
1486 tester.SendVersion(ver_data,
false);
1487 ret = tester.Interact();
1488 BOOST_REQUIRE(
ret &&
ret->empty());
1489 tester.ReceiveGarbage();
1490 tester.ReceiveVersion();
1491 tester.CompareSessionIDs();
1492 for (
unsigned d = 0; d < num_decoys_1; ++d) {
1494 tester.SendPacket(decoy_data, {},
true);
1497 tester.SendMessage(uint8_t(28), msg_data_1);
1498 for (
unsigned d = 0; d < num_decoys_2; ++d) {
1500 tester.SendPacket(decoy_data, {},
true);
1503 tester.SendMessage(uint8_t(13), msg_data_2);
1506 tester.SendMessage(
"foobar", {});
1507 tester.AddMessage(
"barfoo", {});
1508 ret = tester.Interact();
1509 BOOST_REQUIRE(
ret &&
ret->size() == 4);
1514 tester.ReceiveMessage(
"barfoo", {});
1519 V2TransportTester tester(m_rng,
true);
1520 auto ret = tester.Interact();
1521 BOOST_REQUIRE(
ret &&
ret->empty());
1524 tester.ReceiveKey();
1525 tester.SendGarbageTerm();
1526 ret = tester.Interact();
1532 V2TransportTester tester(m_rng,
false);
1535 auto ret = tester.Interact();
1536 BOOST_REQUIRE(
ret &&
ret->empty());
1537 tester.ReceiveKey();
1538 tester.SendGarbageTerm();
1539 ret = tester.Interact();
1545 V2TransportTester tester(m_rng,
true);
1546 auto ret = tester.Interact();
1547 BOOST_REQUIRE(
ret &&
ret->empty());
1549 tester.ReceiveKey();
1555 auto garbage = m_rng.
randbytes<uint8_t>(len_before + 16 + len_after);
1557 auto garb_term =
MakeUCharSpan(tester.GetCipher().GetSendGarbageTerminator());
1558 std::copy(garb_term.begin(), garb_term.begin() + 16, garbage.begin() + len_before);
1561 garbage[len_before + 15] ^= (uint8_t(1) << m_rng.
randrange(8));
1562 tester.SendGarbage(garbage);
1563 tester.SendGarbageTerm();
1564 tester.SendVersion();
1565 ret = tester.Interact();
1566 BOOST_REQUIRE(
ret &&
ret->empty());
1567 tester.ReceiveGarbage();
1568 tester.ReceiveVersion();
1569 tester.CompareSessionIDs();
1570 auto msg_data_1 = m_rng.
randbytes<uint8_t>(4000000);
1571 auto msg_data_2 = m_rng.
randbytes<uint8_t>(4000000);
1573 tester.SendMessage(uint8_t(2), msg_data_1);
1574 tester.AddMessage(
"blocktxn", msg_data_2);
1575 ret = tester.Interact();
1576 BOOST_REQUIRE(
ret &&
ret->size() == 2);
1579 tester.ReceiveMessage(uint8_t(3), msg_data_2);
1584 V2TransportTester tester(m_rng,
false);
1585 tester.SendV1Version(
Params().MessageStart());
1586 auto ret = tester.Interact();
1592 V2TransportTester tester(m_rng,
false);
1594 auto ret = tester.Interact();
1618 connman.Handshake(
node,
static constexpr unsigned BIP324_SHORTIDS_IMPLEMENTED
const CChainParams & Params()
Return the currently selected parameters.
#define Assert(val)
Identity function.
bool SoftSetArg(const std::string &strArg, const std::string &strValue) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Set an argument if it doesn't already have a value.
void ForceSetArg(const std::string &strArg, const std::string &strValue) EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
The BIP324 packet cipher, encapsulating its key derivation, stream cipher, and AEAD.
bool Decrypt(std::span< const std::byte > input, std::span< const std::byte > aad, bool &ignore, std::span< std::byte > contents) noexcept
Decrypt a packet.
std::span< const std::byte > GetSendGarbageTerminator() const noexcept
Get the Garbage Terminator to send.
static constexpr unsigned GARBAGE_TERMINATOR_LEN
unsigned DecryptLength(std::span< const std::byte > input) noexcept
Decrypt the length of a packet.
std::span< const std::byte > GetSessionID() const noexcept
Get the Session ID.
const EllSwiftPubKey & GetOurPubKey() const noexcept
Retrieve our public key.
std::span< const std::byte > GetReceiveGarbageTerminator() const noexcept
Get the expected Garbage Terminator to receive.
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.
void Encrypt(std::span< const std::byte > contents, std::span< const std::byte > aad, bool ignore, std::span< std::byte > output) noexcept
Encrypt a packet.
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(std::string_view 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.
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.
I randrange(I range) noexcept
Generate a random integer in the range [0..range), with range > 0.
uint256 rand256() noexcept
generate a random uint256.
bool randbool() noexcept
Generate a random boolean.
std::vector< B > randbytes(size_t len) noexcept
Generate random bytes.
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)
Minimal stream for reading from an existing byte array by std::span.
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
CNetMessage GetReceivedMessage(NodeClock::time_point time, bool &reject_message) noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex)
Retrieve a completed message from 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).
bool ReceivedBytes(std::span< const uint8_t > &msg_bytes) noexcept override EXCLUSIVE_LOCKS_REQUIRED(!m_recv_mutex
Feed wire bytes to the 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()
@ PRIVATE_BROADCAST
Private broadcast connections are short-lived and only opened to privacy networks (Tor,...
@ 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_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
static CService ip(uint32_t i)
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
BOOST_CHECK_EQUAL(headers.FindFirst("key"), "value")
BOOST_CHECK_EXCEPTION(HTTPHeaders{}.Read(reader), std::runtime_error, HasReason{"Empty HTTP header name"})
std::array< uint8_t, 4 > MessageStartChars
CSerializedNetMsg Make(std::string msg_type, Args &&... args)
constexpr const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
constexpr const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
""_hex is a compile-time user-defined literal returning a std::array<std::byte>, equivalent to ParseH...
std::string ToString(const T &t)
Locale-independent version of std::to_string.
bool IsLocal(const CService &addr)
check whether a given address is potentially local
void RemoveLocal(const CService &addr)
bool AddLocal(const CService &addr_, int nScore)
std::optional< CService > GetLocalAddrForPeer(CNode &node)
Returns a local address that we should advertise to this peer.
CService GetLocalAddress(const CNode &peer)
GlobalMutex g_maplocalhost_mutex
std::function< void(const CAddress &addr, const std::string &msg_type, std::span< const unsigned char > data, bool is_incoming)> CaptureMessage
Defaults to CaptureMessageToFile(), but can be overridden by unit tests.
bool SeenLocal(const CService &addr)
vote for a local address
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< CNetAddr > LookupHost(const std::string &name, unsigned int nMaxSolutions, bool fAllowLookup, DNSLookupFn dns_lookup_function)
Resolve a host string to its corresponding network addresses.
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
#define BOOST_CHECK(expr)
static const int PROTOCOL_VERSION
network protocol versioning
auto MakeByteSpan(const V &v) noexcept
constexpr auto MakeUCharSpan(const V &v) -> decltype(UCharSpanCast(std::span{v}))
Like the std::span constructor, but for (const) unsigned char member types only.
auto MakeWritableByteSpan(V &&v) noexcept
unsigned char * UCharCast(char *c)
static constexpr size_t size()
Identical to TestingSetup, but chain set to regtest.
void JumpOutOfIbd()
Toggle IsInitialBlockDownload from true to false.
std::unique_ptr< CConnman > connman
std::unique_ptr< AddrMan > addrman
std::unique_ptr< ChainstateManager > chainman
std::unique_ptr< PeerManager > peerman
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.