Add tests against the built-in PSL to test-is-public-all.c

This commit is contained in:
Tim Rühsen 2016-01-01 23:26:44 +01:00
parent 748e3ae9cc
commit 9f420e2d91
1 changed files with 83 additions and 65 deletions

View File

@ -54,17 +54,94 @@ static inline int _isspace_ascii(const char c)
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
}
static void test_psl_entry(const psl_ctx_t *psl, const char *domain, int type)
{
int result;
if (*domain == '!') { /* an exception to a wildcard, e.g. !www.ck (wildcard is *.ck) */
if ((result = psl_is_public_suffix(psl, domain + 1))) {
failed++;
printf("psl_is_public_suffix(%s)=%d (expected 0)\n", domain, result);
} else ok++;
if ((domain = strchr(domain, '.'))) {
if (!(result = psl_is_public_suffix(psl, domain + 1))) {
failed++;
printf("psl_is_public_suffix(%s)=%d (expected 1)\n", domain + 1, result);
} else ok++;
}
} else if (*domain == '*') { /* a wildcard, e.g. *.ck or *.platform.sh */
char *xdomain;
size_t len;
if (!(result = psl_is_public_suffix(psl, domain + 1))) {
failed++;
printf("psl_is_public_suffix(%s)=%d (expected 1)\n", domain + 1, result);
} else ok++;
len = strlen(domain);
xdomain = alloca(len + 1);
memcpy(xdomain, domain, len + 1);
*xdomain = 'x';
if (!(result = psl_is_public_suffix(psl, domain))) {
failed++;
printf("psl_is_public_suffix(%s)=%d (expected 1)\n", domain, result);
} else ok++;
} else {
if (!(result = psl_is_public_suffix(psl, domain))) {
failed++;
printf("psl_is_public_suffix(%s)=%d (expected 1)\n", domain, result);
} else ok++;
if (!(strchr(domain, '.'))) {
/* TLDs are always expected to be Publix Suffixes */
if (!(result = psl_is_public_suffix2(psl, domain, PSL_TYPE_PRIVATE))) {
failed++;
printf("psl_is_public_suffix2(%s, PSL_TYPE_PRIVATE)=%d (expected 1)\n", domain, result);
} else ok++;
if (!(result = psl_is_public_suffix2(psl, domain, PSL_TYPE_ICANN))) {
failed++;
printf("psl_is_public_suffix2(%s, PSL_TYPE_ICANN)=%d (expected 0)\n", domain, result);
} else ok++;
} else if (type == PSL_TYPE_PRIVATE) {
if (!(result = psl_is_public_suffix2(psl, domain, PSL_TYPE_PRIVATE))) {
failed++;
printf("psl_is_public_suffix2(%s, PSL_TYPE_PRIVATE)=%d (expected 1)\n", domain, result);
} else ok++;
if ((result = psl_is_public_suffix2(psl, domain, PSL_TYPE_ICANN))) {
failed++;
printf("psl_is_public_suffix2(%s, PSL_TYPE_ICANN)=%d (expected 0)\n", domain, result);
} else ok++;
} else if (type == PSL_TYPE_ICANN) {
if (!(result = psl_is_public_suffix2(psl, domain, PSL_TYPE_ICANN))) {
failed++;
printf("psl_is_public_suffix2(%s, PSL_TYPE_ICANN)=%d (expected 1)\n", domain, result);
} else ok++;
if ((result = psl_is_public_suffix2(psl, domain, PSL_TYPE_PRIVATE))) {
failed++;
printf("psl_is_public_suffix2(%s, PSL_TYPE_PRIVATE)=%d (expected 0)\n", domain, result);
} else ok++;
}
}
}
static void test_psl(void)
{
FILE *fp;
psl_ctx_t *psl;
int result, type = 0;
const psl_ctx_t *psl2;
int type = 0;
char buf[256], *linep, *p;
psl = psl_load_file(PSL_FILE); /* PSL_FILE can be set by ./configure --with-psl-file=[PATH] */
printf("loaded %d suffixes and %d exceptions\n", psl_suffix_count(psl), psl_suffix_exception_count(psl));
psl2 = psl_builtin();
printf("builtin PSL has %d suffixes and %d exceptions\n", psl_suffix_count(psl2), psl_suffix_exception_count(psl2));
if ((fp = fopen(PSL_FILE, "r"))) {
#ifdef HAVE_CLOCK_GETTIME
clock_gettime(CLOCK_REALTIME, &ts1);
@ -93,70 +170,10 @@ static void test_psl(void)
for (p = linep; *linep && !_isspace_ascii(*linep);) linep++;
*linep = 0;
if (*p == '!') { /* an exception to a wildcard, e.g. !www.ck (wildcard is *.ck) */
if ((result = psl_is_public_suffix(psl, p + 1))) {
failed++;
printf("psl_is_public_suffix(%s)=%d (expected 0)\n", p, result);
} else ok++;
test_psl_entry(psl, p, type);
if ((p = strchr(p, '.'))) {
if (!(result = psl_is_public_suffix(psl, p + 1))) {
failed++;
printf("psl_is_public_suffix(%s)=%d (expected 1)\n", p + 1, result);
} else ok++;
}
}
else if (*p == '*') { /* a wildcard, e.g. *.ck */
if (!(result = psl_is_public_suffix(psl, p + 1))) {
failed++;
printf("psl_is_public_suffix(%s)=%d (expected 1)\n", p + 1, result);
} else ok++;
*p = 'x';
if (!(result = psl_is_public_suffix(psl, p))) {
failed++;
printf("psl_is_public_suffix(%s)=%d (expected 1)\n", p, result);
} else ok++;
}
else {
if (!(result = psl_is_public_suffix(psl, p))) {
failed++;
printf("psl_is_public_suffix(%s)=%d (expected 1)\n", p, result);
} else ok++;
if (!(strchr(p, '.'))) {
/* TLDs are always expected to be Publix Suffixes */
if (!(result = psl_is_public_suffix2(psl, p, PSL_TYPE_PRIVATE))) {
failed++;
printf("psl_is_public_suffix2(%s, PSL_TYPE_PRIVATE)=%d (expected 1)\n", p, result);
} else ok++;
if (!(result = psl_is_public_suffix2(psl, p, PSL_TYPE_ICANN))) {
failed++;
printf("psl_is_public_suffix2(%s, PSL_TYPE_ICANN)=%d (expected 0)\n", p, result);
} else ok++;
} else if (type == PSL_TYPE_PRIVATE) {
if (!(result = psl_is_public_suffix2(psl, p, PSL_TYPE_PRIVATE))) {
failed++;
printf("psl_is_public_suffix2(%s, PSL_TYPE_PRIVATE)=%d (expected 1)\n", p, result);
} else ok++;
if ((result = psl_is_public_suffix2(psl, p, PSL_TYPE_ICANN))) {
failed++;
printf("psl_is_public_suffix2(%s, PSL_TYPE_ICANN)=%d (expected 0)\n", p, result);
} else ok++;
} else if (type == PSL_TYPE_ICANN) {
if (!(result = psl_is_public_suffix2(psl, p, PSL_TYPE_ICANN))) {
failed++;
printf("psl_is_public_suffix2(%s, PSL_TYPE_ICANN)=%d (expected 1)\n", p, result);
} else ok++;
if ((result = psl_is_public_suffix2(psl, p, PSL_TYPE_PRIVATE))) {
failed++;
printf("psl_is_public_suffix2(%s, PSL_TYPE_PRIVATE)=%d (expected 0)\n", p, result);
} else ok++;
}
}
if (psl2)
test_psl_entry(psl2, p, type);
}
#ifdef HAVE_CLOCK_GETTIME
@ -169,6 +186,7 @@ static void test_psl(void)
}
psl_free(psl);
psl_free((psl_ctx_t *)psl2);
}
int main(int argc, const char * const *argv)