Skip to content

Commit

Permalink
improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
vergoh committed Jan 12, 2025
1 parent 5804d26 commit bc184d0
Show file tree
Hide file tree
Showing 2 changed files with 222 additions and 1 deletion.
151 changes: 151 additions & 0 deletions tests/cli_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ START_TEST(vnstat_handlers_do_nothing_by_default)
handleaddinterface(&p);
handlesetalias(&p);
handletrafficmeters(&p);
handlemerge(&p);
ck_assert_int_eq(memcmp(&p, &b, sizeof(PARAMS)), 0);
}
END_TEST
Expand Down Expand Up @@ -1580,6 +1581,148 @@ START_TEST(validateinterface_knows_when_to_give_up_searching)
}
END_TEST

START_TEST(handlemerge_source_and_destination_cannot_be_the_same)
{
PARAMS p;

defaultcfg();
initparams(&p);
debug = 1;

p.merge = 1;
strncpy_nt(p.mergesrc, "fileA", 512);
strncpy_nt(p.mergedst, "fileA", 512);

suppress_output();
handlemerge(&p);
}
END_TEST

START_TEST(handlemerge_source_needs_to_be_provided)
{
PARAMS p;

defaultcfg();
initparams(&p);
debug = 1;

p.merge = 1;
strncpy_nt(p.mergedst, "fileA", 512);

suppress_output();
handlemerge(&p);
}
END_TEST

START_TEST(handlemerge_destination_needs_to_be_provided)
{
PARAMS p;

defaultcfg();
initparams(&p);
debug = 1;

p.merge = 1;
strncpy_nt(p.mergesrc, "fileA", 512);

suppress_output();
handlemerge(&p);
}
END_TEST

START_TEST(handlemerge_all_to_one_merge_is_not_supported)
{
PARAMS p;

defaultcfg();
initparams(&p);
debug = 1;

p.merge = 1;
strncpy_nt(p.mergesrc, "fileA", 512);
strncpy_nt(p.mergedst, "fileB:all_to_one", 512);

suppress_output();
handlemerge(&p);
}
END_TEST

START_TEST(handlemerge_source_file_must_exist)
{
PARAMS p;

defaultcfg();
initparams(&p);
debug = 1;

p.merge = 1;
strncpy_nt(p.mergesrc, "fileA_really_does_not_exist", 512);
strncpy_nt(p.mergedst, "fileB", 512);

suppress_output();
handlemerge(&p);
}
END_TEST

START_TEST(handlemerge_reports_merge_failure)
{
PARAMS p;

defaultcfg();
initparams(&p);
debug = 1;

p.merge = 1;
strncpy_nt(p.mergesrc, "Makefile", 512);
strncpy_nt(p.mergedst, "fileB", 512);

suppress_output();
handlemerge(&p);
}
END_TEST

START_TEST(parsedatabaseinterface_can_parse_strings)
{
int ret;
char database[512], interface[512];

database[0] = '\0';
interface[0] = '\0';
ret = parsedatabaseinterface("teststring", database, interface);
ck_assert_int_eq(ret, 1);
ck_assert_str_eq(database, "teststring");
ck_assert_str_eq(interface, "");

database[0] = '\0';
interface[0] = '\0';
ret = parsedatabaseinterface("file.db:someinterface", database, interface);
ck_assert_int_eq(ret, 1);
ck_assert_str_eq(database, "file.db");
ck_assert_str_eq(interface, "someinterface");

database[0] = '\0';
interface[0] = '\0';
ret = parsedatabaseinterface("file.db:someinterface:and_stuff", database, interface);
ck_assert_int_eq(ret, 1);
ck_assert_str_eq(database, "file.db");
ck_assert_str_eq(interface, "someinterface:and_stuff");
}
END_TEST

START_TEST(parsedatabaseinterface_knows_too_long_strings)
{
int ret;
char database[512], interface[512];

database[0] = '\0';
interface[0] = '\0';
ret = parsedatabaseinterface("longstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislongstringislong:string", database, interface);
ck_assert_int_eq(ret, 0);
ck_assert_str_eq(database, "");
ck_assert_str_eq(interface, "");
}
END_TEST

