"Fossies" - the Fresh Open Source Software Archive

Member "vnstat-2.9/tests/cli_tests.c" (17 Jan 2022, 36587 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.

    1 #include "common.h"
    2 #include "vnstat_tests.h"
    3 #include "vnstat_func.h"
    4 #include "cfg.h"
    5 #include "dbsql.h"
    6 #include "cli_tests.h"
    7 
    8 START_TEST(vnstat_can_init_params)
    9 {
   10     PARAMS p;
   11 
   12     initparams(&p);
   13 }
   14 END_TEST
   15 
   16 START_TEST(vnstat_showhelp_does_not_crash)
   17 {
   18     PARAMS p;
   19 
   20     initparams(&p);
   21     strncpy_nt(p.definterface, "ethgone", 32);
   22     suppress_output();
   23     showhelp(&p);
   24 }
   25 END_TEST
   26 
   27 START_TEST(vnstat_showlonghelp_does_not_crash)
   28 {
   29     PARAMS p;
   30 
   31     initparams(&p);
   32     strncpy_nt(p.definterface, "ethgone", 32);
   33     suppress_output();
   34     showlonghelp(&p);
   35 }
   36 END_TEST
   37 
   38 START_TEST(vnstat_handlers_do_nothing_by_default)
   39 {
   40     PARAMS p, b;
   41 
   42     initparams(&p);
   43     memcpy(&b, &p, sizeof(PARAMS));
   44     handleremoveinterface(&p);
   45     handlerenameinterface(&p);
   46     handleaddinterface(&p);
   47     handlesetalias(&p);
   48     handletrafficmeters(&p);
   49     ck_assert_int_eq(memcmp(&p, &b, sizeof(PARAMS)), 0);
   50 }
   51 END_TEST
   52 
   53 START_TEST(vnstat_handletrafficmeters_exists_when_interface_is_not_available)
   54 {
   55     PARAMS p;
   56 
   57     ck_assert_int_eq(remove_directory(TESTDIR), 1);
   58     initparams(&p);
   59     strncpy_nt(cfg.iface, "ethfoo", 32);
   60     strncpy_nt(p.interface, "default", 32);
   61     strncpy_nt(p.definterface, cfg.iface, 32);
   62     p.livetraffic = 1;
   63     suppress_output();
   64 
   65     handletrafficmeters(&p);
   66 }
   67 END_TEST
   68 
   69 START_TEST(vnstat_handleremoveinterface_exits_if_no_interface_has_been_specified)
   70 {
   71     PARAMS p;
   72     initparams(&p);
   73     p.removeiface = 1;
   74 
   75     suppress_output();
   76     handleremoveinterface(&p);
   77 }
   78 END_TEST
   79 
   80 START_TEST(vnstat_handleremoveinterface_exits_if_given_interface_does_not_exist)
   81 {
   82     int ret;
   83     PARAMS p;
   84 
   85     initparams(&p);
   86     p.removeiface = 1;
   87     p.defaultiface = 0;
   88     strncpy_nt(p.interface, "unknown", 32);
   89 
   90     ret = db_open_rw(1);
   91     ck_assert_int_eq(ret, 1);
   92 
   93     ret = db_addinterface("known");
   94     ck_assert_int_eq(ret, 1);
   95 
   96     suppress_output();
   97     handleremoveinterface(&p);
   98 }
   99 END_TEST
  100 
  101 START_TEST(vnstat_handleremoveinterface_exits_if_force_is_not_used)
  102 {
  103     int ret;
  104     PARAMS p;
  105 
  106     initparams(&p);
  107     p.removeiface = 1;
  108     p.defaultiface = 0;
  109     p.force = 0;
  110     strncpy_nt(p.interface, "known", 32);
  111 
  112     ret = db_open_rw(1);
  113     ck_assert_int_eq(ret, 1);
  114 
  115     ret = db_addinterface("known");
  116     ck_assert_int_eq(ret, 1);
  117 
  118     suppress_output();
  119     handleremoveinterface(&p);
  120 }
  121 END_TEST
  122 
  123 START_TEST(vnstat_handleremoveinterface_removes_interface)
  124 {
  125     int ret;
  126     PARAMS p;
  127 
  128     initparams(&p);
  129     p.removeiface = 1;
  130     p.defaultiface = 0;
  131     p.force = 1;
  132     strncpy_nt(p.interface, "known", 32);
  133 
  134     ret = db_open_rw(1);
  135     ck_assert_int_eq(ret, 1);
  136 
  137     ret = db_addinterface("known");
  138     ck_assert_int_eq(ret, 1);
  139 
  140     ret = (int)db_getinterfacecountbyname("known");
  141     ck_assert_int_eq(ret, 1);
  142 
  143     suppress_output();
  144     handleremoveinterface(&p);
  145 
  146     ret = (int)db_getinterfacecountbyname("known");
  147     ck_assert_int_eq(ret, 0);
  148 }
  149 END_TEST
  150 
  151 START_TEST(vnstat_handlerenameinterface_exits_if_no_interface_has_been_specified)
  152 {
  153     PARAMS p;
  154     initparams(&p);
  155     p.renameiface = 1;
  156 
  157     suppress_output();
  158     handlerenameinterface(&p);
  159 }
  160 END_TEST
  161 
  162 START_TEST(vnstat_handlerenameinterface_exits_if_new_interface_name_is_not_given)
  163 {
  164     PARAMS p;
  165     initparams(&p);
  166     p.renameiface = 1;
  167     p.defaultiface = 0;
  168 
  169     suppress_output();
  170     handlerenameinterface(&p);
  171 }
  172 END_TEST
  173 
  174 START_TEST(vnstat_handlerenameinterface_exits_if_given_interface_does_not_exist)
  175 {
  176     int ret;
  177     PARAMS p;
  178 
  179     initparams(&p);
  180     p.renameiface = 1;
  181     p.defaultiface = 0;
  182     strncpy_nt(p.interface, "oldname", 32);
  183     strncpy_nt(p.newifname, "newname", 32);
  184 
  185     ret = db_open_rw(1);
  186     ck_assert_int_eq(ret, 1);
  187 
  188     ret = db_addinterface("somename");
  189     ck_assert_int_eq(ret, 1);
  190 
  191     suppress_output();
  192     handlerenameinterface(&p);
  193 }
  194 END_TEST
  195 
  196 START_TEST(vnstat_handlerenameinterface_exits_if_new_interface_name_already_exist)
  197 {
  198     int ret;
  199     PARAMS p;
  200 
  201     initparams(&p);
  202     p.renameiface = 1;
  203     p.defaultiface = 0;
  204     strncpy_nt(p.interface, "oldname", 32);
  205     strncpy_nt(p.newifname, "newname", 32);
  206 
  207     ret = db_open_rw(1);
  208     ck_assert_int_eq(ret, 1);
  209 
  210     ret = db_addinterface("oldname");
  211     ck_assert_int_eq(ret, 1);
  212 
  213     ret = db_addinterface("newname");
  214     ck_assert_int_eq(ret, 1);
  215 
  216     suppress_output();
  217     handlerenameinterface(&p);
  218 }
  219 END_TEST
  220 
  221 START_TEST(vnstat_handlerenameinterface_exits_if_force_is_not_used)
  222 {
  223     int ret;
  224     PARAMS p;
  225 
  226     initparams(&p);
  227 
  228     p.renameiface = 1;
  229     p.defaultiface = 0;
  230     p.force = 0;
  231     strncpy_nt(p.interface, "oldname", 32);
  232     strncpy_nt(p.newifname, "newname", 32);
  233 
  234     ret = db_open_rw(1);
  235     ck_assert_int_eq(ret, 1);
  236 
  237     ret = db_addinterface("oldname");
  238     ck_assert_int_eq(ret, 1);
  239 
  240     suppress_output();
  241     handlerenameinterface(&p);
  242 }
  243 END_TEST
  244 
  245 START_TEST(vnstat_handlerenameinterface_renames_interface)
  246 {
  247     int ret;
  248     PARAMS p;
  249 
  250     initparams(&p);
  251     p.renameiface = 1;
  252     p.defaultiface = 0;
  253     p.force = 1;
  254     strncpy_nt(p.interface, "oldname", 32);
  255     strncpy_nt(p.newifname, "newname", 32);
  256 
  257     ret = db_open_rw(1);
  258     ck_assert_int_eq(ret, 1);
  259 
  260     ret = db_addinterface("oldname");
  261     ck_assert_int_eq(ret, 1);
  262 
  263     ret = (int)db_getinterfacecountbyname("oldname");
  264     ck_assert_int_eq(ret, 1);
  265 
  266     ret = (int)db_getinterfacecountbyname("newname");
  267     ck_assert_int_eq(ret, 0);
  268 
  269     suppress_output();
  270     handlerenameinterface(&p);
  271 
  272     ret = (int)db_getinterfacecountbyname("oldname");
  273     ck_assert_int_eq(ret, 0);
  274 
  275     ret = (int)db_getinterfacecountbyname("newname");
  276     ck_assert_int_eq(ret, 1);
  277 }
  278 END_TEST
  279 
  280 START_TEST(vnstat_handleaddinterface_exits_if_no_interface_has_been_specified)
  281 {
  282     PARAMS p;
  283     initparams(&p);
  284     p.addiface = 1;
  285 
  286     suppress_output();
  287     handleaddinterface(&p);
  288 }
  289 END_TEST
  290 
  291 START_TEST(vnstat_handleaddinterface_exits_if_interface_already_exist_in_database)
  292 {
  293     int ret;
  294     PARAMS p;
  295 
  296     initparams(&p);
  297     p.addiface = 1;
  298     p.defaultiface = 0;
  299     strncpy_nt(p.interface, "newiface", 32);
  300 
  301     ret = db_open_rw(1);
  302     ck_assert_int_eq(ret, 1);
  303 
  304     ret = db_addinterface("newiface");
  305     ck_assert_int_eq(ret, 1);
  306 
  307     suppress_output();
  308     handleaddinterface(&p);
  309 }
  310 END_TEST
  311 
  312 START_TEST(vnstat_handleaddinterface_exits_if_interface_does_not_exist)
  313 {
  314     int ret;
  315     PARAMS p;
  316 
  317     initparams(&p);
  318     p.addiface = 1;
  319     p.defaultiface = 0;
  320     strncpy_nt(p.interface, "newiface", 32);
  321 
  322     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  323     fake_proc_net_dev("w", "notnewiface", 0, 0, 0, 0);
  324 
  325     ret = db_open_rw(1);
  326     ck_assert_int_eq(ret, 1);
  327 
  328     suppress_output();
  329     handleaddinterface(&p);
  330 }
  331 END_TEST
  332 
  333 START_TEST(vnstat_handleaddinterface_adds_interface)
  334 {
  335     int ret;
  336     PARAMS p;
  337 
  338     initparams(&p);
  339     p.addiface = 1;
  340     p.defaultiface = 0;
  341     cfg.spacecheck = 0;
  342     strncpy_nt(p.interface, "newiface", 32);
  343 
  344     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  345     fake_proc_net_dev("w", "newiface", 0, 0, 0, 0);
  346 
  347     ret = db_open_rw(1);
  348     ck_assert_int_eq(ret, 1);
  349 
  350     ret = (int)db_getinterfacecountbyname("newiface");
  351     ck_assert_int_eq(ret, 0);
  352 
  353     suppress_output();
  354     handleaddinterface(&p);
  355 
  356     ret = (int)db_getinterfacecountbyname("newiface");
  357     ck_assert_int_eq(ret, 1);
  358 }
  359 END_TEST
  360 
  361 START_TEST(vnstat_handleaddinterface_can_be_forced_to_add_interface_that_does_not_exist)
  362 {
  363     int ret;
  364     PARAMS p;
  365 
  366     initparams(&p);
  367     p.addiface = 1;
  368     p.defaultiface = 0;
  369     p.force = 1;
  370     cfg.spacecheck = 0;
  371     strncpy_nt(p.interface, "newiface", 32);
  372 
  373     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  374     fake_proc_net_dev("w", "notnewiface", 0, 0, 0, 0);
  375 
  376     ret = db_open_rw(1);
  377     ck_assert_int_eq(ret, 1);
  378 
  379     ret = (int)db_getinterfacecountbyname("newiface");
  380     ck_assert_int_eq(ret, 0);
  381 
  382     suppress_output();
  383     handleaddinterface(&p);
  384 
  385     ret = (int)db_getinterfacecountbyname("newiface");
  386     ck_assert_int_eq(ret, 1);
  387 }
  388 END_TEST
  389 
  390 START_TEST(vnstat_handlesetalias_exits_if_no_interface_has_been_specified)
  391 {
  392     PARAMS p;
  393     initparams(&p);
  394     p.setalias = 1;
  395 
  396     suppress_output();
  397     handlesetalias(&p);
  398 }
  399 END_TEST
  400 
  401 START_TEST(vnstat_handlesetalias_exits_if_given_interface_does_not_exist)
  402 {
  403     int ret;
  404     PARAMS p;
  405     initparams(&p);
  406     p.setalias = 1;
  407     p.defaultiface = 0;
  408     strncpy_nt(p.interface, "ethiface", 32);
  409 
  410     ret = db_open_rw(1);
  411     ck_assert_int_eq(ret, 1);
  412 
  413     ret = db_addinterface("somename");
  414     ck_assert_int_eq(ret, 1);
  415 
  416     suppress_output();
  417     handlesetalias(&p);
  418 }
  419 END_TEST
  420 
  421 START_TEST(vnstat_handlesetalias_sets_alias)
  422 {
  423     int ret;
  424     PARAMS p;
  425     interfaceinfo info;
  426     initparams(&p);
  427     p.setalias = 1;
  428     p.defaultiface = 0;
  429     strncpy_nt(p.interface, "ethiface", 32);
  430     strncpy_nt(p.alias, "The Internet", 32);
  431 
  432     ret = db_open_rw(1);
  433     ck_assert_int_eq(ret, 1);
  434 
  435     ret = db_addinterface("ethiface");
  436     ck_assert_int_eq(ret, 1);
  437 
  438     ret = db_getinterfaceinfo("ethiface", &info);
  439     ck_assert_int_eq(ret, 1);
  440     ck_assert_str_eq(info.alias, "");
  441 
  442     suppress_output();
  443     handlesetalias(&p);
  444 
  445     ret = db_getinterfaceinfo("ethiface", &info);
  446     ck_assert_int_eq(ret, 1);
  447     ck_assert_str_eq(info.alias, "The Internet");
  448 }
  449 END_TEST
  450 
  451 START_TEST(vnstat_handlesetalias_clears_alias)
  452 {
  453     int ret;
  454     PARAMS p;
  455     interfaceinfo info;
  456     initparams(&p);
  457     p.setalias = 1;
  458     p.defaultiface = 0;
  459     strncpy_nt(p.interface, "ethiface", 32);
  460     strncpy_nt(p.alias, "", 32);
  461 
  462     ret = db_open_rw(1);
  463     ck_assert_int_eq(ret, 1);
  464 
  465     ret = db_addinterface("ethiface");
  466     ck_assert_int_eq(ret, 1);
  467 
  468     ret = db_setalias("ethiface", "Local network");
  469     ck_assert_int_eq(ret, 1);
  470 
  471     ret = db_getinterfaceinfo("ethiface", &info);
  472     ck_assert_int_eq(ret, 1);
  473     ck_assert_str_eq(info.alias, "Local network");
  474 
  475     suppress_output();
  476     handlesetalias(&p);
  477 
  478     ret = db_getinterfaceinfo("ethiface", &info);
  479     ck_assert_int_eq(ret, 1);
  480     ck_assert_str_eq(info.alias, "");
  481 }
  482 END_TEST
  483 
  484 START_TEST(vnstat_handleaddinterface_can_also_set_alias_after_adding_interface)
  485 {
  486     int ret;
  487     PARAMS p;
  488     interfaceinfo info;
  489 
  490     initparams(&p);
  491     p.addiface = 1;
  492     p.setalias = 1;
  493     p.defaultiface = 0;
  494     cfg.spacecheck = 0;
  495     strncpy_nt(p.interface, "newiface", 32);
  496     strncpy_nt(p.alias, "The Interface", 32);
  497 
  498     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  499     fake_proc_net_dev("w", "newiface", 0, 0, 0, 0);
  500 
  501     ret = db_open_rw(1);
  502     ck_assert_int_eq(ret, 1);
  503 
  504     ret = (int)db_getinterfacecountbyname("newiface");
  505     ck_assert_int_eq(ret, 0);
  506 
  507     suppress_output();
  508     handleaddinterface(&p);
  509 
  510     ret = (int)db_getinterfacecountbyname("newiface");
  511     ck_assert_int_eq(ret, 1);
  512 
  513     ret = db_getinterfaceinfo("newiface", &info);
  514     ck_assert_int_eq(ret, 1);
  515     ck_assert_str_eq(info.alias, "The Interface");
  516 }
  517 END_TEST
  518 
  519 START_TEST(vnstat_handletrafficmeters_exits_when_interface_is_not_available)
  520 {
  521     PARAMS p;
  522     initparams(&p);
  523     p.traffic = 1;
  524     p.defaultiface = 1;
  525     strncpy_nt(p.interface, "someiface", 32);
  526 
  527     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  528     fake_proc_net_dev("w", "otheriface", 0, 0, 0, 0);
  529 
  530     suppress_output();
  531     handletrafficmeters(&p);
  532 }
  533 END_TEST
  534 
  535 START_TEST(vnstat_handletrafficmeters_exits_when_interface_is_not_available_with_configuration_tips)
  536 {
  537     PARAMS p;
  538     initparams(&p);
  539     p.traffic = 1;
  540     p.defaultiface = 1;
  541     strncpy_nt(p.interface, "someiface", 32);
  542     strncpy_nt(cfg.cfgfile, "I_do_not_have_a_config_file_here.something", 512);
  543 
  544     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  545     fake_proc_net_dev("w", "otheriface", 0, 0, 0, 0);
  546 
  547     suppress_output();
  548     handletrafficmeters(&p);
  549 }
  550 END_TEST
  551 
  552 START_TEST(vnstat_handletrafficmeters_exits_when_specific_interface_is_not_available)
  553 {
  554     PARAMS p;
  555     initparams(&p);
  556     p.traffic = 1;
  557     p.defaultiface = 0;
  558     strncpy_nt(p.interface, "someiface", 32);
  559 
  560     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  561     fake_proc_net_dev("w", "otheriface", 0, 0, 0, 0);
  562 
  563     suppress_output();
  564     handletrafficmeters(&p);
  565 }
  566 END_TEST
  567 
  568 START_TEST(vnstat_handletrafficmeters_can_calculate_traffic)
  569 {
  570     PARAMS p;
  571     initparams(&p);
  572     p.traffic = 1;
  573     p.defaultiface = 0;
  574     cfg.qmode = 1;
  575     cfg.sampletime = 0;
  576     strncpy_nt(p.interface, "someiface", 32);
  577 
  578     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  579     fake_proc_net_dev("w", "someiface", 0, 0, 0, 0);
  580 
  581     suppress_output();
  582     handletrafficmeters(&p);
  583 }
  584 END_TEST
  585 
  586 START_TEST(vnstat_handletrafficmeters_can_handle_interface_merge_using_first_interface)
  587 {
  588     PARAMS p;
  589     initparams(&p);
  590     p.traffic = 1;
  591     p.defaultiface = 0;
  592     cfg.qmode = 1;
  593     cfg.sampletime = 0;
  594     strncpy_nt(p.interface, "someiface+anotherinterface", 32);
  595 
  596     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  597     fake_proc_net_dev("w", "someiface", 0, 0, 0, 0);
  598 
  599     suppress_output();
  600     handletrafficmeters(&p);
  601 }
  602 END_TEST
  603 
  604 START_TEST(vnstat_handletrafficmeters_can_calculate_traffic_and_output_json)
  605 {
  606     PARAMS p;
  607     initparams(&p);
  608     p.traffic = 1;
  609     p.defaultiface = 0;
  610     cfg.qmode = 10;
  611     cfg.sampletime = 0;
  612     strncpy_nt(p.interface, "someiface", 32);
  613 
  614     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  615     fake_proc_net_dev("w", "someiface", 0, 0, 0, 0);
  616 
  617     suppress_output();
  618     handletrafficmeters(&p);
  619 }
  620 END_TEST
  621 
  622 START_TEST(vnstat_handletrafficmeters_livetraffic_does_not_crash)
  623 {
  624     PARAMS p;
  625     initparams(&p);
  626     p.livetraffic = 1;
  627     p.defaultiface = 0;
  628     cfg.qmode = 1;
  629     cfg.ostyle = 0;
  630     cfg.sampletime = 0;
  631     strncpy_nt(p.interface, "someiface", 32);
  632 
  633     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  634     fake_proc_net_dev("w", "someiface", 0, 0, 0, 0);
  635 
  636     suppress_output();
  637     handletrafficmeters(&p);
  638 }
  639 END_TEST
  640 
  641 START_TEST(vnstat_handletrafficmeters_livetraffic_does_not_crash_with_interface_merge)
  642 {
  643     PARAMS p;
  644     initparams(&p);
  645     p.livetraffic = 1;
  646     p.defaultiface = 0;
  647     cfg.qmode = 1;
  648     cfg.ostyle = 0;
  649     cfg.sampletime = 0;
  650     strncpy_nt(p.interface, "someiface+anotherinterface", 32);
  651 
  652     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  653     fake_proc_net_dev("w", "someiface", 0, 0, 0, 0);
  654 
  655     suppress_output();
  656     handletrafficmeters(&p);
  657 }
  658 END_TEST
  659 
  660 START_TEST(vnstat_handletrafficmeters_livetraffic_does_not_crash_with_json)
  661 {
  662     PARAMS p;
  663     initparams(&p);
  664     p.livetraffic = 1;
  665     p.defaultiface = 0;
  666     cfg.qmode = 10;
  667     cfg.sampletime = 0;
  668     strncpy_nt(p.interface, "someiface", 32);
  669 
  670     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  671     fake_proc_net_dev("w", "someiface", 0, 0, 0, 0);
  672 
  673     suppress_output();
  674     handletrafficmeters(&p);
  675 }
  676 END_TEST
  677 
  678 START_TEST(handleifselection_does_nothing_when_interface_has_already_been_selected)
  679 {
  680     PARAMS p;
  681     initparams(&p);
  682 
  683     p.defaultiface = 0;
  684     strncpy_nt(p.interface, "myiface", 32);
  685 
  686     handleifselection(&p);
  687 
  688     ck_assert_str_eq(p.interface, "myiface");
  689 }
  690 END_TEST
  691 
  692 START_TEST(handleifselection_selects_default_interface_if_field_is_filled)
  693 {
  694     PARAMS p;
  695     initparams(&p);
  696 
  697     p.defaultiface = 1;
  698     strncpy_nt(p.interface, "default", 32);
  699     strncpy_nt(p.definterface, "myiface", 32);
  700 
  701     handleifselection(&p);
  702 
  703     ck_assert_str_eq(p.interface, "myiface");
  704     ck_assert_str_eq(p.definterface, "myiface");
  705 }
  706 END_TEST
  707 
  708 START_TEST(handleifselection_exits_when_no_suitable_interface_is_available_for_query)
  709 {
  710     int ret;
  711     PARAMS p;
  712     initparams(&p);
  713 
  714     p.defaultiface = 1;
  715     p.query = 1;
  716     strncpy_nt(p.interface, "default", 32);
  717 
  718     ret = db_open_rw(1);
  719     ck_assert_int_eq(ret, 1);
  720 
  721     /* no interfaces added to database */
  722 
  723     suppress_output();
  724     handleifselection(&p);
  725 }
  726 END_TEST
  727 
  728 START_TEST(handleifselection_selects_only_interface_from_database_for_query)
  729 {
  730     int ret;
  731     PARAMS p;
  732     initparams(&p);
  733 
  734     p.defaultiface = 1;
  735     p.query = 1;
  736     strncpy_nt(p.interface, "default", 32);
  737 
  738     ret = db_open_rw(1);
  739     ck_assert_int_eq(ret, 1);
  740 
  741     ret = db_addinterface("lettherebemagic");
  742     ck_assert_int_eq(ret, 1);
  743 
  744     handleifselection(&p);
  745 
  746     ck_assert_str_eq(p.interface, "lettherebemagic");
  747 }
  748 END_TEST
  749 
  750 START_TEST(handleifselection_selects_highest_traffic_interface_from_database_for_query)
  751 {
  752     int ret;
  753     PARAMS p;
  754     initparams(&p);
  755 
  756     p.defaultiface = 1;
  757     p.query = 1;
  758     strncpy_nt(p.interface, "default", 32);
  759 
  760     ret = db_open_rw(1);
  761     ck_assert_int_eq(ret, 1);
  762 
  763     ret = db_addinterface("ethslow");
  764     ck_assert_int_eq(ret, 1);
  765 
  766     ret = db_addinterface("ethfast");
  767     ck_assert_int_eq(ret, 1);
  768 
  769     ret = db_addinterface("ethone");
  770     ck_assert_int_eq(ret, 1);
  771 
  772     ret = db_settotal("ethslow", 100, 200);
  773     ck_assert_int_eq(ret, 1);
  774 
  775     ret = db_settotal("ethfast", 80, 9001);
  776     ck_assert_int_eq(ret, 1);
  777 
  778     ret = db_settotal("ethone", 1, 1);
  779     ck_assert_int_eq(ret, 1);
  780 
  781     handleifselection(&p);
  782 
  783     /* interface with most traffic in database is selected */
  784     ck_assert_str_eq(p.interface, "ethfast");
  785 }
  786 END_TEST
  787 
  788 START_TEST(handleifselection_exits_when_no_suitable_interface_is_available_for_traffic)
  789 {
  790     PARAMS p;
  791     initparams(&p);
  792 
  793     p.defaultiface = 1;
  794     p.query = 0;
  795     p.traffic = 1;
  796     strncpy_nt(p.interface, "default", 32);
  797 
  798     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  799 
  800     /* database not available and interface listing provides nothing */
  801 
  802     suppress_output();
  803     handleifselection(&p);
  804 }
  805 END_TEST
  806 
  807 START_TEST(handleifselection_can_select_interface_without_database_for_traffic)
  808 {
  809     PARAMS p;
  810     initparams(&p);
  811 
  812     p.defaultiface = 1;
  813     p.query = 0;
  814     p.traffic = 1;
  815     strncpy_nt(p.interface, "default", 32);
  816 
  817     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  818     fake_proc_net_dev("w", "firstinterface", 0, 0, 0, 0);
  819     fake_proc_net_dev("a", "secondinterface", 0, 0, 0, 0);
  820 
  821     suppress_output();
  822     handleifselection(&p);
  823 
  824     /* first available interface is selected */
  825     ck_assert_str_eq(p.interface, "firstinterface");
  826 }
  827 END_TEST
  828 
  829 START_TEST(handleifselection_exits_if_only_database_shows_interfaces_for_traffic)
  830 {
  831     int ret;
  832     PARAMS p;
  833     initparams(&p);
  834 
  835     p.defaultiface = 1;
  836     p.query = 0;
  837     p.traffic = 1;
  838     strncpy_nt(p.interface, "default", 32);
  839 
  840     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  841 
  842     ret = db_open_rw(1);
  843     ck_assert_int_eq(ret, 1);
  844 
  845     ret = db_addinterface("ethsomething");
  846     ck_assert_int_eq(ret, 1);
  847 
  848     ret = db_addinterface("ethnothing");
  849     ck_assert_int_eq(ret, 1);
  850 
  851     suppress_output();
  852     handleifselection(&p);
  853 }
  854 END_TEST
  855 
  856 START_TEST(handleifselection_selects_only_available_interfaces_for_traffic)
  857 {
  858     int ret;
  859     PARAMS p;
  860     initparams(&p);
  861 
  862     p.defaultiface = 1;
  863     p.query = 0;
  864     p.traffic = 1;
  865     strncpy_nt(p.interface, "default", 32);
  866 
  867     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  868     fake_proc_net_dev("w", "ethnone", 0, 0, 0, 0);
  869     fake_proc_net_dev("a", "ethslow", 0, 0, 0, 0);
  870 
  871     ret = db_open_rw(1);
  872     ck_assert_int_eq(ret, 1);
  873 
  874     ret = db_addinterface("ethslow");
  875     ck_assert_int_eq(ret, 1);
  876 
  877     ret = db_addinterface("ethfast");
  878     ck_assert_int_eq(ret, 1);
  879 
  880     ret = db_settotal("ethslow", 100, 200);
  881     ck_assert_int_eq(ret, 1);
  882 
  883     ret = db_settotal("ethfast", 80, 9001);
  884     ck_assert_int_eq(ret, 1);
  885 
  886     handleifselection(&p);
  887 
  888     /* interface with most traffic in database the is also available is selected */
  889     ck_assert_str_eq(p.interface, "ethslow");
  890 }
  891 END_TEST
  892 
  893 START_TEST(handleifselection_selects_only_available_interfaces_and_can_ignore_database_for_traffic)
  894 {
  895     int ret;
  896     PARAMS p;
  897     initparams(&p);
  898 
  899     p.defaultiface = 1;
  900     p.query = 0;
  901     p.traffic = 1;
  902     strncpy_nt(p.interface, "default", 32);
  903 
  904     ck_assert_int_eq(remove_directory(TESTDIR), 1);
  905     fake_proc_net_dev("w", "ethnone", 0, 0, 0, 0);
  906     fake_proc_net_dev("a", "ethall", 0, 0, 0, 0);
  907 
  908     ret = db_open_rw(1);
  909     ck_assert_int_eq(ret, 1);
  910 
  911     ret = db_addinterface("ethslow");
  912     ck_assert_int_eq(ret, 1);
  913 
  914     ret = db_addinterface("ethfast");
  915     ck_assert_int_eq(ret, 1);
  916 
  917     ret = db_settotal("ethslow", 100, 200);
  918     ck_assert_int_eq(ret, 1);
  919 
  920     ret = db_settotal("ethfast", 80, 9001);
  921     ck_assert_int_eq(ret, 1);
  922 
  923     handleifselection(&p);
  924 
  925     /* first available interface is selected if none of the interfaces in database are available */
  926     ck_assert_str_eq(p.interface, "ethnone");
  927 }
  928 END_TEST
  929 
  930 START_TEST(showalerthelp_does_not_crash)
  931 {
  932     suppress_output();
  933     showalerthelp();
  934 }
  935 END_TEST
  936 
  937 START_TEST(parsealertargs_knows_which_parameters_belong_to_it)
  938 {
  939     int ret;
  940     PARAMS p;
  941     char *argv[] = {"--alert", "1", "2", "--days", NULL};
  942 
  943     defaultcfg();
  944     initparams(&p);
  945     suppress_output();
  946 
  947     ret = parsealertargs(&p, argv);
  948     ck_assert_int_eq(ret, 0);
  949     ck_assert_int_eq(p.alertoutput, 0);
  950     ck_assert_int_eq(p.alertexit, 0);
  951     ck_assert_int_eq(p.alerttype, 0);
  952     ck_assert_int_eq(p.alertcondition, 0);
  953     ck_assert_int_eq(p.alertlimit, 0);
  954 }
  955 END_TEST
  956 
  957 START_TEST(parsealertargs_helps_when_asked)
  958 {
  959     int ret;
  960     PARAMS p;
  961     char *argv[] = {"--alert", "1", "2", "--help", NULL};
  962 
  963     defaultcfg();
  964     initparams(&p);
  965     suppress_output();
  966 
  967     ret = parsealertargs(&p, argv);
  968     ck_assert_int_eq(ret, 0);
  969     ck_assert_int_eq(p.alertoutput, 0);
  970     ck_assert_int_eq(p.alertexit, 0);
  971     ck_assert_int_eq(p.alerttype, 0);
  972     ck_assert_int_eq(p.alertcondition, 0);
  973     ck_assert_int_eq(p.alertlimit, 0);
  974 }
  975 END_TEST
  976 
  977 START_TEST(parsealertargs_can_set_parameters)
  978 {
  979     int ret;
  980     PARAMS p;
  981     char *argv[] = {"--alert", "1", "2", "y", "total", "5", "KiB", NULL};
  982 
  983     defaultcfg();
  984     initparams(&p);
  985     debug = 1;
  986     suppress_output();
  987 
  988     ret = parsealertargs(&p, argv);
  989     ck_assert_int_eq(ret, 1);
  990     ck_assert_int_eq(p.alertoutput, 1);
  991     ck_assert_int_eq(p.alertexit, 2);
  992     ck_assert_int_eq(p.alerttype, 4);
  993     ck_assert_int_eq(p.alertcondition, 3);
  994     ck_assert_int_eq(p.alertlimit, 5120);
  995 }
  996 END_TEST
  997 
  998 START_TEST(parsealertargs_can_validate_output)
  999 {
 1000     int ret;
 1001     PARAMS p;
 1002     char *argv[] = {"--alert", "a", "2", "y", "total", "5", "KiB", NULL};
 1003 
 1004     defaultcfg();
 1005     initparams(&p);
 1006     suppress_output();
 1007 
 1008     ret = parsealertargs(&p, argv);
 1009     ck_assert_int_eq(ret, 0);
 1010     ck_assert_int_eq(p.alertoutput, 0);
 1011     ck_assert_int_eq(p.alertexit, 0);
 1012     ck_assert_int_eq(p.alerttype, 0);
 1013     ck_assert_int_eq(p.alertcondition, 0);
 1014     ck_assert_int_eq(p.alertlimit, 0);
 1015 }
 1016 END_TEST
 1017 
 1018 START_TEST(parsealertargs_can_validate_output_range)
 1019 {
 1020     int ret;
 1021     PARAMS p;
 1022     char *argv[] = {"--alert", "4", "2", "y", "total", "5", "KiB", NULL};
 1023 
 1024     defaultcfg();
 1025     initparams(&p);
 1026     suppress_output();
 1027 
 1028     ret = parsealertargs(&p, argv);
 1029     ck_assert_int_eq(ret, 0);
 1030     ck_assert_int_eq(p.alertoutput, 4);
 1031     ck_assert_int_eq(p.alertexit, 0);
 1032     ck_assert_int_eq(p.alerttype, 0);
 1033     ck_assert_int_eq(p.alertcondition, 0);
 1034     ck_assert_int_eq(p.alertlimit, 0);
 1035 }
 1036 END_TEST
 1037 
 1038 START_TEST(parsealertargs_can_validate_exit)
 1039 {
 1040     int ret;
 1041     PARAMS p;
 1042     char *argv[] = {"--alert", "1", "b", "y", "total", "5", "KiB", NULL};
 1043 
 1044     defaultcfg();
 1045     initparams(&p);
 1046     suppress_output();
 1047 
 1048     ret = parsealertargs(&p, argv);
 1049     ck_assert_int_eq(ret, 0);
 1050     ck_assert_int_eq(p.alertoutput, 1);
 1051     ck_assert_int_eq(p.alertexit, 0);
 1052     ck_assert_int_eq(p.alerttype, 0);
 1053     ck_assert_int_eq(p.alertcondition, 0);
 1054     ck_assert_int_eq(p.alertlimit, 0);
 1055 }
 1056 END_TEST
 1057 
 1058 START_TEST(parsealertargs_can_validate_exit_range)
 1059 {
 1060     int ret;
 1061     PARAMS p;
 1062     char *argv[] = {"--alert", "1", "4", "y", "total", "5", "KiB", NULL};
 1063 
 1064     defaultcfg();
 1065     initparams(&p);
 1066     suppress_output();
 1067 
 1068     ret = parsealertargs(&p, argv);
 1069     ck_assert_int_eq(ret, 0);
 1070     ck_assert_int_eq(p.alertoutput, 1);
 1071     ck_assert_int_eq(p.alertexit, 4);
 1072     ck_assert_int_eq(p.alerttype, 0);
 1073     ck_assert_int_eq(p.alertcondition, 0);
 1074     ck_assert_int_eq(p.alertlimit, 0);
 1075 }
 1076 END_TEST
 1077 
 1078 START_TEST(parsealertargs_knows_first_useless_parameter_combination)
 1079 {
 1080     int ret;
 1081     PARAMS p;
 1082     char *argv[] = {"--alert", "0", "0", "y", "total", "5", "KiB", NULL};
 1083 
 1084     defaultcfg();
 1085     initparams(&p);
 1086     suppress_output();
 1087 
 1088     ret = parsealertargs(&p, argv);
 1089     ck_assert_int_eq(ret, 0);
 1090     ck_assert_int_eq(p.alertoutput, 0);
 1091     ck_assert_int_eq(p.alertexit, 0);
 1092     ck_assert_int_eq(p.alerttype, 0);
 1093     ck_assert_int_eq(p.alertcondition, 0);
 1094     ck_assert_int_eq(p.alertlimit, 0);
 1095 }
 1096 END_TEST
 1097 
 1098 START_TEST(parsealertargs_knows_second_useless_parameter_combination)
 1099 {
 1100     int ret;
 1101     PARAMS p;
 1102     char *argv[] = {"--alert", "0", "1", "y", "total", "5", "KiB", NULL};
 1103 
 1104     defaultcfg();
 1105     initparams(&p);
 1106     suppress_output();
 1107 
 1108     ret = parsealertargs(&p, argv);
 1109     ck_assert_int_eq(ret, 0);
 1110     ck_assert_int_eq(p.alertoutput, 0);
 1111     ck_assert_int_eq(p.alertexit, 1);
 1112     ck_assert_int_eq(p.alerttype, 0);
 1113     ck_assert_int_eq(p.alertcondition, 0);
 1114     ck_assert_int_eq(p.alertlimit, 0);
 1115 }
 1116 END_TEST
 1117 
 1118 START_TEST(parsealertargs_can_validate_type)
 1119 {
 1120     int ret;
 1121     PARAMS p;
 1122     char *argv[] = {"--alert", "1", "2", "a", "total", "5", "KiB", NULL};
 1123 
 1124     defaultcfg();
 1125     initparams(&p);
 1126     suppress_output();
 1127 
 1128     ret = parsealertargs(&p, argv);
 1129     ck_assert_int_eq(ret, 0);
 1130     ck_assert_int_eq(p.alertoutput, 1);
 1131     ck_assert_int_eq(p.alertexit, 2);
 1132     ck_assert_int_eq(p.alerttype, 0);
 1133     ck_assert_int_eq(p.alertcondition, 0);
 1134     ck_assert_int_eq(p.alertlimit, 0);
 1135 }
 1136 END_TEST
 1137 
 1138 START_TEST(parsealertargs_can_validate_condition)
 1139 {
 1140     int ret;
 1141     PARAMS p;
 1142     char *argv[] = {"--alert", "1", "2", "y", "total_recall", "5", "KiB", NULL};
 1143 
 1144     defaultcfg();
 1145     initparams(&p);
 1146     suppress_output();
 1147 
 1148     ret = parsealertargs(&p, argv);
 1149     ck_assert_int_eq(ret, 0);
 1150     ck_assert_int_eq(p.alertoutput, 1);
 1151     ck_assert_int_eq(p.alertexit, 2);
 1152     ck_assert_int_eq(p.alerttype, 4);
 1153     ck_assert_int_eq(p.alertcondition, 0);
 1154     ck_assert_int_eq(p.alertlimit, 0);
 1155 }
 1156 END_TEST
 1157 
 1158 START_TEST(parsealertargs_knows_first_invalid_condition_combination)
 1159 {
 1160     int ret;
 1161     PARAMS p;
 1162     char *argv[] = {"--alert", "2", "3", "y", "rx_estimate", "5", "KiB", NULL};
 1163 
 1164     defaultcfg();
 1165     initparams(&p);
 1166     suppress_output();
 1167 
 1168     ret = parsealertargs(&p, argv);
 1169     ck_assert_int_eq(ret, 0);
 1170     ck_assert_int_eq(p.alertoutput, 2);
 1171     ck_assert_int_eq(p.alertexit, 3);
 1172     ck_assert_int_eq(p.alerttype, 4);
 1173     ck_assert_int_eq(p.alertcondition, 4);
 1174     ck_assert_int_eq(p.alertlimit, 0);
 1175 }
 1176 END_TEST
 1177 
 1178 START_TEST(parsealertargs_knows_second_invalid_condition_combination)
 1179 {
 1180     int ret;
 1181     PARAMS p;
 1182     char *argv[] = {"--alert", "3", "2", "y", "tx_estimate", "5", "KiB", NULL};
 1183 
 1184     defaultcfg();
 1185     initparams(&p);
 1186     suppress_output();
 1187 
 1188     ret = parsealertargs(&p, argv);
 1189     ck_assert_int_eq(ret, 0);
 1190     ck_assert_int_eq(p.alertoutput, 3);
 1191     ck_assert_int_eq(p.alertexit, 2);
 1192     ck_assert_int_eq(p.alerttype, 4);
 1193     ck_assert_int_eq(p.alertcondition, 5);
 1194     ck_assert_int_eq(p.alertlimit, 0);
 1195 }
 1196 END_TEST
 1197 
 1198 START_TEST(parsealertargs_can_validate_limit_as_integer)
 1199 {
 1200     int ret;
 1201     PARAMS p;
 1202     char *argv[] = {"--alert", "1", "2", "y", "total", "5.5", "KiB", NULL};
 1203 
 1204     defaultcfg();
 1205     initparams(&p);
 1206     suppress_output();
 1207 
 1208     ret = parsealertargs(&p, argv);
 1209     ck_assert_int_eq(ret, 0);
 1210     ck_assert_int_eq(p.alertoutput, 1);
 1211     ck_assert_int_eq(p.alertexit, 2);
 1212     ck_assert_int_eq(p.alerttype, 4);
 1213     ck_assert_int_eq(p.alertcondition, 3);
 1214     ck_assert_int_eq(p.alertlimit, 0);
 1215 }
 1216 END_TEST
 1217 
 1218 START_TEST(parsealertargs_can_validate_limit_as_non_zero)
 1219 {
 1220     int ret;
 1221     PARAMS p;
 1222     char *argv[] = {"--alert", "1", "2", "y", "total", "0", "KiB", NULL};
 1223 
 1224     defaultcfg();
 1225     initparams(&p);
 1226     suppress_output();
 1227 
 1228     ret = parsealertargs(&p, argv);
 1229     ck_assert_int_eq(ret, 0);
 1230     ck_assert_int_eq(p.alertoutput, 1);
 1231     ck_assert_int_eq(p.alertexit, 2);
 1232     ck_assert_int_eq(p.alerttype, 4);
 1233     ck_assert_int_eq(p.alertcondition, 3);
 1234     ck_assert_int_eq(p.alertlimit, 0);
 1235 }
 1236 END_TEST
 1237 
 1238 START_TEST(parsealertargs_can_validate_limit_unit)
 1239 {
 1240     int ret;
 1241     PARAMS p;
 1242     char *argv[] = {"--alert", "1", "2", "y", "total", "5", "KeK", NULL};
 1243 
 1244     defaultcfg();
 1245     initparams(&p);
 1246     suppress_output();
 1247 
 1248     ret = parsealertargs(&p, argv);
 1249     ck_assert_int_eq(ret, 0);
 1250     ck_assert_int_eq(p.alertoutput, 1);
 1251     ck_assert_int_eq(p.alertexit, 2);
 1252     ck_assert_int_eq(p.alerttype, 4);
 1253     ck_assert_int_eq(p.alertcondition, 3);
 1254     ck_assert_int_eq(p.alertlimit, 0);
 1255 }
 1256 END_TEST
 1257 
 1258 START_TEST(parsealertargs_knows_the_64_bit_limit_regardless_of_used_unit)
 1259 {
 1260     int ret;
 1261     PARAMS p;
 1262     char *argv[] = {"--alert", "1", "2", "y", "total", "16", "EiB", NULL};
 1263 
 1264     defaultcfg();
 1265     initparams(&p);
 1266     debug = 1;
 1267     suppress_output();
 1268 
 1269     ret = parsealertargs(&p, argv);
 1270     ck_assert_int_eq(ret, 0);
 1271     ck_assert_int_eq(p.alertoutput, 1);
 1272     ck_assert_int_eq(p.alertexit, 2);
 1273     ck_assert_int_eq(p.alerttype, 4);
 1274     ck_assert_int_eq(p.alertcondition, 3);
 1275     ck_assert_int_eq(p.alertlimit, 0);
 1276 }
 1277 END_TEST
 1278 
 1279 START_TEST(handleshowalert_requires_interface_to_be_specified)
 1280 {
 1281     PARAMS p;
 1282 
 1283     defaultcfg();
 1284     initparams(&p);
 1285     suppress_output();
 1286     p.alert = 1;
 1287     p.defaultiface = 1;
 1288 
 1289     handleshowalert(&p);
 1290 }
 1291 END_TEST
 1292 
 1293 START_TEST(validateinterface_does_not_use_alias_if_interface_names_matches)
 1294 {
 1295     int ret;
 1296     PARAMS p;
 1297 
 1298     defaultcfg();
 1299     initparams(&p);
 1300     strncpy_nt(p.interface, "eth0", 32);
 1301 
 1302     ret = db_open_rw(1);
 1303     ck_assert_int_eq(ret, 1);
 1304 
 1305     ret = db_addinterface("eth0");
 1306     ck_assert_int_eq(ret, 1);
 1307 
 1308     ret = db_setalias("eth0", "LAN");
 1309     ck_assert_int_eq(ret, 1);
 1310 
 1311     ret = db_addinterface("eth1");
 1312     ck_assert_int_eq(ret, 1);
 1313 
 1314     ret = db_setalias("eth1", "eth0");
 1315     ck_assert_int_eq(ret, 1);
 1316 
 1317     validateinterface(&p);
 1318 
 1319     ck_assert_str_eq(p.interface, "eth0");
 1320 
 1321     ret = db_close();
 1322     ck_assert_int_eq(ret, 1);
 1323 }
 1324 END_TEST
 1325 
 1326 START_TEST(validateinterface_supports_interface_merges)
 1327 {
 1328     int ret;
 1329     PARAMS p;
 1330 
 1331     defaultcfg();
 1332     initparams(&p);
 1333     strncpy_nt(p.interface, "eth0+eth1", 32);
 1334 
 1335     ret = db_open_rw(1);
 1336     ck_assert_int_eq(ret, 1);
 1337 
 1338     ret = db_addinterface("eth0");
 1339     ck_assert_int_eq(ret, 1);
 1340 
 1341     ret = db_setalias("eth0", "LAN");
 1342     ck_assert_int_eq(ret, 1);
 1343 
 1344     ret = db_addinterface("eth1");
 1345     ck_assert_int_eq(ret, 1);
 1346 
 1347     ret = db_setalias("eth1", "Internet");
 1348     ck_assert_int_eq(ret, 1);
 1349 
 1350     validateinterface(&p);
 1351 
 1352     ck_assert_str_eq(p.interface, "eth0+eth1");
 1353 
 1354     ret = db_close();
 1355     ck_assert_int_eq(ret, 1);
 1356 }
 1357 END_TEST
 1358 
 1359 START_TEST(validateinterface_detects_if_not_all_interfaces_are_available_for_merge)
 1360 {
 1361     int ret;
 1362     PARAMS p;
 1363 
 1364     defaultcfg();
 1365     initparams(&p);
 1366     strncpy_nt(p.interface, "eth0+eth2", 32);
 1367     suppress_output();
 1368 
 1369     ret = db_open_rw(1);
 1370     ck_assert_int_eq(ret, 1);
 1371 
 1372     ret = db_addinterface("eth0");
 1373     ck_assert_int_eq(ret, 1);
 1374 
 1375     ret = db_setalias("eth0", "LAN");
 1376     ck_assert_int_eq(ret, 1);
 1377 
 1378     ret = db_addinterface("eth1");
 1379     ck_assert_int_eq(ret, 1);
 1380 
 1381     ret = db_setalias("eth1", "Internet");
 1382     ck_assert_int_eq(ret, 1);
 1383 
 1384     validateinterface(&p);
 1385 
 1386     ret = db_close();
 1387     ck_assert_int_eq(ret, 1);
 1388 }
 1389 END_TEST
 1390 
 1391 START_TEST(validateinterface_detects_if_not_all_interfaces_are_unique_for_merge)
 1392 {
 1393     int ret;
 1394     PARAMS p;
 1395 
 1396     defaultcfg();
 1397     initparams(&p);
 1398     strncpy_nt(p.interface, "eth0+eth0", 32);
 1399     suppress_output();
 1400 
 1401     ret = db_open_rw(1);
 1402     ck_assert_int_eq(ret, 1);
 1403 
 1404     ret = db_addinterface("eth0");
 1405     ck_assert_int_eq(ret, 1);
 1406 
 1407     ret = db_setalias("eth0", "LAN");
 1408     ck_assert_int_eq(ret, 1);
 1409 
 1410     ret = db_addinterface("eth1");
 1411     ck_assert_int_eq(ret, 1);
 1412 
 1413     ret = db_setalias("eth1", "Internet");
 1414     ck_assert_int_eq(ret, 1);
 1415 
 1416     validateinterface(&p);
 1417 
 1418     ret = db_close();
 1419     ck_assert_int_eq(ret, 1);
 1420 }
 1421 END_TEST
 1422 
 1423 START_TEST(validateinterface_uses_all_matching_methods_if_no_match_for_exact_name_is_found)
 1424 {
 1425     int ret;
 1426     PARAMS p;
 1427 
 1428     defaultcfg();
 1429     initparams(&p);
 1430     debug = 1;
 1431     cfg.ifacematchmethod = 3;
 1432     strncpy_nt(p.interface, "inter", 32);
 1433     suppress_output();
 1434 
 1435     ret = db_open_rw(1);
 1436     ck_assert_int_eq(ret, 1);
 1437 
 1438     ret = db_addinterface("eth0");
 1439     ck_assert_int_eq(ret, 1);
 1440 
 1441     ret = db_setalias("eth0", "LAN");
 1442     ck_assert_int_eq(ret, 1);
 1443 
 1444     ret = db_addinterface("eth1");
 1445     ck_assert_int_eq(ret, 1);
 1446 
 1447     ret = db_setalias("eth1", "Internet");
 1448     ck_assert_int_eq(ret, 1);
 1449 
 1450     validateinterface(&p);
 1451 
 1452     ck_assert_str_eq(p.interface, "eth1");
 1453 
 1454     ret = db_close();
 1455     ck_assert_int_eq(ret, 1);
 1456 }
 1457 END_TEST
 1458 
 1459 START_TEST(validateinterface_knows_when_to_give_up_searching)
 1460 {
 1461     int ret;
 1462     PARAMS p;
 1463 
 1464     defaultcfg();
 1465     initparams(&p);
 1466     debug = 1;
 1467     cfg.ifacematchmethod = 3;
 1468     strncpy_nt(p.interface, "outer", 32);
 1469     suppress_output();
 1470 
 1471     ret = db_open_rw(1);
 1472     ck_assert_int_eq(ret, 1);
 1473 
 1474     ret = db_addinterface("eth0");
 1475     ck_assert_int_eq(ret, 1);
 1476 
 1477     ret = db_setalias("eth0", "LAN");
 1478     ck_assert_int_eq(ret, 1);
 1479 
 1480     ret = db_addinterface("eth1");
 1481     ck_assert_int_eq(ret, 1);
 1482 
 1483     ret = db_setalias("eth1", "Internet");
 1484     ck_assert_int_eq(ret, 1);
 1485 
 1486     validateinterface(&p);
 1487 
 1488     ret = db_close();
 1489     ck_assert_int_eq(ret, 1);
 1490 }
 1491 END_TEST
 1492 
 1493 void add_cli_tests(Suite *s)
 1494 {
 1495     TCase *tc_cli = tcase_create("CLI");
 1496     tcase_add_checked_fixture(tc_cli, setup, teardown);
 1497     tcase_add_unchecked_fixture(tc_cli, setup, teardown);
 1498     tcase_add_test(tc_cli, vnstat_can_init_params);
 1499     tcase_add_test(tc_cli, vnstat_showhelp_does_not_crash);
 1500     tcase_add_test(tc_cli, vnstat_showlonghelp_does_not_crash);
 1501     tcase_add_test(tc_cli, vnstat_handlers_do_nothing_by_default);
 1502     tcase_add_exit_test(tc_cli, vnstat_handletrafficmeters_exists_when_interface_is_not_available, 1);
 1503     tcase_add_exit_test(tc_cli, vnstat_handleremoveinterface_exits_if_no_interface_has_been_specified, 1);
 1504     tcase_add_exit_test(tc_cli, vnstat_handleremoveinterface_exits_if_given_interface_does_not_exist, 1);
 1505     tcase_add_exit_test(tc_cli, vnstat_handleremoveinterface_exits_if_force_is_not_used, 1);
 1506     tcase_add_test(tc_cli, vnstat_handleremoveinterface_removes_interface);
 1507     tcase_add_exit_test(tc_cli, vnstat_handlerenameinterface_exits_if_no_interface_has_been_specified, 1);
 1508     tcase_add_exit_test(tc_cli, vnstat_handlerenameinterface_exits_if_new_interface_name_is_not_given, 1);
 1509     tcase_add_exit_test(tc_cli, vnstat_handlerenameinterface_exits_if_given_interface_does_not_exist, 1);
 1510     tcase_add_exit_test(tc_cli, vnstat_handlerenameinterface_exits_if_new_interface_name_already_exist, 1);
 1511     tcase_add_exit_test(tc_cli, vnstat_handlerenameinterface_exits_if_force_is_not_used, 1);
 1512     tcase_add_test(tc_cli, vnstat_handlerenameinterface_renames_interface);
 1513     tcase_add_exit_test(tc_cli, vnstat_handleaddinterface_exits_if_no_interface_has_been_specified, 1);
 1514     tcase_add_exit_test(tc_cli, vnstat_handleaddinterface_exits_if_interface_already_exist_in_database, 1);
 1515     tcase_add_exit_test(tc_cli, vnstat_handleaddinterface_exits_if_interface_does_not_exist, 1);
 1516     tcase_add_test(tc_cli, vnstat_handleaddinterface_adds_interface);
 1517     tcase_add_test(tc_cli, vnstat_handleaddinterface_can_be_forced_to_add_interface_that_does_not_exist);
 1518     tcase_add_exit_test(tc_cli, vnstat_handlesetalias_exits_if_no_interface_has_been_specified, 1);
 1519     tcase_add_exit_test(tc_cli, vnstat_handlesetalias_exits_if_given_interface_does_not_exist, 1);
 1520     tcase_add_test(tc_cli, vnstat_handlesetalias_sets_alias);
 1521     tcase_add_test(tc_cli, vnstat_handlesetalias_clears_alias);
 1522     tcase_add_test(tc_cli, vnstat_handleaddinterface_can_also_set_alias_after_adding_interface);
 1523     tcase_add_exit_test(tc_cli, vnstat_handletrafficmeters_exits_when_interface_is_not_available, 1);
 1524     tcase_add_exit_test(tc_cli, vnstat_handletrafficmeters_exits_when_interface_is_not_available_with_configuration_tips, 1);
 1525     tcase_add_exit_test(tc_cli, vnstat_handletrafficmeters_exits_when_specific_interface_is_not_available, 1);
 1526     tcase_add_test(tc_cli, vnstat_handletrafficmeters_can_calculate_traffic);
 1527     tcase_add_test(tc_cli, vnstat_handletrafficmeters_can_handle_interface_merge_using_first_interface);
 1528     tcase_add_test(tc_cli, vnstat_handletrafficmeters_can_calculate_traffic_and_output_json);
 1529     tcase_add_test(tc_cli, vnstat_handletrafficmeters_livetraffic_does_not_crash);
 1530     tcase_add_test(tc_cli, vnstat_handletrafficmeters_livetraffic_does_not_crash_with_interface_merge);
 1531     tcase_add_test(tc_cli, vnstat_handletrafficmeters_livetraffic_does_not_crash_with_json);
 1532     tcase_add_test(tc_cli, handleifselection_does_nothing_when_interface_has_already_been_selected);
 1533     tcase_add_test(tc_cli, handleifselection_selects_default_interface_if_field_is_filled);
 1534     tcase_add_exit_test(tc_cli, handleifselection_exits_when_no_suitable_interface_is_available_for_query, 1);
 1535     tcase_add_test(tc_cli, handleifselection_selects_only_interface_from_database_for_query);
 1536     tcase_add_test(tc_cli, handleifselection_selects_highest_traffic_interface_from_database_for_query);
 1537     tcase_add_exit_test(tc_cli, handleifselection_exits_when_no_suitable_interface_is_available_for_traffic, 1);
 1538     tcase_add_test(tc_cli, handleifselection_can_select_interface_without_database_for_traffic);
 1539     tcase_add_exit_test(tc_cli, handleifselection_exits_if_only_database_shows_interfaces_for_traffic, 1);
 1540     tcase_add_test(tc_cli, handleifselection_selects_only_available_interfaces_for_traffic);
 1541     tcase_add_test(tc_cli, handleifselection_selects_only_available_interfaces_and_can_ignore_database_for_traffic);
 1542     tcase_add_test(tc_cli, showalerthelp_does_not_crash);
 1543     tcase_add_test(tc_cli, parsealertargs_knows_which_parameters_belong_to_it);
 1544     tcase_add_test(tc_cli, parsealertargs_helps_when_asked);
 1545     tcase_add_test(tc_cli, parsealertargs_can_set_parameters);
 1546     tcase_add_test(tc_cli, parsealertargs_can_validate_output);
 1547     tcase_add_test(tc_cli, parsealertargs_can_validate_output_range);
 1548     tcase_add_test(tc_cli, parsealertargs_can_validate_exit);
 1549     tcase_add_test(tc_cli, parsealertargs_can_validate_exit_range);
 1550     tcase_add_test(tc_cli, parsealertargs_knows_first_useless_parameter_combination);
 1551     tcase_add_test(tc_cli, parsealertargs_knows_second_useless_parameter_combination);
 1552     tcase_add_test(tc_cli, parsealertargs_can_validate_type);
 1553     tcase_add_test(tc_cli, parsealertargs_can_validate_condition);
 1554     tcase_add_test(tc_cli, parsealertargs_knows_first_invalid_condition_combination);
 1555     tcase_add_test(tc_cli, parsealertargs_knows_second_invalid_condition_combination);
 1556     tcase_add_test(tc_cli, parsealertargs_can_validate_limit_as_integer);
 1557     tcase_add_test(tc_cli, parsealertargs_can_validate_limit_as_non_zero);
 1558     tcase_add_test(tc_cli, parsealertargs_can_validate_limit_unit);
 1559     tcase_add_test(tc_cli, parsealertargs_knows_the_64_bit_limit_regardless_of_used_unit);
 1560     tcase_add_exit_test(tc_cli, handleshowalert_requires_interface_to_be_specified, 1);
 1561     tcase_add_test(tc_cli, validateinterface_does_not_use_alias_if_interface_names_matches);
 1562     tcase_add_test(tc_cli, validateinterface_supports_interface_merges);
 1563     tcase_add_exit_test(tc_cli, validateinterface_detects_if_not_all_interfaces_are_available_for_merge, 1);
 1564     tcase_add_exit_test(tc_cli, validateinterface_detects_if_not_all_interfaces_are_unique_for_merge, 1);
 1565     tcase_add_test(tc_cli, validateinterface_uses_all_matching_methods_if_no_match_for_exact_name_is_found);
 1566     tcase_add_exit_test(tc_cli, validateinterface_knows_when_to_give_up_searching, 1);
 1567     suite_add_tcase(s, tc_cli);
 1568 }