LCOV - code coverage report
Current view: top level - src/test - timedata_tests.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 0 50 0.0 %
Date: 2023-10-05 12:38:51 Functions: 0 15 0.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       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                 :          0 : BOOST_FIXTURE_TEST_SUITE(timedata_tests, BasicTestingSetup)
      20                 :            : 
      21                 :          0 : BOOST_AUTO_TEST_CASE(util_MedianFilter)
      22                 :            : {
      23                 :          0 :     CMedianFilter<int> filter(5, 15);
      24                 :            : 
      25                 :          0 :     BOOST_CHECK_EQUAL(filter.median(), 15);
      26                 :            : 
      27                 :          0 :     filter.input(20); // [15 20]
      28                 :          0 :     BOOST_CHECK_EQUAL(filter.median(), 17);
      29                 :            : 
      30                 :          0 :     filter.input(30); // [15 20 30]
      31                 :          0 :     BOOST_CHECK_EQUAL(filter.median(), 20);
      32                 :            : 
      33                 :          0 :     filter.input(3); // [3 15 20 30]
      34                 :          0 :     BOOST_CHECK_EQUAL(filter.median(), 17);
      35                 :            : 
      36                 :          0 :     filter.input(7); // [3 7 15 20 30]
      37                 :          0 :     BOOST_CHECK_EQUAL(filter.median(), 15);
      38                 :            : 
      39                 :          0 :     filter.input(18); // [3 7 18 20 30]
      40                 :          0 :     BOOST_CHECK_EQUAL(filter.median(), 18);
      41                 :            : 
      42                 :          0 :     filter.input(0); // [0 3 7 18 30]
      43                 :          0 :     BOOST_CHECK_EQUAL(filter.median(), 7);
      44                 :          0 : }
      45                 :            : 
      46                 :          0 : static void MultiAddTimeData(int n, int64_t offset)
      47                 :            : {
      48                 :            :     static int cnt = 0;
      49                 :          0 :     for (int i = 0; i < n; ++i) {
      50                 :          0 :         CNetAddr addr;
      51                 :          0 :         addr.SetInternal(ToString(++cnt));
      52                 :          0 :         AddTimeData(addr, offset);
      53                 :          0 :     }
      54                 :          0 : }
      55                 :            : 
      56                 :            : 
      57                 :          0 : BOOST_AUTO_TEST_CASE(addtimedata)
      58                 :            : {
      59                 :          0 :     BOOST_CHECK_EQUAL(GetTimeOffset(), 0);
      60                 :            : 
      61                 :            :     //Part 1: Add large offsets to test a warning message that our clock may be wrong.
      62                 :          0 :     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                 :          0 :         ASSERT_DEBUG_LOG("Please check that your computer's date and time are correct!");
      67                 :          0 :         MultiAddTimeData(1, DEFAULT_MAX_TIME_ADJUSTMENT + 1); //filter size 5
      68                 :          0 :     }
      69                 :            : 
      70                 :          0 :     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                 :          0 :     BOOST_CHECK_EQUAL(GetTimeOffset(), 0);
      74                 :          0 : 
      75                 :            :     // Part 2: Test positive and negative medians by adding more offsets
      76                 :          0 :     MultiAddTimeData(4, 100); // filter size 9
      77                 :          0 :     BOOST_CHECK_EQUAL(GetTimeOffset(), 100);
      78                 :          0 :     MultiAddTimeData(10, -100); //filter size 19
      79                 :          0 :     BOOST_CHECK_EQUAL(GetTimeOffset(), -100);
      80                 :            : 
      81                 :            :     // Part 3: Test behaviour when filter has reached maximum number of offsets
      82                 :          0 :     const int MAX_SAMPLES = 200;
      83                 :          0 :     int nfill = (MAX_SAMPLES - 3 - 19) / 2; //89
      84                 :          0 :     MultiAddTimeData(nfill, 100);
      85                 :          0 :     MultiAddTimeData(nfill, -100); //filter size MAX_SAMPLES - 3
      86                 :          0 :     BOOST_CHECK_EQUAL(GetTimeOffset(), -100);
      87                 :            : 
      88                 :          0 :     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                 :          0 :     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                 :          0 :     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                 :          0 :     BOOST_CHECK_EQUAL(GetTimeOffset(), 0);
     101                 :            : 
     102                 :          0 :     TestOnlyResetTimeData();
     103                 :          0 : }
     104                 :            : 
     105                 :          0 : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 1.14