"Fossies" - the Fresh Open Source Software Archive

Member "vnstat-2.9/tests/common_tests.c" (26 Jul 2021, 10929 Bytes) of package /linux/misc/vnstat-2.9.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. See also the last Fossies "Diffs" side-by-side code changes report for "common_tests.c": 2.7_vs_2.8.

    1 #include "common.h"
    2 #include "vnstat_tests.h"
    3 #include "common_tests.h"
    4 #include "dbaccess.h"
    5 #include "cfg.h"
    6 
    7 START_TEST(printe_options)
    8 {
    9     noexit = 2;
   10     cfg.uselogging = 0;
   11     ck_assert_int_eq(printe(PT_Info), 1);
   12 
   13     cfg.uselogging = 1;
   14     ck_assert_int_eq(printe(PT_Multiline), 1);
   15 
   16     noexit = 0;
   17     strcpy(errorstring, "dummy string");
   18     suppress_output();
   19     ck_assert_int_eq(printe(PT_Info), 1);
   20     ck_assert_int_eq(printe(PT_Warning), 1);
   21     ck_assert_int_eq(printe(PT_Error), 1);
   22     ck_assert_int_eq(printe(PT_Config), 1);
   23     ck_assert_int_eq(printe(PT_Multiline), 1);
   24     ck_assert_int_eq(printe(PT_ShortMultiline), 1);
   25     ck_assert_int_eq(printe(6), 1);
   26 }
   27 END_TEST
   28 
   29 START_TEST(logprint_options)
   30 {
   31     cfg.uselogging = 0;
   32     ck_assert_int_eq(logprint(PT_Info), 0);
   33 
   34     cfg.uselogging = 1;
   35     strcpy(cfg.logfile, "/dev/null");
   36     strcpy(errorstring, "dummy string");
   37     ck_assert_int_eq(logprint(PT_Info), 1);
   38     ck_assert_int_eq(logprint(PT_Warning), 1);
   39     ck_assert_int_eq(logprint(PT_Error), 1);
   40     ck_assert_int_eq(logprint(PT_Config), 1);
   41     ck_assert_int_eq(logprint(PT_Multiline), 0);
   42     ck_assert_int_eq(logprint(PT_ShortMultiline), 1);
   43     ck_assert_int_eq(logprint(6), 1);
   44 }
   45 END_TEST
   46 
   47 #if defined(__clang__)
   48 #pragma clang diagnostic push
   49 #pragma clang diagnostic ignored "-Wused-but-marked-unused"
   50 #endif
   51 START_TEST(dmonth_return_within_range)
   52 {
   53     int m;
   54     m = dmonth(_i);
   55     ck_assert_int_ge(m, 28);
   56     ck_assert_int_le(m, 31);
   57 }
   58 END_TEST
   59 #if defined(__clang__)
   60 #pragma clang diagnostic pop
   61 #endif
   62 
   63 START_TEST(leapyears_are_known)
   64 {
   65     ck_assert_int_eq(isleapyear(1995), 0);
   66     ck_assert_int_eq(isleapyear(1996), 1);
   67     ck_assert_int_eq(isleapyear(1997), 0);
   68     ck_assert_int_eq(isleapyear(1998), 0);
   69     ck_assert_int_eq(isleapyear(1999), 0);
   70     ck_assert_int_eq(isleapyear(2000), 1);
   71     ck_assert_int_eq(isleapyear(2001), 0);
   72     ck_assert_int_eq(isleapyear(2002), 0);
   73     ck_assert_int_eq(isleapyear(2003), 0);
   74     ck_assert_int_eq(isleapyear(2004), 1);
   75     ck_assert_int_eq(isleapyear(2005), 0);
   76     ck_assert_int_eq(isleapyear(2006), 0);
   77     ck_assert_int_eq(isleapyear(2007), 0);
   78     ck_assert_int_eq(isleapyear(2008), 1);
   79     ck_assert_int_eq(isleapyear(2009), 0);
   80     ck_assert_int_eq(isleapyear(2010), 0);
   81     ck_assert_int_eq(isleapyear(2011), 0);
   82     ck_assert_int_eq(isleapyear(2012), 1);
   83     ck_assert_int_eq(isleapyear(2013), 0);
   84     ck_assert_int_eq(isleapyear(2014), 0);
   85     ck_assert_int_eq(isleapyear(2015), 0);
   86     ck_assert_int_eq(isleapyear(2016), 1);
   87     ck_assert_int_eq(isleapyear(2017), 0);
   88     ck_assert_int_eq(isleapyear(2018), 0);
   89     ck_assert_int_eq(isleapyear(2019), 0);
   90     ck_assert_int_eq(isleapyear(2020), 1);
   91     ck_assert_int_eq(isleapyear(2021), 0);
   92 }
   93 END_TEST
   94 
   95 START_TEST(mosecs_return_values)
   96 {
   97     time_t a, b;
   98     cfg.monthrotate = 1;
   99     ck_assert_int_eq(mosecs(0, 0), 1);
  100 
  101     a = mosecs(172800, 173000);
  102     ck_assert_int_gt(a, 1);
  103 
  104     cfg.monthrotate = 2;
  105     b = mosecs(172800, 173000);
  106     ck_assert_int_gt(b, 1);
  107 
  108     ck_assert_int_gt(a, b);
  109 }
  110 END_TEST
  111 
  112 START_TEST(mosecs_does_not_change_tz)
  113 {
  114 #if defined(_SVID_SOURCE) || defined(_XOPEN_SOURCE) || defined(__APPLE__) || defined(__linux__)
  115     extern long timezone;
  116 #else
  117     long timezone = 0;
  118 #endif
  119     long timezone_before_call;
  120 
  121     tzset();
  122     timezone_before_call = timezone;
  123 
  124     ck_assert_int_eq(cfg.monthrotate, 1);
  125     ck_assert_int_ne(mosecs(1, 2), 0);
  126     ck_assert_int_ne(mosecs(1, 2), 1);
  127     ck_assert_int_eq(timezone_before_call, timezone);
  128 }
  129 END_TEST
  130 
  131 START_TEST(mosecs_does_not_change_struct_tm_pointer_content)
  132 {
  133     struct tm *stm;
  134     time_t current;
  135 
  136     current = time(NULL);
  137     stm = localtime(&current);
  138 
  139     ck_assert_int_eq(cfg.monthrotate, 1);
  140     ck_assert_int_eq(current, mktime(stm));
  141     ck_assert_int_ne(mosecs(1, 2), 0);
  142     ck_assert_int_ne(mosecs(1, 2), 1);
  143     ck_assert_int_eq(current, mktime(stm));
  144 }
  145 END_TEST
  146 
  147 START_TEST(countercalc_no_change_32bit)
  148 {
  149     uint64_t a, b;
  150 
  151     a = b = 0;
  152     ck_assert_int_eq(countercalc(&a, &b, 0), 0);
  153     ck_assert_int_eq(countercalc(&a, &b, -1), 0);
  154     a = b = 1;
  155     ck_assert_int_eq(countercalc(&a, &b, 0), 0);
  156     ck_assert_int_eq(countercalc(&a, &b, -1), 0);
  157 }
  158 END_TEST
  159 
  160 START_TEST(countercalc_no_change_64bit)
  161 {
  162     uint64_t a, b;
  163 
  164     a = b = 0;
  165     ck_assert_int_eq(countercalc(&a, &b, 1), 0);
  166     a = b = 1;
  167     ck_assert_int_eq(countercalc(&a, &b, 1), 0);
  168 }
  169 END_TEST
  170 
  171 START_TEST(countercalc_small_change_32bit)
  172 {
  173     uint64_t a, b;
  174 
  175     a = 0;
  176     b = 1;
  177     ck_assert_int_eq(countercalc(&a, &b, 0), 1);
  178     ck_assert_int_eq(countercalc(&a, &b, -1), 1);
  179     a = 1;
  180     b = 2;
  181     ck_assert_int_eq(countercalc(&a, &b, 0), 1);
  182     ck_assert_int_eq(countercalc(&a, &b, -1), 1);
  183     b = 3;
  184     ck_assert_int_eq(countercalc(&a, &b, 0), 2);
  185     ck_assert_int_eq(countercalc(&a, &b, -1), 2);
  186 }
  187 END_TEST
  188 
  189 START_TEST(countercalc_small_change_64bit)
  190 {
  191     uint64_t a, b;
  192 
  193     a = 0;
  194     b = 1;
  195     ck_assert_int_eq(countercalc(&a, &b, 1), 1);
  196     a = 1;
  197     b = 2;
  198     ck_assert_int_eq(countercalc(&a, &b, 1), 1);
  199     b = 3;
  200     ck_assert_int_eq(countercalc(&a, &b, 1), 2);
  201 }
  202 END_TEST
  203 
  204 START_TEST(countercalc_rollover_with_32bit)
  205 {
  206     uint64_t a, b;
  207 
  208     a = 1;
  209     b = 0;
  210     ck_assert(countercalc(&a, &b, 0) == (MAX32 - 1));
  211     ck_assert(countercalc(&a, &b, -1) == (MAX32 - 1));
  212 }
  213 END_TEST
  214 
  215 START_TEST(countercalc_rollover_with_64bit)
  216 {
  217     uint64_t a, b;
  218 
  219     a = 1;
  220     b = 0;
  221     ck_assert(countercalc(&a, &b, 1) == (MAX64 - 1));
  222 }
  223 END_TEST
  224 
  225 START_TEST(countercalc_rollover_with_64bit_2)
  226 {
  227     uint64_t a, b;
  228 
  229     a = MAX32 + 1;
  230     b = 0;
  231     ck_assert(countercalc(&a, &b, 1) == (MAX64 - MAX32 - 1));
  232 }
  233 END_TEST
  234 
  235 START_TEST(countercalc_rollover_with_32bit_starting_32bit)
  236 {
  237     uint64_t a, b;
  238 
  239     a = MAX32 - 1;
  240     b = 0;
  241     ck_assert(countercalc(&a, &b, 0) == 1);
  242     ck_assert(countercalc(&a, &b, -1) == 1);
  243 }
  244 END_TEST
  245 
  246 START_TEST(countercalc_rollover_with_32bit_starting_over_32bit)
  247 {
  248     uint64_t a, b;
  249 
  250     a = MAX32 + 1;
  251     b = 0;
  252     ck_assert(countercalc(&a, &b, 0) == (MAX64 - MAX32 - 1));
  253     ck_assert(countercalc(&a, &b, -1) == (MAX64 - MAX32 - 1));
  254 }
  255 END_TEST
  256 
  257 START_TEST(countercalc_rollover_with_64bit_starting_32bit)
  258 {
  259     uint64_t a, b;
  260 
  261     a = MAX32 - 1;
  262     b = 0;
  263     ck_assert(countercalc(&a, &b, 1) == (MAX64 - MAX32 + 1));
  264 }
  265 END_TEST
  266 
  267 START_TEST(countercalc_rollover_with_64bit_starting_64bit)
  268 {
  269     uint64_t a, b;
  270 
  271     a = MAX64 - 1;
  272     b = 0;
  273     ck_assert(countercalc(&a, &b, 1) == 1);
  274 }
  275 END_TEST
  276 
  277 START_TEST(strncpy_nt_with_below_maximum_length_string)
  278 {
  279     char dst[6];
  280 
  281     strncpy_nt(dst, "123", 6);
  282     ck_assert_str_eq(dst, "123");
  283 }
  284 END_TEST
  285 
  286 START_TEST(strncpy_nt_with_maximum_length_string)
  287 {
  288     char dst[6];
  289 
  290     strncpy_nt(dst, "12345", 6);
  291     ck_assert_str_eq(dst, "12345");
  292 }
  293 END_TEST
  294 
  295 START_TEST(strncpy_nt_with_over_maximum_length_string)
  296 {
  297     char dst[6];
  298 
  299     strncpy_nt(dst, "123456", 6);
  300     ck_assert_str_eq(dst, "12345");
  301 
  302     strncpy_nt(dst, "1234567890", 6);
  303     ck_assert_str_eq(dst, "12345");
  304 }
  305 END_TEST
  306 
  307 START_TEST(isnumeric_empty)
  308 {
  309     ck_assert_int_eq(isnumeric(""), 0);
  310 }
  311 END_TEST
  312 
  313 START_TEST(isnumeric_it_is)
  314 {
  315     ck_assert_int_eq(isnumeric("0"), 1);
  316     ck_assert_int_eq(isnumeric("1"), 1);
  317     ck_assert_int_eq(isnumeric("12"), 1);
  318     ck_assert_int_eq(isnumeric("123"), 1);
  319 }
  320 END_TEST
  321 
  322 START_TEST(isnumeric_it_is_not)
  323 {
  324     ck_assert_int_eq(isnumeric("a"), 0);
  325     ck_assert_int_eq(isnumeric("abc"), 0);
  326     ck_assert_int_eq(isnumeric("a1"), 0);
  327     ck_assert_int_eq(isnumeric("1a"), 0);
  328     ck_assert_int_eq(isnumeric("123abc"), 0);
  329     ck_assert_int_eq(isnumeric("/"), 0);
  330     ck_assert_int_eq(isnumeric("-"), 0);
  331 }
  332 END_TEST
  333 
  334 START_TEST(getversion_returns_a_version)
  335 {
  336     ck_assert_int_gt((int)strlen(getversion()), 1);
  337     ck_assert(strchr(getversion(), '_') == NULL);
  338     ck_assert(strchr(getversion(), '.') != NULL);
  339 }
  340 END_TEST
  341 
  342 START_TEST(timeused_debug_outputs_something_expected_when_debug_is_enabled)
  343 {
  344     int pipe, len;
  345     char buffer[512];
  346     memset(&buffer, '\0', sizeof(buffer));
  347 
  348     debug = 1;
  349     pipe = pipe_output();
  350     /* the assumption here is that the next two steps
  351        can always execute in less than one second resulting
  352        in a duration that starts with a zero */
  353     timeused_debug("that_func", 1);
  354     timeused_debug("that_func", 0);
  355     fflush(stdout);
  356 
  357     len = (int)read(pipe, buffer, 512);
  358     ck_assert_int_gt(len, 1);
  359     ck_assert_ptr_ne(strstr(buffer, "that_func() in 0"), NULL);
  360 }
  361 END_TEST
  362 
  363 START_TEST(timeused_debug_does_not_output_anything_when_debug_is_disabled)
  364 {
  365     int pipe, len;
  366     char buffer[512];
  367     memset(&buffer, '\0', sizeof(buffer));
  368 
  369     debug = 0;
  370     pipe = pipe_output();
  371     /* the assumption here is that the next two steps
  372        can always execute in less than one second resulting
  373        in a duration that starts with a zero */
  374     timeused_debug("other_func", 1);
  375     timeused_debug("other_func", 0);
  376     printf("-"); // stdout needs to contain something so that read doesn't block
  377     fflush(stdout);
  378 
  379     len = (int)read(pipe, buffer, 512);
  380     ck_assert_int_eq(len, 1);
  381 }
  382 END_TEST
  383 
  384 START_TEST(timeused_tracks_used_time)
  385 {
  386     double used;
  387     struct timespec ts;
  388 
  389 
  390     used = timeused("quick_func", 1);
  391     ck_assert(used == 0.0);
  392 
  393     ts.tv_sec = 0;
  394     ts.tv_nsec = 100000000; // 0.1 s
  395     nanosleep(&ts, NULL);
  396 
  397     used = timeused("quick_func", 0);
  398     ck_assert(used > 0.0);
  399 }
  400 END_TEST
  401 
  402 __attribute__((noreturn))
  403 START_TEST(can_panic)
  404 {
  405     suppress_output();
  406     fclose(stderr);
  407     panicexit(__FILE__, __LINE__);
  408 }
  409 END_TEST
  410 
  411 void add_common_tests(Suite *s)
  412 {
  413     TCase *tc_common = tcase_create("Common");
  414     tcase_add_checked_fixture(tc_common, setup, teardown);
  415     tcase_add_unchecked_fixture(tc_common, setup, teardown);
  416     tcase_add_test(tc_common, printe_options);
  417     tcase_add_test(tc_common, logprint_options);
  418     tcase_add_loop_test(tc_common, dmonth_return_within_range, 0, 12);
  419     tcase_add_test(tc_common, leapyears_are_known);
  420     tcase_add_test(tc_common, mosecs_return_values);
  421     tcase_add_test(tc_common, mosecs_does_not_change_tz);
  422     tcase_add_test(tc_common, mosecs_does_not_change_struct_tm_pointer_content);
  423     tcase_add_test(tc_common, countercalc_no_change_32bit);
  424     tcase_add_test(tc_common, countercalc_no_change_64bit);
  425     tcase_add_test(tc_common, countercalc_small_change_32bit);
  426     tcase_add_test(tc_common, countercalc_small_change_64bit);
  427     tcase_add_test(tc_common, countercalc_rollover_with_32bit);
  428     tcase_add_test(tc_common, countercalc_rollover_with_64bit);
  429     tcase_add_test(tc_common, countercalc_rollover_with_64bit_2);
  430     tcase_add_test(tc_common, countercalc_rollover_with_32bit_starting_32bit);
  431     tcase_add_test(tc_common, countercalc_rollover_with_32bit_starting_over_32bit);
  432     tcase_add_test(tc_common, countercalc_rollover_with_64bit_starting_32bit);
  433     tcase_add_test(tc_common, countercalc_rollover_with_64bit_starting_64bit);
  434     tcase_add_test(tc_common, strncpy_nt_with_below_maximum_length_string);
  435     tcase_add_test(tc_common, strncpy_nt_with_maximum_length_string);
  436     tcase_add_test(tc_common, strncpy_nt_with_over_maximum_length_string);
  437     tcase_add_test(tc_common, isnumeric_empty);
  438     tcase_add_test(tc_common, isnumeric_it_is);
  439     tcase_add_test(tc_common, isnumeric_it_is_not);
  440     tcase_add_test(tc_common, getversion_returns_a_version);
  441     tcase_add_test(tc_common, timeused_debug_outputs_something_expected_when_debug_is_enabled);
  442     tcase_add_test(tc_common, timeused_debug_does_not_output_anything_when_debug_is_disabled);
  443     tcase_add_test(tc_common, timeused_tracks_used_time);
  444     tcase_add_exit_test(tc_common, can_panic, 1);
  445     suite_add_tcase(s, tc_common);
  446 }