1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
36 char *expected_result;
37 GConvertError expected_error; /* If failed */
42 { "/etc", NULL, "file:///etc"},
43 { "/etc", "", "file:///etc"},
44 { "/etc", "otherhost", "file://otherhost/etc"},
46 { "/etc", "localhost", "file:///etc"},
47 { "c:\\windows", NULL, "file:///c:/windows"},
48 { "c:\\windows", "localhost", "file:///c:/windows"},
49 { "c:\\windows", "otherhost", "file://otherhost/c:/windows"},
50 { "\\\\server\\share\\dir", NULL, "file:////server/share/dir"},
51 { "\\\\server\\share\\dir", "localhost", "file:////server/share/dir"},
53 { "/etc", "localhost", "file://localhost/etc"},
54 { "c:\\windows", NULL, NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH}, /* it's important to get this error on Unix */
55 { "c:\\windows", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
56 { "c:\\windows", "otherhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
58 { "etc", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
59 #ifndef G_PLATFORM_WIN32
60 { "/etc/\xE5\xE4\xF6", NULL, "file:///etc/%E5%E4%F6" },
61 { "/etc/\xC3\xB6\xC3\xA4\xC3\xA5", NULL, "file:///etc/%C3%B6%C3%A4%C3%A5"},
63 { "/etc", "\xC3\xB6\xC3\xA4\xC3\xA5", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
64 { "/etc", "\xE5\xE4\xF6", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
65 { "/etc/file with #%", NULL, "file:///etc/file%20with%20%23%25"},
66 { "", NULL, NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
67 { "", "", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
68 { "", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
69 { "", "otherhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
70 { "/0123456789", NULL, "file:///0123456789"},
71 { "/ABCDEFGHIJKLMNOPQRSTUVWXYZ", NULL, "file:///ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
72 { "/abcdefghijklmnopqrstuvwxyz", NULL, "file:///abcdefghijklmnopqrstuvwxyz"},
73 { "/-_.!~*'()", NULL, "file:///-_.!~*'()"},
75 /* As '\\' is a path separator on Win32, it gets turned into '/' in the URI */
76 { "/\"#%<>[\\]^`{|}\x7F", NULL, "file:///%22%23%25%3C%3E%5B/%5D%5E%60%7B%7C%7D%7F"},
78 /* On Unix, '\\' is a normal character in the file name */
79 { "/\"#%<>[\\]^`{|}\x7F", NULL, "file:///%22%23%25%3C%3E%5B%5C%5D%5E%60%7B%7C%7D%7F"},
81 { "/;@+$,", NULL, "file:///%3B@+$,"},
82 /* This and some of the following are of course as such illegal file names on Windows,
83 * and would not occur in real life.
85 { "/:", NULL, "file:///:"},
86 { "/?&=", NULL, "file:///%3F&="},
87 { "/", "0123456789-", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
88 { "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/"},
89 { "/", "abcdefghijklmnopqrstuvwxyz", "file://abcdefghijklmnopqrstuvwxyz/"},
90 { "/", "_.!~*'()", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
91 { "/", "\"#%<>[\\]^`{|}\x7F", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
92 { "/", ";?&=+$,", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
93 { "/", "/", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
94 { "/", "@:", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
95 { "/", "\x80\xFF", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
96 { "/", "\xC3\x80\xC3\xBF", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
103 char *expected_filename;
104 char *expected_hostname;
105 GConvertError expected_error; /* If failed */
110 { "file:///etc", "/etc"},
111 { "file:/etc", "/etc"},
113 /* On Win32 we don't return "localhost" hostames, just in case
114 * it isn't recognized anyway.
116 { "file://localhost/etc", "/etc", NULL},
117 { "file://localhost/etc/%23%25%20file", "/etc/#% file", NULL},
118 { "file://localhost/\xE5\xE4\xF6", "/\xe5\xe4\xf6", NULL},
119 { "file://localhost/%E5%E4%F6", "/\xe5\xe4\xf6", NULL},
121 { "file://localhost/etc", "/etc", "localhost"},
122 { "file://localhost/etc/%23%25%20file", "/etc/#% file", "localhost"},
123 { "file://localhost/\xE5\xE4\xF6", "/\xe5\xe4\xf6", "localhost"},
124 { "file://localhost/%E5%E4%F6", "/\xe5\xe4\xf6", "localhost"},
126 { "file://otherhost/etc", "/etc", "otherhost"},
127 { "file://otherhost/etc/%23%25%20file", "/etc/#% file", "otherhost"},
128 { "file://%C3%B6%C3%A4%C3%A5/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
129 { "file:////etc/%C3%B6%C3%C3%C3%A5", "//etc/\xc3\xb6\xc3\xc3\xc3\xa5", NULL},
130 { "file://\xE5\xE4\xF6/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
131 { "file://%E5%E4%F6/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
132 { "file:///some/file#bad", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
133 { "file://some", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
134 { "", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
135 { "file:test", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
136 { "http://www.yahoo.com/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
137 { "file:////etc", "//etc"},
138 { "file://///etc", "///etc"},
140 /* URIs with backslashes come from some nonstandard application, but accept them anyhow */
141 { "file:///c:\\foo", "c:\\foo"},
142 { "file:///c:/foo\\bar", "c:\\foo\\bar"},
143 /* Accept also the old Netscape drive-letter-and-vertical bar convention */
144 { "file:///c|/foo", "c:\\foo"},
145 { "file:////server/share/dir", "\\\\server\\share\\dir"},
146 { "file://localhost//server/share/foo", "\\\\server\\share\\foo"},
147 { "file://otherhost//server/share/foo", "\\\\server\\share\\foo", "otherhost"},
149 { "file:///c:\\foo", "/c:\\foo"},
150 { "file:///c:/foo", "/c:/foo"},
151 { "file:////c:/foo", "//c:/foo"},
153 { "file://0123456789/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
154 { "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/", "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
155 { "file://abcdefghijklmnopqrstuvwxyz/", "/", "abcdefghijklmnopqrstuvwxyz"},
156 { "file://-_.!~*'()/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
157 { "file://\"<>[\\]^`{|}\x7F/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
158 { "file://;?&=+$,/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
159 { "file://%C3%80%C3%BF/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
160 { "file://@/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
161 { "file://:/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
162 { "file://#/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
163 { "file://%23/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
164 { "file://%2F/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
168 run_to_uri_tests (void)
174 for (i = 0; i < G_N_ELEMENTS (to_uri_tests); i++)
177 res = g_filename_to_uri (to_uri_tests[i].filename,
178 to_uri_tests[i].hostname,
182 g_assert_cmpstr (res, ==, to_uri_tests[i].expected_result);
184 g_assert_error (error, G_CONVERT_ERROR, to_uri_tests[i].expected_error);
187 g_clear_error (&error);
192 run_from_uri_tests (void)
199 for (i = 0; i < G_N_ELEMENTS (from_uri_tests); i++)
202 res = g_filename_from_uri (from_uri_tests[i].uri,
209 p = from_uri_tests[i].expected_filename = g_strdup (from_uri_tests[i].expected_filename);
210 while ((slash = strchr (p, '/')) != NULL)
218 g_assert_cmpstr (res, ==, from_uri_tests[i].expected_filename);
220 g_assert_error (error, G_CONVERT_ERROR, from_uri_tests[i].expected_error);
221 g_assert_cmpstr (hostname, ==, from_uri_tests[i].expected_hostname);
225 g_clear_error (&error);
230 safe_strcmp_filename (const gchar *a, const gchar *b)
233 return g_strcmp0 (a, b);
236 return g_strcmp0 (a, b);
241 if ((G_IS_DIR_SEPARATOR (*a) && G_IS_DIR_SEPARATOR (*b)) ||
253 safe_strcmp_hostname (const gchar *a, const gchar *b)
260 return g_strcmp0 (a, b);
262 if (g_strcmp0 (a, "localhost") == 0 && b == NULL)
265 return g_strcmp0 (a, b);
270 run_roundtrip_tests (void)
273 gchar *uri, *hostname, *res;
276 for (i = 0; i < G_N_ELEMENTS (to_uri_tests); i++)
278 if (to_uri_tests[i].expected_error != 0)
282 uri = g_filename_to_uri (to_uri_tests[i].filename,
283 to_uri_tests[i].hostname,
285 g_assert_no_error (error);
288 res = g_filename_from_uri (uri, &hostname, &error);
289 g_assert_no_error (error);
291 g_assert (safe_strcmp_filename (to_uri_tests[i].filename, res) == 0);
292 g_assert (safe_strcmp_hostname (to_uri_tests[i].hostname, hostname) == 0);
300 run_uri_list_tests (void)
302 /* straight from the RFC */
304 "# urn:isbn:0-201-08372-8\r\n"
305 "http://www.huh.org/books/foo.html\r\n"
306 "http://www.huh.org/books/foo.pdf \r\n"
307 " ftp://ftp.foo.org/books/foo.txt\r\n";
308 gchar *expected_uris[] = {
309 "http://www.huh.org/books/foo.html",
310 "http://www.huh.org/books/foo.pdf",
311 "ftp://ftp.foo.org/books/foo.txt"
317 uris = g_uri_list_extract_uris (list);
318 g_assert_cmpint (g_strv_length (uris), ==, 3);
320 for (j = 0; j < 3; j++)
321 g_assert_cmpstr (uris[j], ==, expected_uris[j]);
325 uris = g_uri_list_extract_uris ("# just hot air\r\n# more hot air");
326 g_assert_cmpint (g_strv_length (uris), ==, 0);
331 test_uri_unescape (void)
335 s = g_uri_unescape_string ("%2Babc %4F", NULL);
336 g_assert_cmpstr (s, ==, "+abc O");
338 g_assert_cmpstr (g_uri_unescape_string ("%2Babc %4F", "+"), ==, NULL);
339 g_assert_cmpstr (g_uri_unescape_string ("%00abc %4F", "+/"), ==, NULL);
340 g_assert_cmpstr (g_uri_unescape_string ("%0", NULL), ==, NULL);
341 g_assert_cmpstr (g_uri_unescape_string ("%ra", NULL), ==, NULL);
342 g_assert_cmpstr (g_uri_unescape_string ("%2r", NULL), ==, NULL);
343 g_assert_cmpstr (g_uri_unescape_string (NULL, NULL), ==, NULL);
347 test_uri_escape (void)
351 s = g_uri_escape_string ("abcdefgABCDEFG._~", NULL, FALSE);
352 g_assert_cmpstr (s, ==, "abcdefgABCDEFG._~");
354 s = g_uri_escape_string (":+ \\?#", NULL, FALSE);
355 g_assert_cmpstr (s, ==, "%3A%2B%20%5C%3F%23");
357 s = g_uri_escape_string ("a+b:c", "+", FALSE);
358 g_assert_cmpstr (s, ==, "a+b%3Ac");
363 test_uri_scheme (void)
367 s = g_uri_parse_scheme ("ftp://ftp.gtk.org");
368 g_assert_cmpstr (s, ==, "ftp");
370 s = g_uri_parse_scheme ("1bad:");
371 g_assert (s == NULL);
372 s = g_uri_parse_scheme ("bad");
373 g_assert (s == NULL);
380 g_test_init (&argc, &argv, NULL);
382 g_test_add_func ("/uri/to-uri", run_to_uri_tests);
383 g_test_add_func ("/uri/from-uri", run_from_uri_tests);
384 g_test_add_func ("/uri/roundtrip", run_roundtrip_tests);
385 g_test_add_func ("/uri/list", run_uri_list_tests);
386 g_test_add_func ("/uri/unescape", test_uri_unescape);
387 g_test_add_func ("/uri/escape", test_uri_escape);
388 g_test_add_func ("/uri/scheme", test_uri_scheme);
390 return g_test_run ();