Bitcoin Core  27.99.0
P2P Digital Currency
timedata_tests.cpp
Go to the documentation of this file.
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>
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 
58 {
60 
61  //Part 1: Add large offsets to test a warning message that our clock may be wrong.
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
74 
75  // Part 2: Test positive and negative medians by adding more offsets
76  MultiAddTimeData(4, 100); // filter size 9
78  MultiAddTimeData(10, -100); //filter size 19
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
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 
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.
101 
103 }
104 
Median filter over a stream of values.
Definition: timedata.h:23
T median() const
Definition: timedata.h:49
void input(T value)
Definition: timedata.h:37
Network address.
Definition: netaddress.h:112
bool SetInternal(const std::string &name)
Create an "internal" address that represents a name or FQDN.
Definition: netaddress.cpp:169
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:110
Basic testing setup.
Definition: setup_common.h:52
#define ASSERT_DEBUG_LOG(message)
Definition: logging.h:39
int64_t GetTimeOffset()
"Never go to sea with two chronometers; take one or three." Our three time sources are:
Definition: timedata.cpp:30
void TestOnlyResetTimeData()
Reset the internal state of GetTimeOffset() and AddTimeData().
Definition: timedata.cpp:109
void AddTimeData(const CNetAddr &ip, int64_t nOffsetSample)
Definition: timedata.cpp:42
static const int64_t DEFAULT_MAX_TIME_ADJUSTMENT
Definition: timedata.h:13
static void MultiAddTimeData(int n, int64_t offset)
BOOST_AUTO_TEST_CASE(util_MedianFilter)
bilingual_str GetWarnings(bool verbose)
Format a string that describes several potential problems detected by the core.
Definition: warnings.cpp:35