Revert "Use standard C99 (and Qt) types for 64-bit integers"
[novacoin.git] / src / test / DoS_tests.cpp
1 //
2 // Unit tests for denial-of-service detection/prevention code
3 //
4 #include <boost/assign/list_of.hpp> // for 'map_list_of()'
5 #include <boost/test/unit_test.hpp>
6 #include <boost/foreach.hpp>
7
8 #include "main.h"
9 #include "wallet.h"
10 #include "net.h"
11 #include "util.h"
12
13 using namespace std;
14
15 BOOST_AUTO_TEST_SUITE(DoS_tests)
16
17 BOOST_AUTO_TEST_CASE(DoS_banning)
18 {
19     CNode::ClearBanned();
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
25
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));
33 }    
34
35 BOOST_AUTO_TEST_CASE(DoS_banscore)
36 {
37     CNode::ClearBanned();
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";
48 }
49
50 BOOST_AUTO_TEST_CASE(DoS_bantime)
51 {
52     CNode::ClearBanned();
53     int64 nStartTime = GetTime();
54     SetMockTime(nStartTime); // Overrides future calls to GetTime()
55
56     CAddress addr(0xa0b0c001);
57     CNode dummyNode(INVALID_SOCKET, addr, true);
58
59     dummyNode.Misbehaving(100);
60     BOOST_CHECK(CNode::IsBanned(addr.ip));
61
62     SetMockTime(nStartTime+60*60);
63     BOOST_CHECK(CNode::IsBanned(addr.ip));
64
65     SetMockTime(nStartTime+60*60*24+1);
66     BOOST_CHECK(!CNode::IsBanned(addr.ip));
67 }    
68
69 static bool CheckNBits(unsigned int nbits1, int64 time1, unsigned int nbits2, int64 time2)
70 {
71     if (time1 > time2)
72         return CheckNBits(nbits2, time2, nbits1, time1);
73     int64 deltaTime = time2-time1;
74
75     CBigNum required;
76     required.SetCompact(ComputeMinWork(nbits1, deltaTime));
77     CBigNum have;
78     have.SetCompact(nbits2);
79     return (have <= required);
80 }
81
82 BOOST_AUTO_TEST_CASE(DoS_checknbits)
83 {
84     using namespace boost::assign; // for 'map_list_of()'
85
86     // Timestamps,nBits from the bitcoin blockchain.
87     // These are the block-chain checkpoint blocks
88     typedef std::map<int64, unsigned int> BlockData;
89     BlockData chainData =
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);
94
95     // Make sure CheckNBits considers every combination of block-chain-lock-in-points
96     // "sane":
97     BOOST_FOREACH(const BlockData::value_type& i, chainData)
98     {
99         BOOST_FOREACH(const BlockData::value_type& j, chainData)
100         {
101             BOOST_CHECK(CheckNBits(i.second, i.first, j.second, j.first));
102         }
103     }
104
105     // Test a couple of insane combinations:
106     BlockData::value_type firstcheck = *(chainData.begin());
107     BlockData::value_type lastcheck = *(chainData.rbegin());
108
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));
113
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));
116     
117 }
118
119 BOOST_AUTO_TEST_SUITE_END()