Ugh, fix braino.
[platform/upstream/glib.git] / tests / uri-test.c
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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
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/. 
25  */
26
27 #undef G_DISABLE_ASSERT
28 #undef G_LOG_DOMAIN
29
30 #include <config.h>
31
32 #include <glib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdlib.h>
36
37 typedef struct
38 {
39   char *filename;
40   char *hostname;
41   char *expected_result;
42   GConvertError expected_error; /* If failed */
43 }  ToUriTest;
44
45 ToUriTest
46 to_uri_tests[] = {
47   { "/etc", NULL, "file:///etc"},
48   { "/etc", "", "file:///etc"},
49   { "/etc", "otherhost", "file://otherhost/etc"},
50 #ifdef G_OS_WIN32
51   { "/etc", "localhost", "file:///etc"},
52   { "c:\\windows", NULL, "file:///c:/windows"},
53   { "c:\\windows", "localhost", "file:///c:/windows"},
54   { "c:\\windows", "otherhost", "file://otherhost/c:/windows"},
55   { "\\\\server\\share\\dir", NULL, "file:////server/share/dir"},
56   { "\\\\server\\share\\dir", "localhost", "file:////server/share/dir"},
57 #else
58   { "/etc", "localhost", "file://localhost/etc"},
59   { "c:\\windows", NULL, NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH}, /* it's important to get this error on Unix */
60   { "c:\\windows", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
61   { "c:\\windows", "otherhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
62 #endif
63   { "etc", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
64 #ifndef G_PLATFORM_WIN32
65   { "/etc/\xE5\xE4\xF6", NULL, "file:///etc/%E5%E4%F6" },
66   { "/etc/\xC3\xB6\xC3\xA4\xC3\xA5", NULL, "file:///etc/%C3%B6%C3%A4%C3%A5"},
67 #endif
68   { "/etc", "\xC3\xB6\xC3\xA4\xC3\xA5", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
69   { "/etc", "\xE5\xE4\xF6", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
70   { "/etc/file with #%", NULL, "file:///etc/file%20with%20%23%25"},
71   { "", NULL, NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
72   { "", "", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
73   { "", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
74   { "", "otherhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
75   { "/0123456789", NULL, "file:///0123456789"},
76   { "/ABCDEFGHIJKLMNOPQRSTUVWXYZ", NULL, "file:///ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
77   { "/abcdefghijklmnopqrstuvwxyz", NULL, "file:///abcdefghijklmnopqrstuvwxyz"},
78   { "/-_.!~*'()", NULL, "file:///-_.!~*'()"},
79 #ifdef G_OS_WIN32
80   /* As '\\' is a path separator on Win32, it gets turned into '/' in the URI */
81   { "/\"#%<>[\\]^`{|}\x7F", NULL, "file:///%22%23%25%3C%3E%5B/%5D%5E%60%7B%7C%7D%7F"},
82 #else
83   /* On Unix, '\\' is a normal character in the file name */
84   { "/\"#%<>[\\]^`{|}\x7F", NULL, "file:///%22%23%25%3C%3E%5B%5C%5D%5E%60%7B%7C%7D%7F"},
85 #endif
86   { "/;@+$,", NULL, "file:///%3B@+$,"},
87   /* This and some of the following are of course as such illegal file names on Windows,
88    * and would not occur in real life.
89    */
90   { "/:", NULL, "file:///:"},
91   { "/?&=", NULL, "file:///%3F&="}, 
92   { "/", "0123456789-", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
93   { "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/"},
94   { "/", "abcdefghijklmnopqrstuvwxyz", "file://abcdefghijklmnopqrstuvwxyz/"},
95   { "/", "_.!~*'()", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
96   { "/", "\"#%<>[\\]^`{|}\x7F", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
97   { "/", ";?&=+$,", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
98   { "/", "/", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
99   { "/", "@:", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
100   { "/", "\x80\xFF", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
101   { "/", "\xC3\x80\xC3\xBF", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
102 };
103
104
105 typedef struct
106 {
107   char *uri;
108   char *expected_filename;
109   char *expected_hostname;
110   GConvertError expected_error; /* If failed */
111 }  FromUriTest;
112
113 FromUriTest
114 from_uri_tests[] = {
115   { "file:///etc", "/etc"},
116   { "file:/etc", "/etc"},
117 #ifdef G_OS_WIN32
118   /* On Win32 we don't return "localhost" hostames, just in case
119    * it isn't recognized anyway.
120    */
121   { "file://localhost/etc", "/etc", NULL},
122   { "file://localhost/etc/%23%25%20file", "/etc/#% file", NULL},
123   { "file://localhost/\xE5\xE4\xF6", "/\xe5\xe4\xf6", NULL},
124   { "file://localhost/%E5%E4%F6", "/\xe5\xe4\xf6", NULL},
125 #else
126   { "file://localhost/etc", "/etc", "localhost"},
127   { "file://localhost/etc/%23%25%20file", "/etc/#% file", "localhost"},
128   { "file://localhost/\xE5\xE4\xF6", "/\xe5\xe4\xf6", "localhost"},
129   { "file://localhost/%E5%E4%F6", "/\xe5\xe4\xf6", "localhost"},
130 #endif
131   { "file://otherhost/etc", "/etc", "otherhost"},
132   { "file://otherhost/etc/%23%25%20file", "/etc/#% file", "otherhost"},
133   { "file://%C3%B6%C3%A4%C3%A5/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
134   { "file:////etc/%C3%B6%C3%C3%C3%A5", "//etc/\xc3\xb6\xc3\xc3\xc3\xa5", NULL},
135   { "file://\xE5\xE4\xF6/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
136   { "file://%E5%E4%F6/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
137   { "file:///some/file#bad", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
138   { "file://some", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
139   { "", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
140   { "file:test", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
141   { "http://www.yahoo.com/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
142   { "file:////etc", "//etc"},
143   { "file://///etc", "///etc"},
144 #ifdef G_OS_WIN32
145   /* URIs with backslashes come from some nonstandard application, but accept them anyhow */
146   { "file:///c:\\foo", "c:\\foo"},
147   { "file:///c:/foo\\bar", "c:\\foo\\bar"},
148   /* Accept also the old Netscape drive-letter-and-vertical bar convention */
149   { "file:///c|/foo", "c:\\foo"},
150   { "file:////server/share/dir", "\\\\server\\share\\dir"},
151   { "file://localhost//server/share/foo", "\\\\server\\share\\foo"},
152   { "file://otherhost//server/share/foo", "\\\\server\\share\\foo", "otherhost"},
153 #else
154   { "file:///c:\\foo", "/c:\\foo"},
155   { "file:///c:/foo", "/c:/foo"},
156   { "file:////c:/foo", "//c:/foo"},
157 #endif
158   { "file://0123456789/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
159   { "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/", "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
160   { "file://abcdefghijklmnopqrstuvwxyz/", "/", "abcdefghijklmnopqrstuvwxyz"},
161   { "file://-_.!~*'()/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
162   { "file://\"<>[\\]^`{|}\x7F/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
163   { "file://;?&=+$,/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
164   { "file://%C3%80%C3%BF/", 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://#/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
168   { "file://%23/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
169   { "file://%2F/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
170 };
171
172
173 static gboolean any_failed = FALSE;
174
175 static void
176 run_to_uri_tests (void)
177 {
178   int i;
179   gchar *res;
180   GError *error;
181   
182   for (i = 0; i < G_N_ELEMENTS (to_uri_tests); i++)
183     {
184       error = NULL;
185       res = g_filename_to_uri (to_uri_tests[i].filename,
186                                to_uri_tests[i].hostname,
187                                &error);
188
189       if (to_uri_tests[i].expected_result == NULL)
190         {
191           if (res != NULL)
192             {
193               g_print ("\ng_filename_to_uri() test %d failed, expected to return NULL, actual result: %s\n", i, res);
194               any_failed = TRUE;
195             }
196           else
197             {
198               if (error == NULL)
199                 {
200                   g_print ("\ng_filename_to_uri() test %d failed, returned NULL, but didn't set error\n", i);
201                   any_failed = TRUE;
202                 }
203               else if (error->domain != G_CONVERT_ERROR)
204                 {
205                   g_print ("\ng_filename_to_uri() test %d failed, returned NULL, set non G_CONVERT_ERROR error\n", i);
206                   any_failed = TRUE;
207                 }
208               else if (error->code != to_uri_tests[i].expected_error)
209                 {
210                   g_print ("\ng_filename_to_uri() test %d failed as expected, but set wrong errorcode %d instead of expected %d \n",
211                            i, error->code, to_uri_tests[i].expected_error);
212                   any_failed = TRUE;
213                 }
214             }
215         }
216       else if (res == NULL || strcmp (res, to_uri_tests[i].expected_result) != 0)
217         {
218           g_print ("\ng_filename_to_uri() test %d failed, expected result: %s, actual result: %s\n",
219                    i, to_uri_tests[i].expected_result, (res) ? res : "NULL");
220           if (error)
221             g_print ("Error message: %s\n", error->message);
222           any_failed = TRUE;
223         }
224     }
225 }
226
227 static void
228 run_from_uri_tests (void)
229 {
230   int i;
231   gchar *res;
232   gchar *hostname;
233   GError *error;
234   
235   for (i = 0; i < G_N_ELEMENTS (from_uri_tests); i++)
236     {
237       error = NULL;
238       res = g_filename_from_uri (from_uri_tests[i].uri,
239                                  &hostname,
240                                  &error);
241
242       if (from_uri_tests[i].expected_filename == NULL)
243         {
244           if (res != NULL)
245             {
246               g_print ("\ng_filename_from_uri() test %d failed, expected to return NULL, actual result: %s\n", i, res);
247               any_failed = TRUE;
248             }
249           else
250             {
251               if (error == NULL)
252                 {
253                   g_print ("\ng_filename_from_uri() test %d failed, returned NULL, but didn't set error\n", i);
254                   any_failed = TRUE;
255                 }
256               else if (error->domain != G_CONVERT_ERROR)
257                 {
258                   g_print ("\ng_filename_from_uri() test %d failed, returned NULL, set non G_CONVERT_ERROR error\n", i);
259                   any_failed = TRUE;
260                 }
261               else if (error->code != from_uri_tests[i].expected_error)
262                 {
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);
265                   any_failed = TRUE;
266                 }
267             }
268         }
269       else
270         {
271 #ifdef G_OS_WIN32
272           gchar *slash, *p;
273
274           p = from_uri_tests[i].expected_filename = g_strdup (from_uri_tests[i].expected_filename);
275           while ((slash = strchr (p, '/')) != NULL)
276             {
277               *slash = '\\';
278               p = slash + 1;
279             }
280 #endif
281           if (res == NULL || strcmp (res, from_uri_tests[i].expected_filename) != 0)
282             {
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");
285               any_failed = TRUE;
286             }
287
288           if (from_uri_tests[i].expected_hostname == NULL)
289             {
290               if (hostname != NULL)
291                 {
292                   g_print ("\ng_filename_from_uri() test %d failed, expected no hostname, got: %s\n",
293                            i, hostname);
294                   any_failed = TRUE;
295                 }
296             }
297           else if (hostname == NULL ||
298                    strcmp (hostname, from_uri_tests[i].expected_hostname) != 0)
299             {
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");
302               any_failed = TRUE;
303             }
304         }
305     }
306 }
307
308 static gint
309 safe_strcmp (const gchar *a, const gchar *b)
310 {
311   return strcmp (a ? a : "", b ? b : "");
312 }
313
314 static gint
315 safe_strcmp_filename (const gchar *a, const gchar *b)
316 {
317 #ifndef G_OS_WIN32
318   return safe_strcmp (a, b);
319 #else
320   if (!a)
321     return safe_strcmp ("", b);
322   else if (!b)
323     return safe_strcmp (a, "");
324   else
325     {
326       while (*a && *b)
327         {
328           if ((G_IS_DIR_SEPARATOR (*a) && G_IS_DIR_SEPARATOR (*b)) ||
329               *a == *b)
330             a++, b++;
331           else
332             return (*a - *b);
333         }
334       return (*a - *b);
335     }
336 #endif
337 }
338
339 static gint
340 safe_strcmp_hostname (const gchar *a, const gchar *b)
341 {
342 #ifndef G_OS_WIN32
343   return safe_strcmp (a, b);
344 #else
345   if (safe_strcmp (a, "localhost") == 0 && b == NULL)
346     return 0;
347   else
348     return safe_strcmp (a, b);
349 #endif
350 }
351
352 static void
353 run_roundtrip_tests (void)
354 {
355   int i;
356   gchar *uri, *hostname, *res;
357   GError *error;
358   
359   for (i = 0; i < G_N_ELEMENTS (to_uri_tests); i++)
360     {
361       if (to_uri_tests[i].expected_error != 0)
362         continue;
363
364       error = NULL;
365       uri = g_filename_to_uri (to_uri_tests[i].filename,
366                                to_uri_tests[i].hostname,
367                                &error);
368       
369       if (error != NULL)
370         {
371           g_print ("g_filename_to_uri failed unexpectedly: %s\n", 
372                    error->message);
373           any_failed = TRUE;
374           continue;
375         }
376       
377       error = NULL;
378       res = g_filename_from_uri (uri, &hostname, &error);
379       if (error != NULL)
380         {
381           g_print ("g_filename_from_uri failed unexpectedly: %s\n", 
382                    error->message);
383           any_failed = TRUE;
384           continue;
385         }
386
387       if (safe_strcmp_filename (to_uri_tests[i].filename, res))
388         {
389           g_print ("roundtrip test %d failed, filename modified: "
390                    " expected \"%s\", but got \"%s\"\n",
391                    i, to_uri_tests[i].filename, res);
392           any_failed = TRUE;
393         }
394
395       if (safe_strcmp_hostname (to_uri_tests[i].hostname, hostname))
396         {
397           g_print ("roundtrip test %d failed, hostname modified: "
398                      " expected \"%s\", but got \"%s\"\n",
399                    i, to_uri_tests[i].hostname, hostname);
400           any_failed = TRUE;
401         }
402     }
403 }
404
405 static void
406 run_uri_list_tests (void)
407 {
408   /* straight from the RFC */
409   gchar *list =
410     "# urn:isbn:0-201-08372-8\r\n"
411     "http://www.huh.org/books/foo.html\r\n"
412     "http://www.huh.org/books/foo.pdf   \r\n"
413     "   ftp://ftp.foo.org/books/foo.txt\r\n";
414   gchar *expected_uris[] = {
415     "http://www.huh.org/books/foo.html",
416     "http://www.huh.org/books/foo.pdf",
417     "ftp://ftp.foo.org/books/foo.txt"
418   };
419
420   gchar **uris;
421   gint j;
422
423   uris = g_uri_list_extract_uris (list);
424   
425   if (g_strv_length (uris) != 3)
426     {
427       g_print ("uri list test failed: "
428                " expected %d uris, but got %d\n",
429                3, g_strv_length (uris));
430       any_failed = TRUE;
431     }
432   
433   for (j = 0; j < 3; j++)
434     {
435       if (safe_strcmp (uris[j], expected_uris[j])) 
436         {
437           g_print ("uri list test failed: "
438                    " expected \"%s\", but got \"%s\"\n",
439                    expected_uris[j], uris[j]);
440           any_failed = TRUE;
441         }
442     }
443
444   g_strfreev (uris);
445
446   uris = g_uri_list_extract_uris ("# just hot air\r\n# more hot air");
447   if (g_strv_length (uris) != 0)
448     {
449       g_print ("uri list test 2 failed: "
450                " expected %d uris, but got %d (first is \"%s\")\n",
451                0, g_strv_length (uris), uris[0]);
452       any_failed = TRUE;
453     }
454   
455 }
456
457 int
458 main (int   argc,
459       char *argv[])
460 {
461 #ifdef G_OS_UNIX
462 #  ifdef HAVE_UNSETENV  
463   unsetenv ("G_BROKEN_FILENAMES");
464 #  else
465   /* putenv with no = isn't standard, but works to unset the variable
466    * on some systems
467    */
468   putenv ("G_BROKEN_FILENAMES");
469 #  endif  
470 #endif
471
472   run_to_uri_tests ();
473   run_from_uri_tests ();
474   run_roundtrip_tests ();
475   run_uri_list_tests ();
476
477   return any_failed ? 1 : 0;
478 }