timedata_tests.cpp
1 // Copyright (c) 2011-2021 The Bitcoin Core developers 2 // Distributed under the MIT software license, see the accompanying 3 // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 // 5 6 #include <netaddress.h> 7 #include <noui.h> 8 #include <test/util/logging.h> 9 #include <test/util/setup_common.h> 10 #include <timedata.h> 11 #include <util/string.h> 12 #include <util/translation.h> 13 #include <warnings.h> 14 15 #include <string> 16 17 #include <boost/test/unit_test.hpp> 18 19 BOOST_FIXTURE_TEST_SUITE(timedata_tests, BasicTestingSetup) 20 21 BOOST_AUTO_TEST_CASE(util_MedianFilter) 22 { 23 CMedianFilter<int> filter(5, 15); 24 25 BOOST_CHECK_EQUAL(filter.median(), 15); 26 27 filter.input(20); // [15 20] 28 BOOST_CHECK_EQUAL(filter.median(), 17); 29 30 filter.input(30); // [15 20 30] 31 BOOST_CHECK_EQUAL(filter.median(), 20); 32 33 filter.input(3); // [3 15 20 30] 34 BOOST_CHECK_EQUAL(filter.median(), 17); 35 36 filter.input(7); // [3 7 15 20 30] 37 BOOST_CHECK_EQUAL(filter.median(), 15); 38 39 filter.input(18); // [3 7 18 20 30] 40 BOOST_CHECK_EQUAL(filter.median(), 18); 41 42 filter.input(0); // [0 3 7 18 30] 43 BOOST_CHECK_EQUAL(filter.median(), 7); 44 } 45 46 static void MultiAddTimeData(int n, int64_t offset) 47 { 48 static int cnt = 0; 49 for (int i = 0; i < n; ++i) { 50 CNetAddr addr; 51 addr.SetInternal(ToString(++cnt)); 52 AddTimeData(addr, offset); 53 } 54 } 55 56 57 BOOST_AUTO_TEST_CASE(addtimedata) 58 { 59 BOOST_CHECK_EQUAL(GetTimeOffset(), 0); 60 61 //Part 1: Add large offsets to test a warning message that our clock may be wrong. 62 MultiAddTimeData(3, DEFAULT_MAX_TIME_ADJUSTMENT + 1); 63 // Filter size is 1 + 3 = 4: It is always initialized with a single element (offset 0) 64 65 { 66 ASSERT_DEBUG_LOG("Please check that your computer's date and time are correct!"); 67 MultiAddTimeData(1, DEFAULT_MAX_TIME_ADJUSTMENT + 1); //filter size 5 68 } 69 70 BOOST_CHECK(GetWarnings(true).original.find("clock is wrong") != std::string::npos); 71 72 // nTimeOffset is not changed if the median of offsets exceeds DEFAULT_MAX_TIME_ADJUSTMENT 73 BOOST_CHECK_EQUAL(GetTimeOffset(), 0); 74 75 // Part 2: Test positive and negative medians by adding more offsets 76 MultiAddTimeData(4, 100); // filter size 9 77 BOOST_CHECK_EQUAL(GetTimeOffset(), 100); 78 MultiAddTimeData(10, -100); //filter size 19 79 BOOST_CHECK_EQUAL(GetTimeOffset(), -100); 80 81 // Part 3: Test behaviour when filter has reached maximum number of offsets 82 const int MAX_SAMPLES = 200; 83 int nfill = (MAX_SAMPLES - 3 - 19) / 2; //89 84 MultiAddTimeData(nfill, 100); 85 MultiAddTimeData(nfill, -100); //filter size MAX_SAMPLES - 3 86 BOOST_CHECK_EQUAL(GetTimeOffset(), -100); 87 88 MultiAddTimeData(2, 100); 89 //filter size MAX_SAMPLES -1, median is the initial 0 offset 90 //since we added same number of positive/negative offsets 91 92 BOOST_CHECK_EQUAL(GetTimeOffset(), 0); 93 94 // After the number of offsets has reached MAX_SAMPLES -1 (=199), nTimeOffset will never change 95 // because it is only updated when the number of elements in the filter becomes odd. It was decided 96 // not to fix this because it prevents possible attacks. See the comment in AddTimeData() or issue #4521 97 // for a more detailed explanation. 98 MultiAddTimeData(2, 100); // filter median is 100 now, but nTimeOffset will not change 99 // We want this test to end with nTimeOffset==0, otherwise subsequent tests of the suite will fail. 100 BOOST_CHECK_EQUAL(GetTimeOffset(), 0); 101 102 TestOnlyResetTimeData(); 103 } 104 105 BOOST_AUTO_TEST_SUITE_END()