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 #define COMMON_URI_TESTS \
233 /* VALID URIS. PARSING AND PRINTING OF THESE SHOULD NOT CHANGE */ \
236 {"scheme", NULL, NULL, GST_URI_NO_PORT, NULL, {{NULL, NULL}}, NULL}}, \
238 {"scheme", NULL, NULL, GST_URI_NO_PORT, "path", {{NULL, NULL}}, NULL}}, \
240 {NULL, NULL, NULL, GST_URI_NO_PORT, "path", {{NULL, NULL}}, NULL}}, \
242 {NULL, NULL, NULL, GST_URI_NO_PORT, "/path", {{NULL, NULL}}, NULL}}, \
243 /* hostname/port */ \
244 {"scheme://hostname/path", \
245 {"scheme", NULL, "hostname", GST_URI_NO_PORT, "/path", {{NULL, NULL}}, \
247 {"scheme://hostname:123/path", \
248 {"scheme", NULL, "hostname", 123, "/path", {{NULL, NULL}}, NULL}}, \
249 /* ipv6 hostname/port */ \
250 {"scheme://[01:23:45:67:89:ab:cd:ef]/path", \
251 {"scheme", NULL, "01:23:45:67:89:ab:cd:ef", GST_URI_NO_PORT, "/path", \
252 {{NULL, NULL}}, NULL}}, \
253 {"scheme://[01:23:45:67:89:ab:cd:ef]:123/path", \
254 {"scheme", NULL, "01:23:45:67:89:ab:cd:ef", 123, "/path", {{NULL, \
256 /* query/fragment */ \
258 {NULL, NULL, NULL, GST_URI_NO_PORT, "path", {{"query", NULL}, {NULL, \
260 {"path?query=value", \
261 {NULL, NULL, NULL, GST_URI_NO_PORT, "path", {{"query", "value"}, {NULL, \
263 {"path?query#fragment", \
264 {NULL, NULL, NULL, GST_URI_NO_PORT, "path", {{"query", NULL}, {NULL, \
265 NULL}}, "fragment"}}, \
266 {"path?query=value#fragment", \
267 {NULL, NULL, NULL, GST_URI_NO_PORT, "path", {{"query", "value"}, {NULL, \
268 NULL}}, "fragment"}}, \
269 {"scheme:path?query#fragment", \
270 {"scheme", NULL, NULL, GST_URI_NO_PORT, "path", {{"query", NULL}, {NULL, \
271 NULL}}, "fragment"}}, \
273 {"scheme://hostname:123/path?query#fragment", \
274 {"scheme", NULL, "hostname", 123, "/path", {{"query", NULL}, {NULL, \
275 NULL}}, "fragment"}}, \
276 {"scheme://hostname:123/path?query=value#fragment", \
277 {"scheme", NULL, "hostname", 123, "/path", {{"query", "value"}, {NULL, \
278 NULL}}, "fragment"}}, \
279 {"scheme://hostname:123?query", \
280 {"scheme", NULL, "hostname", 123, NULL, {{"query", NULL}, {NULL, \
282 {"scheme://hostname:123?query=value", \
283 {"scheme", NULL, "hostname", 123, NULL, {{"query", "value"}, {NULL, \
285 {"scheme://hostname:123?query#fragment", \
286 {"scheme", NULL, "hostname", 123, NULL, {{"query", NULL}, {NULL, \
287 NULL}}, "fragment"}}, \
288 {"scheme://hostname:123?query=value#fragment", \
289 {"scheme", NULL, "hostname", 123, NULL, {{"query", "value"}, {NULL, \
290 NULL}}, "fragment"}}, \
292 {"scheme://userinfo@hostname", \
293 {"scheme", "userinfo", "hostname", GST_URI_NO_PORT, NULL, {{NULL, \
295 {"scheme://userinfo@hostname:123/path?query#fragment", \
296 {"scheme", "userinfo", "hostname", 123, "/path", {{"query", NULL}, \
297 {NULL, NULL}}, "fragment"}}, \
298 {"scheme://user:pass@hostname", \
299 {"scheme", "user:pass", "hostname", GST_URI_NO_PORT, NULL, {{NULL, \
301 {"scheme://user:pass@hostname:123/path?query#fragment", \
302 {"scheme", "user:pass", "hostname", 123, "/path", {{"query", NULL}, \
303 {NULL, NULL}}, "fragment"}}, \
304 /* FUNNY URIS. PARSING AND PRINTING OF THESE MAY CHANGE */ \
305 {"scheme:hostname:123/path?query#fragment", \
306 {"scheme", NULL, NULL, GST_URI_NO_PORT, "hostname:123/path", {{"query", \
307 NULL}, {NULL, NULL}}, "fragment"}}, \
308 {"scheme://:pass@hostname:123/path?query#fragment", \
309 {"scheme", ":pass", "hostname", 123, "/path", {{"query", NULL}, {NULL, \
310 NULL}}, "fragment"}}, \
311 /* Skip initial white space */ \
312 {" \f\n\r\t\vscheme:", \
313 {"scheme", NULL, NULL, GST_URI_NO_PORT, NULL, {{NULL, NULL}}, NULL}}, \
314 {" \f\n\r\t\vpath", \
315 {NULL, NULL, NULL, GST_URI_NO_PORT, "path", {{NULL, NULL}}, NULL}}, \
317 {"file://host/home/joe/foo.txt", \
318 {"file", NULL, "host", GST_URI_NO_PORT, "/home/joe/foo.txt", {{NULL, \
320 {"file:///home/joe/foo.txt", \
321 {"file", NULL, NULL, GST_URI_NO_PORT, "/home/joe/foo.txt", {{NULL, \
324 #define UNESCAPED_URI_TESTS \
325 /* Test cases for gst_uri_from_string */ \
326 {"scheme://user%20info@hostname", \
327 {"scheme", "user info", "hostname", GST_URI_NO_PORT, NULL, {{NULL, \
329 {"scheme://userinfo@hostname:123/path?query#frag%23ment", \
330 {"scheme", "userinfo", "hostname", 123, "/path", {{"query", NULL}, \
331 {NULL, NULL}}, "frag#ment"}}, \
332 {"scheme://us%3Aer:pass@hostname", \
333 {"scheme", "us:er:pass", "hostname", GST_URI_NO_PORT, NULL, {{NULL, \
335 {"scheme://us%3Aer:pa%3Ass@hostname:123/path?query#frag%23ment", \
336 {"scheme", "us:er:pa:ss", "hostname", 123, "/path", {{"query", NULL}, \
337 {NULL, NULL}}, "frag#ment"}},
339 #define ESCAPED_URI_TESTS \
340 /* Test cases for gst_uri_from_string_escaped */ \
341 {"scheme://user%20info@hostname", \
342 {"scheme", "user%20info", "hostname", GST_URI_NO_PORT, NULL, {{NULL, \
344 {"scheme://userinfo@hostname:123/path?query#frag%23ment", \
345 {"scheme", "userinfo", "hostname", 123, "/path", {{"query", NULL}, \
346 {NULL, NULL}}, "frag%23ment"}}, \
347 {"scheme://us%3Aer:pass@hostname", \
348 {"scheme", "us%3Aer:pass", "hostname", GST_URI_NO_PORT, NULL, {{NULL, \
350 {"scheme://us%3Aer:pa%3Ass@hostname:123/path?query#frag%23ment", \
351 {"scheme", "us%3Aer:pa%3Ass", "hostname", 123, "/path", {{"query", NULL}, \
352 {NULL, NULL}}, "frag%23ment"}},
355 static const struct URITest tests[] = {
356 COMMON_URI_TESTS UNESCAPED_URI_TESTS
359 static const gchar *unparsable_uri_tests[] = {
360 /* Path not started correctly */
361 "scheme://hostname:123path?query#fragment",
363 /* Brackets that don't close */
364 "scheme://[01:23:45:67:89:ab:cd:ef:123/path",
366 /* IPv6 hostname without brackets */
367 "scheme://01:23:45:67:89:ab:cd:ef:123/path",
370 GST_START_TEST (test_url_parsing)
377 for (i = 0; i < G_N_ELEMENTS (tests); i++) {
378 GST_DEBUG ("Testing URI '%s'", tests[i].str);
380 uri = gst_uri_from_string (tests[i].str);
381 fail_unless (uri != NULL);
382 fail_unless_equals_string (gst_uri_get_scheme (uri), tests[i].uri.scheme);
383 fail_unless_equals_string (gst_uri_get_userinfo (uri),
384 tests[i].uri.userinfo);
385 fail_unless_equals_string (gst_uri_get_host (uri), tests[i].uri.host);
386 fail_unless_equals_int (gst_uri_get_port (uri), tests[i].uri.port);
387 tmp_str = gst_uri_get_path (uri);
388 fail_unless_equals_string (tmp_str, tests[i].uri.path);
391 for (j = 0; j < 10; j++) {
392 if (!tests[i].uri.query[j].key)
395 if (tests[i].uri.query[j].value) {
396 fail_unless_equals_string (gst_uri_get_query_value (uri,
397 tests[i].uri.query[j].key), tests[i].uri.query[j].value);
399 fail_unless (gst_uri_query_has_key (uri, tests[i].uri.query[j].key));
402 list = gst_uri_get_query_keys (uri);
403 fail_unless_equals_int (j, g_list_length (list));
408 for (i = 0; i < G_N_ELEMENTS (unparsable_uri_tests); i++) {
409 GST_DEBUG ("Testing unparsable URI '%s'", unparsable_uri_tests[i]);
411 uri = gst_uri_from_string (unparsable_uri_tests[i]);
412 fail_unless (uri == NULL);
419 static const struct URITest escaped_tests[] = {
420 COMMON_URI_TESTS ESCAPED_URI_TESTS
423 GST_START_TEST (test_url_parsing_escaped)
430 for (i = 0; i < G_N_ELEMENTS (escaped_tests); i++) {
431 GST_DEBUG ("Testing URI '%s'", escaped_tests[i].str);
433 uri = gst_uri_from_string_escaped (escaped_tests[i].str);
434 fail_unless (uri != NULL);
435 fail_unless_equals_string (gst_uri_get_scheme (uri),
436 escaped_tests[i].uri.scheme);
437 fail_unless_equals_string (gst_uri_get_userinfo (uri),
438 escaped_tests[i].uri.userinfo);
439 fail_unless_equals_string (gst_uri_get_host (uri),
440 escaped_tests[i].uri.host);
441 fail_unless_equals_int (gst_uri_get_port (uri), escaped_tests[i].uri.port);
442 tmp_str = gst_uri_get_path (uri);
443 fail_unless_equals_string (tmp_str, escaped_tests[i].uri.path);
446 for (j = 0; j < 10; j++) {
447 if (!escaped_tests[i].uri.query[j].key)
450 if (escaped_tests[i].uri.query[j].value) {
451 fail_unless_equals_string (gst_uri_get_query_value (uri,
452 escaped_tests[i].uri.query[j].key),
453 escaped_tests[i].uri.query[j].value);
455 fail_unless (gst_uri_query_has_key (uri,
456 escaped_tests[i].uri.query[j].key));
459 list = gst_uri_get_query_keys (uri);
460 fail_unless_equals_int (j, g_list_length (list));
465 for (i = 0; i < G_N_ELEMENTS (unparsable_uri_tests); i++) {
466 GST_DEBUG ("Testing unparsable URI '%s'", unparsable_uri_tests[i]);
468 uri = gst_uri_from_string (unparsable_uri_tests[i]);
469 fail_unless (uri == NULL);
475 static const struct URITest url_presenting_tests[] = {
476 /* check all URI elements present */
477 {.uri = {"scheme", "user:pass", "host", 1234, "/path/to/dir",
478 {{"query", NULL}, {"key", "value"}}, "fragment"},
480 "scheme://user:pass@host:1234/path/to/dir?key=value&query#fragment"},
481 /* IPv6 literal should render in square brackets */
482 {.uri = {"scheme", "user:pass", "12:34:56:78:9a:bc:de:f0", 1234,
483 "/path/to/dir", {{"query", "value"}}, "fragment"},
485 "scheme://user:pass@[12:34:56:78:9a:bc:de:f0]:1234/path/to/dir?query=value#fragment"},
488 GST_START_TEST (test_url_presenting)
494 for (i = 0; i < G_N_ELEMENTS (url_presenting_tests); i++) {
495 uri = gst_uri_new (url_presenting_tests[i].uri.scheme,
496 url_presenting_tests[i].uri.userinfo,
497 url_presenting_tests[i].uri.host,
498 url_presenting_tests[i].uri.port,
499 url_presenting_tests[i].uri.path,
500 NULL, url_presenting_tests[i].uri.fragment);
501 fail_unless (uri != NULL);
502 for (j = 0; j < 10; j++) {
503 if (!url_presenting_tests[i].uri.query[j].key)
506 fail_unless (gst_uri_set_query_value (uri,
507 url_presenting_tests[i].uri.query[j].key,
508 url_presenting_tests[i].uri.query[j].value));
511 result = gst_uri_to_string (uri);
512 fail_unless_equals_string (result, url_presenting_tests[i].str);
520 GST_START_TEST (test_url_normalization)
527 ("ScHeMe://User:P%61ss@HOST.%63om:1234/path/./from/../to%7d/item%2dobj?qu%65ry=something#fr%61gment");
528 fail_unless (gst_uri_normalize (url));
529 fail_unless_equals_string (gst_uri_get_scheme (url), "scheme");
530 fail_unless_equals_string (gst_uri_get_userinfo (url), "User:Pass");
531 fail_unless_equals_string (gst_uri_get_host (url), "host.com");
532 tmp_str = gst_uri_get_path (url);
533 fail_unless_equals_string (tmp_str, "/path/to}/item-obj");
535 fail_unless (gst_uri_query_has_key (url, "query"));
536 fail_unless_equals_string (gst_uri_get_query_value (url, "query"),
538 fail_unless_equals_string (gst_uri_get_fragment (url), "fragment");
544 GST_START_TEST (test_url_joining)
546 GstUri *base, *rel, *joined;
551 ("http://example.com/path/to/dir/filename.html#fragment");
553 /* test change of fragment only */
554 rel = gst_uri_from_string ("#new_frag");
555 joined = gst_uri_join (base, rel);
556 l = gst_uri_to_string (joined);
557 fail_unless_equals_string (l,
558 "http://example.com/path/to/dir/filename.html#new_frag");
560 gst_uri_unref (joined);
563 /* test addition of new query string */
564 rel = gst_uri_from_string ("?key=val");
565 joined = gst_uri_join (base, rel);
566 l = gst_uri_to_string (joined);
567 fail_unless_equals_string (l,
568 "http://example.com/path/to/dir/filename.html?key=val");
570 gst_uri_unref (joined);
573 /* test new base filename */
574 rel = gst_uri_from_string ("new_filename.xml");
575 joined = gst_uri_join (base, rel);
576 l = gst_uri_to_string (joined);
577 fail_unless_equals_string (l,
578 "http://example.com/path/to/dir/new_filename.xml");
580 gst_uri_unref (joined);
583 /* test relative file same directory */
584 rel = gst_uri_from_string ("./new_filename.xml");
585 joined = gst_uri_join (base, rel);
586 l = gst_uri_to_string (joined);
587 fail_unless_equals_string (l,
588 "http://example.com/path/to/dir/new_filename.xml");
590 gst_uri_unref (joined);
593 /* test relative file parent directory */
594 rel = gst_uri_from_string ("../new_filename.xml");
595 joined = gst_uri_join (base, rel);
596 l = gst_uri_to_string (joined);
597 fail_unless_equals_string (l, "http://example.com/path/to/new_filename.xml");
599 gst_uri_unref (joined);
602 /* test relative file grandparent directory */
603 rel = gst_uri_from_string ("../../new_filename.xml");
604 joined = gst_uri_join (base, rel);
605 l = gst_uri_to_string (joined);
606 fail_unless_equals_string (l, "http://example.com/path/new_filename.xml");
608 gst_uri_unref (joined);
611 /* test relative file root directory */
612 rel = gst_uri_from_string ("../../../new_filename.xml");
613 joined = gst_uri_join (base, rel);
614 l = gst_uri_to_string (joined);
615 fail_unless_equals_string (l, "http://example.com/new_filename.xml");
617 gst_uri_unref (joined);
620 /* test relative file beyond root directory */
621 rel = gst_uri_from_string ("../../../../new_filename.xml");
622 joined = gst_uri_join (base, rel);
623 l = gst_uri_to_string (joined);
624 fail_unless_equals_string (l, "http://example.com/new_filename.xml");
626 gst_uri_unref (joined);
629 /* test add subdirectory */
630 rel = gst_uri_from_string ("subdir/new_filename.xml");
631 joined = gst_uri_join (base, rel);
632 l = gst_uri_to_string (joined);
633 fail_unless_equals_string (l,
634 "http://example.com/path/to/dir/subdir/new_filename.xml");
636 gst_uri_unref (joined);
639 /* test change directory */
640 rel = gst_uri_from_string ("../subdir/new_filename.xml");
641 joined = gst_uri_join (base, rel);
642 l = gst_uri_to_string (joined);
643 fail_unless_equals_string (l,
644 "http://example.com/path/to/subdir/new_filename.xml");
646 gst_uri_unref (joined);
649 gst_uri_unref (base);
651 /* change base for path ending in directory */
652 base = gst_uri_from_string ("http://example.com/path/to/dir/");
654 /* test adding file to directory */
655 rel = gst_uri_from_string ("new_filename.xml");
656 joined = gst_uri_join (base, rel);
657 l = gst_uri_to_string (joined);
658 fail_unless_equals_string (l,
659 "http://example.com/path/to/dir/new_filename.xml");
661 gst_uri_unref (joined);
664 /* test adding file to directory using relative path */
665 rel = gst_uri_from_string ("./new_filename.xml");
666 joined = gst_uri_join (base, rel);
667 l = gst_uri_to_string (joined);
668 fail_unless_equals_string (l,
669 "http://example.com/path/to/dir/new_filename.xml");
671 gst_uri_unref (joined);
674 /* test filename in parent directory */
675 rel = gst_uri_from_string ("../new_filename.xml");
676 joined = gst_uri_join (base, rel);
677 l = gst_uri_to_string (joined);
678 fail_unless_equals_string (l, "http://example.com/path/to/new_filename.xml");
680 gst_uri_unref (joined);
683 /* test path ending in '../' */
684 rel = gst_uri_from_string ("one/two/../");
685 joined = gst_uri_join (base, rel);
686 l = gst_uri_to_string (joined);
687 fail_unless_equals_string (l, "http://example.com/path/to/dir/one/");
689 gst_uri_unref (joined);
692 /* test path ending in '..' Result should be the same as when ending in '../' */
693 rel = gst_uri_from_string ("one/two/..");
694 joined = gst_uri_join (base, rel);
695 l = gst_uri_to_string (joined);
696 fail_unless_equals_string (l, "http://example.com/path/to/dir/one/");
698 gst_uri_unref (joined);
701 /* test replace with absolute */
702 rel = gst_uri_from_string ("https://ssl.example.com/new_filename.xml");
703 joined = gst_uri_join (base, rel);
704 l = gst_uri_to_string (joined);
705 fail_unless_equals_string (l, "https://ssl.example.com/new_filename.xml");
707 gst_uri_unref (joined);
710 gst_uri_unref (base);
715 GST_START_TEST (test_url_equality)
721 ("ScHeMe://User:Pass@HOST.com:1234/path/./from/../to%7d/item%2dobj?query=something#fragment");
726 ("scheme://User:Pass@host.com:1234/path/to%7D/item-obj?query=something#fragment");
727 fail_unless (gst_uri_equal (url1, url2));
728 fail_unless (gst_uri_equal (url2, url1));
729 gst_uri_unref (url2);
731 /* different fragment */
734 ("scheme://User:Pass@host.com:1234/path/to%7D/item-obj?query=something#different-fragment");
735 fail_unless (!gst_uri_equal (url1, url2));
736 gst_uri_unref (url2);
738 /* different query */
741 ("scheme://User:Pass@host.com:1234/path/to%7D/item-obj?query=different-something#fragment");
742 fail_unless (!gst_uri_equal (url1, url2));
743 gst_uri_unref (url2);
748 ("scheme://User:Pass@host.com:1234/path/to%7D/different-item-obj?query=something#fragment");
749 fail_unless (!gst_uri_equal (url1, url2));
750 gst_uri_unref (url2);
755 ("scheme://User:Pass@host.com:4321/path/to%7D/item-obj?query=something#fragment");
756 fail_unless (!gst_uri_equal (url1, url2));
757 gst_uri_unref (url2);
762 ("scheme://User:Pass@different-host.com:1234/path/to%7D/item-obj?query=something#fragment");
763 fail_unless (!gst_uri_equal (url1, url2));
764 gst_uri_unref (url2);
766 /* different userinfo */
769 ("scheme://Different-User:Pass@host.com:1234/path/to%7D/item-obj?query=something#fragment");
770 fail_unless (!gst_uri_equal (url1, url2));
771 gst_uri_unref (url2);
773 /* different scheme */
776 ("different+scheme://User:Pass@host.com:1234/path/to%7D/item-obj?query=something#fragment");
777 fail_unless (!gst_uri_equal (url1, url2));
778 gst_uri_unref (url2);
780 /* different (no scheme) */
783 ("//User:Pass@host.com:1234/path/to%7D/item-obj?query=something#fragment");
784 fail_unless (!gst_uri_equal (url1, url2));
785 gst_uri_unref (url2);
787 /* different (no userinfo) */
790 ("scheme://host.com:1234/path/to%7D/item-obj?query=something#fragment");
791 fail_unless (!gst_uri_equal (url1, url2));
792 gst_uri_unref (url2);
794 /* different (no host) */
797 ("scheme://User:Pass@:1234/path/to%7D/item-obj?query=something#fragment");
798 fail_unless (!gst_uri_equal (url1, url2));
799 gst_uri_unref (url2);
801 /* different (no port) */
804 ("scheme://User:Pass@host.com/path/to%7D/item-obj?query=something#fragment");
805 fail_unless (!gst_uri_equal (url1, url2));
806 gst_uri_unref (url2);
808 /* different (no path) */
811 ("scheme://User:Pass@host.com:1234?query=something#fragment");
812 fail_unless (!gst_uri_equal (url1, url2));
813 gst_uri_unref (url2);
815 /* different (no query) */
818 ("scheme://User:Pass@host.com:1234/path/to%7D/item-obj#fragment");
819 fail_unless (!gst_uri_equal (url1, url2));
820 gst_uri_unref (url2);
822 /* different (no fragment) */
825 ("scheme://User:Pass@host.com:1234/path/to%7D/item-obj?query=something");
826 fail_unless (!gst_uri_equal (url1, url2));
827 gst_uri_unref (url2);
829 /* compare two NULL uris */
830 fail_unless (gst_uri_equal (NULL, NULL));
832 /* compare same object */
833 fail_unless (gst_uri_equal (url1, url1));
835 /* compare one NULL and one non-NULL uri */
836 fail_unless (!gst_uri_equal (url1, NULL));
837 fail_unless (!gst_uri_equal (NULL, url1));
839 gst_uri_unref (url1);
844 GST_START_TEST (test_url_constructors)
848 GHashTable *tmp_table;
851 gst_uri_new ("scheme", "userinfo", "hostname", 1234, "/path/to/file",
852 "query", "fragment");
853 fail_unless_equals_string (gst_uri_get_scheme (url1), "scheme");
854 fail_unless_equals_string (gst_uri_get_userinfo (url1), "userinfo");
855 fail_unless_equals_string (gst_uri_get_host (url1), "hostname");
856 fail_unless (gst_uri_get_port (url1) == 1234);
857 tmp_str = gst_uri_get_path (url1);
858 fail_unless_equals_string (tmp_str, "/path/to/file");
860 tmp_table = gst_uri_get_query_table (url1);
861 fail_unless (g_hash_table_size (tmp_table) == 1);
862 fail_unless (g_hash_table_contains (tmp_table, "query"));
863 fail_unless (g_hash_table_lookup (tmp_table, "query") == NULL);
864 g_hash_table_unref (tmp_table);
865 fail_unless_equals_string (gst_uri_get_fragment (url1), "fragment");
866 tmp_str = gst_uri_to_string (url1);
867 fail_unless_equals_string (tmp_str,
868 "scheme://userinfo@hostname:1234/path/to/file?query#fragment");
872 gst_uri_new_with_base (url1, NULL, NULL, NULL, GST_URI_NO_PORT,
873 "new_file", NULL, NULL);
874 fail_unless_equals_string (gst_uri_get_scheme (url2), "scheme");
875 fail_unless_equals_string (gst_uri_get_userinfo (url2), "userinfo");
876 fail_unless_equals_string (gst_uri_get_host (url2), "hostname");
877 fail_unless (gst_uri_get_port (url2) == 1234);
878 tmp_str = gst_uri_get_path (url2);
879 fail_unless_equals_string (tmp_str, "/path/to/new_file");
881 fail_unless (gst_uri_get_query_table (url2) == NULL);
882 fail_unless (gst_uri_get_fragment (url2) == NULL);
883 tmp_str = gst_uri_to_string (url2);
884 fail_unless_equals_string (tmp_str,
885 "scheme://userinfo@hostname:1234/path/to/new_file");
887 gst_uri_unref (url2);
889 url2 = gst_uri_from_string_with_base (url1, "/a/new/path/to/file");
890 fail_unless_equals_string (gst_uri_get_scheme (url2), "scheme");
891 fail_unless_equals_string (gst_uri_get_userinfo (url2), "userinfo");
892 fail_unless_equals_string (gst_uri_get_host (url2), "hostname");
893 fail_unless (gst_uri_get_port (url2) == 1234);
894 tmp_str = gst_uri_get_path (url2);
895 fail_unless_equals_string (tmp_str, "/a/new/path/to/file");
897 fail_unless (gst_uri_get_query_table (url2) == NULL);
898 fail_unless (gst_uri_get_fragment (url2) == NULL);
899 tmp_str = gst_uri_to_string (url2);
900 fail_unless_equals_string (tmp_str,
901 "scheme://userinfo@hostname:1234/a/new/path/to/file");
903 gst_uri_unref (url2);
905 url2 = gst_uri_from_string_with_base (url1, "http://foobar.com/bla");
906 fail_unless_equals_string (gst_uri_get_scheme (url2), "http");
907 fail_unless_equals_string (gst_uri_get_host (url2), "foobar.com");
908 fail_unless (gst_uri_get_port (url2) == 0);
909 tmp_str = gst_uri_get_path (url2);
910 fail_unless_equals_string (tmp_str, "/bla");
912 fail_unless (gst_uri_get_query_table (url2) == NULL);
913 fail_unless (gst_uri_get_fragment (url2) == NULL);
914 tmp_str = gst_uri_to_string (url2);
915 fail_unless_equals_string (tmp_str, "http://foobar.com/bla");
917 gst_uri_unref (url2);
919 url2 = gst_uri_copy (url1);
920 fail_unless (gst_uri_equal (url1, url2));
921 fail_unless (gst_uri_set_query_value (url2, "key", "value"));
922 fail_unless (!gst_uri_equal (url1, url2));
923 gst_uri_unref (url2);
925 gst_uri_unref (url1);
930 GST_START_TEST (test_url_get_set)
936 url = gst_uri_from_string ("scheme://hostname/path/to/file?query#fragment");
938 fail_unless (gst_uri_set_scheme (url, "new+scheme"));
939 fail_unless_equals_string (gst_uri_get_scheme (url), "new+scheme");
940 tmp_str = gst_uri_to_string (url);
941 fail_unless_equals_string (tmp_str,
942 "new+scheme://hostname/path/to/file?query#fragment");
945 fail_unless (gst_uri_set_scheme (url, NULL));
946 fail_unless (gst_uri_get_scheme (url) == NULL);
947 tmp_str = gst_uri_to_string (url);
948 fail_unless_equals_string (tmp_str, "//hostname/path/to/file?query#fragment");
951 fail_unless (!gst_uri_set_scheme (NULL, "fail"));
952 fail_unless (gst_uri_set_scheme (NULL, NULL));
954 fail_unless (gst_uri_set_userinfo (url, "username:password"));
955 fail_unless_equals_string (gst_uri_get_userinfo (url), "username:password");
956 tmp_str = gst_uri_to_string (url);
957 fail_unless_equals_string (tmp_str,
958 "//username:password@hostname/path/to/file?query#fragment");
961 fail_unless (gst_uri_set_userinfo (url, NULL));
962 fail_unless (gst_uri_get_userinfo (url) == NULL);
963 tmp_str = gst_uri_to_string (url);
964 fail_unless_equals_string (tmp_str, "//hostname/path/to/file?query#fragment");
967 fail_unless (!gst_uri_set_userinfo (NULL, "fail"));
968 fail_unless (gst_uri_set_userinfo (NULL, NULL));
970 fail_unless (gst_uri_set_host (url, NULL));
971 fail_unless (gst_uri_get_host (url) == NULL);
972 tmp_str = gst_uri_to_string (url);
973 fail_unless_equals_string (tmp_str, "/path/to/file?query#fragment");
976 fail_unless (gst_uri_set_host (url, "example.com"));
977 fail_unless_equals_string (gst_uri_get_host (url), "example.com");
978 tmp_str = gst_uri_to_string (url);
979 fail_unless_equals_string (tmp_str,
980 "//example.com/path/to/file?query#fragment");
983 fail_unless (!gst_uri_set_host (NULL, "fail"));
984 fail_unless (gst_uri_set_host (NULL, NULL));
986 fail_unless (gst_uri_set_port (url, 12345));
987 fail_unless (gst_uri_get_port (url) == 12345);
988 tmp_str = gst_uri_to_string (url);
989 fail_unless_equals_string (tmp_str,
990 "//example.com:12345/path/to/file?query#fragment");
993 fail_unless (gst_uri_set_port (url, GST_URI_NO_PORT));
994 fail_unless (gst_uri_get_port (url) == GST_URI_NO_PORT);
995 tmp_str = gst_uri_to_string (url);
996 fail_unless_equals_string (tmp_str,
997 "//example.com/path/to/file?query#fragment");
1000 fail_unless (!gst_uri_set_port (NULL, 1234));
1001 fail_unless (gst_uri_set_port (NULL, GST_URI_NO_PORT));
1003 fail_unless (gst_uri_append_path_segment (url, "here"));
1004 tmp_str = gst_uri_to_string (url);
1005 fail_unless_equals_string (tmp_str,
1006 "//example.com/path/to/file/here?query#fragment");
1009 fail_unless (!gst_uri_append_path_segment (NULL, "fail"));
1010 fail_unless (gst_uri_append_path_segment (NULL, NULL));
1012 fail_unless (gst_uri_append_path (url, "../there"));
1013 tmp_str = gst_uri_to_string (url);
1014 fail_unless_equals_string (tmp_str,
1015 "//example.com/path/to/file/here/../there?query#fragment");
1018 fail_unless (!gst_uri_append_path (NULL, "fail"));
1019 fail_unless (gst_uri_append_path (NULL, NULL));
1021 gst_uri_normalize (url);
1023 tmp_list = gst_uri_get_path_segments (url);
1024 fail_unless (tmp_list != NULL);
1025 tmp_list = g_list_append (tmp_list, g_strdup ("segment"));
1026 tmp_str = gst_uri_to_string (url);
1027 fail_unless_equals_string (tmp_str,
1028 "//example.com/path/to/file/there?query#fragment");
1030 fail_unless (gst_uri_set_path_segments (url, tmp_list));
1031 tmp_str = gst_uri_to_string (url);
1032 fail_unless_equals_string (tmp_str,
1033 "//example.com/path/to/file/there/segment?query#fragment");
1036 tmp_list = g_list_append (NULL, g_strdup ("test"));
1037 fail_unless (!gst_uri_set_path_segments (NULL, tmp_list));
1038 fail_unless (gst_uri_set_path_segments (NULL, NULL));
1040 fail_unless (gst_uri_set_query_value (url, "key", "value"));
1041 tmp_str = gst_uri_to_string (url);
1042 fail_unless_equals_string (tmp_str,
1043 "//example.com/path/to/file/there/segment?key=value&query#fragment");
1046 fail_unless (gst_uri_set_query_value (url, "key", NULL));
1047 tmp_str = gst_uri_to_string (url);
1048 fail_unless_equals_string (tmp_str,
1049 "//example.com/path/to/file/there/segment?key&query#fragment");
1052 fail_unless (!gst_uri_set_query_value (NULL, "key", "value"));
1054 fail_unless (gst_uri_remove_query_key (url, "key"));
1055 tmp_str = gst_uri_to_string (url);
1056 fail_unless_equals_string (tmp_str,
1057 "//example.com/path/to/file/there/segment?query#fragment");
1060 fail_unless (!gst_uri_remove_query_key (url, "key"));
1061 fail_unless (!gst_uri_remove_query_key (NULL, "key"));
1063 fail_unless (gst_uri_set_fragment (url, NULL));
1064 fail_unless (gst_uri_get_fragment (url) == NULL);
1065 tmp_str = gst_uri_to_string (url);
1066 fail_unless_equals_string (tmp_str,
1067 "//example.com/path/to/file/there/segment?query");
1070 fail_unless (gst_uri_set_fragment (url, "tag"));
1071 fail_unless_equals_string (gst_uri_get_fragment (url), "tag");
1072 tmp_str = gst_uri_to_string (url);
1073 fail_unless_equals_string (tmp_str,
1074 "//example.com/path/to/file/there/segment?query#tag");
1077 fail_unless (!gst_uri_set_fragment (NULL, "can't set if no URI"));
1078 fail_unless (gst_uri_set_fragment (NULL, NULL));
1080 gst_uri_unref (url);
1085 GST_START_TEST (test_url_get_media_fragment_table)
1091 /* Examples at https://www.w3.org/TR/media-frags/#processing-media-fragment-uri */
1094 url = gst_uri_from_string ("http://foo/var/file#t=1");
1095 table = gst_uri_get_media_fragment_table (url);
1096 fail_unless (table);
1097 fail_unless (g_hash_table_size (table) == 1);
1098 fail_unless (g_hash_table_lookup_extended (table, "t", NULL,
1100 fail_unless_equals_string ("1", val);
1101 g_hash_table_unref (table);
1102 gst_uri_unref (url);
1104 /* NOTE: Media Fragments URI 1.0 (W3C) is saying that
1105 * "Multiple occurrences of the same dimension: only the last valid occurrence
1106 * of a dimension (e.g. t=10 in #t=2&t=10) is interpreted and all previous
1107 * occurrences (valid or invalid) SHOULD be ignored by the user agent"
1109 /* TEST "t=1&t=2" */
1110 url = gst_uri_from_string ("http://foo/var/file#t=1&t=2");
1111 table = gst_uri_get_media_fragment_table (url);
1112 fail_unless (table);
1113 fail_unless (g_hash_table_size (table) == 1);
1114 fail_unless (g_hash_table_lookup_extended (table, "t", NULL,
1116 fail_unless_equals_string ("2", val);
1117 g_hash_table_unref (table);
1118 gst_uri_unref (url);
1121 url = gst_uri_from_string ("http://foo/var/file#a=b=c");
1122 table = gst_uri_get_media_fragment_table (url);
1123 fail_unless (table);
1124 fail_unless (g_hash_table_size (table) == 1);
1125 fail_unless (g_hash_table_lookup_extended (table, "a", NULL,
1127 fail_unless_equals_string ("b=c", val);
1128 g_hash_table_unref (table);
1129 gst_uri_unref (url);
1132 url = gst_uri_from_string ("http://foo/var/file#a&b=c");
1133 table = gst_uri_get_media_fragment_table (url);
1134 fail_unless (table);
1135 fail_unless (g_hash_table_size (table) == 2);
1136 fail_unless (g_hash_table_lookup_extended (table, "a", NULL,
1138 fail_unless (val == NULL);
1139 fail_unless (g_hash_table_lookup_extended (table, "b", NULL,
1141 fail_unless_equals_string ("c", val);
1142 g_hash_table_unref (table);
1143 gst_uri_unref (url);
1145 /* TEST "%74=%6ept%3A%310" */
1146 url = gst_uri_from_string ("http://foo/var/file#%74=%6ept%3A%310");
1147 table = gst_uri_get_media_fragment_table (url);
1148 fail_unless (table);
1149 fail_unless (g_hash_table_size (table) == 1);
1150 fail_unless (g_hash_table_lookup_extended (table, "t", NULL,
1152 fail_unless_equals_string ("npt:10", val);
1153 g_hash_table_unref (table);
1154 gst_uri_unref (url);
1159 GST_START_TEST (test_url_unescape_equals_in_http_query)
1162 gchar *query_string;
1166 ("http://abc.manifest?token=exp=123~acl=/QualityLevels(*~hmac=0cb");
1168 fail_unless_equals_string (gst_uri_get_scheme (url), "http");
1169 query_string = gst_uri_get_query_string (url);
1170 fail_unless_equals_string (query_string,
1171 "token=exp=123~acl=/QualityLevels(*~hmac=0cb");
1172 g_free (query_string);
1173 fail_unless (gst_uri_query_has_key (url, "token"));
1174 fail_unless_equals_string (gst_uri_get_query_value (url, "token"),
1175 "exp=123~acl=/QualityLevels(*~hmac=0cb");
1176 gst_uri_unref (url);
1182 gst_uri_suite (void)
1184 Suite *s = suite_create ("GstURI");
1185 TCase *tc_chain = tcase_create ("uri");
1187 tcase_set_timeout (tc_chain, 20);
1189 suite_add_tcase (s, tc_chain);
1190 tcase_add_test (tc_chain, test_protocol_case);
1191 tcase_add_test (tc_chain, test_uri_get_location);
1192 #ifndef GST_REMOVE_DEPRECATED
1193 tcase_add_test (tc_chain, test_gst_uri_construct);
1195 tcase_add_test (tc_chain, test_uri_misc);
1196 tcase_add_test (tc_chain, test_element_make_from_uri);
1198 tcase_add_test (tc_chain, test_win32_uri);
1200 tcase_add_test (tc_chain, test_url_parsing);
1201 tcase_add_test (tc_chain, test_url_parsing_escaped);
1202 tcase_add_test (tc_chain, test_url_presenting);
1203 tcase_add_test (tc_chain, test_url_normalization);
1204 tcase_add_test (tc_chain, test_url_joining);
1205 tcase_add_test (tc_chain, test_url_equality);
1206 tcase_add_test (tc_chain, test_url_constructors);
1207 tcase_add_test (tc_chain, test_url_get_set);
1208 tcase_add_test (tc_chain, test_url_get_media_fragment_table);
1209 tcase_add_test (tc_chain, test_url_unescape_equals_in_http_query);
1214 GST_CHECK_MAIN (gst_uri);