Debian Codegraph @

tests/test-is-public-builtin.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 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 #elif defined _WIN32
41 # include <malloc.h>
42 #endif
43
44 #include <libpsl.h>
45
46 #define countof(a) (sizeof(a)/sizeof(*(a)))
47
48 static int
49 ok,
50 failed;
51
52 static void test_psl(void)
53 {
54 /* punycode generation: idn ?? */
55 /* octal code generation: echo -n "??" | od -b */
56 static const struct test_data {
57 const char
58 *domain;
59 int
60 result;
61 int
62 no_star_result;
63 } test_data[] = {
64 { "www.example.com", 0, 0 },
65 { "com.ar", 1, 1 },
66 { "www.com.ar", 0, 0 },
67 { "cc.ar.us", 1, 1 },
68 { ".cc.ar.us", 1, 1 },
69 { "www.cc.ar.us", 0, 0 },
70 { "www.ck", 0, 0 }, /* exception from *.ck */
71 { "abc.www.ck", 0, 0 },
72 { "xxx.ck", 1, 1 },
73 { "www.xxx.ck", 0, 0 },
74 { "\345\225\206\346\240\207", 1, 1 }, /* xn--czr694b or ?? */
75 { "www.\345\225\206\346\240\207", 0, 0 },
76 { "xn--czr694b", 1, 1 },
77 { "www.xn--czr694b", 0, 0 },
78 /* some special test follow ('name' and 'forgot.his.name' are public, but e.g. his.name is not) */
79 { "name", 1, 1 },
80 { ".name", 1, 1 },
81 { "his.name", 0, 0 },
82 { ".his.name", 0, 0 },
83 { "forgot.his.name", 1, 1 },
84 { ".forgot.his.name", 1, 1 },
85 { "whoever.his.name", 0, 0 },
86 { "whoever.forgot.his.name", 0, 0 },
87 { "whatever.platformsh.site", 1, 1 },
88 { ".platformsh.site", 1, 1 },
89 { "whatever.yokohama.jp", 1, 1 },
90 { ".yokohama.jp", 1, 1 },
91 { ".", 1, 0 }, /* special case */
92 { "", 1, 0 }, /* special case */
93 { NULL, 1, 1 }, /* special case */
94 { "adfhoweirh", 1, 0 }, /* unknown TLD */
95 { "compute.amazonaws.com", 1, 1 }, /* special rule *.compute.amazonaws.com */
96 { "y.compute.amazonaws.com", 1, 1 },
97 { "x.y.compute.amazonaws.com", 0, 0 },
98 };
99 unsigned it;
100 const psl_ctx_t *psl;
101
102 psl = psl_builtin();
103
104 printf("have %d suffixes and %d exceptions\n", psl_suffix_count(psl), psl_suffix_exception_count(psl));
105
106 for (it = 0; it < countof(test_data); it++) {
107 const struct test_data *t = &test_data[it];
108 int result = psl_is_public_suffix(psl, t->domain);
109
110 if (result == t->result) {
111 ok++;
112 } else {
113 failed++;
114 printf("psl_is_public_suffix(%s)=%d (expected %d)\n", t->domain, result, t->result);
115 }
116 }
117
118 for (it = 0; it < countof(test_data); it++) {
119 const struct test_data *t = &test_data[it];
120 int result = psl_is_public_suffix2(psl, t->domain, PSL_TYPE_ANY|PSL_TYPE_NO_STAR_RULE);
121
122 if (result == t->no_star_result) {
123 ok++;
124 } else {
125 failed++;
126 printf("psl_is_public_suffix2(%s, NO_STAR_RULE)=%d (expected %d)\n", t->domain, result, t->no_star_result);
127 }
128 }
129
130 printf("psl_builtin_file_time()=%ld\n", (long) psl_builtin_file_time());
131 psl_builtin_file_time() == 0 ? failed++ : ok++;
132
133 printf("psl_builtin_sha1sum()=%s\n", psl_builtin_sha1sum());
134 *psl_builtin_sha1sum() == 0 ? failed++ : ok++;
135 }
136
137 int main(int argc, const char * const *argv)
138 {
139 /* if VALGRIND testing is enabled, we have to call ourselves with valgrind checking */
140 if (argc == 1) {
141 const char *valgrind = getenv("TESTS_VALGRIND");
142
143 if (valgrind && *valgrind) {
144 size_t cmdsize = strlen(valgrind) + strlen(argv[0]) + 32;
145 char *cmd = alloca(cmdsize);
146
147 snprintf(cmd, cmdsize, "TESTS_VALGRIND="" %s %s", valgrind, argv[0]);
148 return system(cmd) != 0;
149 }
150 }
151
152 test_psl();
153
154 if (failed) {
155 printf("Summary: %d out of %d tests failed\n", failed, ok + failed);
156 return 1;
157 }
158
159 printf("Summary: All %d tests passed\n", ok + failed);
160 return 0;
161 }