2017-07-05 02:40:22 +02:00
|
|
|
// Copyright (c) 2012-2016 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <addrman.h>
|
|
|
|
#include <test/test_dash.h>
|
2017-07-05 02:40:22 +02:00
|
|
|
#include <string>
|
|
|
|
#include <boost/test/unit_test.hpp>
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <hash.h>
|
|
|
|
#include <serialize.h>
|
|
|
|
#include <streams.h>
|
|
|
|
#include <net.h>
|
|
|
|
#include <netbase.h>
|
|
|
|
#include <chainparams.h>
|
|
|
|
#include <util.h>
|
2017-07-05 02:40:22 +02:00
|
|
|
|
2018-04-02 20:31:40 +02:00
|
|
|
#include <memory>
|
|
|
|
|
2017-07-05 02:40:22 +02:00
|
|
|
class CAddrManSerializationMock : public CAddrMan
|
|
|
|
{
|
|
|
|
public:
|
2016-11-09 12:32:57 +01:00
|
|
|
virtual void Serialize(CDataStream& s) const = 0;
|
2017-07-11 01:03:14 +02:00
|
|
|
|
|
|
|
//! Ensure that bucket placement is always the same for testing purposes.
|
|
|
|
void MakeDeterministic()
|
|
|
|
{
|
|
|
|
nKey.SetNull();
|
2016-10-18 15:38:44 +02:00
|
|
|
insecure_rand = FastRandomContext(true);
|
2017-07-11 01:03:14 +02:00
|
|
|
}
|
2017-07-05 02:40:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class CAddrManUncorrupted : public CAddrManSerializationMock
|
|
|
|
{
|
|
|
|
public:
|
2018-02-15 08:29:15 +01:00
|
|
|
void Serialize(CDataStream& s) const override
|
2017-07-05 02:40:22 +02:00
|
|
|
{
|
2016-11-09 12:32:57 +01:00
|
|
|
CAddrMan::Serialize(s);
|
2017-07-05 02:40:22 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CAddrManCorrupted : public CAddrManSerializationMock
|
|
|
|
{
|
|
|
|
public:
|
2018-02-15 08:29:15 +01:00
|
|
|
void Serialize(CDataStream& s) const override
|
2017-07-05 02:40:22 +02:00
|
|
|
{
|
|
|
|
// Produces corrupt output that claims addrman has 20 addrs when it only has one addr.
|
|
|
|
unsigned char nVersion = 1;
|
|
|
|
s << nVersion;
|
|
|
|
s << ((unsigned char)32);
|
|
|
|
s << nKey;
|
|
|
|
s << 10; // nNew
|
|
|
|
s << 10; // nTried
|
|
|
|
|
|
|
|
int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30);
|
|
|
|
s << nUBuckets;
|
|
|
|
|
2017-09-03 15:29:10 +02:00
|
|
|
CService serv;
|
|
|
|
Lookup("252.1.1.1", serv, 7777, false);
|
|
|
|
CAddress addr = CAddress(serv, NODE_NONE);
|
|
|
|
CNetAddr resolved;
|
|
|
|
LookupHost("252.2.2.2", resolved, false);
|
|
|
|
CAddrInfo info = CAddrInfo(addr, resolved);
|
2017-07-05 02:40:22 +02:00
|
|
|
s << info;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-09-27 13:25:42 +02:00
|
|
|
CDataStream AddrmanToStream(CAddrManSerializationMock& _addrman)
|
2017-07-05 02:40:22 +02:00
|
|
|
{
|
|
|
|
CDataStream ssPeersIn(SER_DISK, CLIENT_VERSION);
|
2018-03-30 13:10:00 +02:00
|
|
|
ssPeersIn << Params().MessageStart();
|
2016-09-27 13:25:42 +02:00
|
|
|
ssPeersIn << _addrman;
|
2017-07-05 02:40:22 +02:00
|
|
|
std::string str = ssPeersIn.str();
|
2017-01-05 11:31:56 +01:00
|
|
|
std::vector<unsigned char> vchData(str.begin(), str.end());
|
2017-07-05 02:40:22 +02:00
|
|
|
return CDataStream(vchData, SER_DISK, CLIENT_VERSION);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_SUITE(net_tests, BasicTestingSetup)
|
|
|
|
|
2017-04-26 09:34:08 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(cnode_listen_port)
|
|
|
|
{
|
|
|
|
// test default
|
|
|
|
unsigned short port = GetListenPort();
|
|
|
|
BOOST_CHECK(port == Params().GetDefaultPort());
|
|
|
|
// test set port
|
|
|
|
unsigned short altPort = 12345;
|
2019-06-24 18:44:27 +02:00
|
|
|
gArgs.SoftSetArg("-port", std::to_string(altPort));
|
2017-04-26 09:34:08 +02:00
|
|
|
port = GetListenPort();
|
|
|
|
BOOST_CHECK(port == altPort);
|
|
|
|
}
|
|
|
|
|
2017-07-05 02:40:22 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(caddrdb_read)
|
|
|
|
{
|
2018-07-17 14:04:33 +02:00
|
|
|
SetDataDir("caddrdb_read");
|
2017-07-05 02:40:22 +02:00
|
|
|
CAddrManUncorrupted addrmanUncorrupted;
|
2017-07-11 01:03:14 +02:00
|
|
|
addrmanUncorrupted.MakeDeterministic();
|
2017-07-05 02:40:22 +02:00
|
|
|
|
2017-09-03 15:29:10 +02:00
|
|
|
CService addr1, addr2, addr3;
|
|
|
|
Lookup("250.7.1.1", addr1, 8333, false);
|
|
|
|
Lookup("250.7.2.2", addr2, 9999, false);
|
|
|
|
Lookup("250.7.3.3", addr3, 9999, false);
|
2017-07-05 02:40:22 +02:00
|
|
|
|
|
|
|
// Add three addresses to new table.
|
2017-09-03 15:29:10 +02:00
|
|
|
CService source;
|
|
|
|
Lookup("252.5.1.1", source, 8333, false);
|
|
|
|
addrmanUncorrupted.Add(CAddress(addr1, NODE_NONE), source);
|
|
|
|
addrmanUncorrupted.Add(CAddress(addr2, NODE_NONE), source);
|
|
|
|
addrmanUncorrupted.Add(CAddress(addr3, NODE_NONE), source);
|
2017-07-05 02:40:22 +02:00
|
|
|
|
|
|
|
// Test that the de-serialization does not throw an exception.
|
|
|
|
CDataStream ssPeers1 = AddrmanToStream(addrmanUncorrupted);
|
|
|
|
bool exceptionThrown = false;
|
|
|
|
CAddrMan addrman1;
|
|
|
|
|
|
|
|
BOOST_CHECK(addrman1.size() == 0);
|
|
|
|
try {
|
|
|
|
unsigned char pchMsgTmp[4];
|
2018-03-30 13:10:00 +02:00
|
|
|
ssPeers1 >> pchMsgTmp;
|
2017-07-05 02:40:22 +02:00
|
|
|
ssPeers1 >> addrman1;
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
exceptionThrown = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_CHECK(addrman1.size() == 3);
|
|
|
|
BOOST_CHECK(exceptionThrown == false);
|
|
|
|
|
|
|
|
// Test that CAddrDB::Read creates an addrman with the correct number of addrs.
|
|
|
|
CDataStream ssPeers2 = AddrmanToStream(addrmanUncorrupted);
|
|
|
|
|
|
|
|
CAddrMan addrman2;
|
|
|
|
CAddrDB adb;
|
|
|
|
BOOST_CHECK(addrman2.size() == 0);
|
|
|
|
adb.Read(addrman2, ssPeers2);
|
|
|
|
BOOST_CHECK(addrman2.size() == 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(caddrdb_read_corrupted)
|
|
|
|
{
|
2018-07-17 14:04:33 +02:00
|
|
|
SetDataDir("caddrdb_read_corrupted");
|
2017-07-05 02:40:22 +02:00
|
|
|
CAddrManCorrupted addrmanCorrupted;
|
2017-07-11 01:03:14 +02:00
|
|
|
addrmanCorrupted.MakeDeterministic();
|
2017-07-05 02:40:22 +02:00
|
|
|
|
|
|
|
// Test that the de-serialization of corrupted addrman throws an exception.
|
|
|
|
CDataStream ssPeers1 = AddrmanToStream(addrmanCorrupted);
|
|
|
|
bool exceptionThrown = false;
|
|
|
|
CAddrMan addrman1;
|
|
|
|
BOOST_CHECK(addrman1.size() == 0);
|
|
|
|
try {
|
|
|
|
unsigned char pchMsgTmp[4];
|
2018-03-30 13:10:00 +02:00
|
|
|
ssPeers1 >> pchMsgTmp;
|
2017-07-05 02:40:22 +02:00
|
|
|
ssPeers1 >> addrman1;
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
exceptionThrown = true;
|
|
|
|
}
|
2016-05-19 02:14:26 +02:00
|
|
|
// Even through de-serialization failed addrman is not left in a clean state.
|
2017-07-05 02:40:22 +02:00
|
|
|
BOOST_CHECK(addrman1.size() == 1);
|
|
|
|
BOOST_CHECK(exceptionThrown);
|
|
|
|
|
|
|
|
// Test that CAddrDB::Read leaves addrman in a clean state if de-serialization fails.
|
|
|
|
CDataStream ssPeers2 = AddrmanToStream(addrmanCorrupted);
|
|
|
|
|
|
|
|
CAddrMan addrman2;
|
|
|
|
CAddrDB adb;
|
|
|
|
BOOST_CHECK(addrman2.size() == 0);
|
|
|
|
adb.Read(addrman2, ssPeers2);
|
|
|
|
BOOST_CHECK(addrman2.size() == 0);
|
|
|
|
}
|
|
|
|
|
2017-07-17 12:39:12 +02:00
|
|
|
BOOST_AUTO_TEST_CASE(cnode_simple_test)
|
|
|
|
{
|
|
|
|
SOCKET hSocket = INVALID_SOCKET;
|
Backport Bitcoin PR#8085: p2p: Begin encapsulation (#1537)
* net: move CBanDB and CAddrDB out of net.h/cpp
This will eventually solve a circular dependency
* net: Create CConnman to encapsulate p2p connections
* net: Move socket binding into CConnman
* net: move OpenNetworkConnection into CConnman
* net: move ban and addrman functions into CConnman
* net: Add oneshot functions to CConnman
* net: move added node functions to CConnman
* net: Add most functions needed for vNodes to CConnman
* net: handle nodesignals in CConnman
* net: Pass CConnection to wallet rather than using the global
* net: Add rpc error for missing/disabled p2p functionality
* net: Pass CConnman around as needed
* gui: add NodeID to the peer table
* net: create generic functor accessors and move vNodes to CConnman
* net: move whitelist functions into CConnman
* net: move nLastNodeId to CConnman
* net: move nLocalHostNonce to CConnman
This behavior seems to have been quite racy and broken.
Move nLocalHostNonce into CNode, and check received nonces against all
non-fully-connected nodes. If there's a match, assume we've connected
to ourself.
* net: move messageHandlerCondition to CConnman
* net: move send/recv statistics to CConnman
* net: move SendBufferSize/ReceiveFloodSize to CConnman
* net: move nLocalServices/nRelevantServices to CConnman
These are in-turn passed to CNode at connection time. This allows us to offer
different services to different peers (or test the effects of doing so).
* net: move semOutbound and semMasternodeOutbound to CConnman
* net: SocketSendData returns written size
* net: move max/max-outbound to CConnman
* net: Pass best block known height into CConnman
CConnman then passes the current best height into CNode at creation time.
This way CConnman/CNode have no dependency on main for height, and the signals
only move in one direction.
This also helps to prevent identity leakage a tiny bit. Before this change, an
attacker could theoretically make 2 connections on different interfaces. They
would connect fully on one, and only establish the initial connection on the
other. Once they receive a new block, they would relay it to your first
connection, and immediately commence the version handshake on the second. Since
the new block height is reflected immediately, they could attempt to learn
whether the two connections were correlated.
This is, of course, incredibly unlikely to work due to the small timings
involved and receipt from other senders. But it doesn't hurt to lock-in
nBestHeight at the time of connection, rather than letting the remote choose
the time.
* net: pass CClientUIInterface into CConnman
* net: Drop StartNode/StopNode and use CConnman directly
* net: Introduce CConnection::Options to avoid passing so many params
* net: add nSendBufferMaxSize/nReceiveFloodSize to CConnection::Options
* net: move vNodesDisconnected into CConnman
* Made the ForEachNode* functions in src/net.cpp more pragmatic and self documenting
* Convert ForEachNode* functions to take a templated function argument rather than a std::function to eliminate std::function overhead
* net: move MAX_FEELER_CONNECTIONS into connman
2017-07-21 11:35:19 +02:00
|
|
|
NodeId id = 0;
|
|
|
|
int height = 0;
|
2017-07-17 12:39:12 +02:00
|
|
|
|
|
|
|
in_addr ipv4Addr;
|
|
|
|
ipv4Addr.s_addr = 0xa0b0c001;
|
2020-07-29 03:23:12 +02:00
|
|
|
|
2017-07-17 12:39:12 +02:00
|
|
|
CAddress addr = CAddress(CService(ipv4Addr, 7777), NODE_NETWORK);
|
2018-03-01 20:50:33 +01:00
|
|
|
std::string pszDest;
|
2017-07-17 12:39:12 +02:00
|
|
|
bool fInboundIn = false;
|
|
|
|
|
|
|
|
// Test that fFeeler is false by default.
|
2017-06-05 15:39:11 +02:00
|
|
|
std::unique_ptr<CNode> pnode1(new CNode(id++, NODE_NETWORK, height, hSocket, addr, 0, 0, CAddress(), pszDest, fInboundIn));
|
2017-07-17 12:39:12 +02:00
|
|
|
BOOST_CHECK(pnode1->fInbound == false);
|
|
|
|
BOOST_CHECK(pnode1->fFeeler == false);
|
|
|
|
|
|
|
|
fInboundIn = true;
|
2017-06-05 15:39:11 +02:00
|
|
|
std::unique_ptr<CNode> pnode2(new CNode(id++, NODE_NETWORK, height, hSocket, addr, 1, 1, CAddress(), pszDest, fInboundIn));
|
2017-07-17 12:39:12 +02:00
|
|
|
BOOST_CHECK(pnode2->fInbound == true);
|
|
|
|
BOOST_CHECK(pnode2->fFeeler == false);
|
|
|
|
}
|
|
|
|
|
2019-11-05 18:38:17 +01:00
|
|
|
BOOST_AUTO_TEST_CASE(PoissonNextSend)
|
|
|
|
{
|
|
|
|
g_mock_deterministic_tests = true;
|
|
|
|
int64_t now = 5000;
|
|
|
|
int average_interval_seconds = 600;
|
|
|
|
|
|
|
|
auto poisson = ::PoissonNextSend(now, average_interval_seconds);
|
|
|
|
std::chrono::microseconds poisson_chrono = ::PoissonNextSend(std::chrono::microseconds{now}, std::chrono::seconds{average_interval_seconds});
|
|
|
|
|
|
|
|
BOOST_CHECK_EQUAL(poisson, poisson_chrono.count());
|
|
|
|
|
|
|
|
g_mock_deterministic_tests = false;
|
|
|
|
}
|
|
|
|
|
2020-06-10 02:06:54 +02:00
|
|
|
// prior to PR #14728, this test triggers an undefined behavior
|
|
|
|
BOOST_AUTO_TEST_CASE(ipv4_peer_with_ipv6_addrMe_test)
|
|
|
|
{
|
|
|
|
// set up local addresses; all that's necessary to reproduce the bug is
|
|
|
|
// that a normal IPv4 address is among the entries, but if this address is
|
|
|
|
// !IsRoutable the undefined behavior is easier to trigger deterministically
|
|
|
|
{
|
|
|
|
LOCK(cs_mapLocalHost);
|
|
|
|
in_addr ipv4AddrLocal;
|
|
|
|
ipv4AddrLocal.s_addr = 0x0100007f;
|
|
|
|
CNetAddr addr = CNetAddr(ipv4AddrLocal);
|
|
|
|
LocalServiceInfo lsi;
|
|
|
|
lsi.nScore = 23;
|
|
|
|
lsi.nPort = 42;
|
|
|
|
mapLocalHost[addr] = lsi;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a peer with an IPv4 address
|
|
|
|
in_addr ipv4AddrPeer;
|
|
|
|
ipv4AddrPeer.s_addr = 0xa0b0c001;
|
|
|
|
CAddress addr = CAddress(CService(ipv4AddrPeer, 7777), NODE_NETWORK);
|
|
|
|
std::unique_ptr<CNode> pnode = MakeUnique<CNode>(0, NODE_NETWORK, 0, INVALID_SOCKET, addr, 0, 0, CAddress{}, std::string{}, false);
|
|
|
|
pnode->fSuccessfullyConnected.store(true);
|
|
|
|
|
|
|
|
// the peer claims to be reaching us via IPv6
|
|
|
|
in6_addr ipv6AddrLocal;
|
|
|
|
memset(ipv6AddrLocal.s6_addr, 0, 16);
|
|
|
|
ipv6AddrLocal.s6_addr[0] = 0xcc;
|
|
|
|
CAddress addrLocal = CAddress(CService(ipv6AddrLocal, 7777), NODE_NETWORK);
|
|
|
|
pnode->SetAddrLocal(addrLocal);
|
|
|
|
|
|
|
|
// before patch, this causes undefined behavior detectable with clang's -fsanitize=memory
|
|
|
|
AdvertiseLocal(&*pnode);
|
|
|
|
|
|
|
|
// suppress no-checks-run warning; if this test fails, it's by triggering a sanitizer
|
|
|
|
BOOST_CHECK(1);
|
|
|
|
}
|
|
|
|
|
2017-07-05 02:40:22 +02:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|