• 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 built-in functions
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 		{ "xn--czr694b", 1, 1 },
75 		{ "www.xn--czr694b", 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 		{ "whatever.platformsh.site", 1, 1 },
86 		{ ".platformsh.site", 1, 1 },
87 		{ "whatever.yokohama.jp", 1, 1 },
88 		{ ".yokohama.jp", 1, 1 },
89 		{ ".", 1, 0 }, /* special case */
90 		{ "", 1, 0 },  /* special case */
91 		{ NULL, 1, 1 },  /* special case */
92 		{ "adfhoweirh", 1, 0 }, /* unknown TLD */
93 		{ "compute.amazonaws.com", 1, 1 }, /* special rule *.compute.amazonaws.com */
94 		{ "y.compute.amazonaws.com", 1, 1 },
95 		{ "x.y.compute.amazonaws.com", 0, 0 },
96 	};
97 	unsigned it;
98 	const psl_ctx_t *psl;
99 
100 	psl = psl_builtin();
101 
102 	printf("have %d suffixes and %d exceptions\n", psl_suffix_count(psl), psl_suffix_exception_count(psl));
103 
104 	for (it = 0; it < countof(test_data); it++) {
105 		const struct test_data *t = &test_data[it];
106 		int result = psl_is_public_suffix(psl, t->domain);
107 
108 		if (result == t->result) {
109 			ok++;
110 		} else {
111 			failed++;
112 			printf("psl_is_public_suffix(%s)=%d (expected %d)\n", t->domain, result, t->result);
113 		}
114 	}
115 
116 	for (it = 0; it < countof(test_data); it++) {
117 		const struct test_data *t = &test_data[it];
118 		int result = psl_is_public_suffix2(psl, t->domain, PSL_TYPE_ANY|PSL_TYPE_NO_STAR_RULE);
119 
120 		if (result == t->no_star_result) {
121 			ok++;
122 		} else {
123 			failed++;
124 			printf("psl_is_public_suffix2(%s, NO_STAR_RULE)=%d (expected %d)\n", t->domain, result, t->no_star_result);
125 		}
126 	}
127 
128 	printf("psl_builtin_file_time()=%ld\n", (long) psl_builtin_file_time());
129 	psl_builtin_file_time() == 0 ? failed++ : ok++;
130 
131 	printf("psl_builtin_sha1sum()=%s\n", psl_builtin_sha1sum());
132 	*psl_builtin_sha1sum() == 0 ? failed++ : ok++;
133 }
134 
main(int argc,const char * const * argv)135 int main(int argc, const char * const *argv)
136 {
137 	/* if VALGRIND testing is enabled, we have to call ourselves with valgrind checking */
138 	if (argc == 1) {
139 		const char *valgrind = getenv("TESTS_VALGRIND");
140 
141 		if (valgrind && *valgrind) {
142 			size_t cmdsize = strlen(valgrind) + strlen(argv[0]) + 32;
143 			char *cmd = alloca(cmdsize);
144 
145 			snprintf(cmd, cmdsize, "TESTS_VALGRIND="" %s %s", valgrind, argv[0]);
146 			return system(cmd) != 0;
147 		}
148 	}
149 
150 	test_psl();
151 
152 	if (failed) {
153 		printf("Summary: %d out of %d tests failed\n", failed, ok + failed);
154 		return 1;
155 	}
156 
157 	printf("Summary: All %d tests passed\n", ok + failed);
158 	return 0;
159 }
160