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