"Fossies" - the Fresh Open Source Software Archive

Member "vnstat-2.9/tests/misc_tests.c" (29 Dec 2021, 38799 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 latest Fossies "Diffs" side-by-side code changes report for "misc_tests.c": 2.8_vs_2.9.

    1 #include "common.h"
    2 #include "vnstat_tests.h"
    3 #include "misc_tests.h"
    4 #include "misc.h"
    5 #include "dbsql.h"
    6 
    7 START_TEST(getbtime_does_not_return_zero)
    8 {
    9     ck_assert_int_gt(getbtime(), 0);
   10 }
   11 END_TEST
   12 
   13 #if defined(__clang__)
   14 #pragma clang diagnostic push
   15 #pragma clang diagnostic ignored "-Wused-but-marked-unused"
   16 #endif
   17 START_TEST(getunitprefix_returns_something_with_all_cfg_combinations)
   18 {
   19     const char *string;
   20     int j;
   21 
   22     cfg.unitmode = _i;
   23     for (j = 1; j <= (UNITPREFIXCOUNT + 1); j++) {
   24         string = getunitprefix(j);
   25         ck_assert_int_gt(strlen(string), 0);
   26     }
   27 }
   28 END_TEST
   29 
   30 START_TEST(getrateunitprefix_returns_something_with_all_cfg_combinations)
   31 {
   32     const char *string;
   33     int j;
   34 
   35     for (j = 1; j <= (UNITPREFIXCOUNT + 1); j++) {
   36         string = getrateunitprefix(_i, j);
   37         ck_assert_int_gt(strlen(string), 0);
   38     }
   39 }
   40 END_TEST
   41 
   42 START_TEST(getunitdivisor_returns_something_with_all_cfg_combinations)
   43 {
   44     int j;
   45     char div[16];
   46 
   47     for (j = 1; j <= (UNITPREFIXCOUNT + 1); j++) {
   48         snprintf(div, 15, "%" PRIu64 "", getunitdivisor(_i, j));
   49         if (j > UNITPREFIXCOUNT) {
   50             ck_assert_str_eq(div, "1");
   51         } else {
   52             ck_assert_str_ne(div, "0");
   53         }
   54     }
   55 }
   56 END_TEST
   57 #if defined(__clang__)
   58 #pragma clang diagnostic pop
   59 #endif
   60 
   61 START_TEST(spacecheck_does_not_check_when_not_configured)
   62 {
   63     cfg.spacecheck = 0;
   64     ck_assert_int_eq(spacecheck("/nonexistentpath"), 1);
   65 }
   66 END_TEST
   67 
   68 START_TEST(spacecheck_checks_space)
   69 {
   70     cfg.spacecheck = 1;
   71     /* it's assumed that /tmp isn't full */
   72     ck_assert_int_eq(spacecheck("/tmp"), 1);
   73 }
   74 END_TEST
   75 
   76 START_TEST(spacecheck_fails_with_invalid_path)
   77 {
   78     noexit = 1;
   79     cfg.spacecheck = 1;
   80     ck_assert_int_eq(spacecheck("/nonexistentpath"), 0);
   81 }
   82 END_TEST
   83 
   84 START_TEST(getvalue_normal)
   85 {
   86     cfg.defaultdecimals = 2;
   87     cfg.unitmode = 0;
   88     ck_assert_str_eq(getvalue(100, 0, RT_Normal), "100 B");
   89     ck_assert_str_eq(getvalue(1024, 0, RT_Normal), "1.00 KiB");
   90     ck_assert_str_eq(getvalue(1048576, 0, RT_Normal), "1.00 MiB");
   91     ck_assert_str_eq(getvalue(1073741824, 0, RT_Normal), "1.00 GiB");
   92     ck_assert_str_eq(getvalue(1099511627776ULL, 0, RT_Normal), "1.00 TiB");
   93     ck_assert_str_eq(getvalue(1125899906842624ULL, 0, RT_Normal), "1.00 PiB");
   94     ck_assert_str_eq(getvalue(1152921504606846976ULL, 0, RT_Normal), "1.00 EiB");
   95     cfg.unitmode = 1;
   96     ck_assert_str_eq(getvalue(100, 0, RT_Normal), "100 B");
   97     ck_assert_str_eq(getvalue(1024, 0, RT_Normal), "1.00 KB");
   98     ck_assert_str_eq(getvalue(1048576, 0, RT_Normal), "1.00 MB");
   99     ck_assert_str_eq(getvalue(1073741824, 0, RT_Normal), "1.00 GB");
  100     ck_assert_str_eq(getvalue(1099511627776ULL, 0, RT_Normal), "1.00 TB");
  101     ck_assert_str_eq(getvalue(1125899906842624ULL, 0, RT_Normal), "1.00 PB");
  102     ck_assert_str_eq(getvalue(1152921504606846976ULL, 0, RT_Normal), "1.00 EB");
  103     cfg.unitmode = 2;
  104     ck_assert_str_eq(getvalue(100, 0, RT_Normal), "100 B");
  105     ck_assert_str_eq(getvalue(1000, 0, RT_Normal), "1.00 kB");
  106     ck_assert_str_eq(getvalue(1000000, 0, RT_Normal), "1.00 MB");
  107     ck_assert_str_eq(getvalue(1000000000, 0, RT_Normal), "1.00 GB");
  108     ck_assert_str_eq(getvalue(1000000000000ULL, 0, RT_Normal), "1.00 TB");
  109     ck_assert_str_eq(getvalue(1000000000000000ULL, 0, RT_Normal), "1.00 PB");
  110     ck_assert_str_eq(getvalue(1000000000000000000ULL, 0, RT_Normal), "1.00 EB");
  111 }
  112 END_TEST
  113 
  114 START_TEST(getvalue_estimate)
  115 {
  116     cfg.defaultdecimals = 2;
  117     cfg.unitmode = 0;
  118     ck_assert_str_eq(getvalue(100, 0, RT_Estimate), "100 B");
  119     ck_assert_str_eq(getvalue(1024, 0, RT_Estimate), "1 KiB");
  120     ck_assert_str_eq(getvalue(1048576, 0, RT_Estimate), "1.00 MiB");
  121     ck_assert_str_eq(getvalue(1073741824, 0, RT_Estimate), "1.00 GiB");
  122     ck_assert_str_eq(getvalue(1099511627776ULL, 0, RT_Estimate), "1.00 TiB");
  123     ck_assert_str_eq(getvalue(1125899906842624ULL, 0, RT_Estimate), "1.00 PiB");
  124     ck_assert_str_eq(getvalue(1152921504606846976ULL, 0, RT_Estimate), "1.00 EiB");
  125     cfg.unitmode = 1;
  126     ck_assert_str_eq(getvalue(100, 0, RT_Estimate), "100 B");
  127     ck_assert_str_eq(getvalue(1024, 0, RT_Estimate), "1 KB");
  128     ck_assert_str_eq(getvalue(1048576, 0, RT_Estimate), "1.00 MB");
  129     ck_assert_str_eq(getvalue(1073741824, 0, RT_Estimate), "1.00 GB");
  130     ck_assert_str_eq(getvalue(1099511627776ULL, 0, RT_Estimate), "1.00 TB");
  131     ck_assert_str_eq(getvalue(1125899906842624ULL, 0, RT_Estimate), "1.00 PB");
  132     ck_assert_str_eq(getvalue(1152921504606846976ULL, 0, RT_Estimate), "1.00 EB");
  133     cfg.unitmode = 2;
  134     ck_assert_str_eq(getvalue(100, 0, RT_Estimate), "100 B");
  135     ck_assert_str_eq(getvalue(1000, 0, RT_Estimate), "1 kB");
  136     ck_assert_str_eq(getvalue(1000000, 0, RT_Estimate), "1.00 MB");
  137     ck_assert_str_eq(getvalue(1000000000, 0, RT_Estimate), "1.00 GB");
  138     ck_assert_str_eq(getvalue(1000000000000ULL, 0, RT_Estimate), "1.00 TB");
  139     ck_assert_str_eq(getvalue(1000000000000000ULL, 0, RT_Estimate), "1.00 PB");
  140     ck_assert_str_eq(getvalue(1000000000000000000ULL, 0, RT_Estimate), "1.00 EB");
  141 }
  142 END_TEST
  143 
  144 START_TEST(getvalue_imagescale)
  145 {
  146     cfg.defaultdecimals = 2;
  147     cfg.unitmode = 0;
  148     ck_assert_str_eq(getvalue(100, 0, RT_ImageScale), "100 B");
  149     ck_assert_str_eq(getvalue(1024, 0, RT_ImageScale), "1 KiB");
  150     ck_assert_str_eq(getvalue(1048576, 0, RT_ImageScale), "1 MiB");
  151     ck_assert_str_eq(getvalue(1073741824, 0, RT_ImageScale), "1 GiB");
  152     ck_assert_str_eq(getvalue(1099511627776ULL, 0, RT_ImageScale), "1 TiB");
  153     ck_assert_str_eq(getvalue(1125899906842624ULL, 0, RT_ImageScale), "1 PiB");
  154     ck_assert_str_eq(getvalue(1152921504606846976ULL, 0, RT_ImageScale), "1 EiB");
  155     cfg.unitmode = 1;
  156     ck_assert_str_eq(getvalue(100, 0, RT_ImageScale), "100 B");
  157     ck_assert_str_eq(getvalue(1024, 0, RT_ImageScale), "1 KB");
  158     ck_assert_str_eq(getvalue(1048576, 0, RT_ImageScale), "1 MB");
  159     ck_assert_str_eq(getvalue(1073741824, 0, RT_ImageScale), "1 GB");
  160     ck_assert_str_eq(getvalue(1099511627776ULL, 0, RT_ImageScale), "1 TB");
  161     ck_assert_str_eq(getvalue(1125899906842624ULL, 0, RT_ImageScale), "1 PB");
  162     ck_assert_str_eq(getvalue(1152921504606846976ULL, 0, RT_ImageScale), "1 EB");
  163     cfg.unitmode = 2;
  164     ck_assert_str_eq(getvalue(100, 0, RT_ImageScale), "100 B");
  165     ck_assert_str_eq(getvalue(1000, 0, RT_ImageScale), "1 kB");
  166     ck_assert_str_eq(getvalue(1000000, 0, RT_ImageScale), "1 MB");
  167     ck_assert_str_eq(getvalue(1000000000, 0, RT_ImageScale), "1 GB");
  168     ck_assert_str_eq(getvalue(1000000000000ULL, 0, RT_ImageScale), "1 TB");
  169     ck_assert_str_eq(getvalue(1000000000000000ULL, 0, RT_ImageScale), "1 PB");
  170     ck_assert_str_eq(getvalue(1000000000000000000ULL, 0, RT_ImageScale), "1 EB");
  171 }
  172 END_TEST
  173 
  174 START_TEST(getvalue_padding)
  175 {
  176     cfg.defaultdecimals = 2;
  177     cfg.unitmode = 0;
  178     ck_assert_str_eq(getvalue(1024, 10, RT_Normal), "  1.00 KiB");
  179     cfg.unitmode = 1;
  180     ck_assert_str_eq(getvalue(1024, 10, RT_Normal), "   1.00 KB");
  181     cfg.unitmode = 2;
  182     ck_assert_str_eq(getvalue(1000, 10, RT_Normal), "   1.00 kB");
  183 }
  184 END_TEST
  185 
  186 START_TEST(getvalue_zero_values)
  187 {
  188     cfg.unitmode = 0;
  189     ck_assert_str_eq(getvalue(0, 0, RT_Normal), "0 B");
  190     ck_assert_str_eq(getvalue(0, 10, RT_Estimate), "   --     ");
  191     ck_assert_int_eq((int)strlen(getvalue(0, 10, RT_Estimate)), 10);
  192     ck_assert_int_eq((int)strlen(getvalue(0, 20, RT_Estimate)), 20);
  193     ck_assert_str_eq(getvalue(0, 0, RT_ImageScale), "0 B");
  194     cfg.unitmode = 1;
  195     ck_assert_str_eq(getvalue(0, 0, RT_Normal), "0 B");
  196     ck_assert_str_eq(getvalue(0, 10, RT_Estimate), "    --    ");
  197     ck_assert_int_eq((int)strlen(getvalue(0, 10, RT_Estimate)), 10);
  198     ck_assert_int_eq((int)strlen(getvalue(0, 20, RT_Estimate)), 20);
  199     ck_assert_str_eq(getvalue(0, 0, RT_ImageScale), "0 B");
  200     cfg.unitmode = 2;
  201     ck_assert_str_eq(getvalue(0, 0, RT_Normal), "0 B");
  202     ck_assert_str_eq(getvalue(0, 10, RT_Estimate), "    --    ");
  203     ck_assert_int_eq((int)strlen(getvalue(0, 10, RT_Estimate)), 10);
  204     ck_assert_int_eq((int)strlen(getvalue(0, 20, RT_Estimate)), 20);
  205     ck_assert_str_eq(getvalue(0, 0, RT_ImageScale), "0 B");
  206 }
  207 END_TEST
  208 
  209 START_TEST(gettrafficrate_zero_interval)
  210 {
  211     int i, j;
  212 
  213     for (i = 0; i <= 1; i++) {
  214         cfg.rateunit = i;
  215         for (j = 0; j <= 2; j++) {
  216             cfg.unitmode = j;
  217             ck_assert_str_eq(gettrafficrate(1, 0, 0), "n/a");
  218         }
  219     }
  220 }
  221 END_TEST
  222 
  223 START_TEST(gettrafficrate_bytes)
  224 {
  225     cfg.defaultdecimals = 2;
  226     cfg.rateunit = 0;
  227     cfg.unitmode = 0;
  228     ck_assert_str_eq(gettrafficrate(900, 1, 0), "900 B/s");
  229     ck_assert_str_eq(gettrafficrate(9000, 10, 0), "900 B/s");
  230     ck_assert_str_eq(gettrafficrate(102400, 1, 0), "100.00 KiB/s");
  231     ck_assert_str_eq(gettrafficrate(1024000, 10, 0), "100.00 KiB/s");
  232     ck_assert_str_eq(gettrafficrate(1048576, 1, 0), "1.00 MiB/s");
  233     ck_assert_str_eq(gettrafficrate(1073741824, 1, 0), "1.00 GiB/s");
  234     ck_assert_str_eq(gettrafficrate(1099511627776ULL, 1, 0), "1.00 TiB/s");
  235     ck_assert_str_eq(gettrafficrate(1125899906842624ULL, 1, 0), "1.00 PiB/s");
  236     ck_assert_str_eq(gettrafficrate(1152921504606846976ULL, 1, 0), "1.00 EiB/s");
  237     cfg.unitmode = 1;
  238     ck_assert_str_eq(gettrafficrate(900, 1, 0), "900 B/s");
  239     ck_assert_str_eq(gettrafficrate(9000, 10, 0), "900 B/s");
  240     ck_assert_str_eq(gettrafficrate(102400, 1, 0), "100.00 KB/s");
  241     ck_assert_str_eq(gettrafficrate(1024000, 10, 0), "100.00 KB/s");
  242     ck_assert_str_eq(gettrafficrate(1048576, 1, 0), "1.00 MB/s");
  243     ck_assert_str_eq(gettrafficrate(1073741824, 1, 0), "1.00 GB/s");
  244     ck_assert_str_eq(gettrafficrate(1099511627776ULL, 1, 0), "1.00 TB/s");
  245     ck_assert_str_eq(gettrafficrate(1125899906842624ULL, 1, 0), "1.00 PB/s");
  246     ck_assert_str_eq(gettrafficrate(1152921504606846976ULL, 1, 0), "1.00 EB/s");
  247     cfg.unitmode = 2;
  248     ck_assert_str_eq(gettrafficrate(900, 1, 0), "900 B/s");
  249     ck_assert_str_eq(gettrafficrate(9000, 10, 0), "900 B/s");
  250     ck_assert_str_eq(gettrafficrate(100000, 1, 0), "100.00 kB/s");
  251     ck_assert_str_eq(gettrafficrate(1000000, 10, 0), "100.00 kB/s");
  252     ck_assert_str_eq(gettrafficrate(1000000, 1, 0), "1.00 MB/s");
  253     ck_assert_str_eq(gettrafficrate(1000000000, 1, 0), "1.00 GB/s");
  254     ck_assert_str_eq(gettrafficrate(1000000000000ULL, 1, 0), "1.00 TB/s");
  255     ck_assert_str_eq(gettrafficrate(1000000000000000ULL, 1, 0), "1.00 PB/s");
  256     ck_assert_str_eq(gettrafficrate(1000000000000000000ULL, 1, 0), "1.00 EB/s");
  257 }
  258 END_TEST
  259 
  260 START_TEST(gettrafficrate_bits)
  261 {
  262     cfg.defaultdecimals = 2;
  263     cfg.rateunit = 1;
  264 
  265     cfg.rateunitmode = 1;
  266     cfg.unitmode = 0;
  267     ck_assert_str_eq(gettrafficrate(100, 1, 0), "800 bit/s");
  268     ck_assert_str_eq(gettrafficrate(1000, 10, 0), "800 bit/s");
  269     ck_assert_str_eq(gettrafficrate(102400, 1, 0), "819.20 kbit/s");
  270     ck_assert_str_eq(gettrafficrate(1024000, 10, 0), "819.20 kbit/s");
  271     ck_assert_str_eq(gettrafficrate(1048576, 1, 0), "8.39 Mbit/s");
  272     ck_assert_str_eq(gettrafficrate(1073741824, 1, 0), "8.59 Gbit/s");
  273     ck_assert_str_eq(gettrafficrate(1099511627776ULL, 1, 0), "8.80 Tbit/s");
  274     ck_assert_str_eq(gettrafficrate(1125899906842624ULL, 1, 0), "9.01 Pbit/s");
  275     ck_assert_str_eq(gettrafficrate(1152921504606846976ULL, 1, 0), "9.22 Ebit/s");
  276     cfg.unitmode = 1;
  277     ck_assert_str_eq(gettrafficrate(100, 1, 0), "800 bit/s");
  278     ck_assert_str_eq(gettrafficrate(1000, 10, 0), "800 bit/s");
  279     ck_assert_str_eq(gettrafficrate(102400, 1, 0), "819.20 kbit/s");
  280     ck_assert_str_eq(gettrafficrate(1024000, 10, 0), "819.20 kbit/s");
  281     ck_assert_str_eq(gettrafficrate(1048576, 1, 0), "8.39 Mbit/s");
  282     ck_assert_str_eq(gettrafficrate(1073741824, 1, 0), "8.59 Gbit/s");
  283     ck_assert_str_eq(gettrafficrate(1099511627776ULL, 1, 0), "8.80 Tbit/s");
  284     ck_assert_str_eq(gettrafficrate(1125899906842624ULL, 1, 0), "9.01 Pbit/s");
  285     ck_assert_str_eq(gettrafficrate(1152921504606846976ULL, 1, 0), "9.22 Ebit/s");
  286     cfg.unitmode = 2;
  287     ck_assert_str_eq(gettrafficrate(100, 1, 0), "800 bit/s");
  288     ck_assert_str_eq(gettrafficrate(1000, 10, 0), "800 bit/s");
  289     ck_assert_str_eq(gettrafficrate(102400, 1, 0), "819.20 kbit/s");
  290     ck_assert_str_eq(gettrafficrate(1024000, 10, 0), "819.20 kbit/s");
  291     ck_assert_str_eq(gettrafficrate(1048576, 1, 0), "8.39 Mbit/s");
  292     ck_assert_str_eq(gettrafficrate(1073741824, 1, 0), "8.59 Gbit/s");
  293     ck_assert_str_eq(gettrafficrate(1099511627776ULL, 1, 0), "8.80 Tbit/s");
  294     ck_assert_str_eq(gettrafficrate(1125899906842624ULL, 1, 0), "9.01 Pbit/s");
  295     ck_assert_str_eq(gettrafficrate(1152921504606846976ULL, 1, 0), "9.22 Ebit/s");
  296 
  297     cfg.rateunitmode = 0;
  298     cfg.unitmode = 0;
  299     ck_assert_str_eq(gettrafficrate(100, 1, 0), "800 bit/s");
  300     ck_assert_str_eq(gettrafficrate(1000, 10, 0), "800 bit/s");
  301     ck_assert_str_eq(gettrafficrate(102400, 1, 0), "800.00 Kibit/s");
  302     ck_assert_str_eq(gettrafficrate(1024000, 10, 0), "800.00 Kibit/s");
  303     ck_assert_str_eq(gettrafficrate(1048576, 1, 0), "8.00 Mibit/s");
  304     ck_assert_str_eq(gettrafficrate(1073741824, 1, 0), "8.00 Gibit/s");
  305     ck_assert_str_eq(gettrafficrate(1099511627776ULL, 1, 0), "8.00 Tibit/s");
  306     ck_assert_str_eq(gettrafficrate(1125899906842624ULL, 1, 0), "8.00 Pibit/s");
  307     ck_assert_str_eq(gettrafficrate(1152921504606846976ULL, 1, 0), "8.00 Eibit/s");
  308     cfg.unitmode = 1;
  309     ck_assert_str_eq(gettrafficrate(100, 1, 0), "800 bit/s");
  310     ck_assert_str_eq(gettrafficrate(1000, 10, 0), "800 bit/s");
  311     ck_assert_str_eq(gettrafficrate(102400, 1, 0), "800.00 Kibit/s");
  312     ck_assert_str_eq(gettrafficrate(1024000, 10, 0), "800.00 Kibit/s");
  313     ck_assert_str_eq(gettrafficrate(1048576, 1, 0), "8.00 Mibit/s");
  314     ck_assert_str_eq(gettrafficrate(1073741824, 1, 0), "8.00 Gibit/s");
  315     ck_assert_str_eq(gettrafficrate(1099511627776ULL, 1, 0), "8.00 Tibit/s");
  316     ck_assert_str_eq(gettrafficrate(1125899906842624ULL, 1, 0), "8.00 Pibit/s");
  317     ck_assert_str_eq(gettrafficrate(1152921504606846976ULL, 1, 0), "8.00 Eibit/s");
  318     cfg.unitmode = 2;
  319     ck_assert_str_eq(gettrafficrate(100, 1, 0), "800 bit/s");
  320     ck_assert_str_eq(gettrafficrate(1000, 10, 0), "800 bit/s");
  321     ck_assert_str_eq(gettrafficrate(102400, 1, 0), "800.00 Kibit/s");
  322     ck_assert_str_eq(gettrafficrate(1024000, 10, 0), "800.00 Kibit/s");
  323     ck_assert_str_eq(gettrafficrate(1048576, 1, 0), "8.00 Mibit/s");
  324     ck_assert_str_eq(gettrafficrate(1073741824, 1, 0), "8.00 Gibit/s");
  325     ck_assert_str_eq(gettrafficrate(1099511627776ULL, 1, 0), "8.00 Tibit/s");
  326     ck_assert_str_eq(gettrafficrate(1125899906842624ULL, 1, 0), "8.00 Pibit/s");
  327     ck_assert_str_eq(gettrafficrate(1152921504606846976ULL, 1, 0), "8.00 Eibit/s");
  328 }
  329 END_TEST
  330 
  331 START_TEST(gettrafficrate_interval_divides)
  332 {
  333     cfg.defaultdecimals = 2;
  334     cfg.unitmode = 0;
  335     cfg.rateunitmode = 1;
  336     cfg.rateunit = 0;
  337     ck_assert_str_eq(gettrafficrate(102400, 1, 0), "100.00 KiB/s");
  338     ck_assert_str_eq(gettrafficrate(102400, 2, 0), "50.00 KiB/s");
  339     ck_assert_str_eq(gettrafficrate(102400, 10, 0), "10.00 KiB/s");
  340     cfg.rateunit = 1;
  341     ck_assert_str_eq(gettrafficrate(102400, 1, 0), "819.20 kbit/s");
  342     ck_assert_str_eq(gettrafficrate(102400, 2, 0), "409.60 kbit/s");
  343     ck_assert_str_eq(gettrafficrate(102400, 10, 0), "81.92 kbit/s");
  344 }
  345 END_TEST
  346 
  347 START_TEST(gettrafficrate_padding)
  348 {
  349     cfg.defaultdecimals = 2;
  350     cfg.unitmode = 0;
  351     cfg.rateunit = 0;
  352     ck_assert_str_eq(gettrafficrate(102400, 1, 0), "100.00 KiB/s");
  353     ck_assert_str_eq(gettrafficrate(102400, 1, 12), "100.00 KiB/s");
  354     ck_assert_str_eq(gettrafficrate(102400, 1, 14), "  100.00 KiB/s");
  355 
  356     ck_assert_str_eq(gettrafficrate(900, 1, 0), "900 B/s");
  357     ck_assert_str_eq(gettrafficrate(900, 1, 7), "900 B/s");
  358     ck_assert_str_eq(gettrafficrate(900, 1, 12), "     900 B/s");
  359     ck_assert_str_eq(gettrafficrate(900, 1, 14), "       900 B/s");
  360 }
  361 END_TEST
  362 
  363 START_TEST(sighandler_sets_signal)
  364 {
  365     debug = 1;
  366     intsignal = 0;
  367     disable_logprints();
  368     ck_assert(signal(SIGINT, sighandler) != SIG_ERR);
  369     ck_assert(signal(SIGHUP, sighandler) != SIG_ERR);
  370     ck_assert(signal(SIGTERM, sighandler) != SIG_ERR);
  371 
  372     ck_assert_int_eq(kill(getpid(), SIGINT), 0);
  373     ck_assert_int_eq(intsignal, SIGINT);
  374 
  375     ck_assert_int_eq(kill(getpid(), SIGHUP), 0);
  376     ck_assert_int_eq(intsignal, SIGHUP);
  377 
  378     ck_assert_int_eq(kill(getpid(), SIGTERM), 0);
  379     ck_assert_int_eq(intsignal, SIGTERM);
  380 }
  381 END_TEST
  382 
  383 START_TEST(validatedatetime_can_detect_valid_strings)
  384 {
  385     ck_assert_int_eq(validatedatetime("2018-03-24 01:23"), 1);
  386     ck_assert_int_eq(validatedatetime("1998-01-15 23:16"), 1);
  387     ck_assert_int_eq(validatedatetime("2018-03-24"), 1);
  388     ck_assert_int_eq(validatedatetime("1998-01-15"), 1);
  389     ck_assert_int_eq(validatedatetime("today"), 1);
  390 }
  391 END_TEST
  392 
  393 START_TEST(validatedatetime_can_detect_invalid_strings)
  394 {
  395     ck_assert_int_eq(validatedatetime("2018-03-24 01:23:12"), 0);
  396     ck_assert_int_eq(validatedatetime("2018-03-24 01"), 0);
  397     ck_assert_int_eq(validatedatetime("2018-03-24 01:23am"), 0);
  398     ck_assert_int_eq(validatedatetime("2018-03-24 1:23"), 0);
  399     ck_assert_int_eq(validatedatetime("2018-03-24 01-23"), 0);
  400     ck_assert_int_eq(validatedatetime("2018-03-24 "), 0);
  401     ck_assert_int_eq(validatedatetime("2018-03-24 01:23 "), 0);
  402     ck_assert_int_eq(validatedatetime("2018-03-24 "), 0);
  403     ck_assert_int_eq(validatedatetime("2018-o3-24"), 0);
  404     ck_assert_int_eq(validatedatetime("2018-03-24T01:23"), 0);
  405     ck_assert_int_eq(validatedatetime("2018-03-24_01:23"), 0);
  406     ck_assert_int_eq(validatedatetime("2018-03-241"), 0);
  407     ck_assert_int_eq(validatedatetime("2018/03/24"), 0);
  408     ck_assert_int_eq(validatedatetime("2018-03"), 0);
  409     ck_assert_int_eq(validatedatetime("1998-01"), 0);
  410     ck_assert_int_eq(validatedatetime("2018-03-"), 0);
  411     ck_assert_int_eq(validatedatetime("2018_03"), 0);
  412     ck_assert_int_eq(validatedatetime("2018-3"), 0);
  413     ck_assert_int_eq(validatedatetime("2018_03"), 0);
  414     ck_assert_int_eq(validatedatetime("2018"), 0);
  415     ck_assert_int_eq(validatedatetime("1998"), 0);
  416     ck_assert_int_eq(validatedatetime("9999"), 0);
  417     ck_assert_int_eq(validatedatetime("18-03"), 0);
  418     ck_assert_int_eq(validatedatetime("18"), 0);
  419     ck_assert_int_eq(validatedatetime(" "), 0);
  420     ck_assert_int_eq(validatedatetime(""), 0);
  421     ck_assert_int_eq(validatedatetime("wtf?"), 0);
  422     ck_assert_int_eq(validatedatetime("yesterday"), 0);
  423     ck_assert_int_eq(validatedatetime("tomorrow"), 0);
  424 }
  425 END_TEST
  426 
  427 START_TEST(validatedatetime_does_not_validate_numbers)
  428 {
  429     ck_assert_int_eq(validatedatetime("9999-99-99 99:99"), 1);
  430     ck_assert_int_eq(validatedatetime("0000-00-00 00:00"), 1);
  431     ck_assert_int_eq(validatedatetime("2018-03-24 01:90"), 1);
  432 }
  433 END_TEST
  434 
  435 START_TEST(defaultdecimals_controls_the_number_of_decimals)
  436 {
  437     cfg.unitmode = 0;
  438     cfg.rateunitmode = 1;
  439 
  440     cfg.defaultdecimals = 0;
  441     cfg.rateunit = 0;
  442     ck_assert_str_eq(getvalue(100, 0, RT_Normal), "100 B");
  443     ck_assert_str_eq(getvalue(1024, 0, RT_Normal), "1 KiB");
  444     ck_assert_str_eq(getvalue(1048576, 0, RT_Normal), "1 MiB");
  445     ck_assert_str_eq(getvalue(100, 0, RT_Estimate), "100 B");
  446     ck_assert_str_eq(getvalue(1024, 0, RT_Estimate), "1 KiB");
  447     ck_assert_str_eq(getvalue(1048576, 0, RT_Estimate), "1 MiB");
  448     ck_assert_str_eq(getvalue(100, 0, RT_ImageScale), "100 B");
  449     ck_assert_str_eq(getvalue(1024, 0, RT_ImageScale), "1 KiB");
  450     ck_assert_str_eq(getvalue(1048576, 0, RT_ImageScale), "1 MiB");
  451     ck_assert_str_eq(gettrafficrate(900, 1, 0), "900 B/s");
  452     ck_assert_str_eq(gettrafficrate(9000, 10, 0), "900 B/s");
  453     ck_assert_str_eq(gettrafficrate(102400, 1, 0), "100 KiB/s");
  454     ck_assert_str_eq(gettrafficrate(1024000, 10, 0), "100 KiB/s");
  455     ck_assert_str_eq(gettrafficrate(1048576, 1, 0), "1 MiB/s");
  456     cfg.rateunit = 1;
  457     ck_assert_str_eq(gettrafficrate(100, 1, 0), "800 bit/s");
  458     ck_assert_str_eq(gettrafficrate(1000, 10, 0), "800 bit/s");
  459     ck_assert_str_eq(gettrafficrate(102400, 1, 0), "819 kbit/s");
  460     ck_assert_str_eq(gettrafficrate(1024000, 10, 0), "819 kbit/s");
  461     ck_assert_str_eq(gettrafficrate(1048576, 1, 0), "8 Mbit/s");
  462 
  463     cfg.defaultdecimals = 1;
  464     cfg.rateunit = 0;
  465     ck_assert_str_eq(getvalue(100, 0, RT_Normal), "100 B");
  466     ck_assert_str_eq(getvalue(1024, 0, RT_Normal), "1.0 KiB");
  467     ck_assert_str_eq(getvalue(1048576, 0, RT_Normal), "1.0 MiB");
  468     ck_assert_str_eq(getvalue(100, 0, RT_Estimate), "100 B");
  469     ck_assert_str_eq(getvalue(1024, 0, RT_Estimate), "1 KiB");
  470     ck_assert_str_eq(getvalue(1048576, 0, RT_Estimate), "1.0 MiB");
  471     ck_assert_str_eq(getvalue(100, 0, RT_ImageScale), "100 B");
  472     ck_assert_str_eq(getvalue(1024, 0, RT_ImageScale), "1 KiB");
  473     ck_assert_str_eq(getvalue(1048576, 0, RT_ImageScale), "1 MiB");
  474     ck_assert_str_eq(gettrafficrate(900, 1, 0), "900 B/s");
  475     ck_assert_str_eq(gettrafficrate(9000, 10, 0), "900 B/s");
  476     ck_assert_str_eq(gettrafficrate(102400, 1, 0), "100.0 KiB/s");
  477     ck_assert_str_eq(gettrafficrate(1024000, 10, 0), "100.0 KiB/s");
  478     ck_assert_str_eq(gettrafficrate(1048576, 1, 0), "1.0 MiB/s");
  479     cfg.rateunit = 1;
  480     ck_assert_str_eq(gettrafficrate(100, 1, 0), "800 bit/s");
  481     ck_assert_str_eq(gettrafficrate(1000, 10, 0), "800 bit/s");
  482     ck_assert_str_eq(gettrafficrate(102400, 1, 0), "819.2 kbit/s");
  483     ck_assert_str_eq(gettrafficrate(1024000, 10, 0), "819.2 kbit/s");
  484     ck_assert_str_eq(gettrafficrate(1048576, 1, 0), "8.4 Mbit/s");
  485 
  486     cfg.defaultdecimals = 2;
  487     cfg.rateunit = 0;
  488     ck_assert_str_eq(getvalue(100, 0, RT_Normal), "100 B");
  489     ck_assert_str_eq(getvalue(1024, 0, RT_Normal), "1.00 KiB");
  490     ck_assert_str_eq(getvalue(1048576, 0, RT_Normal), "1.00 MiB");
  491     ck_assert_str_eq(getvalue(100, 0, RT_Estimate), "100 B");
  492     ck_assert_str_eq(getvalue(1024, 0, RT_Estimate), "1 KiB");
  493     ck_assert_str_eq(getvalue(1048576, 0, RT_Estimate), "1.00 MiB");
  494     ck_assert_str_eq(getvalue(100, 0, RT_ImageScale), "100 B");
  495     ck_assert_str_eq(getvalue(1024, 0, RT_ImageScale), "1 KiB");
  496     ck_assert_str_eq(getvalue(1048576, 0, RT_ImageScale), "1 MiB");
  497     ck_assert_str_eq(gettrafficrate(900, 1, 0), "900 B/s");
  498     ck_assert_str_eq(gettrafficrate(9000, 10, 0), "900 B/s");
  499     ck_assert_str_eq(gettrafficrate(102400, 1, 0), "100.00 KiB/s");
  500     ck_assert_str_eq(gettrafficrate(1024000, 10, 0), "100.00 KiB/s");
  501     ck_assert_str_eq(gettrafficrate(1048576, 1, 0), "1.00 MiB/s");
  502     cfg.rateunit = 1;
  503     ck_assert_str_eq(gettrafficrate(100, 1, 0), "800 bit/s");
  504     ck_assert_str_eq(gettrafficrate(1000, 10, 0), "800 bit/s");
  505     ck_assert_str_eq(gettrafficrate(102400, 1, 0), "819.20 kbit/s");
  506     ck_assert_str_eq(gettrafficrate(1024000, 10, 0), "819.20 kbit/s");
  507     ck_assert_str_eq(gettrafficrate(1048576, 1, 0), "8.39 Mbit/s");
  508 
  509     cfg.defaultdecimals = 3;
  510     cfg.rateunit = 0;
  511     ck_assert_str_eq(getvalue(100, 0, RT_Normal), "100 B");
  512     ck_assert_str_eq(getvalue(1024, 0, RT_Normal), "1.000 KiB");
  513     ck_assert_str_eq(getvalue(1048576, 0, RT_Normal), "1.000 MiB");
  514     ck_assert_str_eq(getvalue(100, 0, RT_Estimate), "100 B");
  515     ck_assert_str_eq(getvalue(1024, 0, RT_Estimate), "1 KiB");
  516     ck_assert_str_eq(getvalue(1048576, 0, RT_Estimate), "1.000 MiB");
  517     ck_assert_str_eq(getvalue(100, 0, RT_ImageScale), "100 B");
  518     ck_assert_str_eq(getvalue(1024, 0, RT_ImageScale), "1 KiB");
  519     ck_assert_str_eq(getvalue(1048576, 0, RT_ImageScale), "1 MiB");
  520     ck_assert_str_eq(gettrafficrate(900, 1, 0), "900 B/s");
  521     ck_assert_str_eq(gettrafficrate(9000, 10, 0), "900 B/s");
  522     ck_assert_str_eq(gettrafficrate(102400, 1, 0), "100.000 KiB/s");
  523     ck_assert_str_eq(gettrafficrate(1024000, 10, 0), "100.000 KiB/s");
  524     ck_assert_str_eq(gettrafficrate(1048576, 1, 0), "1.000 MiB/s");
  525     cfg.rateunit = 1;
  526     ck_assert_str_eq(gettrafficrate(100, 1, 0), "800 bit/s");
  527     ck_assert_str_eq(gettrafficrate(1000, 10, 0), "800 bit/s");
  528     ck_assert_str_eq(gettrafficrate(102400, 1, 0), "819.200 kbit/s");
  529     ck_assert_str_eq(gettrafficrate(1024000, 10, 0), "819.200 kbit/s");
  530     ck_assert_str_eq(gettrafficrate(1048576, 1, 0), "8.389 Mbit/s");
  531 }
  532 END_TEST
  533 
  534 START_TEST(issametimeslot_knows_the_none_list)
  535 {
  536     ck_assert_int_eq(issametimeslot(LT_None, 10, 11), 0);
  537 }
  538 END_TEST
  539 
  540 START_TEST(issametimeslot_handles_updates_before_the_entry_time)
  541 {
  542     time_t entry, updated;
  543 
  544     entry = (time_t)get_timestamp(2019, 4, 15, 12, 31);
  545     updated = (time_t)get_timestamp(2019, 4, 15, 12, 30);
  546 
  547     ck_assert_int_eq(issametimeslot(LT_5min, entry, updated), 0);
  548     ck_assert_int_eq(issametimeslot(LT_Hour, entry, updated), 0);
  549     ck_assert_int_eq(issametimeslot(LT_Day, entry, updated), 0);
  550     ck_assert_int_eq(issametimeslot(LT_Top, entry, updated), 0);
  551     ck_assert_int_eq(issametimeslot(LT_Month, entry, updated), 0);
  552     ck_assert_int_eq(issametimeslot(LT_Year, entry, updated), 0);
  553 }
  554 END_TEST
  555 
  556 START_TEST(issametimeslot_knows_simple_slots)
  557 {
  558     time_t entry, updated;
  559 
  560     entry = (time_t)get_timestamp(2019, 4, 15, 12, 30);
  561 
  562     updated = entry;
  563     ck_assert_int_eq(issametimeslot(LT_5min, entry, updated), 1);
  564     ck_assert_int_eq(issametimeslot(LT_Hour, entry, updated), 1);
  565     ck_assert_int_eq(issametimeslot(LT_Day, entry, updated), 1);
  566     ck_assert_int_eq(issametimeslot(LT_Top, entry, updated), 1);
  567     ck_assert_int_eq(issametimeslot(LT_Month, entry, updated), 1);
  568     ck_assert_int_eq(issametimeslot(LT_Year, entry, updated), 1);
  569 
  570     updated = entry + 1;
  571     ck_assert_int_eq(issametimeslot(LT_5min, entry, updated), 1);
  572     ck_assert_int_eq(issametimeslot(LT_Hour, entry, updated), 1);
  573     ck_assert_int_eq(issametimeslot(LT_Day, entry, updated), 1);
  574     ck_assert_int_eq(issametimeslot(LT_Top, entry, updated), 1);
  575     ck_assert_int_eq(issametimeslot(LT_Month, entry, updated), 1);
  576     ck_assert_int_eq(issametimeslot(LT_Year, entry, updated), 1);
  577 
  578     entry = (time_t)get_timestamp(2019, 4, 1, 12, 30);
  579     updated = (time_t)get_timestamp(2019, 4, 1, 12, 34);
  580     ck_assert_int_eq(issametimeslot(LT_5min, entry, updated), 1);
  581 
  582     updated = (time_t)get_timestamp(2019, 4, 1, 12, 35);
  583     ck_assert_int_eq(issametimeslot(LT_5min, entry, updated), 0);
  584 
  585     entry = (time_t)get_timestamp(2019, 4, 1, 12, 0);
  586     updated = (time_t)get_timestamp(2019, 4, 1, 12, 59);
  587     ck_assert_int_eq(issametimeslot(LT_Hour, entry, updated), 1);
  588 
  589     updated = (time_t)get_timestamp(2019, 4, 1, 13, 0);
  590     ck_assert_int_eq(issametimeslot(LT_Hour, entry, updated), 0);
  591 
  592     entry = (time_t)get_timestamp(2019, 4, 1, 0, 0);
  593     updated = (time_t)get_timestamp(2019, 4, 1, 23, 59);
  594     ck_assert_int_eq(issametimeslot(LT_Day, entry, updated), 1);
  595     ck_assert_int_eq(issametimeslot(LT_Top, entry, updated), 1);
  596 
  597     updated = (time_t)get_timestamp(2019, 4, 2, 0, 0);
  598     ck_assert_int_eq(issametimeslot(LT_Day, entry, updated), 0);
  599     ck_assert_int_eq(issametimeslot(LT_Top, entry, updated), 0);
  600 
  601     entry = (time_t)get_timestamp(2019, 4, 1, 0, 0);
  602     updated = (time_t)get_timestamp(2019, 4, 30, 23, 59);
  603     ck_assert_int_eq(issametimeslot(LT_Month, entry, updated), 1);
  604 
  605     updated = (time_t)get_timestamp(2019, 5, 1, 0, 0);
  606     ck_assert_int_eq(issametimeslot(LT_Month, entry, updated), 0);
  607 
  608     entry = (time_t)get_timestamp(2019, 1, 1, 0, 0);
  609     updated = (time_t)get_timestamp(2019, 12, 31, 23, 59);
  610     ck_assert_int_eq(issametimeslot(LT_Year, entry, updated), 1);
  611 
  612     entry = (time_t)get_timestamp(2019, 1, 1, 0, 0);
  613     updated = (time_t)get_timestamp(2020, 1, 1, 0, 0);
  614     ck_assert_int_eq(issametimeslot(LT_Year, entry, updated), 0);
  615 }
  616 END_TEST
  617 
  618 START_TEST(issametimeslot_knows_its_slots)
  619 {
  620     time_t entry, updated;
  621 
  622     entry = (time_t)get_timestamp(2019, 4, 15, 12, 30);
  623 
  624     /* the database has the entry timestamp stored with the first possible */
  625     /* time of the specific range resulting in many of the following scenarios */
  626     /* never happening during normal usage */
  627 
  628     updated = (time_t)get_timestamp(2019, 4, 15, 12, 32);
  629     ck_assert_int_eq(issametimeslot(LT_5min, entry, updated), 1);
  630     ck_assert_int_eq(issametimeslot(LT_Hour, entry, updated), 1);
  631     ck_assert_int_eq(issametimeslot(LT_Day, entry, updated), 1);
  632     ck_assert_int_eq(issametimeslot(LT_Top, entry, updated), 1);
  633     ck_assert_int_eq(issametimeslot(LT_Month, entry, updated), 1);
  634     ck_assert_int_eq(issametimeslot(LT_Year, entry, updated), 1);
  635 
  636     updated = (time_t)get_timestamp(2019, 4, 15, 12, 35);
  637     ck_assert_int_eq(issametimeslot(LT_5min, entry, updated), 0);
  638     ck_assert_int_eq(issametimeslot(LT_Hour, entry, updated), 1);
  639     ck_assert_int_eq(issametimeslot(LT_Day, entry, updated), 1);
  640     ck_assert_int_eq(issametimeslot(LT_Top, entry, updated), 1);
  641     ck_assert_int_eq(issametimeslot(LT_Month, entry, updated), 1);
  642     ck_assert_int_eq(issametimeslot(LT_Year, entry, updated), 1);
  643 
  644     updated = (time_t)get_timestamp(2019, 4, 15, 13, 00);
  645     ck_assert_int_eq(issametimeslot(LT_5min, entry, updated), 0);
  646     ck_assert_int_eq(issametimeslot(LT_Hour, entry, updated), 0);
  647     ck_assert_int_eq(issametimeslot(LT_Day, entry, updated), 1);
  648     ck_assert_int_eq(issametimeslot(LT_Top, entry, updated), 1);
  649     ck_assert_int_eq(issametimeslot(LT_Month, entry, updated), 1);
  650     ck_assert_int_eq(issametimeslot(LT_Year, entry, updated), 1);
  651 
  652     updated = (time_t)get_timestamp(2019, 4, 16, 13, 00);
  653     ck_assert_int_eq(issametimeslot(LT_5min, entry, updated), 0);
  654     ck_assert_int_eq(issametimeslot(LT_Hour, entry, updated), 0);
  655     ck_assert_int_eq(issametimeslot(LT_Day, entry, updated), 0);
  656     ck_assert_int_eq(issametimeslot(LT_Top, entry, updated), 0);
  657     ck_assert_int_eq(issametimeslot(LT_Month, entry, updated), 1);
  658     ck_assert_int_eq(issametimeslot(LT_Year, entry, updated), 1);
  659 
  660     updated = (time_t)get_timestamp(2019, 4, 30, 00, 00);
  661     ck_assert_int_eq(issametimeslot(LT_5min, entry, updated), 0);
  662     ck_assert_int_eq(issametimeslot(LT_Hour, entry, updated), 0);
  663     ck_assert_int_eq(issametimeslot(LT_Day, entry, updated), 0);
  664     ck_assert_int_eq(issametimeslot(LT_Top, entry, updated), 0);
  665     ck_assert_int_eq(issametimeslot(LT_Month, entry, updated), 1);
  666     ck_assert_int_eq(issametimeslot(LT_Year, entry, updated), 1);
  667 
  668     updated = (time_t)get_timestamp(2019, 4, 30, 23, 59);
  669     ck_assert_int_eq(issametimeslot(LT_5min, entry, updated), 0);
  670     ck_assert_int_eq(issametimeslot(LT_Hour, entry, updated), 0);
  671     ck_assert_int_eq(issametimeslot(LT_Day, entry, updated), 0);
  672     ck_assert_int_eq(issametimeslot(LT_Top, entry, updated), 0);
  673     ck_assert_int_eq(issametimeslot(LT_Month, entry, updated), 1);
  674     ck_assert_int_eq(issametimeslot(LT_Year, entry, updated), 1);
  675 
  676     updated = (time_t)get_timestamp(2019, 5, 16, 13, 00);
  677     ck_assert_int_eq(issametimeslot(LT_5min, entry, updated), 0);
  678     ck_assert_int_eq(issametimeslot(LT_Hour, entry, updated), 0);
  679     ck_assert_int_eq(issametimeslot(LT_Day, entry, updated), 0);
  680     ck_assert_int_eq(issametimeslot(LT_Top, entry, updated), 0);
  681     ck_assert_int_eq(issametimeslot(LT_Month, entry, updated), 0);
  682     ck_assert_int_eq(issametimeslot(LT_Year, entry, updated), 1);
  683 
  684     updated = (time_t)get_timestamp(2020, 5, 16, 13, 00);
  685     ck_assert_int_eq(issametimeslot(LT_5min, entry, updated), 0);
  686     ck_assert_int_eq(issametimeslot(LT_Hour, entry, updated), 0);
  687     ck_assert_int_eq(issametimeslot(LT_Day, entry, updated), 0);
  688     ck_assert_int_eq(issametimeslot(LT_Top, entry, updated), 0);
  689     ck_assert_int_eq(issametimeslot(LT_Month, entry, updated), 0);
  690     ck_assert_int_eq(issametimeslot(LT_Year, entry, updated), 0);
  691 
  692     entry = (time_t)get_timestamp(2019, 4, 1, 0, 0);
  693     updated = (time_t)get_timestamp(2019, 5, 1, 0, 0);
  694     ck_assert_int_eq(issametimeslot(LT_5min, entry, updated), 0);
  695     ck_assert_int_eq(issametimeslot(LT_Hour, entry, updated), 0);
  696     ck_assert_int_eq(issametimeslot(LT_Day, entry, updated), 0);
  697     ck_assert_int_eq(issametimeslot(LT_Top, entry, updated), 0);
  698     ck_assert_int_eq(issametimeslot(LT_Month, entry, updated), 0);
  699     ck_assert_int_eq(issametimeslot(LT_Year, entry, updated), 1);
  700 
  701     entry = (time_t)get_timestamp(2019, 12, 31, 23, 59);
  702     updated = (time_t)get_timestamp(2020, 1, 1, 0, 0);
  703     ck_assert_int_eq(issametimeslot(LT_5min, entry, updated), 0);
  704     ck_assert_int_eq(issametimeslot(LT_Hour, entry, updated), 0);
  705     ck_assert_int_eq(issametimeslot(LT_Day, entry, updated), 0);
  706     ck_assert_int_eq(issametimeslot(LT_Top, entry, updated), 0);
  707     ck_assert_int_eq(issametimeslot(LT_Month, entry, updated), 0);
  708     ck_assert_int_eq(issametimeslot(LT_Year, entry, updated), 0);
  709 }
  710 END_TEST
  711 
  712 START_TEST(getperiodseconds_knows_fixed_not_ongoing_periods)
  713 {
  714     time_t entry, updated;
  715 
  716     entry = (time_t)get_timestamp(2021, 1, 1, 18, 0);
  717     updated = entry;
  718 
  719     ck_assert_int_eq(getperiodseconds(LT_None, entry, updated, 0), 0);
  720     ck_assert_int_eq(getperiodseconds(LT_5min, entry, updated, 0), 300);
  721     ck_assert_int_eq(getperiodseconds(LT_Hour, entry, updated, 0), 3600);
  722     ck_assert_int_eq(getperiodseconds(LT_Day, entry, updated, 0), 86400);
  723     ck_assert_int_eq(getperiodseconds(LT_Top, entry, updated, 0), 86400);
  724 }
  725 END_TEST
  726 
  727 START_TEST(getperiodseconds_knows_dynamic_not_ongoing_periods)
  728 {
  729     time_t entry, updated;
  730 
  731     entry = (time_t)get_timestamp(2021, 1, 1, 18, 0);
  732     updated = entry;
  733 
  734     ck_assert_int_eq(getperiodseconds(LT_Month, entry, updated, 0), 2678400);
  735 
  736     /* 2021 isn't a leap year */
  737     ck_assert_int_eq(getperiodseconds(LT_Year, entry, updated, 0), 31536000);
  738 
  739     entry = (time_t)get_timestamp(2020, 1, 1, 18, 0);
  740     updated = entry;
  741 
  742     /* 2020 is a leap year */
  743     ck_assert_int_eq(getperiodseconds(LT_Year, entry, updated, 0), 31622400);
  744 }
  745 END_TEST
  746 
  747 START_TEST(getperiodseconds_returns_zero_when_there_is_no_time_spent)
  748 {
  749     time_t entry, updated;
  750 
  751     cfg.monthrotate = 1;
  752     entry = (time_t)get_timestamp(2021, 1, 1, 0, 0);
  753     updated = entry;
  754 
  755     ck_assert_int_eq(getperiodseconds(LT_None, entry, updated, 1), 0);
  756     ck_assert_int_eq(getperiodseconds(LT_5min, entry, updated, 1), 0);
  757     ck_assert_int_eq(getperiodseconds(LT_Hour, entry, updated, 1), 0);
  758     ck_assert_int_eq(getperiodseconds(LT_Day, entry, updated, 1), 0);
  759     ck_assert_int_eq(getperiodseconds(LT_Year, entry, updated, 1), 0);
  760 
  761     /* months are special due to cfg.monthrotate */
  762     ck_assert_int_eq(getperiodseconds(LT_Month, entry, updated, 1), 1);
  763 
  764     /* LT_Top always returns the same value */
  765     ck_assert_int_eq(getperiodseconds(LT_Top, entry, updated, 1), 86400);
  766 }
  767 END_TEST
  768 
  769 START_TEST(getperiodseconds_knows_spent_ongoing_time)
  770 {
  771     time_t entry, updated;
  772 
  773     cfg.monthrotate = 1;
  774     entry = (time_t)get_timestamp(2021, 1, 2, 3, 46);
  775     updated = entry;
  776 
  777     ck_assert_int_eq(getperiodseconds(LT_None, entry, updated, 1), 0);
  778     ck_assert_int_eq(getperiodseconds(LT_5min, entry, updated, 1), 60);
  779     ck_assert_int_eq(getperiodseconds(LT_Hour, entry, updated, 1), 2760);
  780     ck_assert_int_eq(getperiodseconds(LT_Day, entry, updated, 1), 13560);
  781     ck_assert_int_eq(getperiodseconds(LT_Year, entry, updated, 1), 99960);
  782 
  783     /* months are special due to cfg.monthrotate */
  784     ck_assert_int_eq(getperiodseconds(LT_Month, entry, updated, 1), 1);
  785 
  786     /* LT_Top always returns the same value */
  787     ck_assert_int_eq(getperiodseconds(LT_Top, entry, updated, 1), 86400);
  788 }
  789 END_TEST
  790 
  791 START_TEST(getestimates_has_error_handling)
  792 {
  793     time_t updated;
  794     uint64_t rx = 1, tx = 2;
  795     dbdatalist *datalist = NULL;
  796 
  797     updated = (time_t)get_timestamp(2021, 1, 2, 3, 46);
  798 
  799     getestimates(&rx, &tx, LT_None, updated, &datalist);
  800     ck_assert_int_eq(rx, 0);
  801     ck_assert_int_eq(tx, 0);
  802 
  803     rx = 1;
  804     tx = 2;
  805     ck_assert_int_eq(dbdatalistadd(&datalist, 0, 0, 0, 1), 1);
  806     getestimates(&rx, &tx, LT_Day, updated, &datalist);
  807     ck_assert_int_eq(rx, 0);
  808     ck_assert_int_eq(tx, 0);
  809     dbdatalistfree(&datalist);
  810 
  811     rx = 1;
  812     tx = 2;
  813     ck_assert_int_eq(dbdatalistadd(&datalist, 1000000, 0, 0, 1), 1);
  814     getestimates(&rx, &tx, LT_Day, updated, &datalist);
  815     ck_assert_int_eq(rx, 0);
  816     ck_assert_int_eq(tx, 0);
  817     dbdatalistfree(&datalist);
  818 
  819     rx = 1;
  820     tx = 2;
  821     ck_assert_int_eq(dbdatalistadd(&datalist, 0, 1000000, 0, 1), 1);
  822     getestimates(&rx, &tx, LT_Day, updated, &datalist);
  823     ck_assert_int_eq(rx, 0);
  824     ck_assert_int_eq(tx, 0);
  825     dbdatalistfree(&datalist);
  826 
  827     rx = 1;
  828     tx = 2;
  829     ck_assert_int_eq(dbdatalistadd(&datalist, 1000000, 1000000, 0, 1), 1);
  830     getestimates(&rx, &tx, LT_None, updated, &datalist);
  831     ck_assert_int_eq(rx, 0);
  832     ck_assert_int_eq(tx, 0);
  833     dbdatalistfree(&datalist);
  834 }
  835 END_TEST
  836 
  837 START_TEST(getestimates_has_a_crystal_ball)
  838 {
  839     time_t updated;
  840     uint64_t rx = 1, tx = 2;
  841     dbdatalist *datalist = NULL;
  842 
  843     cfg.monthrotate = 1;
  844     updated = (time_t)get_timestamp(2021, 1, 1, 3, 45);
  845     ck_assert_int_eq(dbdatalistadd(&datalist, 100000, 200000, updated, 1), 1);
  846 
  847     rx = 1;
  848     tx = 2;
  849     /* on the 5 minute so there's no calculation done */
  850     getestimates(&rx, &tx, LT_5min, updated, &datalist);
  851     ck_assert_int_eq(rx, 100000);
  852     ck_assert_int_eq(tx, 200000);
  853 
  854     updated = (time_t)get_timestamp(2021, 1, 1, 3, 46);
  855 
  856     rx = 1;
  857     tx = 2;
  858     getestimates(&rx, &tx, LT_5min, updated, &datalist);
  859     ck_assert_int_eq(rx, 499800);
  860     ck_assert_int_eq(tx, 999900);
  861 
  862     rx = 1;
  863     tx = 2;
  864     getestimates(&rx, &tx, LT_Hour, updated, &datalist);
  865     ck_assert_int_eq(rx, 129600);
  866     ck_assert_int_eq(tx, 259200);
  867 
  868     updated = (time_t)get_timestamp(2021, 1, 2, 3, 0);
  869 
  870     rx = 1;
  871     tx = 2;
  872     /* on the hour so there's no calculation done */
  873     getestimates(&rx, &tx, LT_Hour, updated, &datalist);
  874     ck_assert_int_eq(rx, 100000);
  875     ck_assert_int_eq(tx, 200000);
  876 
  877     rx = 1;
  878     tx = 2;
  879     getestimates(&rx, &tx, LT_Day, updated, &datalist);
  880     ck_assert_int_eq(rx, 777600);
  881     ck_assert_int_eq(tx, 1555200);
  882 
  883     rx = 1;
  884     tx = 2;
  885     getestimates(&rx, &tx, LT_Month, updated, &datalist);
  886     ck_assert_int_eq(rx, 2678400);
  887     ck_assert_int_eq(tx, 5356800);
  888 
  889     rx = 1;
  890     tx = 2;
  891     getestimates(&rx, &tx, LT_Year, updated, &datalist);
  892     ck_assert_int_eq(rx, 32061600);
  893     ck_assert_int_eq(tx, 64648800);
  894 
  895     dbdatalistfree(&datalist);
  896 }
  897 END_TEST
  898 
  899 START_TEST(ishelprequest_knows_what_a_help_request_is)
  900 {
  901     ck_assert_int_eq(ishelprequest("--help"), 1);
  902     ck_assert_int_eq(ishelprequest("-?"), 1);
  903     ck_assert_int_eq(ishelprequest("?"), 1);
  904     ck_assert_int_eq(ishelprequest("help"), 0);
  905     ck_assert_int_eq(ishelprequest("-h"), 0);
  906     ck_assert_int_eq(ishelprequest("--helpme"), 0);
  907     ck_assert_int_eq(ishelprequest(""), 0);
  908     ck_assert_int_eq(ishelprequest("1"), 0);
  909     ck_assert_int_eq(ishelprequest("a"), 0);
  910 }
  911 END_TEST
  912 
  913 void add_misc_tests(Suite *s)
  914 {
  915     TCase *tc_misc = tcase_create("Misc");
  916     tcase_add_checked_fixture(tc_misc, setup, teardown);
  917     tcase_add_unchecked_fixture(tc_misc, setup, teardown);
  918     tcase_add_test(tc_misc, getbtime_does_not_return_zero);
  919     tcase_add_loop_test(tc_misc, getunitprefix_returns_something_with_all_cfg_combinations, 0, 2);
  920     tcase_add_loop_test(tc_misc, getrateunitprefix_returns_something_with_all_cfg_combinations, 0, 3);
  921     tcase_add_loop_test(tc_misc, getunitdivisor_returns_something_with_all_cfg_combinations, 0, 3);
  922     tcase_add_test(tc_misc, spacecheck_does_not_check_when_not_configured);
  923     tcase_add_test(tc_misc, spacecheck_checks_space);
  924     tcase_add_test(tc_misc, spacecheck_fails_with_invalid_path);
  925     tcase_add_test(tc_misc, getvalue_normal);
  926     tcase_add_test(tc_misc, getvalue_estimate);
  927     tcase_add_test(tc_misc, getvalue_imagescale);
  928     tcase_add_test(tc_misc, getvalue_padding);
  929     tcase_add_test(tc_misc, getvalue_zero_values);
  930     tcase_add_test(tc_misc, gettrafficrate_zero_interval);
  931     tcase_add_test(tc_misc, gettrafficrate_bytes);
  932     tcase_add_test(tc_misc, gettrafficrate_bits);
  933     tcase_add_test(tc_misc, gettrafficrate_interval_divides);
  934     tcase_add_test(tc_misc, gettrafficrate_padding);
  935     tcase_add_test(tc_misc, sighandler_sets_signal);
  936     tcase_add_test(tc_misc, validatedatetime_can_detect_valid_strings);
  937     tcase_add_test(tc_misc, validatedatetime_can_detect_invalid_strings);
  938     tcase_add_test(tc_misc, validatedatetime_does_not_validate_numbers);
  939     tcase_add_test(tc_misc, defaultdecimals_controls_the_number_of_decimals);
  940     tcase_add_test(tc_misc, issametimeslot_knows_the_none_list);
  941     tcase_add_test(tc_misc, issametimeslot_handles_updates_before_the_entry_time);
  942     tcase_add_test(tc_misc, issametimeslot_knows_simple_slots);
  943     tcase_add_test(tc_misc, issametimeslot_knows_its_slots);
  944     tcase_add_test(tc_misc, getperiodseconds_knows_fixed_not_ongoing_periods);
  945     tcase_add_test(tc_misc, getperiodseconds_knows_dynamic_not_ongoing_periods);
  946     tcase_add_test(tc_misc, getperiodseconds_returns_zero_when_there_is_no_time_spent);
  947     tcase_add_test(tc_misc, getperiodseconds_knows_spent_ongoing_time);
  948     tcase_add_test(tc_misc, getestimates_has_error_handling);
  949     tcase_add_test(tc_misc, getestimates_has_a_crystal_ball);
  950     tcase_add_test(tc_misc, ishelprequest_knows_what_a_help_request_is);
  951     suite_add_tcase(s, tc_misc);
  952 }