-
+ 79E2727E67312D283B32233C52EDC2A234D6F7D53AE33BBD47E49E9B058593C94BE45C9A25A03F98C0BE2A217F54DA211720EA2C43FB51A160906738E8E9EEFA
bitcoin/src/test/DoS_tests.cpp
(0 . 0)(1 . 149)
22698 // /****************************\
22699 // * EXPERIMENTAL BRANCH. *
22700 // * FOR LABORATORY USE ONLY. *
22701 // ********************************
22702 // ************
22703 // **************
22704 // ****************
22705 // **** **** ****
22706 // *** *** ***
22707 // *** *** ***
22708 // *** * * **
22709 // ******** ********
22710 // ******* ******
22711 // *** **
22712 // * ******* **
22713 // ** * * * * *
22714 // ** * * ***
22715 // **** * * * * ****
22716 // **** *** * * ** ***
22717 // **** ********* ******
22718 // ******* ***** *******
22719 // ********* ****** **
22720 // ** ****** ******
22721 // ** ******* **
22722 // ** ******* ***
22723 // **** ******** ************
22724 // ************ ************
22725 // ******** *******
22726 // ****** ****
22727 // *** ***
22728 // ********************************
22729 //
22730 // Unit tests for denial-of-service detection/prevention code
22731 //
22732 #include <boost/assign/list_of.hpp> // for 'map_list_of()'
22733 #include <boost/test/unit_test.hpp>
22734 #include <boost/foreach.hpp>
22735
22736 #include "../main.h"
22737 #include "../net.h"
22738 #include "../util.h"
22739
22740 using namespace std;
22741
22742 BOOST_AUTO_TEST_SUITE(DoS_tests)
22743
22744 BOOST_AUTO_TEST_CASE(DoS_banning)
22745 {
22746 CNode::ClearBanned();
22747 CAddress addr1(0xa0b0c001);
22748 CNode dummyNode1(INVALID_SOCKET, addr1, true);
22749 dummyNode1.Misbehaving(100); // Should get banned
22750 BOOST_CHECK(CNode::IsBanned(addr1.ip));
22751 BOOST_CHECK(!CNode::IsBanned(addr1.ip|0x0000ff00)); // Different ip, not banned
22752
22753 CAddress addr2(0xa0b0c002);
22754 CNode dummyNode2(INVALID_SOCKET, addr2, true);
22755 dummyNode2.Misbehaving(50);
22756 BOOST_CHECK(!CNode::IsBanned(addr2.ip)); // 2 not banned yet...
22757 BOOST_CHECK(CNode::IsBanned(addr1.ip)); // ... but 1 still should be
22758 dummyNode2.Misbehaving(50);
22759 BOOST_CHECK(CNode::IsBanned(addr2.ip));
22760 }
22761
22762 BOOST_AUTO_TEST_CASE(DoS_banscore)
22763 {
22764 CNode::ClearBanned();
22765 mapArgs["-banscore"] = "111"; // because 11 is my favorite number
22766 CAddress addr1(0xa0b0c001);
22767 CNode dummyNode1(INVALID_SOCKET, addr1, true);
22768 dummyNode1.Misbehaving(100);
22769 BOOST_CHECK(!CNode::IsBanned(addr1.ip));
22770 dummyNode1.Misbehaving(10);
22771 BOOST_CHECK(!CNode::IsBanned(addr1.ip));
22772 dummyNode1.Misbehaving(1);
22773 BOOST_CHECK(CNode::IsBanned(addr1.ip));
22774 mapArgs["-banscore"] = "100";
22775 }
22776
22777 BOOST_AUTO_TEST_CASE(DoS_bantime)
22778 {
22779 CNode::ClearBanned();
22780 int64 nStartTime = GetTime();
22781 SetMockTime(nStartTime); // Overrides future calls to GetTime()
22782
22783 CAddress addr(0xa0b0c001);
22784 CNode dummyNode(INVALID_SOCKET, addr, true);
22785
22786 dummyNode.Misbehaving(100);
22787 BOOST_CHECK(CNode::IsBanned(addr.ip));
22788
22789 SetMockTime(nStartTime+60*60);
22790 BOOST_CHECK(CNode::IsBanned(addr.ip));
22791
22792 SetMockTime(nStartTime+60*60*24+1);
22793 BOOST_CHECK(!CNode::IsBanned(addr.ip));
22794 }
22795
22796 static bool CheckNBits(unsigned int nbits1, int64 time1, unsigned int nbits2, int64 time2)
22797 {
22798 if (time1 > time2)
22799 return CheckNBits(nbits2, time2, nbits1, time1);
22800 int64 deltaTime = time2-time1;
22801
22802 CBigNum required;
22803 required.SetCompact(ComputeMinWork(nbits1, deltaTime));
22804 CBigNum have;
22805 have.SetCompact(nbits2);
22806 return (have <= required);
22807 }
22808
22809 BOOST_AUTO_TEST_CASE(DoS_checknbits)
22810 {
22811 using namespace boost::assign; // for 'map_list_of()'
22812
22813 // Timestamps,nBits from the bitcoin blockchain.
22814 // These are the block-chain checkpoint blocks
22815 typedef std::map<int64, unsigned int> BlockData;
22816 BlockData chainData =
22817 map_list_of(1239852051,486604799)(1262749024,486594666)
22818 (1279305360,469854461)(1280200847,469830746)(1281678674,469809688)
22819 (1296207707,453179945)(1302624061,453036989)(1309640330,437004818)
22820 (1313172719,436789733);
22821
22822 // Make sure CheckNBits considers every combination of block-chain-lock-in-points
22823 // "sane":
22824 BOOST_FOREACH(const BlockData::value_type& i, chainData)
22825 {
22826 BOOST_FOREACH(const BlockData::value_type& j, chainData)
22827 {
22828 BOOST_CHECK(CheckNBits(i.second, i.first, j.second, j.first));
22829 }
22830 }
22831
22832 // Test a couple of insane combinations:
22833 BlockData::value_type firstcheck = *(chainData.begin());
22834 BlockData::value_type lastcheck = *(chainData.rbegin());
22835
22836 // First checkpoint difficulty at or a while after the last checkpoint time should fail when
22837 // compared to last checkpoint
22838 BOOST_CHECK(!CheckNBits(firstcheck.second, lastcheck.first+60*10, lastcheck.second, lastcheck.first));
22839 BOOST_CHECK(!CheckNBits(firstcheck.second, lastcheck.first+60*60*24*14, lastcheck.second, lastcheck.first));
22840
22841 // ... but OK if enough time passed for difficulty to adjust downward:
22842 BOOST_CHECK(CheckNBits(firstcheck.second, lastcheck.first+60*60*24*365*4, lastcheck.second, lastcheck.first));
22843
22844 }
22845
22846 BOOST_AUTO_TEST_SUITE_END()