7#include <chainparams.h>
29#include <boost/test/unit_test.hpp>
38using namespace std::literals;
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++,
157 addr =
LookupHost(
"255.255.255.255",
false).value();
166 addr =
LookupHost(
"12.34.56.78",
false).value();
184 addr =
LookupHost(
"1122:3344:5566:7788:9900:aabb:ccdd:eeff",
false).value();
209 const char*
torv3_addr =
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion";
227 "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd\0wtf.onion", 66}));
236 const char*
i2p_addr =
"UDHDrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.I2P";
258 !addr.
SetSpecial(
"pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscsad.b32.i2p"));
281 {
"0000:0000:0000:0000:0000:0000:0000:0000",
"::"},
282 {
"000:0000:000:00:0:00:000:0000",
"::"},
283 {
"000:000:000:000:000:000:000:000",
"::"},
284 {
"00:00:00:00:00:00:00:00",
"::"},
285 {
"0:0:0:0:0:0:0:0",
"::"},
286 {
"0:0:0:0:0:0:0:1",
"::1"},
287 {
"2001:0:0:1:0:0:0:1",
"2001:0:0:1::1"},
288 {
"2001:0db8:0:0:1:0:0:1",
"2001:db8::1:0:0:1"},
289 {
"2001:0db8:85a3:0000:0000:8a2e:0370:7334",
"2001:db8:85a3::8a2e:370:7334"},
290 {
"2001:0db8::0001",
"2001:db8::1"},
291 {
"2001:0db8::0001:0000",
"2001:db8::1:0"},
292 {
"2001:0db8::1:0:0:1",
"2001:db8::1:0:0:1"},
293 {
"2001:db8:0000:0:1::1",
"2001:db8::1:0:0:1"},
294 {
"2001:db8:0000:1:1:1:1:1",
"2001:db8:0:1:1:1:1:1"},
295 {
"2001:db8:0:0:0:0:2:1",
"2001:db8::2:1"},
296 {
"2001:db8:0:0:0::1",
"2001:db8::1"},
297 {
"2001:db8:0:0:1:0:0: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::1",
"2001:db8::1:0:0:1"},
300 {
"2001:db8:0:0::1",
"2001:db8::1"},
301 {
"2001:db8:0:0:aaaa::1",
"2001:db8::aaaa:0:0:1"},
302 {
"2001:db8:0:1:1:1:1:1",
"2001:db8:0:1:1:1:1:1"},
303 {
"2001:db8:0::1",
"2001:db8::1"},
304 {
"2001:db8:85a3:0:0:8a2e:370:7334",
"2001:db8:85a3::8a2e:370:7334"},
305 {
"2001:db8::0:1",
"2001:db8::1"},
306 {
"2001:db8::0:1:0:0:1",
"2001:db8::1:0:0:1"},
307 {
"2001:DB8::1",
"2001:db8::1"},
308 {
"2001:db8::1",
"2001:db8::1"},
309 {
"2001:db8::1:0:0:1",
"2001:db8::1:0:0:1"},
310 {
"2001:db8::1:1:1:1:1",
"2001:db8:0:1:1:1:1:1"},
311 {
"2001:db8::aaaa:0:0:1",
"2001:db8::aaaa:0:0:1"},
312 {
"2001:db8:aaaa:bbbb:cccc:dddd:0:1",
"2001:db8:aaaa:bbbb:cccc:dddd:0:1"},
313 {
"2001:db8:aaaa:bbbb:cccc:dddd::1",
"2001:db8:aaaa:bbbb:cccc:dddd:0:1"},
314 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:0001",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
315 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:001",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
316 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:01",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
317 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:1"},
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"},
320 {
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:AaAa",
"2001:db8:aaaa:bbbb:cccc:dddd:eeee:aaaa"},
344 addr =
LookupHost(
"1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b",
false).value();
378 addr =
LookupHost(
"1a1b:2a2b:3a3b:4a4b:5a5b:6a6b:7a7b:8a8b",
false).value();
427 HasReason(
"BIP155 IPv4 address with length 5 (should be 4)"));
436 HasReason(
"Address too long: 513 > 512"));
443 "0102030405060708090a0b0c0d0e0f10"_hex;
454 "fd6b88c08724ca978112ca1bbdcafac2"_hex;
467 HasReason(
"BIP155 IPv6 address with length 4 (should be 16)"));
474 "00000000000000000000ffff01020304"_hex;
482 "fd87d87eeb430102030405060708090a"_hex;
490 "f1f2f3f4f5f6f7f8f9fa"_hex;
498 "79bcc625184b05194975c28b66b66b04"
499 "69f7f6556fb1ac3189a79b40dda32f1f"_hex;
505 "pg6mmjiyjmcrsslvykfwnntlaru7p5svn6y2ymmju6nubxndf4pscryd.onion");
513 HasReason(
"BIP155 TORv3 address with length 0 (should be 32)"));
520 "a2894dabaec08c0051a481a6dac88b64"
521 "f98232ae42d4b6fd2fa81952dfe36a87"_hex;
527 "ukeu3k5oycgaauneqgtnvselmt4yemvoilkln7jpvamvfx7dnkdq.b32.i2p");
535 HasReason(
"BIP155 I2P address with length 3 (should be 32)"));
542 "fc000001000200030004000500060007"_hex;
553 "aa000001000200030004000500060007"_hex;
564 HasReason(
"BIP155 CJDNS address with length 1 (should be 16)"));
571 "01020304050607"_hex;
573 HasReason(
"Address too long: 33554432 > 512"));
615 std::unique_ptr<CNode>
pnode = std::make_unique<CNode>(0,
625 pnode->fSuccessfullyConnected.store(
true);
632 pnode->SetAddrLocal(addrLocal);
680 peer_out.fSuccessfullyConnected =
true;
702 peer_in.fSuccessfullyConnected =
true;
813 return it !=
mapLocalHost.end() ? it->second.nScore : 0;
888 connman.FlushSendBuffer(peer);
893 peer.fPauseSend =
false;
894 bool more_work{connman.ProcessMessagesOnce(peer)};
898 connman.FlushSendBuffer(peer);
901 peer.fPauseSend =
false;
903 more_work = connman.ProcessMessagesOnce(peer);
913 std::span<const unsigned char>
data,
921 if (addr == expected) {
943 return std::make_unique<CNode>(0,
1063class V2TransportTester
1068 bool m_test_initiator;
1070 std::vector<uint8_t> m_sent_garbage;
1071 std::vector<uint8_t> m_recv_garbage;
1072 std::vector<uint8_t> m_to_send;
1073 std::vector<uint8_t> m_received;
1074 std::deque<CSerializedNetMsg> m_msg_to_send;
1075 bool m_sent_aad{
false};
1092 using InteractResult = std::optional<std::vector<std::optional<CNetMessage>>>;
1101 std::vector<std::optional<CNetMessage>>
ret;
1103 bool progress{
false};
1105 if (!m_to_send.empty()) {
1106 std::span<const uint8_t>
to_send = std::span{m_to_send}.first(1 + m_rng.
randrange(m_to_send.size()));
1109 return std::nullopt;
1113 m_to_send.erase(m_to_send.begin(), m_to_send.begin() + (
old_len -
to_send.size()));
1121 ret.emplace_back(std::nullopt);
1123 ret.emplace_back(std::move(msg));
1128 if (!m_msg_to_send.empty() && (!progress || m_rng.
randbool())) {
1130 m_msg_to_send.pop_front();
1142 if (!progress)
break;
1151 void Send(std::span<const uint8_t>
data)
1153 m_to_send.insert(m_to_send.end(),
data.begin(),
data.end());
1177 Send(m_sent_garbage);
1197 msg.m_type = std::move(m_type);
1199 m_msg_to_send.push_back(std::move(msg));
1219 void SendPacket(std::span<const uint8_t>
content, std::span<const uint8_t>
aad = {},
bool ignore =
false)
1243 std::span<const std::uint8_t>
aad;
1245 if (!m_sent_aad)
aad = m_sent_garbage;
1296 m_recv_garbage.assign(m_received.begin(), m_received.begin() +
garblen);
1328 for (
unsigned i = 0; i < 12; ++i) {
1329 if (i < m_type.size()) {
1365 auto info = m_transport.
GetInfo();
1382 for (
int i = 0; i < 10; ++i) {
1383 V2TransportTester
tester(m_rng,
true);
1390 tester.SendGarbageTerm();
1397 tester.CompareSessionIDs();
1401 tester.SendMessage(0, {});
1439 for (
int i = 0; i < 10; ++i) {
1440 V2TransportTester
tester(m_rng,
false);
1447 tester.SendGarbageTerm();
1454 tester.CompareSessionIDs();
1477 for (
int i = 0; i < 50; ++i) {
1503 tester.SendGarbageTerm();
1515 tester.CompareSessionIDs();
1530 tester.SendMessage(
"foobar", {});
1531 tester.AddMessage(
"barfoo", {});
1545 tester.ReceiveMessage(
"barfoo", {});
1550 V2TransportTester
tester(m_rng,
true);
1557 tester.SendGarbageTerm();
1564 V2TransportTester
tester(m_rng,
false);
1571 tester.SendGarbageTerm();
1578 V2TransportTester
tester(m_rng,
true);
1597 tester.SendGarbageTerm();
1604 tester.CompareSessionIDs();
1622 V2TransportTester
tester(m_rng,
false);
1630 V2TransportTester
tester(m_rng,
false);
1643 for (
const bool too_old : {
true,
false}) {
1654 connman.Handshake(
node,
1684 connman.Handshake(
node,
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.
std::unordered_set< Network > All() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
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.
static const PrecomputedData data
Precomputed COutPoint and CCoins values.
@ 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_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)
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 AddLocal(const CService &addr_, int nScore, bool add_even_if_unreachable)
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_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
ServiceFlags
nServices flags
constexpr int MIN_PEER_PROTO_VERSION
disconnect from peers older than this proto version
constexpr 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.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.