Files

133 lines
4.0 KiB
C++
Raw Permalink Normal View History

2017-11-09 19:34:51 +01:00
/*
* This file is part of the Flowee project
* Copyright (C) 2011-2015 The Bitcoin Core developers
2021-06-20 22:44:44 +02:00
* Copyright (C) 2020 Tom Zander <tom@flowee.org>
2017-11-09 19:34:51 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2014-03-18 10:11:00 +01:00
2011-09-06 16:09:04 -04:00
// Unit tests for denial-of-service detection/prevention code
2013-04-13 00:13:08 -05:00
2015-07-05 14:17:46 +02:00
#include "chainparams.h"
2013-04-13 00:13:08 -05:00
#include "main.h"
#include "net.h"
2014-08-21 16:11:09 +02:00
#include "util.h"
2026-05-14 13:13:40 +02:00
#include "utiltime.h"
#include <txorphancache.h>
#include <SettingsDefaults.h>
2013-04-13 00:13:08 -05:00
#include "test/test_bitcoin.h"
2013-04-13 00:13:08 -05:00
#include <stdint.h>
2012-05-16 15:57:04 -04:00
#include <boost/date_time/posix_time/posix_time_types.hpp>
2013-04-13 00:13:08 -05:00
#include <boost/test/unit_test.hpp>
2012-01-03 23:33:31 +01:00
CService ip(uint32_t i)
{
struct in_addr s;
s.s_addr = i;
return CService(CNetAddr(s), Params().GetDefaultPort());
2012-01-03 23:33:31 +01:00
}
2011-09-06 16:09:04 -04:00
2017-02-08 17:17:38 +01:00
class OrphanCacheMock : public CTxOrphanCache
{
public:
std::map<uint256, COrphanTx> mapOrphanTransactions() {
return m_mapOrphanTransactions;
}
std::map<uint256, std::set<uint256> > mapOrphanTransactionsByPrev() {
return m_mapOrphanTransactionsByPrev;
}
void LimitOrphanTxSizePublic(unsigned int max) {
2019-11-21 20:03:26 +01:00
limitOrphanTxSize(max);
2017-02-08 17:17:38 +01:00
}
CTransaction RandomOrphan()
{
auto it = m_mapOrphanTransactions.lower_bound(GetRandHash());
if (it == m_mapOrphanTransactions.end())
it = m_mapOrphanTransactions.begin();
return it->second.tx;
}
};
BOOST_FIXTURE_TEST_SUITE(DoS_tests, TestingSetup)
2011-09-06 16:09:04 -04:00
BOOST_AUTO_TEST_CASE(DoS_banning)
{
CNode::ClearBanned();
2012-01-03 23:33:31 +01:00
CAddress addr1(ip(0xa0b0c001));
2012-02-12 13:45:24 +01:00
CNode dummyNode1(INVALID_SOCKET, addr1, "", true);
2013-11-18 01:25:17 +01:00
dummyNode1.nVersion = 1;
Misbehaving(dummyNode1.GetId(), 100); // Should get banned
SendMessages(&dummyNode1);
2012-01-03 23:33:31 +01:00
BOOST_CHECK(CNode::IsBanned(addr1));
2012-07-26 00:48:39 +00:00
BOOST_CHECK(!CNode::IsBanned(ip(0xa0b0c001|0x0000ff00))); // Different IP, not banned
2011-09-06 16:09:04 -04:00
2012-01-03 23:33:31 +01:00
CAddress addr2(ip(0xa0b0c002));
2012-02-12 13:45:24 +01:00
CNode dummyNode2(INVALID_SOCKET, addr2, "", true);
2013-11-18 01:25:17 +01:00
dummyNode2.nVersion = 1;
Misbehaving(dummyNode2.GetId(), 50);
SendMessages(&dummyNode2);
2012-01-03 23:33:31 +01:00
BOOST_CHECK(!CNode::IsBanned(addr2)); // 2 not banned yet...
BOOST_CHECK(CNode::IsBanned(addr1)); // ... but 1 still should be
2013-11-18 01:25:17 +01:00
Misbehaving(dummyNode2.GetId(), 50);
SendMessages(&dummyNode2);
2012-01-03 23:33:31 +01:00
BOOST_CHECK(CNode::IsBanned(addr2));
2012-10-05 19:22:21 +02:00
}
2011-09-06 16:09:04 -04:00
BOOST_AUTO_TEST_CASE(DoS_banscore)
{
CNode::ClearBanned();
mapArgs["-banscore"] = "111"; // because 11 is my favorite number
2012-01-03 23:33:31 +01:00
CAddress addr1(ip(0xa0b0c001));
2012-02-12 13:45:24 +01:00
CNode dummyNode1(INVALID_SOCKET, addr1, "", true);
2013-11-18 01:25:17 +01:00
dummyNode1.nVersion = 1;
Misbehaving(dummyNode1.GetId(), 100);
SendMessages(&dummyNode1);
2012-01-03 23:33:31 +01:00
BOOST_CHECK(!CNode::IsBanned(addr1));
2013-11-18 01:25:17 +01:00
Misbehaving(dummyNode1.GetId(), 10);
SendMessages(&dummyNode1);
2012-01-03 23:33:31 +01:00
BOOST_CHECK(!CNode::IsBanned(addr1));
2013-11-18 01:25:17 +01:00
Misbehaving(dummyNode1.GetId(), 1);
SendMessages(&dummyNode1);
2012-01-03 23:33:31 +01:00
BOOST_CHECK(CNode::IsBanned(addr1));
2012-05-16 15:57:04 -04:00
mapArgs.erase("-banscore");
2011-09-06 16:09:04 -04:00
}
BOOST_AUTO_TEST_CASE(DoS_bantime)
{
CNode::ClearBanned();
2013-04-13 00:13:08 -05:00
int64_t nStartTime = GetTime();
2011-09-06 16:09:04 -04:00
SetMockTime(nStartTime); // Overrides future calls to GetTime()
2012-01-03 23:33:31 +01:00
CAddress addr(ip(0xa0b0c001));
2012-02-12 13:45:24 +01:00
CNode dummyNode(INVALID_SOCKET, addr, "", true);
2013-11-18 01:25:17 +01:00
dummyNode.nVersion = 1;
2011-09-06 16:09:04 -04:00
2013-11-18 01:25:17 +01:00
Misbehaving(dummyNode.GetId(), 100);
SendMessages(&dummyNode);
2012-01-03 23:33:31 +01:00
BOOST_CHECK(CNode::IsBanned(addr));
2011-09-06 16:09:04 -04:00
SetMockTime(nStartTime+60*60);
2012-01-03 23:33:31 +01:00
BOOST_CHECK(CNode::IsBanned(addr));
2011-09-06 16:09:04 -04:00
SetMockTime(nStartTime+60*60*24+1);
2012-01-03 23:33:31 +01:00
BOOST_CHECK(!CNode::IsBanned(addr));
}
2011-09-06 16:09:04 -04:00
BOOST_AUTO_TEST_SUITE_END()