Skip to content

Commit a7aa809

Browse files
committed
Merge #16563: test: Add unit test for AddTimeData
7cd069d Add test for AddTimeData (Martin Zumsande) Pull request description: `AddTimeData()` has poor test coverage but interesting logic (including a bug turned into a feature). This PR adds a unit test for it. ACKs for top commit: laanwj: ACK 7cd069d, thanks for adding a test Tree-SHA512: 8228f9027e52ed534411d595c7e45cf4edeee9757f26f5141fbcfae3fc6f598a8cea7f734bb8f55238857a37ad2f2d518e859e1fe8c106c0712da976792ac132
2 parents 24f2979 + 7cd069d commit a7aa809

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

src/test/timedata_tests.cpp

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44
//
5-
#include <timedata.h>
5+
6+
#include <netaddress.h>
7+
#include <noui.h>
68
#include <test/setup_common.h>
9+
#include <timedata.h>
10+
#include <warnings.h>
11+
12+
#include <string>
713

814
#include <boost/test/unit_test.hpp>
915

@@ -34,4 +40,61 @@ BOOST_AUTO_TEST_CASE(util_MedianFilter)
3440
BOOST_CHECK_EQUAL(filter.median(), 7);
3541
}
3642

43+
static void MultiAddTimeData(int n, int64_t offset)
44+
{
45+
static int cnt = 0;
46+
for (int i = 0; i < n; ++i) {
47+
CNetAddr addr;
48+
addr.SetInternal(std::to_string(++cnt));
49+
AddTimeData(addr, offset);
50+
}
51+
}
52+
53+
54+
BOOST_AUTO_TEST_CASE(addtimedata)
55+
{
56+
BOOST_CHECK_EQUAL(GetTimeOffset(), 0);
57+
58+
//Part 1: Add large offsets to test a warning message that our clock may be wrong.
59+
MultiAddTimeData(3, DEFAULT_MAX_TIME_ADJUSTMENT + 1);
60+
// Filter size is 1 + 3 = 4: It is always initialized with a single element (offset 0)
61+
62+
noui_suppress();
63+
MultiAddTimeData(1, DEFAULT_MAX_TIME_ADJUSTMENT + 1); //filter size 5
64+
noui_reconnect();
65+
66+
BOOST_CHECK(GetWarnings("gui").find("clock is wrong") != std::string::npos);
67+
68+
// nTimeOffset is not changed if the median of offsets exceeds DEFAULT_MAX_TIME_ADJUSTMENT
69+
BOOST_CHECK_EQUAL(GetTimeOffset(), 0);
70+
71+
// Part 2: Test positive and negative medians by adding more offsets
72+
MultiAddTimeData(4, 100); // filter size 9
73+
BOOST_CHECK_EQUAL(GetTimeOffset(), 100);
74+
MultiAddTimeData(10, -100); //filter size 19
75+
BOOST_CHECK_EQUAL(GetTimeOffset(), -100);
76+
77+
// Part 3: Test behaviour when filter has reached maximum number of offsets
78+
const int MAX_SAMPLES = 200;
79+
int nfill = (MAX_SAMPLES - 3 - 19) / 2; //89
80+
MultiAddTimeData(nfill, 100);
81+
MultiAddTimeData(nfill, -100); //filter size MAX_SAMPLES - 3
82+
BOOST_CHECK_EQUAL(GetTimeOffset(), -100);
83+
84+
MultiAddTimeData(2, 100);
85+
//filter size MAX_SAMPLES -1, median is the initial 0 offset
86+
//since we added same number of positive/negative offsets
87+
88+
BOOST_CHECK_EQUAL(GetTimeOffset(), 0);
89+
90+
// After the number of offsets has reached MAX_SAMPLES -1 (=199), nTimeOffset will never change
91+
// because it is only updated when the number of elements in the filter becomes odd. It was decided
92+
// not to fix this because it prevents possible attacks. See the comment in AddTimeData() or issue #4521
93+
// for a more detailed explanation.
94+
MultiAddTimeData(2, 100); // filter median is 100 now, but nTimeOffset will not change
95+
BOOST_CHECK_EQUAL(GetTimeOffset(), 0);
96+
97+
// We want this test to end with nTimeOffset==0, otherwise subsequent tests of the suite will fail.
98+
}
99+
37100
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)