Add better test code coverage

This commit is contained in:
Tim Rühsen 2016-09-26 15:15:34 +02:00
parent 5ebc24f0e0
commit 598a78b2de
6 changed files with 110 additions and 9 deletions

View File

@ -21,12 +21,26 @@ dist-hook:
cp -p $(PSL_TESTFILE) $(distdir)/list/tests cp -p $(PSL_TESTFILE) $(distdir)/list/tests
clean-local: clean-local:
rm -rf */*.gc?? libpsl.info lcov rm -rf */*.gc?? */*/*.gc?? libpsl.info lcov
check-coverage: check-coverage:
$(MAKE) clean if test -z "$(XLIB)"; then \
lcov --no-external --capture --initial --directory . --output-file libpsl.info CFLAGS=$$CFLAGS" --coverage -O0" LDFLAGS=$$LDFLAGS" --coverage" ./configure --disable-runtime --disable-builtin; \
CFLAGS=$$CFLAGS" --coverage" LDFLAGS=$$LDFLAGS" --coverage" ./configure && $(MAKE) check else \
lcov --no-external --capture --directory . --output-file libpsl.info CFLAGS=$$CFLAGS" --coverage -O0" LDFLAGS=$$LDFLAGS" --coverage" ./configure --enable-runtime=$(XLIB) --enable-builtin=$(XLIB); \
# lcov --remove libpsl.info 'tests/*.c' 'src/lookup_*' 'src/psl2c.c' -o libpsl.info fi
$(MAKE) clean && $(MAKE)
lcov --no-external --capture --initial --directory src --output-file libpsl.info
$(MAKE) check
lcov --no-external --capture --directory src --output-file libpsl.info
lcov --remove libpsl.info 'src/psl2c.c' -o libpsl.info
genhtml --prefix . --ignore-errors source libpsl.info --legend --title "libpsl" --output-directory=lcov genhtml --prefix . --ignore-errors source libpsl.info --legend --title "libpsl" --output-directory=lcov
check-coverage-libidn:
XLIB=libidn $(MAKE) check-coverage
check-coverage-libidn2:
XLIB=libidn2 $(MAKE) check-coverage
check-coverage-libicu:
XLIB=libicu $(MAKE) check-coverage

View File

@ -1728,7 +1728,7 @@ out:
if ((tmp = u8_tolower((uint8_t *)str, len, 0, UNINORM_NFKC, NULL, &len))) { if ((tmp = u8_tolower((uint8_t *)str, len, 0, UNINORM_NFKC, NULL, &len))) {
ret = PSL_SUCCESS; ret = PSL_SUCCESS;
if (lower) { if (lower) {
*lower = tmp; *lower = (char*)tmp;
tmp = NULL; tmp = NULL;
} else } else
free(tmp); free(tmp);

View File

@ -23,3 +23,12 @@ check_PROGRAMS = $(PSL_TESTS)
TESTS_ENVIRONMENT = TESTS_VALGRIND="@VALGRIND_ENVIRONMENT@" TESTS_ENVIRONMENT = TESTS_VALGRIND="@VALGRIND_ENVIRONMENT@"
TESTS = $(PSL_TESTS) TESTS = $(PSL_TESTS)
# dafsa.psl must be created before any test is executed
# check-local target works in parallel to the tests, so the test suite will likely fail
BUILT_SOURCES = dafsa.psl
dafsa.psl:
$(top_srcdir)/src/psl-make-dafsa --input-format=psl --output-format=binary "$(PSL_FILE)" psl.dafsa
clean-local:
rm -f psl.dafsa

View File

@ -65,6 +65,7 @@ static void test_psl(void)
{ "www.his.name", "his.name", 1 }, { "www.his.name", "his.name", 1 },
{ "www.his.name", "name", 0 }, { "www.his.name", "name", 0 },
{ "www.example.com", "www.example.com", 1 }, { "www.example.com", "www.example.com", 1 },
{ "www.example.com", "wwww.example.com", 0 },
{ "www.example.com", "example.com", 1 }, { "www.example.com", "example.com", 1 },
{ "www.example.com", "com", 0 }, /* not accepted by normalization (PSL rule 'com') */ { "www.example.com", "com", 0 }, /* not accepted by normalization (PSL rule 'com') */
{ "www.example.com", "example.org", 0 }, { "www.example.com", "example.org", 0 },
@ -77,6 +78,8 @@ static void test_psl(void)
{ "2a00:1450:4013:c01::8b", ":1450:4013:c01::8b", 0 }, /* IPv6 address, partial match */ { "2a00:1450:4013:c01::8b", ":1450:4013:c01::8b", 0 }, /* IPv6 address, partial match */
{ "::ffff:192.1.123.2", "::ffff:192.1.123.2", 1 }, /* IPv6 address dotted-quad, full match */ { "::ffff:192.1.123.2", "::ffff:192.1.123.2", 1 }, /* IPv6 address dotted-quad, full match */
{ "::ffff:192.1.123.2", ".1.123.2", 0 }, /* IPv6 address dotted-quad, partial match */ { "::ffff:192.1.123.2", ".1.123.2", 0 }, /* IPv6 address dotted-quad, partial match */
{ NULL, ".1.123.2", 0 },
{ "hiho", NULL, 0 },
}; };
unsigned it; unsigned it;
psl_ctx_t *psl; psl_ctx_t *psl;
@ -98,6 +101,9 @@ static void test_psl(void)
} }
} }
/* do checks to cover more code paths in libpsl */
psl_is_cookie_domain_acceptable(NULL, "example.com", "example.com");
psl_free(psl); psl_free(psl);
} }

