nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* GLIB - Library of useful routines for C programming |
2 | * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald |
||
3 | * |
||
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. |
||
8 | * |
||
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. |
||
13 | * |
||
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/>. |
||
16 | */ |
||
17 | |||
18 | /* |
||
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/. |
||
23 | */ |
||
24 | |||
25 | #include <glib.h> |
||
26 | #include <stdio.h> |
||
27 | #include <string.h> |
||
28 | #include <stdlib.h> |
||
29 | |||
30 | typedef struct |
||
31 | { |
||
32 | char *filename; |
||
33 | char *hostname; |
||
34 | char *expected_result; |
||
35 | GConvertError expected_error; /* If failed */ |
||
36 | } ToUriTest; |
||
37 | |||
38 | ToUriTest |
||
39 | to_uri_tests[] = { |
||
40 | { "/etc", NULL, "file:///etc"}, |
||
41 | { "/etc", "", "file:///etc"}, |
||
42 | { "/etc", "otherhost", "file://otherhost/etc"}, |
||
43 | #ifdef G_OS_WIN32 |
||
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"}, |
||
50 | #else |
||
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}, |
||
55 | #endif |
||
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"}, |
||
60 | #endif |
||
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:///-_.!~*'()"}, |
||
72 | #ifdef G_OS_WIN32 |
||
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"}, |
||
75 | #else |
||
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"}, |
||
78 | #endif |
||
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. |
||
82 | */ |
||
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}, |
||
95 | }; |
||
96 | |||
97 | |||
98 | typedef struct |
||
99 | { |
||
100 | char *uri; |
||
101 | char *expected_filename; |
||
102 | char *expected_hostname; |
||
103 | GConvertError expected_error; /* If failed */ |
||
104 | } FromUriTest; |
||
105 | |||
106 | FromUriTest |
||
107 | from_uri_tests[] = { |
||
108 | { "file:///etc", "/etc"}, |
||
109 | { "file:/etc", "/etc"}, |
||
110 | #ifdef G_OS_WIN32 |
||
111 | /* On Win32 we don't return "localhost" hostames, just in case |
||
112 | * it isn't recognized anyway. |
||
113 | */ |
||
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}, |
||
118 | #else |
||
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"}, |
||
123 | #endif |
||
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"}, |
||
137 | #ifdef G_OS_WIN32 |
||
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"}, |
||
146 | #else |
||
147 | { "file:///c:\\foo", "/c:\\foo"}, |
||
148 | { "file:///c:/foo", "/c:/foo"}, |
||
149 | { "file:////c:/foo", "//c:/foo"}, |
||
150 | #endif |
||
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}, |
||
163 | }; |
||
164 | |||
165 | static void |
||
166 | run_to_uri_tests (void) |
||
167 | { |
||
168 | int i; |
||
169 | gchar *res; |
||
170 | GError *error; |
||
171 | |||
172 | for (i = 0; i < G_N_ELEMENTS (to_uri_tests); i++) |
||
173 | { |
||
174 | error = NULL; |
||
175 | res = g_filename_to_uri (to_uri_tests[i].filename, |
||
176 | to_uri_tests[i].hostname, |
||
177 | &error); |
||
178 | |||
179 | if (res) |
||
180 | g_assert_cmpstr (res, ==, to_uri_tests[i].expected_result); |
||
181 | else |
||
182 | g_assert_error (error, G_CONVERT_ERROR, to_uri_tests[i].expected_error); |
||
183 | |||
184 | g_free (res); |
||
185 | g_clear_error (&error); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | static void |
||
190 | run_from_uri_tests (void) |
||
191 | { |
||
192 | int i; |
||
193 | gchar *res; |
||
194 | gchar *hostname; |
||
195 | GError *error; |
||
196 | |||
197 | for (i = 0; i < G_N_ELEMENTS (from_uri_tests); i++) |
||
198 | { |
||
199 | error = NULL; |
||
200 | res = g_filename_from_uri (from_uri_tests[i].uri, |
||
201 | &hostname, |
||
202 | &error); |
||
203 | |||
204 | #ifdef G_OS_WIN32 |
||
205 | if (from_uri_tests[i].expected_filename) |
||
206 | { |
||
207 | gchar *p, *slash; |
||
208 | p = from_uri_tests[i].expected_filename = g_strdup (from_uri_tests[i].expected_filename); |
||
209 | while ((slash = strchr (p, '/')) != NULL) |
||
210 | { |
||
211 | *slash = '\\'; |
||
212 | p = slash + 1; |
||
213 | } |
||
214 | } |
||
215 | #endif |
||
216 | if (res) |
||
217 | g_assert_cmpstr (res, ==, from_uri_tests[i].expected_filename); |
||
218 | else |
||
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); |
||
221 | |||
222 | g_free (res); |
||
223 | g_free (hostname); |
||
224 | g_clear_error (&error); |
||
225 | } |
||
226 | } |
||
227 | |||
228 | static gint |
||
229 | safe_strcmp_filename (const gchar *a, const gchar *b) |
||
230 | { |
||
231 | #ifndef G_OS_WIN32 |
||
232 | return g_strcmp0 (a, b); |
||
233 | #else |
||
234 | if (!a || !b) |
||
235 | return g_strcmp0 (a, b); |
||
236 | else |
||
237 | { |
||
238 | while (*a && *b) |
||
239 | { |
||
240 | if ((G_IS_DIR_SEPARATOR (*a) && G_IS_DIR_SEPARATOR (*b)) || |
||
241 | *a == *b) |
||
242 | a++, b++; |
||
243 | else |
||
244 | return (*a - *b); |
||
245 | } |
||
246 | return (*a - *b); |
||
247 | } |
||
248 | #endif |
||
249 | } |
||
250 | |||
251 | static gint |
||
252 | safe_strcmp_hostname (const gchar *a, const gchar *b) |
||
253 | { |
||
254 | if (a == NULL) |
||
255 | a = ""; |
||
256 | if (b == NULL) |
||
257 | b = ""; |
||
258 | #ifndef G_OS_WIN32 |
||
259 | return strcmp (a, b); |
||
260 | #else |
||
261 | if (strcmp (a, "localhost") == 0 && !*b) |
||
262 | return 0; |
||
263 | else |
||
264 | return strcmp (a, b); |
||
265 | #endif |
||
266 | } |
||
267 | |||
268 | static void |
||
269 | run_roundtrip_tests (void) |
||
270 | { |
||
271 | int i; |
||
272 | gchar *uri, *hostname, *res; |
||
273 | GError *error; |
||
274 | |||
275 | for (i = 0; i < G_N_ELEMENTS (to_uri_tests); i++) |
||
276 | { |
||
277 | if (to_uri_tests[i].expected_error != 0) |
||
278 | continue; |
||
279 | |||
280 | error = NULL; |
||
281 | uri = g_filename_to_uri (to_uri_tests[i].filename, |
||
282 | to_uri_tests[i].hostname, |
||
283 | &error); |
||
284 | g_assert_no_error (error); |
||
285 | |||
286 | hostname = NULL; |
||
287 | res = g_filename_from_uri (uri, &hostname, &error); |
||
288 | g_assert_no_error (error); |
||
289 | |||
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); |
||
292 | g_free (res); |
||
293 | g_free (uri); |
||
294 | g_free (hostname); |
||
295 | } |
||
296 | } |
||
297 | |||
298 | static void |
||
299 | run_uri_list_tests (void) |
||
300 | { |
||
301 | /* straight from the RFC */ |
||
302 | gchar *list = |
||
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" |
||
311 | }; |
||
312 | |||
313 | gchar **uris; |
||
314 | gint j; |
||
315 | |||
316 | uris = g_uri_list_extract_uris (list); |
||
317 | g_assert_cmpint (g_strv_length (uris), ==, 3); |
||
318 | |||
319 | for (j = 0; j < 3; j++) |
||
320 | g_assert_cmpstr (uris[j], ==, expected_uris[j]); |
||
321 | |||
322 | g_strfreev (uris); |
||
323 | |||
324 | uris = g_uri_list_extract_uris ("# just hot air\r\n# more hot air"); |
||
325 | g_assert_cmpint (g_strv_length (uris), ==, 0); |
||
326 | g_strfreev (uris); |
||
327 | } |
||
328 | |||
329 | static void |
||
330 | test_uri_unescape (void) |
||
331 | { |
||
332 | gchar *s; |
||
333 | |||
334 | s = g_uri_unescape_string ("%2Babc %4F", NULL); |
||
335 | g_assert_cmpstr (s, ==, "+abc O"); |
||
336 | g_free (s); |
||
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); |
||
343 | } |
||
344 | |||
345 | static void |
||
346 | test_uri_escape (void) |
||
347 | { |
||
348 | gchar *s; |
||
349 | |||
350 | s = g_uri_escape_string ("abcdefgABCDEFG._~", NULL, FALSE); |
||
351 | g_assert_cmpstr (s, ==, "abcdefgABCDEFG._~"); |
||
352 | g_free (s); |
||
353 | s = g_uri_escape_string (":+ \\?#", NULL, FALSE); |
||
354 | g_assert_cmpstr (s, ==, "%3A%2B%20%5C%3F%23"); |
||
355 | g_free (s); |
||
356 | s = g_uri_escape_string ("a+b:c", "+", FALSE); |
||
357 | g_assert_cmpstr (s, ==, "a+b%3Ac"); |
||
358 | g_free (s); |
||
359 | s = g_uri_escape_string ("a+b:c\303\234", "+", TRUE); |
||
360 | g_assert_cmpstr (s, ==, "a+b%3Ac\303\234"); |
||
361 | g_free (s); |
||
362 | } |
||
363 | |||
364 | static void |
||
365 | test_uri_scheme (void) |
||
366 | { |
||
367 | gchar *s; |
||
368 | |||
369 | s = g_uri_parse_scheme ("ftp://ftp.gtk.org"); |
||
370 | g_assert_cmpstr (s, ==, "ftp"); |
||
371 | g_free (s); |
||
372 | s = g_uri_parse_scheme ("1bad:"); |
||
373 | g_assert (s == NULL); |
||
374 | s = g_uri_parse_scheme ("bad"); |
||
375 | g_assert (s == NULL); |
||
376 | } |
||
377 | |||
378 | int |
||
379 | main (int argc, |
||
380 | char *argv[]) |
||
381 | { |
||
382 | g_test_init (&argc, &argv, NULL); |
||
383 | |||
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); |
||
391 | |||
392 | return g_test_run (); |
||
393 | } |