1 /* GStreamer unit tests for GstURI
3 * Copyright (C) 2007 Tim-Philipp Müller <tim centricular net>
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.
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.
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.
24 #ifndef GST_REMOVE_DEPRECATED
25 #undef GST_DISABLE_DEPRECATED
28 #include <gst/check/gstcheck.h>
30 GST_START_TEST (test_protocol_case)
35 element = gst_element_make_from_uri (GST_URI_SRC, "file:///foo/bar", NULL,
38 /* no element? probably no registry, bail out */
39 if (element == NULL && err->code == GST_URI_ERROR_UNSUPPORTED_PROTOCOL) {
44 gst_object_unref (element);
45 element = gst_element_make_from_uri (GST_URI_SRC, "FILE:///foo/bar", NULL,
47 fail_unless (element != NULL,
48 "Got source for 'file://' URI but not for 'FILE://' URI");
49 gst_object_unref (element);
54 GST_START_TEST (test_uri_get_location)
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, "");
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");
71 l = gst_uri_get_location ("file:///path/to/file");
72 fail_unless (l != NULL);
73 fail_unless_equals_string (l, "/path/to/file");
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");
85 #ifndef GST_REMOVE_DEPRECATED
86 GST_START_TEST (test_gst_uri_construct)
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);
96 /* URI with no location should return empty string */
97 ASSERT_CRITICAL (l = gst_uri_construct ("protocol", NULL));
98 fail_unless (l == NULL);
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");
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");
120 GST_START_TEST (test_win32_uri)
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);
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);
151 #endif /* G_OS_WIN32 */
153 GST_START_TEST (test_uri_misc)
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"));
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"));
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"));
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"));
178 GST_START_TEST (test_element_make_from_uri)
183 element = gst_element_make_from_uri (GST_URI_SRC, "foo://", NULL, NULL);
184 fail_unless (element == NULL);
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);
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,
197 fail_unless (element == NULL);
198 fail_unless (err != NULL);
199 fail_unless (err->code == GST_URI_ERROR_BAD_URI);
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
222 const gchar *userinfo;
226 /* needs to be updated if more than 10 */
227 struct QueryValue query[10];
228 const gchar *fragment;
232 static const struct URITest tests[] = {
233 /* VALID URIS. PARSING AND PRINTING OF THESE SHOULD NOT CHANGE */
237 {"scheme", NULL, NULL, GST_URI_NO_PORT, NULL, {{NULL, NULL}}, NULL}},
240 {"scheme", NULL, NULL, GST_URI_NO_PORT, "path", {{NULL, NULL}}, NULL}},
243 {NULL, NULL, NULL, GST_URI_NO_PORT, "path", {{NULL, NULL}}, NULL}},
246 {NULL, NULL, NULL, GST_URI_NO_PORT, "/path", {{NULL, NULL}}, NULL}},
249 {"scheme://hostname/path",
250 {"scheme", NULL, "hostname", GST_URI_NO_PORT, "/path", {{NULL, NULL}},
253 {"scheme://hostname:123/path",
254 {"scheme", NULL, "hostname", 123, "/path", {{NULL, NULL}}, NULL}},
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}},
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,
267 {NULL, NULL, NULL, GST_URI_NO_PORT, "path", {{"query", NULL}, {NULL,
270 {NULL, NULL, NULL, GST_URI_NO_PORT, "path", {{"query", "value"}, {NULL,
273 {"path?query#fragment",
274 {NULL, NULL, NULL, GST_URI_NO_PORT, "path", {{"query", NULL}, {NULL,
275 NULL}}, "fragment"}},
277 {"path?query=value#fragment",
278 {NULL, NULL, NULL, GST_URI_NO_PORT, "path", {{"query", "value"}, {NULL,
279 NULL}}, "fragment"}},
281 {"scheme:path?query#fragment",
282 {"scheme", NULL, NULL, GST_URI_NO_PORT, "path", {{"query", NULL}, {NULL,
283 NULL}}, "fragment"}},
286 {"scheme://hostname:123/path?query#fragment",
287 {"scheme", NULL, "hostname", 123, "/path", {{"query", NULL}, {NULL,
288 NULL}}, "fragment"}},
290 {"scheme://hostname:123/path?query=value#fragment",
291 {"scheme", NULL, "hostname", 123, "/path", {{"query", "value"}, {NULL,
292 NULL}}, "fragment"}},
294 {"scheme://hostname:123?query",
295 {"scheme", NULL, "hostname", 123, NULL, {{"query", NULL}, {NULL,
298 {"scheme://hostname:123?query=value",
299 {"scheme", NULL, "hostname", 123, NULL, {{"query", "value"}, {NULL,
302 {"scheme://hostname:123?query#fragment",
303 {"scheme", NULL, "hostname", 123, NULL, {{"query", NULL}, {NULL,
304 NULL}}, "fragment"}},
306 {"scheme://hostname:123?query=value#fragment",
307 {"scheme", NULL, "hostname", 123, NULL, {{"query", "value"}, {NULL,
308 NULL}}, "fragment"}},
311 {"scheme://userinfo@hostname",
312 {"scheme", "userinfo", "hostname", GST_URI_NO_PORT, NULL, {{NULL,
315 {"scheme://userinfo@hostname:123/path?query#fragment",
316 {"scheme", "userinfo", "hostname", 123, "/path", {{"query", NULL},
317 {NULL, NULL}}, "fragment"}},
319 {"scheme://user:pass@hostname",
320 {"scheme", "user:pass", "hostname", GST_URI_NO_PORT, NULL, {{NULL,
323 {"scheme://user:pass@hostname:123/path?query#fragment",
324 {"scheme", "user:pass", "hostname", 123, "/path", {{"query", NULL},
325 {NULL, NULL}}, "fragment"}},
327 /* FUNNY URIS. PARSING AND PRINTING OF THESE MAY CHANGE */
329 {"scheme:hostname:123/path?query#fragment",
330 {"scheme", NULL, NULL, GST_URI_NO_PORT, "hostname:123/path", {{"query",
331 NULL}, {NULL, NULL}}, "fragment"}},
333 {"scheme://:pass@hostname:123/path?query#fragment",
334 {"scheme", ":pass", "hostname", 123, "/path", {{"query", NULL}, {NULL,
335 NULL}}, "fragment"}},
337 /* Skip initial white space */
338 {" \f\n\r\t\vscheme:",
339 {"scheme", NULL, NULL, GST_URI_NO_PORT, NULL, {{NULL, NULL}}, NULL}},
342 {NULL, NULL, NULL, GST_URI_NO_PORT, "path", {{NULL, NULL}}, NULL}},
345 {"file://host/home/joe/foo.txt",
346 {"file", NULL, "host", GST_URI_NO_PORT, "/home/joe/foo.txt", {{NULL,
348 {"file:///home/joe/foo.txt",
349 {"file", NULL, NULL, GST_URI_NO_PORT, "/home/joe/foo.txt", {{NULL,
353 static const gchar *unparsable_uri_tests[] = {
354 /* Path not started correctly */
355 "scheme://hostname:123path?query#fragment",
357 /* Brackets that don't close */
358 "scheme://[01:23:45:67:89:ab:cd:ef:123/path",
360 /* IPv6 hostname without brackets */
361 "scheme://01:23:45:67:89:ab:cd:ef:123/path",
364 GST_START_TEST (test_url_parsing)
371 for (i = 0; i < G_N_ELEMENTS (tests); i++) {
372 GST_DEBUG ("Testing URI '%s'", tests[i].str);
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);
385 for (j = 0; j < 10; j++) {
386 if (!tests[i].uri.query[j].key)
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);
393 fail_unless (gst_uri_query_has_key (uri, tests[i].uri.query[j].key));
396 list = gst_uri_get_query_keys (uri);
397 fail_unless_equals_int (j, g_list_length (list));
402 for (i = 0; i < G_N_ELEMENTS (unparsable_uri_tests); i++) {
403 GST_DEBUG ("Testing unparsable URI '%s'", unparsable_uri_tests[i]);
405 uri = gst_uri_from_string (unparsable_uri_tests[i]);
406 fail_unless (uri == NULL);
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"},
417 #if GLIB_CHECK_VERSION(2, 59, 0)
418 "scheme://user:pass@host:1234/path/to/dir?key=value&query#fragment"},
420 "scheme://user:pass@host:1234/path/to/dir?query&key=value#fragment"},
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"},
427 "scheme://user:pass@[12:34:56:78:9a:bc:de:f0]:1234/path/to/dir?query=value#fragment"},
430 GST_START_TEST (test_url_presenting)
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)
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));
453 result = gst_uri_to_string (uri);
454 fail_unless_equals_string (result, url_presenting_tests[i].str);
462 GST_START_TEST (test_url_normalization)
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");
477 fail_unless (gst_uri_query_has_key (url, "query"));
478 fail_unless_equals_string (gst_uri_get_query_value (url, "query"),
480 fail_unless_equals_string (gst_uri_get_fragment (url), "fragment");
486 GST_START_TEST (test_url_joining)
488 GstUri *base, *rel, *joined;
493 ("http://example.com/path/to/dir/filename.html#fragment");
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");
502 gst_uri_unref (joined);
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");
512 gst_uri_unref (joined);
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");
522 gst_uri_unref (joined);
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");
532 gst_uri_unref (joined);
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");
541 gst_uri_unref (joined);
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");
550 gst_uri_unref (joined);
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");
559 gst_uri_unref (joined);
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");
568 gst_uri_unref (joined);
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");
578 gst_uri_unref (joined);
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");
588 gst_uri_unref (joined);
591 gst_uri_unref (base);
593 /* change base for path ending in directory */
594 base = gst_uri_from_string ("http://example.com/path/to/dir/");
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");
603 gst_uri_unref (joined);
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");
613 gst_uri_unref (joined);
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");
622 gst_uri_unref (joined);
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/");
631 gst_uri_unref (joined);
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/");
640 gst_uri_unref (joined);
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");
649 gst_uri_unref (joined);
652 gst_uri_unref (base);
657 GST_START_TEST (test_url_equality)
663 ("ScHeMe://User:Pass@HOST.com:1234/path/./from/../to%7d/item%2dobj?query=something#fragment");
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);
673 /* different fragment */
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);
680 /* different query */
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);
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);
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);
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);
708 /* different userinfo */
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);
715 /* different scheme */
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);
722 /* different (no scheme) */
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);
729 /* different (no userinfo) */
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);
736 /* different (no host) */
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);
743 /* different (no port) */
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);
750 /* different (no path) */
753 ("scheme://User:Pass@host.com:1234?query=something#fragment");
754 fail_unless (!gst_uri_equal (url1, url2));
755 gst_uri_unref (url2);
757 /* different (no query) */
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);
764 /* different (no fragment) */
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);
771 /* compare two NULL uris */
772 fail_unless (gst_uri_equal (NULL, NULL));
774 /* compare same object */
775 fail_unless (gst_uri_equal (url1, url1));
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));
781 gst_uri_unref (url1);
786 GST_START_TEST (test_url_constructors)
790 GHashTable *tmp_table;
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");
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");
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");
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");
829 gst_uri_unref (url2);
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");
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");
845 gst_uri_unref (url2);
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");
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");
859 gst_uri_unref (url2);
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);
867 gst_uri_unref (url1);
872 GST_START_TEST (test_url_get_set)
878 url = gst_uri_from_string ("scheme://hostname/path/to/file?query#fragment");
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");
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");
893 fail_unless (!gst_uri_set_scheme (NULL, "fail"));
894 fail_unless (gst_uri_set_scheme (NULL, NULL));
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");
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");
909 fail_unless (!gst_uri_set_userinfo (NULL, "fail"));
910 fail_unless (gst_uri_set_userinfo (NULL, NULL));
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");
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");
925 fail_unless (!gst_uri_set_host (NULL, "fail"));
926 fail_unless (gst_uri_set_host (NULL, NULL));
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");
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");
942 fail_unless (!gst_uri_set_port (NULL, 1234));
943 fail_unless (gst_uri_set_port (NULL, GST_URI_NO_PORT));
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");
951 fail_unless (!gst_uri_append_path_segment (NULL, "fail"));
952 fail_unless (gst_uri_append_path_segment (NULL, NULL));
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");
960 fail_unless (!gst_uri_append_path (NULL, "fail"));
961 fail_unless (gst_uri_append_path (NULL, NULL));
963 gst_uri_normalize (url);
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");
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");
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));
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");
988 fail_unless_equals_string (tmp_str,
989 "//example.com/path/to/file/there/segment?query&key=value#fragment");
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");
999 fail_unless_equals_string (tmp_str,
1000 "//example.com/path/to/file/there/segment?query&key#fragment");
1004 fail_unless (!gst_uri_set_query_value (NULL, "key", "value"));
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");
1012 fail_unless (!gst_uri_remove_query_key (url, "key"));
1013 fail_unless (!gst_uri_remove_query_key (NULL, "key"));
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");
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");
1029 fail_unless (!gst_uri_set_fragment (NULL, "can't set if no URI"));
1030 fail_unless (gst_uri_set_fragment (NULL, NULL));
1032 gst_uri_unref (url);
1037 GST_START_TEST (test_url_get_media_fragment_table)
1043 /* Examples at https://www.w3.org/TR/media-frags/#processing-media-fragment-uri */
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,
1052 fail_unless_equals_string ("1", val);
1053 g_hash_table_unref (table);
1054 gst_uri_unref (url);
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"
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,
1068 fail_unless_equals_string ("2", val);
1069 g_hash_table_unref (table);
1070 gst_uri_unref (url);
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,
1079 fail_unless_equals_string ("b=c", val);
1080 g_hash_table_unref (table);
1081 gst_uri_unref (url);
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,
1090 fail_unless (val == NULL);
1091 fail_unless (g_hash_table_lookup_extended (table, "b", NULL,
1093 fail_unless_equals_string ("c", val);
1094 g_hash_table_unref (table);
1095 gst_uri_unref (url);
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,
1104 fail_unless_equals_string ("npt:10", val);
1105 g_hash_table_unref (table);
1106 gst_uri_unref (url);
1112 gst_uri_suite (void)
1114 Suite *s = suite_create ("GstURI");
1115 TCase *tc_chain = tcase_create ("uri");
1117 tcase_set_timeout (tc_chain, 20);
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);
1125 tcase_add_test (tc_chain, test_uri_misc);
1126 tcase_add_test (tc_chain, test_element_make_from_uri);
1128 tcase_add_test (tc_chain, test_win32_uri);
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);
1142 GST_CHECK_MAIN (gst_uri);