Debian Codegraph @

tests/test-is-public.c

raw
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 case for psl_load_file(), psl_is_public_suffix(), psl_free()
25 *
26 * Changelog
27 * 19.03.2014 Tim Ruehsen created from libmget/cookie.c
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 #ifdef HAVE_ALLOCA_H
39 # include <alloca.h>
40 #elif defined _WIN32
41 # include <malloc.h>
42 #endif
43
44 #include <libpsl.h>
45
46 #define countof(a) (sizeof(a)/sizeof(*(a)))
47
48 static int
49 ok,
50 failed;
51
52 static void test_psl(void)
53 {
54 /* punycode generation: idn ?? */
55 /* octal code generation: echo -n "??" | od -b */
56 static const struct test_data {
57 const char
58 *domain;
59 int
60 result;
61 int
62 no_star_result;
63 } test_data[] = {
64 { "www.example.com", 0, 0 },
65 { "com.ar", 1 , 1},
66 { "www.com.ar", 0, 0 },
67 { "cc.ar.us", 1, 1 },
68 { ".cc.ar.us", 1, 1 },
69 { "www.cc.ar.us", 0, 0 },
70 { "www.ck", 0, 0 }, /* exception from *.ck */
71 { "abc.www.ck", 0, 0 },
72 { "xxx.ck", 1, 1 },
73 { "www.xxx.ck", 0, 0 },
74 { "\345\225\206\346\240\207", 1, 1 }, /* xn--czr694b or ?? */
75 { "www.\345\225\206\346\240\207", 0, 0 },
76 /* some special test follow ('name' and 'forgot.his.name' are public, but e.g. his.name is not) */
77 { "name", 1, 1 },
78 { ".name", 1, 1 },
79 { "his.name", 0, 0 },
80 { ".his.name", 0, 0 },
81 { "forgot.his.name", 1, 1 },
82 { ".forgot.his.name", 1, 1 },
83 { "whoever.his.name", 0, 0 },
84 { "whoever.forgot.his.name", 0, 0},
85 { ".", 1, 0 }, /* special case */
86 { "", 1, 0 }, /* special case */
87 { NULL, 1, 1 }, /* special case */
88 { "adfhoweirh", 1, 0 }, /* unknown TLD */
89 { "compute.amazonaws.com", 1, 1 }, /* special rule *.compute.amazonaws.com */
90 { "y.compute.amazonaws.com", 1, 1 },
91 { "x.y.compute.amazonaws.com", 0, 0 },
92 };
93 unsigned it;
94 int result, ver;
95 psl_ctx_t *psl;
96
97 psl = psl_load_file(PSL_FILE);
98
99 printf("loaded %d suffixes and %d exceptions\n", psl_suffix_count(psl), psl_suffix_exception_count(psl));
100
101 for (it = 0; it < countof(test_data); it++) {
102 const struct test_data *t = &test_data[it];
103 result = psl_is_public_suffix(psl, t->domain);
104
105 if (result == t->result) {
106 ok++;
107 } else {
108 failed++;
109 printf("psl_is_public_suffix(%s)=%d (expected %d)\n", t->domain, result, t->result);
110 }
111 }
112
113 for (it = 0; it < countof(test_data); it++) {
114 const struct test_data *t = &test_data[it];
115 result = psl_is_public_suffix2(psl, t->domain, PSL_TYPE_ANY|PSL_TYPE_NO_STAR_RULE);
116
117 if (result == t->no_star_result) {
118 ok++;
119 } else {
120 failed++;
121 printf("psl_is_public_suffix2(%s, NO_STAR_RULE)=%d (expected %d)\n", t->domain, result, t->no_star_result);
122 }
123 }
124
125 /* do some checks to cover more code paths in libpsl */
126 psl_is_public_suffix(NULL, "xxx");
127
128 if ((ver = psl_check_version_number(0)) == 0) {
129 printf("psl_check_version_number(0) is 0\n");
130 failed++;
131 } else {
132 if (((result = psl_check_version_number(ver)) != ver)) {
133 printf("psl_check_version_number(%06X) is %06X\n", ver, result);
134 failed++;
135 }
136
137 if (((result = psl_check_version_number(ver - 1)) != 0)) {
138 printf("psl_check_version_number(%06X) is %06X\n", ver - 1, result);
139 failed++;
140 }
141
142 if (((result = psl_check_version_number(ver + 1)) != ver)) {
143 printf("psl_check_version_number(%06X) is %06X\n", ver, result);
144 failed++;
145 }
146 }
147
148 psl_str_to_utf8lower("www.example.com", "utf-8", "en", NULL);
149 psl_str_to_utf8lower(NULL, "utf-8", "en", NULL);
150
151 {
152 char *lower = NULL;
153
154 psl_str_to_utf8lower("www.example.com", NULL, "de", &lower);
155 psl_free_string(lower); lower = NULL;
156
157 psl_str_to_utf8lower("\374bel.de", NULL, "de", &lower);
158 psl_free_string(lower); lower = NULL;
159
160 psl_str_to_utf8lower("\374bel.de", "iso-8859-1", NULL, &lower);
161 psl_free_string(lower); lower = NULL;
162
163 psl_str_to_utf8lower(NULL, "utf-8", "en", &lower);
164 psl_free_string(lower); lower = NULL;
165 }
166
167 psl_get_version();
168 psl_dist_filename();
169 psl_builtin_filename();
170 psl_builtin_outdated();
171 psl_builtin_file_time();
172 psl_builtin_sha1sum();
173 psl_suffix_wildcard_count(NULL);
174 psl_suffix_wildcard_count(psl);
175 psl_suffix_wildcard_count(psl_builtin());
176 psl_suffix_count(NULL);
177 psl_suffix_exception_count(NULL);
178 psl_load_file(NULL);
179 psl_load_fp(NULL);
180 psl_registrable_domain(NULL, "");
181 psl_registrable_domain(psl, NULL);
182 psl_registrable_domain(psl, "www.example.com");
183 psl_unregistrable_domain(NULL, "");
184 psl_unregistrable_domain(psl, NULL);
185 psl_is_public_suffix2(NULL, "", PSL_TYPE_ANY);
186 psl_is_public_suffix2(psl, NULL, PSL_TYPE_ANY);
187
188 psl_free(psl);
189 }
190
191 int main(int argc, const char * const *argv)
192 {
193 /* if VALGRIND testing is enabled, we have to call ourselves with valgrind checking */
194 if (argc == 1) {
195 const char *valgrind = getenv("TESTS_VALGRIND");
196
197 if (valgrind && *valgrind) {
198 size_t cmdsize = strlen(valgrind) + strlen(argv[0]) + 32;
199 char *cmd = alloca(cmdsize);
200
201 snprintf(cmd, cmdsize, "TESTS_VALGRIND="" %s %s", valgrind, argv[0]);
202 return system(cmd) != 0;
203 }
204 }
205
206 test_psl();
207
208 if (failed) {
209 printf("Summary: %d out of %d tests failed\n", failed, ok + failed);
210 return 1;
211 }
212
213 printf("Summary: All %d tests passed\n", ok + failed);
214 return 0;
215 }