"Fossies" - the Fresh Open Source Software Archive

Member "vnstat-2.9/tests/config_tests.c" (26 Jul 2021, 18742 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 "config_tests.c": 2.7_vs_2.8.

    1 #include "common.h"
    2 #include "vnstat_tests.h"
    3 #include "config_tests.h"
    4 #include "cfg.h"
    5 #include "cfgoutput.h"
    6 #include "ibw.h"
    7 
    8 START_TEST(validatecfg_default_all)
    9 {
   10     validatecfg(CT_All);
   11 }
   12 END_TEST
   13 
   14 START_TEST(validatecfg_default_cli)
   15 {
   16     validatecfg(CT_CLI);
   17 }
   18 END_TEST
   19 
   20 START_TEST(validatecfg_default_daemon)
   21 {
   22     validatecfg(CT_Daemon);
   23 }
   24 END_TEST
   25 
   26 START_TEST(validatecfg_default_image)
   27 {
   28     validatecfg(CT_Image);
   29 }
   30 END_TEST
   31 
   32 START_TEST(validatecfg_does_not_modify_valid_changes)
   33 {
   34     ck_assert_int_eq(cfg.listhours, LISTHOURS);
   35     cfg.listhours = 1;
   36     ck_assert_int_ne(cfg.listhours, LISTHOURS);
   37     validatecfg(CT_All);
   38     ck_assert_int_eq(cfg.listhours, 1);
   39 }
   40 END_TEST
   41 
   42 START_TEST(validatecfg_restores_invalid_values_back_to_default)
   43 {
   44     cfg.unitmode = 3;
   45     cfg.savestatus = 2;
   46     cfg.listhours = -1;
   47     suppress_output();
   48     validatecfg(CT_All);
   49     ck_assert_int_eq(cfg.unitmode, UNITMODE);
   50     ck_assert_int_eq(cfg.savestatus, SAVESTATUS);
   51     ck_assert_int_eq(cfg.listhours, LISTHOURS);
   52 }
   53 END_TEST
   54 
   55 START_TEST(validatecfg_can_tune_updateinterval_to_avoid_rollover_issues)
   56 {
   57     cfg.updateinterval = 60;
   58     cfg.maxbw = 1000;
   59     cfg.bwdetection = 1;
   60     suppress_output();
   61     validatecfg(CT_Daemon);
   62     ck_assert_int_ne(cfg.updateinterval, 60);
   63     ck_assert_int_eq(cfg.updateinterval, UPDATEINTERVAL);
   64 }
   65 END_TEST
   66 
   67 START_TEST(validatecfg_has_fallback_for_updateinterval_for_very_fast_interfaces)
   68 {
   69     cfg.updateinterval = 60;
   70     cfg.maxbw = 2000;
   71     cfg.bwdetection = 1;
   72     suppress_output();
   73     validatecfg(CT_Daemon);
   74     ck_assert_int_ne(cfg.updateinterval, 60);
   75     ck_assert_int_ne(cfg.updateinterval, UPDATEINTERVAL);
   76     ck_assert_int_eq(cfg.updateinterval, (UPDATEINTERVAL / 2));
   77 }
   78 END_TEST
   79 
   80 START_TEST(validatecfg_can_change_estimatestyle_for_images_depending_on_settings)
   81 {
   82     debug = 1;
   83     cfg.barshowsrate = 0;
   84     cfg.estimatebarvisible = 0;
   85     cfg.estimatestyle = 1;
   86     suppress_output();
   87 
   88     validatecfg(CT_Image);
   89     ck_assert_int_eq(cfg.barshowsrate, 0);
   90     ck_assert_int_eq(cfg.estimatebarvisible, 0);
   91     ck_assert_int_eq(cfg.estimatestyle, 1);
   92 
   93     cfg.barshowsrate = 1;
   94     cfg.estimatebarvisible = 0;
   95     validatecfg(CT_Image);
   96     ck_assert_int_eq(cfg.barshowsrate, 1);
   97     ck_assert_int_eq(cfg.estimatebarvisible, 0);
   98     ck_assert_int_eq(cfg.estimatestyle, 1);
   99 
  100     cfg.barshowsrate = 0;
  101     cfg.estimatebarvisible = 1;
  102     validatecfg(CT_Image);
  103     ck_assert_int_eq(cfg.barshowsrate, 0);
  104     ck_assert_int_eq(cfg.estimatebarvisible, 1);
  105     ck_assert_int_eq(cfg.estimatestyle, 1);
  106 
  107     cfg.barshowsrate = 1;
  108     cfg.estimatebarvisible = 1;
  109     validatecfg(CT_Image);
  110     ck_assert_int_eq(cfg.barshowsrate, 1);
  111     ck_assert_int_eq(cfg.estimatebarvisible, 1);
  112     ck_assert_int_eq(cfg.estimatestyle, 0);
  113 }
  114 END_TEST
  115 
  116 START_TEST(validatecfg_limits_5_minute_result_count_to_available_data_amount)
  117 {
  118     cfg.fiveminutehours = 26;
  119     cfg.fivegresultcount = 9001;
  120     suppress_output();
  121     validatecfg(CT_Image);
  122     ck_assert_int_eq(cfg.fiveminutehours, 26);
  123     ck_assert_int_eq(cfg.fivegresultcount, 312);
  124 }
  125 END_TEST
  126 
  127 START_TEST(validatecfg_limits_5_minute_result_count_to_minimum_usable)
  128 {
  129     cfg.fiveminutehours = 12;
  130     cfg.fivegresultcount = 9001;
  131     suppress_output();
  132     validatecfg(CT_Image);
  133     ck_assert_int_eq(cfg.fiveminutehours, 12);
  134     ck_assert_int_eq(cfg.fivegresultcount, 288);
  135 }
  136 END_TEST
  137 
  138 START_TEST(validatecfg_limits_5_minute_result_count_to_not_be_too_much)
  139 {
  140     cfg.fiveminutehours = 9001;
  141     cfg.fivegresultcount = 12345;
  142     suppress_output();
  143     validatecfg(CT_Image);
  144     ck_assert_int_eq(cfg.fiveminutehours, 9001);
  145     ck_assert_int_eq(cfg.fivegresultcount, FIVEGRESULTCOUNT);
  146 }
  147 END_TEST
  148 
  149 START_TEST(validatecfg_does_not_touch_5_minute_result_count_if_data_is_not_being_created)
  150 {
  151     cfg.fiveminutehours = 0;
  152     cfg.fivegresultcount = 1234;
  153     suppress_output();
  154     validatecfg(CT_Image);
  155     ck_assert_int_eq(cfg.fiveminutehours, 0);
  156     ck_assert_int_eq(cfg.fivegresultcount, 1234);
  157 }
  158 END_TEST
  159 
  160 START_TEST(validatecfg_is_not_stupid_with_5_minute_result_count_if_there_is_no_data_limit)
  161 {
  162     cfg.fiveminutehours = -1;
  163     cfg.fivegresultcount = 1242;
  164     suppress_output();
  165     validatecfg(CT_Image);
  166     ck_assert_int_eq(cfg.fiveminutehours, -1);
  167     ck_assert_int_eq(cfg.fivegresultcount, 1242);
  168 }
  169 END_TEST
  170 
  171 START_TEST(printcfgfile_default)
  172 {
  173     ck_assert_int_eq(ibwadd("name1", 1), 1);
  174     ck_assert_int_eq(ibwadd("name2", 2), 1);
  175     suppress_output();
  176     printcfgfile();
  177 }
  178 END_TEST
  179 
  180 START_TEST(printcfgfile_experimental)
  181 {
  182     cfg.experimental = 1;
  183     ck_assert_int_eq(ibwadd("name1", 1), 1);
  184     ck_assert_int_eq(ibwadd("name2", 2), 1);
  185     suppress_output();
  186     printcfgfile();
  187 }
  188 END_TEST
  189 
  190 START_TEST(loadcfg_included_default)
  191 {
  192     ck_assert_int_eq(loadcfg(CFGFILE, CT_All), 1);
  193 }
  194 END_TEST
  195 
  196 START_TEST(loadcfg_no_file)
  197 {
  198     ck_assert_int_eq(loadcfg("", CT_All), 1);
  199 }
  200 END_TEST
  201 
  202 START_TEST(loadcfg_nonexistent_file)
  203 {
  204     suppress_output();
  205     ck_assert_int_eq(loadcfg("_nosuchfile_", CT_All), 0);
  206 }
  207 END_TEST
  208 
  209 START_TEST(loadcfg_not_a_cfgfile)
  210 {
  211     ck_assert_int_eq(loadcfg("Makefile", CT_All), 1);
  212 }
  213 END_TEST
  214 
  215 START_TEST(ibwloadcfg_included_default)
  216 {
  217     ck_assert_int_eq(ibwloadcfg(CFGFILE), 1);
  218 }
  219 END_TEST
  220 
  221 START_TEST(ibwloadcfg_no_file)
  222 {
  223     ck_assert_int_eq(ibwloadcfg(""), 1);
  224 }
  225 END_TEST
  226 
  227 START_TEST(ibwloadcfg_nonexistent_file)
  228 {
  229     suppress_output();
  230     ck_assert_int_eq(ibwloadcfg("_nosuchfile_"), 0);
  231 }
  232 END_TEST
  233 
  234 START_TEST(ibwloadcfg_not_a_cfgfile)
  235 {
  236     ck_assert_int_eq(ibwloadcfg("Makefile"), 1);
  237 }
  238 END_TEST
  239 
  240 START_TEST(ibwget_with_empty_list_and_no_maxbw)
  241 {
  242     uint32_t limit;
  243     cfg.maxbw = 0;
  244     ibwflush();
  245     ck_assert_int_eq(ibwget("does_not_exist", &limit), 0);
  246 }
  247 END_TEST
  248 
  249 START_TEST(ibwget_with_empty_list_and_maxbw)
  250 {
  251     int ret;
  252     uint32_t limit;
  253     cfg.maxbw = 10;
  254     ibwflush();
  255     ret = ibwget("does_not_exist", &limit);
  256     ck_assert_int_eq(ret, 1);
  257     ck_assert_int_eq(limit, 10);
  258 }
  259 END_TEST
  260 
  261 START_TEST(ibwget_with_empty_list_gives_speed_instead_of_maxbw_if_available)
  262 {
  263     int ret;
  264     uint32_t limit;
  265 
  266     linuxonly;
  267 
  268     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  269     fake_sys_class_net("ethnotseen", 0, 0, 0, 0, 21);
  270 
  271     cfg.maxbw = 12;
  272     ibwflush();
  273     ret = ibwget("ethnotseen", &limit);
  274     ck_assert_int_eq(ret, 1);
  275     ck_assert_int_eq(limit, 21);
  276 }
  277 END_TEST
  278 
  279 START_TEST(ibwget_with_empty_list_gives_maxbw_for_tun_even_if_speed_is_available)
  280 {
  281     int ret;
  282     uint32_t limit;
  283 
  284     linuxonly;
  285 
  286     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  287     fake_sys_class_net("tun142", 0, 0, 0, 0, 10);
  288 
  289     cfg.maxbw = 12;
  290     ibwflush();
  291     ret = ibwget("tun142", &limit);
  292     ck_assert_int_eq(ret, 1);
  293     ck_assert_int_eq(limit, 12);
  294 }
  295 END_TEST
  296 
  297 START_TEST(ibwget_from_config)
  298 {
  299     int ret;
  300     uint32_t limit;
  301     ck_assert_int_eq(loadcfg(CFGFILE, CT_All), 1);
  302     ck_assert_int_eq(ibwloadcfg(CFGFILE), 1);
  303     cfg.maxbw = 10;
  304     ret = ibwget("ethnone", &limit);
  305     ck_assert_int_eq(ret, 1);
  306     ck_assert_int_eq(limit, 8);
  307 }
  308 END_TEST
  309 
  310 START_TEST(ibwget_from_config_ignores_detected_speed)
  311 {
  312     int ret;
  313     uint32_t limit;
  314 
  315     linuxonly;
  316 
  317     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  318     fake_sys_class_net("ethnone", 0, 0, 0, 0, 42);
  319 
  320     ck_assert_int_eq(loadcfg(CFGFILE, CT_All), 1);
  321     ck_assert_int_eq(ibwloadcfg(CFGFILE), 1);
  322     cfg.maxbw = 10;
  323     ret = ibwget("ethnone", &limit);
  324     ck_assert_int_eq(ret, 1);
  325     ck_assert_int_eq(limit, 8);
  326 }
  327 END_TEST
  328 
  329 START_TEST(ibwadd_single_success)
  330 {
  331     int ret;
  332     uint32_t limit;
  333     cfg.maxbw = 0;
  334     ck_assert_int_eq(ibwadd("newinterface", 1), 1);
  335     ret = ibwget("does_not_exist", &limit);
  336     ck_assert_int_eq(ret, 0);
  337     ck_assert_int_eq(limit, 0);
  338     ret = ibwget("newinterface", &limit);
  339     ck_assert_int_eq(ret, 1);
  340     ck_assert_int_eq(limit, 1);
  341 }
  342 END_TEST
  343 
  344 START_TEST(ibwadd_multi_success)
  345 {
  346     int ret;
  347     uint32_t limit;
  348     cfg.maxbw = 0;
  349     ck_assert_int_eq(ibwadd("name1", 1), 1);
  350     ck_assert_int_eq(ibwadd("name2", 2), 1);
  351     ck_assert_int_eq(ibwadd("name3", 3), 1);
  352     ck_assert_int_eq(ibwadd("name4", 2), 1);
  353     ck_assert_int_eq(ibwadd("name5", 1), 1);
  354     ck_assert_int_eq(ibwadd("name6", 10), 1);
  355 
  356     ret = ibwget("does_not_exist", &limit);
  357     ck_assert_int_eq(ret, 0);
  358     ck_assert_int_eq(limit, 0);
  359     ret = ibwget("name1", &limit);
  360     ck_assert_int_eq(ret, 1);
  361     ck_assert_int_eq(limit, 1);
  362     ret = ibwget("name3", &limit);
  363     ck_assert_int_eq(ret, 1);
  364     ck_assert_int_eq(limit, 3);
  365     ret = ibwget("name4", &limit);
  366     ck_assert_int_eq(ret, 1);
  367     ck_assert_int_eq(limit, 2);
  368     ret = ibwget("name6", &limit);
  369     ck_assert_int_eq(ret, 1);
  370     ck_assert_int_eq(limit, 10);
  371     ret = ibwget("name2", &limit);
  372     ck_assert_int_eq(ret, 1);
  373     ck_assert_int_eq(limit, 2);
  374     ret = ibwget("name5", &limit);
  375     ck_assert_int_eq(ret, 1);
  376     ck_assert_int_eq(limit, 1);
  377     ret = ibwget("name1", &limit);
  378     ck_assert_int_eq(ret, 1);
  379     ck_assert_int_eq(limit, 1);
  380     ret = ibwget("does_not_exist", &limit);
  381     ck_assert_int_eq(ret, 0);
  382     ck_assert_int_eq(limit, 0);
  383 }
  384 END_TEST
  385 
  386 START_TEST(ibwadd_update_success)
  387 {
  388     int ret;
  389     uint32_t limit;
  390     cfg.maxbw = 0;
  391     ck_assert_int_eq(ibwadd("name1", 1), 1);
  392     ck_assert_int_eq(ibwadd("name2", 2), 1);
  393     ret = ibwget("does_not_exist", &limit);
  394     ck_assert_int_eq(ret, 0);
  395     ck_assert_int_eq(limit, 0);
  396 
  397     ret = ibwget("name1", &limit);
  398     ck_assert_int_eq(ret, 1);
  399     ck_assert_int_eq(limit, 1);
  400     ret = ibwget("name2", &limit);
  401     ck_assert_int_eq(ret, 1);
  402     ck_assert_int_eq(limit, 2);
  403     ret = ibwget("does_not_exist", &limit);
  404     ck_assert_int_eq(ret, 0);
  405     ck_assert_int_eq(limit, 0);
  406 
  407     ck_assert_int_eq(ibwadd("name2", 5), 1);
  408     ck_assert_int_eq(ibwadd("name1", 4), 1);
  409 
  410     ret = ibwget("name1", &limit);
  411     ck_assert_int_eq(ret, 1);
  412     ck_assert_int_eq(limit, 4);
  413     ret = ibwget("name2", &limit);
  414     ck_assert_int_eq(ret, 1);
  415     ck_assert_int_eq(limit, 5);
  416     ret = ibwget("does_not_exist", &limit);
  417     ck_assert_int_eq(ret, 0);
  418     ck_assert_int_eq(limit, 0);
  419 }
  420 END_TEST
  421 
  422 START_TEST(ibwflush_success)
  423 {
  424     int ret;
  425     uint32_t limit;
  426     cfg.maxbw = 0;
  427     ck_assert_int_eq(ibwadd("name1", 1), 1);
  428     ck_assert_int_eq(ibwadd("name2", 2), 1);
  429 
  430     ret = ibwget("name1", &limit);
  431     ck_assert_int_eq(ret, 1);
  432     ck_assert_int_eq(limit, 1);
  433     ret = ibwget("name2", &limit);
  434     ck_assert_int_eq(ret, 1);
  435     ck_assert_int_eq(limit, 2);
  436     ret = ibwget("does_not_exist", &limit);
  437     ck_assert_int_eq(ret, 0);
  438     ck_assert_int_eq(limit, 0);
  439 
  440     ibwflush();
  441 
  442     ret = ibwget("name1", &limit);
  443     ck_assert_int_eq(ret, 0);
  444     ck_assert_int_eq(limit, 0);
  445     ret = ibwget("name2", &limit);
  446     ck_assert_int_eq(ret, 0);
  447     ck_assert_int_eq(limit, 0);
  448     ret = ibwget("does_not_exist", &limit);
  449     ck_assert_int_eq(ret, 0);
  450     ck_assert_int_eq(limit, 0);
  451 
  452     ck_assert_int_eq(ibwadd("name1", 1), 1);
  453     ck_assert_int_eq(ibwadd("name2", 2), 1);
  454 
  455     ret = ibwget("name1", &limit);
  456     ck_assert_int_eq(ret, 1);
  457     ck_assert_int_eq(limit, 1);
  458     ret = ibwget("name2", &limit);
  459     ck_assert_int_eq(ret, 1);
  460     ck_assert_int_eq(limit, 2);
  461     ret = ibwget("does_not_exist", &limit);
  462     ck_assert_int_eq(ret, 0);
  463     ck_assert_int_eq(limit, 0);
  464 }
  465 END_TEST
  466 
  467 START_TEST(ibwlist_empty)
  468 {
  469     suppress_output();
  470     ibwlist();
  471 }
  472 END_TEST
  473 
  474 START_TEST(ibwlist_filled)
  475 {
  476     cfg.maxbw = 0;
  477     ck_assert_int_eq(ibwadd("name1", 1), 1);
  478     ck_assert_int_eq(ibwadd("name2", 2), 1);
  479     suppress_output();
  480     ibwlist();
  481 }
  482 END_TEST
  483 
  484 START_TEST(extractcfgvalue_can_extract)
  485 {
  486     int ret;
  487     char value[32], cfgline[32];
  488 
  489     snprintf(cfgline, 32, "one 1");
  490     ret = extractcfgvalue(value, 32, cfgline, 3);
  491     ck_assert_int_eq(ret, 1);
  492     ck_assert_str_eq(value, "1");
  493 }
  494 END_TEST
  495 
  496 START_TEST(extractcfgvalue_can_really_extract)
  497 {
  498     int ret;
  499     char value[32], cfgline[32];
  500 
  501     snprintf(cfgline, 32, "one\t1");
  502     ret = extractcfgvalue(value, 32, cfgline, 3);
  503     ck_assert_int_eq(ret, 1);
  504     ck_assert_str_eq(value, "1");
  505 
  506     snprintf(cfgline, 32, "one\t\t1");
  507     ret = extractcfgvalue(value, 32, cfgline, 3);
  508     ck_assert_int_eq(ret, 1);
  509     ck_assert_str_eq(value, "1");
  510 
  511     snprintf(cfgline, 32, "one \t 1");
  512     ret = extractcfgvalue(value, 32, cfgline, 3);
  513     ck_assert_int_eq(ret, 1);
  514     ck_assert_str_eq(value, "1");
  515 
  516     snprintf(cfgline, 32, "one \t 1 \t2");
  517     ret = extractcfgvalue(value, 32, cfgline, 3);
  518     ck_assert_int_eq(ret, 4);
  519     ck_assert_str_eq(value, "1 \t2");
  520 
  521     snprintf(cfgline, 32, "one \"1\"");
  522     ret = extractcfgvalue(value, 32, cfgline, 3);
  523     ck_assert_int_eq(ret, 1);
  524     ck_assert_str_eq(value, "1");
  525 
  526     snprintf(cfgline, 32, "one\t\"1\"");
  527     ret = extractcfgvalue(value, 32, cfgline, 3);
  528     ck_assert_int_eq(ret, 1);
  529     ck_assert_str_eq(value, "1");
  530 
  531     snprintf(cfgline, 32, "one \t \"1\"");
  532     ret = extractcfgvalue(value, 32, cfgline, 3);
  533     ck_assert_int_eq(ret, 1);
  534     ck_assert_str_eq(value, "1");
  535 
  536     snprintf(cfgline, 32, "one \t \"1\" \t");
  537     ret = extractcfgvalue(value, 32, cfgline, 3);
  538     ck_assert_int_eq(ret, 1);
  539     ck_assert_str_eq(value, "1");
  540 
  541     snprintf(cfgline, 32, "one \t \"1\" \t2");
  542     ret = extractcfgvalue(value, 32, cfgline, 3);
  543     ck_assert_int_eq(ret, 1);
  544     ck_assert_str_eq(value, "1");
  545 
  546     snprintf(cfgline, 32, "one \t == \t \"1\" \t == well doh");
  547     ret = extractcfgvalue(value, 32, cfgline, 3);
  548     ck_assert_int_eq(ret, 1);
  549     ck_assert_str_eq(value, "1");
  550 }
  551 END_TEST
  552 
  553 START_TEST(extractcfgvalue_knows_when_not_to_extract)
  554 {
  555     int ret;
  556     char value[32], cfgline[32];
  557 
  558     snprintf(cfgline, 32, "one");
  559     ret = extractcfgvalue(value, 32, cfgline, 3);
  560     ck_assert_int_eq(ret, 0);
  561     ck_assert_str_eq(value, "");
  562 }
  563 END_TEST
  564 
  565 START_TEST(extractcfgvalue_really_knows_when_not_to_extract)
  566 {
  567     int ret;
  568     char value[32], cfgline[32];
  569 
  570     snprintf(cfgline, 32, "one   \t   ");
  571     ret = extractcfgvalue(value, 32, cfgline, 3);
  572     ck_assert_int_eq(ret, 0);
  573     ck_assert_str_eq(value, "");
  574 }
  575 END_TEST
  576 
  577 START_TEST(setcfgvalue_can_set_chars)
  578 {
  579     int ret;
  580     char target[32];
  581     struct cfgsetting cset[] = {{"unused", target, 0, 32, 0}};
  582 
  583     ret = setcfgvalue(&cset[0], "one", "unused");
  584     ck_assert_int_eq(ret, 1);
  585     ck_assert_str_eq(target, "one");
  586 
  587     ret = setcfgvalue(&cset[0], "1", "unused");
  588     ck_assert_int_eq(ret, 1);
  589     ck_assert_str_eq(target, "1");
  590 
  591     ret = setcfgvalue(&cset[0], "-", "unused");
  592     ck_assert_int_eq(ret, 1);
  593     ck_assert_str_eq(target, "-");
  594 
  595     ret = setcfgvalue(&cset[0], "qwe rty uio  ads", "unused");
  596     ck_assert_int_eq(ret, 1);
  597     ck_assert_str_eq(target, "qwe rty uio  ads");
  598 }
  599 END_TEST
  600 
  601 START_TEST(setcfgvalue_can_set_ints)
  602 {
  603     int ret, target;
  604     struct cfgsetting cset[] = {{"unused", 0, &target, 0, 0}};
  605 
  606     ret = setcfgvalue(&cset[0], "1", "unused");
  607     ck_assert_int_eq(ret, 1);
  608     ck_assert_int_eq(target, 1);
  609 
  610     ret = setcfgvalue(&cset[0], "123", "unused");
  611     ck_assert_int_eq(ret, 1);
  612     ck_assert_int_eq(target, 123);
  613 
  614     ret = setcfgvalue(&cset[0], "-1", "unused");
  615     ck_assert_int_eq(ret, 1);
  616     ck_assert_int_eq(target, -1);
  617 
  618     ret = setcfgvalue(&cset[0], "-321", "unused");
  619     ck_assert_int_eq(ret, 1);
  620     ck_assert_int_eq(target, -321);
  621 
  622     ret = setcfgvalue(&cset[0], "0", "unused");
  623     ck_assert_int_eq(ret, 1);
  624     ck_assert_int_eq(target, 0);
  625 }
  626 END_TEST
  627 
  628 START_TEST(setcfgvalue_does_not_exceed_char_limit)
  629 {
  630     int ret;
  631     char target[10];
  632     struct cfgsetting cset[] = {{"unused", target, 0, 5, 0}};
  633 
  634     ret = setcfgvalue(&cset[0], "one", "unused");
  635     ck_assert_int_eq(ret, 1);
  636     ck_assert_str_eq(target, "one");
  637 
  638     ret = setcfgvalue(&cset[0], "12345", "unused");
  639     ck_assert_int_eq(ret, 1);
  640     ck_assert_str_eq(target, "1234");
  641 
  642     ret = setcfgvalue(&cset[0], "12  5", "unused");
  643     ck_assert_int_eq(ret, 1);
  644     ck_assert_str_eq(target, "12  ");
  645 }
  646 END_TEST
  647 
  648 START_TEST(setcfgvalue_can_do_nothing)
  649 {
  650     int ret;
  651     struct cfgsetting cset[] = {{"unused", 0, 0, 0, 0}};
  652 
  653     ret = setcfgvalue(&cset[0], "nothing", "unused");
  654     ck_assert_int_eq(ret, 0);
  655 }
  656 END_TEST
  657 
  658 START_TEST(configlocale_does_not_crash)
  659 {
  660 
  661     unsetenv("LC_ALL");
  662     snprintf(cfg.locale, 32, "en_US");
  663     configlocale();
  664     snprintf(cfg.locale, 32, "-");
  665     configlocale();
  666 
  667     setenv("LC_ALL", "en_US", 1);
  668     snprintf(cfg.locale, 32, "en_US");
  669     configlocale();
  670     ck_assert_int_eq(cfg.utflocale, 0);
  671     snprintf(cfg.locale, 32, "-");
  672     configlocale();
  673     ck_assert_int_eq(cfg.utflocale, 0);
  674 
  675     setenv("LC_ALL", "en_US.UTF-8", 1);
  676     snprintf(cfg.locale, 32, "en_US");
  677     configlocale();
  678     ck_assert_int_eq(cfg.utflocale, 1);
  679     snprintf(cfg.locale, 32, "-");
  680     configlocale();
  681     ck_assert_int_eq(cfg.utflocale, 1);
  682 }
  683 END_TEST
  684 
  685 void add_config_tests(Suite *s)
  686 {
  687     TCase *tc_config = tcase_create("Config");
  688     tcase_add_checked_fixture(tc_config, setup, teardown);
  689     tcase_add_unchecked_fixture(tc_config, setup, teardown);
  690     tcase_add_test(tc_config, validatecfg_default_all);
  691     tcase_add_test(tc_config, validatecfg_default_cli);
  692     tcase_add_test(tc_config, validatecfg_default_daemon);
  693     tcase_add_test(tc_config, validatecfg_default_image);
  694     tcase_add_test(tc_config, validatecfg_does_not_modify_valid_changes);
  695     tcase_add_test(tc_config, validatecfg_restores_invalid_values_back_to_default);
  696     tcase_add_test(tc_config, validatecfg_can_tune_updateinterval_to_avoid_rollover_issues);
  697     tcase_add_test(tc_config, validatecfg_has_fallback_for_updateinterval_for_very_fast_interfaces);
  698     tcase_add_test(tc_config, validatecfg_can_change_estimatestyle_for_images_depending_on_settings);
  699     tcase_add_test(tc_config, validatecfg_limits_5_minute_result_count_to_available_data_amount);
  700     tcase_add_test(tc_config, validatecfg_limits_5_minute_result_count_to_minimum_usable);
  701     tcase_add_test(tc_config, validatecfg_limits_5_minute_result_count_to_not_be_too_much);
  702     tcase_add_test(tc_config, validatecfg_does_not_touch_5_minute_result_count_if_data_is_not_being_created);
  703     tcase_add_test(tc_config, validatecfg_is_not_stupid_with_5_minute_result_count_if_there_is_no_data_limit);
  704     tcase_add_test(tc_config, printcfgfile_default);
  705     tcase_add_test(tc_config, printcfgfile_experimental);
  706     tcase_add_test(tc_config, loadcfg_included_default);
  707     tcase_add_test(tc_config, loadcfg_no_file);
  708     tcase_add_test(tc_config, loadcfg_nonexistent_file);
  709     tcase_add_test(tc_config, loadcfg_not_a_cfgfile);
  710     tcase_add_test(tc_config, ibwloadcfg_included_default);
  711     tcase_add_test(tc_config, ibwloadcfg_no_file);
  712     tcase_add_test(tc_config, ibwloadcfg_nonexistent_file);
  713     tcase_add_test(tc_config, ibwloadcfg_not_a_cfgfile);
  714     tcase_add_test(tc_config, ibwget_with_empty_list_and_no_maxbw);
  715     tcase_add_test(tc_config, ibwget_with_empty_list_and_maxbw);
  716     tcase_add_test(tc_config, ibwget_with_empty_list_gives_speed_instead_of_maxbw_if_available);
  717     tcase_add_test(tc_config, ibwget_with_empty_list_gives_maxbw_for_tun_even_if_speed_is_available);
  718     tcase_add_test(tc_config, ibwget_from_config);
  719     tcase_add_test(tc_config, ibwget_from_config_ignores_detected_speed);
  720     tcase_add_test(tc_config, ibwadd_single_success);
  721     tcase_add_test(tc_config, ibwadd_multi_success);
  722     tcase_add_test(tc_config, ibwadd_update_success);
  723     tcase_add_test(tc_config, ibwflush_success);
  724     tcase_add_test(tc_config, ibwlist_empty);
  725     tcase_add_test(tc_config, ibwlist_filled);
  726     tcase_add_test(tc_config, extractcfgvalue_can_extract);
  727     tcase_add_test(tc_config, extractcfgvalue_can_really_extract);
  728     tcase_add_test(tc_config, extractcfgvalue_knows_when_not_to_extract);
  729     tcase_add_test(tc_config, extractcfgvalue_really_knows_when_not_to_extract);
  730     tcase_add_test(tc_config, setcfgvalue_can_set_chars);
  731     tcase_add_test(tc_config, setcfgvalue_can_set_ints);
  732     tcase_add_test(tc_config, setcfgvalue_does_not_exceed_char_limit);
  733     tcase_add_test(tc_config, setcfgvalue_can_do_nothing);
  734     tcase_add_test(tc_config, configlocale_does_not_crash);
  735     suite_add_tcase(s, tc_config);
  736 }