raw
experimental-genesis    1 //  /****************************\
experimental-genesis 2 // * EXPERIMENTAL BRANCH. *
experimental-genesis 3 // * FOR LABORATORY USE ONLY. *
experimental-genesis 4 // ********************************
experimental-genesis 5 // ************
experimental-genesis 6 // **************
experimental-genesis 7 // ****************
experimental-genesis 8 // **** **** ****
experimental-genesis 9 // *** *** ***
experimental-genesis 10 // *** *** ***
experimental-genesis 11 // *** * * **
experimental-genesis 12 // ******** ********
experimental-genesis 13 // ******* ******
experimental-genesis 14 // *** **
experimental-genesis 15 // * ******* **
experimental-genesis 16 // ** * * * * *
experimental-genesis 17 // ** * * ***
experimental-genesis 18 // **** * * * * ****
experimental-genesis 19 // **** *** * * ** ***
experimental-genesis 20 // **** ********* ******
experimental-genesis 21 // ******* ***** *******
experimental-genesis 22 // ********* ****** **
experimental-genesis 23 // ** ****** ******
experimental-genesis 24 // ** ******* **
experimental-genesis 25 // ** ******* ***
experimental-genesis 26 // **** ******** ************
experimental-genesis 27 // ************ ************
experimental-genesis 28 // ******** *******
experimental-genesis 29 // ****** ****
experimental-genesis 30 // *** ***
experimental-genesis 31 // ********************************
experimental-genesis 32 //
experimental-genesis 33 // Unit tests for denial-of-service detection/prevention code
experimental-genesis 34 //
experimental-genesis 35 #include <boost/assign/list_of.hpp> // for 'map_list_of()'
experimental-genesis 36 #include <boost/test/unit_test.hpp>
experimental-genesis 37 #include <boost/foreach.hpp>
experimental-genesis 38
experimental-genesis 39 #include "../main.h"
experimental-genesis 40 #include "../net.h"
experimental-genesis 41 #include "../util.h"
experimental-genesis 42
experimental-genesis 43 using namespace std;
experimental-genesis 44
experimental-genesis 45 BOOST_AUTO_TEST_SUITE(DoS_tests)
experimental-genesis 46
experimental-genesis 47 BOOST_AUTO_TEST_CASE(DoS_banning)
experimental-genesis 48 {
experimental-genesis 49 CNode::ClearBanned();
experimental-genesis 50 CAddress addr1(0xa0b0c001);
experimental-genesis 51 CNode dummyNode1(INVALID_SOCKET, addr1, true);
experimental-genesis 52 dummyNode1.Misbehaving(100); // Should get banned
experimental-genesis 53 BOOST_CHECK(CNode::IsBanned(addr1.ip));
experimental-genesis 54 BOOST_CHECK(!CNode::IsBanned(addr1.ip|0x0000ff00)); // Different ip, not banned
experimental-genesis 55
experimental-genesis 56 CAddress addr2(0xa0b0c002);
experimental-genesis 57 CNode dummyNode2(INVALID_SOCKET, addr2, true);
experimental-genesis 58 dummyNode2.Misbehaving(50);
experimental-genesis 59 BOOST_CHECK(!CNode::IsBanned(addr2.ip)); // 2 not banned yet...
experimental-genesis 60 BOOST_CHECK(CNode::IsBanned(addr1.ip)); // ... but 1 still should be
experimental-genesis 61 dummyNode2.Misbehaving(50);
experimental-genesis 62 BOOST_CHECK(CNode::IsBanned(addr2.ip));
experimental-genesis 63 }
experimental-genesis 64
experimental-genesis 65 BOOST_AUTO_TEST_CASE(DoS_banscore)
experimental-genesis 66 {
experimental-genesis 67 CNode::ClearBanned();
experimental-genesis 68 mapArgs["-banscore"] = "111"; // because 11 is my favorite number
experimental-genesis 69 CAddress addr1(0xa0b0c001);
experimental-genesis 70 CNode dummyNode1(INVALID_SOCKET, addr1, true);
experimental-genesis 71 dummyNode1.Misbehaving(100);
experimental-genesis 72 BOOST_CHECK(!CNode::IsBanned(addr1.ip));
experimental-genesis 73 dummyNode1.Misbehaving(10);
experimental-genesis 74 BOOST_CHECK(!CNode::IsBanned(addr1.ip));
experimental-genesis 75 dummyNode1.Misbehaving(1);
experimental-genesis 76 BOOST_CHECK(CNode::IsBanned(addr1.ip));
experimental-genesis 77 mapArgs["-banscore"] = "100";
experimental-genesis 78 }
experimental-genesis 79
experimental-genesis 80 BOOST_AUTO_TEST_CASE(DoS_bantime)
experimental-genesis 81 {
experimental-genesis 82 CNode::ClearBanned();
experimental-genesis 83 int64 nStartTime = GetTime();
experimental-genesis 84 SetMockTime(nStartTime); // Overrides future calls to GetTime()
experimental-genesis 85
experimental-genesis 86 CAddress addr(0xa0b0c001);
experimental-genesis 87 CNode dummyNode(INVALID_SOCKET, addr, true);
experimental-genesis 88
experimental-genesis 89 dummyNode.Misbehaving(100);
experimental-genesis 90 BOOST_CHECK(CNode::IsBanned(addr.ip));
experimental-genesis 91
experimental-genesis 92 SetMockTime(nStartTime+60*60);
experimental-genesis 93 BOOST_CHECK(CNode::IsBanned(addr.ip));
experimental-genesis 94
experimental-genesis 95 SetMockTime(nStartTime+60*60*24+1);
experimental-genesis 96 BOOST_CHECK(!CNode::IsBanned(addr.ip));
experimental-genesis 97 }
experimental-genesis 98
experimental-genesis 99 static bool CheckNBits(unsigned int nbits1, int64 time1, unsigned int nbits2, int64 time2)
experimental-genesis 100 {
experimental-genesis 101 if (time1 > time2)
experimental-genesis 102 return CheckNBits(nbits2, time2, nbits1, time1);
experimental-genesis 103 int64 deltaTime = time2-time1;
experimental-genesis 104
experimental-genesis 105 CBigNum required;
experimental-genesis 106 required.SetCompact(ComputeMinWork(nbits1, deltaTime));
experimental-genesis 107 CBigNum have;
experimental-genesis 108 have.SetCompact(nbits2);
experimental-genesis 109 return (have <= required);
experimental-genesis 110 }
experimental-genesis 111
experimental-genesis 112 BOOST_AUTO_TEST_CASE(DoS_checknbits)
experimental-genesis 113 {
experimental-genesis 114 using namespace boost::assign; // for 'map_list_of()'
experimental-genesis 115
experimental-genesis 116 // Timestamps,nBits from the bitcoin blockchain.
experimental-genesis 117 // These are the block-chain checkpoint blocks
experimental-genesis 118 typedef std::map<int64, unsigned int> BlockData;
experimental-genesis 119 BlockData chainData =
experimental-genesis 120 map_list_of(1239852051,486604799)(1262749024,486594666)
experimental-genesis 121 (1279305360,469854461)(1280200847,469830746)(1281678674,469809688)
experimental-genesis 122 (1296207707,453179945)(1302624061,453036989)(1309640330,437004818)
experimental-genesis 123 (1313172719,436789733);
experimental-genesis 124
experimental-genesis 125 // Make sure CheckNBits considers every combination of block-chain-lock-in-points
experimental-genesis 126 // "sane":
experimental-genesis 127 BOOST_FOREACH(const BlockData::value_type& i, chainData)
experimental-genesis 128 {
experimental-genesis 129 BOOST_FOREACH(const BlockData::value_type& j, chainData)
experimental-genesis 130 {
experimental-genesis 131 BOOST_CHECK(CheckNBits(i.second, i.first, j.second, j.first));
experimental-genesis 132 }
experimental-genesis 133 }
experimental-genesis 134
experimental-genesis 135 // Test a couple of insane combinations:
experimental-genesis 136 BlockData::value_type firstcheck = *(chainData.begin());
experimental-genesis 137 BlockData::value_type lastcheck = *(chainData.rbegin());
experimental-genesis 138
experimental-genesis 139 // First checkpoint difficulty at or a while after the last checkpoint time should fail when
experimental-genesis 140 // compared to last checkpoint
experimental-genesis 141 BOOST_CHECK(!CheckNBits(firstcheck.second, lastcheck.first+60*10, lastcheck.second, lastcheck.first));
experimental-genesis 142 BOOST_CHECK(!CheckNBits(firstcheck.second, lastcheck.first+60*60*24*14, lastcheck.second, lastcheck.first));
experimental-genesis 143
experimental-genesis 144 // ... but OK if enough time passed for difficulty to adjust downward:
experimental-genesis 145 BOOST_CHECK(CheckNBits(firstcheck.second, lastcheck.first+60*60*24*365*4, lastcheck.second, lastcheck.first));
experimental-genesis 146
experimental-genesis 147 }
experimental-genesis 148
experimental-genesis 149 BOOST_AUTO_TEST_SUITE_END()