Debian Codegraph @

tests/test-registrable-domain.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 psl_registered_domain() for all entries in test_psl.dat
25 *
26 * Changelog
27 * 26.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 #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 static int
47 ok,
48 failed;
49
50 static void testx(const psl_ctx_t *psl, const char *domain, const char *encoding, const char *lang, const char *expected_result)
51 {
52 const char *result;
53 char *lower = NULL;
54 int rc;
55
56 /* just to cover special code paths for valgrind checking */
57 psl_str_to_utf8lower(domain, encoding, lang, NULL);
58
59 if ((rc = psl_str_to_utf8lower(domain, encoding, lang, &lower)) == PSL_SUCCESS)
60 domain = lower;
61 /* non-ASCII domains fail here if no runtime IDN library is configured, so skip it */
62 #if defined(WITH_LIBIDN) || defined(WITH_LIBIDN2) || defined(WITH_LIBICU)
63 else if (domain) {
64 /* if we do not runtime support, test failure have to be skipped */
65 failed++;
66 printf("psl_str_to_utf8lower(%s)=%d\n", domain ? domain : "NULL", rc);
67 return;
68 }
69 #endif
70
71 result = psl_registrable_domain(psl, domain);
72
73 if ((result && expected_result && !strcmp(result, expected_result)) || (!result && !expected_result)) {
74 ok++;
75 } else {
76 failed++;
77 printf("psl_registrable_domain(%s)=%s (expected %s)\n",
78 domain ? domain : "NULL", result ? result : "NULL", expected_result ? expected_result : "NULL");
79 }
80
81 psl_free_string(lower);
82 }
83
84 static void test(const psl_ctx_t *psl, const char *domain, const char *expected_result)
85 {
86 testx(psl, domain, "utf-8", "en", expected_result);
87 }
88
89 static void test_iso(const psl_ctx_t *psl, const char *domain, const char *expected_result)
90 {
91 testx(psl, domain, "iso-8859-15", "de", expected_result);
92 }
93
94 static void test_psl(void)
95 {
96 FILE *fp;
97 const psl_ctx_t *psl;
98 const char *p;
99 char buf[256], domain[128], expected_regdom[128], semicolon[2];
100 char lbuf[258];
101 int er_is_null, d_is_null;
102 unsigned it;
103
104 psl = psl_builtin();
105
106 printf("have %d suffixes and %d exceptions\n", psl_suffix_count(psl), psl_suffix_exception_count(psl));
107
108 /* special check with NULL values */
109 test(NULL, NULL, NULL);
110
111 /* special check with NULL psl context */
112 test(NULL, "www.example.com", NULL);
113
114 /* special check with NULL psl context and TLD */
115 test(NULL, "com", NULL);
116
117 /* Norwegian with uppercase oe */
118 #ifdef WITH_LIBICU
119 test(psl, "www.\303\230yer.no", "www.\303\270yer.no");
120 #endif
121
122 /* Norwegian with lowercase oe */
123 test(psl, "www.\303\270yer.no", "www.\303\270yer.no");
124
125 /* Norwegian with lowercase oe, encoded as ISO-8859-15 */
126 /* makes only sense with a runtime IDN library configured */
127 #if defined(WITH_LIBIDN) || defined(WITH_LIBIDN2) || defined(WITH_LIBICU)
128 test_iso(psl, "www.\370yer.no", "www.\303\270yer.no");
129 #endif
130
131 /* Testing special code paths of psl_str_to_utf8lower() */
132 for (it = 254; it <= 257; it++) {
133 memset(lbuf, 'a', it);
134 lbuf[it] = 0;
135
136 lbuf[0] = '\370';
137 test_iso(psl, lbuf, NULL);
138
139 lbuf[0] = '\303';
140 lbuf[1] = '\270';
141 test(psl, lbuf, NULL);
142 }
143
144 /* special check with NULL psl context and TLD */
145 test(psl, "whoever.forgot.his.name", "whoever.forgot.his.name");
146
147 /* special check with NULL psl context and TLD */
148 test(psl, "forgot.his.name", NULL);
149
150 /* special check with NULL psl context and TLD */
151 test(psl, "his.name", "his.name");
152
153 if ((fp = fopen(PSL_TESTFILE, "r"))) {
154 while ((fgets(buf, sizeof(buf), fp))) {
155 /* advance over ASCII white space */
156 for (p = buf; *p == ' ' || *p == '\t' || *p == '\r' || *p == '\n'; p++)
157 ;
158
159 if (!*p || (*p == '/' && p[1] == '/'))
160 continue; /* ignore comments and blank lines */
161
162 er_is_null = 0;
163 d_is_null = 0;
164
165 if (sscanf(p, "checkPublicSuffix ( '%127[^']' , '%127[^']' ) %1[;]", domain, expected_regdom, semicolon) != 3) {
166 if (sscanf(p, "checkPublicSuffix ( '%127[^']' , null ) %1[;]", domain, semicolon) == 2) {
167 er_is_null = 1;
168 } else if (sscanf(p, "checkPublicSuffix ( null , '%127[^']' ) %1[;]", expected_regdom, semicolon) == 2) {
169 d_is_null = 1;
170 } else if (sscanf(p, "checkPublicSuffix ( null , null ) %1[;]", semicolon) == 1) {
171 d_is_null = 1;
172 er_is_null = 1;
173 } else if (sscanf(p, "%127s %127s", domain, expected_regdom) == 2) {
174 if (!strcmp(domain, "null"))
175 d_is_null = 1;
176 if (!strcmp(expected_regdom, "null"))
177 er_is_null = 1;
178 } else {
179 failed++;
180 printf("Malformed line from '" PSL_TESTFILE "': %s", buf);
181 continue;
182 }
183 }
184
185 test(psl, d_is_null ? NULL : domain, er_is_null ? NULL : expected_regdom);
186 }
187
188 fclose(fp);
189 } else {
190 printf("Failed to open %s\n", PSL_TESTFILE);
191 failed++;
192 }
193 }
194
195 int main(int argc, const char * const *argv)
196 {
197 /* if VALGRIND testing is enabled, we have to call ourselves with valgrind checking */
198 if (argc == 1) {
199 const char *valgrind = getenv("TESTS_VALGRIND");
200
201 if (valgrind && *valgrind) {
202 size_t cmdsize = strlen(valgrind) + strlen(argv[0]) + 32;
203 char *cmd = alloca(cmdsize);
204
205 snprintf(cmd, cmdsize, "TESTS_VALGRIND="" %s %s", valgrind, argv[0]);
206 return system(cmd) != 0;
207 }
208 }
209
210 test_psl();
211
212 if (failed) {
213 printf("Summary: %d out of %d tests failed\n", failed, ok + failed);
214 return 1;
215 }
216
217 printf("Summary: All %d tests passed\n", ok + failed);
218 return 0;
219 }