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