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