|
Packit |
0af36a |
/*
|
|
Packit |
0af36a |
* Copyright(c) 2014-2018 Tim Ruehsen
|
|
Packit |
0af36a |
*
|
|
Packit |
0af36a |
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
Packit |
0af36a |
* copy of this software and associated documentation files (the "Software"),
|
|
Packit |
0af36a |
* to deal in the Software without restriction, including without limitation
|
|
Packit |
0af36a |
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
Packit |
0af36a |
* and/or sell copies of the Software, and to permit persons to whom the
|
|
Packit |
0af36a |
* Software is furnished to do so, subject to the following conditions:
|
|
Packit |
0af36a |
*
|
|
Packit |
0af36a |
* The above copyright notice and this permission notice shall be included in
|
|
Packit |
0af36a |
* all copies or substantial portions of the Software.
|
|
Packit |
0af36a |
*
|
|
Packit |
0af36a |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
Packit |
0af36a |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
Packit |
0af36a |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
Packit |
0af36a |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
Packit |
0af36a |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
Packit |
0af36a |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
Packit |
0af36a |
* DEALINGS IN THE SOFTWARE.
|
|
Packit |
0af36a |
*
|
|
Packit |
0af36a |
* This file is part of the test suite of libpsl.
|
|
Packit |
0af36a |
*
|
|
Packit |
0af36a |
* Test case for psl built-in functions
|
|
Packit |
0af36a |
*
|
|
Packit |
0af36a |
* Changelog
|
|
Packit |
0af36a |
* 19.03.2014 Tim Ruehsen created from libmget/cookie.c
|
|
Packit |
0af36a |
*
|
|
Packit |
0af36a |
*/
|
|
Packit |
0af36a |
|
|
Packit |
0af36a |
#if HAVE_CONFIG_H
|
|
Packit |
0af36a |
# include <config.h>
|
|
Packit |
0af36a |
#endif
|
|
Packit |
0af36a |
|
|
Packit |
0af36a |
#include <stdio.h>
|
|
Packit |
0af36a |
#include <stdlib.h>
|
|
Packit |
0af36a |
#include <string.h>
|
|
Packit |
0af36a |
#ifdef HAVE_ALLOCA_H
|
|
Packit |
0af36a |
# include <alloca.h>
|
|
Packit |
0af36a |
#endif
|
|
Packit |
0af36a |
|
|
Packit |
0af36a |
#include <libpsl.h>
|
|
Packit |
0af36a |
|
|
Packit |
0af36a |
#define countof(a) (sizeof(a)/sizeof(*(a)))
|
|
Packit |
0af36a |
|
|
Packit |
0af36a |
static int
|
|
Packit |
0af36a |
ok,
|
|
Packit |
0af36a |
failed;
|
|
Packit |
0af36a |
|
|
Packit |
0af36a |
static void test_psl(void)
|
|
Packit |
0af36a |
{
|
|
Packit |
0af36a |
/* punycode generation: idn ?? */
|
|
Packit |
0af36a |
/* octal code generation: echo -n "??" | od -b */
|
|
Packit |
0af36a |
static const struct test_data {
|
|
Packit |
0af36a |
const char
|
|
Packit |
0af36a |
*domain;
|
|
Packit |
0af36a |
int
|
|
Packit |
0af36a |
result;
|
|
Packit |
0af36a |
int
|
|
Packit |
0af36a |
no_star_result;
|
|
Packit |
0af36a |
} test_data[] = {
|
|
Packit |
0af36a |
{ "www.example.com", 0, 0 },
|
|
Packit |
0af36a |
{ "com.ar", 1, 1 },
|
|
Packit |
0af36a |
{ "www.com.ar", 0, 0 },
|
|
Packit |
0af36a |
{ "cc.ar.us", 1, 1 },
|
|
Packit |
0af36a |
{ ".cc.ar.us", 1, 1 },
|
|
Packit |
0af36a |
{ "www.cc.ar.us", 0, 0 },
|
|
Packit |
0af36a |
{ "www.ck", 0, 0 }, /* exception from *.ck */
|
|
Packit |
0af36a |
{ "abc.www.ck", 0, 0 },
|
|
Packit |
0af36a |
{ "xxx.ck", 1, 1 },
|
|
Packit |
0af36a |
{ "www.xxx.ck", 0, 0 },
|
|
Packit |
0af36a |
{ "\345\225\206\346\240\207", 1, 1 }, /* xn--czr694b oder ?? */
|
|
Packit |
0af36a |
{ "www.\345\225\206\346\240\207", 0, 0 },
|
|
Packit |
0af36a |
{ "xn--czr694b", 1, 1 },
|
|
Packit |
0af36a |
{ "www.xn--czr694b", 0, 0 },
|
|
Packit |
0af36a |
/* some special test follow ('name' and 'forgot.his.name' are public, but e.g. his.name is not) */
|
|
Packit |
0af36a |
{ "name", 1, 1 },
|
|
Packit |
0af36a |
{ ".name", 1, 1 },
|
|
Packit |
0af36a |
{ "his.name", 0, 0 },
|
|
Packit |
0af36a |
{ ".his.name", 0, 0 },
|
|
Packit |
0af36a |
{ "forgot.his.name", 1, 1 },
|
|
Packit |
0af36a |
{ ".forgot.his.name", 1, 1 },
|
|
Packit |
0af36a |
{ "whoever.his.name", 0, 0 },
|
|
Packit |
0af36a |
{ "whoever.forgot.his.name", 0, 0 },
|
|
Packit |
0af36a |
{ "whatever.platform.sh", 1, 1 },
|
|
Packit |
0af36a |
{ ".platform.sh", 1, 1 },
|
|
Packit |
0af36a |
{ "whatever.yokohama.jp", 1, 1 },
|
|
Packit |
0af36a |
{ ".yokohama.jp", 1, 1 },
|
|
Packit |
0af36a |
{ ".", 1, 0 }, /* special case */
|
|
Packit |
0af36a |
{ "", 1, 0 }, /* special case */
|
|
Packit |
0af36a |
{ NULL, 1, 1 }, /* special case */
|
|
Packit |
0af36a |
{ "adfhoweirh", 1, 0 }, /* unknown TLD */
|
|
Packit |
0af36a |
{ "compute.amazonaws.com", 1, 1 }, /* special rule *.compute.amazonaws.com */
|
|
Packit |
0af36a |
{ "y.compute.amazonaws.com", 1, 1 },
|
|
Packit |
0af36a |
{ "x.y.compute.amazonaws.com", 0, 0 },
|
|
Packit |
0af36a |
};
|
|
Packit |
0af36a |
unsigned it;
|
|
Packit |
0af36a |
const psl_ctx_t *psl;
|
|
Packit |
0af36a |
|
|
Packit |
0af36a |
psl = psl_builtin();
|
|
Packit |
0af36a |
|
|
Packit |
0af36a |
printf("have %d suffixes and %d exceptions\n", psl_suffix_count(psl), psl_suffix_exception_count(psl));
|
|
Packit |
0af36a |
|
|
Packit |
0af36a |
for (it = 0; it < countof(test_data); it++) {
|
|
Packit |
0af36a |
const struct test_data *t = &test_data[it];
|
|
Packit |
0af36a |
int result = psl_is_public_suffix(psl, t->domain);
|
|
Packit |
0af36a |
|
|
Packit |
0af36a |
if (result == t->result) {
|
|
Packit |
0af36a |
ok++;
|
|
Packit |
0af36a |
} else {
|
|
Packit |
0af36a |
failed++;
|
|
Packit |
0af36a |
printf("psl_is_public_suffix(%s)=%d (expected %d)\n", t->domain, result, t->result);
|
|
Packit |
0af36a |
}
|
|
Packit |
0af36a |
}
|
|
Packit |
0af36a |
|
|
Packit |
0af36a |
for (it = 0; it < countof(test_data); it++) {
|
|
Packit |
0af36a |
const struct test_data *t = &test_data[it];
|
|
Packit |
0af36a |
int result = psl_is_public_suffix2(psl, t->domain, PSL_TYPE_ANY|PSL_TYPE_NO_STAR_RULE);
|
|
Packit |
0af36a |
|
|
Packit |
0af36a |
if (result == t->no_star_result) {
|
|
Packit |
0af36a |
ok++;
|
|
Packit |
0af36a |
} else {
|
|
Packit |
0af36a |
failed++;
|
|
Packit |
0af36a |
printf("psl_is_public_suffix2(%s, NO_STAR_RULE)=%d (expected %d)\n", t->domain, result, t->no_star_result);
|
|
Packit |
0af36a |
}
|
|
Packit |
0af36a |
}
|
|
Packit |
0af36a |
|
|
Packit |
0af36a |
printf("psl_builtin_file_time()=%ld\n", psl_builtin_file_time());
|
|
Packit |
0af36a |
psl_builtin_file_time() == 0 ? failed++ : ok++;
|
|
Packit |
0af36a |
|
|
Packit |
0af36a |
printf("psl_builtin_sha1sum()=%s\n", psl_builtin_sha1sum());
|
|
Packit |
0af36a |
*psl_builtin_sha1sum() == 0 ? failed++ : ok++;
|
|
Packit |
0af36a |
}
|
|
Packit |
0af36a |
|
|
Packit |
0af36a |
int main(int argc, const char * const *argv)
|
|
Packit |
0af36a |
{
|
|
Packit |
0af36a |
/* if VALGRIND testing is enabled, we have to call ourselves with valgrind checking */
|
|
Packit |
0af36a |
if (argc == 1) {
|
|
Packit |
0af36a |
const char *valgrind = getenv("TESTS_VALGRIND");
|
|
Packit |
0af36a |
|
|
Packit |
0af36a |
if (valgrind && *valgrind) {
|
|
Packit |
0af36a |
size_t cmdsize = strlen(valgrind) + strlen(argv[0]) + 32;
|
|
Packit |
0af36a |
char *cmd = alloca(cmdsize);
|
|
Packit |
0af36a |
|
|
Packit |
0af36a |
snprintf(cmd, cmdsize, "TESTS_VALGRIND="" %s %s", valgrind, argv[0]);
|
|
Packit |
0af36a |
return system(cmd) != 0;
|
|
Packit |
0af36a |
}
|
|
Packit |
0af36a |
}
|
|
Packit |
0af36a |
|
|
Packit |
0af36a |
test_psl();
|
|
Packit |
0af36a |
|
|
Packit |
0af36a |
if (failed) {
|
|
Packit |
0af36a |
printf("Summary: %d out of %d tests failed\n", failed, ok + failed);
|
|
Packit |
0af36a |
return 1;
|
|
Packit |
0af36a |
}
|
|
Packit |
0af36a |
|
|
Packit |
0af36a |
printf("Summary: All %d tests passed\n", ok + failed);
|
|
Packit |
0af36a |
return 0;
|
|
Packit |
0af36a |
}
|