tests: fix tests when compiling with glib_checks=disabled
[platform/upstream/gstreamer.git] / tests / check / gst / gsturi.c
1 /* GStreamer unit tests for GstURI
2  *
3  * Copyright (C) 2007 Tim-Philipp Müller <tim centricular net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #ifndef GST_REMOVE_DEPRECATED
25 #undef GST_DISABLE_DEPRECATED
26 #endif
27
28 #include <gst/check/gstcheck.h>
29
30 GST_START_TEST (test_protocol_case)
31 {
32   GstElement *element;
33   GError *err = NULL;
34
35   element = gst_element_make_from_uri (GST_URI_SRC, "file:///foo/bar", NULL,
36       &err);
37
38   /* no element? probably no registry, bail out */
39   if (element == NULL && err->code == GST_URI_ERROR_UNSUPPORTED_PROTOCOL) {
40     g_error_free (err);
41     return;
42   }
43
44   gst_object_unref (element);
45   element = gst_element_make_from_uri (GST_URI_SRC, "FILE:///foo/bar", NULL,
46       NULL);
47   fail_unless (element != NULL,
48       "Got source for 'file://' URI but not for 'FILE://' URI");
49   gst_object_unref (element);
50 }
51
52 GST_END_TEST;
53
54 GST_START_TEST (test_uri_get_location)
55 {
56   gchar *l;
57
58   /* URI with no location should return empty string */
59   l = gst_uri_get_location ("dvd://");
60   fail_unless (l != NULL);
61   fail_unless_equals_string (l, "");
62   g_free (l);
63
64   /* URI with hostname */
65   l = gst_uri_get_location ("smb://supercomputer/path/to/file");
66   fail_unless (l != NULL);
67   fail_unless_equals_string (l, "supercomputer/path/to/file");
68   g_free (l);
69
70   /* URI */
71   l = gst_uri_get_location ("file:///path/to/file");
72   fail_unless (l != NULL);
73   fail_unless_equals_string (l, "/path/to/file");
74   g_free (l);
75
76   /* unescaping */
77   l = gst_uri_get_location ("file:///path/to/some%20file");
78   fail_unless (l != NULL);
79   fail_unless_equals_string (l, "/path/to/some file");
80   g_free (l);
81 }
82
83 GST_END_TEST;
84
85 #ifndef GST_REMOVE_DEPRECATED
86 GST_START_TEST (test_gst_uri_construct)
87 {
88   gchar *l = NULL;
89
90   /* URI with no protocol or empty protocol should return empty string */
91   ASSERT_CRITICAL (l = gst_uri_construct (NULL, "/path/to/file"));
92   fail_unless (l == NULL);
93   ASSERT_CRITICAL (l = gst_uri_construct ("", "/path/to/file"));
94   fail_unless (l == NULL);
95
96   /* URI with no location should return empty string */
97   ASSERT_CRITICAL (l = gst_uri_construct ("protocol", NULL));
98   fail_unless (l == NULL);
99
100   /* check the protocol for validity */
101   l = gst_uri_construct ("protocol1234567890+-.", "somefile");
102   fail_unless (l != NULL);
103   fail_unless_equals_string (l, "protocol1234567890+-.://somefile");
104   g_free (l);
105
106   /* check the location for correct handling */
107   l = gst_uri_construct ("aprotocol",
108       "/path+ to/some/file%d?akey=aval&key2=val2");
109   fail_unless (l != NULL);
110   fail_unless_equals_string (l,
111       "aprotocol:///path%2B%20to/some/file%25d?akey=aval&key2=val2");
112   g_free (l);
113 }
114
115 GST_END_TEST;
116 #endif
117
118 #ifdef G_OS_WIN32
119
120 GST_START_TEST (test_win32_uri)
121 {
122   gchar *uri, *l;
123
124   uri = g_strdup ("file:///c:/my%20music/foo.ogg");
125   l = gst_uri_get_location (uri);
126   fail_unless (l != NULL);
127   /* fail_unless_equals_string will screw up here in the failure case
128    * because the string constant will be appended to the printf format
129    * message string and contains a '%', that's why we use fail_unless here */
130   fail_unless (g_str_equal (l, "c:/my music/foo.ogg"),
131       "wrong location '%s' returned for URI '%s'", l, uri);
132   g_free (l);
133   g_free (uri);
134
135   /* make sure the other variant with two slashes before the C: (which was
136    * needed before because of a bug in _get_location()) still works */
137   uri = g_strdup ("file://c:/my%20music/foo.ogg");
138   l = gst_uri_get_location (uri);
139   fail_unless (l != NULL);
140   /* fail_unless_equals_string will screw up here in the failure case
141    * because the string constant will be appended to the printf format
142    * message string and contains a '%', that's why we use fail_unless here */
143   fail_unless (g_str_equal (l, "c:/my music/foo.ogg"),
144       "wrong location '%s' returned for URI '%s'", l, uri);
145   g_free (l);
146   g_free (uri);
147 }
148
149 GST_END_TEST;
150
151 #endif /* G_OS_WIN32 */
152
153 GST_START_TEST (test_uri_misc)
154 {
155   /* require at least two characters for the protocol */
156   fail_if (gst_uri_is_valid ("B:\\foo.txt"));
157   fail_if (gst_uri_is_valid ("B:/foo.txt"));
158   fail_if (gst_uri_is_valid ("B://foo.txt"));
159   fail_if (gst_uri_is_valid ("B:foo.txt"));
160
161   fail_unless (gst_uri_is_valid ("fd://0"));
162   fail_unless (gst_uri_is_valid ("AB:\\foo.txt"));
163   fail_unless (gst_uri_is_valid ("AB:/foo.txt"));
164   fail_unless (gst_uri_is_valid ("AB://foo.txt"));
165   fail_unless (gst_uri_is_valid ("AB:foo.txt"));
166
167   fail_unless (gst_uri_is_valid ("ABC:/foo.txt"));
168   fail_unless (gst_uri_is_valid ("ABC://foo.txt"));
169   fail_unless (gst_uri_is_valid ("ABC:foo.txt"));
170
171   fail_unless (gst_uri_is_valid ("ABCD:/foo.txt"));
172   fail_unless (gst_uri_is_valid ("ABCD://foo.txt"));
173   fail_unless (gst_uri_is_valid ("ABCD:foo.txt"));
174 }
175
176 GST_END_TEST;
177
178 GST_START_TEST (test_element_make_from_uri)
179 {
180   GstElement *element;
181   GError *err = NULL;
182
183   element = gst_element_make_from_uri (GST_URI_SRC, "foo://", NULL, NULL);
184   fail_unless (element == NULL);
185
186   element = gst_element_make_from_uri (GST_URI_SRC, "foo://", NULL, &err);
187   fail_unless (element == NULL);
188   fail_unless (err != NULL);
189   fail_unless (err->code == GST_URI_ERROR_UNSUPPORTED_PROTOCOL);
190   g_error_free (err);
191   err = NULL;
192
193   if (gst_registry_check_feature_version (gst_registry_get (), "filesrc",
194           GST_VERSION_MAJOR, GST_VERSION_MINOR, GST_VERSION_MICRO)) {
195     element = gst_element_make_from_uri (GST_URI_SRC, "file://host/foo", NULL,
196         &err);
197     fail_unless (element == NULL);
198     fail_unless (err != NULL);
199     fail_unless (err->code == GST_URI_ERROR_BAD_URI);
200     g_error_free (err);
201     err = NULL;
202   }
203 }
204
205 GST_END_TEST;
206
207 /* Taken from the GNet unit test and extended with other URIs:
208  * https://git.gnome.org/browse/archive/gnet/plain/tests/check/gnet/gneturi.c
209  */
210 struct QueryValue
211 {
212   const gchar *key;
213   const gchar *value;
214 };
215
216 struct URITest
217 {
218   const gchar *str;
219   struct
220   {
221     const gchar *scheme;
222     const gchar *userinfo;
223     const gchar *host;
224     gint port;
225     const gchar *path;
226     /* needs to be updated if more than 10 */
227     struct QueryValue query[10];
228     const gchar *fragment;
229   } uri;
230 };
231
232 static const struct URITest tests[] = {
233   /* VALID URIS.  PARSING AND PRINTING OF THESE SHOULD NOT CHANGE */
234
235   /* scheme/path */
236   {"scheme:",
237       {"scheme", NULL, NULL, GST_URI_NO_PORT, NULL, {{NULL, NULL}}, NULL}},
238
239   {"scheme:path",
240       {"scheme", NULL, NULL, GST_URI_NO_PORT, "path", {{NULL, NULL}}, NULL}},
241
242   {"path",
243       {NULL, NULL, NULL, GST_URI_NO_PORT, "path", {{NULL, NULL}}, NULL}},
244
245   {"/path",
246       {NULL, NULL, NULL, GST_URI_NO_PORT, "/path", {{NULL, NULL}}, NULL}},
247
248   /* hostname/port */
249   {"scheme://hostname/path",
250         {"scheme", NULL, "hostname", GST_URI_NO_PORT, "/path", {{NULL, NULL}},
251           NULL}},
252
253   {"scheme://hostname:123/path",
254       {"scheme", NULL, "hostname", 123, "/path", {{NULL, NULL}}, NULL}},
255
256   /* ipv6 hostname/port */
257   {"scheme://[01:23:45:67:89:ab:cd:ef]/path",
258         {"scheme", NULL, "01:23:45:67:89:ab:cd:ef", GST_URI_NO_PORT, "/path",
259           {{NULL, NULL}}, NULL}},
260
261   {"scheme://[01:23:45:67:89:ab:cd:ef]:123/path",
262         {"scheme", NULL, "01:23:45:67:89:ab:cd:ef", 123, "/path", {{NULL,
263                   NULL}}, NULL}},
264
265   /* query/fragment */
266   {"path?query",
267         {NULL, NULL, NULL, GST_URI_NO_PORT, "path", {{"query", NULL}, {NULL,
268                   NULL}}, NULL}},
269   {"path?query=value",
270         {NULL, NULL, NULL, GST_URI_NO_PORT, "path", {{"query", "value"}, {NULL,
271                   NULL}}, NULL}},
272
273   {"path?query#fragment",
274         {NULL, NULL, NULL, GST_URI_NO_PORT, "path", {{"query", NULL}, {NULL,
275                   NULL}}, "fragment"}},
276
277   {"path?query=value#fragment",
278         {NULL, NULL, NULL, GST_URI_NO_PORT, "path", {{"query", "value"}, {NULL,
279                   NULL}}, "fragment"}},
280
281   {"scheme:path?query#fragment",
282         {"scheme", NULL, NULL, GST_URI_NO_PORT, "path", {{"query", NULL}, {NULL,
283                   NULL}}, "fragment"}},
284
285   /* full */
286   {"scheme://hostname:123/path?query#fragment",
287         {"scheme", NULL, "hostname", 123, "/path", {{"query", NULL}, {NULL,
288                   NULL}}, "fragment"}},
289
290   {"scheme://hostname:123/path?query=value#fragment",
291         {"scheme", NULL, "hostname", 123, "/path", {{"query", "value"}, {NULL,
292                   NULL}}, "fragment"}},
293
294   {"scheme://hostname:123?query",
295         {"scheme", NULL, "hostname", 123, NULL, {{"query", NULL}, {NULL,
296                   NULL}}, NULL}},
297
298   {"scheme://hostname:123?query=value",
299         {"scheme", NULL, "hostname", 123, NULL, {{"query", "value"}, {NULL,
300                   NULL}}, NULL}},
301
302   {"scheme://hostname:123?query#fragment",
303         {"scheme", NULL, "hostname", 123, NULL, {{"query", NULL}, {NULL,
304                   NULL}}, "fragment"}},
305
306   {"scheme://hostname:123?query=value#fragment",
307         {"scheme", NULL, "hostname", 123, NULL, {{"query", "value"}, {NULL,
308                   NULL}}, "fragment"}},
309
310   /* user/pass */
311   {"scheme://userinfo@hostname",
312         {"scheme", "userinfo", "hostname", GST_URI_NO_PORT, NULL, {{NULL,
313                   NULL}}, NULL}},
314
315   {"scheme://userinfo@hostname:123/path?query#fragment",
316         {"scheme", "userinfo", "hostname", 123, "/path", {{"query", NULL},
317               {NULL, NULL}}, "fragment"}},
318
319   {"scheme://user:pass@hostname",
320         {"scheme", "user:pass", "hostname", GST_URI_NO_PORT, NULL, {{NULL,
321                   NULL}}, NULL}},
322
323   {"scheme://user:pass@hostname:123/path?query#fragment",
324         {"scheme", "user:pass", "hostname", 123, "/path", {{"query", NULL},
325               {NULL, NULL}}, "fragment"}},
326
327   /* FUNNY URIS.  PARSING AND PRINTING OF THESE MAY CHANGE */
328
329   {"scheme:hostname:123/path?query#fragment",
330         {"scheme", NULL, NULL, GST_URI_NO_PORT, "hostname:123/path", {{"query",
331                   NULL}, {NULL, NULL}}, "fragment"}},
332
333   {"scheme://:pass@hostname:123/path?query#fragment",
334         {"scheme", ":pass", "hostname", 123, "/path", {{"query", NULL}, {NULL,
335                   NULL}}, "fragment"}},
336
337   /* Skip initial white space */
338   {" \f\n\r\t\vscheme:",
339       {"scheme", NULL, NULL, GST_URI_NO_PORT, NULL, {{NULL, NULL}}, NULL}},
340
341   {" \f\n\r\t\vpath",
342       {NULL, NULL, NULL, GST_URI_NO_PORT, "path", {{NULL, NULL}}, NULL}},
343
344   /* file URI */
345   {"file://host/home/joe/foo.txt",
346         {"file", NULL, "host", GST_URI_NO_PORT, "/home/joe/foo.txt", {{NULL,
347                   NULL}}, NULL}},
348   {"file:///home/joe/foo.txt",
349         {"file", NULL, NULL, GST_URI_NO_PORT, "/home/joe/foo.txt", {{NULL,
350                   NULL}}, NULL}},
351 };
352
353 static const gchar *unparsable_uri_tests[] = {
354   /* Path not started correctly */
355   "scheme://hostname:123path?query#fragment",
356
357   /* Brackets that don't close */
358   "scheme://[01:23:45:67:89:ab:cd:ef:123/path",
359
360   /* IPv6 hostname without brackets */
361   "scheme://01:23:45:67:89:ab:cd:ef:123/path",
362 };
363
364 GST_START_TEST (test_url_parsing)
365 {
366   GstUri *uri;
367   GList *list;
368   gchar *tmp_str;
369   guint i, j;
370
371   for (i = 0; i < G_N_ELEMENTS (tests); i++) {
372     GST_DEBUG ("Testing URI '%s'", tests[i].str);
373
374     uri = gst_uri_from_string (tests[i].str);
375     fail_unless (uri != NULL);
376     fail_unless_equals_string (gst_uri_get_scheme (uri), tests[i].uri.scheme);
377     fail_unless_equals_string (gst_uri_get_userinfo (uri),
378         tests[i].uri.userinfo);
379     fail_unless_equals_string (gst_uri_get_host (uri), tests[i].uri.host);
380     fail_unless_equals_int (gst_uri_get_port (uri), tests[i].uri.port);
381     tmp_str = gst_uri_get_path (uri);
382     fail_unless_equals_string (tmp_str, tests[i].uri.path);
383     g_free (tmp_str);
384
385     for (j = 0; j < 10; j++) {
386       if (!tests[i].uri.query[j].key)
387         break;
388
389       if (tests[i].uri.query[j].value) {
390         fail_unless_equals_string (gst_uri_get_query_value (uri,
391                 tests[i].uri.query[j].key), tests[i].uri.query[j].value);
392       } else {
393         fail_unless (gst_uri_query_has_key (uri, tests[i].uri.query[j].key));
394       }
395     }
396     list = gst_uri_get_query_keys (uri);
397     fail_unless_equals_int (j, g_list_length (list));
398     g_list_free (list);
399     gst_uri_unref (uri);
400   }
401
402   for (i = 0; i < G_N_ELEMENTS (unparsable_uri_tests); i++) {
403     GST_DEBUG ("Testing unparsable URI '%s'", unparsable_uri_tests[i]);
404
405     uri = gst_uri_from_string (unparsable_uri_tests[i]);
406     fail_unless (uri == NULL);
407   }
408 }
409
410 GST_END_TEST;
411
412 static const struct URITest url_presenting_tests[] = {
413   /* check all URI elements present */
414   {.uri = {"scheme", "user:pass", "host", 1234, "/path/to/dir",
415           {{"query", NULL}, {"key", "value"}}, "fragment"},
416       .str =
417 #if GLIB_CHECK_VERSION(2, 59, 0)
418       "scheme://user:pass@host:1234/path/to/dir?key=value&query#fragment"},
419 #else
420       "scheme://user:pass@host:1234/path/to/dir?query&key=value#fragment"},
421 #endif
422
423   /* IPv6 literal should render in square brackets */
424   {.uri = {"scheme", "user:pass", "12:34:56:78:9a:bc:de:f0", 1234,
425           "/path/to/dir", {{"query", "value"}}, "fragment"},
426       .str =
427       "scheme://user:pass@[12:34:56:78:9a:bc:de:f0]:1234/path/to/dir?query=value#fragment"},
428 };
429
430 GST_START_TEST (test_url_presenting)
431 {
432   GstUri *uri;
433   gchar *result;
434   guint i, j;
435
436   for (i = 0; i < G_N_ELEMENTS (url_presenting_tests); i++) {
437     uri = gst_uri_new (url_presenting_tests[i].uri.scheme,
438         url_presenting_tests[i].uri.userinfo,
439         url_presenting_tests[i].uri.host,
440         url_presenting_tests[i].uri.port,
441         url_presenting_tests[i].uri.path,
442         NULL, url_presenting_tests[i].uri.fragment);
443     fail_unless (uri != NULL);
444     for (j = 0; j < 10; j++) {
445       if (!url_presenting_tests[i].uri.query[j].key)
446         break;
447
448       fail_unless (gst_uri_set_query_value (uri,
449               url_presenting_tests[i].uri.query[j].key,
450               url_presenting_tests[i].uri.query[j].value));
451     }
452
453     result = gst_uri_to_string (uri);
454     fail_unless_equals_string (result, url_presenting_tests[i].str);
455     g_free (result);
456     gst_uri_unref (uri);
457   }
458 }
459
460 GST_END_TEST;
461
462 GST_START_TEST (test_url_normalization)
463 {
464   GstUri *url;
465   gchar *tmp_str;
466
467   url =
468       gst_uri_from_string
469       ("ScHeMe://User:P%61ss@HOST.%63om:1234/path/./from/../to%7d/item%2dobj?qu%65ry=something#fr%61gment");
470   fail_unless (gst_uri_normalize (url));
471   fail_unless_equals_string (gst_uri_get_scheme (url), "scheme");
472   fail_unless_equals_string (gst_uri_get_userinfo (url), "User:Pass");
473   fail_unless_equals_string (gst_uri_get_host (url), "host.com");
474   tmp_str = gst_uri_get_path (url);
475   fail_unless_equals_string (tmp_str, "/path/to}/item-obj");
476   g_free (tmp_str);
477   fail_unless (gst_uri_query_has_key (url, "query"));
478   fail_unless_equals_string (gst_uri_get_query_value (url, "query"),
479       "something");
480   fail_unless_equals_string (gst_uri_get_fragment (url), "fragment");
481   gst_uri_unref (url);
482 }
483
484 GST_END_TEST;
485
486 GST_START_TEST (test_url_joining)
487 {
488   GstUri *base, *rel, *joined;
489   gchar *l;
490
491   base =
492       gst_uri_from_string
493       ("http://example.com/path/to/dir/filename.html#fragment");
494
495   /* test change of fragment only */
496   rel = gst_uri_from_string ("#new_frag");
497   joined = gst_uri_join (base, rel);
498   l = gst_uri_to_string (joined);
499   fail_unless_equals_string (l,
500       "http://example.com/path/to/dir/filename.html#new_frag");
501   g_free (l);
502   gst_uri_unref (joined);
503   gst_uri_unref (rel);
504
505   /* test addition of new query string */
506   rel = gst_uri_from_string ("?key=val");
507   joined = gst_uri_join (base, rel);
508   l = gst_uri_to_string (joined);
509   fail_unless_equals_string (l,
510       "http://example.com/path/to/dir/filename.html?key=val");
511   g_free (l);
512   gst_uri_unref (joined);
513   gst_uri_unref (rel);
514
515   /* test new base filename */
516   rel = gst_uri_from_string ("new_filename.xml");
517   joined = gst_uri_join (base, rel);
518   l = gst_uri_to_string (joined);
519   fail_unless_equals_string (l,
520       "http://example.com/path/to/dir/new_filename.xml");
521   g_free (l);
522   gst_uri_unref (joined);
523   gst_uri_unref (rel);
524
525   /* test relative file same directory */
526   rel = gst_uri_from_string ("./new_filename.xml");
527   joined = gst_uri_join (base, rel);
528   l = gst_uri_to_string (joined);
529   fail_unless_equals_string (l,
530       "http://example.com/path/to/dir/new_filename.xml");
531   g_free (l);
532   gst_uri_unref (joined);
533   gst_uri_unref (rel);
534
535   /* test relative file parent directory */
536   rel = gst_uri_from_string ("../new_filename.xml");
537   joined = gst_uri_join (base, rel);
538   l = gst_uri_to_string (joined);
539   fail_unless_equals_string (l, "http://example.com/path/to/new_filename.xml");
540   g_free (l);
541   gst_uri_unref (joined);
542   gst_uri_unref (rel);
543
544   /* test relative file grandparent directory */
545   rel = gst_uri_from_string ("../../new_filename.xml");
546   joined = gst_uri_join (base, rel);
547   l = gst_uri_to_string (joined);
548   fail_unless_equals_string (l, "http://example.com/path/new_filename.xml");
549   g_free (l);
550   gst_uri_unref (joined);
551   gst_uri_unref (rel);
552
553   /* test relative file root directory */
554   rel = gst_uri_from_string ("../../../new_filename.xml");
555   joined = gst_uri_join (base, rel);
556   l = gst_uri_to_string (joined);
557   fail_unless_equals_string (l, "http://example.com/new_filename.xml");
558   g_free (l);
559   gst_uri_unref (joined);
560   gst_uri_unref (rel);
561
562   /* test relative file beyond root directory */
563   rel = gst_uri_from_string ("../../../../new_filename.xml");
564   joined = gst_uri_join (base, rel);
565   l = gst_uri_to_string (joined);
566   fail_unless_equals_string (l, "http://example.com/new_filename.xml");
567   g_free (l);
568   gst_uri_unref (joined);
569   gst_uri_unref (rel);
570
571   /* test add subdirectory */
572   rel = gst_uri_from_string ("subdir/new_filename.xml");
573   joined = gst_uri_join (base, rel);
574   l = gst_uri_to_string (joined);
575   fail_unless_equals_string (l,
576       "http://example.com/path/to/dir/subdir/new_filename.xml");
577   g_free (l);
578   gst_uri_unref (joined);
579   gst_uri_unref (rel);
580
581   /* test change directory */
582   rel = gst_uri_from_string ("../subdir/new_filename.xml");
583   joined = gst_uri_join (base, rel);
584   l = gst_uri_to_string (joined);
585   fail_unless_equals_string (l,
586       "http://example.com/path/to/subdir/new_filename.xml");
587   g_free (l);
588   gst_uri_unref (joined);
589   gst_uri_unref (rel);
590
591   gst_uri_unref (base);
592
593   /* change base for path ending in directory */
594   base = gst_uri_from_string ("http://example.com/path/to/dir/");
595
596   /* test adding file to directory */
597   rel = gst_uri_from_string ("new_filename.xml");
598   joined = gst_uri_join (base, rel);
599   l = gst_uri_to_string (joined);
600   fail_unless_equals_string (l,
601       "http://example.com/path/to/dir/new_filename.xml");
602   g_free (l);
603   gst_uri_unref (joined);
604   gst_uri_unref (rel);
605
606   /* test adding file to directory using relative path */
607   rel = gst_uri_from_string ("./new_filename.xml");
608   joined = gst_uri_join (base, rel);
609   l = gst_uri_to_string (joined);
610   fail_unless_equals_string (l,
611       "http://example.com/path/to/dir/new_filename.xml");
612   g_free (l);
613   gst_uri_unref (joined);
614   gst_uri_unref (rel);
615
616   /* test filename in parent directory */
617   rel = gst_uri_from_string ("../new_filename.xml");
618   joined = gst_uri_join (base, rel);
619   l = gst_uri_to_string (joined);
620   fail_unless_equals_string (l, "http://example.com/path/to/new_filename.xml");
621   g_free (l);
622   gst_uri_unref (joined);
623   gst_uri_unref (rel);
624
625   /* test path ending in '../' */
626   rel = gst_uri_from_string ("one/two/../");
627   joined = gst_uri_join (base, rel);
628   l = gst_uri_to_string (joined);
629   fail_unless_equals_string (l, "http://example.com/path/to/dir/one/");
630   g_free (l);
631   gst_uri_unref (joined);
632   gst_uri_unref (rel);
633
634   /* test path ending in '..' Result should be the same as when ending in '../' */
635   rel = gst_uri_from_string ("one/two/..");
636   joined = gst_uri_join (base, rel);
637   l = gst_uri_to_string (joined);
638   fail_unless_equals_string (l, "http://example.com/path/to/dir/one/");
639   g_free (l);
640   gst_uri_unref (joined);
641   gst_uri_unref (rel);
642
643   /* test replace with absolute */
644   rel = gst_uri_from_string ("https://ssl.example.com/new_filename.xml");
645   joined = gst_uri_join (base, rel);
646   l = gst_uri_to_string (joined);
647   fail_unless_equals_string (l, "https://ssl.example.com/new_filename.xml");
648   g_free (l);
649   gst_uri_unref (joined);
650   gst_uri_unref (rel);
651
652   gst_uri_unref (base);
653 }
654
655 GST_END_TEST;
656
657 GST_START_TEST (test_url_equality)
658 {
659   GstUri *url1, *url2;
660
661   url1 =
662       gst_uri_from_string
663       ("ScHeMe://User:Pass@HOST.com:1234/path/./from/../to%7d/item%2dobj?query=something#fragment");
664
665   /* equal */
666   url2 =
667       gst_uri_from_string
668       ("scheme://User:Pass@host.com:1234/path/to%7D/item-obj?query=something#fragment");
669   fail_unless (gst_uri_equal (url1, url2));
670   fail_unless (gst_uri_equal (url2, url1));
671   gst_uri_unref (url2);
672
673   /* different fragment */
674   url2 =
675       gst_uri_from_string
676       ("scheme://User:Pass@host.com:1234/path/to%7D/item-obj?query=something#different-fragment");
677   fail_unless (!gst_uri_equal (url1, url2));
678   gst_uri_unref (url2);
679
680   /* different query */
681   url2 =
682       gst_uri_from_string
683       ("scheme://User:Pass@host.com:1234/path/to%7D/item-obj?query=different-something#fragment");
684   fail_unless (!gst_uri_equal (url1, url2));
685   gst_uri_unref (url2);
686
687   /* different path */
688   url2 =
689       gst_uri_from_string
690       ("scheme://User:Pass@host.com:1234/path/to%7D/different-item-obj?query=something#fragment");
691   fail_unless (!gst_uri_equal (url1, url2));
692   gst_uri_unref (url2);
693
694   /* different port */
695   url2 =
696       gst_uri_from_string
697       ("scheme://User:Pass@host.com:4321/path/to%7D/item-obj?query=something#fragment");
698   fail_unless (!gst_uri_equal (url1, url2));
699   gst_uri_unref (url2);
700
701   /* different host */
702   url2 =
703       gst_uri_from_string
704       ("scheme://User:Pass@different-host.com:1234/path/to%7D/item-obj?query=something#fragment");
705   fail_unless (!gst_uri_equal (url1, url2));
706   gst_uri_unref (url2);
707
708   /* different userinfo */
709   url2 =
710       gst_uri_from_string
711       ("scheme://Different-User:Pass@host.com:1234/path/to%7D/item-obj?query=something#fragment");
712   fail_unless (!gst_uri_equal (url1, url2));
713   gst_uri_unref (url2);
714
715   /* different scheme */
716   url2 =
717       gst_uri_from_string
718       ("different+scheme://User:Pass@host.com:1234/path/to%7D/item-obj?query=something#fragment");
719   fail_unless (!gst_uri_equal (url1, url2));
720   gst_uri_unref (url2);
721
722   /* different (no scheme) */
723   url2 =
724       gst_uri_from_string
725       ("//User:Pass@host.com:1234/path/to%7D/item-obj?query=something#fragment");
726   fail_unless (!gst_uri_equal (url1, url2));
727   gst_uri_unref (url2);
728
729   /* different (no userinfo) */
730   url2 =
731       gst_uri_from_string
732       ("scheme://host.com:1234/path/to%7D/item-obj?query=something#fragment");
733   fail_unless (!gst_uri_equal (url1, url2));
734   gst_uri_unref (url2);
735
736   /* different (no host) */
737   url2 =
738       gst_uri_from_string
739       ("scheme://User:Pass@:1234/path/to%7D/item-obj?query=something#fragment");
740   fail_unless (!gst_uri_equal (url1, url2));
741   gst_uri_unref (url2);
742
743   /* different (no port) */
744   url2 =
745       gst_uri_from_string
746       ("scheme://User:Pass@host.com/path/to%7D/item-obj?query=something#fragment");
747   fail_unless (!gst_uri_equal (url1, url2));
748   gst_uri_unref (url2);
749
750   /* different (no path) */
751   url2 =
752       gst_uri_from_string
753       ("scheme://User:Pass@host.com:1234?query=something#fragment");
754   fail_unless (!gst_uri_equal (url1, url2));
755   gst_uri_unref (url2);
756
757   /* different (no query) */
758   url2 =
759       gst_uri_from_string
760       ("scheme://User:Pass@host.com:1234/path/to%7D/item-obj#fragment");
761   fail_unless (!gst_uri_equal (url1, url2));
762   gst_uri_unref (url2);
763
764   /* different (no fragment) */
765   url2 =
766       gst_uri_from_string
767       ("scheme://User:Pass@host.com:1234/path/to%7D/item-obj?query=something");
768   fail_unless (!gst_uri_equal (url1, url2));
769   gst_uri_unref (url2);
770
771   /* compare two NULL uris */
772   fail_unless (gst_uri_equal (NULL, NULL));
773
774   /* compare same object */
775   fail_unless (gst_uri_equal (url1, url1));
776
777   /* compare one NULL and one non-NULL uri */
778   fail_unless (!gst_uri_equal (url1, NULL));
779   fail_unless (!gst_uri_equal (NULL, url1));
780
781   gst_uri_unref (url1);
782 }
783
784 GST_END_TEST;
785
786 GST_START_TEST (test_url_constructors)
787 {
788   GstUri *url1, *url2;
789   gchar *tmp_str;
790   GHashTable *tmp_table;
791
792   url1 =
793       gst_uri_new ("scheme", "userinfo", "hostname", 1234, "/path/to/file",
794       "query", "fragment");
795   fail_unless_equals_string (gst_uri_get_scheme (url1), "scheme");
796   fail_unless_equals_string (gst_uri_get_userinfo (url1), "userinfo");
797   fail_unless_equals_string (gst_uri_get_host (url1), "hostname");
798   fail_unless (gst_uri_get_port (url1) == 1234);
799   tmp_str = gst_uri_get_path (url1);
800   fail_unless_equals_string (tmp_str, "/path/to/file");
801   g_free (tmp_str);
802   tmp_table = gst_uri_get_query_table (url1);
803   fail_unless (g_hash_table_size (tmp_table) == 1);
804   fail_unless (g_hash_table_contains (tmp_table, "query"));
805   fail_unless (g_hash_table_lookup (tmp_table, "query") == NULL);
806   g_hash_table_unref (tmp_table);
807   fail_unless_equals_string (gst_uri_get_fragment (url1), "fragment");
808   tmp_str = gst_uri_to_string (url1);
809   fail_unless_equals_string (tmp_str,
810       "scheme://userinfo@hostname:1234/path/to/file?query#fragment");
811   g_free (tmp_str);
812
813   url2 =
814       gst_uri_new_with_base (url1, NULL, NULL, NULL, GST_URI_NO_PORT,
815       "new_file", NULL, NULL);
816   fail_unless_equals_string (gst_uri_get_scheme (url2), "scheme");
817   fail_unless_equals_string (gst_uri_get_userinfo (url2), "userinfo");
818   fail_unless_equals_string (gst_uri_get_host (url2), "hostname");
819   fail_unless (gst_uri_get_port (url2) == 1234);
820   tmp_str = gst_uri_get_path (url2);
821   fail_unless_equals_string (tmp_str, "/path/to/new_file");
822   g_free (tmp_str);
823   fail_unless (gst_uri_get_query_table (url2) == NULL);
824   fail_unless (gst_uri_get_fragment (url2) == NULL);
825   tmp_str = gst_uri_to_string (url2);
826   fail_unless_equals_string (tmp_str,
827       "scheme://userinfo@hostname:1234/path/to/new_file");
828   g_free (tmp_str);
829   gst_uri_unref (url2);
830
831   url2 = gst_uri_from_string_with_base (url1, "/a/new/path/to/file");
832   fail_unless_equals_string (gst_uri_get_scheme (url2), "scheme");
833   fail_unless_equals_string (gst_uri_get_userinfo (url2), "userinfo");
834   fail_unless_equals_string (gst_uri_get_host (url2), "hostname");
835   fail_unless (gst_uri_get_port (url2) == 1234);
836   tmp_str = gst_uri_get_path (url2);
837   fail_unless_equals_string (tmp_str, "/a/new/path/to/file");
838   g_free (tmp_str);
839   fail_unless (gst_uri_get_query_table (url2) == NULL);
840   fail_unless (gst_uri_get_fragment (url2) == NULL);
841   tmp_str = gst_uri_to_string (url2);
842   fail_unless_equals_string (tmp_str,
843       "scheme://userinfo@hostname:1234/a/new/path/to/file");
844   g_free (tmp_str);
845   gst_uri_unref (url2);
846
847   url2 = gst_uri_from_string_with_base (url1, "http://foobar.com/bla");
848   fail_unless_equals_string (gst_uri_get_scheme (url2), "http");
849   fail_unless_equals_string (gst_uri_get_host (url2), "foobar.com");
850   fail_unless (gst_uri_get_port (url2) == 0);
851   tmp_str = gst_uri_get_path (url2);
852   fail_unless_equals_string (tmp_str, "/bla");
853   g_free (tmp_str);
854   fail_unless (gst_uri_get_query_table (url2) == NULL);
855   fail_unless (gst_uri_get_fragment (url2) == NULL);
856   tmp_str = gst_uri_to_string (url2);
857   fail_unless_equals_string (tmp_str, "http://foobar.com/bla");
858   g_free (tmp_str);
859   gst_uri_unref (url2);
860
861   url2 = gst_uri_copy (url1);
862   fail_unless (gst_uri_equal (url1, url2));
863   gst_uri_set_query_value (url2, "key", "value");
864   fail_unless (!gst_uri_equal (url1, url2));
865   gst_uri_unref (url2);
866
867   gst_uri_unref (url1);
868 }
869
870 GST_END_TEST;
871
872 GST_START_TEST (test_url_get_set)
873 {
874   GstUri *url;
875   gchar *tmp_str;
876   GList *tmp_list;
877
878   url = gst_uri_from_string ("scheme://hostname/path/to/file?query#fragment");
879
880   fail_unless (gst_uri_set_scheme (url, "new+scheme"));
881   fail_unless_equals_string (gst_uri_get_scheme (url), "new+scheme");
882   tmp_str = gst_uri_to_string (url);
883   fail_unless_equals_string (tmp_str,
884       "new+scheme://hostname/path/to/file?query#fragment");
885   g_free (tmp_str);
886
887   fail_unless (gst_uri_set_scheme (url, NULL));
888   fail_unless (gst_uri_get_scheme (url) == NULL);
889   tmp_str = gst_uri_to_string (url);
890   fail_unless_equals_string (tmp_str, "//hostname/path/to/file?query#fragment");
891   g_free (tmp_str);
892
893   fail_unless (!gst_uri_set_scheme (NULL, "fail"));
894   fail_unless (gst_uri_set_scheme (NULL, NULL));
895
896   fail_unless (gst_uri_set_userinfo (url, "username:password"));
897   fail_unless_equals_string (gst_uri_get_userinfo (url), "username:password");
898   tmp_str = gst_uri_to_string (url);
899   fail_unless_equals_string (tmp_str,
900       "//username:password@hostname/path/to/file?query#fragment");
901   g_free (tmp_str);
902
903   fail_unless (gst_uri_set_userinfo (url, NULL));
904   fail_unless (gst_uri_get_userinfo (url) == NULL);
905   tmp_str = gst_uri_to_string (url);
906   fail_unless_equals_string (tmp_str, "//hostname/path/to/file?query#fragment");
907   g_free (tmp_str);
908
909   fail_unless (!gst_uri_set_userinfo (NULL, "fail"));
910   fail_unless (gst_uri_set_userinfo (NULL, NULL));
911
912   fail_unless (gst_uri_set_host (url, NULL));
913   fail_unless (gst_uri_get_host (url) == NULL);
914   tmp_str = gst_uri_to_string (url);
915   fail_unless_equals_string (tmp_str, "/path/to/file?query#fragment");
916   g_free (tmp_str);
917
918   fail_unless (gst_uri_set_host (url, "example.com"));
919   fail_unless_equals_string (gst_uri_get_host (url), "example.com");
920   tmp_str = gst_uri_to_string (url);
921   fail_unless_equals_string (tmp_str,
922       "//example.com/path/to/file?query#fragment");
923   g_free (tmp_str);
924
925   fail_unless (!gst_uri_set_host (NULL, "fail"));
926   fail_unless (gst_uri_set_host (NULL, NULL));
927
928   fail_unless (gst_uri_set_port (url, 12345));
929   fail_unless (gst_uri_get_port (url) == 12345);
930   tmp_str = gst_uri_to_string (url);
931   fail_unless_equals_string (tmp_str,
932       "//example.com:12345/path/to/file?query#fragment");
933   g_free (tmp_str);
934
935   fail_unless (gst_uri_set_port (url, GST_URI_NO_PORT));
936   fail_unless (gst_uri_get_port (url) == GST_URI_NO_PORT);
937   tmp_str = gst_uri_to_string (url);
938   fail_unless_equals_string (tmp_str,
939       "//example.com/path/to/file?query#fragment");
940   g_free (tmp_str);
941
942   fail_unless (!gst_uri_set_port (NULL, 1234));
943   fail_unless (gst_uri_set_port (NULL, GST_URI_NO_PORT));
944
945   fail_unless (gst_uri_append_path_segment (url, "here"));
946   tmp_str = gst_uri_to_string (url);
947   fail_unless_equals_string (tmp_str,
948       "//example.com/path/to/file/here?query#fragment");
949   g_free (tmp_str);
950
951   fail_unless (!gst_uri_append_path_segment (NULL, "fail"));
952   fail_unless (gst_uri_append_path_segment (NULL, NULL));
953
954   fail_unless (gst_uri_append_path (url, "../there"));
955   tmp_str = gst_uri_to_string (url);
956   fail_unless_equals_string (tmp_str,
957       "//example.com/path/to/file/here/../there?query#fragment");
958   g_free (tmp_str);
959
960   fail_unless (!gst_uri_append_path (NULL, "fail"));
961   fail_unless (gst_uri_append_path (NULL, NULL));
962
963   gst_uri_normalize (url);
964
965   tmp_list = gst_uri_get_path_segments (url);
966   fail_unless (tmp_list != NULL);
967   tmp_list = g_list_append (tmp_list, g_strdup ("segment"));
968   tmp_str = gst_uri_to_string (url);
969   fail_unless_equals_string (tmp_str,
970       "//example.com/path/to/file/there?query#fragment");
971   g_free (tmp_str);
972   fail_unless (gst_uri_set_path_segments (url, tmp_list));
973   tmp_str = gst_uri_to_string (url);
974   fail_unless_equals_string (tmp_str,
975       "//example.com/path/to/file/there/segment?query#fragment");
976   g_free (tmp_str);
977
978   tmp_list = g_list_append (NULL, g_strdup ("test"));
979   fail_unless (!gst_uri_set_path_segments (NULL, tmp_list));
980   fail_unless (gst_uri_set_path_segments (NULL, NULL));
981
982   fail_unless (gst_uri_set_query_value (url, "key", "value"));
983   tmp_str = gst_uri_to_string (url);
984 #if GLIB_CHECK_VERSION(2, 59, 0)
985   fail_unless_equals_string (tmp_str,
986      "//example.com/path/to/file/there/segment?key=value&query#fragment");
987 #else
988   fail_unless_equals_string (tmp_str,
989      "//example.com/path/to/file/there/segment?query&key=value#fragment");
990 #endif
991   g_free (tmp_str);
992
993   fail_unless (gst_uri_set_query_value (url, "key", NULL));
994   tmp_str = gst_uri_to_string (url);
995 #if GLIB_CHECK_VERSION(2, 59, 0)
996   fail_unless_equals_string (tmp_str,
997       "//example.com/path/to/file/there/segment?key&query#fragment");
998 #else
999   fail_unless_equals_string (tmp_str,
1000       "//example.com/path/to/file/there/segment?query&key#fragment");
1001 #endif
1002   g_free (tmp_str);
1003
1004   fail_unless (!gst_uri_set_query_value (NULL, "key", "value"));
1005
1006   fail_unless (gst_uri_remove_query_key (url, "key"));
1007   tmp_str = gst_uri_to_string (url);
1008   fail_unless_equals_string (tmp_str,
1009       "//example.com/path/to/file/there/segment?query#fragment");
1010   g_free (tmp_str);
1011
1012   fail_unless (!gst_uri_remove_query_key (url, "key"));
1013   fail_unless (!gst_uri_remove_query_key (NULL, "key"));
1014
1015   fail_unless (gst_uri_set_fragment (url, NULL));
1016   fail_unless (gst_uri_get_fragment (url) == NULL);
1017   tmp_str = gst_uri_to_string (url);
1018   fail_unless_equals_string (tmp_str,
1019       "//example.com/path/to/file/there/segment?query");
1020   g_free (tmp_str);
1021
1022   fail_unless (gst_uri_set_fragment (url, "tag"));
1023   fail_unless_equals_string (gst_uri_get_fragment (url), "tag");
1024   tmp_str = gst_uri_to_string (url);
1025   fail_unless_equals_string (tmp_str,
1026       "//example.com/path/to/file/there/segment?query#tag");
1027   g_free (tmp_str);
1028
1029   fail_unless (!gst_uri_set_fragment (NULL, "can't set if no URI"));
1030   fail_unless (gst_uri_set_fragment (NULL, NULL));
1031
1032   gst_uri_unref (url);
1033 }
1034
1035 GST_END_TEST;
1036
1037 GST_START_TEST (test_url_get_media_fragment_table)
1038 {
1039   GstUri *url;
1040   gchar *val;
1041   GHashTable *table;
1042
1043   /* Examples at https://www.w3.org/TR/media-frags/#processing-media-fragment-uri */
1044
1045   /* TEST "t=1" */
1046   url = gst_uri_from_string ("http://foo/var/file#t=1");
1047   table = gst_uri_get_media_fragment_table (url);
1048   fail_unless (table);
1049   fail_unless (g_hash_table_size (table) == 1);
1050   fail_unless (g_hash_table_lookup_extended (table, "t", NULL,
1051           (gpointer) & val));
1052   fail_unless_equals_string ("1", val);
1053   g_hash_table_unref (table);
1054   gst_uri_unref (url);
1055
1056   /* NOTE: Media Fragments URI 1.0 (W3C) is saying that
1057    * "Multiple occurrences of the same dimension: only the last valid occurrence
1058    *  of a dimension (e.g. t=10 in #t=2&t=10) is interpreted and all previous
1059    *  occurrences (valid or invalid) SHOULD be ignored by the user agent"
1060    */
1061   /* TEST "t=1&t=2" */
1062   url = gst_uri_from_string ("http://foo/var/file#t=1&t=2");
1063   table = gst_uri_get_media_fragment_table (url);
1064   fail_unless (table);
1065   fail_unless (g_hash_table_size (table) == 1);
1066   fail_unless (g_hash_table_lookup_extended (table, "t", NULL,
1067           (gpointer) & val));
1068   fail_unless_equals_string ("2", val);
1069   g_hash_table_unref (table);
1070   gst_uri_unref (url);
1071
1072   /* TEST "a=b=c" */
1073   url = gst_uri_from_string ("http://foo/var/file#a=b=c");
1074   table = gst_uri_get_media_fragment_table (url);
1075   fail_unless (table);
1076   fail_unless (g_hash_table_size (table) == 1);
1077   fail_unless (g_hash_table_lookup_extended (table, "a", NULL,
1078           (gpointer) & val));
1079   fail_unless_equals_string ("b=c", val);
1080   g_hash_table_unref (table);
1081   gst_uri_unref (url);
1082
1083   /* TEST "a&b=c" */
1084   url = gst_uri_from_string ("http://foo/var/file#a&b=c");
1085   table = gst_uri_get_media_fragment_table (url);
1086   fail_unless (table);
1087   fail_unless (g_hash_table_size (table) == 2);
1088   fail_unless (g_hash_table_lookup_extended (table, "a", NULL,
1089           (gpointer) & val));
1090   fail_unless (val == NULL);
1091   fail_unless (g_hash_table_lookup_extended (table, "b", NULL,
1092           (gpointer) & val));
1093   fail_unless_equals_string ("c", val);
1094   g_hash_table_unref (table);
1095   gst_uri_unref (url);
1096
1097   /* TEST "%74=%6ept%3A%310" */
1098   url = gst_uri_from_string ("http://foo/var/file#%74=%6ept%3A%310");
1099   table = gst_uri_get_media_fragment_table (url);
1100   fail_unless (table);
1101   fail_unless (g_hash_table_size (table) == 1);
1102   fail_unless (g_hash_table_lookup_extended (table, "t", NULL,
1103           (gpointer) & val));
1104   fail_unless_equals_string ("npt:10", val);
1105   g_hash_table_unref (table);
1106   gst_uri_unref (url);
1107 }
1108
1109 GST_END_TEST;
1110
1111 static Suite *
1112 gst_uri_suite (void)
1113 {
1114   Suite *s = suite_create ("GstURI");
1115   TCase *tc_chain = tcase_create ("uri");
1116
1117   tcase_set_timeout (tc_chain, 20);
1118
1119   suite_add_tcase (s, tc_chain);
1120   tcase_add_test (tc_chain, test_protocol_case);
1121   tcase_add_test (tc_chain, test_uri_get_location);
1122 #ifndef GST_REMOVE_DEPRECATED
1123   tcase_add_test (tc_chain, test_gst_uri_construct);
1124 #endif
1125   tcase_add_test (tc_chain, test_uri_misc);
1126   tcase_add_test (tc_chain, test_element_make_from_uri);
1127 #ifdef G_OS_WIN32
1128   tcase_add_test (tc_chain, test_win32_uri);
1129 #endif
1130   tcase_add_test (tc_chain, test_url_parsing);
1131   tcase_add_test (tc_chain, test_url_presenting);
1132   tcase_add_test (tc_chain, test_url_normalization);
1133   tcase_add_test (tc_chain, test_url_joining);
1134   tcase_add_test (tc_chain, test_url_equality);
1135   tcase_add_test (tc_chain, test_url_constructors);
1136   tcase_add_test (tc_chain, test_url_get_set);
1137   tcase_add_test (tc_chain, test_url_get_media_fragment_table);
1138
1139   return s;
1140 }
1141
1142 GST_CHECK_MAIN (gst_uri);