| 1 |
/* |
| 2 |
* Copyright(c) 2014-2024 Tim Ruehsen |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in |
| 12 |
* all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 |
* DEALINGS IN THE SOFTWARE. |
| 21 |
* |
| 22 |
* This file is part of the test suite of libpsl. |
| 23 |
* |
| 24 |
* Test psl_is_public_suffix() for all entries in public_suffix_list.dat |
| 25 |
* |
| 26 |
* Changelog |
| 27 |
* 19.03.2014 Tim Ruehsen created |
| 28 |
* |
| 29 |
*/ |
| 30 |
|
| 31 |
#if HAVE_CONFIG_H |
| 32 |
# include <config.h> |
| 33 |
#endif |
| 34 |
|
| 35 |
#include <stdio.h> |
| 36 |
#include <stdlib.h> |
| 37 |
#include <string.h> |
| 38 |
#include <ctype.h> |
| 39 |
#ifdef HAVE_ALLOCA_H |
| 40 |
# include <alloca.h> |
| 41 |
#elif defined _WIN32 |
| 42 |
# include <malloc.h> |
| 43 |
#endif |
| 44 |
|
| 45 |
#include <libpsl.h> |
| 46 |
|
| 47 |
static int |
| 48 |
ok, |
| 49 |
failed; |
| 50 |
#ifdef HAVE_CLOCK_GETTIME |
| 51 |
static struct timespec ts1, ts2; |
| 52 |
#endif |
| 53 |
|
| 54 |
static int _isspace_ascii(const char c) |
| 55 |
{ |
| 56 |
return c == ' ' || c == '\t' || c == '\r' || c == '\n'; |
| 57 |
} |
| 58 |
|
| 59 |
static const char *_type_string(int type) |
| 60 |
{ |
| 61 |
switch (type) { |
| 62 |
case PSL_TYPE_ANY: return "PSL_TYPE_ANY"; |
| 63 |
case PSL_TYPE_PRIVATE: return "PSL_TYPE_PRIVATE"; |
| 64 |
case PSL_TYPE_ICANN: return "PSL_TYPE_ICANN"; |
| 65 |
case PSL_TYPE_ANY|PSL_TYPE_NO_STAR_RULE: return "PSL_TYPE_ANY|PSL_TYPE_NO_STAR_RULE"; |
| 66 |
case PSL_TYPE_PRIVATE|PSL_TYPE_NO_STAR_RULE: return "PSL_TYPE_PRIVATE|PSL_TYPE_NO_STAR_RULE"; |
| 67 |
case PSL_TYPE_ICANN|PSL_TYPE_NO_STAR_RULE: return "PSL_TYPE_ICANN|PSL_TYPE_NO_STAR_RULE"; |
| 68 |
default: return "Unsupported type"; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
static void test_ps(const psl_ctx_t *psl, const char *domain, int type, int expected) |
| 73 |
{ |
| 74 |
int result; |
| 75 |
|
| 76 |
if ((result = psl_is_public_suffix2(psl, domain, type)) != expected) { |
| 77 |
failed++; |
| 78 |
printf("psl_is_public_suffix2(%s, %s)=%d (expected %d)\n", domain, _type_string(type), result, expected); |
| 79 |
} else ok++; |
| 80 |
} |
| 81 |
|
| 82 |
/* section: either PSL_TYPE_PRIVATE or PSL_TYPE_ICANN */ |
| 83 |
static void test_type_any(const psl_ctx_t *psl, const char *domain, int type, int expected) |
| 84 |
{ |
| 85 |
int wildcard = (*domain == '.'); |
| 86 |
int tld = !(strchr(domain + wildcard, '.')); |
| 87 |
|
| 88 |
test_ps(psl, domain, type, expected); |
| 89 |
test_ps(psl, domain, type|PSL_TYPE_NO_STAR_RULE, expected); |
| 90 |
test_ps(psl, domain, PSL_TYPE_ANY, expected); |
| 91 |
test_ps(psl, domain, PSL_TYPE_ANY|PSL_TYPE_NO_STAR_RULE, expected); |
| 92 |
|
| 93 |
if (type == PSL_TYPE_PRIVATE) { |
| 94 |
if (tld) { |
| 95 |
test_ps(psl, domain, PSL_TYPE_ICANN, 1); |
| 96 |
test_ps(psl, domain, PSL_TYPE_ICANN|PSL_TYPE_NO_STAR_RULE, 0); |
| 97 |
} else { |
| 98 |
test_ps(psl, domain, PSL_TYPE_ICANN, 0); |
| 99 |
test_ps(psl, domain, PSL_TYPE_ICANN|PSL_TYPE_NO_STAR_RULE, 0); |
| 100 |
} |
| 101 |
} else if (type == PSL_TYPE_ICANN) { |
| 102 |
if (tld) { |
| 103 |
test_ps(psl, domain, PSL_TYPE_PRIVATE, 1); |
| 104 |
test_ps(psl, domain, PSL_TYPE_PRIVATE|PSL_TYPE_NO_STAR_RULE, 0); |
| 105 |
} else { |
| 106 |
test_ps(psl, domain, PSL_TYPE_PRIVATE, 0); |
| 107 |
test_ps(psl, domain, PSL_TYPE_PRIVATE|PSL_TYPE_NO_STAR_RULE, 0); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
static void test_psl_entry(const psl_ctx_t *psl, const char *domain, int type) |
| 113 |
{ |
| 114 |
if (*domain == '!') { /* an exception to a wildcard, e.g. !www.ck (wildcard is *.ck) */ |
| 115 |
test_type_any(psl, domain + 1, type, 0); /* the exception itself is not a PS */ |
| 116 |
|
| 117 |
if ((domain = strchr(domain, '.'))) |
| 118 |
test_type_any(psl, domain, type, 1); /* the related wildcard domain is a PS */ |
| 119 |
|
| 120 |
} else if (*domain == '*') { /* a wildcard, e.g. *.ck or *.platform.sh */ |
| 121 |
/* '*.platform.sh' -> 'y.x.platform.sh' */ |
| 122 |
size_t len = strlen(domain); |
| 123 |
char *xdomain = alloca(len + 3); |
| 124 |
|
| 125 |
memcpy(xdomain, "y.x", 3); |
| 126 |
memcpy(xdomain + 3, domain + 1, len); |
| 127 |
|
| 128 |
test_type_any(psl, domain + 1, type, 1); /* the domain without wildcard is a PS */ |
| 129 |
test_type_any(psl, xdomain + 2, type, 1); /* random wildcard-matching domain is a PS... */ |
| 130 |
test_type_any(psl, xdomain, type, 0); /* ... but sub domain is not */ |
| 131 |
|
| 132 |
} else { |
| 133 |
test_type_any(psl, domain, type, 1); /* Any normal PSL entry */ |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
static void test_psl(void) |
| 138 |
{ |
| 139 |
FILE *fp; |
| 140 |
psl_ctx_t *psl, *psl3, *psl4, *psl5; |
| 141 |
const psl_ctx_t *psl2; |
| 142 |
int type = 0; |
| 143 |
char buf[256], *linep, *p; |
| 144 |
|
| 145 |
psl = psl_load_file(PSL_FILE); /* PSL_FILE can be set by ./configure --with-psl-file=[PATH] */ |
| 146 |
printf("loaded %d suffixes and %d exceptions\n", psl_suffix_count(psl), psl_suffix_exception_count(psl)); |
| 147 |
|
| 148 |
psl2 = psl_builtin(); |
| 149 |
printf("builtin PSL has %d suffixes and %d exceptions\n", psl_suffix_count(psl2), psl_suffix_exception_count(psl2)); |
| 150 |
|
| 151 |
if (!(psl3 = psl_load_file(PSL_DAFSA))) { |
| 152 |
fprintf(stderr, "Failed to load 'psl.dafsa'\n"); |
| 153 |
failed++; |
| 154 |
} |
| 155 |
|
| 156 |
if (!(psl4 = psl_load_file(PSL_ASCII_DAFSA))) { |
| 157 |
fprintf(stderr, "Failed to load 'psl_ascii.dafsa'\n"); |
| 158 |
failed++; |
| 159 |
} |
| 160 |
|
| 161 |
psl5 = psl_latest("psl.dafsa"); |
| 162 |
|
| 163 |
if ((fp = fopen(PSL_FILE, "r"))) { |
| 164 |
#ifdef HAVE_CLOCK_GETTIME |
| 165 |
clock_gettime(CLOCK_REALTIME, &ts1); |
| 166 |
#endif |
| 167 |
|
| 168 |
while ((linep = fgets(buf, sizeof(buf), fp))) { |
| 169 |
while (_isspace_ascii(*linep)) linep++; /* ignore leading whitespace */ |
| 170 |
if (!*linep) continue; /* skip empty lines */ |
| 171 |
|
| 172 |
if (*linep == '/' && linep[1] == '/') { |
| 173 |
if (!type) { |
| 174 |
if (strstr(linep + 2, "===BEGIN ICANN DOMAINS===")) |
| 175 |
type = PSL_TYPE_ICANN; |
| 176 |
else if (!type && strstr(linep + 2, "===BEGIN PRIVATE DOMAINS===")) |
| 177 |
type = PSL_TYPE_PRIVATE; |
| 178 |
} |
| 179 |
else if (type == PSL_TYPE_ICANN && strstr(linep + 2, "===END ICANN DOMAINS===")) |
| 180 |
type = 0; |
| 181 |
else if (type == PSL_TYPE_PRIVATE && strstr(linep + 2, "===END PRIVATE DOMAINS===")) |
| 182 |
type = 0; |
| 183 |
|
| 184 |
continue; /* skip comments */ |
| 185 |
} |
| 186 |
|
| 187 |
/* parse suffix rule */ |
| 188 |
for (p = linep; *linep && !_isspace_ascii(*linep);) linep++; |
| 189 |
*linep = 0; |
| 190 |
|
| 191 |
test_psl_entry(psl, p, type); |
| 192 |
|
| 193 |
if (psl2) |
| 194 |
test_psl_entry(psl2, p, type); |
| 195 |
|
| 196 |
if (psl3) |
| 197 |
test_psl_entry(psl3, p, type); |
| 198 |
|
| 199 |
if (psl4) |
| 200 |
test_psl_entry(psl4, p, type); |
| 201 |
|
| 202 |
if (psl5) |
| 203 |
test_psl_entry(psl5, p, type); |
| 204 |
} |
| 205 |
|
| 206 |
#ifdef HAVE_CLOCK_GETTIME |
| 207 |
clock_gettime(CLOCK_REALTIME, &ts2); |
| 208 |
#endif |
| 209 |
fclose(fp); |
| 210 |
} else { |
| 211 |
printf("Failed to open %s\n", PSL_FILE); |
| 212 |
failed++; |
| 213 |
} |
| 214 |
|
| 215 |
psl_free(psl5); |
| 216 |
psl_free(psl4); |
| 217 |
psl_free(psl3); |
| 218 |
psl_free((psl_ctx_t *)psl2); |
| 219 |
psl_free(psl); |
| 220 |
} |
| 221 |
|
| 222 |
int main(int argc, const char * const *argv) |
| 223 |
{ |
| 224 |
#ifdef HAVE_CLOCK_GETTIME |
| 225 |
long ns; |
| 226 |
#endif |
| 227 |
|
| 228 |
/* if VALGRIND testing is enabled, we have to call ourselves with valgrind checking */ |
| 229 |
if (argc == 1) { |
| 230 |
const char *valgrind = getenv("TESTS_VALGRIND"); |
| 231 |
|
| 232 |
if (valgrind && *valgrind) { |
| 233 |
size_t cmdsize = strlen(valgrind) + strlen(argv[0]) + 32; |
| 234 |
char *cmd = alloca(cmdsize); |
| 235 |
|
| 236 |
snprintf(cmd, cmdsize, "TESTS_VALGRIND="" %s %s", valgrind, argv[0]); |
| 237 |
return system(cmd) != 0; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
test_psl(); |
| 242 |
|
| 243 |
if (failed) { |
| 244 |
printf("Summary: %d out of %d tests failed\n", failed, ok + failed); |
| 245 |
return 1; |
| 246 |
} |
| 247 |
|
| 248 |
#ifdef HAVE_CLOCK_GETTIME |
| 249 |
if (ts1.tv_sec == ts2.tv_sec) |
| 250 |
ns = ts2.tv_nsec - ts1.tv_nsec; |
| 251 |
else if (ts1.tv_sec == ts2.tv_sec - 1) |
| 252 |
ns = 1000000000L - (ts2.tv_nsec - ts1.tv_nsec); |
| 253 |
else |
| 254 |
ns = 0; /* let's assume something is wrong and skip outputting measured time */ |
| 255 |
|
| 256 |
if (ns) |
| 257 |
printf("Summary: All %d tests passed in %ld.%06ld ms\n", ok, ns / 1000000, ns % 1000000000); |
| 258 |
else |
| 259 |
printf("Summary: All %d tests passed\n", ok); |
| 260 |
#else |
| 261 |
printf("Summary: All %d tests passed\n", ok); |
| 262 |
#endif |
| 263 |
|
| 264 |
return 0; |
| 265 |
} |