From e59309d0b7ddb3edd80184310a5cf55d35f44153 Mon Sep 17 00:00:00 2001 From: Teemu Toivola Date: Sat, 14 Sep 2019 18:25:40 +0300 Subject: [PATCH] relocate parseargs() tests to separate file --- Makefile.am | 1 + Makefile.in | 24 ++- tests/cli_tests.c | 357 -------------------------------------- tests/parseargs_tests.c | 370 ++++++++++++++++++++++++++++++++++++++++ tests/parseargs_tests.h | 6 + tests/vnstat_tests.c | 2 + 6 files changed, 401 insertions(+), 359 deletions(-) create mode 100644 tests/parseargs_tests.c create mode 100644 tests/parseargs_tests.h diff --git a/Makefile.am b/Makefile.am index 54f528a..20578e6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -76,6 +76,7 @@ check_vnstat_SOURCES = tests/vnstat_tests.c tests/vnstat_tests.h \ tests/id_tests.c tests/id_tests.h \ tests/iflist_tests.c tests/iflist_tests.h \ tests/cli_tests.c tests/cli_tests.h \ + tests/parseargs_tests.c tests/parseargs_tests.h \ tests/daemon_tests.c tests/daemon_tests.h check_vnstat_SOURCES += src/cfg.c src/cfg.h \ diff --git a/Makefile.in b/Makefile.in index bf163b9..c038c3f 100644 --- a/Makefile.in +++ b/Makefile.in @@ -125,7 +125,8 @@ am__check_vnstat_SOURCES_DIST = tests/vnstat_tests.c \ tests/ifinfo_tests.h tests/misc_tests.c tests/misc_tests.h \ tests/fs_tests.c tests/fs_tests.h tests/id_tests.c \ tests/id_tests.h tests/iflist_tests.c tests/iflist_tests.h \ - tests/cli_tests.c tests/cli_tests.h tests/daemon_tests.c \ + tests/cli_tests.c tests/cli_tests.h tests/parseargs_tests.c \ + tests/parseargs_tests.h tests/daemon_tests.c \ tests/daemon_tests.h src/cfg.c src/cfg.h src/cfgoutput.c \ src/cfgoutput.h src/common.c src/common.h src/daemon.c \ src/daemon.h src/dbsql.c src/dbsql.h src/dbaccess.c \ @@ -152,6 +153,7 @@ am__dirstamp = $(am__leading_dot)dirstamp @HAVE_CHECK_TRUE@ tests/check_vnstat-id_tests.$(OBJEXT) \ @HAVE_CHECK_TRUE@ tests/check_vnstat-iflist_tests.$(OBJEXT) \ @HAVE_CHECK_TRUE@ tests/check_vnstat-cli_tests.$(OBJEXT) \ +@HAVE_CHECK_TRUE@ tests/check_vnstat-parseargs_tests.$(OBJEXT) \ @HAVE_CHECK_TRUE@ tests/check_vnstat-daemon_tests.$(OBJEXT) \ @HAVE_CHECK_TRUE@ src/check_vnstat-cfg.$(OBJEXT) \ @HAVE_CHECK_TRUE@ src/check_vnstat-cfgoutput.$(OBJEXT) \ @@ -699,7 +701,8 @@ vnstati_SOURCES = src/vnstati.c src/vnstati.h \ @HAVE_CHECK_TRUE@ tests/fs_tests.h tests/id_tests.c \ @HAVE_CHECK_TRUE@ tests/id_tests.h tests/iflist_tests.c \ @HAVE_CHECK_TRUE@ tests/iflist_tests.h tests/cli_tests.c \ -@HAVE_CHECK_TRUE@ tests/cli_tests.h tests/daemon_tests.c \ +@HAVE_CHECK_TRUE@ tests/cli_tests.h tests/parseargs_tests.c \ +@HAVE_CHECK_TRUE@ tests/parseargs_tests.h tests/daemon_tests.c \ @HAVE_CHECK_TRUE@ tests/daemon_tests.h src/cfg.c src/cfg.h \ @HAVE_CHECK_TRUE@ src/cfgoutput.c src/cfgoutput.h src/common.c \ @HAVE_CHECK_TRUE@ src/common.h src/daemon.c src/daemon.h \ @@ -914,6 +917,8 @@ tests/check_vnstat-iflist_tests.$(OBJEXT): tests/$(am__dirstamp) \ tests/$(DEPDIR)/$(am__dirstamp) tests/check_vnstat-cli_tests.$(OBJEXT): tests/$(am__dirstamp) \ tests/$(DEPDIR)/$(am__dirstamp) +tests/check_vnstat-parseargs_tests.$(OBJEXT): tests/$(am__dirstamp) \ + tests/$(DEPDIR)/$(am__dirstamp) tests/check_vnstat-daemon_tests.$(OBJEXT): tests/$(am__dirstamp) \ tests/$(DEPDIR)/$(am__dirstamp) src/$(am__dirstamp): @@ -1077,6 +1082,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@tests/$(DEPDIR)/check_vnstat-iflist_tests.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@tests/$(DEPDIR)/check_vnstat-image_tests.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@tests/$(DEPDIR)/check_vnstat-misc_tests.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@tests/$(DEPDIR)/check_vnstat-parseargs_tests.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@tests/$(DEPDIR)/check_vnstat-vnstat_tests.Po@am__quote@ .c.o: @@ -1263,6 +1269,20 @@ tests/check_vnstat-cli_tests.obj: tests/cli_tests.c @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_vnstat_CFLAGS) $(CFLAGS) -c -o tests/check_vnstat-cli_tests.obj `if test -f 'tests/cli_tests.c'; then $(CYGPATH_W) 'tests/cli_tests.c'; else $(CYGPATH_W) '$(srcdir)/tests/cli_tests.c'; fi` +tests/check_vnstat-parseargs_tests.o: tests/parseargs_tests.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_vnstat_CFLAGS) $(CFLAGS) -MT tests/check_vnstat-parseargs_tests.o -MD -MP -MF tests/$(DEPDIR)/check_vnstat-parseargs_tests.Tpo -c -o tests/check_vnstat-parseargs_tests.o `test -f 'tests/parseargs_tests.c' || echo '$(srcdir)/'`tests/parseargs_tests.c +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) tests/$(DEPDIR)/check_vnstat-parseargs_tests.Tpo tests/$(DEPDIR)/check_vnstat-parseargs_tests.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='tests/parseargs_tests.c' object='tests/check_vnstat-parseargs_tests.o' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_vnstat_CFLAGS) $(CFLAGS) -c -o tests/check_vnstat-parseargs_tests.o `test -f 'tests/parseargs_tests.c' || echo '$(srcdir)/'`tests/parseargs_tests.c + +tests/check_vnstat-parseargs_tests.obj: tests/parseargs_tests.c +@am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_vnstat_CFLAGS) $(CFLAGS) -MT tests/check_vnstat-parseargs_tests.obj -MD -MP -MF tests/$(DEPDIR)/check_vnstat-parseargs_tests.Tpo -c -o tests/check_vnstat-parseargs_tests.obj `if test -f 'tests/parseargs_tests.c'; then $(CYGPATH_W) 'tests/parseargs_tests.c'; else $(CYGPATH_W) '$(srcdir)/tests/parseargs_tests.c'; fi` +@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) tests/$(DEPDIR)/check_vnstat-parseargs_tests.Tpo tests/$(DEPDIR)/check_vnstat-parseargs_tests.Po +@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='tests/parseargs_tests.c' object='tests/check_vnstat-parseargs_tests.obj' libtool=no @AMDEPBACKSLASH@ +@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ +@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_vnstat_CFLAGS) $(CFLAGS) -c -o tests/check_vnstat-parseargs_tests.obj `if test -f 'tests/parseargs_tests.c'; then $(CYGPATH_W) 'tests/parseargs_tests.c'; else $(CYGPATH_W) '$(srcdir)/tests/parseargs_tests.c'; fi` + tests/check_vnstat-daemon_tests.o: tests/daemon_tests.c @am__fastdepCC_TRUE@ $(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(check_vnstat_CFLAGS) $(CFLAGS) -MT tests/check_vnstat-daemon_tests.o -MD -MP -MF tests/$(DEPDIR)/check_vnstat-daemon_tests.Tpo -c -o tests/check_vnstat-daemon_tests.o `test -f 'tests/daemon_tests.c' || echo '$(srcdir)/'`tests/daemon_tests.c @am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) tests/$(DEPDIR)/check_vnstat-daemon_tests.Tpo tests/$(DEPDIR)/check_vnstat-daemon_tests.Po diff --git a/tests/cli_tests.c b/tests/cli_tests.c index 12796c5..6285ebd 100644 --- a/tests/cli_tests.c +++ b/tests/cli_tests.c @@ -67,338 +67,6 @@ START_TEST(vnstat_handletrafficmeters_exists_when_interface_is_not_available) } END_TEST -START_TEST(vnstat_parseargs_does_nothing_without_args) -{ - PARAMS p; - char *argv[] = {"vnstat", "-h", NULL}; - int argc = sizeof(argv) / sizeof(char *) - 1; - - initparams(&p); - parseargs(&p, argc, argv); -} -END_TEST - -START_TEST(vnstat_parseargs_can_help) -{ - PARAMS p; - char *argv[] = {"vnstat", "--help", NULL}; - int argc = sizeof(argv) / sizeof(char *) - 1; - - initparams(&p); - debug = 1; - suppress_output(); - parseargs(&p, argc, argv); -} -END_TEST - -START_TEST(vnstat_parseargs_can_longhelp) -{ - PARAMS p; - char *argv[] = {"vnstat", "--longhelp", NULL}; - int argc = sizeof(argv) / sizeof(char *) - 1; - - initparams(&p); - suppress_output(); - parseargs(&p, argc, argv); -} -END_TEST - -START_TEST(vnstat_parseargs_can_show_version) -{ - PARAMS p; - char *argv[] = {"vnstat", "--version", NULL}; - int argc = sizeof(argv) / sizeof(char *) - 1; - - initparams(&p); - suppress_output(); - parseargs(&p, argc, argv); -} -END_TEST - -START_TEST(vnstat_parseargs_detects_unknown_parameters) -{ - PARAMS p; - char *argv[] = {"vnstat", "--something_fishy", NULL}; - int argc = sizeof(argv) / sizeof(char *) - 1; - - initparams(&p); - suppress_output(); - parseargs(&p, argc, argv); -} -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 - START_TEST(vnstat_handleremoveinterface_exits_if_no_interface_has_been_specified) { PARAMS p; @@ -1145,31 +813,6 @@ void add_cli_tests(Suite *s) tcase_add_test(tc_cli, vnstat_showlonghelp_does_not_crash); tcase_add_test(tc_cli, vnstat_handlers_do_nothing_by_default); tcase_add_exit_test(tc_cli, vnstat_handletrafficmeters_exists_when_interface_is_not_available, 1); - tcase_add_test(tc_cli, vnstat_parseargs_does_nothing_without_args); - tcase_add_exit_test(tc_cli, vnstat_parseargs_can_help, 0); - 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); tcase_add_exit_test(tc_cli, vnstat_handleremoveinterface_exits_if_no_interface_has_been_specified, 1); tcase_add_exit_test(tc_cli, vnstat_handleremoveinterface_exits_if_given_interface_does_not_exist, 1); tcase_add_exit_test(tc_cli, vnstat_handleremoveinterface_exits_if_force_is_not_used, 1); diff --git a/tests/parseargs_tests.c b/tests/parseargs_tests.c new file mode 100644 index 0000000..cbab7c4 --- /dev/null +++ b/tests/parseargs_tests.c @@ -0,0 +1,370 @@ +#include "common.h" +#include "vnstat_tests.h" +#include "vnstat_func.h" +#include "cfg.h" +#include "parseargs_tests.h" + +START_TEST(vnstat_parseargs_does_nothing_without_args) +{ + PARAMS p; + char *argv[] = {"vnstat", "-h", NULL}; + int argc = sizeof(argv) / sizeof(char *) - 1; + + initparams(&p); + parseargs(&p, argc, argv); +} +END_TEST + +START_TEST(vnstat_parseargs_can_help) +{ + PARAMS p; + char *argv[] = {"vnstat", "--help", NULL}; + int argc = sizeof(argv) / sizeof(char *) - 1; + + initparams(&p); + debug = 1; + suppress_output(); + parseargs(&p, argc, argv); +} +END_TEST + +START_TEST(vnstat_parseargs_can_longhelp) +{ + PARAMS p; + char *argv[] = {"vnstat", "--longhelp", NULL}; + int argc = sizeof(argv) / sizeof(char *) - 1; + + initparams(&p); + suppress_output(); + parseargs(&p, argc, argv); +} +END_TEST + +START_TEST(vnstat_parseargs_can_show_version) +{ + PARAMS p; + char *argv[] = {"vnstat", "--version", NULL}; + int argc = sizeof(argv) / sizeof(char *) - 1; + + initparams(&p); + suppress_output(); + parseargs(&p, argc, argv); +} +END_TEST + +START_TEST(vnstat_parseargs_detects_unknown_parameters) +{ + PARAMS p; + char *argv[] = {"vnstat", "--something_fishy", NULL}; + int argc = sizeof(argv) / sizeof(char *) - 1; + + initparams(&p); + suppress_output(); + parseargs(&p, argc, argv); +} +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_parseargs_tests(Suite *s) +{ + TCase *tc_pa = tcase_create("ParseArgs"); + tcase_add_checked_fixture(tc_pa, setup, teardown); + tcase_add_unchecked_fixture(tc_pa, setup, teardown); + tcase_add_test(tc_pa, vnstat_parseargs_does_nothing_without_args); + tcase_add_exit_test(tc_pa, vnstat_parseargs_can_help, 0); + tcase_add_exit_test(tc_pa, vnstat_parseargs_can_longhelp, 0); + tcase_add_exit_test(tc_pa, vnstat_parseargs_can_show_version, 0); + tcase_add_exit_test(tc_pa, vnstat_parseargs_detects_unknown_parameters, 1); + tcase_add_test(tc_pa, vnstat_parseargs_can_modify_settings); + tcase_add_exit_test(tc_pa, vnstat_parseargs_does_not_allow_too_long_interface_names, 1); + tcase_add_exit_test(tc_pa, vnstat_parseargs_style_requires_parameter, 1); + tcase_add_exit_test(tc_pa, vnstat_parseargs_style_checks_parameter, 1); + tcase_add_exit_test(tc_pa, vnstat_parseargs_knows_that_update_is_not_supported, 1); + tcase_add_exit_test(tc_pa, vnstat_parseargs_dbdir_requires_a_directory, 1); + tcase_add_exit_test(tc_pa, vnstat_parseargs_oneline_gives_help, 1); + tcase_add_exit_test(tc_pa, vnstat_parseargs_xml_gives_help, 1); + tcase_add_exit_test(tc_pa, vnstat_parseargs_json_gives_help, 1); + tcase_add_exit_test(tc_pa, vnstat_parseargs_rateunit_gives_help, 1); + tcase_add_exit_test(tc_pa, vnstat_parseargs_live_gives_help, 1); + tcase_add_exit_test(tc_pa, vnstat_parseargs_begin_gives_help, 1); + tcase_add_exit_test(tc_pa, vnstat_parseargs_end_gives_help, 1); + tcase_add_exit_test(tc_pa, vnstat_parseargs_begin_validates_input, 1); + tcase_add_exit_test(tc_pa, vnstat_parseargs_end_validates_input, 1); + tcase_add_exit_test(tc_pa, vnstat_parseargs_can_show_config, 0); + tcase_add_exit_test(tc_pa, vnstat_parseargs_iface_requires_parameter, 1); + tcase_add_exit_test(tc_pa, vnstat_parseargs_locale_requires_parameter, 1); + tcase_add_exit_test(tc_pa, vnstat_parseargs_setalias_requires_parameter, 1); + tcase_add_exit_test(tc_pa, vnstat_parseargs_rename_requires_parameter, 1); + suite_add_tcase(s, tc_pa); +} diff --git a/tests/parseargs_tests.h b/tests/parseargs_tests.h new file mode 100644 index 0000000..8c0b23c --- /dev/null +++ b/tests/parseargs_tests.h @@ -0,0 +1,6 @@ +#ifndef PARSEARGS_TESTS_H +#define PARSEARGS_TESTS_H + +void add_parseargs_tests(Suite *s); + +#endif diff --git a/tests/vnstat_tests.c b/tests/vnstat_tests.c index ee3d1b3..d437a89 100644 --- a/tests/vnstat_tests.c +++ b/tests/vnstat_tests.c @@ -12,6 +12,7 @@ #include "id_tests.h" #include "iflist_tests.h" #include "cli_tests.h" +#include "parseargs_tests.h" #if defined(HAVE_IMAGE) #include "image_tests.h" #endif @@ -56,6 +57,7 @@ Suite *test_suite(void) add_id_tests(s); add_iflist_tests(s); add_cli_tests(s); + add_parseargs_tests(s); #if defined(HAVE_IMAGE) add_image_tests(s); #endif -- 2.40.0