GstUri: Add GstUri miniobject to handle URIs in an RFC 3986 compliant fashion
[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
21 #include <gst/check/gstcheck.h>
22
23 GST_START_TEST (test_protocol_case)
24 {
25   GstElement *element;
26   GError *err = NULL;
27
28   element = gst_element_make_from_uri (GST_URI_SRC, "file:///foo/bar", NULL,
29       &err);
30
31   /* no element? probably no registry, bail out */
32   if (element == NULL && err->code == GST_URI_ERROR_UNSUPPORTED_PROTOCOL) {
33     g_error_free (err);
34     return;
35   }
36
37   gst_object_unref (element);
38   element = gst_element_make_from_uri (GST_URI_SRC, "FILE:///foo/bar", NULL,
39       NULL);
40   fail_unless (element != NULL,
41       "Got source for 'file://' URI but not for 'FILE://' URI");
42   gst_object_unref (element);
43 }
44
45 GST_END_TEST;
46
47 GST_START_TEST (test_uri_get_location)
48 {
49   gchar *l;
50
51   /* URI with no location should return empty string */
52   l = gst_uri_get_location ("dvd://");
53   fail_unless (l != NULL);
54   fail_unless_equals_string (l, "");
55   g_free (l);
56
57   /* URI with hostname */
58   l = gst_uri_get_location ("smb://supercomputer/path/to/file");
59   fail_unless (l != NULL);
60   fail_unless_equals_string (l, "supercomputer/path/to/file");
61   g_free (l);
62
63   /* URI */
64   l = gst_uri_get_location ("file:///path/to/file");
65   fail_unless (l != NULL);
66   fail_unless_equals_string (l, "/path/to/file");
67   g_free (l);
68
69   /* unescaping */
70   l = gst_uri_get_location ("file:///path/to/some%20file");
71   fail_unless (l != NULL);
72   fail_unless_equals_string (l, "/path/to/some file");
73   g_free (l);
74 }
75
76 GST_END_TEST;
77
78 #ifdef G_OS_WIN32
79
80 GST_START_TEST (test_win32_uri)
81 {
82   gchar *uri, *l;
83
84   uri = g_strdup ("file:///c:/my%20music/foo.ogg");
85   l = gst_uri_get_location (uri);
86   fail_unless (l != NULL);
87   /* fail_unless_equals_string will screw up here in the failure case
88    * because the string constant will be appended to the printf format
89    * message string and contains a '%', that's why we use fail_unless here */
90   fail_unless (g_str_equal (l, "c:/my music/foo.ogg"),
91       "wrong location '%s' returned for URI '%s'", l, uri);
92   g_free (l);
93   g_free (uri);
94
95   /* make sure the other variant with two slashes before the C: (which was
96    * needed before because of a bug in _get_location()) still works */
97   uri = g_strdup ("file://c:/my%20music/foo.ogg");
98   l = gst_uri_get_location (uri);
99   fail_unless (l != NULL);
100   /* fail_unless_equals_string will screw up here in the failure case
101    * because the string constant will be appended to the printf format
102    * message string and contains a '%', that's why we use fail_unless here */
103   fail_unless (g_str_equal (l, "c:/my music/foo.ogg"),
104       "wrong location '%s' returned for URI '%s'", l, uri);
105   g_free (l);
106   g_free (uri);
107 }
108
109 GST_END_TEST;
110
111 #endif /* G_OS_WIN32 */
112
113 GST_START_TEST (test_uri_misc)
114 {
115   /* require at least two characters for the protocol */
116   fail_if (gst_uri_is_valid ("B:\\foo.txt"));
117   fail_if (gst_uri_is_valid ("B:/foo.txt"));
118   fail_if (gst_uri_is_valid ("B://foo.txt"));
119   fail_if (gst_uri_is_valid ("B:foo.txt"));
120
121   fail_unless (gst_uri_is_valid ("fd://0"));
122   fail_unless (gst_uri_is_valid ("AB:\\foo.txt"));
123   fail_unless (gst_uri_is_valid ("AB:/foo.txt"));
124   fail_unless (gst_uri_is_valid ("AB://foo.txt"));
125   fail_unless (gst_uri_is_valid ("AB:foo.txt"));
126
127   fail_unless (gst_uri_is_valid ("ABC:/foo.txt"));
128   fail_unless (gst_uri_is_valid ("ABC://foo.txt"));
129   fail_unless (gst_uri_is_valid ("ABC:foo.txt"));
130
131   fail_unless (gst_uri_is_valid ("ABCD:/foo.txt"));
132   fail_unless (gst_uri_is_valid ("ABCD://foo.txt"));
133   fail_unless (gst_uri_is_valid ("ABCD:foo.txt"));
134 }
135
136 GST_END_TEST;
137
138 GST_START_TEST (test_element_make_from_uri)
139 {
140   GstElement *element;
141   GError *err = NULL;
142
143   element = gst_element_make_from_uri (GST_URI_SRC, "foo://", NULL, NULL);
144   fail_unless (element == NULL);
145
146   element = gst_element_make_from_uri (GST_URI_SRC, "foo://", NULL, &err);
147   fail_unless (element == NULL);
148   fail_unless (err != NULL);
149   fail_unless (err->code == GST_URI_ERROR_UNSUPPORTED_PROTOCOL);
150   g_error_free (err);
151   err = NULL;
152
153   if (gst_registry_check_feature_version (gst_registry_get (), "filesrc",
154           GST_VERSION_MAJOR, GST_VERSION_MINOR, GST_VERSION_MICRO)) {
155     element = gst_element_make_from_uri (GST_URI_SRC, "file://host/foo", NULL,
156         &err);
157     fail_unless (element == NULL);
158     fail_unless (err != NULL);
159     fail_unless (err->code == GST_URI_ERROR_BAD_URI);
160     g_error_free (err);
161     err = NULL;
162   }
163 }
164
165 GST_END_TEST;
166
167 GST_START_TEST (test_url_parsing)
168 {
169   GstUri *url;
170   GList *list;
171   gchar *tmp_str;
172
173   url =
174       gst_uri_from_string
175       ("scheme://user:pass@host.com:1234/path/to/item-obj?query=something#fragment");
176
177   fail_unless_equals_string (gst_uri_get_scheme (url), "scheme");
178   fail_unless_equals_string (gst_uri_get_userinfo (url), "user:pass");
179   fail_unless_equals_string (gst_uri_get_host (url), "host.com");
180   fail_unless (gst_uri_get_port (url) == 1234);
181   tmp_str = gst_uri_get_path (url);
182   fail_unless_equals_string (tmp_str, "/path/to/item-obj");
183   g_free (tmp_str);
184   list = gst_uri_get_query_keys (url);
185   fail_unless (g_list_length (list) == 1);
186   g_list_free (list);
187   fail_unless (gst_uri_query_has_key (url, "query"));
188   fail_unless_equals_string (gst_uri_get_query_value (url, "query"),
189       "something");
190   fail_unless_equals_string (gst_uri_get_fragment (url), "fragment");
191   gst_uri_unref (url);
192
193   url = gst_uri_from_string ("scheme://host/path/to/dir/");
194   fail_unless_equals_string (gst_uri_get_scheme (url), "scheme");
195   fail_unless (gst_uri_get_userinfo (url) == NULL);
196   fail_unless_equals_string (gst_uri_get_host (url), "host");
197   fail_unless (gst_uri_get_port (url) == GST_URI_NO_PORT);
198   tmp_str = gst_uri_get_path (url);
199   fail_unless_equals_string (tmp_str, "/path/to/dir/");
200   g_free (tmp_str);
201   fail_unless (gst_uri_get_query_table (url) == NULL);
202   fail_unless (gst_uri_get_fragment (url) == NULL);
203   gst_uri_unref (url);
204
205   url = gst_uri_from_string ("urn:name:path");
206   fail_unless_equals_string (gst_uri_get_scheme (url), "urn");
207   fail_unless (gst_uri_get_userinfo (url) == NULL);
208   fail_unless (gst_uri_get_host (url) == NULL);
209   fail_unless (gst_uri_get_port (url) == GST_URI_NO_PORT);
210   tmp_str = gst_uri_get_path (url);
211   fail_unless_equals_string (tmp_str, "name:path");
212   g_free (tmp_str);
213   list = gst_uri_get_query_keys (url);
214   fail_unless (g_list_length (list) == 0);
215   g_list_free (list);
216   fail_unless (gst_uri_get_fragment (url) == NULL);
217   gst_uri_unref (url);
218 }
219
220 GST_END_TEST;
221
222 GST_START_TEST (test_url_normalization)
223 {
224   GstUri *url;
225   gchar *tmp_str;
226
227   url =
228       gst_uri_from_string
229       ("ScHeMe://User:P%61ss@HOST.%63om:1234/path/./from/../to%7d/item%2dobj?qu%65ry=something#fr%61gment");
230   fail_unless (gst_uri_normalize (url));
231   fail_unless_equals_string (gst_uri_get_scheme (url), "scheme");
232   fail_unless_equals_string (gst_uri_get_userinfo (url), "User:Pass");
233   fail_unless_equals_string (gst_uri_get_host (url), "host.com");
234   tmp_str = gst_uri_get_path (url);
235   fail_unless_equals_string (tmp_str, "/path/to}/item-obj");
236   g_free (tmp_str);
237   fail_unless (gst_uri_query_has_key (url, "query"));
238   fail_unless_equals_string (gst_uri_get_query_value (url, "query"),
239       "something");
240   fail_unless_equals_string (gst_uri_get_fragment (url), "fragment");
241   gst_uri_unref (url);
242 }
243
244 GST_END_TEST;
245
246 GST_START_TEST (test_url_joining)
247 {
248   GstUri *base, *rel, *joined;
249   gchar *l;
250
251   base =
252       gst_uri_from_string
253       ("http://example.com/path/to/dir/filename.html#fragment");
254
255   /* test change of fragment only */
256   rel = gst_uri_from_string ("#new_frag");
257   joined = gst_uri_join (base, rel);
258   l = gst_uri_to_string (joined);
259   fail_unless_equals_string (l,
260       "http://example.com/path/to/dir/filename.html#new_frag");
261   g_free (l);
262   gst_uri_unref (joined);
263   gst_uri_unref (rel);
264
265   /* test addition of new query string */
266   rel = gst_uri_from_string ("?key=val");
267   joined = gst_uri_join (base, rel);
268   l = gst_uri_to_string (joined);
269   fail_unless_equals_string (l,
270       "http://example.com/path/to/dir/filename.html?key=val");
271   g_free (l);
272   gst_uri_unref (joined);
273   gst_uri_unref (rel);
274
275   /* test new base filename */
276   rel = gst_uri_from_string ("new_filename.xml");
277   joined = gst_uri_join (base, rel);
278   l = gst_uri_to_string (joined);
279   fail_unless_equals_string (l,
280       "http://example.com/path/to/dir/new_filename.xml");
281   g_free (l);
282   gst_uri_unref (joined);
283   gst_uri_unref (rel);
284
285   /* test relative file same directory */
286   rel = gst_uri_from_string ("./new_filename.xml");
287   joined = gst_uri_join (base, rel);
288   l = gst_uri_to_string (joined);
289   fail_unless_equals_string (l,
290       "http://example.com/path/to/dir/new_filename.xml");
291   g_free (l);
292   gst_uri_unref (joined);
293   gst_uri_unref (rel);
294
295   /* test relative file parent directory */
296   rel = gst_uri_from_string ("../new_filename.xml");
297   joined = gst_uri_join (base, rel);
298   l = gst_uri_to_string (joined);
299   fail_unless_equals_string (l, "http://example.com/path/to/new_filename.xml");
300   g_free (l);
301   gst_uri_unref (joined);
302   gst_uri_unref (rel);
303
304   /* test relative file grandparent directory */
305   rel = gst_uri_from_string ("../../new_filename.xml");
306   joined = gst_uri_join (base, rel);
307   l = gst_uri_to_string (joined);
308   fail_unless_equals_string (l, "http://example.com/path/new_filename.xml");
309   g_free (l);
310   gst_uri_unref (joined);
311   gst_uri_unref (rel);
312
313   /* test relative file root directory */
314   rel = gst_uri_from_string ("../../../new_filename.xml");
315   joined = gst_uri_join (base, rel);
316   l = gst_uri_to_string (joined);
317   fail_unless_equals_string (l, "http://example.com/new_filename.xml");
318   g_free (l);
319   gst_uri_unref (joined);
320   gst_uri_unref (rel);
321
322   /* test relative file beyond root directory */
323   rel = gst_uri_from_string ("../../../../new_filename.xml");
324   joined = gst_uri_join (base, rel);
325   l = gst_uri_to_string (joined);
326   fail_unless_equals_string (l, "http://example.com/new_filename.xml");
327   g_free (l);
328   gst_uri_unref (joined);
329   gst_uri_unref (rel);
330
331   /* test add subdirectory */
332   rel = gst_uri_from_string ("subdir/new_filename.xml");
333   joined = gst_uri_join (base, rel);
334   l = gst_uri_to_string (joined);
335   fail_unless_equals_string (l,
336       "http://example.com/path/to/dir/subdir/new_filename.xml");
337   g_free (l);
338   gst_uri_unref (joined);
339   gst_uri_unref (rel);
340
341   /* test change directory */
342   rel = gst_uri_from_string ("../subdir/new_filename.xml");
343   joined = gst_uri_join (base, rel);
344   l = gst_uri_to_string (joined);
345   fail_unless_equals_string (l,
346       "http://example.com/path/to/subdir/new_filename.xml");
347   g_free (l);
348   gst_uri_unref (joined);
349   gst_uri_unref (rel);
350
351   gst_uri_unref (base);
352
353   /* change base for path ending in directory */
354   base = gst_uri_from_string ("http://example.com/path/to/dir/");
355
356   /* test adding file to directory */
357   rel = gst_uri_from_string ("new_filename.xml");
358   joined = gst_uri_join (base, rel);
359   l = gst_uri_to_string (joined);
360   fail_unless_equals_string (l,
361       "http://example.com/path/to/dir/new_filename.xml");
362   g_free (l);
363   gst_uri_unref (joined);
364   gst_uri_unref (rel);
365
366   /* test adding file to directory using relative path */
367   rel = gst_uri_from_string ("./new_filename.xml");
368   joined = gst_uri_join (base, rel);
369   l = gst_uri_to_string (joined);
370   fail_unless_equals_string (l,
371       "http://example.com/path/to/dir/new_filename.xml");
372   g_free (l);
373   gst_uri_unref (joined);
374   gst_uri_unref (rel);
375
376   /* test filename in parent directory */
377   rel = gst_uri_from_string ("../new_filename.xml");
378   joined = gst_uri_join (base, rel);
379   l = gst_uri_to_string (joined);
380   fail_unless_equals_string (l, "http://example.com/path/to/new_filename.xml");
381   g_free (l);
382   gst_uri_unref (joined);
383   gst_uri_unref (rel);
384
385   /* test replace with absolute */
386   rel = gst_uri_from_string ("https://ssl.example.com/new_filename.xml");
387   joined = gst_uri_join (base, rel);
388   l = gst_uri_to_string (joined);
389   fail_unless_equals_string (l, "https://ssl.example.com/new_filename.xml");
390   g_free (l);
391   gst_uri_unref (joined);
392   gst_uri_unref (rel);
393
394   gst_uri_unref (base);
395 }
396
397 GST_END_TEST;
398
399 GST_START_TEST (test_url_equality)
400 {
401   GstUri *url1, *url2;
402
403   url1 =
404       gst_uri_from_string
405       ("ScHeMe://User:Pass@HOST.com:1234/path/./from/../to%7d/item%2dobj?query=something#fragment");
406
407   /* equal */
408   url2 =
409       gst_uri_from_string
410       ("scheme://User:Pass@host.com:1234/path/to%7D/item-obj?query=something#fragment");
411   fail_unless (gst_uri_equal (url1, url2));
412   fail_unless (gst_uri_equal (url2, url1));
413   gst_uri_unref (url2);
414
415   /* different fragment */
416   url2 =
417       gst_uri_from_string
418       ("scheme://User:Pass@host.com:1234/path/to%7D/item-obj?query=something#different-fragment");
419   fail_unless (!gst_uri_equal (url1, url2));
420   gst_uri_unref (url2);
421
422   /* different query */
423   url2 =
424       gst_uri_from_string
425       ("scheme://User:Pass@host.com:1234/path/to%7D/item-obj?query=different-something#fragment");
426   fail_unless (!gst_uri_equal (url1, url2));
427   gst_uri_unref (url2);
428
429   /* different path */
430   url2 =
431       gst_uri_from_string
432       ("scheme://User:Pass@host.com:1234/path/to%7D/different-item-obj?query=something#fragment");
433   fail_unless (!gst_uri_equal (url1, url2));
434   gst_uri_unref (url2);
435
436   /* different port */
437   url2 =
438       gst_uri_from_string
439       ("scheme://User:Pass@host.com:4321/path/to%7D/item-obj?query=something#fragment");
440   fail_unless (!gst_uri_equal (url1, url2));
441   gst_uri_unref (url2);
442
443   /* different host */
444   url2 =
445       gst_uri_from_string
446       ("scheme://User:Pass@different-host.com:1234/path/to%7D/item-obj?query=something#fragment");
447   fail_unless (!gst_uri_equal (url1, url2));
448   gst_uri_unref (url2);
449
450   /* different userinfo */
451   url2 =
452       gst_uri_from_string
453       ("scheme://Different-User:Pass@host.com:1234/path/to%7D/item-obj?query=something#fragment");
454   fail_unless (!gst_uri_equal (url1, url2));
455   gst_uri_unref (url2);
456
457   /* different scheme */
458   url2 =
459       gst_uri_from_string
460       ("different+scheme://User:Pass@host.com:1234/path/to%7D/item-obj?query=something#fragment");
461   fail_unless (!gst_uri_equal (url1, url2));
462   gst_uri_unref (url2);
463
464   /* different (no scheme) */
465   url2 =
466       gst_uri_from_string
467       ("//User:Pass@host.com:1234/path/to%7D/item-obj?query=something#fragment");
468   fail_unless (!gst_uri_equal (url1, url2));
469   gst_uri_unref (url2);
470
471   /* different (no userinfo) */
472   url2 =
473       gst_uri_from_string
474       ("scheme://host.com:1234/path/to%7D/item-obj?query=something#fragment");
475   fail_unless (!gst_uri_equal (url1, url2));
476   gst_uri_unref (url2);
477
478   /* different (no host) */
479   url2 =
480       gst_uri_from_string
481       ("scheme://User:Pass@:1234/path/to%7D/item-obj?query=something#fragment");
482   fail_unless (!gst_uri_equal (url1, url2));
483   gst_uri_unref (url2);
484
485   /* different (no port) */
486   url2 =
487       gst_uri_from_string
488       ("scheme://User:Pass@host.com/path/to%7D/item-obj?query=something#fragment");
489   fail_unless (!gst_uri_equal (url1, url2));
490   gst_uri_unref (url2);
491
492   /* different (no path) */
493   url2 =
494       gst_uri_from_string
495       ("scheme://User:Pass@host.com:1234?query=something#fragment");
496   fail_unless (!gst_uri_equal (url1, url2));
497   gst_uri_unref (url2);
498
499   /* different (no query) */
500   url2 =
501       gst_uri_from_string
502       ("scheme://User:Pass@host.com:1234/path/to%7D/item-obj#fragment");
503   fail_unless (!gst_uri_equal (url1, url2));
504   gst_uri_unref (url2);
505
506   /* different (no fragment) */
507   url2 =
508       gst_uri_from_string
509       ("scheme://User:Pass@host.com:1234/path/to%7D/item-obj?query=something");
510   fail_unless (!gst_uri_equal (url1, url2));
511   gst_uri_unref (url2);
512
513   /* compare two NULL uris */
514   fail_unless (gst_uri_equal (NULL, NULL));
515
516   /* compare same object */
517   fail_unless (gst_uri_equal (url1, url1));
518
519   /* compare one NULL and one non-NULL uri */
520   fail_unless (!gst_uri_equal (url1, NULL));
521   fail_unless (!gst_uri_equal (NULL, url1));
522
523   gst_uri_unref (url1);
524 }
525
526 GST_END_TEST;
527
528 GST_START_TEST (test_url_constructors)
529 {
530   GstUri *url1, *url2;
531   gchar *tmp_str;
532   GHashTable *tmp_table;
533
534   url1 =
535       gst_uri_new ("scheme", "userinfo", "hostname", 1234, "/path/to/file",
536       "query", "fragment");
537   fail_unless_equals_string (gst_uri_get_scheme (url1), "scheme");
538   fail_unless_equals_string (gst_uri_get_userinfo (url1), "userinfo");
539   fail_unless_equals_string (gst_uri_get_host (url1), "hostname");
540   fail_unless (gst_uri_get_port (url1) == 1234);
541   tmp_str = gst_uri_get_path (url1);
542   fail_unless_equals_string (tmp_str, "/path/to/file");
543   g_free (tmp_str);
544   tmp_table = gst_uri_get_query_table (url1);
545   fail_unless (g_hash_table_size (tmp_table) == 1);
546   fail_unless (g_hash_table_contains (tmp_table, "query"));
547   fail_unless (g_hash_table_lookup (tmp_table, "query") == NULL);
548   g_hash_table_unref (tmp_table);
549   fail_unless_equals_string (gst_uri_get_fragment (url1), "fragment");
550   tmp_str = gst_uri_to_string (url1);
551   fail_unless_equals_string (tmp_str,
552       "scheme://userinfo@hostname:1234/path/to/file?query#fragment");
553   g_free (tmp_str);
554
555   url2 =
556       gst_uri_new_with_base (url1, NULL, NULL, NULL, GST_URI_NO_PORT,
557       "new_file", NULL, NULL);
558   fail_unless_equals_string (gst_uri_get_scheme (url2), "scheme");
559   fail_unless_equals_string (gst_uri_get_userinfo (url2), "userinfo");
560   fail_unless_equals_string (gst_uri_get_host (url2), "hostname");
561   fail_unless (gst_uri_get_port (url2) == 1234);
562   tmp_str = gst_uri_get_path (url2);
563   fail_unless_equals_string (tmp_str, "/path/to/new_file");
564   g_free (tmp_str);
565   fail_unless (gst_uri_get_query_table (url2) == NULL);
566   fail_unless (gst_uri_get_fragment (url2) == NULL);
567   tmp_str = gst_uri_to_string (url2);
568   fail_unless_equals_string (tmp_str,
569       "scheme://userinfo@hostname:1234/path/to/new_file");
570   g_free (tmp_str);
571   gst_uri_unref (url2);
572
573   url2 = gst_uri_from_string_with_base (url1, "/a/new/path/to/file");
574   fail_unless_equals_string (gst_uri_get_scheme (url2), "scheme");
575   fail_unless_equals_string (gst_uri_get_userinfo (url2), "userinfo");
576   fail_unless_equals_string (gst_uri_get_host (url2), "hostname");
577   fail_unless (gst_uri_get_port (url2) == 1234);
578   tmp_str = gst_uri_get_path (url2);
579   fail_unless_equals_string (tmp_str, "/a/new/path/to/file");
580   g_free (tmp_str);
581   fail_unless (gst_uri_get_query_table (url2) == NULL);
582   fail_unless (gst_uri_get_fragment (url2) == NULL);
583   tmp_str = gst_uri_to_string (url2);
584   fail_unless_equals_string (tmp_str,
585       "scheme://userinfo@hostname:1234/a/new/path/to/file");
586   g_free (tmp_str);
587   gst_uri_unref (url2);
588
589   url2 = gst_uri_copy (url1);
590   fail_unless (gst_uri_equal (url1, url2));
591   gst_uri_set_query_value (url2, "key", "value");
592   fail_unless (!gst_uri_equal (url1, url2));
593   gst_uri_unref (url2);
594
595   gst_uri_unref (url1);
596 }
597
598 GST_END_TEST;
599
600 GST_START_TEST (test_url_get_set)
601 {
602   GstUri *url;
603   gchar *tmp_str;
604   GList *tmp_list;
605
606   url = gst_uri_from_string ("scheme://hostname/path/to/file?query#fragment");
607
608   fail_unless (gst_uri_set_scheme (url, "new+scheme"));
609   fail_unless_equals_string (gst_uri_get_scheme (url), "new+scheme");
610   tmp_str = gst_uri_to_string (url);
611   fail_unless_equals_string (tmp_str,
612       "new+scheme://hostname/path/to/file?query#fragment");
613   g_free (tmp_str);
614
615   fail_unless (gst_uri_set_scheme (url, NULL));
616   fail_unless (gst_uri_get_scheme (url) == NULL);
617   tmp_str = gst_uri_to_string (url);
618   fail_unless_equals_string (tmp_str, "//hostname/path/to/file?query#fragment");
619   g_free (tmp_str);
620
621   fail_unless (!gst_uri_set_scheme (NULL, "fail"));
622   fail_unless (gst_uri_set_scheme (NULL, NULL));
623
624   fail_unless (gst_uri_set_userinfo (url, "username:password"));
625   fail_unless_equals_string (gst_uri_get_userinfo (url), "username:password");
626   tmp_str = gst_uri_to_string (url);
627   fail_unless_equals_string (tmp_str,
628       "//username:password@hostname/path/to/file?query#fragment");
629   g_free (tmp_str);
630
631   fail_unless (gst_uri_set_userinfo (url, NULL));
632   fail_unless (gst_uri_get_userinfo (url) == NULL);
633   tmp_str = gst_uri_to_string (url);
634   fail_unless_equals_string (tmp_str, "//hostname/path/to/file?query#fragment");
635   g_free (tmp_str);
636
637   fail_unless (!gst_uri_set_userinfo (NULL, "fail"));
638   fail_unless (gst_uri_set_userinfo (NULL, NULL));
639
640   fail_unless (gst_uri_set_host (url, NULL));
641   fail_unless (gst_uri_get_host (url) == NULL);
642   tmp_str = gst_uri_to_string (url);
643   fail_unless_equals_string (tmp_str, "/path/to/file?query#fragment");
644   g_free (tmp_str);
645
646   fail_unless (gst_uri_set_host (url, "example.com"));
647   fail_unless_equals_string (gst_uri_get_host (url), "example.com");
648   tmp_str = gst_uri_to_string (url);
649   fail_unless_equals_string (tmp_str,
650       "//example.com/path/to/file?query#fragment");
651   g_free (tmp_str);
652
653   fail_unless (!gst_uri_set_host (NULL, "fail"));
654   fail_unless (gst_uri_set_host (NULL, NULL));
655
656   fail_unless (gst_uri_set_port (url, 12345));
657   fail_unless (gst_uri_get_port (url) == 12345);
658   tmp_str = gst_uri_to_string (url);
659   fail_unless_equals_string (tmp_str,
660       "//example.com:12345/path/to/file?query#fragment");
661   g_free (tmp_str);
662
663   fail_unless (gst_uri_set_port (url, GST_URI_NO_PORT));
664   fail_unless (gst_uri_get_port (url) == GST_URI_NO_PORT);
665   tmp_str = gst_uri_to_string (url);
666   fail_unless_equals_string (tmp_str,
667       "//example.com/path/to/file?query#fragment");
668   g_free (tmp_str);
669
670   fail_unless (!gst_uri_set_port (NULL, 1234));
671   fail_unless (gst_uri_set_port (NULL, GST_URI_NO_PORT));
672
673   fail_unless (gst_uri_append_path_segment (url, "here"));
674   tmp_str = gst_uri_to_string (url);
675   fail_unless_equals_string (tmp_str,
676       "//example.com/path/to/file/here?query#fragment");
677   g_free (tmp_str);
678
679   fail_unless (!gst_uri_append_path_segment (NULL, "fail"));
680   fail_unless (gst_uri_append_path_segment (NULL, NULL));
681
682   fail_unless (gst_uri_append_path (url, "../there"));
683   tmp_str = gst_uri_to_string (url);
684   fail_unless_equals_string (tmp_str,
685       "//example.com/path/to/file/here/../there?query#fragment");
686   g_free (tmp_str);
687
688   fail_unless (!gst_uri_append_path (NULL, "fail"));
689   fail_unless (gst_uri_append_path (NULL, NULL));
690
691   gst_uri_normalize (url);
692
693   tmp_list = gst_uri_get_path_segments (url);
694   fail_unless (tmp_list != NULL);
695   tmp_list = g_list_append (tmp_list, g_strdup ("segment"));
696   tmp_str = gst_uri_to_string (url);
697   fail_unless_equals_string (tmp_str,
698       "//example.com/path/to/file/there?query#fragment");
699   g_free (tmp_str);
700   fail_unless (gst_uri_set_path_segments (url, tmp_list));
701   tmp_str = gst_uri_to_string (url);
702   fail_unless_equals_string (tmp_str,
703       "//example.com/path/to/file/there/segment?query#fragment");
704   g_free (tmp_str);
705
706   tmp_list = g_list_append (NULL, g_strdup ("test"));
707   fail_unless (!gst_uri_set_path_segments (NULL, tmp_list));
708   fail_unless (gst_uri_set_path_segments (NULL, NULL));
709
710   fail_unless (gst_uri_set_query_value (url, "key", "value"));
711   tmp_str = gst_uri_to_string (url);
712   fail_unless_equals_string (tmp_str,
713       "//example.com/path/to/file/there/segment?query&key=value#fragment");
714   g_free (tmp_str);
715
716   fail_unless (gst_uri_set_query_value (url, "key", NULL));
717   tmp_str = gst_uri_to_string (url);
718   fail_unless_equals_string (tmp_str,
719       "//example.com/path/to/file/there/segment?query&key#fragment");
720   g_free (tmp_str);
721
722   fail_unless (!gst_uri_set_query_value (NULL, "key", "value"));
723
724   fail_unless (gst_uri_remove_query_key (url, "key"));
725   tmp_str = gst_uri_to_string (url);
726   fail_unless_equals_string (tmp_str,
727       "//example.com/path/to/file/there/segment?query#fragment");
728   g_free (tmp_str);
729
730   fail_unless (!gst_uri_remove_query_key (url, "key"));
731   fail_unless (!gst_uri_remove_query_key (NULL, "key"));
732
733   fail_unless (gst_uri_set_fragment (url, NULL));
734   fail_unless (gst_uri_get_fragment (url) == NULL);
735   tmp_str = gst_uri_to_string (url);
736   fail_unless_equals_string (tmp_str,
737       "//example.com/path/to/file/there/segment?query");
738   g_free (tmp_str);
739
740   fail_unless (gst_uri_set_fragment (url, "tag"));
741   fail_unless_equals_string (gst_uri_get_fragment (url), "tag");
742   tmp_str = gst_uri_to_string (url);
743   fail_unless_equals_string (tmp_str,
744       "//example.com/path/to/file/there/segment?query#tag");
745   g_free (tmp_str);
746
747   fail_unless (!gst_uri_set_fragment (NULL, "can't set if no URI"));
748   fail_unless (gst_uri_set_fragment (NULL, NULL));
749
750   gst_uri_unref (url);
751 }
752
753 GST_END_TEST;
754
755 static Suite *
756 gst_uri_suite (void)
757 {
758   Suite *s = suite_create ("GstURI");
759   TCase *tc_chain = tcase_create ("uri");
760
761   tcase_set_timeout (tc_chain, 20);
762
763   suite_add_tcase (s, tc_chain);
764   tcase_add_test (tc_chain, test_protocol_case);
765   tcase_add_test (tc_chain, test_uri_get_location);
766   tcase_add_test (tc_chain, test_uri_misc);
767   tcase_add_test (tc_chain, test_element_make_from_uri);
768 #ifdef G_OS_WIN32
769   tcase_add_test (tc_chain, test_win32_uri);
770 #endif
771   tcase_add_test (tc_chain, test_url_parsing);
772   tcase_add_test (tc_chain, test_url_normalization);
773   tcase_add_test (tc_chain, test_url_joining);
774   tcase_add_test (tc_chain, test_url_equality);
775   tcase_add_test (tc_chain, test_url_constructors);
776   tcase_add_test (tc_chain, test_url_get_set);
777
778   return s;
779 }
780
781 GST_CHECK_MAIN (gst_uri);