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.1 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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
34 char *expected_result
;
35 GConvertError expected_error
; /* If failed */
40 { "/etc", NULL
, "file:///etc"},
41 { "/etc", "", "file:///etc"},
42 { "/etc", "otherhost", "file://otherhost/etc"},
44 { "/etc", "localhost", "file:///etc"},
45 { "c:\\windows", NULL
, "file:///c:/windows"},
46 { "c:\\windows", "localhost", "file:///c:/windows"},
47 { "c:\\windows", "otherhost", "file://otherhost/c:/windows"},
48 { "\\\\server\\share\\dir", NULL
, "file:////server/share/dir"},
49 { "\\\\server\\share\\dir", "localhost", "file:////server/share/dir"},
51 { "/etc", "localhost", "file://localhost/etc"},
52 { "c:\\windows", NULL
, NULL
, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH
}, /* it's important to get this error on Unix */
53 { "c:\\windows", "localhost", NULL
, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH
},
54 { "c:\\windows", "otherhost", NULL
, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH
},
56 { "etc", "localhost", NULL
, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH
},
57 #ifndef G_PLATFORM_WIN32
58 { "/etc/\xE5\xE4\xF6", NULL
, "file:///etc/%E5%E4%F6" },
59 { "/etc/\xC3\xB6\xC3\xA4\xC3\xA5", NULL
, "file:///etc/%C3%B6%C3%A4%C3%A5"},
61 { "/etc", "\xC3\xB6\xC3\xA4\xC3\xA5", NULL
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
},
62 { "/etc", "\xE5\xE4\xF6", 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:///-_.!~*'()"},
73 /* As '\\' is a path separator on Win32, it gets turned into '/' in the URI */
74 { "/\"#%<>[\\]^`{|}\x7F", NULL
, "file:///%22%23%25%3C%3E%5B/%5D%5E%60%7B%7C%7D%7F"},
76 /* On Unix, '\\' is a normal character in the file name */
77 { "/\"#%<>[\\]^`{|}\x7F", NULL
, "file:///%22%23%25%3C%3E%5B%5C%5D%5E%60%7B%7C%7D%7F"},
79 { "/;@+$,", NULL
, "file:///%3B@+$,"},
80 /* This and some of the following are of course as such illegal file names on Windows,
81 * and would not occur in real life.
83 { "/:", NULL
, "file:///:"},
84 { "/?&=", NULL
, "file:///%3F&="},
85 { "/", "0123456789-", NULL
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
},
86 { "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/"},
87 { "/", "abcdefghijklmnopqrstuvwxyz", "file://abcdefghijklmnopqrstuvwxyz/"},
88 { "/", "_.!~*'()", NULL
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
},
89 { "/", "\"#%<>[\\]^`{|}\x7F", NULL
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
},
90 { "/", ";?&=+$,", NULL
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
},
91 { "/", "/", NULL
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
},
92 { "/", "@:", NULL
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
},
93 { "/", "\x80\xFF", NULL
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
},
94 { "/", "\xC3\x80\xC3\xBF", NULL
, G_CONVERT_ERROR_ILLEGAL_SEQUENCE
},
101 char *expected_filename
;
102 char *expected_hostname
;
103 GConvertError expected_error
; /* If failed */
108 { "file:///etc", "/etc"},
109 { "file:/etc", "/etc"},
111 /* On Win32 we don't return "localhost" hostames, just in case
112 * it isn't recognized anyway.
114 { "file://localhost/etc", "/etc", NULL
},
115 { "file://localhost/etc/%23%25%20file", "/etc/#% file", NULL
},
116 { "file://localhost/\xE5\xE4\xF6", "/\xe5\xe4\xf6", NULL
},
117 { "file://localhost/%E5%E4%F6", "/\xe5\xe4\xf6", NULL
},
119 { "file://localhost/etc", "/etc", "localhost"},
120 { "file://localhost/etc/%23%25%20file", "/etc/#% file", "localhost"},
121 { "file://localhost/\xE5\xE4\xF6", "/\xe5\xe4\xf6", "localhost"},
122 { "file://localhost/%E5%E4%F6", "/\xe5\xe4\xf6", "localhost"},
124 { "file://otherhost/etc", "/etc", "otherhost"},
125 { "file://otherhost/etc/%23%25%20file", "/etc/#% file", "otherhost"},
126 { "file://%C3%B6%C3%A4%C3%A5/etc", NULL
, NULL
, G_CONVERT_ERROR_BAD_URI
},
127 { "file:////etc/%C3%B6%C3%C3%C3%A5", "//etc/\xc3\xb6\xc3\xc3\xc3\xa5", NULL
},
128 { "file://\xE5\xE4\xF6/etc", NULL
, NULL
, G_CONVERT_ERROR_BAD_URI
},
129 { "file://%E5%E4%F6/etc", NULL
, NULL
, G_CONVERT_ERROR_BAD_URI
},
130 { "file:///some/file#bad", NULL
, NULL
, G_CONVERT_ERROR_BAD_URI
},
131 { "file://some", NULL
, NULL
, G_CONVERT_ERROR_BAD_URI
},
132 { "", NULL
, NULL
, G_CONVERT_ERROR_BAD_URI
},
133 { "file:test", NULL
, NULL
, G_CONVERT_ERROR_BAD_URI
},
134 { "http://www.yahoo.com/", NULL
, NULL
, G_CONVERT_ERROR_BAD_URI
},
135 { "file:////etc", "//etc"},
136 { "file://///etc", "///etc"},
138 /* URIs with backslashes come from some nonstandard application, but accept them anyhow */
139 { "file:///c:\\foo", "c:\\foo"},
140 { "file:///c:/foo\\bar", "c:\\foo\\bar"},
141 /* Accept also the old Netscape drive-letter-and-vertical bar convention */
142 { "file:///c|/foo", "c:\\foo"},
143 { "file:////server/share/dir", "\\\\server\\share\\dir"},
144 { "file://localhost//server/share/foo", "\\\\server\\share\\foo"},
145 { "file://otherhost//server/share/foo", "\\\\server\\share\\foo", "otherhost"},
147 { "file:///c:\\foo", "/c:\\foo"},
148 { "file:///c:/foo", "/c:/foo"},
149 { "file:////c:/foo", "//c:/foo"},
151 { "file://0123456789/", NULL
, NULL
, G_CONVERT_ERROR_BAD_URI
},
152 { "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/", "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
153 { "file://abcdefghijklmnopqrstuvwxyz/", "/", "abcdefghijklmnopqrstuvwxyz"},
154 { "file://-_.!~*'()/", NULL
, NULL
, G_CONVERT_ERROR_BAD_URI
},
155 { "file://\"<>[\\]^`{|}\x7F/", NULL
, NULL
, G_CONVERT_ERROR_BAD_URI
},
156 { "file://;?&=+$,/", NULL
, NULL
, G_CONVERT_ERROR_BAD_URI
},
157 { "file://%C3%80%C3%BF/", NULL
, NULL
, G_CONVERT_ERROR_BAD_URI
},
158 { "file://@/", NULL
, NULL
, G_CONVERT_ERROR_BAD_URI
},
159 { "file://:/", NULL
, NULL
, G_CONVERT_ERROR_BAD_URI
},
160 { "file://#/", NULL
, NULL
, G_CONVERT_ERROR_BAD_URI
},
161 { "file://%23/", NULL
, NULL
, G_CONVERT_ERROR_BAD_URI
},
162 { "file://%2F/", NULL
, NULL
, G_CONVERT_ERROR_BAD_URI
},
166 run_to_uri_tests (void)
172 for (i
= 0; i
< G_N_ELEMENTS (to_uri_tests
); i
++)
175 res
= g_filename_to_uri (to_uri_tests
[i
].filename
,
176 to_uri_tests
[i
].hostname
,
180 g_assert_cmpstr (res
, ==, to_uri_tests
[i
].expected_result
);
182 g_assert_error (error
, G_CONVERT_ERROR
, to_uri_tests
[i
].expected_error
);
185 g_clear_error (&error
);
190 run_from_uri_tests (void)
197 for (i
= 0; i
< G_N_ELEMENTS (from_uri_tests
); i
++)
200 res
= g_filename_from_uri (from_uri_tests
[i
].uri
,
205 if (from_uri_tests
[i
].expected_filename
)
208 p
= from_uri_tests
[i
].expected_filename
= g_strdup (from_uri_tests
[i
].expected_filename
);
209 while ((slash
= strchr (p
, '/')) != NULL
)
217 g_assert_cmpstr (res
, ==, from_uri_tests
[i
].expected_filename
);
219 g_assert_error (error
, G_CONVERT_ERROR
, from_uri_tests
[i
].expected_error
);
220 g_assert_cmpstr (hostname
, ==, from_uri_tests
[i
].expected_hostname
);
224 g_clear_error (&error
);
229 safe_strcmp_filename (const gchar
*a
, const gchar
*b
)
232 return g_strcmp0 (a
, b
);
235 return g_strcmp0 (a
, b
);
240 if ((G_IS_DIR_SEPARATOR (*a
) && G_IS_DIR_SEPARATOR (*b
)) ||
252 safe_strcmp_hostname (const gchar
*a
, const gchar
*b
)
259 return strcmp (a
, b
);
261 if (strcmp (a
, "localhost") == 0 && !*b
)
264 return strcmp (a
, b
);
269 run_roundtrip_tests (void)
272 gchar
*uri
, *hostname
, *res
;
275 for (i
= 0; i
< G_N_ELEMENTS (to_uri_tests
); i
++)
277 if (to_uri_tests
[i
].expected_error
!= 0)
281 uri
= g_filename_to_uri (to_uri_tests
[i
].filename
,
282 to_uri_tests
[i
].hostname
,
284 g_assert_no_error (error
);
287 res
= g_filename_from_uri (uri
, &hostname
, &error
);
288 g_assert_no_error (error
);
290 g_assert (safe_strcmp_filename (to_uri_tests
[i
].filename
, res
) == 0);
291 g_assert (safe_strcmp_hostname (to_uri_tests
[i
].hostname
, hostname
) == 0);
299 run_uri_list_tests (void)
301 /* straight from the RFC */
303 "# urn:isbn:0-201-08372-8\r\n"
304 "http://www.huh.org/books/foo.html\r\n"
305 "http://www.huh.org/books/foo.pdf \r\n"
306 " ftp://ftp.foo.org/books/foo.txt\r\n";
307 gchar
*expected_uris
[] = {
308 "http://www.huh.org/books/foo.html",
309 "http://www.huh.org/books/foo.pdf",
310 "ftp://ftp.foo.org/books/foo.txt"
316 uris
= g_uri_list_extract_uris (list
);
317 g_assert_cmpint (g_strv_length (uris
), ==, 3);
319 for (j
= 0; j
< 3; j
++)
320 g_assert_cmpstr (uris
[j
], ==, expected_uris
[j
]);
324 uris
= g_uri_list_extract_uris ("# just hot air\r\n# more hot air");
325 g_assert_cmpint (g_strv_length (uris
), ==, 0);
330 test_uri_unescape (void)
334 s
= g_uri_unescape_string ("%2Babc %4F", NULL
);
335 g_assert_cmpstr (s
, ==, "+abc O");
337 g_assert_cmpstr (g_uri_unescape_string ("%2Babc %4F", "+"), ==, NULL
);
338 g_assert_cmpstr (g_uri_unescape_string ("%00abc %4F", "+/"), ==, NULL
);
339 g_assert_cmpstr (g_uri_unescape_string ("%0", NULL
), ==, NULL
);
340 g_assert_cmpstr (g_uri_unescape_string ("%ra", NULL
), ==, NULL
);
341 g_assert_cmpstr (g_uri_unescape_string ("%2r", NULL
), ==, NULL
);
342 g_assert_cmpstr (g_uri_unescape_string (NULL
, NULL
), ==, NULL
);
346 test_uri_escape (void)
350 s
= g_uri_escape_string ("abcdefgABCDEFG._~", NULL
, FALSE
);
351 g_assert_cmpstr (s
, ==, "abcdefgABCDEFG._~");
353 s
= g_uri_escape_string (":+ \\?#", NULL
, FALSE
);
354 g_assert_cmpstr (s
, ==, "%3A%2B%20%5C%3F%23");
356 s
= g_uri_escape_string ("a+b:c", "+", FALSE
);
357 g_assert_cmpstr (s
, ==, "a+b%3Ac");
359 s
= g_uri_escape_string ("a+b:c\303\234", "+", TRUE
);
360 g_assert_cmpstr (s
, ==, "a+b%3Ac\303\234");
365 test_uri_scheme (void)
369 s
= g_uri_parse_scheme ("ftp://ftp.gtk.org");
370 g_assert_cmpstr (s
, ==, "ftp");
372 s
= g_uri_parse_scheme ("1bad:");
373 g_assert (s
== NULL
);
374 s
= g_uri_parse_scheme ("bad");
375 g_assert (s
== NULL
);
382 g_test_init (&argc
, &argv
, NULL
);
384 g_test_add_func ("/uri/to-uri", run_to_uri_tests
);
385 g_test_add_func ("/uri/from-uri", run_from_uri_tests
);
386 g_test_add_func ("/uri/roundtrip", run_roundtrip_tests
);
387 g_test_add_func ("/uri/list", run_uri_list_tests
);
388 g_test_add_func ("/uri/unescape", test_uri_unescape
);
389 g_test_add_func ("/uri/escape", test_uri_escape
);
390 g_test_add_func ("/uri/scheme", test_uri_scheme
);
392 return g_test_run ();