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>
15 BOOST_AUTO_TEST_SUITE(DoS_tests)
17 BOOST_AUTO_TEST_CASE(DoS_banning)
20 CAddress addr1(0xa0b0c001);
21 CNode dummyNode1(INVALID_SOCKET, addr1, true);
22 dummyNode1.Misbehaving(100); // Should get banned
23 BOOST_CHECK(CNode::IsBanned(addr1.ip));
24 BOOST_CHECK(!CNode::IsBanned(addr1.ip|0x0000ff00)); // Different ip, not banned
26 CAddress addr2(0xa0b0c002);
27 CNode dummyNode2(INVALID_SOCKET, addr2, true);
28 dummyNode2.Misbehaving(50);
29 BOOST_CHECK(!CNode::IsBanned(addr2.ip)); // 2 not banned yet...
30 BOOST_CHECK(CNode::IsBanned(addr1.ip)); // ... but 1 still should be
31 dummyNode2.Misbehaving(50);
32 BOOST_CHECK(CNode::IsBanned(addr2.ip));
35 BOOST_AUTO_TEST_CASE(DoS_banscore)
38 mapArgs["-banscore"] = "111"; // because 11 is my favorite number
39 CAddress addr1(0xa0b0c001);
40 CNode dummyNode1(INVALID_SOCKET, addr1, true);
41 dummyNode1.Misbehaving(100);
42 BOOST_CHECK(!CNode::IsBanned(addr1.ip));
43 dummyNode1.Misbehaving(10);
44 BOOST_CHECK(!CNode::IsBanned(addr1.ip));
45 dummyNode1.Misbehaving(1);
46 BOOST_CHECK(CNode::IsBanned(addr1.ip));
47 mapArgs["-banscore"] = "100";
50 BOOST_AUTO_TEST_CASE(DoS_bantime)
53 int64 nStartTime = GetTime();
54 SetMockTime(nStartTime); // Overrides future calls to GetTime()
56 CAddress addr(0xa0b0c001);
57 CNode dummyNode(INVALID_SOCKET, addr, true);
59 dummyNode.Misbehaving(100);
60 BOOST_CHECK(CNode::IsBanned(addr.ip));
62 SetMockTime(nStartTime+60*60);
63 BOOST_CHECK(CNode::IsBanned(addr.ip));
65 SetMockTime(nStartTime+60*60*24+1);
66 BOOST_CHECK(!CNode::IsBanned(addr.ip));
69 static bool CheckNBits(unsigned int nbits1, int64 time1, unsigned int nbits2, int64 time2)
72 return CheckNBits(nbits2, time2, nbits1, time1);
73 int64 deltaTime = time2-time1;
76 required.SetCompact(ComputeMinWork(nbits1, deltaTime));
78 have.SetCompact(nbits2);
79 return (have <= required);
82 BOOST_AUTO_TEST_CASE(DoS_checknbits)
84 using namespace boost::assign; // for 'map_list_of()'
86 // Timestamps,nBits from the bitcoin blockchain.
87 // These are the block-chain checkpoint blocks
88 typedef std::map<int64, unsigned int> BlockData;
90 map_list_of(1239852051,486604799)(1262749024,486594666)
91 (1279305360,469854461)(1280200847,469830746)(1281678674,469809688)
92 (1296207707,453179945)(1302624061,453036989)(1309640330,437004818)
93 (1313172719,436789733);
95 // Make sure CheckNBits considers every combination of block-chain-lock-in-points
97 BOOST_FOREACH(const BlockData::value_type& i, chainData)
99 BOOST_FOREACH(const BlockData::value_type& j, chainData)
101 BOOST_CHECK(CheckNBits(i.second, i.first, j.second, j.first));
105 // Test a couple of insane combinations:
106 BlockData::value_type firstcheck = *(chainData.begin());
107 BlockData::value_type lastcheck = *(chainData.rbegin());
109 // First checkpoint difficulty at or a while after the last checkpoint time should fail when
110 // compared to last checkpoint
111 BOOST_CHECK(!CheckNBits(firstcheck.second, lastcheck.first+60*10, lastcheck.second, lastcheck.first));
112 BOOST_CHECK(!CheckNBits(firstcheck.second, lastcheck.first+60*60*24*14, lastcheck.second, lastcheck.first));
114 // ... but OK if enough time passed for difficulty to adjust downward:
115 BOOST_CHECK(CheckNBits(firstcheck.second, lastcheck.first+60*60*24*365*4, lastcheck.second, lastcheck.first));
119 BOOST_AUTO_TEST_SUITE_END()