"Fossies" - the Fresh Open Source Software Archive

Member "vnstat-2.9/tests/parseargs_tests.c" (29 Aug 2021, 22996 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 "parseargs_tests.c": 2.7_vs_2.8.

    1 #include "common.h"
    2 #include "vnstat_tests.h"
    3 #include "vnstat_func.h"
    4 #include "cfg.h"
    5 #include "dbsql.h"
    6 #include "parseargs_tests.h"
    7 
    8 START_TEST(vnstat_parseargs_does_nothing_without_args)
    9 {
   10     PARAMS p;
   11     char *argv[] = {"vnstat", "-h", NULL};
   12     int argc = sizeof(argv) / sizeof(char *) - 1;
   13 
   14     initparams(&p);
   15     parseargs(&p, argc, argv);
   16 }
   17 END_TEST
   18 
   19 START_TEST(vnstat_parseargs_can_help)
   20 {
   21     PARAMS p;
   22     char *argv[] = {"vnstat", "--help", NULL};
   23     int argc = sizeof(argv) / sizeof(char *) - 1;
   24 
   25     initparams(&p);
   26     debug = 1;
   27     suppress_output();
   28     parseargs(&p, argc, argv);
   29 }
   30 END_TEST
   31 
   32 START_TEST(vnstat_parseargs_can_longhelp)
   33 {
   34     PARAMS p;
   35     char *argv[] = {"vnstat", "--longhelp", NULL};
   36     int argc = sizeof(argv) / sizeof(char *) - 1;
   37 
   38     initparams(&p);
   39     suppress_output();
   40     parseargs(&p, argc, argv);
   41 }
   42 END_TEST
   43 
   44 START_TEST(vnstat_parseargs_can_show_version)
   45 {
   46     PARAMS p;
   47     char *argv[] = {"vnstat", "--version", NULL};
   48     int argc = sizeof(argv) / sizeof(char *) - 1;
   49 
   50     initparams(&p);
   51     suppress_output();
   52     parseargs(&p, argc, argv);
   53 }
   54 END_TEST
   55 
   56 START_TEST(vnstat_parseargs_detects_unknown_parameters)
   57 {
   58     PARAMS p;
   59     char *argv[] = {"vnstat", "--something_fishy", NULL};
   60     int argc = sizeof(argv) / sizeof(char *) - 1;
   61 
   62     initparams(&p);
   63     suppress_output();
   64     parseargs(&p, argc, argv);
   65 }
   66 END_TEST
   67 
   68 START_TEST(vnstat_parseargs_can_modify_settings)
   69 {
   70     PARAMS p;
   71     char *argv[] = {"vnstat", "--debug", "--traffic", "12", "--add", "--rename", "aname", "--config",
   72                     "does_nothing", "-l", "1", "--remove", "-i", "ethsomething", "--style", "0", "--dbdir",
   73                     "dbsomewhere", "-q", "-d", "1", "-m", "2", "-t", "3", "-s", "-y", "4", "-hg", "-h", "5", "-5", "6",
   74                     "--oneline", "b", "--xml", "h", "7", "--json", "d", "7", "-ru", "--rateunit", "0",
   75                     "--force", "--setalias", "super", "--begin", "2000-01-01",
   76                     "--end", "2001-01-01", NULL};
   77     int argc = sizeof(argv) / sizeof(char *) - 1;
   78 
   79     initparams(&p);
   80     suppress_output();
   81     parseargs(&p, argc, argv);
   82 
   83     ck_assert_str_eq(p.interface, "ethsomething");
   84     ck_assert_int_eq(p.defaultiface, 0);
   85     ck_assert_int_eq(cfg.ostyle, 4);
   86     ck_assert_str_eq(cfg.dbdir, "dbsomewhere");
   87     ck_assert_int_eq(p.query, 1);
   88     ck_assert_int_eq(p.force, 1);
   89     ck_assert_int_eq(cfg.qmode, 10);
   90     ck_assert_int_eq(p.jsonmode, 'd');
   91     ck_assert_int_eq(p.xmlmode, 'h');
   92     ck_assert_int_eq(cfg.rateunit, 0);
   93     ck_assert_int_eq(p.setalias, 1);
   94     ck_assert_str_eq(p.alias, "super");
   95     ck_assert_str_eq(p.newifname, "aname");
   96     ck_assert_int_eq(p.renameiface, 1);
   97     ck_assert_int_eq(p.removeiface, 1);
   98     ck_assert_int_eq(p.traffic, 1);
   99     ck_assert_int_eq(cfg.sampletime, 12);
  100     ck_assert_int_eq(p.livetraffic, 1);
  101     ck_assert_int_eq(p.livemode, 1);
  102     ck_assert_int_eq(cfg.listdays, 1);
  103     ck_assert_int_eq(cfg.listmonths, 2);
  104     ck_assert_int_eq(cfg.listtop, 3);
  105     ck_assert_int_eq(cfg.listyears, 4);
  106     ck_assert_int_eq(cfg.listhours, 5);
  107     ck_assert_int_eq(cfg.listfivemins, 6);
  108     ck_assert_int_eq(cfg.listjsonxml, 7);
  109     ck_assert_str_eq(p.databegin, "2000-01-01");
  110     ck_assert_str_eq(p.dataend, "2001-01-01");
  111 }
  112 END_TEST
  113 
  114 START_TEST(vnstat_parseargs_does_not_allow_too_long_interface_names)
  115 {
  116     PARAMS p;
  117     char *argv[] = {"vnstat", "--iface", "12345678901234567890123456789012", NULL};
  118     int argc = sizeof(argv) / sizeof(char *) - 1;
  119 
  120     initparams(&p);
  121     suppress_output();
  122     parseargs(&p, argc, argv);
  123 }
  124 END_TEST
  125 
  126 START_TEST(vnstat_parseargs_style_requires_parameter)
  127 {
  128     PARAMS p;
  129     char *argv[] = {"vnstat", "--style", NULL};
  130     int argc = sizeof(argv) / sizeof(char *) - 1;
  131 
  132     initparams(&p);
  133     suppress_output();
  134     parseargs(&p, argc, argv);
  135 }
  136 END_TEST
  137 
  138 START_TEST(vnstat_parseargs_style_checks_parameter)
  139 {
  140     PARAMS p;
  141     char *argv[] = {"vnstat", "--style", "9001", NULL};
  142     int argc = sizeof(argv) / sizeof(char *) - 1;
  143 
  144     initparams(&p);
  145     suppress_output();
  146     parseargs(&p, argc, argv);
  147 }
  148 END_TEST
  149 
  150 START_TEST(vnstat_parseargs_knows_that_update_is_not_supported)
  151 {
  152     PARAMS p;
  153     char *argv[] = {"vnstat", "-u", NULL};
  154     int argc = sizeof(argv) / sizeof(char *) - 1;
  155 
  156     initparams(&p);
  157     suppress_output();
  158     parseargs(&p, argc, argv);
  159 }
  160 END_TEST
  161 
  162 START_TEST(vnstat_parseargs_dbdir_requires_a_directory)
  163 {
  164     PARAMS p;
  165     char *argv[] = {"vnstat", "--dbdir", NULL};
  166     int argc = sizeof(argv) / sizeof(char *) - 1;
  167 
  168     initparams(&p);
  169     suppress_output();
  170     parseargs(&p, argc, argv);
  171 }
  172 END_TEST
  173 
  174 START_TEST(vnstat_parseargs_oneline_gives_help)
  175 {
  176     PARAMS p;
  177     char *argv[] = {"vnstat", "--oneline", "a", NULL};
  178     int argc = sizeof(argv) / sizeof(char *) - 1;
  179 
  180     initparams(&p);
  181     suppress_output();
  182     parseargs(&p, argc, argv);
  183 }
  184 END_TEST
  185 
  186 START_TEST(vnstat_parseargs_xml_gives_help)
  187 {
  188     PARAMS p;
  189     char *argv[] = {"vnstat", "--xml", "b", NULL};
  190     int argc = sizeof(argv) / sizeof(char *) - 1;
  191 
  192     initparams(&p);
  193     suppress_output();
  194     parseargs(&p, argc, argv);
  195 }
  196 END_TEST
  197 
  198 START_TEST(vnstat_parseargs_xml_without_extra_params)
  199 {
  200     PARAMS p;
  201     char *argv[] = {"vnstat", "--xml", NULL};
  202     int argc = sizeof(argv) / sizeof(char *) - 1;
  203 
  204     cfg.qmode = 0;
  205     initparams(&p);
  206     suppress_output();
  207     parseargs(&p, argc, argv);
  208     ck_assert_int_eq(cfg.qmode, 8);
  209     ck_assert_int_eq(p.xmlmode, 'a');
  210     ck_assert_int_eq(cfg.listjsonxml, 0);
  211 }
  212 END_TEST
  213 
  214 START_TEST(vnstat_parseargs_xml_with_mode)
  215 {
  216     PARAMS p;
  217     char *argv[] = {"vnstat", "--xml", "m", NULL};
  218     int argc = sizeof(argv) / sizeof(char *) - 1;
  219 
  220     cfg.qmode = 0;
  221     initparams(&p);
  222     suppress_output();
  223     parseargs(&p, argc, argv);
  224     ck_assert_int_eq(cfg.qmode, 8);
  225     ck_assert_int_eq(p.xmlmode, 'm');
  226     ck_assert_int_eq(cfg.listjsonxml, 0);
  227 }
  228 END_TEST
  229 
  230 START_TEST(vnstat_parseargs_xml_with_limit)
  231 {
  232     PARAMS p;
  233     char *argv[] = {"vnstat", "--xml", "1231", NULL};
  234     int argc = sizeof(argv) / sizeof(char *) - 1;
  235 
  236     cfg.qmode = 0;
  237     initparams(&p);
  238     suppress_output();
  239     parseargs(&p, argc, argv);
  240     ck_assert_int_eq(cfg.qmode, 8);
  241     ck_assert_int_eq(p.xmlmode, 'a');
  242     ck_assert_int_eq(cfg.listjsonxml, 1231);
  243 }
  244 END_TEST
  245 
  246 START_TEST(vnstat_parseargs_xml_with_mode_and_limit)
  247 {
  248     PARAMS p;
  249     char *argv[] = {"vnstat", "--xml", "t", "2", NULL};
  250     int argc = sizeof(argv) / sizeof(char *) - 1;
  251 
  252     cfg.qmode = 0;
  253     initparams(&p);
  254     suppress_output();
  255     parseargs(&p, argc, argv);
  256     ck_assert_int_eq(cfg.qmode, 8);
  257     ck_assert_int_eq(p.xmlmode, 't');
  258     ck_assert_int_eq(cfg.listjsonxml, 2);
  259 }
  260 END_TEST
  261 
  262 START_TEST(vnstat_parseargs_json_gives_help)
  263 {
  264     PARAMS p;
  265     char *argv[] = {"vnstat", "--json", "b", NULL};
  266     int argc = sizeof(argv) / sizeof(char *) - 1;
  267 
  268     initparams(&p);
  269     suppress_output();
  270     parseargs(&p, argc, argv);
  271 }
  272 END_TEST
  273 
  274 START_TEST(vnstat_parseargs_json_without_extra_params)
  275 {
  276     PARAMS p;
  277     char *argv[] = {"vnstat", "--json", NULL};
  278     int argc = sizeof(argv) / sizeof(char *) - 1;
  279 
  280     cfg.qmode = 0;
  281     initparams(&p);
  282     suppress_output();
  283     parseargs(&p, argc, argv);
  284     ck_assert_int_eq(cfg.qmode, 10);
  285     ck_assert_int_eq(p.jsonmode, 'a');
  286     ck_assert_int_eq(cfg.listjsonxml, 0);
  287 }
  288 END_TEST
  289 
  290 START_TEST(vnstat_parseargs_json_with_mode)
  291 {
  292     PARAMS p;
  293     char *argv[] = {"vnstat", "--json", "m", NULL};
  294     int argc = sizeof(argv) / sizeof(char *) - 1;
  295 
  296     cfg.qmode = 0;
  297     initparams(&p);
  298     suppress_output();
  299     parseargs(&p, argc, argv);
  300     ck_assert_int_eq(cfg.qmode, 10);
  301     ck_assert_int_eq(p.jsonmode, 'm');
  302     ck_assert_int_eq(cfg.listjsonxml, 0);
  303 }
  304 END_TEST
  305 
  306 START_TEST(vnstat_parseargs_json_with_limit)
  307 {
  308     PARAMS p;
  309     char *argv[] = {"vnstat", "--json", "1232", NULL};
  310     int argc = sizeof(argv) / sizeof(char *) - 1;
  311 
  312     cfg.qmode = 0;
  313     initparams(&p);
  314     suppress_output();
  315     parseargs(&p, argc, argv);
  316     ck_assert_int_eq(cfg.qmode, 10);
  317     ck_assert_int_eq(p.jsonmode, 'a');
  318     ck_assert_int_eq(cfg.listjsonxml, 1232);
  319 }
  320 END_TEST
  321 
  322 START_TEST(vnstat_parseargs_json_with_mode_and_limit)
  323 {
  324     PARAMS p;
  325     char *argv[] = {"vnstat", "--json", "d", "3", NULL};
  326     int argc = sizeof(argv) / sizeof(char *) - 1;
  327 
  328     cfg.qmode = 0;
  329     initparams(&p);
  330     suppress_output();
  331     parseargs(&p, argc, argv);
  332     ck_assert_int_eq(cfg.qmode, 10);
  333     ck_assert_int_eq(p.jsonmode, 'd');
  334     ck_assert_int_eq(cfg.listjsonxml, 3);
  335 }
  336 END_TEST
  337 
  338 START_TEST(vnstat_parseargs_rateunit_gives_help)
  339 {
  340     PARAMS p;
  341     char *argv[] = {"vnstat", "--rateunit", "2", NULL};
  342     int argc = sizeof(argv) / sizeof(char *) - 1;
  343 
  344     initparams(&p);
  345     suppress_output();
  346     parseargs(&p, argc, argv);
  347 }
  348 END_TEST
  349 
  350 START_TEST(vnstat_parseargs_live_gives_help)
  351 {
  352     PARAMS p;
  353     char *argv[] = {"vnstat", "--live", "2", NULL};
  354     int argc = sizeof(argv) / sizeof(char *) - 1;
  355 
  356     initparams(&p);
  357     suppress_output();
  358     parseargs(&p, argc, argv);
  359 }
  360 END_TEST
  361 
  362 START_TEST(vnstat_parseargs_begin_gives_help)
  363 {
  364     PARAMS p;
  365     char *argv[] = {"vnstat", "--begin", NULL};
  366     int argc = sizeof(argv) / sizeof(char *) - 1;
  367 
  368     initparams(&p);
  369     suppress_output();
  370     parseargs(&p, argc, argv);
  371 }
  372 END_TEST
  373 
  374 START_TEST(vnstat_parseargs_end_gives_help)
  375 {
  376     PARAMS p;
  377     char *argv[] = {"vnstat", "--end", NULL};
  378     int argc = sizeof(argv) / sizeof(char *) - 1;
  379 
  380     initparams(&p);
  381     suppress_output();
  382     parseargs(&p, argc, argv);
  383 }
  384 END_TEST
  385 
  386 START_TEST(vnstat_parseargs_begin_validates_input)
  387 {
  388     PARAMS p;
  389     char *argv[] = {"vnstat", "--begin", "world_domination", NULL};
  390     int argc = sizeof(argv) / sizeof(char *) - 1;
  391 
  392     initparams(&p);
  393     suppress_output();
  394     parseargs(&p, argc, argv);
  395 }
  396 END_TEST
  397 
  398 START_TEST(vnstat_parseargs_end_validates_input)
  399 {
  400     PARAMS p;
  401     char *argv[] = {"vnstat", "--end", "what?", NULL};
  402     int argc = sizeof(argv) / sizeof(char *) - 1;
  403 
  404     initparams(&p);
  405     suppress_output();
  406     parseargs(&p, argc, argv);
  407 }
  408 END_TEST
  409 
  410 START_TEST(vnstat_parseargs_can_show_config)
  411 {
  412     PARAMS p;
  413     char *argv[] = {"vnstat", "--showconfig", NULL};
  414     int argc = sizeof(argv) / sizeof(char *) - 1;
  415 
  416     initparams(&p);
  417     suppress_output();
  418     parseargs(&p, argc, argv);
  419 }
  420 END_TEST
  421 
  422 START_TEST(vnstat_parseargs_iface_requires_parameter)
  423 {
  424     PARAMS p;
  425     char *argv[] = {"vnstat", "--iface", NULL};
  426     int argc = sizeof(argv) / sizeof(char *) - 1;
  427 
  428     initparams(&p);
  429     suppress_output();
  430     parseargs(&p, argc, argv);
  431 }
  432 END_TEST
  433 
  434 START_TEST(vnstat_parseargs_locale_requires_parameter)
  435 {
  436     PARAMS p;
  437     char *argv[] = {"vnstat", "--locale", NULL};
  438     int argc = sizeof(argv) / sizeof(char *) - 1;
  439 
  440     initparams(&p);
  441     suppress_output();
  442     parseargs(&p, argc, argv);
  443 }
  444 END_TEST
  445 
  446 START_TEST(vnstat_parseargs_setalias_requires_parameter)
  447 {
  448     PARAMS p;
  449     char *argv[] = {"vnstat", "--setalias", NULL};
  450     int argc = sizeof(argv) / sizeof(char *) - 1;
  451 
  452     initparams(&p);
  453     suppress_output();
  454     parseargs(&p, argc, argv);
  455 }
  456 END_TEST
  457 
  458 START_TEST(vnstat_parseargs_setalias_still_supports_nick)
  459 {
  460     PARAMS p;
  461     char *argv[] = {"vnstat", "--nick", "Underground", NULL};
  462     int argc = sizeof(argv) / sizeof(char *) - 1;
  463 
  464     initparams(&p);
  465     suppress_output();
  466     parseargs(&p, argc, argv);
  467     ck_assert_int_eq(p.setalias, 1);
  468     ck_assert_str_eq(p.alias, "Underground");
  469 }
  470 END_TEST
  471 
  472 START_TEST(vnstat_parseargs_rename_requires_parameter)
  473 {
  474     PARAMS p;
  475     char *argv[] = {"vnstat", "--rename", NULL};
  476     int argc = sizeof(argv) / sizeof(char *) - 1;
  477 
  478     initparams(&p);
  479     suppress_output();
  480     parseargs(&p, argc, argv);
  481 }
  482 END_TEST
  483 
  484 START_TEST(vnstat_parseargs_limit_requires_parameter)
  485 {
  486     PARAMS p;
  487     char *argv[] = {"vnstat", "--limit", NULL};
  488     int argc = sizeof(argv) / sizeof(char *) - 1;
  489 
  490     initparams(&p);
  491     suppress_output();
  492     parseargs(&p, argc, argv);
  493 }
  494 END_TEST
  495 
  496 START_TEST(vnstat_parseargs_limit_cannot_be_negative)
  497 {
  498     PARAMS p;
  499     char *argv[] = {"vnstat", "--limit", "-1", NULL};
  500     int argc = sizeof(argv) / sizeof(char *) - 1;
  501 
  502     initparams(&p);
  503     suppress_output();
  504     parseargs(&p, argc, argv);
  505 }
  506 END_TEST
  507 
  508 START_TEST(vnstat_parseargs_limit_changes_defaults)
  509 {
  510     PARAMS p;
  511     char *argv[] = {"vnstat", "--limit", "1234", NULL};
  512     int argc = sizeof(argv) / sizeof(char *) - 1;
  513 
  514     initparams(&p);
  515     suppress_output();
  516     parseargs(&p, argc, argv);
  517 
  518     ck_assert_int_eq(cfg.listfivemins, 1234);
  519     ck_assert_int_eq(cfg.listhours, 1234);
  520     ck_assert_int_eq(cfg.listdays, 1234);
  521     ck_assert_int_eq(cfg.listmonths, 1234);
  522     ck_assert_int_eq(cfg.listyears, 1234);
  523     ck_assert_int_eq(cfg.listtop, 1234);
  524     ck_assert_int_eq(cfg.listjsonxml, 1234);
  525 }
  526 END_TEST
  527 
  528 START_TEST(vnstat_parseargs_limit_overrides)
  529 {
  530     PARAMS p;
  531     char *argv[] = {"vnstat", "-d", "5", "--limit", "234", NULL};
  532     int argc = sizeof(argv) / sizeof(char *) - 1;
  533 
  534     initparams(&p);
  535     suppress_output();
  536     parseargs(&p, argc, argv);
  537 
  538     ck_assert_int_eq(cfg.listfivemins, 234);
  539     ck_assert_int_eq(cfg.listhours, 234);
  540     ck_assert_int_eq(cfg.listdays, 234);
  541     ck_assert_int_eq(cfg.listmonths, 234);
  542     ck_assert_int_eq(cfg.listyears, 234);
  543     ck_assert_int_eq(cfg.listtop, 234);
  544     ck_assert_int_eq(cfg.listjsonxml, 234);
  545 }
  546 END_TEST
  547 
  548 START_TEST(vnstat_parseargs_limit_overrides_regardless_of_position)
  549 {
  550     PARAMS p;
  551     char *argv[] = {"vnstat", "--limit", "345", "-d", "5", NULL};
  552     int argc = sizeof(argv) / sizeof(char *) - 1;
  553 
  554     initparams(&p);
  555     suppress_output();
  556     parseargs(&p, argc, argv);
  557 
  558     ck_assert_int_eq(cfg.listfivemins, 345);
  559     ck_assert_int_eq(cfg.listhours, 345);
  560     ck_assert_int_eq(cfg.listdays, 345);
  561     ck_assert_int_eq(cfg.listmonths, 345);
  562     ck_assert_int_eq(cfg.listyears, 345);
  563     ck_assert_int_eq(cfg.listtop, 345);
  564     ck_assert_int_eq(cfg.listjsonxml, 345);
  565 }
  566 END_TEST
  567 
  568 START_TEST(vnstat_parseargs_iflist_gives_help)
  569 {
  570     PARAMS p;
  571     char *argv[] = {"vnstat", "--iflist", "--help", NULL};
  572     int argc = sizeof(argv) / sizeof(char *) - 1;
  573 
  574     initparams(&p);
  575     suppress_output();
  576     parseargs(&p, argc, argv);
  577 }
  578 END_TEST
  579 
  580 START_TEST(vnstat_parseargs_iflist_without_parameters_and_no_interfaces)
  581 {
  582     PARAMS p;
  583     char *argv[] = {"vnstat", "--iflist", NULL};
  584     int argc = sizeof(argv) / sizeof(char *) - 1;
  585 
  586     initparams(&p);
  587     remove_directory(TESTDIR);
  588     suppress_output();
  589     parseargs(&p, argc, argv);
  590 }
  591 END_TEST
  592 
  593 START_TEST(vnstat_parseargs_iflist_without_parameters_and_only_lo)
  594 {
  595     PARAMS p;
  596     char *argv[] = {"vnstat", "--iflist", NULL};
  597     int argc = sizeof(argv) / sizeof(char *) - 1;
  598 
  599     initparams(&p);
  600     fake_proc_net_dev("w", "lo", 0, 0, 0, 0);
  601     suppress_output();
  602     parseargs(&p, argc, argv);
  603 }
  604 END_TEST
  605 
  606 START_TEST(vnstat_parseargs_iflist_without_parameters)
  607 {
  608     PARAMS p;
  609     char *argv[] = {"vnstat", "--iflist", NULL};
  610     int argc = sizeof(argv) / sizeof(char *) - 1;
  611 
  612     initparams(&p);
  613     fake_proc_net_dev("w", "ethsomething", 0, 0, 0, 0);
  614     fake_proc_net_dev("a", "ethanything", 0, 0, 0, 0);
  615     suppress_output();
  616     parseargs(&p, argc, argv);
  617 }
  618 END_TEST
  619 
  620 START_TEST(vnstat_parseargs_iflist_with_verbose_mode)
  621 {
  622     PARAMS p;
  623     char *argv[] = {"vnstat", "--iflist", "0", NULL};
  624     int argc = sizeof(argv) / sizeof(char *) - 1;
  625 
  626     initparams(&p);
  627     fake_proc_net_dev("w", "ethsomething", 0, 0, 0, 0);
  628     fake_proc_net_dev("a", "ethanything", 0, 0, 0, 0);
  629     suppress_output();
  630     parseargs(&p, argc, argv);
  631 }
  632 END_TEST
  633 
  634 START_TEST(vnstat_parseargs_iflist_with_parseable_mode)
  635 {
  636     PARAMS p;
  637     char *argv[] = {"vnstat", "--iflist", "1", NULL};
  638     int argc = sizeof(argv) / sizeof(char *) - 1;
  639 
  640     initparams(&p);
  641     fake_proc_net_dev("w", "ethsomething", 0, 0, 0, 0);
  642     fake_proc_net_dev("a", "ethanything", 0, 0, 0, 0);
  643     suppress_output();
  644     parseargs(&p, argc, argv);
  645 }
  646 END_TEST
  647 
  648 START_TEST(vnstat_parseargs_dbiflist_gives_help)
  649 {
  650     PARAMS p;
  651     char *argv[] = {"vnstat", "--dbiflist", "unknown", NULL};
  652     int argc = sizeof(argv) / sizeof(char *) - 1;
  653 
  654     initparams(&p);
  655     suppress_output();
  656     parseargs(&p, argc, argv);
  657 }
  658 END_TEST
  659 
  660 START_TEST(vnstat_parseargs_dbiflist_with_no_database)
  661 {
  662     PARAMS p;
  663     char *argv[] = {"vnstat", "--dbiflist", NULL};
  664     int argc = sizeof(argv) / sizeof(char *) - 1;
  665 
  666     initparams(&p);
  667     suppress_output();
  668     parseargs(&p, argc, argv);
  669 }
  670 END_TEST
  671 
  672 START_TEST(vnstat_parseargs_dbiflist_without_parameters_and_no_interfaces)
  673 {
  674     int ret;
  675     PARAMS p;
  676     char *argv[] = {"vnstat", "--dbiflist", NULL};
  677     int argc = sizeof(argv) / sizeof(char *) - 1;
  678 
  679     initparams(&p);
  680     ret = db_open_rw(1);
  681     ck_assert_int_eq(ret, 1);
  682     suppress_output();
  683     parseargs(&p, argc, argv);
  684     ret = db_close();
  685     ck_assert_int_eq(ret, 1);
  686 }
  687 END_TEST
  688 
  689 START_TEST(vnstat_parseargs_dbiflist_without_parameters)
  690 {
  691     int ret;
  692     PARAMS p;
  693     char *argv[] = {"vnstat", "--dbiflist", NULL};
  694     int argc = sizeof(argv) / sizeof(char *) - 1;
  695 
  696     initparams(&p);
  697     ret = db_open_rw(1);
  698     ck_assert_int_eq(ret, 1);
  699     ret = db_addinterface("etheverything");
  700     ck_assert_int_eq(ret, 1);
  701     ret = db_addinterface("ethnothing");
  702     ck_assert_int_eq(ret, 1);
  703     suppress_output();
  704     parseargs(&p, argc, argv);
  705     ret = db_close();
  706     ck_assert_int_eq(ret, 1);
  707 }
  708 END_TEST
  709 
  710 START_TEST(vnstat_parseargs_dbiflist_with_verbose_mode)
  711 {
  712     int ret;
  713     PARAMS p;
  714     char *argv[] = {"vnstat", "--dbiflist", "0", NULL};
  715     int argc = sizeof(argv) / sizeof(char *) - 1;
  716 
  717     initparams(&p);
  718     ret = db_open_rw(1);
  719     ck_assert_int_eq(ret, 1);
  720     ret = db_addinterface("etheverything");
  721     ck_assert_int_eq(ret, 1);
  722     ret = db_addinterface("ethnothing");
  723     ck_assert_int_eq(ret, 1);
  724     suppress_output();
  725     parseargs(&p, argc, argv);
  726     ret = db_close();
  727     ck_assert_int_eq(ret, 1);
  728 }
  729 END_TEST
  730 
  731 START_TEST(vnstat_parseargs_dbiflist_with_parseable_mode)
  732 {
  733     int ret;
  734     PARAMS p;
  735     char *argv[] = {"vnstat", "--dbiflist", "1", NULL};
  736     int argc = sizeof(argv) / sizeof(char *) - 1;
  737 
  738     initparams(&p);
  739     ret = db_open_rw(1);
  740     ck_assert_int_eq(ret, 1);
  741     ret = db_addinterface("etheverything");
  742     ck_assert_int_eq(ret, 1);
  743     ret = db_addinterface("ethnothing");
  744     ck_assert_int_eq(ret, 1);
  745     suppress_output();
  746     parseargs(&p, argc, argv);
  747     ret = db_close();
  748     ck_assert_int_eq(ret, 1);
  749 }
  750 END_TEST
  751 
  752 START_TEST(vnstat_parseargs_can_select_interface_without_parameter)
  753 {
  754     PARAMS p;
  755     char *argv[] = {"vnstat", "ethsomething", NULL};
  756     int argc = sizeof(argv) / sizeof(char *) - 1;
  757 
  758     defaultcfg();
  759     initparams(&p);
  760     suppress_output();
  761     parseargs(&p, argc, argv);
  762     ck_assert_str_eq(p.interface, "ethsomething");
  763     ck_assert_int_eq(p.defaultiface, 0);
  764 }
  765 END_TEST
  766 
  767 START_TEST(vnstat_parseargs_can_select_interface_without_parameter_even_if_there_are_other_parameters)
  768 {
  769     PARAMS p;
  770     char *argv[] = {"vnstat", "-d", "ethsomethingtoo", NULL};
  771     int argc = sizeof(argv) / sizeof(char *) - 1;
  772 
  773     defaultcfg();
  774     initparams(&p);
  775     suppress_output();
  776     ck_assert_int_ne(cfg.qmode, 1);
  777     parseargs(&p, argc, argv);
  778     ck_assert_str_eq(p.interface, "ethsomethingtoo");
  779     ck_assert_int_eq(p.defaultiface, 0);
  780     ck_assert_int_eq(cfg.qmode, 1);
  781 }
  782 END_TEST
  783 
  784 START_TEST(vnstat_parseargs_can_select_interface_without_parameter_from_the_middle_of_the_parameter_list)
  785 {
  786     PARAMS p;
  787     char *argv[] = {"vnstat", "-m", "ethsomethings", "-ru", NULL};
  788     int argc = sizeof(argv) / sizeof(char *) - 1;
  789 
  790     defaultcfg();
  791     initparams(&p);
  792     suppress_output();
  793     debug = 1;
  794     ck_assert_int_ne(cfg.qmode, 2);
  795     parseargs(&p, argc, argv);
  796     ck_assert_str_eq(p.interface, "ethsomethings");
  797     ck_assert_int_eq(p.defaultiface, 0);
  798     ck_assert_int_eq(cfg.qmode, 2);
  799 }
  800 END_TEST
  801 
  802 START_TEST(vnstat_parseargs_knows_when_interface_name_is_too_long_even_without_parameter)
  803 {
  804     PARAMS p;
  805     char *argv[] = {"vnstat", "ethethethethethethethethethetheth", NULL};
  806     int argc = sizeof(argv) / sizeof(char *) - 1;
  807 
  808     defaultcfg();
  809     initparams(&p);
  810     suppress_output();
  811     parseargs(&p, argc, argv);
  812     ck_assert_int_eq(p.defaultiface, 1);
  813 }
  814 END_TEST
  815 
  816 void add_parseargs_tests(Suite *s)
  817 {
  818     TCase *tc_pa = tcase_create("ParseArgs");
  819     tcase_add_checked_fixture(tc_pa, setup, teardown);
  820     tcase_add_unchecked_fixture(tc_pa, setup, teardown);
  821     tcase_add_test(tc_pa, vnstat_parseargs_does_nothing_without_args);
  822     tcase_add_exit_test(tc_pa, vnstat_parseargs_can_help, 0);
  823     tcase_add_exit_test(tc_pa, vnstat_parseargs_can_longhelp, 0);
  824     tcase_add_exit_test(tc_pa, vnstat_parseargs_can_show_version, 0);
  825     tcase_add_exit_test(tc_pa, vnstat_parseargs_detects_unknown_parameters, 1);
  826     tcase_add_test(tc_pa, vnstat_parseargs_can_modify_settings);
  827     tcase_add_exit_test(tc_pa, vnstat_parseargs_does_not_allow_too_long_interface_names, 1);
  828     tcase_add_exit_test(tc_pa, vnstat_parseargs_style_requires_parameter, 1);
  829     tcase_add_exit_test(tc_pa, vnstat_parseargs_style_checks_parameter, 1);
  830     tcase_add_exit_test(tc_pa, vnstat_parseargs_knows_that_update_is_not_supported, 1);
  831     tcase_add_exit_test(tc_pa, vnstat_parseargs_dbdir_requires_a_directory, 1);
  832     tcase_add_exit_test(tc_pa, vnstat_parseargs_oneline_gives_help, 1);
  833     tcase_add_exit_test(tc_pa, vnstat_parseargs_xml_gives_help, 1);
  834     tcase_add_test(tc_pa, vnstat_parseargs_xml_without_extra_params);
  835     tcase_add_test(tc_pa, vnstat_parseargs_xml_with_mode);
  836     tcase_add_test(tc_pa, vnstat_parseargs_xml_with_limit);
  837     tcase_add_test(tc_pa, vnstat_parseargs_xml_with_mode_and_limit);
  838     tcase_add_exit_test(tc_pa, vnstat_parseargs_json_gives_help, 1);
  839     tcase_add_test(tc_pa, vnstat_parseargs_json_without_extra_params);
  840     tcase_add_test(tc_pa, vnstat_parseargs_json_with_mode);
  841     tcase_add_test(tc_pa, vnstat_parseargs_json_with_limit);
  842     tcase_add_test(tc_pa, vnstat_parseargs_json_with_mode_and_limit);
  843     tcase_add_exit_test(tc_pa, vnstat_parseargs_rateunit_gives_help, 1);
  844     tcase_add_exit_test(tc_pa, vnstat_parseargs_live_gives_help, 1);
  845     tcase_add_exit_test(tc_pa, vnstat_parseargs_begin_gives_help, 1);
  846     tcase_add_exit_test(tc_pa, vnstat_parseargs_end_gives_help, 1);
  847     tcase_add_exit_test(tc_pa, vnstat_parseargs_begin_validates_input, 1);
  848     tcase_add_exit_test(tc_pa, vnstat_parseargs_end_validates_input, 1);
  849     tcase_add_exit_test(tc_pa, vnstat_parseargs_can_show_config, 0);
  850     tcase_add_exit_test(tc_pa, vnstat_parseargs_iface_requires_parameter, 1);
  851     tcase_add_exit_test(tc_pa, vnstat_parseargs_locale_requires_parameter, 1);
  852     tcase_add_exit_test(tc_pa, vnstat_parseargs_setalias_requires_parameter, 1);
  853     tcase_add_test(tc_pa, vnstat_parseargs_setalias_still_supports_nick);
  854     tcase_add_exit_test(tc_pa, vnstat_parseargs_rename_requires_parameter, 1);
  855     tcase_add_exit_test(tc_pa, vnstat_parseargs_limit_requires_parameter, 1);
  856     tcase_add_exit_test(tc_pa, vnstat_parseargs_limit_cannot_be_negative, 1);
  857     tcase_add_test(tc_pa, vnstat_parseargs_limit_changes_defaults);
  858     tcase_add_test(tc_pa, vnstat_parseargs_limit_overrides);
  859     tcase_add_test(tc_pa, vnstat_parseargs_limit_overrides_regardless_of_position);
  860     tcase_add_exit_test(tc_pa, vnstat_parseargs_iflist_gives_help, 1);
  861     tcase_add_exit_test(tc_pa, vnstat_parseargs_iflist_without_parameters_and_no_interfaces, 1);
  862     tcase_add_exit_test(tc_pa, vnstat_parseargs_iflist_without_parameters_and_only_lo, 0);
  863     tcase_add_exit_test(tc_pa, vnstat_parseargs_iflist_without_parameters, 0);
  864     tcase_add_exit_test(tc_pa, vnstat_parseargs_iflist_with_verbose_mode, 0);
  865     tcase_add_exit_test(tc_pa, vnstat_parseargs_iflist_with_parseable_mode, 0);
  866     tcase_add_exit_test(tc_pa, vnstat_parseargs_dbiflist_gives_help, 1);
  867     tcase_add_exit_test(tc_pa, vnstat_parseargs_dbiflist_with_no_database, 1);
  868     tcase_add_exit_test(tc_pa, vnstat_parseargs_dbiflist_without_parameters_and_no_interfaces, 0);
  869     tcase_add_exit_test(tc_pa, vnstat_parseargs_dbiflist_without_parameters, 0);
  870     tcase_add_exit_test(tc_pa, vnstat_parseargs_dbiflist_with_verbose_mode, 0);
  871     tcase_add_exit_test(tc_pa, vnstat_parseargs_dbiflist_with_parseable_mode, 0);
  872     tcase_add_test(tc_pa, vnstat_parseargs_can_select_interface_without_parameter);
  873     tcase_add_test(tc_pa, vnstat_parseargs_can_select_interface_without_parameter_even_if_there_are_other_parameters);
  874     tcase_add_test(tc_pa, vnstat_parseargs_can_select_interface_without_parameter_from_the_middle_of_the_parameter_list);
  875     tcase_add_exit_test(tc_pa, vnstat_parseargs_knows_when_interface_name_is_too_long_even_without_parameter, 1);
  876     suite_add_tcase(s, tc_pa);
  877 }