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 #if GLIB_CHECK_VERSION(2, 59, 0)
481 "scheme://user:pass@host:1234/path/to/dir?key=value&query#fragment"},
483 "scheme://user:pass@host:1234/path/to/dir?query&key=value#fragment"},
486 /* IPv6 literal should render in square brackets */
487 {.uri = {"scheme", "user:pass", "12:34:56:78:9a:bc:de:f0", 1234,
488 "/path/to/dir", {{"query", "value"}}, "fragment"},
490 "scheme://user:pass@[12:34:56:78:9a:bc:de:f0]:1234/path/to/dir?query=value#fragment"},
493 GST_START_TEST (test_url_presenting)
499 for (i = 0; i < G_N_ELEMENTS (url_presenting_tests); i++) {
500 uri = gst_uri_new (url_presenting_tests[i].uri.scheme,
501 url_presenting_tests[i].uri.userinfo,
502 url_presenting_tests[i].uri.host,
503 url_presenting_tests[i].uri.port,
504 url_presenting_tests[i].uri.path,
505 NULL, url_presenting_tests[i].uri.fragment);
506 fail_unless (uri != NULL);
507 for (j = 0; j < 10; j++) {
508 if (!url_presenting_tests[i].uri.query[j].key)
511 fail_unless (gst_uri_set_query_value (uri,
512 url_presenting_tests[i].uri.query[j].key,
513 url_presenting_tests[i].uri.query[j].value));
516 result = gst_uri_to_string (uri);
517 fail_unless_equals_string (result, url_presenting_tests[i].str);
525 GST_START_TEST (test_url_normalization)
532 ("ScHeMe://User:P%61ss@HOST.%63om:1234/path/./from/../to%7d/item%2dobj?qu%65ry=something#fr%61gment");
533 fail_unless (gst_uri_normalize (url));
534 fail_unless_equals_string (gst_uri_get_scheme (url), "scheme");
535 fail_unless_equals_string (gst_uri_get_userinfo (url), "User:Pass");
536 fail_unless_equals_string (gst_uri_get_host (url), "host.com");
537 tmp_str = gst_uri_get_path (url);
538 fail_unless_equals_string (tmp_str, "/path/to}/item-obj");
540 fail_unless (gst_uri_query_has_key (url, "query"));
541 fail_unless_equals_string (gst_uri_get_query_value (url, "query"),
543 fail_unless_equals_string (gst_uri_get_fragment (url), "fragment");
549 GST_START_TEST (test_url_joining)
551 GstUri *base, *rel, *joined;
556 ("http://example.com/path/to/dir/filename.html#fragment");
558 /* test change of fragment only */
559 rel = gst_uri_from_string ("#new_frag");
560 joined = gst_uri_join (base, rel);
561 l = gst_uri_to_string (joined);
562 fail_unless_equals_string (l,
563 "http://example.com/path/to/dir/filename.html#new_frag");
565 gst_uri_unref (joined);
568 /* test addition of new query string */
569 rel = gst_uri_from_string ("?key=val");
570 joined = gst_uri_join (base, rel);
571 l = gst_uri_to_string (joined);
572 fail_unless_equals_string (l,
573 "http://example.com/path/to/dir/filename.html?key=val");
575 gst_uri_unref (joined);
578 /* test new base filename */
579 rel = gst_uri_from_string ("new_filename.xml");
580 joined = gst_uri_join (base, rel);
581 l = gst_uri_to_string (joined);
582 fail_unless_equals_string (l,
583 "http://example.com/path/to/dir/new_filename.xml");
585 gst_uri_unref (joined);
588 /* test relative file same directory */
589 rel = gst_uri_from_string ("./new_filename.xml");
590 joined = gst_uri_join (base, rel);
591 l = gst_uri_to_string (joined);
592 fail_unless_equals_string (l,
593 "http://example.com/path/to/dir/new_filename.xml");
595 gst_uri_unref (joined);
598 /* test relative file parent directory */
599 rel = gst_uri_from_string ("../new_filename.xml");
600 joined = gst_uri_join (base, rel);
601 l = gst_uri_to_string (joined);
602 fail_unless_equals_string (l, "http://example.com/path/to/new_filename.xml");
604 gst_uri_unref (joined);
607 /* test relative file grandparent directory */
608 rel = gst_uri_from_string ("../../new_filename.xml");
609 joined = gst_uri_join (base, rel);
610 l = gst_uri_to_string (joined);
611 fail_unless_equals_string (l, "http://example.com/path/new_filename.xml");
613 gst_uri_unref (joined);
616 /* test relative file root 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/new_filename.xml");
622 gst_uri_unref (joined);
625 /* test relative file beyond root directory */
626 rel = gst_uri_from_string ("../../../../new_filename.xml");
627 joined = gst_uri_join (base, rel);
628 l = gst_uri_to_string (joined);
629 fail_unless_equals_string (l, "http://example.com/new_filename.xml");
631 gst_uri_unref (joined);
634 /* test add subdirectory */
635 rel = gst_uri_from_string ("subdir/new_filename.xml");
636 joined = gst_uri_join (base, rel);
637 l = gst_uri_to_string (joined);
638 fail_unless_equals_string (l,
639 "http://example.com/path/to/dir/subdir/new_filename.xml");
641 gst_uri_unref (joined);
644 /* test change directory */
645 rel = gst_uri_from_string ("../subdir/new_filename.xml");
646 joined = gst_uri_join (base, rel);
647 l = gst_uri_to_string (joined);
648 fail_unless_equals_string (l,
649 "http://example.com/path/to/subdir/new_filename.xml");
651 gst_uri_unref (joined);
654 gst_uri_unref (base);
656 /* change base for path ending in directory */
657 base = gst_uri_from_string ("http://example.com/path/to/dir/");
659 /* test adding file to directory */
660 rel = gst_uri_from_string ("new_filename.xml");
661 joined = gst_uri_join (base, rel);
662 l = gst_uri_to_string (joined);
663 fail_unless_equals_string (l,
664 "http://example.com/path/to/dir/new_filename.xml");
666 gst_uri_unref (joined);
669 /* test adding file to directory using relative path */
670 rel = gst_uri_from_string ("./new_filename.xml");
671 joined = gst_uri_join (base, rel);
672 l = gst_uri_to_string (joined);
673 fail_unless_equals_string (l,
674 "http://example.com/path/to/dir/new_filename.xml");
676 gst_uri_unref (joined);
679 /* test filename in parent directory */
680 rel = gst_uri_from_string ("../new_filename.xml");
681 joined = gst_uri_join (base, rel);
682 l = gst_uri_to_string (joined);
683 fail_unless_equals_string (l, "http://example.com/path/to/new_filename.xml");
685 gst_uri_unref (joined);
688 /* test path ending in '../' */
689 rel = gst_uri_from_string ("one/two/../");
690 joined = gst_uri_join (base, rel);
691 l = gst_uri_to_string (joined);
692 fail_unless_equals_string (l, "http://example.com/path/to/dir/one/");
694 gst_uri_unref (joined);
697 /* test path ending in '..' Result should be the same as when ending in '../' */
698 rel = gst_uri_from_string ("one/two/..");
699 joined = gst_uri_join (base, rel);
700 l = gst_uri_to_string (joined);
701 fail_unless_equals_string (l, "http://example.com/path/to/dir/one/");
703 gst_uri_unref (joined);
706 /* test replace with absolute */
707 rel = gst_uri_from_string ("https://ssl.example.com/new_filename.xml");
708 joined = gst_uri_join (base, rel);
709 l = gst_uri_to_string (joined);
710 fail_unless_equals_string (l, "https://ssl.example.com/new_filename.xml");
712 gst_uri_unref (joined);
715 gst_uri_unref (base);
720 GST_START_TEST (test_url_equality)
726 ("ScHeMe://User:Pass@HOST.com:1234/path/./from/../to%7d/item%2dobj?query=something#fragment");
731 ("scheme://User:Pass@host.com:1234/path/to%7D/item-obj?query=something#fragment");
732 fail_unless (gst_uri_equal (url1, url2));
733 fail_unless (gst_uri_equal (url2, url1));
734 gst_uri_unref (url2);
736 /* different fragment */
739 ("scheme://User:Pass@host.com:1234/path/to%7D/item-obj?query=something#different-fragment");
740 fail_unless (!gst_uri_equal (url1, url2));
741 gst_uri_unref (url2);
743 /* different query */
746 ("scheme://User:Pass@host.com:1234/path/to%7D/item-obj?query=different-something#fragment");
747 fail_unless (!gst_uri_equal (url1, url2));
748 gst_uri_unref (url2);
753 ("scheme://User:Pass@host.com:1234/path/to%7D/different-item-obj?query=something#fragment");
754 fail_unless (!gst_uri_equal (url1, url2));
755 gst_uri_unref (url2);
760 ("scheme://User:Pass@host.com:4321/path/to%7D/item-obj?query=something#fragment");
761 fail_unless (!gst_uri_equal (url1, url2));
762 gst_uri_unref (url2);
767 ("scheme://User:Pass@different-host.com:1234/path/to%7D/item-obj?query=something#fragment");
768 fail_unless (!gst_uri_equal (url1, url2));
769 gst_uri_unref (url2);
771 /* different userinfo */
774 ("scheme://Different-User:Pass@host.com:1234/path/to%7D/item-obj?query=something#fragment");
775 fail_unless (!gst_uri_equal (url1, url2));
776 gst_uri_unref (url2);
778 /* different scheme */
781 ("different+scheme://User:Pass@host.com:1234/path/to%7D/item-obj?query=something#fragment");
782 fail_unless (!gst_uri_equal (url1, url2));
783 gst_uri_unref (url2);
785 /* different (no scheme) */
788 ("//User:Pass@host.com:1234/path/to%7D/item-obj?query=something#fragment");
789 fail_unless (!gst_uri_equal (url1, url2));
790 gst_uri_unref (url2);
792 /* different (no userinfo) */
795 ("scheme://host.com:1234/path/to%7D/item-obj?query=something#fragment");
796 fail_unless (!gst_uri_equal (url1, url2));
797 gst_uri_unref (url2);
799 /* different (no host) */
802 ("scheme://User:Pass@:1234/path/to%7D/item-obj?query=something#fragment");
803 fail_unless (!gst_uri_equal (url1, url2));
804 gst_uri_unref (url2);
806 /* different (no port) */
809 ("scheme://User:Pass@host.com/path/to%7D/item-obj?query=something#fragment");
810 fail_unless (!gst_uri_equal (url1, url2));
811 gst_uri_unref (url2);
813 /* different (no path) */
816 ("scheme://User:Pass@host.com:1234?query=something#fragment");
817 fail_unless (!gst_uri_equal (url1, url2));
818 gst_uri_unref (url2);
820 /* different (no query) */
823 ("scheme://User:Pass@host.com:1234/path/to%7D/item-obj#fragment");
824 fail_unless (!gst_uri_equal (url1, url2));
825 gst_uri_unref (url2);
827 /* different (no fragment) */
830 ("scheme://User:Pass@host.com:1234/path/to%7D/item-obj?query=something");
831 fail_unless (!gst_uri_equal (url1, url2));
832 gst_uri_unref (url2);
834 /* compare two NULL uris */
835 fail_unless (gst_uri_equal (NULL, NULL));
837 /* compare same object */
838 fail_unless (gst_uri_equal (url1, url1));
840 /* compare one NULL and one non-NULL uri */
841 fail_unless (!gst_uri_equal (url1, NULL));
842 fail_unless (!gst_uri_equal (NULL, url1));
844 gst_uri_unref (url1);
849 GST_START_TEST (test_url_constructors)
853 GHashTable *tmp_table;
856 gst_uri_new ("scheme", "userinfo", "hostname", 1234, "/path/to/file",
857 "query", "fragment");
858 fail_unless_equals_string (gst_uri_get_scheme (url1), "scheme");
859 fail_unless_equals_string (gst_uri_get_userinfo (url1), "userinfo");
860 fail_unless_equals_string (gst_uri_get_host (url1), "hostname");
861 fail_unless (gst_uri_get_port (url1) == 1234);
862 tmp_str = gst_uri_get_path (url1);
863 fail_unless_equals_string (tmp_str, "/path/to/file");
865 tmp_table = gst_uri_get_query_table (url1);
866 fail_unless (g_hash_table_size (tmp_table) == 1);
867 fail_unless (g_hash_table_contains (tmp_table, "query"));
868 fail_unless (g_hash_table_lookup (tmp_table, "query") == NULL);
869 g_hash_table_unref (tmp_table);
870 fail_unless_equals_string (gst_uri_get_fragment (url1), "fragment");
871 tmp_str = gst_uri_to_string (url1);
872 fail_unless_equals_string (tmp_str,
873 "scheme://userinfo@hostname:1234/path/to/file?query#fragment");
877 gst_uri_new_with_base (url1, NULL, NULL, NULL, GST_URI_NO_PORT,
878 "new_file", NULL, NULL);
879 fail_unless_equals_string (gst_uri_get_scheme (url2), "scheme");
880 fail_unless_equals_string (gst_uri_get_userinfo (url2), "userinfo");
881 fail_unless_equals_string (gst_uri_get_host (url2), "hostname");
882 fail_unless (gst_uri_get_port (url2) == 1234);
883 tmp_str = gst_uri_get_path (url2);
884 fail_unless_equals_string (tmp_str, "/path/to/new_file");
886 fail_unless (gst_uri_get_query_table (url2) == NULL);
887 fail_unless (gst_uri_get_fragment (url2) == NULL);
888 tmp_str = gst_uri_to_string (url2);
889 fail_unless_equals_string (tmp_str,
890 "scheme://userinfo@hostname:1234/path/to/new_file");
892 gst_uri_unref (url2);
894 url2 = gst_uri_from_string_with_base (url1, "/a/new/path/to/file");
895 fail_unless_equals_string (gst_uri_get_scheme (url2), "scheme");
896 fail_unless_equals_string (gst_uri_get_userinfo (url2), "userinfo");
897 fail_unless_equals_string (gst_uri_get_host (url2), "hostname");
898 fail_unless (gst_uri_get_port (url2) == 1234);
899 tmp_str = gst_uri_get_path (url2);
900 fail_unless_equals_string (tmp_str, "/a/new/path/to/file");
902 fail_unless (gst_uri_get_query_table (url2) == NULL);
903 fail_unless (gst_uri_get_fragment (url2) == NULL);
904 tmp_str = gst_uri_to_string (url2);
905 fail_unless_equals_string (tmp_str,
906 "scheme://userinfo@hostname:1234/a/new/path/to/file");
908 gst_uri_unref (url2);
910 url2 = gst_uri_from_string_with_base (url1, "http://foobar.com/bla");
911 fail_unless_equals_string (gst_uri_get_scheme (url2), "http");
912 fail_unless_equals_string (gst_uri_get_host (url2), "foobar.com");
913 fail_unless (gst_uri_get_port (url2) == 0);
914 tmp_str = gst_uri_get_path (url2);
915 fail_unless_equals_string (tmp_str, "/bla");
917 fail_unless (gst_uri_get_query_table (url2) == NULL);
918 fail_unless (gst_uri_get_fragment (url2) == NULL);
919 tmp_str = gst_uri_to_string (url2);
920 fail_unless_equals_string (tmp_str, "http://foobar.com/bla");
922 gst_uri_unref (url2);
924 url2 = gst_uri_copy (url1);
925 fail_unless (gst_uri_equal (url1, url2));
926 fail_unless (gst_uri_set_query_value (url2, "key", "value"));
927 fail_unless (!gst_uri_equal (url1, url2));
928 gst_uri_unref (url2);
930 gst_uri_unref (url1);
935 GST_START_TEST (test_url_get_set)
941 url = gst_uri_from_string ("scheme://hostname/path/to/file?query#fragment");
943 fail_unless (gst_uri_set_scheme (url, "new+scheme"));
944 fail_unless_equals_string (gst_uri_get_scheme (url), "new+scheme");
945 tmp_str = gst_uri_to_string (url);
946 fail_unless_equals_string (tmp_str,
947 "new+scheme://hostname/path/to/file?query#fragment");
950 fail_unless (gst_uri_set_scheme (url, NULL));
951 fail_unless (gst_uri_get_scheme (url) == NULL);
952 tmp_str = gst_uri_to_string (url);
953 fail_unless_equals_string (tmp_str, "//hostname/path/to/file?query#fragment");
956 fail_unless (!gst_uri_set_scheme (NULL, "fail"));
957 fail_unless (gst_uri_set_scheme (NULL, NULL));
959 fail_unless (gst_uri_set_userinfo (url, "username:password"));
960 fail_unless_equals_string (gst_uri_get_userinfo (url), "username:password");
961 tmp_str = gst_uri_to_string (url);
962 fail_unless_equals_string (tmp_str,
963 "//username:password@hostname/path/to/file?query#fragment");
966 fail_unless (gst_uri_set_userinfo (url, NULL));
967 fail_unless (gst_uri_get_userinfo (url) == NULL);
968 tmp_str = gst_uri_to_string (url);
969 fail_unless_equals_string (tmp_str, "//hostname/path/to/file?query#fragment");
972 fail_unless (!gst_uri_set_userinfo (NULL, "fail"));
973 fail_unless (gst_uri_set_userinfo (NULL, NULL));
975 fail_unless (gst_uri_set_host (url, NULL));
976 fail_unless (gst_uri_get_host (url) == NULL);
977 tmp_str = gst_uri_to_string (url);
978 fail_unless_equals_string (tmp_str, "/path/to/file?query#fragment");
981 fail_unless (gst_uri_set_host (url, "example.com"));
982 fail_unless_equals_string (gst_uri_get_host (url), "example.com");
983 tmp_str = gst_uri_to_string (url);
984 fail_unless_equals_string (tmp_str,
985 "//example.com/path/to/file?query#fragment");
988 fail_unless (!gst_uri_set_host (NULL, "fail"));
989 fail_unless (gst_uri_set_host (NULL, NULL));
991 fail_unless (gst_uri_set_port (url, 12345));
992 fail_unless (gst_uri_get_port (url) == 12345);
993 tmp_str = gst_uri_to_string (url);
994 fail_unless_equals_string (tmp_str,
995 "//example.com:12345/path/to/file?query#fragment");
998 fail_unless (gst_uri_set_port (url, GST_URI_NO_PORT));
999 fail_unless (gst_uri_get_port (url) == GST_URI_NO_PORT);
1000 tmp_str = gst_uri_to_string (url);
1001 fail_unless_equals_string (tmp_str,
1002 "//example.com/path/to/file?query#fragment");
1005 fail_unless (!gst_uri_set_port (NULL, 1234));
1006 fail_unless (gst_uri_set_port (NULL, GST_URI_NO_PORT));
1008 fail_unless (gst_uri_append_path_segment (url, "here"));
1009 tmp_str = gst_uri_to_string (url);
1010 fail_unless_equals_string (tmp_str,
1011 "//example.com/path/to/file/here?query#fragment");
1014 fail_unless (!gst_uri_append_path_segment (NULL, "fail"));
1015 fail_unless (gst_uri_append_path_segment (NULL, NULL));
1017 fail_unless (gst_uri_append_path (url, "../there"));
1018 tmp_str = gst_uri_to_string (url);
1019 fail_unless_equals_string (tmp_str,
1020 "//example.com/path/to/file/here/../there?query#fragment");
1023 fail_unless (!gst_uri_append_path (NULL, "fail"));
1024 fail_unless (gst_uri_append_path (NULL, NULL));
1026 gst_uri_normalize (url);
1028 tmp_list = gst_uri_get_path_segments (url);
1029 fail_unless (tmp_list != NULL);
1030 tmp_list = g_list_append (tmp_list, g_strdup ("segment"));
1031 tmp_str = gst_uri_to_string (url);
1032 fail_unless_equals_string (tmp_str,
1033 "//example.com/path/to/file/there?query#fragment");
1035 fail_unless (gst_uri_set_path_segments (url, tmp_list));
1036 tmp_str = gst_uri_to_string (url);
1037 fail_unless_equals_string (tmp_str,
1038 "//example.com/path/to/file/there/segment?query#fragment");
1041 tmp_list = g_list_append (NULL, g_strdup ("test"));
1042 fail_unless (!gst_uri_set_path_segments (NULL, tmp_list));
1043 fail_unless (gst_uri_set_path_segments (NULL, NULL));
1045 fail_unless (gst_uri_set_query_value (url, "key", "value"));
1046 tmp_str = gst_uri_to_string (url);
1047 #if GLIB_CHECK_VERSION(2, 59, 0)
1048 fail_unless_equals_string (tmp_str,
1049 "//example.com/path/to/file/there/segment?key=value&query#fragment");
1051 fail_unless_equals_string (tmp_str,
1052 "//example.com/path/to/file/there/segment?query&key=value#fragment");
1056 fail_unless (gst_uri_set_query_value (url, "key", NULL));
1057 tmp_str = gst_uri_to_string (url);
1058 #if GLIB_CHECK_VERSION(2, 59, 0)
1059 fail_unless_equals_string (tmp_str,
1060 "//example.com/path/to/file/there/segment?key&query#fragment");
1062 fail_unless_equals_string (tmp_str,
1063 "//example.com/path/to/file/there/segment?query&key#fragment");
1067 fail_unless (!gst_uri_set_query_value (NULL, "key", "value"));
1069 fail_unless (gst_uri_remove_query_key (url, "key"));
1070 tmp_str = gst_uri_to_string (url);
1071 fail_unless_equals_string (tmp_str,
1072 "//example.com/path/to/file/there/segment?query#fragment");
1075 fail_unless (!gst_uri_remove_query_key (url, "key"));
1076 fail_unless (!gst_uri_remove_query_key (NULL, "key"));
1078 fail_unless (gst_uri_set_fragment (url, NULL));
1079 fail_unless (gst_uri_get_fragment (url) == NULL);
1080 tmp_str = gst_uri_to_string (url);
1081 fail_unless_equals_string (tmp_str,
1082 "//example.com/path/to/file/there/segment?query");
1085 fail_unless (gst_uri_set_fragment (url, "tag"));
1086 fail_unless_equals_string (gst_uri_get_fragment (url), "tag");
1087 tmp_str = gst_uri_to_string (url);
1088 fail_unless_equals_string (tmp_str,
1089 "//example.com/path/to/file/there/segment?query#tag");
1092 fail_unless (!gst_uri_set_fragment (NULL, "can't set if no URI"));
1093 fail_unless (gst_uri_set_fragment (NULL, NULL));
1095 gst_uri_unref (url);
1100 GST_START_TEST (test_url_get_media_fragment_table)
1106 /* Examples at https://www.w3.org/TR/media-frags/#processing-media-fragment-uri */
1109 url = gst_uri_from_string ("http://foo/var/file#t=1");
1110 table = gst_uri_get_media_fragment_table (url);
1111 fail_unless (table);
1112 fail_unless (g_hash_table_size (table) == 1);
1113 fail_unless (g_hash_table_lookup_extended (table, "t", NULL,
1115 fail_unless_equals_string ("1", val);
1116 g_hash_table_unref (table);
1117 gst_uri_unref (url);
1119 /* NOTE: Media Fragments URI 1.0 (W3C) is saying that
1120 * "Multiple occurrences of the same dimension: only the last valid occurrence
1121 * of a dimension (e.g. t=10 in #t=2&t=10) is interpreted and all previous
1122 * occurrences (valid or invalid) SHOULD be ignored by the user agent"
1124 /* TEST "t=1&t=2" */
1125 url = gst_uri_from_string ("http://foo/var/file#t=1&t=2");
1126 table = gst_uri_get_media_fragment_table (url);
1127 fail_unless (table);
1128 fail_unless (g_hash_table_size (table) == 1);
1129 fail_unless (g_hash_table_lookup_extended (table, "t", NULL,
1131 fail_unless_equals_string ("2", val);
1132 g_hash_table_unref (table);
1133 gst_uri_unref (url);
1136 url = gst_uri_from_string ("http://foo/var/file#a=b=c");
1137 table = gst_uri_get_media_fragment_table (url);
1138 fail_unless (table);
1139 fail_unless (g_hash_table_size (table) == 1);
1140 fail_unless (g_hash_table_lookup_extended (table, "a", NULL,
1142 fail_unless_equals_string ("b=c", val);
1143 g_hash_table_unref (table);
1144 gst_uri_unref (url);
1147 url = gst_uri_from_string ("http://foo/var/file#a&b=c");
1148 table = gst_uri_get_media_fragment_table (url);
1149 fail_unless (table);
1150 fail_unless (g_hash_table_size (table) == 2);
1151 fail_unless (g_hash_table_lookup_extended (table, "a", NULL,
1153 fail_unless (val == NULL);
1154 fail_unless (g_hash_table_lookup_extended (table, "b", NULL,
1156 fail_unless_equals_string ("c", val);
1157 g_hash_table_unref (table);
1158 gst_uri_unref (url);
1160 /* TEST "%74=%6ept%3A%310" */
1161 url = gst_uri_from_string ("http://foo/var/file#%74=%6ept%3A%310");
1162 table = gst_uri_get_media_fragment_table (url);
1163 fail_unless (table);
1164 fail_unless (g_hash_table_size (table) == 1);
1165 fail_unless (g_hash_table_lookup_extended (table, "t", NULL,
1167 fail_unless_equals_string ("npt:10", val);
1168 g_hash_table_unref (table);
1169 gst_uri_unref (url);
1174 GST_START_TEST (test_url_unescape_equals_in_http_query)
1177 gchar *query_string;
1181 ("http://abc.manifest?token=exp=123~acl=/QualityLevels(*~hmac=0cb");
1183 fail_unless_equals_string (gst_uri_get_scheme (url), "http");
1184 query_string = gst_uri_get_query_string (url);
1185 fail_unless_equals_string (query_string,
1186 "token=exp=123~acl=/QualityLevels(*~hmac=0cb");
1187 g_free (query_string);
1188 fail_unless (gst_uri_query_has_key (url, "token"));
1189 fail_unless_equals_string (gst_uri_get_query_value (url, "token"),
1190 "exp=123~acl=/QualityLevels(*~hmac=0cb");
1191 gst_uri_unref (url);
1197 gst_uri_suite (void)
1199 Suite *s = suite_create ("GstURI");
1200 TCase *tc_chain = tcase_create ("uri");
1202 tcase_set_timeout (tc_chain, 20);
1204 suite_add_tcase (s, tc_chain);
1205 tcase_add_test (tc_chain, test_protocol_case);
1206 tcase_add_test (tc_chain, test_uri_get_location);
1207 #ifndef GST_REMOVE_DEPRECATED
1208 tcase_add_test (tc_chain, test_gst_uri_construct);
1210 tcase_add_test (tc_chain, test_uri_misc);
1211 tcase_add_test (tc_chain, test_element_make_from_uri);
1213 tcase_add_test (tc_chain, test_win32_uri);
1215 tcase_add_test (tc_chain, test_url_parsing);
1216 tcase_add_test (tc_chain, test_url_parsing_escaped);
1217 tcase_add_test (tc_chain, test_url_presenting);
1218 tcase_add_test (tc_chain, test_url_normalization);
1219 tcase_add_test (tc_chain, test_url_joining);
1220 tcase_add_test (tc_chain, test_url_equality);
1221 tcase_add_test (tc_chain, test_url_constructors);
1222 tcase_add_test (tc_chain, test_url_get_set);
1223 tcase_add_test (tc_chain, test_url_get_media_fragment_table);
1224 tcase_add_test (tc_chain, test_url_unescape_equals_in_http_query);
1229 GST_CHECK_MAIN (gst_uri);