Projects : bitcoin : bitcoin_dumpblock_no_losers
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 "../net.h" |
10 | #include "../util.h" |
11 | |
12 | using namespace std; |
13 | |
14 | BOOST_AUTO_TEST_SUITE(DoS_tests) |
15 | |
16 | BOOST_AUTO_TEST_CASE(DoS_banning) |
17 | { |
18 | CNode::ClearBanned(); |
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 |
24 | |
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)); |
32 | } |
33 | |
34 | BOOST_AUTO_TEST_CASE(DoS_banscore) |
35 | { |
36 | CNode::ClearBanned(); |
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"; |
47 | } |
48 | |
49 | BOOST_AUTO_TEST_CASE(DoS_bantime) |
50 | { |
51 | CNode::ClearBanned(); |
52 | int64 nStartTime = GetTime(); |
53 | SetMockTime(nStartTime); // Overrides future calls to GetTime() |
54 | |
55 | CAddress addr(0xa0b0c001); |
56 | CNode dummyNode(INVALID_SOCKET, addr, true); |
57 | |
58 | dummyNode.Misbehaving(100); |
59 | BOOST_CHECK(CNode::IsBanned(addr.ip)); |
60 | |
61 | SetMockTime(nStartTime+60*60); |
62 | BOOST_CHECK(CNode::IsBanned(addr.ip)); |
63 | |
64 | SetMockTime(nStartTime+60*60*24+1); |
65 | BOOST_CHECK(!CNode::IsBanned(addr.ip)); |
66 | } |
67 | |
68 | static bool CheckNBits(unsigned int nbits1, int64 time1, unsigned int nbits2, int64 time2) |
69 | { |
70 | if (time1 > time2) |
71 | return CheckNBits(nbits2, time2, nbits1, time1); |
72 | int64 deltaTime = time2-time1; |
73 | |
74 | CBigNum required; |
75 | required.SetCompact(ComputeMinWork(nbits1, deltaTime)); |
76 | CBigNum have; |
77 | have.SetCompact(nbits2); |
78 | return (have <= required); |
79 | } |
80 | |
81 | BOOST_AUTO_TEST_CASE(DoS_checknbits) |
82 | { |
83 | using namespace boost::assign; // for 'map_list_of()' |
84 | |
85 | // Timestamps,nBits from the bitcoin blockchain. |
86 | // These are the block-chain checkpoint blocks |
87 | typedef std::map<int64, unsigned int> BlockData; |
88 | BlockData chainData = |
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); |
93 | |
94 | // Make sure CheckNBits considers every combination of block-chain-lock-in-points |
95 | // "sane": |
96 | BOOST_FOREACH(const BlockData::value_type& i, chainData) |
97 | { |
98 | BOOST_FOREACH(const BlockData::value_type& j, chainData) |
99 | { |
100 | BOOST_CHECK(CheckNBits(i.second, i.first, j.second, j.first)); |
101 | } |
102 | } |
103 | |
104 | // Test a couple of insane combinations: |
105 | BlockData::value_type firstcheck = *(chainData.begin()); |
106 | BlockData::value_type lastcheck = *(chainData.rbegin()); |
107 | |
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)); |
112 | |
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)); |
115 | |
116 | } |
117 | |
118 | BOOST_AUTO_TEST_SUITE_END() |