2 // Unit tests for denial-of-service detection/prevention code
4 #include <boost/assign/list_of.hpp> // for 'map_list_of()'
5 #include <boost/test/unit_test.hpp>
6 #include <boost/foreach.hpp>
14 BOOST_AUTO_TEST_SUITE(DoS_tests)
16 BOOST_AUTO_TEST_CASE(DoS_banning)
19 CAddress addr1(0xa0b0c001);
20 CNode dummyNode1(INVALID_SOCKET, addr1, true);
21 dummyNode1.Misbehaving(100); // Should get banned
22 BOOST_CHECK(CNode::IsBanned(addr1.ip));
23 BOOST_CHECK(!CNode::IsBanned(addr1.ip|0x0000ff00)); // Different ip, not banned
25 CAddress addr2(0xa0b0c002);
26 CNode dummyNode2(INVALID_SOCKET, addr2, true);
27 dummyNode2.Misbehaving(50);
28 BOOST_CHECK(!CNode::IsBanned(addr2.ip)); // 2 not banned yet...
29 BOOST_CHECK(CNode::IsBanned(addr1.ip)); // ... but 1 still should be
30 dummyNode2.Misbehaving(50);
31 BOOST_CHECK(CNode::IsBanned(addr2.ip));
34 BOOST_AUTO_TEST_CASE(DoS_banscore)
37 mapArgs["-banscore"] = "111"; // because 11 is my favorite number
38 CAddress addr1(0xa0b0c001);
39 CNode dummyNode1(INVALID_SOCKET, addr1, true);
40 dummyNode1.Misbehaving(100);
41 BOOST_CHECK(!CNode::IsBanned(addr1.ip));
42 dummyNode1.Misbehaving(10);
43 BOOST_CHECK(!CNode::IsBanned(addr1.ip));
44 dummyNode1.Misbehaving(1);
45 BOOST_CHECK(CNode::IsBanned(addr1.ip));
46 mapArgs["-banscore"] = "100";
49 BOOST_AUTO_TEST_CASE(DoS_bantime)
52 int64 nStartTime = GetTime();
53 SetMockTime(nStartTime); // Overrides future calls to GetTime()
55 CAddress addr(0xa0b0c001);
56 CNode dummyNode(INVALID_SOCKET, addr, true);
58 dummyNode.Misbehaving(100);
59 BOOST_CHECK(CNode::IsBanned(addr.ip));
61 SetMockTime(nStartTime+60*60);
62 BOOST_CHECK(CNode::IsBanned(addr.ip));
64 SetMockTime(nStartTime+60*60*24+1);
65 BOOST_CHECK(!CNode::IsBanned(addr.ip));
68 static bool CheckNBits(unsigned int nbits1, int64 time1, unsigned int nbits2, int64 time2)
71 return CheckNBits(nbits2, time2, nbits1, time1);
72 int64 deltaTime = time2-time1;
75 required.SetCompact(ComputeMinWork(nbits1, deltaTime));
77 have.SetCompact(nbits2);
78 return (have <= required);
81 BOOST_AUTO_TEST_CASE(DoS_checknbits)
83 using namespace boost::assign; // for 'map_list_of()'
85 // Timestamps,nBits from the bitcoin blockchain.
86 // These are the block-chain checkpoint blocks
87 typedef std::map<int64, unsigned int> BlockData;
89 map_list_of(1239852051,486604799)(1262749024,486594666)
90 (1279305360,469854461)(1280200847,469830746)(1281678674,469809688)
91 (1296207707,453179945)(1302624061,453036989)(1309640330,437004818)
92 (1313172719,436789733);
94 // Make sure CheckNBits considers every combination of block-chain-lock-in-points
96 BOOST_FOREACH(const BlockData::value_type& i, chainData)
98 BOOST_FOREACH(const BlockData::value_type& j, chainData)
100 BOOST_CHECK(CheckNBits(i.second, i.first, j.second, j.first));
104 // Test a couple of insane combinations:
105 BlockData::value_type firstcheck = *(chainData.begin());
106 BlockData::value_type lastcheck = *(chainData.rbegin());
108 // First checkpoint difficulty at or a while after the last checkpoint time should fail when
109 // compared to last checkpoint
110 BOOST_CHECK(!CheckNBits(firstcheck.second, lastcheck.first+60*10, lastcheck.second, lastcheck.first));
111 BOOST_CHECK(!CheckNBits(firstcheck.second, lastcheck.first+60*60*24*14, lastcheck.second, lastcheck.first));
113 // ... but OK if enough time passed for difficulty to adjust downward:
114 BOOST_CHECK(CheckNBits(firstcheck.second, lastcheck.first+60*60*24*365*4, lastcheck.second, lastcheck.first));
118 BOOST_AUTO_TEST_SUITE_END()