Back to home page

Bitcoin sources

 
 

    


0001 //
0002 // Unit tests for denial-of-service detection/prevention code
0003 //
0004 #include <boost/assign/list_of.hpp> // for 'map_list_of()'
0005 #include <boost/test/unit_test.hpp>
0006 #include <boost/foreach.hpp>
0007 
0008 #include "../main.h"
0009 #include "../net.h"
0010 #include "../util.h"
0011 
0012 using namespace std;
0013 
0014 BOOST_AUTO_TEST_SUITE(DoS_tests)
0015 
0016 BOOST_AUTO_TEST_CASE(DoS_banning)
0017 {
0018     CNode::ClearBanned();
0019     CAddress addr1(0xa0b0c001);
0020     CNode dummyNode1(INVALID_SOCKET, addr1, true);
0021     dummyNode1.Misbehaving(100); // Should get banned
0022     BOOST_CHECK(CNode::IsBanned(addr1.ip));
0023     BOOST_CHECK(!CNode::IsBanned(addr1.ip|0x0000ff00)); // Different ip, not banned
0024 
0025     CAddress addr2(0xa0b0c002);
0026     CNode dummyNode2(INVALID_SOCKET, addr2, true);
0027     dummyNode2.Misbehaving(50);
0028     BOOST_CHECK(!CNode::IsBanned(addr2.ip)); // 2 not banned yet...
0029     BOOST_CHECK(CNode::IsBanned(addr1.ip));  // ... but 1 still should be
0030     dummyNode2.Misbehaving(50);
0031     BOOST_CHECK(CNode::IsBanned(addr2.ip));
0032 }    
0033 
0034 BOOST_AUTO_TEST_CASE(DoS_banscore)
0035 {
0036     CNode::ClearBanned();
0037     mapArgs["-banscore"] = "111"; // because 11 is my favorite number
0038     CAddress addr1(0xa0b0c001);
0039     CNode dummyNode1(INVALID_SOCKET, addr1, true);
0040     dummyNode1.Misbehaving(100);
0041     BOOST_CHECK(!CNode::IsBanned(addr1.ip));
0042     dummyNode1.Misbehaving(10);
0043     BOOST_CHECK(!CNode::IsBanned(addr1.ip));
0044     dummyNode1.Misbehaving(1);
0045     BOOST_CHECK(CNode::IsBanned(addr1.ip));
0046     mapArgs["-banscore"] = "100";
0047 }
0048 
0049 BOOST_AUTO_TEST_CASE(DoS_bantime)
0050 {
0051     CNode::ClearBanned();
0052     int64 nStartTime = GetTime();
0053     SetMockTime(nStartTime); // Overrides future calls to GetTime()
0054 
0055     CAddress addr(0xa0b0c001);
0056     CNode dummyNode(INVALID_SOCKET, addr, true);
0057 
0058     dummyNode.Misbehaving(100);
0059     BOOST_CHECK(CNode::IsBanned(addr.ip));
0060 
0061     SetMockTime(nStartTime+60*60);
0062     BOOST_CHECK(CNode::IsBanned(addr.ip));
0063 
0064     SetMockTime(nStartTime+60*60*24+1);
0065     BOOST_CHECK(!CNode::IsBanned(addr.ip));
0066 }    
0067 
0068 static bool CheckNBits(unsigned int nbits1, int64 time1, unsigned int nbits2, int64 time2)
0069 {
0070     if (time1 > time2)
0071         return CheckNBits(nbits2, time2, nbits1, time1);
0072     int64 deltaTime = time2-time1;
0073 
0074     CBigNum required;
0075     required.SetCompact(ComputeMinWork(nbits1, deltaTime));
0076     CBigNum have;
0077     have.SetCompact(nbits2);
0078     return (have <= required);
0079 }
0080 
0081 BOOST_AUTO_TEST_CASE(DoS_checknbits)
0082 {
0083     using namespace boost::assign; // for 'map_list_of()'
0084 
0085     // Timestamps,nBits from the bitcoin blockchain.
0086     // These are the block-chain checkpoint blocks
0087     typedef std::map<int64, unsigned int> BlockData;
0088     BlockData chainData =
0089         map_list_of(1239852051,486604799)(1262749024,486594666)
0090         (1279305360,469854461)(1280200847,469830746)(1281678674,469809688)
0091         (1296207707,453179945)(1302624061,453036989)(1309640330,437004818)
0092         (1313172719,436789733);
0093 
0094     // Make sure CheckNBits considers every combination of block-chain-lock-in-points
0095     // "sane":
0096     BOOST_FOREACH(const BlockData::value_type& i, chainData)
0097     {
0098         BOOST_FOREACH(const BlockData::value_type& j, chainData)
0099         {
0100             BOOST_CHECK(CheckNBits(i.second, i.first, j.second, j.first));
0101         }
0102     }
0103 
0104     // Test a couple of insane combinations:
0105     BlockData::value_type firstcheck = *(chainData.begin());
0106     BlockData::value_type lastcheck = *(chainData.rbegin());
0107 
0108     // First checkpoint difficulty at or a while after the last checkpoint time should fail when
0109     // compared to last checkpoint
0110     BOOST_CHECK(!CheckNBits(firstcheck.second, lastcheck.first+60*10, lastcheck.second, lastcheck.first));
0111     BOOST_CHECK(!CheckNBits(firstcheck.second, lastcheck.first+60*60*24*14, lastcheck.second, lastcheck.first));
0112 
0113     // ... but OK if enough time passed for difficulty to adjust downward:
0114     BOOST_CHECK(CheckNBits(firstcheck.second, lastcheck.first+60*60*24*365*4, lastcheck.second, lastcheck.first));
0115     
0116 }
0117 
0118 BOOST_AUTO_TEST_SUITE_END()