Line data Source code
1 : /* TLD library -- test the TLD interface
2 : * Copyright (c) 2011-2018 Made to Order Software Corp. All Rights Reserved
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 a full URI with tld_check_uri() function.
26 : *
27 : * This file implements various tests to verify that the
28 : * tld_check_uri() function works as expected.
29 : */
30 :
31 : #include "libtld/tld.h"
32 : #include <string.h>
33 : #include <stdlib.h>
34 : #include <stdio.h>
35 : #include <limits.h>
36 :
37 : /* we get access to the table with all the TLDs so we can go through them all
38 : * the library does not give direct access by default... (although maybe we
39 : * could give users access to the data)
40 : */
41 : #include "tld_data.h"
42 : extern const struct tld_description tld_descriptions[];
43 : extern unsigned short tld_start_offset;
44 : extern unsigned short tld_end_offset;
45 :
46 : int err_count = 0;
47 : int verbose = 0;
48 :
49 :
50 :
51 : /// \brief Structure used to define many tests to run against the tld_check_uri() function
52 : struct test_info
53 : {
54 : /// The URI to be tested
55 : const char * f_uri;
56 : /// The valid protocols for that test
57 : const char * f_protocols;
58 : /// Flags used to call the tld_check_uri() function
59 : int f_flags;
60 : /// The expected result
61 : enum tld_result f_result;
62 : /// The expected info values
63 : struct tld_info f_info;
64 : };
65 :
66 : /* WARNING: the first and last protocols need to be tested for full
67 : * validity of the library; if you add more, add them in the
68 : * middle or update the list of entries below to include the
69 : * new start/end protocols.
70 : */
71 : #define PROTOCOLS "ftp,sftp,http,https,gopher"
72 :
73 : const struct test_info test_info_entries[] =
74 : {
75 : /* NULL entries */
76 : {
77 : NULL,
78 : PROTOCOLS,
79 : 0,
80 : TLD_RESULT_NULL,
81 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
82 : },
83 : {
84 : "",
85 : PROTOCOLS,
86 : 0,
87 : TLD_RESULT_NULL,
88 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
89 : },
90 : /* invalid entries */
91 : {
92 : "bad://www.m2osw.th/bad-protocol",
93 : PROTOCOLS,
94 : 0,
95 : TLD_RESULT_BAD_URI,
96 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
97 : },
98 : {
99 : "b!d://www.m2osw.cg/bad-protocol/character",
100 : PROTOCOLS,
101 : 0,
102 : TLD_RESULT_BAD_URI,
103 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
104 : },
105 : {
106 : "https:/www.m2osw.sx.cn/missing/slash",
107 : PROTOCOLS,
108 : 0,
109 : TLD_RESULT_BAD_URI,
110 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
111 : },
112 : {
113 : "http:q/www.m2osw.lezajsk.pl/q/instead/of/slash",
114 : PROTOCOLS,
115 : 0,
116 : TLD_RESULT_BAD_URI,
117 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
118 : },
119 : {
120 : "https::/www.m2osw.museum.mv/two/colons",
121 : PROTOCOLS,
122 : 0,
123 : TLD_RESULT_BAD_URI,
124 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
125 : },
126 : {
127 : "http:///www.m2osw.rec.co/3/slashes",
128 : PROTOCOLS,
129 : 0,
130 : TLD_RESULT_BAD_URI,
131 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
132 : },
133 : {
134 : "https://@www.m2osw.co.zm/bad?user=info",
135 : PROTOCOLS,
136 : 0,
137 : TLD_RESULT_BAD_URI,
138 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
139 : },
140 : {
141 : "https://@www.m2osw.mil.py@/bad?user=info",
142 : PROTOCOLS,
143 : 0,
144 : TLD_RESULT_BAD_URI,
145 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
146 : },
147 : {
148 : "https://:password1@www.m2osw.net.tn/bad?user=info",
149 : PROTOCOLS,
150 : 0,
151 : TLD_RESULT_BAD_URI,
152 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
153 : },
154 : {
155 : "https://alexis:@www.m2osw.int.rw/bad?user=info",
156 : PROTOCOLS,
157 : 0,
158 : TLD_RESULT_BAD_URI,
159 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
160 : },
161 : {
162 : "https://:@www.m2osw.l.lc/bad?user=info",
163 : PROTOCOLS,
164 : 0,
165 : TLD_RESULT_BAD_URI,
166 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
167 : },
168 : {
169 : "https://www.m2osw.mil.sh:/bad?port=empty",
170 : PROTOCOLS,
171 : 0,
172 : TLD_RESULT_BAD_URI,
173 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
174 : },
175 : {
176 : "https://www.m2osw.krasnoyarsk.ru:abc/bad?port=invalid&p=number",
177 : PROTOCOLS,
178 : 0,
179 : TLD_RESULT_BAD_URI,
180 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
181 : },
182 : {
183 : "sftp://www.m2osw.cat.tt:-33/bad?port=unexpected number",
184 : PROTOCOLS,
185 : 0,
186 : TLD_RESULT_BAD_URI,
187 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
188 : },
189 : {
190 : "https://www.m2osw.cat.tt/bad/variable/name?=value",
191 : PROTOCOLS,
192 : 0,
193 : TLD_RESULT_BAD_URI,
194 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
195 : },
196 : {
197 : "http://www.m2osw.edu.pt/bar%xx/percent",
198 : PROTOCOLS,
199 : 0,
200 : TLD_RESULT_BAD_URI,
201 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
202 : },
203 : {
204 : "http://www.m2osw.tjeldsund.no/con\trol/character",
205 : PROTOCOLS,
206 : 0,
207 : TLD_RESULT_BAD_URI,
208 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
209 : },
210 : {
211 : "http://www.m2osw.edu.pt/con%09rol/character", /* %09 = \t */
212 : PROTOCOLS,
213 : 0,
214 : TLD_RESULT_BAD_URI,
215 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
216 : },
217 : {
218 : "http://www.m2osw.other.nf/space character",
219 : PROTOCOLS,
220 : VALID_URI_NO_SPACES,
221 : TLD_RESULT_BAD_URI,
222 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
223 : },
224 : {
225 : "http://www.m2osw.co.mp/space+character",
226 : PROTOCOLS,
227 : VALID_URI_NO_SPACES,
228 : TLD_RESULT_BAD_URI,
229 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
230 : },
231 : {
232 : "http://www.m2osw.bv.nl/space%20character",
233 : PROTOCOLS,
234 : VALID_URI_NO_SPACES,
235 : TLD_RESULT_BAD_URI,
236 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
237 : },
238 : {
239 : "http://www.m2osw.gov.pk/bad/variable/name?good=value&=at&the=beginning",
240 : PROTOCOLS,
241 : 0,
242 : TLD_RESULT_BAD_URI,
243 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
244 : },
245 : {
246 : "http://www.m2osw.rakpetroleum.om/bad/variable/name?good=value&at=the&=beginning",
247 : PROTOCOLS,
248 : 0,
249 : TLD_RESULT_BAD_URI,
250 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
251 : },
252 : {
253 : "http://www.m2osw.orange.om/missing/question/mark&good=values&here=perfect",
254 : PROTOCOLS,
255 : 0,
256 : TLD_RESULT_BAD_URI,
257 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
258 : },
259 : {
260 : "http://www.m2osw.orange.om/invalid%9ftoo/large",
261 : PROTOCOLS,
262 : VALID_URI_ASCII_ONLY,
263 : TLD_RESULT_BAD_URI,
264 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
265 : },
266 : {
267 : "http://www.m2osw.orange.om/invalid%9ztoo/large",
268 : PROTOCOLS,
269 : 0,
270 : TLD_RESULT_BAD_URI,
271 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
272 : },
273 : {
274 : "http://www.m2osw.orange.om/invalid%z9too/large",
275 : PROTOCOLS,
276 : 0,
277 : TLD_RESULT_BAD_URI,
278 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
279 : },
280 : {
281 : "http://www.m2osw.backspace.\010.no/forbid/control-characters",
282 : PROTOCOLS,
283 : 0,
284 : TLD_RESULT_BAD_URI,
285 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
286 : },
287 : {
288 : "http://www.m2osw.backspace.no/forbid/control-characters/\010/in/path/too",
289 : PROTOCOLS,
290 : 0,
291 : TLD_RESULT_BAD_URI,
292 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
293 : },
294 : {
295 : "http://www.m2osw.and\xF8y.no/forbid/special-characters",
296 : PROTOCOLS,
297 : VALID_URI_ASCII_ONLY,
298 : TLD_RESULT_BAD_URI,
299 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
300 : },
301 : {
302 : "http://www.m2osw.and%F8y.no/forbid/special-characters",
303 : PROTOCOLS,
304 : VALID_URI_ASCII_ONLY,
305 : TLD_RESULT_BAD_URI,
306 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
307 : },
308 : {
309 : "http://www.m2osw.and .no/forbid/spaces",
310 : PROTOCOLS,
311 : VALID_URI_ASCII_ONLY,
312 : TLD_RESULT_BAD_URI,
313 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
314 : },
315 : {
316 : "http://www.m2osw.and+.no/forbid/spaces",
317 : PROTOCOLS,
318 : VALID_URI_ASCII_ONLY,
319 : TLD_RESULT_BAD_URI,
320 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
321 : },
322 : {
323 : "http://www.m2osw.and%20.no/forbid/spaces",
324 : PROTOCOLS,
325 : VALID_URI_ASCII_ONLY,
326 : TLD_RESULT_BAD_URI,
327 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
328 : },
329 : {
330 : "http://www.m2osw.%1F.no/escape/forbidden",
331 : PROTOCOLS,
332 : VALID_URI_ASCII_ONLY,
333 : TLD_RESULT_BAD_URI,
334 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
335 : },
336 : {
337 : "http://www.m2osw.no/large/characters\xF0/forbidden",
338 : PROTOCOLS,
339 : VALID_URI_ASCII_ONLY,
340 : TLD_RESULT_BAD_URI,
341 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
342 : },
343 : {
344 : "http://a--really--long--domain-name--is-actually--forbidden.this--is--done-by--checking--the--length--which--is--limited--to--two--hundred--and--fifty--six--characters.note--that--the--buffer--must--include--a--null--terminator--which--means--you--really--are--limited--to--255--characters.www.m2osw.no/large/characters\xF0/forbidden",
345 : PROTOCOLS,
346 : 0,
347 : TLD_RESULT_BAD_URI,
348 : { TLD_CATEGORY_UNDEFINED, TLD_STATUS_UNDEFINED, NULL, NULL, -1 }
349 : },
350 : /* valid entries */
351 : {
352 : "https://www.m2osw.co.uk/simple",
353 : PROTOCOLS,
354 : 0,
355 : TLD_RESULT_SUCCESS,
356 : { TLD_CATEGORY_COUNTRY, TLD_STATUS_VALID, "United Kingdom", ".co.uk/", 17 }
357 : },
358 : {
359 : "ftp://www.m2osw.ltd%2Euk/encoded/period", /* still encoded */
360 : PROTOCOLS,
361 : 0,
362 : TLD_RESULT_SUCCESS,
363 : { TLD_CATEGORY_COUNTRY, TLD_STATUS_VALID, "United Kingdom", ".ltd%2Euk/", 15 }
364 : },
365 : {
366 : "http://www.m2osw.orange.ac.om/valid%9fcharacter",
367 : PROTOCOLS,
368 : 0,
369 : TLD_RESULT_SUCCESS,
370 : { TLD_CATEGORY_COUNTRY, TLD_STATUS_VALID, "Oman", ".ac.om", 23 }
371 : },
372 : {
373 : "http://www.m2osw.orange.ac.om/valid\x9f/character",
374 : PROTOCOLS,
375 : 0,
376 : TLD_RESULT_SUCCESS,
377 : { TLD_CATEGORY_COUNTRY, TLD_STATUS_VALID, "Oman", ".ac.om", 23 }
378 : },
379 : {
380 : "ftp://www.m2osw.ltd%2euk/encoded/period", /* still encoded */
381 : PROTOCOLS,
382 : 0,
383 : TLD_RESULT_SUCCESS,
384 : { TLD_CATEGORY_COUNTRY, TLD_STATUS_VALID, "United Kingdom", ".ltd%2euk/", 15 }
385 : },
386 : {
387 : "http://m2osw.org.nr/encoded/period?#&=55", /* looks like an empty variable name */
388 : PROTOCOLS,
389 : 0,
390 : TLD_RESULT_SUCCESS,
391 : { TLD_CATEGORY_COUNTRY, TLD_STATUS_VALID, "Nauru", ".org.nr/", 12 }
392 : },
393 : {
394 : "http://snap.m2osw.blogspot.mx:888/longer/address",
395 : PROTOCOLS,
396 : 0,
397 : TLD_RESULT_SUCCESS,
398 : { TLD_CATEGORY_ENTREPRENEURIAL, TLD_STATUS_VALID, NULL, ".blogspot.mx:", 17 }
399 : },
400 : {
401 : "HTTP://alexis:password1@www.m2osw.com:888/correct/uri?with=variable",
402 : PROTOCOLS,
403 : 0,
404 : TLD_RESULT_SUCCESS,
405 : { TLD_CATEGORY_INTERNATIONAL, TLD_STATUS_VALID, NULL, ".com:", 33 }
406 : },
407 : {
408 : "ftp://ftp.m2osw.me/freeware/download/snap/snap-1.0.tar.gz",
409 : PROTOCOLS,
410 : 0,
411 : TLD_RESULT_SUCCESS,
412 : { TLD_CATEGORY_COUNTRY, TLD_STATUS_VALID, "Montenegro", ".me/", 15 }
413 : },
414 : {
415 : "gopher://news.m2osw.ca.us:71/protocols",
416 : PROTOCOLS,
417 : 0,
418 : TLD_RESULT_SUCCESS,
419 : { TLD_CATEGORY_COUNTRY, TLD_STATUS_VALID, "United States", ".ca.us:", 19 }
420 : },
421 : {
422 : "random://news.m2osw.edu.mm/asterisk/protocols",
423 : "*",
424 : 0,
425 : TLD_RESULT_SUCCESS,
426 : { TLD_CATEGORY_COUNTRY, TLD_STATUS_VALID, "Republic of the Union of Myanmar", ".edu.mm/", 19 }
427 : },
428 : };
429 : #define test_info_entries_length (sizeof(test_info_entries) / sizeof(test_info_entries[0]))
430 :
431 :
432 1 : void test_uri()
433 : {
434 : struct tld_info info;
435 : enum tld_result result;
436 : size_t i;
437 :
438 51 : for(i = 0; i < test_info_entries_length; ++i)
439 : {
440 : //fprintf(stderr, "testing [%s]\n", test_info_entries[i].f_uri);
441 50 : result = tld_check_uri(test_info_entries[i].f_uri, &info, test_info_entries[i].f_protocols, test_info_entries[i].f_flags);
442 50 : if(result != test_info_entries[i].f_result)
443 : {
444 0 : fprintf(stderr, "error:%s: URI failed with the wrong result: %d\n", test_info_entries[i].f_uri, result);
445 0 : ++err_count;
446 : }
447 :
448 50 : if(test_info_entries[i].f_info.f_tld == NULL)
449 : {
450 39 : if(info.f_tld != NULL)
451 : {
452 0 : fprintf(stderr, "error:%s: TLD was expected to be NULL, got \"%s\" instead.\n", test_info_entries[i].f_uri, info.f_tld);
453 0 : ++err_count;
454 : }
455 : }
456 : else
457 : {
458 11 : if(info.f_tld == NULL)
459 : {
460 0 : fprintf(stderr, "error:%s: TLD was not expected to be NULL but \"%s\" instead.\n", test_info_entries[i].f_uri, test_info_entries[i].f_info.f_tld);
461 0 : ++err_count;
462 : }
463 11 : else if(strncmp(test_info_entries[i].f_info.f_tld, info.f_tld, strlen(test_info_entries[i].f_info.f_tld)) != 0)
464 : {
465 0 : fprintf(stderr, "error:%s: TLD was not properly extracted: \"%s\".\n", test_info_entries[i].f_uri, info.f_tld);
466 0 : ++err_count;
467 : }
468 : }
469 :
470 50 : if(info.f_category != test_info_entries[i].f_info.f_category)
471 : {
472 0 : fprintf(stderr, "error:%s: category was not properly extracted.\n", test_info_entries[i].f_uri);
473 0 : ++err_count;
474 : }
475 :
476 50 : if(info.f_status != test_info_entries[i].f_info.f_status)
477 : {
478 0 : fprintf(stderr, "error:%s: category was not properly extracted.\n", test_info_entries[i].f_uri);
479 0 : ++err_count;
480 : }
481 :
482 50 : if(test_info_entries[i].f_info.f_country == NULL)
483 : {
484 41 : if(info.f_country != NULL)
485 : {
486 0 : fprintf(stderr, "error:%s: country was expected to be NULL.\n", test_info_entries[i].f_uri);
487 0 : ++err_count;
488 : }
489 : }
490 : else
491 : {
492 9 : if(info.f_country == NULL)
493 : {
494 0 : fprintf(stderr, "error:%s: country is NULL when it was expected to be \"%s\".\n", test_info_entries[i].f_uri, test_info_entries[i].f_info.f_country);
495 0 : ++err_count;
496 : }
497 9 : else if(strcmp(info.f_country, test_info_entries[i].f_info.f_country) != 0)
498 : {
499 0 : fprintf(stderr, "error:%s: country was not properly extracted.\n", test_info_entries[i].f_uri);
500 0 : ++err_count;
501 : }
502 : }
503 :
504 50 : if(info.f_offset != test_info_entries[i].f_info.f_offset)
505 : {
506 0 : fprintf(stderr, "error:%s: TLD offset is not the expected value (%d).\n", test_info_entries[i].f_uri, info.f_offset);
507 0 : ++err_count;
508 : }
509 : }
510 1 : }
511 :
512 :
513 :
514 :
515 :
516 :
517 :
518 :
519 :
520 1 : int main(int argc, char *argv[])
521 : {
522 1 : fprintf(stderr, "testing tld full URI version %s: tld_valid_uri() function\n", tld_version());
523 :
524 1 : if(argc > 1)
525 : {
526 0 : if(strcmp(argv[1], "-v") == 0)
527 : {
528 0 : verbose = 1;
529 : }
530 : }
531 :
532 : /* call all the tests, one by one
533 : * failures are "recorded" in the err_count global variable
534 : * and the process stops with an error message and exit(1)
535 : * if err_count is not zero.
536 : */
537 1 : test_uri();
538 :
539 1 : if(err_count)
540 : {
541 0 : fprintf(stderr, "%d error%s occured.\n",
542 0 : err_count, err_count != 1 ? "s" : "");
543 : }
544 1 : exit(err_count ? 1 : 0);
545 : }
546 :
547 : /* vim: ts=4 sw=4 et
548 : */
|