glib/giowin32.c glib/gmain.c glib/gstrfuncs.c Decorating variable
[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   /* g_filename_to_utf8 uses current code page on Win32, these tests assume that
66    * local filenames *are* in UTF-8.
67    */
68   { "/etc/\xE5\xE4\xF6", NULL, NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
69   { "/etc/\xC3\xB6\xC3\xA4\xC3\xA5", NULL, "file:///etc/%C3%B6%C3%A4%C3%A5"},
70 #endif
71   { "/etc", "\xC3\xB6\xC3\xA4\xC3\xA5", "file://%C3%B6%C3%A4%C3%A5/etc"},
72   { "/etc", "\xE5\xE4\xF6", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
73   { "/etc/file with #%", NULL, "file:///etc/file%20with%20%23%25"},
74   { "", NULL, NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
75   { "", "", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
76   { "", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
77   { "", "otherhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
78   { "/0123456789", NULL, "file:///0123456789"},
79   { "/ABCDEFGHIJKLMNOPQRSTUVWXYZ", NULL, "file:///ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
80   { "/abcdefghijklmnopqrstuvwxyz", NULL, "file:///abcdefghijklmnopqrstuvwxyz"},
81   { "/-_.!~*'()", NULL, "file:///-_.!~*'()"},
82 #ifdef G_OS_WIN32
83   /* As '\\' is a path separator on Win32, it gets turned into '/' in the URI */
84   { "/\"#%<>[\\]^`{|}\x7F", NULL, "file:///%22%23%25%3C%3E%5B/%5D%5E%60%7B%7C%7D%7F"},
85 #else
86   /* On Unix, '\\' is a normal character in the file name */
87   { "/\"#%<>[\\]^`{|}\x7F", NULL, "file:///%22%23%25%3C%3E%5B%5C%5D%5E%60%7B%7C%7D%7F"},
88 #endif
89   { "/;@+$,", NULL, "file:///%3B@+$,"},
90   /* This and some of the following are of course as such illegal file names on Windows,
91    * and would not occur in real life.
92    */
93   { "/:", NULL, "file:///:"},
94   { "/?&=", NULL, "file:///%3F&="}, 
95   { "/", "0123456789-", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
96   { "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/"},
97   { "/", "abcdefghijklmnopqrstuvwxyz", "file://abcdefghijklmnopqrstuvwxyz/"},
98   { "/", "_.!~*'()", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
99   { "/", "\"#%<>[\\]^`{|}\x7F", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
100   { "/", ";?&=+$,", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
101   { "/", "/", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
102   { "/", "@:", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
103   { "/", "\x80\xFF", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
104   { "/", "\xC3\x80\xC3\xBF", "file://%C3%80%C3%BF/"},
105 };
106
107
108 typedef struct
109 {
110   char *uri;
111   char *expected_filename;
112   char *expected_hostname;
113   GConvertError expected_error; /* If failed */
114 }  FromUriTest;
115
116 FromUriTest
117 from_uri_tests[] = {
118   { "file:///etc", "/etc"},
119   { "file:/etc", "/etc"},
120 #ifdef G_OS_WIN32
121   /* On Win32 we don't return "localhost" hostames, just in case
122    * it isn't recognized anyway.
123    */
124   { "file://localhost/etc", "/etc", NULL},
125   { "file://localhost/etc/%23%25%20file", "/etc/#% file", NULL},
126 #else
127   { "file://localhost/etc", "/etc", "localhost"},
128   { "file://localhost/etc/%23%25%20file", "/etc/#% file", "localhost"},
129 #endif
130   { "file://otherhost/etc", "/etc", "otherhost"},
131   { "file://otherhost/etc/%23%25%20file", "/etc/#% file", "otherhost"},
132   { "file://%C3%B6%C3%A4%C3%A5/etc", "/etc", "\xC3\xB6\xC3\xA4\xC3\xA5"},
133   { "file:////etc/%C3%B6%C3%C3%C3%A5", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
134   { "file://localhost/\xE5\xE4\xF6", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
135   { "file://\xE5\xE4\xF6/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
136   { "file://localhost/%E5%E4%F6", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
137   { "file://%E5%E4%F6/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
138   { "file:///some/file#bad", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
139   { "file://some", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
140   { "", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
141   { "file:test", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
142   { "http://www.yahoo.com/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
143   { "file:////etc", "//etc"},
144   { "file://///etc", "///etc"},
145 #ifdef G_OS_WIN32
146   /* URIs with backslashes come from some nonstandard application, but accept them anyhow */
147   { "file:///c:\\foo", "c:\\foo"},
148   { "file:///c:/foo\\bar", "c:\\foo\\bar"},
149   /* Accept also the old Netscape drive-letter-and-vertical bar convention */
150   { "file:///c|/foo", "c:\\foo"},
151   { "file:////server/share/dir", "\\\\server\\share\\dir"},
152   { "file://localhost//server/share/foo", "\\\\server\\share\\foo"},
153   { "file://otherhost//server/share/foo", "\\\\server\\share\\foo", "otherhost"},
154 #else
155   { "file:///c:\\foo", "/c:\\foo"},
156   { "file:///c:/foo", "/c:/foo"},
157   { "file:////c:/foo", "//c:/foo"},
158 #endif
159   { "file://0123456789/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
160   { "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/", "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
161   { "file://abcdefghijklmnopqrstuvwxyz/", "/", "abcdefghijklmnopqrstuvwxyz"},
162   { "file://-_.!~*'()/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
163   { "file://\"<>[\\]^`{|}\x7F/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
164   { "file://;?&=+$,/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
165   { "file://%C3%80%C3%BF/", "/", "\xC3\x80\xC3\xBF"},
166   { "file://@/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
167   { "file://:/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
168   { "file://#/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
169   { "file://%23/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
170   { "file://%2F/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
171 };
172
173
174 static gboolean any_failed = FALSE;
175
176 static void
177 run_to_uri_tests (void)
178 {
179   int i;
180   gchar *res;
181   GError *error;
182   
183   for (i = 0; i < G_N_ELEMENTS (to_uri_tests); i++)
184     {
185       error = NULL;
186       res = g_filename_to_uri (to_uri_tests[i].filename,
187                                to_uri_tests[i].hostname,
188                                &error);
189
190       if (to_uri_tests[i].expected_result == NULL)
191         {
192           if (res != NULL)
193             {
194               g_print ("\ng_filename_to_uri() test %d failed, expected to return NULL, actual result: %s\n", i, res);
195               any_failed = TRUE;
196             }
197           else
198             {
199               if (error == NULL)
200                 {
201                   g_print ("\ng_filename_to_uri() test %d failed, returned NULL, but didn't set error\n", i);
202                   any_failed = TRUE;
203                 }
204               else if (error->domain != G_CONVERT_ERROR)
205                 {
206                   g_print ("\ng_filename_to_uri() test %d failed, returned NULL, set non G_CONVERT_ERROR error\n", i);
207                   any_failed = TRUE;
208                 }
209               else if (error->code != to_uri_tests[i].expected_error)
210                 {
211                   g_print ("\ng_filename_to_uri() test %d failed as expected, but set wrong errorcode %d instead of expected %d \n",
212                            i, error->code, to_uri_tests[i].expected_error);
213                   any_failed = TRUE;
214                 }
215             }
216         }
217       else if (res == NULL || strcmp (res, to_uri_tests[i].expected_result) != 0)
218         {
219           g_print ("\ng_filename_to_uri() test %d failed, expected result: %s, actual result: %s\n",
220                    i, to_uri_tests[i].expected_result, (res) ? res : "NULL");
221           if (error)
222             g_print ("Error message: %s\n", error->message);
223           any_failed = TRUE;
224         }
225       
226       /* Give some output */
227       g_print (".");
228     }
229 }
230
231 static void
232 run_from_uri_tests (void)
233 {
234   int i;
235   gchar *res;
236   gchar *hostname;
237   GError *error;
238   
239   for (i = 0; i < G_N_ELEMENTS (from_uri_tests); i++)
240     {
241       error = NULL;
242       res = g_filename_from_uri (from_uri_tests[i].uri,
243                                  &hostname,
244                                  &error);
245
246       if (from_uri_tests[i].expected_filename == NULL)
247         {
248           if (res != NULL)
249             {
250               g_print ("\ng_filename_from_uri() test %d failed, expected to return NULL, actual result: %s\n", i, res);
251               any_failed = TRUE;
252             }
253           else
254             {
255               if (error == NULL)
256                 {
257                   g_print ("\ng_filename_from_uri() test %d failed, returned NULL, but didn't set error\n", i);
258                   any_failed = TRUE;
259                 }
260               else if (error->domain != G_CONVERT_ERROR)
261                 {
262                   g_print ("\ng_filename_from_uri() test %d failed, returned NULL, set non G_CONVERT_ERROR error\n", i);
263                   any_failed = TRUE;
264                 }
265               else if (error->code != from_uri_tests[i].expected_error)
266                 {
267                   g_print ("\ng_filename_from_uri() test %d failed as expected, but set wrong errorcode %d instead of expected %d \n",
268                            i, error->code, from_uri_tests[i].expected_error);
269                   any_failed = TRUE;
270                 }
271             }
272         }
273       else
274         {
275 #ifdef G_OS_WIN32
276           gchar *slash, *p;
277
278           p = from_uri_tests[i].expected_filename = g_strdup (from_uri_tests[i].expected_filename);
279           while ((slash = strchr (p, '/')) != NULL)
280             {
281               *slash = '\\';
282               p = slash + 1;
283             }
284 #endif
285           if (res == NULL || strcmp (res, from_uri_tests[i].expected_filename) != 0)
286             {
287               g_print ("\ng_filename_from_uri() test %d failed, expected result: %s, actual result: %s\n",
288                        i, from_uri_tests[i].expected_filename, (res) ? res : "NULL");
289               any_failed = TRUE;
290             }
291
292           if (from_uri_tests[i].expected_hostname == NULL)
293             {
294               if (hostname != NULL)
295                 {
296                   g_print ("\ng_filename_from_uri() test %d failed, expected no hostname, got: %s\n",
297                            i, hostname);
298                   any_failed = TRUE;
299                 }
300             }
301           else if (hostname == NULL ||
302                    strcmp (hostname, from_uri_tests[i].expected_hostname) != 0)
303             {
304               g_print ("\ng_filename_from_uri() test %d failed, expected hostname: %s, actual result: %s\n",
305                        i, from_uri_tests[i].expected_hostname, (hostname) ? hostname : "NULL");
306               any_failed = TRUE;
307             }
308         }
309       
310       /* Give some output */
311       g_print (".");
312     }
313   g_print ("\n");
314 }
315
316 int
317 main (int   argc,
318       char *argv[])
319 {
320 #ifdef G_OS_UNIX
321 #  ifdef HAVE_UNSETENV  
322   unsetenv ("G_BROKEN_FILENAMES");
323 #  else
324   /* putenv with no = isn't standard, but works to unset the variable
325    * on some systems
326    */
327   putenv ("G_BROKEN_FILENAMES");
328 #  endif  
329 #endif
330
331   run_to_uri_tests ();
332   run_from_uri_tests ();
333
334   return any_failed ? 1 : 0;
335 }