|
2 | 2 | // Distributed under the MIT software license, see the accompanying
|
3 | 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
4 | 4 | //
|
5 |
| -#include <timedata.h> |
| 5 | + |
| 6 | +#include <netaddress.h> |
| 7 | +#include <noui.h> |
6 | 8 | #include <test/setup_common.h>
|
| 9 | +#include <timedata.h> |
| 10 | +#include <warnings.h> |
| 11 | + |
| 12 | +#include <string> |
7 | 13 |
|
8 | 14 | #include <boost/test/unit_test.hpp>
|
9 | 15 |
|
@@ -34,4 +40,61 @@ BOOST_AUTO_TEST_CASE(util_MedianFilter)
|
34 | 40 | BOOST_CHECK_EQUAL(filter.median(), 7);
|
35 | 41 | }
|
36 | 42 |
|
| 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 | + |
37 | 100 | BOOST_AUTO_TEST_SUITE_END()
|
0 commit comments