-
+ A025DBC1426005B603C696E86B278AD6F7730E16B91433747844EA7931CB631715A2B50B677F4E96A9682F62A8E65C01673B194B791B9847476FA3406122083A
bitcoin/src/test/DoS_tests.cpp
(0 . 0)(1 . 118)
21086 //
21087 // Unit tests for denial-of-service detection/prevention code
21088 //
21089 #include <boost/assign/list_of.hpp> // for 'map_list_of()'
21090 #include <boost/test/unit_test.hpp>
21091 #include <boost/foreach.hpp>
21092
21093 #include "../main.h"
21094 #include "../net.h"
21095 #include "../util.h"
21096
21097 using namespace std;
21098
21099 BOOST_AUTO_TEST_SUITE(DoS_tests)
21100
21101 BOOST_AUTO_TEST_CASE(DoS_banning)
21102 {
21103 CNode::ClearBanned();
21104 CAddress addr1(0xa0b0c001);
21105 CNode dummyNode1(INVALID_SOCKET, addr1, true);
21106 dummyNode1.Misbehaving(100); // Should get banned
21107 BOOST_CHECK(CNode::IsBanned(addr1.ip));
21108 BOOST_CHECK(!CNode::IsBanned(addr1.ip|0x0000ff00)); // Different ip, not banned
21109
21110 CAddress addr2(0xa0b0c002);
21111 CNode dummyNode2(INVALID_SOCKET, addr2, true);
21112 dummyNode2.Misbehaving(50);
21113 BOOST_CHECK(!CNode::IsBanned(addr2.ip)); // 2 not banned yet...
21114 BOOST_CHECK(CNode::IsBanned(addr1.ip)); // ... but 1 still should be
21115 dummyNode2.Misbehaving(50);
21116 BOOST_CHECK(CNode::IsBanned(addr2.ip));
21117 }
21118
21119 BOOST_AUTO_TEST_CASE(DoS_banscore)
21120 {
21121 CNode::ClearBanned();
21122 mapArgs["-banscore"] = "111"; // because 11 is my favorite number
21123 CAddress addr1(0xa0b0c001);
21124 CNode dummyNode1(INVALID_SOCKET, addr1, true);
21125 dummyNode1.Misbehaving(100);
21126 BOOST_CHECK(!CNode::IsBanned(addr1.ip));
21127 dummyNode1.Misbehaving(10);
21128 BOOST_CHECK(!CNode::IsBanned(addr1.ip));
21129 dummyNode1.Misbehaving(1);
21130 BOOST_CHECK(CNode::IsBanned(addr1.ip));
21131 mapArgs["-banscore"] = "100";
21132 }
21133
21134 BOOST_AUTO_TEST_CASE(DoS_bantime)
21135 {
21136 CNode::ClearBanned();
21137 int64 nStartTime = GetTime();
21138 SetMockTime(nStartTime); // Overrides future calls to GetTime()
21139
21140 CAddress addr(0xa0b0c001);
21141 CNode dummyNode(INVALID_SOCKET, addr, true);
21142
21143 dummyNode.Misbehaving(100);
21144 BOOST_CHECK(CNode::IsBanned(addr.ip));
21145
21146 SetMockTime(nStartTime+60*60);
21147 BOOST_CHECK(CNode::IsBanned(addr.ip));
21148
21149 SetMockTime(nStartTime+60*60*24+1);
21150 BOOST_CHECK(!CNode::IsBanned(addr.ip));
21151 }
21152
21153 static bool CheckNBits(unsigned int nbits1, int64 time1, unsigned int nbits2, int64 time2)
21154 {
21155 if (time1 > time2)
21156 return CheckNBits(nbits2, time2, nbits1, time1);
21157 int64 deltaTime = time2-time1;
21158
21159 CBigNum required;
21160 required.SetCompact(ComputeMinWork(nbits1, deltaTime));
21161 CBigNum have;
21162 have.SetCompact(nbits2);
21163 return (have <= required);
21164 }
21165
21166 BOOST_AUTO_TEST_CASE(DoS_checknbits)
21167 {
21168 using namespace boost::assign; // for 'map_list_of()'
21169
21170 // Timestamps,nBits from the bitcoin blockchain.
21171 // These are the block-chain checkpoint blocks
21172 typedef std::map<int64, unsigned int> BlockData;
21173 BlockData chainData =
21174 map_list_of(1239852051,486604799)(1262749024,486594666)
21175 (1279305360,469854461)(1280200847,469830746)(1281678674,469809688)
21176 (1296207707,453179945)(1302624061,453036989)(1309640330,437004818)
21177 (1313172719,436789733);
21178
21179 // Make sure CheckNBits considers every combination of block-chain-lock-in-points
21180 // "sane":
21181 BOOST_FOREACH(const BlockData::value_type& i, chainData)
21182 {
21183 BOOST_FOREACH(const BlockData::value_type& j, chainData)
21184 {
21185 BOOST_CHECK(CheckNBits(i.second, i.first, j.second, j.first));
21186 }
21187 }
21188
21189 // Test a couple of insane combinations:
21190 BlockData::value_type firstcheck = *(chainData.begin());
21191 BlockData::value_type lastcheck = *(chainData.rbegin());
21192
21193 // First checkpoint difficulty at or a while after the last checkpoint time should fail when
21194 // compared to last checkpoint
21195 BOOST_CHECK(!CheckNBits(firstcheck.second, lastcheck.first+60*10, lastcheck.second, lastcheck.first));
21196 BOOST_CHECK(!CheckNBits(firstcheck.second, lastcheck.first+60*60*24*14, lastcheck.second, lastcheck.first));
21197
21198 // ... but OK if enough time passed for difficulty to adjust downward:
21199 BOOST_CHECK(CheckNBits(firstcheck.second, lastcheck.first+60*60*24*365*4, lastcheck.second, lastcheck.first));
21200
21201 }
21202
21203 BOOST_AUTO_TEST_SUITE_END()