"Fossies" - the Fresh Open Source Software Archive

Member "chrony-3.5/test/unit/sources.c" (10 May 2019, 5291 Bytes) of package /linux/misc/chrony-3.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "sources.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 3.3_vs_3.4.

    1 /*
    2  **********************************************************************
    3  * Copyright (C) Miroslav Lichvar  2016, 2018
    4  * 
    5  * This program is free software; you can redistribute it and/or modify
    6  * it under the terms of version 2 of the GNU General Public License as
    7  * published by the Free Software Foundation.
    8  * 
    9  * This program is distributed in the hope that it will be useful, but
   10  * WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   12  * General Public License for more details.
   13  * 
   14  * You should have received a copy of the GNU General Public License along
   15  * with this program; if not, write to the Free Software Foundation, Inc.,
   16  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   17  * 
   18  **********************************************************************
   19  */
   20 
   21 #include <sources.c>
   22 #include "test.h"
   23 
   24 void
   25 test_unit(void)
   26 {
   27   SRC_Instance srcs[16];
   28   RPT_SourceReport report;
   29   NTP_Sample sample;
   30   IPAddr addr;
   31   int i, j, k, l, samples, sel_options;
   32 
   33   CNF_Initialise(0, 0);
   34   LCL_Initialise();
   35   TST_RegisterDummyDrivers();
   36   SCH_Initialise();
   37   SRC_Initialise();
   38   REF_Initialise();
   39 
   40   REF_SetMode(REF_ModeIgnore);
   41 
   42   for (i = 0; i < 1000; i++) {
   43     DEBUG_LOG("iteration %d", i);
   44 
   45     for (j = 0; j < sizeof (srcs) / sizeof (srcs[0]); j++) {
   46       TEST_CHECK(n_sources == j);
   47 
   48       TST_GetRandomAddress(&addr, IPADDR_UNSPEC, -1);
   49 
   50       sel_options = i & random() & (SRC_SELECT_NOSELECT | SRC_SELECT_PREFER |
   51                                     SRC_SELECT_TRUST | SRC_SELECT_REQUIRE);
   52 
   53       DEBUG_LOG("added source %d options %d", j, sel_options);
   54       srcs[j] = SRC_CreateNewInstance(UTI_IPToRefid(&addr), SRC_NTP, sel_options, &addr,
   55                                       SRC_DEFAULT_MINSAMPLES, SRC_DEFAULT_MAXSAMPLES,
   56                                       0.0, 1.0);
   57       SRC_UpdateReachability(srcs[j], 1);
   58 
   59       samples = (i + j) % 5 + 3;
   60 
   61       sample.offset = TST_GetRandomDouble(-1.0, 1.0);
   62 
   63       for (k = 0; k < samples; k++) {
   64         SCH_GetLastEventTime(&sample.time, NULL, NULL);
   65         UTI_AddDoubleToTimespec(&sample.time, TST_GetRandomDouble(k - samples, k - samples + 1),
   66                                 &sample.time);
   67 
   68         sample.offset += TST_GetRandomDouble(-1.0e-2, 1.0e-2);
   69         sample.peer_delay = TST_GetRandomDouble(1.0e-6, 1.0e-1);
   70         sample.peer_dispersion = TST_GetRandomDouble(1.0e-6, 1.0e-1);
   71         sample.root_delay = sample.peer_delay;
   72         sample.root_dispersion = sample.peer_dispersion;
   73         sample.stratum = 1;
   74         sample.leap = LEAP_Normal;
   75 
   76         DEBUG_LOG("source %d sample %d offset %f delay %f disp %f", j, k,
   77                   sample.offset, sample.peer_delay, sample.peer_dispersion);
   78 
   79         SRC_AccumulateSample(srcs[j], &sample);
   80       }
   81 
   82       for (k = 0; k <= j; k++) {
   83         int passed = 0, trusted = 0, trusted_passed = 0, required = 0, required_passed = 0;
   84         double trusted_lo = DBL_MAX, trusted_hi = DBL_MIN;
   85         double passed_lo = DBL_MAX, passed_hi = DBL_MIN;
   86 
   87         SRC_SelectSource(srcs[k]);
   88         DEBUG_LOG("source %d status %u", k, sources[k]->status);
   89 
   90         for (l = 0; l <= j; l++) {
   91           TEST_CHECK(sources[l]->status > SRC_OK && sources[l]->status <= SRC_SELECTED);
   92           if (sources[l]->sel_options & SRC_SELECT_NOSELECT) {
   93             TEST_CHECK(sources[l]->status == SRC_UNSELECTABLE);
   94           } else if (sources[l]->status != SRC_BAD_DISTANCE) {
   95             if (sources[l]->status >= SRC_NONPREFERRED) {
   96               passed++;
   97               if (passed_lo > sources[l]->sel_info.lo_limit)
   98                 passed_lo = sources[l]->sel_info.lo_limit;
   99               if (passed_hi < sources[l]->sel_info.hi_limit)
  100                 passed_hi = sources[l]->sel_info.hi_limit;
  101             }
  102             if (sources[l]->sel_options & SRC_SELECT_TRUST) {
  103               trusted++;
  104               if (trusted_lo > sources[l]->sel_info.lo_limit)
  105                 trusted_lo = sources[l]->sel_info.lo_limit;
  106               if (trusted_hi < sources[l]->sel_info.hi_limit)
  107                 trusted_hi = sources[l]->sel_info.hi_limit;
  108               if (sources[l]->status >= SRC_NONPREFERRED)
  109                 trusted_passed++;
  110             }
  111             if (sources[l]->sel_options & SRC_SELECT_REQUIRE) {
  112               required++;
  113               if (sources[l]->status >= SRC_NONPREFERRED)
  114                 required_passed++;
  115             }
  116             if (sources[l]->sel_options & SRC_SELECT_PREFER)
  117               TEST_CHECK(sources[l]->status != SRC_NONPREFERRED);
  118           }
  119         }
  120 
  121         DEBUG_LOG("sources %d passed %d trusted %d/%d required %d/%d", j, passed,
  122                   trusted_passed, trusted, required_passed, required);
  123 
  124         TEST_CHECK(!trusted || !passed || (passed_lo >= trusted_lo && passed_hi <= trusted_hi));
  125         TEST_CHECK(!passed || trusted != 1 || (trusted == 1 && trusted_passed == 1));
  126         TEST_CHECK(!passed || !required || required_passed > 0);
  127       }
  128     }
  129 
  130     for (j = 0; j < sizeof (srcs) / sizeof (srcs[0]); j++) {
  131       SRC_ReportSource(j, &report, &sample.time);
  132       SRC_DestroyInstance(srcs[j]);
  133     }
  134   }
  135 
  136   REF_Finalise();
  137   SRC_Finalise();
  138   SCH_Finalise();
  139   LCL_Finalise();
  140   CNF_Finalise();
  141   HSH_Finalise();
  142 }