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", "localhost", "file://localhost/etc"},
48 { "/etc", "otherhost", "file://otherhost/etc"},
50 { "c:\\windows", NULL, "file:///c:\\windows"}, /* these 3 tests almost certainly fail */
51 { "c:\\windows", "localhost", "file://localhost/c:\\windows"},
52 { "c:\\windows", "otherhost", "file://otherhost/c:\\windows"},
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 { "/etc/öäå", NULL, NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
60 { "/etc/öäå", NULL, "file:///etc/%C3%B6%C3%A4%C3%A5"},
61 { "/etc", "öäå", "file://%C3%B6%C3%A4%C3%A5/etc"},
62 { "/etc", "åäö", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
63 { "/etc/file with #%", NULL, "file:///etc/file%20with%20%23%25"},
64 { "", NULL, NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
65 { "", "", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
66 { "", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
67 { "", "otherhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
68 { "/0123456789", NULL, "file:///0123456789"},
69 { "/ABCDEFGHIJKLMNOPQRSTUVWXYZ", NULL, "file:///ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
70 { "/abcdefghijklmnopqrstuvwxyz", NULL, "file:///abcdefghijklmnopqrstuvwxyz"},
71 { "/-_.!~*'()", NULL, "file:///-_.!~*'()"},
72 { "/\"#%<>[\\]^`{|}\x7F", NULL, "file:///%22%23%25%3C%3E%5B%5C%5D%5E%60%7B%7C%7D%7F"},
73 { "/;@+$,", NULL, "file:///%3B%40%2B%24%2C"},
74 { "/:", NULL, "file:///:"}, /* not escaped even though reserved as side effect of DOS support -- is that really what we want on Unix? */
75 { "/?&=", NULL, "file:///?&="}, /* these are not escaped and other reserved characters are -- is that really what we want? */
76 { "/", "0123456789", "file://0123456789/"},
77 { "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/"},
78 { "/", "abcdefghijklmnopqrstuvwxyz", "file://abcdefghijklmnopqrstuvwxyz/"},
79 { "/", "-_.!~*'()", "file://-_.!~*'()/"},
80 { "/", "\"#%<>[\\]^`{|}\x7F", "file://%22%23%25%3C%3E%5B%5C%5D%5E%60%7B%7C%7D%7F/"},
81 { "/", ";?&=+$,", "file://%3B%3F%26%3D%2B%24%2C/"},
82 { "/", "/", "file:////"}, /* should be "file://%2F/" or an error */
83 { "/", "@:", "file://@:/"}, /* these are not escaped and other reserved characters are -- is that really what we want? */
84 { "/", "\x80\xFF", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
85 { "/", "\xC3\x80\xC3\xBF", "file://%C3%80%C3%BF/"},
92 char *expected_filename;
93 char *expected_hostname;
94 GConvertError expected_error; /* If failed */
99 { "file:///etc", "/etc"},
100 { "file:/etc", "/etc"},
101 { "file://localhost/etc", "/etc", "localhost"},
102 { "file://localhost/etc/%23%25%20file", "/etc/#% file", "localhost"},
103 { "file://otherhost/etc", "/etc", "otherhost"},
104 { "file://otherhost/etc/%23%25%20file", "/etc/#% file", "otherhost"},
105 { "file://%C3%B6%C3%A4%C3%A5/etc", "/etc", "öäå"},
106 { "file:////etc/%C3%B6%C3%C3%C3%A5", NULL, NULL, G_CONVERT_ERROR_INVALID_URI},
107 { "file://localhost/åäö", NULL, NULL, G_CONVERT_ERROR_INVALID_URI},
108 { "file://åäö/etc", NULL, NULL, G_CONVERT_ERROR_INVALID_URI},
109 { "file:///some/file#bad", NULL, NULL, G_CONVERT_ERROR_INVALID_URI},
110 { "file://some", NULL, NULL, G_CONVERT_ERROR_INVALID_URI},
111 { "", NULL, NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_FILE_URI}, /* should be G_CONVERT_ERROR_INVALID_URI */
112 { "file:test", NULL, NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_FILE_URI},
113 { "http://www.yahoo.com/", NULL, NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_FILE_URI},
114 { "file:////etc", "/etc"}, /* should be "//etc" -- mistake in code for DOS results in dropped slash */
115 { "file://///etc", "//etc"}, /* should be "///etc" -- mistake in code for DOS results in dropped slash */
116 { "file:///c:\\foo", "/c:\\foo"}, /* should be "c:\\foo" on DOS perhaps, but that would be bad for Unix */
117 { "file:///c:/foo", "/c:/foo"}, /* should be "c:/foo" on DOS perhaps, but that would be bad for Unix */
118 { "file:////c:/foo", "/c:/foo"}, /* should be "//c:/foo" on Unix */
119 { "file://0123456789/", "/", "0123456789"},
120 { "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/", "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
121 { "file://abcdefghijklmnopqrstuvwxyz/", "/", "abcdefghijklmnopqrstuvwxyz"},
122 { "file://-_.!~*'()/", "/", "-_.!~*'()"},
123 { "file://\"<>[\\]^`{|}\x7F/", "/", "\"<>[\\]^`{|}\x7F"},
124 { "file://;?&=+$,/", "/", ";?&=+$,"},
125 { "file://%C3%80%C3%BF/", "/", "\xC3\x80\xC3\xBF"},
126 { "file://@/", "/", "@"},
127 { "file://:/", "/", ":"},
128 { "file://#/", NULL, NULL, G_CONVERT_ERROR_INVALID_URI},
129 { "file://%23/", "/", "#"}, /* is it dangerous to return a hostname with a "#" character in it? */
130 { "file://%2F/", "/", "/"}, /* is it dangerous to return a hostname with a "/" character in it? */
134 static gboolean any_failed = FALSE;
137 run_to_uri_tests (void)
143 for (i = 0; i < G_N_ELEMENTS (to_uri_tests); i++)
146 res = g_filename_to_uri (to_uri_tests[i].filename,
147 to_uri_tests[i].hostname,
150 if (to_uri_tests[i].expected_result == NULL)
154 g_print ("\ng_filename_to_uri() test %d failed, expected to return NULL, actual result: %s\n", i, res);
161 g_print ("\ng_filename_to_uri() test %d failed, returned NULL, but didn't set error\n", i);
164 else if (error->domain != G_CONVERT_ERROR)
166 g_print ("\ng_filename_to_uri() test %d failed, returned NULL, set non G_CONVERT_ERROR error\n", i);
169 else if (error->code != to_uri_tests[i].expected_error)
171 g_print ("\ng_filename_to_uri() test %d failed as expected, but set wrong errorcode %d instead of expected %d \n",
172 i, error->code, to_uri_tests[i].expected_error);
177 else if (res == NULL || strcmp (res, to_uri_tests[i].expected_result) != 0)
179 g_print ("\ng_filename_to_uri() test %d failed, expected result: %s, actual result: %s\n",
180 i, to_uri_tests[i].expected_result, (res) ? res : "NULL");
182 g_print ("Error message: %s\n", error->message);
186 /* Give some output */
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,
206 if (from_uri_tests[i].expected_filename == NULL)
210 g_print ("\ng_filename_from_uri() test %d failed, expected to return NULL, actual result: %s\n", i, res);
217 g_print ("\ng_filename_from_uri() test %d failed, returned NULL, but didn't set error\n", i);
220 else if (error->domain != G_CONVERT_ERROR)
222 g_print ("\ng_filename_from_uri() test %d failed, returned NULL, set non G_CONVERT_ERROR error\n", i);
225 else if (error->code != from_uri_tests[i].expected_error)
227 g_print ("\ng_filename_from_uri() test %d failed as expected, but set wrong errorcode %d instead of expected %d \n",
228 i, error->code, from_uri_tests[i].expected_error);
235 if (res == NULL || strcmp (res, from_uri_tests[i].expected_filename) != 0)
237 g_print ("\ng_filename_from_uri() test %d failed, expected result: %s, actual result: %s\n",
238 i, from_uri_tests[i].expected_filename, (res) ? res : "NULL");
242 if (from_uri_tests[i].expected_hostname == NULL)
244 if (hostname != NULL)
246 g_print ("\ng_filename_from_uri() test %d failed, expected no hostname, got: %s\n",
251 else if (hostname == NULL ||
252 strcmp (hostname, from_uri_tests[i].expected_hostname) != 0)
254 g_print ("\ng_filename_from_uri() test %d failed, expected hostname: %s, actual result: %s\n",
255 i, from_uri_tests[i].expected_hostname, (hostname) ? hostname : "NULL");
260 /* Give some output */
271 run_from_uri_tests ();
273 return any_failed ? 1 : 0;