}
} else if ((strcmp(argv[currentarg], "--style")) == 0) {
if (currentarg + 1 < argc && isdigit(argv[currentarg + 1][0])) {
+ cfg.ostyle = atoi(argv[currentarg + 1]);
if (cfg.ostyle > 4 || cfg.ostyle < 0) {
printf("Error: Invalid style parameter \"%d\" for --style.\n", cfg.ostyle);
printf(" Valid parameters:\n");
printf(" and show raw values in --oneline\n");
exit(EXIT_FAILURE);
}
- cfg.ostyle = atoi(argv[currentarg + 1]);
if (debug)
printf("Style changed: %d\n", cfg.ostyle);
currentarg++;
exit(EXIT_FAILURE);
}
currentarg++;
+ continue;
}
} else if ((strcmp(argv[currentarg], "-m") == 0) || (strcmp(argv[currentarg], "--months") == 0)) {
cfg.qmode = 2;
exit(EXIT_FAILURE);
}
currentarg++;
+ continue;
}
} else if ((strcmp(argv[currentarg], "-t") == 0) || (strcmp(argv[currentarg], "--top") == 0)) {
cfg.qmode = 3;
exit(EXIT_FAILURE);
}
currentarg++;
+ continue;
}
} else if ((strcmp(argv[currentarg], "-s") == 0) || (strcmp(argv[currentarg], "--short") == 0)) {
cfg.qmode = 5;
exit(EXIT_FAILURE);
}
currentarg++;
+ continue;
}
} else if ((strcmp(argv[currentarg], "-hg") == 0) || (strcmp(argv[currentarg], "--hoursgraph") == 0)) {
cfg.qmode = 7;
exit(EXIT_FAILURE);
}
currentarg++;
+ continue;
}
} else if ((strcmp(argv[currentarg], "-5") == 0) || (strcmp(argv[currentarg], "--fiveminutes") == 0)) {
cfg.qmode = 12;
exit(EXIT_FAILURE);
}
currentarg++;
+ continue;
}
} else if (strcmp(argv[currentarg], "--oneline") == 0) {
cfg.qmode = 9;
if (argv[currentarg + 1][0] == 'b') {
cfg.ostyle = 4;
currentarg++;
+ continue;
} else {
printf("Error: Invalid mode parameter \"%s\" for --oneline.\n", argv[currentarg + 1]);
printf(" Valid parameters:\n");
}
}
} else if (strcmp(argv[currentarg], "--xml") == 0) {
+ cfg.qmode = 8;
if (currentarg + 1 < argc && argv[currentarg + 1][0] != '-' && !isdigit(argv[currentarg + 1][0])) {
p->xmlmode = argv[currentarg + 1][0];
if (strlen(argv[currentarg + 1]) != 1 || strchr("afhdmyt", p->xmlmode) == NULL) {
exit(EXIT_FAILURE);
}
currentarg++;
+ continue;
}
if (currentarg + 1 < argc && isdigit(argv[currentarg + 1][0])) {
cfg.listjsonxml = atoi(argv[currentarg + 1]);
exit(EXIT_FAILURE);
}
currentarg++;
+ continue;
}
- cfg.qmode = 8;
} else if (strcmp(argv[currentarg], "--json") == 0) {
+ cfg.qmode = 10;
if (currentarg + 1 < argc && argv[currentarg + 1][0] != '-' && !isdigit(argv[currentarg + 1][0])) {
p->jsonmode = argv[currentarg + 1][0];
if (strlen(argv[currentarg + 1]) != 1 || strchr("afhdmyt", p->jsonmode) == NULL) {
exit(EXIT_FAILURE);
}
currentarg++;
+ continue;
}
if (currentarg + 1 < argc && isdigit(argv[currentarg + 1][0])) {
cfg.listjsonxml = atoi(argv[currentarg + 1]);
exit(EXIT_FAILURE);
}
currentarg++;
+ continue;
}
- cfg.qmode = 10;
} else if ((strcmp(argv[currentarg], "-ru") == 0) || (strcmp(argv[currentarg], "--rateunit")) == 0) {
if (currentarg + 1 < argc && isdigit(argv[currentarg + 1][0])) {
+ cfg.rateunit = atoi(argv[currentarg + 1]);
if (cfg.rateunit > 1 || cfg.rateunit < 0) {
printf("Error: Invalid parameter \"%d\" for --rateunit.\n", cfg.rateunit);
printf(" Valid parameters:\n");
printf(" 1 - bits\n");
exit(EXIT_FAILURE);
}
- cfg.rateunit = atoi(argv[currentarg + 1]);
if (debug)
printf("Rateunit changed: %d\n", cfg.rateunit);
currentarg++;
if (currentarg + 1 < argc && isdigit(argv[currentarg + 1][0])) {
cfg.sampletime = atoi(argv[currentarg + 1]);
currentarg++;
- p->traffic = 1;
- p->query = 0;
- continue;
}
p->traffic = 1;
p->query = 0;
+ continue;
} else if ((strcmp(argv[currentarg], "-l") == 0) || (strcmp(argv[currentarg], "--live") == 0)) {
if (currentarg + 1 < argc && argv[currentarg + 1][0] != '-') {
- if (!isdigit(argv[currentarg + 1][0]) || p->livemode > 1 || p->livemode < 0) {
+ if (!isdigit(argv[currentarg + 1][0]) || atoi(argv[currentarg + 1]) > 1 || atoi(argv[currentarg + 1]) < 0) {
printf("Error: Invalid mode parameter \"%s\" for -l / --live.\n", argv[currentarg + 1]);
printf(" Valid parameters:\n");
printf(" 0 - show packets per second (default)\n");
}
p->livetraffic = 1;
p->query = 0;
+ continue;
} else if (strcmp(argv[currentarg], "--force") == 0) {
p->force = 1;
} else if (strcmp(argv[currentarg], "--showconfig") == 0) {
}
strncpy_nt(p->databegin, argv[currentarg + 1], 18);
currentarg++;
+ continue;
} else {
printf("Error: Date of format YYYY-MM-DD HH:MM or YYYY-MM-DD for %s missing.\n", argv[currentarg]);
exit(EXIT_FAILURE);
}
strncpy_nt(p->dataend, argv[currentarg + 1], 18);
currentarg++;
+ continue;
} else {
printf("Error: Date of format YYYY-MM-DD HH:MM or YYYY-MM-DD for %s missing.\n", argv[currentarg]);
exit(EXIT_FAILURE);
printf("vnStat %s by Teemu Toivola <tst at iki dot fi>\n", getversion());
exit(EXIT_SUCCESS);
} else {
- printf("Unknown parameter \"%s\". Use --help for help->\n", argv[currentarg]);
+ printf("Unknown parameter \"%s\". Use --help for help.\n", argv[currentarg]);
exit(EXIT_FAILURE);
}
}
}
END_TEST
+START_TEST(vnstat_parseargs_can_modify_settings)
+{
+ PARAMS p;
+ char *argv[] = {"vnstat", "--debug", "--traffic", "12", "--add", "--rename", "aname", "--config", \
+ "does_nothing", "-l", "1", "--remove", "-i", "ethsomething", "--style", "0", "--dbdir", \
+ "dbsomewhere", "-q", "-d", "1", "-m", "2", "-t", "3", "-s", "-y", "4", "-hg", "-h", "5", "-5", "6", \
+ "--oneline", "b", "--xml", "h", "--json", "d", "-ru", "--rateunit", "0", \
+ "--force", "--setalias", "super", "--begin", "2000-01-01", \
+ "--end", "2001-01-01", NULL};
+ int argc = sizeof(argv) / sizeof(char*) - 1;
+
+ defaultcfg();
+ initparams(&p);
+ suppress_output();
+ parseargs(&p, argc, argv);
+
+ ck_assert_str_eq(p.interface, "ethsomething");
+ ck_assert_int_eq(p.defaultiface, 0);
+ ck_assert_int_eq(cfg.ostyle, 4);
+ ck_assert_str_eq(cfg.dbdir, "dbsomewhere");
+ ck_assert_int_eq(p.query, 1);
+ ck_assert_int_eq(p.force, 1);
+ ck_assert_int_eq(cfg.qmode, 10);
+ ck_assert_int_eq(p.jsonmode, 'd');
+ ck_assert_int_eq(p.xmlmode, 'h');
+ ck_assert_int_eq(cfg.rateunit, 0);
+ ck_assert_int_eq(p.setalias, 1);
+ ck_assert_str_eq(p.alias, "super");
+ ck_assert_str_eq(p.newifname, "aname");
+ ck_assert_int_eq(p.renameiface, 1);
+ ck_assert_int_eq(p.removeiface, 1);
+ ck_assert_int_eq(p.traffic, 1);
+ ck_assert_int_eq(cfg.sampletime, 12);
+ ck_assert_int_eq(p.livetraffic, 1);
+ ck_assert_int_eq(p.livemode, 1);
+ ck_assert_int_eq(cfg.listdays, 1);
+ ck_assert_int_eq(cfg.listmonths, 2);
+ ck_assert_int_eq(cfg.listtop, 3);
+ ck_assert_int_eq(cfg.listyears, 4);
+ ck_assert_int_eq(cfg.listhours, 5);
+ ck_assert_int_eq(cfg.listfivemins, 6);
+}
+END_TEST
+
+START_TEST(vnstat_parseargs_does_not_allow_too_long_interface_names)
+{
+ PARAMS p;
+ char *argv[] = {"vnstat", "--iface", "12345678901234567890123456789012", NULL};
+ int argc = sizeof(argv) / sizeof(char*) - 1;
+
+ initparams(&p);
+ suppress_output();
+ parseargs(&p, argc, argv);
+}
+END_TEST
+
+START_TEST(vnstat_parseargs_style_requires_parameter)
+{
+ PARAMS p;
+ char *argv[] = {"vnstat", "--style", NULL};
+ int argc = sizeof(argv) / sizeof(char*) - 1;
+
+ initparams(&p);
+ suppress_output();
+ parseargs(&p, argc, argv);
+}
+END_TEST
+
+START_TEST(vnstat_parseargs_style_checks_parameter)
+{
+ PARAMS p;
+ char *argv[] = {"vnstat", "--style", "9001", NULL};
+ int argc = sizeof(argv) / sizeof(char*) - 1;
+
+ initparams(&p);
+ suppress_output();
+ parseargs(&p, argc, argv);
+}
+END_TEST
+
+START_TEST(vnstat_parseargs_knows_that_update_is_not_supported)
+{
+ PARAMS p;
+ char *argv[] = {"vnstat", "-u", NULL};
+ int argc = sizeof(argv) / sizeof(char*) - 1;
+
+ initparams(&p);
+ suppress_output();
+ parseargs(&p, argc, argv);
+}
+END_TEST
+
+START_TEST(vnstat_parseargs_dbdir_requires_a_directory)
+{
+ PARAMS p;
+ char *argv[] = {"vnstat", "--dbdir", NULL};
+ int argc = sizeof(argv) / sizeof(char*) - 1;
+
+ initparams(&p);
+ suppress_output();
+ parseargs(&p, argc, argv);
+}
+END_TEST
+
+START_TEST(vnstat_parseargs_oneline_gives_help)
+{
+ PARAMS p;
+ char *argv[] = {"vnstat", "--oneline", "a", NULL};
+ int argc = sizeof(argv) / sizeof(char*) - 1;
+
+ initparams(&p);
+ suppress_output();
+ parseargs(&p, argc, argv);
+}
+END_TEST
+
+START_TEST(vnstat_parseargs_xml_gives_help)
+{
+ PARAMS p;
+ char *argv[] = {"vnstat", "--xml", "b", NULL};
+ int argc = sizeof(argv) / sizeof(char*) - 1;
+
+ initparams(&p);
+ suppress_output();
+ parseargs(&p, argc, argv);
+}
+END_TEST
+
+START_TEST(vnstat_parseargs_json_gives_help)
+{
+ PARAMS p;
+ char *argv[] = {"vnstat", "--json", "b", NULL};
+ int argc = sizeof(argv) / sizeof(char*) - 1;
+
+ initparams(&p);
+ suppress_output();
+ parseargs(&p, argc, argv);
+}
+END_TEST
+
+START_TEST(vnstat_parseargs_rateunit_gives_help)
+{
+ PARAMS p;
+ char *argv[] = {"vnstat", "--rateunit", "2", NULL};
+ int argc = sizeof(argv) / sizeof(char*) - 1;
+
+ initparams(&p);
+ suppress_output();
+ parseargs(&p, argc, argv);
+}
+END_TEST
+
+START_TEST(vnstat_parseargs_live_gives_help)
+{
+ PARAMS p;
+ char *argv[] = {"vnstat", "--live", "2", NULL};
+ int argc = sizeof(argv) / sizeof(char*) - 1;
+
+ initparams(&p);
+ suppress_output();
+ parseargs(&p, argc, argv);
+}
+END_TEST
+
+START_TEST(vnstat_parseargs_begin_gives_help)
+{
+ PARAMS p;
+ char *argv[] = {"vnstat", "--begin", NULL};
+ int argc = sizeof(argv) / sizeof(char*) - 1;
+
+ initparams(&p);
+ suppress_output();
+ parseargs(&p, argc, argv);
+}
+END_TEST
+
+START_TEST(vnstat_parseargs_end_gives_help)
+{
+ PARAMS p;
+ char *argv[] = {"vnstat", "--end", NULL};
+ int argc = sizeof(argv) / sizeof(char*) - 1;
+
+ initparams(&p);
+ suppress_output();
+ parseargs(&p, argc, argv);
+}
+END_TEST
+
+START_TEST(vnstat_parseargs_begin_validates_input)
+{
+ PARAMS p;
+ char *argv[] = {"vnstat", "--begin", "world_domination", NULL};
+ int argc = sizeof(argv) / sizeof(char*) - 1;
+
+ initparams(&p);
+ suppress_output();
+ parseargs(&p, argc, argv);
+}
+END_TEST
+
+START_TEST(vnstat_parseargs_end_validates_input)
+{
+ PARAMS p;
+ char *argv[] = {"vnstat", "--end", "what?", NULL};
+ int argc = sizeof(argv) / sizeof(char*) - 1;
+
+ initparams(&p);
+ suppress_output();
+ parseargs(&p, argc, argv);
+}
+END_TEST
+
+START_TEST(vnstat_parseargs_can_show_config)
+{
+ PARAMS p;
+ char *argv[] = {"vnstat", "--showconfig", NULL};
+ int argc = sizeof(argv) / sizeof(char*) - 1;
+
+ initparams(&p);
+ suppress_output();
+ parseargs(&p, argc, argv);
+}
+END_TEST
+
+START_TEST(vnstat_parseargs_iface_requires_parameter)
+{
+ PARAMS p;
+ char *argv[] = {"vnstat", "--iface", NULL};
+ int argc = sizeof(argv) / sizeof(char*) - 1;
+
+ initparams(&p);
+ suppress_output();
+ parseargs(&p, argc, argv);
+}
+END_TEST
+
+START_TEST(vnstat_parseargs_locale_requires_parameter)
+{
+ PARAMS p;
+ char *argv[] = {"vnstat", "--locale", NULL};
+ int argc = sizeof(argv) / sizeof(char*) - 1;
+
+ initparams(&p);
+ suppress_output();
+ parseargs(&p, argc, argv);
+}
+END_TEST
+
+START_TEST(vnstat_parseargs_setalias_requires_parameter)
+{
+ PARAMS p;
+ char *argv[] = {"vnstat", "--setalias", NULL};
+ int argc = sizeof(argv) / sizeof(char*) - 1;
+
+ initparams(&p);
+ suppress_output();
+ parseargs(&p, argc, argv);
+}
+END_TEST
+
+START_TEST(vnstat_parseargs_rename_requires_parameter)
+{
+ PARAMS p;
+ char *argv[] = {"vnstat", "--rename", NULL};
+ int argc = sizeof(argv) / sizeof(char*) - 1;
+
+ initparams(&p);
+ suppress_output();
+ parseargs(&p, argc, argv);
+}
+END_TEST
+
void add_cli_tests(Suite *s)
{
TCase *tc_cli = tcase_create("CLI");
tcase_add_exit_test(tc_cli, vnstat_parseargs_can_longhelp, 0);
tcase_add_exit_test(tc_cli, vnstat_parseargs_can_show_version, 0);
tcase_add_exit_test(tc_cli, vnstat_parseargs_detects_unknown_parameters, 1);
+ tcase_add_test(tc_cli, vnstat_parseargs_can_modify_settings);
+ tcase_add_exit_test(tc_cli, vnstat_parseargs_does_not_allow_too_long_interface_names, 1);
+ tcase_add_exit_test(tc_cli, vnstat_parseargs_style_requires_parameter, 1);
+ tcase_add_exit_test(tc_cli, vnstat_parseargs_style_checks_parameter, 1);
+ tcase_add_exit_test(tc_cli, vnstat_parseargs_knows_that_update_is_not_supported, 1);
+ tcase_add_exit_test(tc_cli, vnstat_parseargs_dbdir_requires_a_directory, 1);
+ tcase_add_exit_test(tc_cli, vnstat_parseargs_oneline_gives_help, 1);
+ tcase_add_exit_test(tc_cli, vnstat_parseargs_xml_gives_help, 1);
+ tcase_add_exit_test(tc_cli, vnstat_parseargs_json_gives_help, 1);
+ tcase_add_exit_test(tc_cli, vnstat_parseargs_rateunit_gives_help, 1);
+ tcase_add_exit_test(tc_cli, vnstat_parseargs_live_gives_help, 1);
+ tcase_add_exit_test(tc_cli, vnstat_parseargs_begin_gives_help, 1);
+ tcase_add_exit_test(tc_cli, vnstat_parseargs_end_gives_help, 1);
+ tcase_add_exit_test(tc_cli, vnstat_parseargs_begin_validates_input, 1);
+ tcase_add_exit_test(tc_cli, vnstat_parseargs_end_validates_input, 1);
+ tcase_add_exit_test(tc_cli, vnstat_parseargs_can_show_config, 0);
+ tcase_add_exit_test(tc_cli, vnstat_parseargs_iface_requires_parameter, 1);
+ tcase_add_exit_test(tc_cli, vnstat_parseargs_locale_requires_parameter, 1);
+ tcase_add_exit_test(tc_cli, vnstat_parseargs_setalias_requires_parameter, 1);
+ tcase_add_exit_test(tc_cli, vnstat_parseargs_rename_requires_parameter, 1);
suite_add_tcase(s, tc_cli);
}