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/\xE5\xE4\xF6", NULL, NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
67 { "/etc/\xC3\xB6\xC3\xA4\xC3\xA5", NULL, "file:///etc/%C3%B6%C3%A4%C3%A5"},
69 { "/etc", "\xC3\xB6\xC3\xA4\xC3\xA5", "file://%C3%B6%C3%A4%C3%A5/etc"},
70 { "/etc", "\xE5\xE4\xF6", 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-", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
94 { "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/"},
95 { "/", "abcdefghijklmnopqrstuvwxyz", "file://abcdefghijklmnopqrstuvwxyz/"},
96 { "/", "_.!~*'()", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
97 { "/", "\"#%<>[\\]^`{|}\x7F", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
98 { "/", ";?&=+$,", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
99 { "/", "/", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
100 { "/", "@:", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
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", "\xC3\xB6\xC3\xA4\xC3\xA5"},
131 { "file:////etc/%C3%B6%C3%C3%C3%A5", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
132 { "file://localhost/\xE5\xE4\xF6", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
133 { "file://\xE5\xE4\xF6/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
134 { "file://localhost/%E5%E4%F6", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
135 { "file://%E5%E4%F6/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
136 { "file:///some/file#bad", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
137 { "file://some", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
138 { "", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
139 { "file:test", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
140 { "http://www.yahoo.com/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
141 { "file:////etc", "//etc"},
142 { "file://///etc", "///etc"},
144 /* URIs with backslashes come from some nonstandard application, but accept them anyhow */
145 { "file:///c:\\foo", "c:\\foo"},
146 { "file:///c:/foo\\bar", "c:\\foo\\bar"},
147 /* Accept also the old Netscape drive-letter-and-vertical bar convention */
148 { "file:///c|/foo", "c:\\foo"},
149 { "file:////server/share/dir", "\\\\server\\share\\dir"},
150 { "file://localhost//server/share/foo", "\\\\server\\share\\foo"},
151 { "file://otherhost//server/share/foo", "\\\\server\\share\\foo", "otherhost"},
153 { "file:///c:\\foo", "/c:\\foo"},
154 { "file:///c:/foo", "/c:/foo"},
155 { "file:////c:/foo", "//c:/foo"},
157 { "file://0123456789/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
158 { "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/", "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
159 { "file://abcdefghijklmnopqrstuvwxyz/", "/", "abcdefghijklmnopqrstuvwxyz"},
160 { "file://-_.!~*'()/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
161 { "file://\"<>[\\]^`{|}\x7F/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
162 { "file://;?&=+$,/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
163 { "file://%C3%80%C3%BF/", "/", "\xC3\x80\xC3\xBF"},
164 { "file://@/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
165 { "file://:/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
166 { "file://#/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
167 { "file://%23/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
168 { "file://%2F/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
172 static gboolean any_failed = FALSE;
175 run_to_uri_tests (void)
181 for (i = 0; i < G_N_ELEMENTS (to_uri_tests); i++)
184 res = g_filename_to_uri (to_uri_tests[i].filename,
185 to_uri_tests[i].hostname,
188 if (to_uri_tests[i].expected_result == NULL)
192 g_print ("\ng_filename_to_uri() test %d failed, expected to return NULL, actual result: %s\n", i, res);
199 g_print ("\ng_filename_to_uri() test %d failed, returned NULL, but didn't set error\n", i);
202 else if (error->domain != G_CONVERT_ERROR)
204 g_print ("\ng_filename_to_uri() test %d failed, returned NULL, set non G_CONVERT_ERROR error\n", i);
207 else if (error->code != to_uri_tests[i].expected_error)
209 g_print ("\ng_filename_to_uri() test %d failed as expected, but set wrong errorcode %d instead of expected %d \n",
210 i, error->code, to_uri_tests[i].expected_error);
215 else if (res == NULL || strcmp (res, to_uri_tests[i].expected_result) != 0)
217 g_print ("\ng_filename_to_uri() test %d failed, expected result: %s, actual result: %s\n",
218 i, to_uri_tests[i].expected_result, (res) ? res : "NULL");
220 g_print ("Error message: %s\n", error->message);
224 /* Give some output */
230 run_from_uri_tests (void)
237 for (i = 0; i < G_N_ELEMENTS (from_uri_tests); i++)
240 res = g_filename_from_uri (from_uri_tests[i].uri,
244 if (from_uri_tests[i].expected_filename == NULL)
248 g_print ("\ng_filename_from_uri() test %d failed, expected to return NULL, actual result: %s\n", i, res);
255 g_print ("\ng_filename_from_uri() test %d failed, returned NULL, but didn't set error\n", i);
258 else if (error->domain != G_CONVERT_ERROR)
260 g_print ("\ng_filename_from_uri() test %d failed, returned NULL, set non G_CONVERT_ERROR error\n", i);
263 else if (error->code != from_uri_tests[i].expected_error)
265 g_print ("\ng_filename_from_uri() test %d failed as expected, but set wrong errorcode %d instead of expected %d \n",
266 i, error->code, from_uri_tests[i].expected_error);
276 p = from_uri_tests[i].expected_filename = g_strdup (from_uri_tests[i].expected_filename);
277 while ((slash = strchr (p, '/')) != NULL)
283 if (res == NULL || strcmp (res, from_uri_tests[i].expected_filename) != 0)
285 g_print ("\ng_filename_from_uri() test %d failed, expected result: %s, actual result: %s\n",
286 i, from_uri_tests[i].expected_filename, (res) ? res : "NULL");
290 if (from_uri_tests[i].expected_hostname == NULL)
292 if (hostname != NULL)
294 g_print ("\ng_filename_from_uri() test %d failed, expected no hostname, got: %s\n",
299 else if (hostname == NULL ||
300 strcmp (hostname, from_uri_tests[i].expected_hostname) != 0)
302 g_print ("\ng_filename_from_uri() test %d failed, expected hostname: %s, actual result: %s\n",
303 i, from_uri_tests[i].expected_hostname, (hostname) ? hostname : "NULL");
308 /* Give some output */
319 # ifdef HAVE_UNSETENV
320 unsetenv ("G_BROKEN_FILENAMES");
322 /* putenv with no = isn't standard, but works to unset the variable
325 putenv ("G_BROKEN_FILENAMES");
330 run_from_uri_tests ();
332 return any_failed ? 1 : 0;