• 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_is_cookie_doamin_acceptable()
25  *
26  * Changelog
27  * 15.04.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 	static const struct test_data {
53 		const char
54 			*request_domain,
55 			*cookie_domain;
56 		int
57 			result;
58 	} test_data[] = {
59 		{ "www.dkg.forgot.his.name", "www.dkg.forgot.his.name", 1 },
60 		{ "www.dkg.forgot.his.name", "dkg.forgot.his.name", 1 },
61 		{ "www.dkg.forgot.his.name", "forgot.his.name", 0 },
62 		{ "www.dkg.forgot.his.name", "his.name", 0 },
63 		{ "www.dkg.forgot.his.name", "name", 0 },
64 		{ "www.his.name", "www.his.name", 1 },
65 		{ "www.his.name", "his.name", 1 },
66 		{ "www.his.name", "name", 0 },
67 		{ "www.example.com", "www.example.com", 1 },
68 		{ "www.example.com", "wwww.example.com", 0 },
69 		{ "www.example.com", "example.com", 1 },
70 		{ "www.example.com", "com", 0 }, /* not accepted by normalization (PSL rule 'com') */
71 		{ "www.example.com", "example.org", 0 },
72 		{ "www.sa.gov.au", "sa.gov.au", 0 }, /* not accepted by normalization  (PSL rule '*.ar') */
73 		{ "www.educ.ar", "educ.ar", 1 }, /* PSL exception rule '!educ.ar' */
74 		/* RFC6265 5.1.3: Having IP addresses, request and domain IP must be identical */
75 		{ "192.1.123.2", ".1.123.2", 0 }, /* IPv4 address, partial match */
76 		{ "192.1.123.2", "192.1.123.2", 1 }, /* IPv4 address, full match */
77 		{ "::1", "::1", 1 }, /* IPv6 address, full match */
78 		{ "2a00:1450:4013:c01::8b", ":1450:4013:c01::8b", 0 }, /* IPv6 address, partial match */
79 		{ "::ffff:192.1.123.2", "::ffff:192.1.123.2", 1 }, /* IPv6 address dotted-quad, full match */
80 		{ "::ffff:192.1.123.2", ".1.123.2", 0 }, /* IPv6 address dotted-quad, partial match */
81 		{ NULL, ".1.123.2", 0 },
82 		{ "hiho", NULL, 0 },
83 	};
84 	unsigned it;
85 	psl_ctx_t *psl;
86 
87 	psl = psl_load_file(PSL_FILE);
88 
89 	printf("loaded %d suffixes and %d exceptions\n", psl_suffix_count(psl), psl_suffix_exception_count(psl));
90 
91 	for (it = 0; it < countof(test_data); it++) {
92 		const struct test_data *t = &test_data[it];
93 		int result = psl_is_cookie_domain_acceptable(psl, t->request_domain, t->cookie_domain);
94 
95 		if (result == t->result) {
96 			ok++;
97 		} else {
98 			failed++;
99 			printf("psl_is_cookie_domain_acceptable(%s, %s)=%d (expected %d)\n",
100 				t->request_domain, t->cookie_domain, result, t->result);
101 		}
102 	}
103 
104 	/* do checks to cover more code paths in libpsl */
105 	psl_is_cookie_domain_acceptable(NULL, "example.com", "example.com");
106 
107 	psl_free(psl);
108 }
109 
main(int argc,const char * const * argv)110 int main(int argc, const char * const *argv)
111 {
112 	/* if VALGRIND testing is enabled, we have to call ourselves with valgrind checking */
113 	if (argc == 1) {
114 		const char *valgrind = getenv("TESTS_VALGRIND");
115 
116 		if (valgrind && *valgrind) {
117 			size_t cmdsize = strlen(valgrind) + strlen(argv[0]) + 32;
118 			char *cmd = alloca(cmdsize);
119 
120 			snprintf(cmd, cmdsize, "TESTS_VALGRIND="" %s %s", valgrind, argv[0]);
121 			return system(cmd) != 0;
122 		}
123 	}
124 
125 	test_psl();
126 
127 	if (failed) {
128 		printf("Summary: %d out of %d tests failed\n", failed, ok + failed);
129 		return 1;
130 	}
131 
132 	printf("Summary: All %d tests passed\n", ok + failed);
133 	return 0;
134 }
135