void add_cli_tests(Suite *s)
{
TCase *tc_cli = tcase_create("CLI");
Expand Down Expand Up @@ -1658,5 +1801,13 @@ void add_cli_tests(Suite *s)
tcase_add_exit_test(tc_cli, validateinterface_detects_if_not_all_interfaces_are_unique_for_merge, 1);
tcase_add_test(tc_cli, validateinterface_uses_all_matching_methods_if_no_match_for_exact_name_is_found);
tcase_add_exit_test(tc_cli, validateinterface_knows_when_to_give_up_searching, 1);
tcase_add_exit_test(tc_cli, handlemerge_source_and_destination_cannot_be_the_same, 1);
tcase_add_exit_test(tc_cli, handlemerge_source_needs_to_be_provided, 1);
tcase_add_exit_test(tc_cli, handlemerge_destination_needs_to_be_provided, 1);
tcase_add_exit_test(tc_cli, handlemerge_all_to_one_merge_is_not_supported, 1);
tcase_add_exit_test(tc_cli, handlemerge_source_file_must_exist, 1);
tcase_add_exit_test(tc_cli, handlemerge_reports_merge_failure, 1);
tcase_add_test(tc_cli, parsedatabaseinterface_can_parse_strings);
tcase_add_test(tc_cli, parsedatabaseinterface_knows_too_long_strings);
suite_add_tcase(s, tc_cli);
}
72 changes: 71 additions & 1 deletion tests/parseargs_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ 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",
"does_nothing", "-l", "1", "--remove", "-i", "ethsomething", "--style", "0", "--db", "/path/dbfile.db", "--dbdir",
"dbsomewhere", "-q", "-d", "1", "-m", "2", "-t", "3", "-s", "-y", "4", "-hg", "-h", "5", "-5", "6",
"--oneline", "b", "--xml", "h", "7", "--json", "d", "7", "-ru", "--rateunit", "0",
"--force", "--setalias", "super", "--begin", "2000-01-01",
Expand All @@ -83,6 +83,7 @@ START_TEST(vnstat_parseargs_can_modify_settings)
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.dbfile, "/path/dbfile.db");
ck_assert_str_eq(cfg.dbdir, "dbsomewhere");
ck_assert_int_eq(p.query, 1);
ck_assert_int_eq(p.force, 1);
Expand Down Expand Up @@ -173,6 +174,18 @@ START_TEST(vnstat_parseargs_style_checks_parameter)
}
END_TEST

START_TEST(vnstat_parseargs_db_requires_parameter)
{
PARAMS p;
char *argv[] = {"vnstat", "--db", 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;
Expand Down Expand Up @@ -981,6 +994,58 @@ START_TEST(vnstat_parseargs_alert_gives_help)
}
END_TEST

START_TEST(vnstat_parseargs_merge_gives_help_when_asked)
{
PARAMS p;
char *argv[] = {"vnstat", "--merge", "?", NULL};
int argc = sizeof(argv) / sizeof(char *) - 1;

initparams(&p);
suppress_output();
parseargs(&p, argc, argv);
}
END_TEST

START_TEST(vnstat_parseargs_merge_gives_help_without_parameters)
{
PARAMS p;
char *argv[] = {"vnstat", "--merge", NULL};
int argc = sizeof(argv) / sizeof(char *) - 1;

initparams(&p);
suppress_output();
parseargs(&p, argc, argv);
}
END_TEST

START_TEST(vnstat_parseargs_merge_gives_help_with_wrong_parameter_count)
{
PARAMS p;
char *argv[] = {"vnstat", "--merge", "somefile", NULL};
int argc = sizeof(argv) / sizeof(char *) - 1;

initparams(&p);
suppress_output();
parseargs(&p, argc, argv);
}
END_TEST

START_TEST(vnstat_parseargs_merge_can_be_configured)
{
PARAMS p;
char *argv[] = {"vnstat", "--merge", "/path/sourcefile", "/another_path/destinationfile", NULL};
int argc = sizeof(argv) / sizeof(char *) - 1;

initparams(&p);
debug = 1;
suppress_output();
parseargs(&p, argc, argv);
ck_assert_int_eq(p.merge, 1);
ck_assert_str_eq(p.mergesrc, "/path/sourcefile");
ck_assert_str_eq(p.mergedst, "/another_path/destinationfile");
}
END_TEST

void add_parseargs_tests(Suite *s)
{
TCase *tc_pa = tcase_create("ParseArgs");
Expand All @@ -997,6 +1062,7 @@ void add_parseargs_tests(Suite *s)
tcase_add_exit_test(tc_pa, vnstat_parseargs_does_not_allow_too_long_interface_merges, 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_db_requires_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);
Expand Down Expand Up @@ -1053,5 +1119,9 @@ void add_parseargs_tests(Suite *s)
tcase_add_test(tc_pa, vnstat_parseargs_95p);
tcase_add_test(tc_pa, vnstat_parseargs_95);
tcase_add_exit_test(tc_pa, vnstat_parseargs_alert_gives_help, 1);
tcase_add_exit_test(tc_pa, vnstat_parseargs_merge_gives_help_when_asked, 1);
tcase_add_exit_test(tc_pa, vnstat_parseargs_merge_gives_help_without_parameters, 1);
tcase_add_exit_test(tc_pa, vnstat_parseargs_merge_gives_help_with_wrong_parameter_count, 1);
tcase_add_test(tc_pa, vnstat_parseargs_merge_can_be_configured);
suite_add_tcase(s, tc_pa);
}

0 comments on commit bc184d0

Please sign in to comment.