Line data Source code
1 : /* TLD library -- Test the TLD library by including the tld.c file.
2 : * Copyright (C) 2011-2017 Made to Order Software Corp.
3 : *
4 : * Permission is hereby granted, free of charge, to any person obtaining a
5 : * copy of this software and associated documentation files (the
6 : * "Software"), to deal in the Software without restriction, including
7 : * without limitation the rights to use, copy, modify, merge, publish,
8 : * distribute, sublicense, and/or sell copies of the Software, and to
9 : * permit persons to whom the Software is furnished to do so, subject to
10 : * the following conditions:
11 : *
12 : * The above copyright notice and this permission notice shall be included
13 : * in all copies or substantial portions of the Software.
14 : *
15 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 : * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 : * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 : * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 : * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 : */
23 :
24 : /** \file
25 : * \brief Test the tld.c, tld_data.c, and tld_domain_to_lowercase.c functions.
26 : *
27 : * This file implements various tests that can directly access the internal
28 : * functions of the tld.c, tld_data.c, and tld_domain_to_lowercase.c
29 : * files.
30 : *
31 : * For that purpose we directly include those files in this test. This
32 : * is why the test is not actually linked against the library, it
33 : * includes it within itself.
34 : */
35 :
36 : #include "tld.c"
37 : #include "tld_data.c"
38 : #include "tld_domain_to_lowercase.c"
39 :
40 : #include <stdlib.h>
41 : #include <string.h>
42 :
43 : int err_count = 0;
44 : int verbose = 0;
45 :
46 1 : void test_compare()
47 : {
48 : struct data
49 : {
50 : const char *a;
51 : const char *b;
52 : int n;
53 : int r;
54 : };
55 1 : struct data d[] = {
56 : { "uj", "uk", 2, -1 },
57 : { "uk", "uk", 2, 0 },
58 : { "ul", "uk", 2, 1 },
59 :
60 : { "uj", "ukmore", 2, -1 },
61 : { "uk", "ukstuff", 2, 0 },
62 : { "ul", "ukhere", 2, 1 },
63 :
64 : { "uk1", "ukmore", 2, 1 },
65 : { "uk2", "ukstuff", 2, 1 },
66 : { "uk3", "ukhere", 2, 1 },
67 :
68 : { "uk1", "uk.", 3, 1 },
69 : { "uk2", "uk.", 3, 1 },
70 : { "uk3", "uk.", 3, 1 },
71 :
72 : { "uk1", ".uk", 3, 1 },
73 : { "uk2", ".uk", 3, 1 },
74 : { "uk3", ".uk", 3, 1 },
75 :
76 : { "uk", "uk1", 3, -1 },
77 : { "uk", "uk22", 4, -1 },
78 : { "uk", "uk333", 5, -1 },
79 :
80 : { "uk1", "uk", 2, 1 },
81 : { "uk22", "uk", 2, 1 },
82 : { "uk333", "uk", 2, 1 },
83 : };
84 : int i, r, max;
85 : char *s, *vd, *u;
86 :
87 1 : max = sizeof(d) / sizeof(d[0]);
88 22 : for(i = 0; i < max; ++i)
89 : {
90 21 : r = cmp(d[i].a, d[i].b, d[i].n);
91 21 : if(r != d[i].r) {
92 0 : fprintf(stderr, "error: cmp() failed with \"%s\" / \"%s\", expected %d and got %d [1]\n",
93 : d[i].a, d[i].b, d[i].r, r);
94 0 : ++err_count;
95 : }
96 :
97 : // create a version with uppercase and try again
98 21 : s = strdup(d[i].b);
99 101 : for(u = s; *u != '\0'; ++u)
100 : {
101 80 : if(*u >= 'a' && *u <= 'z')
102 : {
103 68 : *u &= 0x5F;
104 : }
105 : }
106 21 : vd = tld_domain_to_lowercase(s);
107 21 : r = cmp(d[i].a, d[i].b, d[i].n);
108 21 : if(r != d[i].r) {
109 0 : fprintf(stderr, "error: cmp() failed with \"%s\" / \"%s\", expected %d and got %d (with domain to lowercase) [2]\n",
110 : d[i].a, d[i].b, d[i].r, r);
111 0 : ++err_count;
112 : }
113 21 : free(vd);
114 21 : free(s);
115 : }
116 1 : }
117 :
118 1 : void test_search()
119 : {
120 : struct search_info
121 : {
122 : int f_start;
123 : int f_end;
124 : const char * f_tld;
125 : int f_length;
126 : int f_result;
127 : };
128 1 : struct search_info d[] = {
129 : /*
130 : * This table is very annoying since each time the data changes
131 : * it gets out of sync. On the other hand that's the best way
132 : * to make sure our tests work like in the real world.
133 : */
134 :
135 : /* get the .uk offset */
136 : { 7929, 9537, "uk", 2, 9419 },
137 :
138 : /* get each offset of the .uk 2nd level domain */
139 : { 7712, 7737, "ac", 2, 7712 },
140 : { 7712, 7737, "bl", 2, 7713 },
141 : { 7712, 7737, "british-library", 15, 7714 },
142 : { 7712, 7737, "co", 2, 7715 },
143 : { 7712, 7737, "gov", 3, 7716 },
144 : { 7712, 7737, "govt", 4, 7717 },
145 : { 7712, 7737, "icnet", 5, 7718 },
146 : { 7712, 7737, "jet", 3, 7719 },
147 : { 7712, 7737, "lea", 3, 7720 },
148 : { 7712, 7737, "ltd", 3, 7721 },
149 : { 7712, 7737, "me", 2, 7722 },
150 : { 7712, 7737, "mil", 3, 7723 },
151 : { 7712, 7737, "mod", 3, 7724 },
152 : { 7712, 7737, "national-library-scotland", 25, 7725 },
153 : { 7712, 7737, "nel", 3, 7726 },
154 : { 7712, 7737, "net", 3, 7727 },
155 : { 7712, 7737, "nhs", 3, 7728 },
156 : { 7712, 7737, "nic", 3, 7729 },
157 : { 7712, 7737, "nls", 3, 7730 },
158 : { 7712, 7737, "org", 3, 7731 },
159 : { 7712, 7737, "orgn", 4, 7732 },
160 : { 7712, 7737, "parliament", 10, 7733 },
161 : { 7712, 7737, "plc", 3, 7734 },
162 : { 7712, 7737, "police", 6, 7735 },
163 : { 7712, 7737, "sch", 3, 7736 },
164 :
165 : /* test with a few invalid TLDs for .uk */
166 : { 7712, 7737, "com", 3, -1 },
167 : { 7712, 7737, "aca", 3, -1 },
168 : { 7712, 7737, "aac", 3, -1 },
169 : { 7712, 7737, "ca", 2, -1 },
170 : { 7712, 7737, "cn", 2, -1 },
171 : { 7712, 7737, "cp", 2, -1 },
172 : { 7712, 7737, "cz", 2, -1 },
173 :
174 : /* get the .vu offset */
175 : { 7929, 9537, "vu", 2, 9466 },
176 :
177 : /* get the 2nd level .vu offsets */
178 : { 7859, 7864, "com", 3, 7859 },
179 : { 7859, 7864, "edu", 3, 7860 },
180 : { 7859, 7864, "gov", 3, 7861 },
181 : { 7859, 7864, "net", 3, 7862 },
182 : { 7859, 7864, "org", 3, 7863 },
183 :
184 : /* test with a few .vu 2nd level domains that do not exist */
185 : { 7859, 7864, "nom", 3, -1 },
186 : { 7859, 7864, "sch", 3, -1 },
187 :
188 : /* verify ordering of mari, mari-el, and marine (from .ru) */
189 : { 7126, 7264, "mari", 4, 7190 },
190 : { 7126, 7264, "mari-el", 7, 7191 },
191 : { 7126, 7264, "marine", 6, 7192 },
192 : };
193 :
194 : size_t i;
195 :
196 1 : size_t const max = sizeof(d) / sizeof(d[0]);
197 45 : for(i = 0; i < max; ++i)
198 : {
199 44 : int const r = search(d[i].f_start, d[i].f_end, d[i].f_tld, d[i].f_length);
200 44 : if(r != d[i].f_result)
201 : {
202 0 : fprintf(stderr, "error: test_search() failed with \"%s\", expected %d and got %d [3]\n",
203 : d[i].f_tld, d[i].f_result, r);
204 0 : ++err_count;
205 : }
206 : }
207 1 : }
208 :
209 :
210 592 : void test_search_array(int start, int end)
211 : {
212 : int i, r;
213 :
214 : /* now test all from the arrays */
215 10129 : for(i = start; i < end; ++i)
216 : {
217 9537 : if(verbose)
218 : {
219 0 : printf("{%d..%d} i = %d, [%s]\n", start, end, i, tld_descriptions[i].f_tld);
220 : }
221 9537 : r = search(start, end, tld_descriptions[i].f_tld, strlen(tld_descriptions[i].f_tld));
222 9537 : if(r != i)
223 : {
224 0 : fprintf(stderr, "error: test_search_array() failed with \"%s\", expected %d and got %d [4]\n",
225 : tld_descriptions[i].f_tld, i, r);
226 0 : ++err_count;
227 : }
228 9537 : if(tld_descriptions[i].f_start_offset != USHRT_MAX)
229 : {
230 591 : test_search_array(tld_descriptions[i].f_start_offset,
231 591 : tld_descriptions[i].f_end_offset);
232 : }
233 : }
234 592 : }
235 :
236 1 : void test_search_all()
237 : {
238 1 : test_search_array(tld_start_offset, tld_end_offset);
239 1 : }
240 :
241 :
242 1 : int main(int argc, char *argv[])
243 : {
244 1 : fprintf(stderr, "testing internal tld version %s\n", tld_version());
245 :
246 1 : if(argc > 1)
247 : {
248 0 : if(strcmp(argv[1], "-v") == 0)
249 : {
250 0 : verbose = 1;
251 : }
252 : }
253 :
254 : /* call all the tests, one by one
255 : * failures are "recorded" in the err_count global variable
256 : * and the process stops with an error message and exit(1)
257 : * if err_count is not zero.
258 : */
259 1 : test_compare();
260 1 : test_search();
261 1 : test_search_all();
262 :
263 1 : if(err_count)
264 : {
265 0 : fprintf(stderr, "%d error%s occured.\n",
266 0 : err_count, err_count != 1 ? "s" : "");
267 : }
268 1 : exit(err_count ? 1 : 0);
269 : }
270 :
271 : /* vim: ts=4 sw=4
272 : */
|