From: Teemu Toivola Date: Wed, 10 Jan 2018 20:09:50 +0000 (+0200) Subject: improve cfg test coverage, related to #84 X-Git-Tag: v2.0~98^2~20 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e9b092284a298136add0950ad284acb5b1344c61;p=vnstat improve cfg test coverage, related to #84 --- diff --git a/src/cfg.c b/src/cfg.c index 4a1784e..75ca1c4 100644 --- a/src/cfg.c +++ b/src/cfg.c @@ -294,7 +294,7 @@ int loadcfg(const char *cfgfile) continue; } - if (!extractcfgvalue(value, cfgline, cfglen)) { + if (!extractcfgvalue(value, 512, cfgline, cfglen)) { if (debug) printf(" c: %s -> \"%s\" with no value, keeping default.\n", cfgline, cset[i].name); cset[i].found = 1; @@ -707,14 +707,13 @@ int opencfgfile(const char *cfgfile, FILE **fd) return 2; } -/* TODO: function specific testcases missing */ -int extractcfgvalue(char *value, const char *cfgline, int cfglen) { +int extractcfgvalue(char *value, const int valuelen, const char *cfgline, const int cfglen) { int i, j, linelen; linelen = (int)strlen(cfgline); - for (i=0; i<512; i++) { + for (i=0; inamelen>0) { strncpy_nt(cset->locc, value, cset->namelen); - cset->locc[cset->namelen-1]='\0'; if (debug) printf(" c: %s -> \"%s\": \"%s\"\n", cfgline, cset->name, cset->locc); } else if ( ( strlen(value)>1 && isdigit(value[1]) ) || isdigit(value[0]) ) { diff --git a/src/cfg.h b/src/cfg.h index a397e81..6b816fd 100644 --- a/src/cfg.h +++ b/src/cfg.h @@ -14,8 +14,8 @@ int loadcfg(const char *cfgfile); void validatecfg(void); void defaultcfg(void); int opencfgfile(const char *cfgfile, FILE **fd); -int extractcfgvalue(char *value, const char *cfgline, int cfglen); -int setcfgvalue(struct cfgsetting *cset, const char *value, const char *cfgline); +int extractcfgvalue(char *value, const int valuelen, const char *cfgline, const int cfglen); +int setcfgvalue(const struct cfgsetting *cset, const char *value, const char *cfgline); void configlocale(void); #endif diff --git a/tests/config_tests.c b/tests/config_tests.c index 9d5e4e9..a9e50a4 100644 --- a/tests/config_tests.c +++ b/tests/config_tests.c @@ -258,6 +258,208 @@ START_TEST(ibwlist_filled) } END_TEST +START_TEST(extractcfgvalue_can_extract) +{ + int ret; + char value[32], cfgline[32]; + + snprintf(cfgline, 32, "one 1"); + ret = extractcfgvalue(value, 32, cfgline, 3); + ck_assert_int_eq(ret, 1); + ck_assert_str_eq(value, "1"); +} +END_TEST + +START_TEST(extractcfgvalue_can_really_extract) +{ + int ret; + char value[32], cfgline[32]; + + snprintf(cfgline, 32, "one\t1"); + ret = extractcfgvalue(value, 32, cfgline, 3); + ck_assert_int_eq(ret, 1); + ck_assert_str_eq(value, "1"); + + snprintf(cfgline, 32, "one\t\t1"); + ret = extractcfgvalue(value, 32, cfgline, 3); + ck_assert_int_eq(ret, 1); + ck_assert_str_eq(value, "1"); + + snprintf(cfgline, 32, "one \t 1"); + ret = extractcfgvalue(value, 32, cfgline, 3); + ck_assert_int_eq(ret, 1); + ck_assert_str_eq(value, "1"); + + snprintf(cfgline, 32, "one \t 1 \t2"); + ret = extractcfgvalue(value, 32, cfgline, 3); + ck_assert_int_eq(ret, 4); + ck_assert_str_eq(value, "1 \t2"); + + snprintf(cfgline, 32, "one \"1\""); + ret = extractcfgvalue(value, 32, cfgline, 3); + ck_assert_int_eq(ret, 1); + ck_assert_str_eq(value, "1"); + + snprintf(cfgline, 32, "one\t\"1\""); + ret = extractcfgvalue(value, 32, cfgline, 3); + ck_assert_int_eq(ret, 1); + ck_assert_str_eq(value, "1"); + + snprintf(cfgline, 32, "one \t \"1\""); + ret = extractcfgvalue(value, 32, cfgline, 3); + ck_assert_int_eq(ret, 1); + ck_assert_str_eq(value, "1"); + + snprintf(cfgline, 32, "one \t \"1\" \t"); + ret = extractcfgvalue(value, 32, cfgline, 3); + ck_assert_int_eq(ret, 1); + ck_assert_str_eq(value, "1"); + + snprintf(cfgline, 32, "one \t \"1\" \t2"); + ret = extractcfgvalue(value, 32, cfgline, 3); + ck_assert_int_eq(ret, 1); + ck_assert_str_eq(value, "1"); + + snprintf(cfgline, 32, "one \t == \t \"1\" \t == well doh"); + ret = extractcfgvalue(value, 32, cfgline, 3); + ck_assert_int_eq(ret, 1); + ck_assert_str_eq(value, "1"); +} +END_TEST + +START_TEST(extractcfgvalue_knows_when_not_to_extract) +{ + int ret; + char value[32], cfgline[32]; + + snprintf(cfgline, 32, "one"); + ret = extractcfgvalue(value, 32, cfgline, 3); + ck_assert_int_eq(ret, 0); + ck_assert_str_eq(value, ""); +} +END_TEST + +START_TEST(extractcfgvalue_really_knows_when_not_to_extract) +{ + int ret; + char value[32], cfgline[32]; + + snprintf(cfgline, 32, "one \t "); + ret = extractcfgvalue(value, 32, cfgline, 3); + ck_assert_int_eq(ret, 0); + ck_assert_str_eq(value, ""); +} +END_TEST + +START_TEST(setcfgvalue_can_set_chars) +{ + int ret; + char target[32]; + struct cfgsetting cset[] = {{ "unused", target, 0, 32, 0 }}; + + ret = setcfgvalue(&cset[0], "one", "unused"); + ck_assert_int_eq(ret, 1); + ck_assert_str_eq(target, "one"); + + ret = setcfgvalue(&cset[0], "1", "unused"); + ck_assert_int_eq(ret, 1); + ck_assert_str_eq(target, "1"); + + ret = setcfgvalue(&cset[0], "-", "unused"); + ck_assert_int_eq(ret, 1); + ck_assert_str_eq(target, "-"); + + ret = setcfgvalue(&cset[0], "qwe rty uio ads", "unused"); + ck_assert_int_eq(ret, 1); + ck_assert_str_eq(target, "qwe rty uio ads"); +} +END_TEST + +START_TEST(setcfgvalue_can_set_ints) +{ + int ret, target; + struct cfgsetting cset[] = {{ "unused", 0, &target, 0, 0 }}; + + ret = setcfgvalue(&cset[0], "1", "unused"); + ck_assert_int_eq(ret, 1); + ck_assert_int_eq(target, 1); + + ret = setcfgvalue(&cset[0], "123", "unused"); + ck_assert_int_eq(ret, 1); + ck_assert_int_eq(target, 123); + + ret = setcfgvalue(&cset[0], "-1", "unused"); + ck_assert_int_eq(ret, 1); + ck_assert_int_eq(target, -1); + + ret = setcfgvalue(&cset[0], "-321", "unused"); + ck_assert_int_eq(ret, 1); + ck_assert_int_eq(target, -321); + + ret = setcfgvalue(&cset[0], "0", "unused"); + ck_assert_int_eq(ret, 1); + ck_assert_int_eq(target, 0); +} +END_TEST + +START_TEST(setcfgvalue_does_not_exceed_char_limit) +{ + int ret; + char target[10]; + struct cfgsetting cset[] = {{ "unused", target, 0, 5, 0 }}; + + ret = setcfgvalue(&cset[0], "one", "unused"); + ck_assert_int_eq(ret, 1); + ck_assert_str_eq(target, "one"); + + ret = setcfgvalue(&cset[0], "12345", "unused"); + ck_assert_int_eq(ret, 1); + ck_assert_str_eq(target, "1234"); + + ret = setcfgvalue(&cset[0], "12 5", "unused"); + ck_assert_int_eq(ret, 1); + ck_assert_str_eq(target, "12 "); +} +END_TEST + +START_TEST(setcfgvalue_can_do_nothing) +{ + int ret; + struct cfgsetting cset[] = {{ "unused", 0, 0, 0, 0 }}; + + ret = setcfgvalue(&cset[0], "nothing", "unused"); + ck_assert_int_eq(ret, 0); +} +END_TEST + +START_TEST(configlocale_does_not_crash) +{ + defaultcfg(); + + unsetenv("LC_ALL"); + snprintf(cfg.locale, 32, "en_US"); + configlocale(); + snprintf(cfg.locale, 32, "-"); + configlocale(); + + setenv("LC_ALL", "en_US", 1); + snprintf(cfg.locale, 32, "en_US"); + configlocale(); + ck_assert_int_eq(cfg.utflocale, 0); + snprintf(cfg.locale, 32, "-"); + configlocale(); + ck_assert_int_eq(cfg.utflocale, 0); + + setenv("LC_ALL", "en_US.UTF-8", 1); + snprintf(cfg.locale, 32, "en_US"); + configlocale(); + ck_assert_int_eq(cfg.utflocale, 1); + snprintf(cfg.locale, 32, "-"); + configlocale(); + ck_assert_int_eq(cfg.utflocale, 1); +} +END_TEST + void add_config_tests(Suite *s) { TCase *tc_config = tcase_create("Config"); @@ -280,5 +482,14 @@ void add_config_tests(Suite *s) tcase_add_test(tc_config, ibwflush_success); tcase_add_test(tc_config, ibwlist_empty); tcase_add_test(tc_config, ibwlist_filled); + tcase_add_test(tc_config, extractcfgvalue_can_extract); + tcase_add_test(tc_config, extractcfgvalue_can_really_extract); + tcase_add_test(tc_config, extractcfgvalue_knows_when_not_to_extract); + tcase_add_test(tc_config, extractcfgvalue_really_knows_when_not_to_extract); + tcase_add_test(tc_config, setcfgvalue_can_set_chars); + tcase_add_test(tc_config, setcfgvalue_can_set_ints); + tcase_add_test(tc_config, setcfgvalue_does_not_exceed_char_limit); + tcase_add_test(tc_config, setcfgvalue_can_do_nothing); + tcase_add_test(tc_config, configlocale_does_not_crash); suite_add_tcase(s, tc_config); }