aboutsummaryrefslogtreecommitdiff
path: root/src/lib/util/test_uri.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/util/test_uri.c')
-rw-r--r--src/lib/util/test_uri.c838
1 files changed, 838 insertions, 0 deletions
diff --git a/src/lib/util/test_uri.c b/src/lib/util/test_uri.c
new file mode 100644
index 000000000..d8fa38e96
--- /dev/null
+++ b/src/lib/util/test_uri.c
@@ -0,0 +1,838 @@
1#include "platform.h"
2#include <stdlib.h>
3#include <stdio.h>
4#include <string.h>
5#include "gnunet_uri_lib.h"
6
7#define KNRM "\x1B[0m"
8#define KBLU "\x1B[34m"
9#define KGRN "\x1B[32m"
10#define KERR "\x1B[5;31;50m"
11
12/* macro to print out the header for a new group of tests */
13#define mu_group(name) printf ("%s • %s%s\n", KBLU, name, KNRM)
14
15/* macro for asserting a statement */
16#define mu_assert(message, test) do { \
17 if (!(test)) { \
18 printf ("\t%s× %s%s\n", KERR, message, KNRM); \
19 return message; \
20 } \
21 printf ("\t%s• %s%s\n", KGRN, message, KNRM); \
22 } while (0)
23
24/* macro for asserting a statement without printing it unless it is a failure */
25#define mu_silent_assert(message, test) do { \
26 if (!(test)) { \
27 printf ("\t\t%s× %s%s\n", KERR, message, KNRM); \
28 return message; \
29 } \
30 } while (0)
31
32/* run a test function and return result */
33#define mu_run_test(test) do { \
34 char *message = test (); tests_run++; \
35 if (message) { return message; } \
36 } while (0)
37
38
39int tests_run;
40
41static int
42strcmp_wrap (const char *str,
43 const char *str2)
44{
45 if (NULL == str && NULL == str2) {
46 return 0;
47 }
48 if (NULL == str) {
49 return 1;
50 }
51 if (NULL == str2) {
52 return -1;
53 }
54
55 return strcmp (str, str2);
56}
57
58#define assert_struct(as_url, \
59 as_scheme, \
60 as_user, \
61 as_pass, \
62 as_host, \
63 as_port, \
64 as_path, \
65 as_query, \
66 as_fragment) \
67 mu_silent_assert ("should set the scheme attribute correctly", \
68 0 == strcmp_wrap (as_url.scheme, as_scheme)); \
69 mu_silent_assert ("should set the username attribute correctly", \
70 0 == strcmp_wrap (as_url.username, as_user)); \
71 mu_silent_assert ("should set the password attribute correctly", \
72 0 == strcmp_wrap (as_url.password, as_pass)); \
73 mu_silent_assert ("should set the host attribute correctly", \
74 0 == strcmp_wrap (as_url.host, as_host)); \
75 mu_silent_assert ("should set the port attribute correctly", \
76 as_port == as_url.port); \
77 mu_silent_assert ("should set the path attribute correctly", \
78 0 == strcmp_wrap (as_url.path, as_path)); \
79 mu_silent_assert ("should set the query attribute correctly", \
80 0 == strcmp_wrap (as_url.query, as_query)); \
81 mu_silent_assert ("should set the fragment attribute correctly", \
82 0 == strcmp_wrap (as_url.fragment, as_fragment));
83
84static char *
85test_parse_http_url_ok (void)
86{
87 int rc;
88 struct GNUNET_Uri url;
89 char *url_string;
90
91 /* Minimal URL */
92 url_string = strdup ("http://example.com");
93 rc = GNUNET_uri_parse (&url,
94 url_string);
95 mu_assert ("minimal HTTP URL", -1 != rc);
96 assert_struct (url,
97 "http",
98 NULL,
99 NULL,
100 "example.com",
101 0,
102 NULL,
103 NULL,
104 NULL);
105 free (url_string);
106
107 /* With path (/) */
108 url_string = strdup ("http://example.com/");
109 rc = GNUNET_uri_parse (&url,
110 url_string);
111 mu_assert ("with path ('/')", -1 != rc);
112 assert_struct (url,
113 "http",
114 NULL,
115 NULL,
116 "example.com",
117 0,
118 "",
119 NULL,
120 NULL);
121 free (url_string);
122
123 /* With path */
124 url_string = strdup ("http://example.com/path");
125 rc = GNUNET_uri_parse (&url,
126 url_string);
127 mu_assert ("with path ('/path')", -1 != rc);
128 assert_struct (url,
129 "http",
130 NULL,
131 NULL,
132 "example.com",
133 0,
134 "path",
135 NULL,
136 NULL);
137 free (url_string);
138
139 /* With port */
140 url_string = strdup ("http://example.com:80");
141 rc = GNUNET_uri_parse (&url,
142 url_string);
143 mu_assert ("with port only",
144 -1 != rc);
145 assert_struct (url,
146 "http",
147 NULL,
148 NULL,
149 "example.com",
150 80,
151 NULL,
152 NULL,
153 NULL);
154 free (url_string);
155
156 /* With query */
157 url_string = strdup ("http://example.com?query=only");
158 rc = GNUNET_uri_parse (&url,
159 url_string);
160 mu_assert ("with query only",
161 -1 != rc);
162 assert_struct (url,
163 "http",
164 NULL,
165 NULL,
166 "example.com",
167 0,
168 NULL,
169 "query=only",
170 NULL);
171 free (url_string);
172
173 /* With fragment */
174 url_string = strdup ("http://example.com#frag=f1");
175 rc = GNUNET_uri_parse (&url,
176 url_string);
177 mu_assert ("with fragment only",
178 -1 != rc);
179 assert_struct (url,
180 "http",
181 NULL,
182 NULL,
183 "example.com",
184 0,
185 NULL,
186 NULL,
187 "frag=f1");
188 free (url_string);
189
190 /* With credentials */
191 url_string = strdup ("http://u:p@example.com");
192 rc = GNUNET_uri_parse (&url,
193 url_string);
194 mu_assert ("with credentials only",
195 -1 != rc);
196 assert_struct (url,
197 "http",
198 "u",
199 "p",
200 "example.com",
201 0,
202 NULL,
203 NULL,
204 NULL);
205 free (url_string);
206
207 /* With port and path */
208 url_string = strdup ("http://example.com:8080/port/and/path");
209 rc = GNUNET_uri_parse (&url,
210 url_string);
211 mu_assert ("with port and path",
212 -1 != rc);
213 assert_struct (url,
214 "http",
215 NULL,
216 NULL,
217 "example.com",
218 8080,
219 "port/and/path",
220 NULL,
221 NULL);
222 free (url_string);
223
224 /* With port and query */
225 url_string = strdup ("http://example.com:8080?query=portANDquery");
226 rc = GNUNET_uri_parse (&url,
227 url_string);
228 mu_assert ("with port and query",
229 -1 != rc);
230 assert_struct (url,
231 "http",
232 NULL,
233 NULL,
234 "example.com",
235 8080,
236 NULL,
237 "query=portANDquery",
238 NULL);
239 free (url_string);
240
241 /* With port and fragment */
242 url_string = strdup ("http://example.com:8080#f1");
243 rc = GNUNET_uri_parse (&url,
244 url_string);
245 mu_assert ("with port and fragment",
246 -1 != rc);
247 assert_struct (url,
248 "http",
249 NULL,
250 NULL,
251 "example.com",
252 8080,
253 NULL,
254 NULL,
255 "f1");
256 free (url_string);
257
258 /* With port and credentials */
259 url_string = strdup ("http://u:p@example.com:8080");
260 rc = GNUNET_uri_parse (&url,
261 url_string);
262 mu_assert ("with port and credentials",
263 -1 != rc);
264 assert_struct (url,
265 "http",
266 "u",
267 "p",
268 "example.com",
269 8080,
270 NULL,
271 NULL,
272 NULL);
273 free (url_string);
274
275 /* With path and query */
276 url_string = strdup ("http://example.com/path/and/query?q=yes");
277 rc = GNUNET_uri_parse (&url,
278 url_string);
279 mu_assert ("with path and query",
280 -1 != rc);
281 assert_struct (url,
282 "http",
283 NULL,
284 NULL,
285 "example.com",
286 0,
287 "path/and/query",
288 "q=yes",
289 NULL);
290 free (url_string);
291
292 /* With path and fragment */
293 url_string = strdup ("http://example.com/path/and#fragment");
294 rc = GNUNET_uri_parse (&url,
295 url_string);
296 mu_assert ("with path and fragment",
297 -1 != rc);
298 assert_struct (url,
299 "http",
300 NULL,
301 NULL,
302 "example.com",
303 0,
304 "path/and",
305 NULL,
306 "fragment");
307 free (url_string);
308
309 /* With query and fragment */
310 url_string = strdup ("http://example.com?q=yes#f1");
311 rc = GNUNET_uri_parse (&url,
312 url_string);
313 mu_assert ("with query and fragment",
314 -1 != rc);
315 assert_struct (url,
316 "http",
317 NULL,
318 NULL,
319 "example.com",
320 0,
321 NULL,
322 "q=yes",
323 "f1");
324 free (url_string);
325
326 /* With query and credentials */
327 url_string = strdup ("http://u:p@example.com?q=yes");
328 rc = GNUNET_uri_parse (&url,
329 url_string);
330 mu_assert ("with query and credentials",
331 -1 != rc);
332 assert_struct (url,
333 "http",
334 "u",
335 "p",
336 "example.com",
337 0,
338 NULL,
339 "q=yes",
340 NULL);
341 free (url_string);
342
343 /* With empty credentials */
344 url_string = strdup ("http://:@example.com");
345 rc = GNUNET_uri_parse (&url,
346 url_string);
347 mu_assert ("with empty credentials",
348 -1 != rc);
349 assert_struct (url,
350 "http",
351 "",
352 "",
353 "example.com",
354 0,
355 NULL,
356 NULL,
357 NULL);
358 free (url_string);
359
360 /* With empty credentials and port */
361 url_string = strdup ("http://:@example.com:89");
362 rc = GNUNET_uri_parse (&url,
363 url_string);
364 mu_assert ("with empty credentials and port",
365 -1 != rc);
366 assert_struct (url,
367 "http",
368 "",
369 "",
370 "example.com",
371 89,
372 NULL,
373 NULL,
374 NULL);
375 free (url_string);
376
377 /* Full URL */
378 url_string = strdup ("https://jack:password@localhost:8989/path/to/test?query=yes&q=jack#fragment1");
379 rc = GNUNET_uri_parse (&url,
380 url_string);
381 mu_assert ("with port, path and query",
382 -1 != rc);
383 assert_struct (url,
384 "https",
385 "jack",
386 "password",
387 "localhost",
388 8989,
389 "path/to/test",
390 "query=yes&q=jack",
391 "fragment1");
392 free (url_string);
393
394 return NULL;
395}
396
397static char *
398test_parse_http_rel_url_ok (void)
399{
400 int rc;
401 struct GNUNET_Uri url;
402 char *url_string;
403
404 /* Minimal relative URL */
405 url_string = strdup ("/");
406 rc = GNUNET_uri_parse (&url,
407 url_string);
408 mu_assert ("minimal relative URL",
409 -1 != rc);
410 assert_struct (url,
411 NULL,
412 NULL,
413 NULL,
414 NULL,
415 0,
416 "",
417 NULL,
418 NULL);
419 free (url_string);
420
421 /* Path only */
422 url_string = strdup ("/hejsan");
423 rc = GNUNET_uri_parse (&url,
424 url_string);
425 mu_assert ("path only",
426 -1 != rc);
427 assert_struct (url,
428 NULL,
429 NULL,
430 NULL,
431 NULL,
432 0,
433 "hejsan",
434 NULL,
435 NULL);
436 free (url_string);
437
438 /* Path and query */
439 url_string = strdup ("/hejsan?q=yes");
440 rc = GNUNET_uri_parse (&url,
441 url_string);
442 mu_assert ("path only",
443 -1 != rc);
444 assert_struct (url,
445 NULL,
446 NULL,
447 NULL,
448 NULL,
449 0,
450 "hejsan",
451 "q=yes",
452 NULL);
453 free (url_string);
454
455 /* Path and fragment */
456 url_string = strdup ("/hejsan#fragment");
457 rc = GNUNET_uri_parse (&url,
458 url_string);
459 mu_assert ("path and fragment",
460 -1 != rc);
461 assert_struct (url,
462 NULL,
463 NULL,
464 NULL,
465 NULL,
466 0,
467 "hejsan",
468 NULL,
469 "fragment");
470 free (url_string);
471
472 /* Path, query and fragment */
473 url_string = strdup ("/?q=yes&q2=no#fragment");
474 rc = GNUNET_uri_parse (&url,
475 url_string);
476 mu_assert ("path, query and fragment",
477 -1 != rc);
478 assert_struct (url,
479 NULL,
480 NULL,
481 NULL,
482 NULL,
483 0,
484 "",
485 "q=yes&q2=no",
486 "fragment");
487 free (url_string);
488
489 return NULL;
490}
491
492static char *
493test_parse_url_fail (void)
494{
495 int rc;
496 struct GNUNET_Uri url;
497 char *url_string;
498
499 /* Empty */
500 url_string = strdup ("");
501 rc = GNUNET_uri_parse (&url,
502 url_string);
503 mu_assert ("empty string should return -1",
504 -1 == rc);
505 free (url_string);
506
507 /* Scheme only */
508 url_string = strdup ("rtsp://");
509 rc = GNUNET_uri_parse (&url,
510 url_string);
511 mu_assert ("scheme only should return -1",
512 -1 == rc);
513 free (url_string);
514
515 /* Hostname only */
516 url_string = strdup ("hostname");
517 rc = GNUNET_uri_parse (&url,
518 url_string);
519 mu_assert ("hostname only should return -1",
520 -1 == rc);
521 free (url_string);
522
523 /* Query only */
524 url_string = strdup ("?query=only");
525 rc = GNUNET_uri_parse (&url,
526 url_string);
527 mu_assert ("query only should return -1",
528 -1 == rc);
529 free (url_string);
530
531 /* Missing scheme */
532 url_string = strdup ("://");
533 rc = GNUNET_uri_parse (&url,
534 url_string);
535 mu_assert ("missing scheme should return -1",
536 -1 == rc);
537 free (url_string);
538
539 /* Missing hostname */
540 url_string = strdup ("rtsp://:8910/path");
541 rc = GNUNET_uri_parse (&url,
542 url_string);
543 mu_assert ("missing hostname should return -1",
544 -1 == rc);
545 free (url_string);
546
547 /* Missing credentials */
548 url_string = strdup ("rtsp://@hostname:8910/path");
549 rc = GNUNET_uri_parse (&url,
550 url_string);
551 mu_assert ("missing credentials should return -1",
552 -1 == rc);
553 free (url_string);
554
555 return NULL;
556}
557
558static char *
559test_split_path_ok (void)
560{
561 int rc;
562 char *path;
563 char *parts[10];
564
565 /* Simple path */
566 path = strdup ("/this/is/a/path");
567 rc = GNUNET_uri_split_path (path,
568 parts,
569 10);
570 mu_assert ("should be able to parse a regular path",
571 4 == rc);
572 mu_silent_assert ("first part should be 'this'",
573 0 == strcmp ("this", parts[0]));
574 mu_silent_assert ("second part should be 'is'",
575 0 == strcmp ("is", parts[1]));
576 mu_silent_assert ("third part should be 'a'",
577 0 == strcmp ("a", parts[2]));
578 mu_silent_assert ("fourth part should be 'path'",
579 0 == strcmp ("path", parts[3]));
580 free (path);
581
582 /* Relative path */
583 path = strdup ("this/is/a/path");
584 rc = GNUNET_uri_split_path (path,
585 parts,
586 10);
587 mu_assert ("should be able to parse a relative path",
588 4 == rc);
589 mu_silent_assert ("first part should be 'this'",
590 0 == strcmp ("this", parts[0]));
591 mu_silent_assert ("second part should be 'is'",
592 0 == strcmp ("is", parts[1]));
593 mu_silent_assert ("third part should be 'a'",
594 0 == strcmp ("a", parts[2]));
595 mu_silent_assert ("fourth part should be 'path'",
596 0 == strcmp ("path", parts[3]));
597 free (path);
598
599 /* Path with empty parts */
600 path = strdup ("//this//is/a/path/");
601 rc = GNUNET_uri_split_path (path,
602 parts,
603 10);
604 mu_assert ("should treat multiple slashes as one",
605 4 == rc);
606 mu_silent_assert ("first part should be 'this'",
607 0 == strcmp("this", parts[0]));
608 mu_silent_assert ("second part should be 'is'",
609 0 == strcmp("is", parts[1]));
610 mu_silent_assert ("third part should be 'a'",
611 0 == strcmp("a", parts[2]));
612 mu_silent_assert ("fourth part should be 'path'",
613 0 == strcmp("path", parts[3]));
614 free (path);
615
616 /* Just one level */
617 path = strdup("/one_level");
618 rc = GNUNET_uri_split_path(path, parts, 10);
619 mu_assert("should be able to parse a path with one level", 1 == rc);
620 mu_silent_assert("first part should be 'this'", 0 == strcmp("one_level", parts[0]));
621 free(path);
622
623 return NULL;
624}
625
626static char *
627test_parse_query_ok (void)
628{
629 int rc;
630 char *q;
631 struct GNUNET_UriParam params[10];
632
633 /* One param query */
634 q = strdup ("q=yes");
635 rc = GNUNET_uri_parse_query (q,
636 '&',
637 params,
638 10);
639 mu_assert ("single parameter with value",
640 1 == rc);
641 mu_silent_assert ("first param key should be 'q'",
642 0 == strcmp ("q", params[0].key));
643 mu_silent_assert ("first param val should be 'yes'",
644 0 == strcmp ("yes", params[0].val));
645 free (q);
646
647 /* One param query without value */
648 q = strdup ("q");
649 rc = GNUNET_uri_parse_query (q,
650 '&',
651 params,
652 10);
653 mu_assert ("single parameter without value",
654 1 == rc);
655 mu_silent_assert ("first param key should be 'q'",
656 0 == strcmp ("q", params[0].key));
657 mu_silent_assert ("first param val should be NULL",
658 NULL == params[0].val);
659 free (q);
660
661 /* Two param query */
662 q = strdup ("query=yes&a1=hello");
663 rc = GNUNET_uri_parse_query (q,
664 '&',
665 params,
666 10);
667 mu_assert ("multiple params with value",
668 2 == rc);
669 mu_silent_assert ("first param key should be 'query'",
670 0 == strcmp ("query", params[0].key));
671 mu_silent_assert ("first param val should be 'yes'",
672 0 == strcmp ("yes", params[0].val));
673 mu_silent_assert ("second param key should be 'a1'",
674 0 == strcmp ("a1", params[1].key));
675 mu_silent_assert ("second param val should be 'hello'",
676 0 == strcmp ("hello", params[1].val));
677 free (q);
678
679 /* Two param query, one without value */
680 q = strdup ("query=yes&forceHttps");
681 rc = GNUNET_uri_parse_query (q,
682 '&',
683 params,
684 10);
685 mu_assert ("multiple params one without value",
686 2 == rc);
687 mu_silent_assert ("first param key should be 'query'",
688 0 == strcmp ("query", params[0].key));
689 mu_silent_assert ("first param val should be 'yes'",
690 0 == strcmp ("yes", params[0].val));
691 mu_silent_assert ("second param key should be 'forceHttps'",
692 0 == strcmp ("forceHttps", params[1].key));
693 mu_silent_assert ("second param val should be NULL",
694 NULL == params[1].val);
695 free (q);
696
697 /* Three param query, all without value */
698 q = strdup ("query&forceHttps&log");
699 rc = GNUNET_uri_parse_query (q,
700 '&',
701 params,
702 10);
703 mu_assert ("multiple params all without value",
704 3 == rc);
705 mu_silent_assert ("first param key should be 'query'",
706 0 == strcmp ("query", params[0].key));
707 mu_silent_assert ("first param val should be NULL",
708 NULL == params[0].val);
709 mu_silent_assert ("second param key should be 'forceHttps'",
710 0 == strcmp ("forceHttps", params[1].key));
711 mu_silent_assert ("second param val should be NULL",
712 NULL == params[1].val);
713 mu_silent_assert ("third param key should be 'log'",
714 0 == strcmp ("log", params[2].key));
715 mu_silent_assert ("third param val should be NULL",
716 NULL == params[2].val);
717 free (q);
718
719 /* Param with empty value */
720 q = strdup ("param=&query=no");
721 rc = GNUNET_uri_parse_query (q,
722 '&',
723 params,
724 10);
725 mu_assert ("param with empty value",
726 2 == rc);
727 mu_silent_assert ("first param key should be 'param'",
728 0 == strcmp ("param", params[0].key));
729 mu_silent_assert ("first param val should be ''",
730 0 == strcmp ("", params[0].val));
731 mu_silent_assert ("second param key should be 'query'",
732 0 == strcmp ("query", params[1].key));
733 mu_silent_assert ("second param val should be 'no'",
734 0 == strcmp ("no", params[1].val));
735 free (q);
736
737 /* Double delimiter */
738 q = strdup ("param=jack&&query=no");
739 rc = GNUNET_uri_parse_query (q,
740 '&',
741 params,
742 10);
743 mu_assert ("double delimiter",
744 3 == rc);
745 mu_silent_assert ("first param key should be 'param'",
746 0 == strcmp ("param", params[0].key));
747 mu_silent_assert ("first param val should be 'jack'",
748 0 == strcmp ("jack", params[0].val));
749 mu_silent_assert ("second param key should be ''",
750 0 == strcmp ("", params[1].key));
751 mu_silent_assert ("second param val should be NULL",
752 NULL == params[1].val);
753 mu_silent_assert ("third param key should be 'query'",
754 0 == strcmp ("query", params[2].key));
755 mu_silent_assert ("third param val should be 'no'",
756 0 == strcmp ("no", params[2].val));
757 free (q);
758
759 /* Delimiter in beginning */
760 q = strdup ("&param=jack&query=no");
761 rc = GNUNET_uri_parse_query (q,
762 '&',
763 params,
764 10);
765 mu_assert ("delimiter in beginning",
766 3 == rc);
767 mu_silent_assert ("first param key should be ''",
768 0 == strcmp ("", params[0].key));
769 mu_silent_assert ("first param val should be NULL",
770 NULL == params[0].val);
771 mu_silent_assert ("second param key should be 'param'",
772 0 == strcmp ("param", params[1].key));
773 mu_silent_assert ("second param val should be 'jack'",
774 0 == strcmp ("jack", params[1].val));
775 mu_silent_assert ("third param key should be 'query'",
776 0 == strcmp ("query", params[2].key));
777 mu_silent_assert ("third param val should be 'no'",
778 0 == strcmp ("no", params[2].val));
779 free (q);
780
781 /* Delimiter at the end */
782 q = strdup ("param=jack&query=no&");
783 rc = GNUNET_uri_parse_query (q,
784 '&',
785 params,
786 10);
787 mu_assert ("delimiter at the end",
788 3 == rc);
789 mu_silent_assert ("first param key should be 'param'",
790 0 == strcmp ("param", params[0].key));
791 mu_silent_assert ("first param val should be 'jack'",
792 0 == strcmp ("jack", params[0].val));
793 mu_silent_assert ("second param key should be 'query'",
794 0 == strcmp ("query", params[1].key));
795 mu_silent_assert ("second param val should be 'no'",
796 0 == strcmp ("no", params[1].val));
797 mu_silent_assert ("third param key should be ''",
798 0 == strcmp ("", params[2].key));
799 mu_silent_assert ("third param val should be NULL",
800 NULL == params[2].val);
801 free (q);
802
803 return NULL;
804}
805
806static char *
807all_tests (void)
808{
809 mu_group ("GNUNET_uri_parse () with an HTTP URL");
810 mu_run_test (test_parse_http_url_ok);
811
812 mu_group ("GNUNET_uri_parse () with an relative URL");
813 mu_run_test (test_parse_http_rel_url_ok);
814
815 mu_group ("GNUNET_uri_parse () with faulty values");
816 mu_run_test (test_parse_url_fail);
817
818 mu_group ("GNUNET_uri_split_path ()");
819 mu_run_test (test_split_path_ok);
820
821 mu_group ("GNUNET_uri_parse_query ()");
822 mu_run_test (test_parse_query_ok);
823
824 return NULL;
825}
826
827int
828main (void)
829{
830 char *result;
831
832 result = all_tests ();
833 if (result != NULL) {
834 exit (EXIT_FAILURE);
835 }
836
837 exit (EXIT_SUCCESS);
838}