tests: remove member connman/peerLogic in TestingSetup

This commit is contained in:
Cory Fields 2017-10-05 11:52:50 -04:00 committed by Carl Dong
parent 7cc2b9f678
commit 136bd7926c
3 changed files with 33 additions and 29 deletions

View file

@ -20,6 +20,23 @@
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
struct CConnmanTest : public CConnman {
using CConnman::CConnman;
void AddNode(CNode& node)
{
LOCK(cs_vNodes);
vNodes.push_back(&node);
}
void ClearNodes()
{
LOCK(cs_vNodes);
for (CNode* node : vNodes) {
delete node;
}
vNodes.clear();
}
};
// Tests these internal-to-net_processing.cpp methods: // Tests these internal-to-net_processing.cpp methods:
extern bool AddOrphanTx(const CTransactionRef& tx, NodeId peer); extern bool AddOrphanTx(const CTransactionRef& tx, NodeId peer);
extern void EraseOrphansFor(NodeId peer); extern void EraseOrphansFor(NodeId peer);
@ -57,6 +74,8 @@ BOOST_FIXTURE_TEST_SUITE(denialofservice_tests, TestingSetup)
// work. // work.
BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction) BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
{ {
auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), scheduler, false);
// Mock an outbound peer // Mock an outbound peer
CAddress addr1(ip(0xa0b0c001), NODE_NONE); CAddress addr1(ip(0xa0b0c001), NODE_NONE);
@ -109,7 +128,7 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction)
peerLogic->FinalizeNode(dummyNode1.GetId(), dummy); peerLogic->FinalizeNode(dummyNode1.GetId(), dummy);
} }
static void AddRandomOutboundPeer(std::vector<CNode *> &vNodes, PeerLogicValidation &peerLogic) static void AddRandomOutboundPeer(std::vector<CNode *> &vNodes, PeerLogicValidation &peerLogic, CConnmanTest* connman)
{ {
CAddress addr(ip(g_insecure_rand_ctx.randbits(32)), NODE_NONE); CAddress addr(ip(g_insecure_rand_ctx.randbits(32)), NODE_NONE);
vNodes.emplace_back(new CNode(id++, ServiceFlags(NODE_NETWORK|NODE_WITNESS), 0, INVALID_SOCKET, addr, 0, 0, CAddress(), "", /*fInboundIn=*/ false)); vNodes.emplace_back(new CNode(id++, ServiceFlags(NODE_NETWORK|NODE_WITNESS), 0, INVALID_SOCKET, addr, 0, 0, CAddress(), "", /*fInboundIn=*/ false));
@ -120,11 +139,14 @@ static void AddRandomOutboundPeer(std::vector<CNode *> &vNodes, PeerLogicValidat
node.nVersion = 1; node.nVersion = 1;
node.fSuccessfullyConnected = true; node.fSuccessfullyConnected = true;
CConnmanTest::AddNode(node); connman->AddNode(node);
} }
BOOST_AUTO_TEST_CASE(stale_tip_peer_management) BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
{ {
auto connman = MakeUnique<CConnmanTest>(0x1337, 0x1337);
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), scheduler, false);
const Consensus::Params& consensusParams = Params().GetConsensus(); const Consensus::Params& consensusParams = Params().GetConsensus();
constexpr int nMaxOutbound = 8; constexpr int nMaxOutbound = 8;
CConnman::Options options; CConnman::Options options;
@ -137,7 +159,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
// Mock some outbound peers // Mock some outbound peers
for (int i=0; i<nMaxOutbound; ++i) { for (int i=0; i<nMaxOutbound; ++i) {
AddRandomOutboundPeer(vNodes, *peerLogic); AddRandomOutboundPeer(vNodes, *peerLogic, connman.get());
} }
peerLogic->CheckForStaleTipAndEvictPeers(consensusParams); peerLogic->CheckForStaleTipAndEvictPeers(consensusParams);
@ -162,7 +184,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
// If we add one more peer, something should get marked for eviction // If we add one more peer, something should get marked for eviction
// on the next check (since we're mocking the time to be in the future, the // on the next check (since we're mocking the time to be in the future, the
// required time connected check should be satisfied). // required time connected check should be satisfied).
AddRandomOutboundPeer(vNodes, *peerLogic); AddRandomOutboundPeer(vNodes, *peerLogic, connman.get());
peerLogic->CheckForStaleTipAndEvictPeers(consensusParams); peerLogic->CheckForStaleTipAndEvictPeers(consensusParams);
for (int i=0; i<nMaxOutbound; ++i) { for (int i=0; i<nMaxOutbound; ++i) {
@ -189,11 +211,13 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management)
peerLogic->FinalizeNode(node->GetId(), dummy); peerLogic->FinalizeNode(node->GetId(), dummy);
} }
CConnmanTest::ClearNodes(); connman->ClearNodes();
} }
BOOST_AUTO_TEST_CASE(DoS_banning) BOOST_AUTO_TEST_CASE(DoS_banning)
{ {
auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), scheduler, false);
connman->ClearBanned(); connman->ClearBanned();
CAddress addr1(ip(0xa0b0c001), NODE_NONE); CAddress addr1(ip(0xa0b0c001), NODE_NONE);
@ -246,6 +270,8 @@ BOOST_AUTO_TEST_CASE(DoS_banning)
BOOST_AUTO_TEST_CASE(DoS_banscore) BOOST_AUTO_TEST_CASE(DoS_banscore)
{ {
auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), scheduler, false);
connman->ClearBanned(); connman->ClearBanned();
gArgs.ForceSetArg("-banscore", "111"); // because 11 is my favorite number gArgs.ForceSetArg("-banscore", "111"); // because 11 is my favorite number
@ -290,6 +316,8 @@ BOOST_AUTO_TEST_CASE(DoS_banscore)
BOOST_AUTO_TEST_CASE(DoS_bantime) BOOST_AUTO_TEST_CASE(DoS_bantime)
{ {
auto connman = MakeUnique<CConnman>(0x1337, 0x1337);
auto peerLogic = MakeUnique<PeerLogicValidation>(connman.get(), scheduler, false);
connman->ClearBanned(); connman->ClearBanned();
int64_t nStartTime = GetTime(); int64_t nStartTime = GetTime();

View file

@ -24,21 +24,6 @@ const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
FastRandomContext g_insecure_rand_ctx; FastRandomContext g_insecure_rand_ctx;
void CConnmanTest::AddNode(CNode& node)
{
LOCK(g_connman->cs_vNodes);
g_connman->vNodes.push_back(&node);
}
void CConnmanTest::ClearNodes()
{
LOCK(g_connman->cs_vNodes);
for (const CNode* node : g_connman->vNodes) {
delete node;
}
g_connman->vNodes.clear();
}
std::ostream& operator<<(std::ostream& os, const uint256& num) std::ostream& operator<<(std::ostream& os, const uint256& num)
{ {
os << num.ToString(); os << num.ToString();
@ -109,8 +94,6 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
for (int i=0; i < nScriptCheckThreads-1; i++) for (int i=0; i < nScriptCheckThreads-1; i++)
threadGroup.create_thread(&ThreadScriptCheck); threadGroup.create_thread(&ThreadScriptCheck);
g_connman = MakeUnique<CConnman>(0x1337, 0x1337); // Deterministic randomness for tests. g_connman = MakeUnique<CConnman>(0x1337, 0x1337); // Deterministic randomness for tests.
connman = g_connman.get();
peerLogic.reset(new PeerLogicValidation(connman, scheduler, /*enable_bip61=*/true));
} }
TestingSetup::~TestingSetup() TestingSetup::~TestingSetup()
@ -120,7 +103,6 @@ TestingSetup::~TestingSetup()
GetMainSignals().FlushBackgroundCallbacks(); GetMainSignals().FlushBackgroundCallbacks();
GetMainSignals().UnregisterBackgroundSignalScheduler(); GetMainSignals().UnregisterBackgroundSignalScheduler();
g_connman.reset(); g_connman.reset();
peerLogic.reset();
UnloadBlockIndex(); UnloadBlockIndex();
pcoinsTip.reset(); pcoinsTip.reset();
pcoinsdbview.reset(); pcoinsdbview.reset();

View file

@ -68,17 +68,11 @@ private:
*/ */
class CConnman; class CConnman;
class CNode; class CNode;
struct CConnmanTest {
static void AddNode(CNode& node);
static void ClearNodes();
};
class PeerLogicValidation; class PeerLogicValidation;
struct TestingSetup : public BasicTestingSetup { struct TestingSetup : public BasicTestingSetup {
boost::thread_group threadGroup; boost::thread_group threadGroup;
CConnman* connman;
CScheduler scheduler; CScheduler scheduler;
std::unique_ptr<PeerLogicValidation> peerLogic;
explicit TestingSetup(const std::string& chainName = CBaseChainParams::MAIN); explicit TestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
~TestingSetup(); ~TestingSetup();