You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DoS_tests.cpp 3.9KB

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