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/.
27 #undef G_DISABLE_ASSERT
39 char *expected_result;
40 GConvertError expected_error; /* If failed */
45 { "/etc", NULL, "file:///etc"},
46 { "/etc", "", "file:///etc"},
47 { "/etc", "otherhost", "file://otherhost/etc"},
49 { "/etc", "localhost", "file:///etc"},
50 { "c:\\windows", NULL, "file:///c:/windows"},
51 { "c:\\windows", "localhost", "file:///c:/windows"},
52 { "c:\\windows", "otherhost", "file://otherhost/c:/windows"},
53 { "\\\\server\\share\\dir", NULL, "file:////server/share/dir"},
54 { "\\\\server\\share\\dir", "localhost", "file:////server/share/dir"},
56 { "/etc", "localhost", "file://localhost/etc"},
57 { "c:\\windows", NULL, NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH}, /* it's important to get this error on Unix */
58 { "c:\\windows", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
59 { "c:\\windows", "otherhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
61 { "etc", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
63 /* g_filename_to_utf8 uses current code page on Win32, these tests assume that
64 * local filenames *are* in UTF-8.
66 { "/etc/öäå", NULL, NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
67 { "/etc/öäå", NULL, "file:///etc/%C3%B6%C3%A4%C3%A5"},
69 { "/etc", "öäå", "file://%C3%B6%C3%A4%C3%A5/etc"},
70 { "/etc", "åäö", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
71 { "/etc/file with #%", NULL, "file:///etc/file%20with%20%23%25"},
72 { "", NULL, NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
73 { "", "", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
74 { "", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
75 { "", "otherhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
76 { "/0123456789", NULL, "file:///0123456789"},
77 { "/ABCDEFGHIJKLMNOPQRSTUVWXYZ", NULL, "file:///ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
78 { "/abcdefghijklmnopqrstuvwxyz", NULL, "file:///abcdefghijklmnopqrstuvwxyz"},
79 { "/-_.!~*'()", NULL, "file:///-_.!~*'()"},
81 /* As '\\' is a path separator on Win32, it gets turned into '/' in the URI */
82 { "/\"#%<>[\\]^`{|}\x7F", NULL, "file:///%22%23%25%3C%3E%5B/%5D%5E%60%7B%7C%7D%7F"},
84 /* On Unix, '\\' is a normal character in the file name */
85 { "/\"#%<>[\\]^`{|}\x7F", NULL, "file:///%22%23%25%3C%3E%5B%5C%5D%5E%60%7B%7C%7D%7F"},
87 { "/;@+$,", NULL, "file:///%3B%40%2B%24%2C"},
88 /* This and some of the following are of course as such illegal file names on Windows,
89 * and would not occur in real life.
91 { "/:", NULL, "file:///:"},
92 { "/?&=", NULL, "file:///?&="}, /* these are not escaped and other reserved characters are -- is that really what we want? */
93 { "/", "0123456789", "file://0123456789/"},
94 { "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/"},
95 { "/", "abcdefghijklmnopqrstuvwxyz", "file://abcdefghijklmnopqrstuvwxyz/"},
96 { "/", "-_.!~*'()", "file://-_.!~*'()/"},
97 { "/", "\"#%<>[\\]^`{|}\x7F", "file://%22%23%25%3C%3E%5B%5C%5D%5E%60%7B%7C%7D%7F/"},
98 { "/", ";?&=+$,", "file://%3B%3F%26%3D%2B%24%2C/"},
99 { "/", "/", "file:////"}, /* should be "file://%2F/" or an error */
100 { "/", "@:", "file://@:/"}, /* these are not escaped and other reserved characters are -- is that really what we want? */
101 { "/", "\x80\xFF", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
102 { "/", "\xC3\x80\xC3\xBF", "file://%C3%80%C3%BF/"},
109 char *expected_filename;
110 char *expected_hostname;
111 GConvertError expected_error; /* If failed */
116 { "file:///etc", "/etc"},
117 { "file:/etc", "/etc"},
119 /* On Win32 we don't return "localhost" hostames, just in case
120 * it isn't recognized anyway.
122 { "file://localhost/etc", "/etc", NULL},
123 { "file://localhost/etc/%23%25%20file", "/etc/#% file", NULL},
125 { "file://localhost/etc", "/etc", "localhost"},
126 { "file://localhost/etc/%23%25%20file", "/etc/#% file", "localhost"},
128 { "file://otherhost/etc", "/etc", "otherhost"},
129 { "file://otherhost/etc/%23%25%20file", "/etc/#% file", "otherhost"},
130 { "file://%C3%B6%C3%A4%C3%A5/etc", "/etc", "öäå"},
131 { "file:////etc/%C3%B6%C3%C3%C3%A5", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
132 { "file://localhost/åäö", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
133 { "file://åäö/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
134 { "file:///some/file#bad", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
135 { "file://some", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
136 { "", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
137 { "file:test", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
138 { "http://www.yahoo.com/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
139 { "file:////etc", "//etc"},
140 { "file://///etc", "///etc"},
142 /* URIs with backslashes come from some nonstandard application, but accept them anyhow */
143 { "file:///c:\\foo", "c:\\foo"},
144 { "file:///c:/foo\\bar", "c:\\foo\\bar"},
145 /* Accept also the old Netscape drive-letter-and-vertical bar convention */
146 { "file:///c|/foo", "c:\\foo"},
147 { "file:////server/share/dir", "\\\\server\\share\\dir"},
148 { "file://localhost//server/share/foo", "\\\\server\\share\\foo"},
149 { "file://otherhost//server/share/foo", "\\\\server\\share\\foo", "otherhost"},
151 { "file:///c:\\foo", "/c:\\foo"},
152 { "file:///c:/foo", "/c:/foo"},
153 { "file:////c:/foo", "//c:/foo"},
155 { "file://0123456789/", "/", "0123456789"},
156 { "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/", "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
157 { "file://abcdefghijklmnopqrstuvwxyz/", "/", "abcdefghijklmnopqrstuvwxyz"},
158 { "file://-_.!~*'()/", "/", "-_.!~*'()"},
159 { "file://\"<>[\\]^`{|}\x7F/", "/", "\"<>[\\]^`{|}\x7F"},
160 { "file://;?&=+$,/", "/", ";?&=+$,"},
161 { "file://%C3%80%C3%BF/", "/", "\xC3\x80\xC3\xBF"},
162 { "file://@/", "/", "@"},
163 { "file://:/", "/", ":"},
164 { "file://#/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
165 { "file://%23/", "/", "#"}, /* is it dangerous to return a hostname with a "#" character in it? */
166 { "file://%2F/", "/", "/"}, /* is it dangerous to return a hostname with a "/" character in it? */
170 static gboolean any_failed = FALSE;
173 run_to_uri_tests (void)
179 for (i = 0; i < G_N_ELEMENTS (to_uri_tests); i++)
182 res = g_filename_to_uri (to_uri_tests[i].filename,
183 to_uri_tests[i].hostname,
186 if (to_uri_tests[i].expected_result == NULL)
190 g_print ("\ng_filename_to_uri() test %d failed, expected to return NULL, actual result: %s\n", i, res);
197 g_print ("\ng_filename_to_uri() test %d failed, returned NULL, but didn't set error\n", i);
200 else if (error->domain != G_CONVERT_ERROR)
202 g_print ("\ng_filename_to_uri() test %d failed, returned NULL, set non G_CONVERT_ERROR error\n", i);
205 else if (error->code != to_uri_tests[i].expected_error)
207 g_print ("\ng_filename_to_uri() test %d failed as expected, but set wrong errorcode %d instead of expected %d \n",
208 i, error->code, to_uri_tests[i].expected_error);
213 else if (res == NULL || strcmp (res, to_uri_tests[i].expected_result) != 0)
215 g_print ("\ng_filename_to_uri() test %d failed, expected result: %s, actual result: %s\n",
216 i, to_uri_tests[i].expected_result, (res) ? res : "NULL");
218 g_print ("Error message: %s\n", error->message);
222 /* Give some output */
228 run_from_uri_tests (void)
235 for (i = 0; i < G_N_ELEMENTS (from_uri_tests); i++)
238 res = g_filename_from_uri (from_uri_tests[i].uri,
242 if (from_uri_tests[i].expected_filename == NULL)
246 g_print ("\ng_filename_from_uri() test %d failed, expected to return NULL, actual result: %s\n", i, res);
253 g_print ("\ng_filename_from_uri() test %d failed, returned NULL, but didn't set error\n", i);
256 else if (error->domain != G_CONVERT_ERROR)
258 g_print ("\ng_filename_from_uri() test %d failed, returned NULL, set non G_CONVERT_ERROR error\n", i);
261 else if (error->code != from_uri_tests[i].expected_error)
263 g_print ("\ng_filename_from_uri() test %d failed as expected, but set wrong errorcode %d instead of expected %d \n",
264 i, error->code, from_uri_tests[i].expected_error);
274 p = from_uri_tests[i].expected_filename = g_strdup (from_uri_tests[i].expected_filename);
275 while ((slash = strchr (p, '/')) != NULL)
281 if (res == NULL || strcmp (res, from_uri_tests[i].expected_filename) != 0)
283 g_print ("\ng_filename_from_uri() test %d failed, expected result: %s, actual result: %s\n",
284 i, from_uri_tests[i].expected_filename, (res) ? res : "NULL");
288 if (from_uri_tests[i].expected_hostname == NULL)
290 if (hostname != NULL)
292 g_print ("\ng_filename_from_uri() test %d failed, expected no hostname, got: %s\n",
297 else if (hostname == NULL ||
298 strcmp (hostname, from_uri_tests[i].expected_hostname) != 0)
300 g_print ("\ng_filename_from_uri() test %d failed, expected hostname: %s, actual result: %s\n",
301 i, from_uri_tests[i].expected_hostname, (hostname) ? hostname : "NULL");
306 /* Give some output */
317 unsetenv ("G_BROKEN_FILENAMES");
321 run_from_uri_tests ();
323 return any_failed ? 1 : 0;