Debian Codegraph @

tests/test-is-cookie-domain-acceptable.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 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 #ifdef _WIN32
36 # include <winsock2.h> // WSAStartup, WSACleanup
37 #endif
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #ifdef HAVE_ALLOCA_H
43 # include <alloca.h>
44 #endif
45
46 #include <libpsl.h>
47
48 #define countof(a) (sizeof(a)/sizeof(*(a)))
49
50 static int
51 ok,
52 failed;
53
54 static void test_psl(void)
55 {
56 static const struct test_data {
57 const char
58 *request_domain,
59 *cookie_domain;
60 int
61 result;
62 } test_data[] = {
63 { "www.dkg.forgot.his.name", "www.dkg.forgot.his.name", 1 },
64 { "www.dkg.forgot.his.name", "dkg.forgot.his.name", 1 },
65 { "www.dkg.forgot.his.name", "forgot.his.name", 0 },
66 { "www.dkg.forgot.his.name", "his.name", 0 },
67 { "www.dkg.forgot.his.name", "name", 0 },
68 { "www.his.name", "www.his.name", 1 },
69 { "www.his.name", "his.name", 1 },
70 { "www.his.name", "name", 0 },
71 { "www.example.com", "www.example.com", 1 },
72 { "www.example.com", "wwww.example.com", 0 },
73 { "www.example.com", "example.com", 1 },
74 { "www.example.com", "com", 0 }, /* not accepted by normalization (PSL rule 'com') */
75 { "www.example.com", "example.org", 0 },
76 { "www.sa.gov.au", "sa.gov.au", 0 }, /* not accepted by normalization (PSL rule '*.ar') */
77 { "www.educ.ar", "educ.ar", 1 }, /* PSL exception rule '!educ.ar' */
78 /* RFC6265 5.1.3: Having IP addresses, request and domain IP must be identical */
79 { "192.1.123.2", ".1.123.2", 0 }, /* IPv4 address, partial match */
80 { "192.1.123.2", "192.1.123.2", 1 }, /* IPv4 address, full match */
81 { "::1", "::1", 1 }, /* IPv6 address, full match */
82 { "2a00:1450:4013:c01::8b", ":1450:4013:c01::8b", 0 }, /* IPv6 address, partial match */
83 { "::ffff:192.1.123.2", "::ffff:192.1.123.2", 1 }, /* IPv6 address dotted-quad, full match */
84 { "::ffff:192.1.123.2", ".1.123.2", 0 }, /* IPv6 address dotted-quad, partial match */
85 { NULL, ".1.123.2", 0 },
86 { "hiho", NULL, 0 },
87 };
88 unsigned it;
89 psl_ctx_t *psl;
90
91 psl = psl_load_file(PSL_FILE);
92
93 printf("loaded %d suffixes and %d exceptions\n", psl_suffix_count(psl), psl_suffix_exception_count(psl));
94
95 for (it = 0; it < countof(test_data); it++) {
96 const struct test_data *t = &test_data[it];
97 int result = psl_is_cookie_domain_acceptable(psl, t->request_domain, t->cookie_domain);
98
99 if (result == t->result) {
100 ok++;
101 } else {
102 failed++;
103 printf("psl_is_cookie_domain_acceptable(%s, %s)=%d (expected %d)\n",
104 t->request_domain, t->cookie_domain, result, t->result);
105 }
106 }
107
108 /* do checks to cover more code paths in libpsl */
109 psl_is_cookie_domain_acceptable(NULL, "example.com", "example.com");
110
111 psl_free(psl);
112 }
113
114 int main(int argc, const char * const *argv)
115 {
116 #ifdef _WIN32
117 WSADATA wsa_data;
118 int err;
119
120 if ((err = WSAStartup(MAKEWORD(2,2), &wsa_data))) {
121 printf("WSAStartup failed with error: %d\n", err);
122 return 1;
123 }
124
125 atexit((void (__cdecl*)(void)) WSACleanup);
126 #endif
127
128 /* if VALGRIND testing is enabled, we have to call ourselves with valgrind checking */
129 if (argc == 1) {
130 const char *valgrind = getenv("TESTS_VALGRIND");
131
132 if (valgrind && *valgrind) {
133 size_t cmdsize = strlen(valgrind) + strlen(argv[0]) + 32;
134 char *cmd = alloca(cmdsize);
135
136 snprintf(cmd, cmdsize, "TESTS_VALGRIND="" %s %s", valgrind, argv[0]);
137 return system(cmd) != 0;
138 }
139 }
140
141 test_psl();
142
143 if (failed) {
144 printf("Summary: %d out of %d tests failed\n", failed, ok + failed);
145 return 1;
146 }
147
148 printf("Summary: All %d tests passed\n", ok + failed);
149 return 0;
150 }