View File

@ -131,7 +131,7 @@ static void test_psl_entry(const psl_ctx_t *psl, const char *domain, int type)
static void test_psl(void) static void test_psl(void)
{ {
FILE *fp; FILE *fp;
psl_ctx_t *psl; psl_ctx_t *psl, *psl3;
const psl_ctx_t *psl2; const psl_ctx_t *psl2;
int type = 0; int type = 0;
char buf[256], *linep, *p; char buf[256], *linep, *p;
@ -142,6 +142,11 @@ static void test_psl(void)
psl2 = psl_builtin(); psl2 = psl_builtin();
printf("builtin PSL has %d suffixes and %d exceptions\n", psl_suffix_count(psl2), psl_suffix_exception_count(psl2)); printf("builtin PSL has %d suffixes and %d exceptions\n", psl_suffix_count(psl2), psl_suffix_exception_count(psl2));
if (!(psl3 = psl_load_file("psl.dafsa"))) {
fprintf(stderr, "Failed to load 'psl.dafsa'\n");
failed++;
}
if ((fp = fopen(PSL_FILE, "r"))) { if ((fp = fopen(PSL_FILE, "r"))) {
#ifdef HAVE_CLOCK_GETTIME #ifdef HAVE_CLOCK_GETTIME
clock_gettime(CLOCK_REALTIME, &ts1); clock_gettime(CLOCK_REALTIME, &ts1);
@ -174,6 +179,9 @@ static void test_psl(void)
if (psl2) if (psl2)
test_psl_entry(psl2, p, type); test_psl_entry(psl2, p, type);
if (psl3)
test_psl_entry(psl3, p, type);
} }
#ifdef HAVE_CLOCK_GETTIME #ifdef HAVE_CLOCK_GETTIME
@ -187,6 +195,7 @@ static void test_psl(void)
psl_free(psl); psl_free(psl);
psl_free((psl_ctx_t *)psl2); psl_free((psl_ctx_t *)psl2);
psl_free(psl3);
} }
int main(int argc, const char * const *argv) int main(int argc, const char * const *argv)

View File

@ -84,6 +84,7 @@ static void test_psl(void)
{ "adfhoweirh", 1 }, /* unknown TLD */ { "adfhoweirh", 1 }, /* unknown TLD */
}; };
unsigned it; unsigned it;
int result, ver;
psl_ctx_t *psl; psl_ctx_t *psl;
psl = psl_load_file(PSL_FILE); psl = psl_load_file(PSL_FILE);
@ -92,7 +93,7 @@ static void test_psl(void)
for (it = 0; it < countof(test_data); it++) { for (it = 0; it < countof(test_data); it++) {
const struct test_data *t = &test_data[it]; const struct test_data *t = &test_data[it];
int result = psl_is_public_suffix(psl, t->domain); result = psl_is_public_suffix(psl, t->domain);
if (result == t->result) { if (result == t->result) {
ok++; ok++;
@ -102,6 +103,68 @@ static void test_psl(void)
} }
} }
/* do some checks to cover more code paths in libpsl */
psl_is_public_suffix(NULL, "xxx");
if ((ver = psl_check_version_number(0)) == 0) {
printf("psl_check_version_number(0) is 0\n");
failed++;
} else {
if (((result = psl_check_version_number(ver)) != ver)) {
printf("psl_check_version_number(%06X) is %06X\n", ver, result);
failed++;
}
if (((result = psl_check_version_number(ver - 1)) != 0)) {
printf("psl_check_version_number(%06X) is %06X\n", ver - 1, result);
failed++;
}
if (((result = psl_check_version_number(ver + 1)) != ver)) {
printf("psl_check_version_number(%06X) is %06X\n", ver, result);
failed++;
}
}
psl_str_to_utf8lower("www.example.com", "utf-8", "en", NULL);
psl_str_to_utf8lower(NULL, "utf-8", "en", NULL);
{
char *lower = NULL;
psl_str_to_utf8lower("www.example.com", NULL, "de", &lower);
free(lower); lower = NULL;
psl_str_to_utf8lower("\374bel.de", NULL, "de", &lower);
free(lower); lower = NULL;
psl_str_to_utf8lower("\374bel.de", "iso-8859-1", NULL, &lower);
free(lower); lower = NULL;
psl_str_to_utf8lower(NULL, "utf-8", "en", &lower);
free(lower); lower = NULL;
}
psl_get_version();
psl_builtin_filename();
psl_builtin_outdated();
psl_builtin_file_time();
psl_builtin_sha1sum();
psl_suffix_wildcard_count(NULL);
psl_suffix_wildcard_count(psl);
psl_suffix_wildcard_count(psl_builtin());
psl_suffix_count(NULL);
psl_suffix_exception_count(NULL);
psl_load_file(NULL);
psl_load_fp(NULL);
psl_registrable_domain(NULL, "");
psl_registrable_domain(psl, NULL);
psl_registrable_domain(psl, "www.example.com");
psl_unregistrable_domain(NULL, "");
psl_unregistrable_domain(psl, NULL);
psl_is_public_suffix2(NULL, "", PSL_TYPE_ANY);
psl_is_public_suffix2(psl, NULL, PSL_TYPE_ANY);
psl_free(psl); psl_free(psl);
} }