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