Updated name of error from G_CONVERT_ERROR_NOT_LOCAL_FILE to
[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", "localhost", "file://localhost/etc"},
48   { "/etc", "otherhost", "file://otherhost/etc"},
49 #ifdef G_OS_WIN32
50   { "c:\\windows", NULL, "file:///c:\\windows"}, /* these 3 tests almost certainly fail */
51   { "c:\\windows", "localhost", "file://localhost/c:\\windows"},
52   { "c:\\windows", "otherhost", "file://otherhost/c:\\windows"},
53 #else
54   { "c:\\windows", NULL, NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH}, /* it's important to get this error on Unix */
55   { "c:\\windows", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
56   { "c:\\windows", "otherhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
57 #endif
58   { "etc", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
59   { "/etc/öäå", NULL, NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
60   { "/etc/öäå", NULL, "file:///etc/%C3%B6%C3%A4%C3%A5"},
61   { "/etc", "öäå", "file://%C3%B6%C3%A4%C3%A5/etc"},
62   { "/etc", "åäö", 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   { "/\"#%<>[\\]^`{|}\x7F", NULL, "file:///%22%23%25%3C%3E%5B%5C%5D%5E%60%7B%7C%7D%7F"},
73   { "/;@+$,", NULL, "file:///%3B%40%2B%24%2C"},
74   { "/:", NULL, "file:///:"}, /* not escaped even though reserved as side effect of DOS support -- is that really what we want on Unix? */
75   { "/?&=", NULL, "file:///?&="}, /* these are not escaped and other reserved characters are -- is that really what we want? */
76   { "/", "0123456789", "file://0123456789/"},
77   { "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/"},
78   { "/", "abcdefghijklmnopqrstuvwxyz", "file://abcdefghijklmnopqrstuvwxyz/"},
79   { "/", "-_.!~*'()", "file://-_.!~*'()/"},
80   { "/", "\"#%<>[\\]^`{|}\x7F", "file://%22%23%25%3C%3E%5B%5C%5D%5E%60%7B%7C%7D%7F/"},
81   { "/", ";?&=+$,", "file://%3B%3F%26%3D%2B%24%2C/"},
82   { "/", "/", "file:////"}, /* should be "file://%2F/" or an error */
83   { "/", "@:", "file://@:/"}, /* these are not escaped and other reserved characters are -- is that really what we want? */
84   { "/", "\x80\xFF", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
85   { "/", "\xC3\x80\xC3\xBF", "file://%C3%80%C3%BF/"},
86 };
87
88
89 typedef struct
90 {
91   char *uri;
92   char *expected_filename;
93   char *expected_hostname;
94   GConvertError expected_error; /* If failed */
95 }  FromUriTest;
96
97 FromUriTest
98 from_uri_tests[] = {
99   { "file:///etc", "/etc"},
100   { "file:/etc", "/etc"},
101   { "file://localhost/etc", "/etc", "localhost"},
102   { "file://localhost/etc/%23%25%20file", "/etc/#% file", "localhost"},
103   { "file://otherhost/etc", "/etc", "otherhost"},
104   { "file://otherhost/etc/%23%25%20file", "/etc/#% file", "otherhost"},
105   { "file://%C3%B6%C3%A4%C3%A5/etc", "/etc", "öäå"},
106   { "file:////etc/%C3%B6%C3%C3%C3%A5", NULL, NULL, G_CONVERT_ERROR_INVALID_URI},
107   { "file://localhost/åäö", NULL, NULL, G_CONVERT_ERROR_INVALID_URI},
108   { "file://åäö/etc", NULL, NULL, G_CONVERT_ERROR_INVALID_URI},
109   { "file:///some/file#bad", NULL, NULL, G_CONVERT_ERROR_INVALID_URI},
110   { "file://some", NULL, NULL, G_CONVERT_ERROR_INVALID_URI},
111   { "", NULL, NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_FILE_URI}, /* should be G_CONVERT_ERROR_INVALID_URI */
112   { "file:test", NULL, NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_FILE_URI},
113   { "http://www.yahoo.com/", NULL, NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_FILE_URI},
114   { "file:////etc", "/etc"}, /* should be "//etc" -- mistake in code for DOS results in dropped slash */
115   { "file://///etc", "//etc"}, /* should be "///etc" -- mistake in code for DOS results in dropped slash */
116   { "file:///c:\\foo", "/c:\\foo"}, /* should be "c:\\foo" on DOS perhaps, but that would be bad for Unix */
117   { "file:///c:/foo", "/c:/foo"}, /* should be "c:/foo" on DOS perhaps, but that would be bad for Unix */
118   { "file:////c:/foo", "/c:/foo"}, /* should be "//c:/foo" on Unix */
119   { "file://0123456789/", "/", "0123456789"},
120   { "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/", "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
121   { "file://abcdefghijklmnopqrstuvwxyz/", "/", "abcdefghijklmnopqrstuvwxyz"},
122   { "file://-_.!~*'()/", "/", "-_.!~*'()"},
123   { "file://\"<>[\\]^`{|}\x7F/", "/", "\"<>[\\]^`{|}\x7F"},
124   { "file://;?&=+$,/", "/", ";?&=+$,"},
125   { "file://%C3%80%C3%BF/", "/", "\xC3\x80\xC3\xBF"},
126   { "file://@/", "/", "@"},
127   { "file://:/", "/", ":"},
128   { "file://#/", NULL, NULL, G_CONVERT_ERROR_INVALID_URI},
129   { "file://%23/", "/", "#"}, /* is it dangerous to return a hostname with a "#" character in it? */
130   { "file://%2F/", "/", "/"}, /* is it dangerous to return a hostname with a "/" character in it? */
131 };
132
133
134 static gboolean any_failed = FALSE;
135
136 static void
137 run_to_uri_tests (void)
138 {
139   int i;
140   gchar *res;
141   GError *error;
142   
143   for (i = 0; i < G_N_ELEMENTS (to_uri_tests); i++)
144     {
145       error = NULL;
146       res = g_filename_to_uri (to_uri_tests[i].filename,
147                                to_uri_tests[i].hostname,
148                                &error);
149
150       if (to_uri_tests[i].expected_result == NULL)
151         {
152           if (res != NULL)
153             {
154               g_print ("\ng_filename_to_uri() test %d failed, expected to return NULL, actual result: %s\n", i, res);
155               any_failed = TRUE;
156             }
157           else
158             {
159               if (error == NULL)
160                 {
161                   g_print ("\ng_filename_to_uri() test %d failed, returned NULL, but didn't set error\n", i);
162                   any_failed = TRUE;
163                 }
164               else if (error->domain != G_CONVERT_ERROR)
165                 {
166                   g_print ("\ng_filename_to_uri() test %d failed, returned NULL, set non G_CONVERT_ERROR error\n", i);
167                   any_failed = TRUE;
168                 }
169               else if (error->code != to_uri_tests[i].expected_error)
170                 {
171                   g_print ("\ng_filename_to_uri() test %d failed as expected, but set wrong errorcode %d instead of expected %d \n",
172                            i, error->code, to_uri_tests[i].expected_error);
173                   any_failed = TRUE;
174                 }
175             }
176         }
177       else if (res == NULL || strcmp (res, to_uri_tests[i].expected_result) != 0)
178         {
179           g_print ("\ng_filename_to_uri() test %d failed, expected result: %s, actual result: %s\n",
180                    i, to_uri_tests[i].expected_result, (res) ? res : "NULL");
181           if (error)
182             g_print ("Error message: %s\n", error->message);
183           any_failed = TRUE;
184         }
185       
186       /* Give some output */
187       g_print (".");
188     }
189 }
190
191 static void
192 run_from_uri_tests (void)
193 {
194   int i;
195   gchar *res;
196   gchar *hostname;
197   GError *error;
198   
199   for (i = 0; i < G_N_ELEMENTS (from_uri_tests); i++)
200     {
201       error = NULL;
202       res = g_filename_from_uri (from_uri_tests[i].uri,
203                                  &hostname,
204                                  &error);
205
206       if (from_uri_tests[i].expected_filename == NULL)
207         {
208           if (res != NULL)
209             {
210               g_print ("\ng_filename_from_uri() test %d failed, expected to return NULL, actual result: %s\n", i, res);
211               any_failed = TRUE;
212             }
213           else
214             {
215               if (error == NULL)
216                 {
217                   g_print ("\ng_filename_from_uri() test %d failed, returned NULL, but didn't set error\n", i);
218                   any_failed = TRUE;
219                 }
220               else if (error->domain != G_CONVERT_ERROR)
221                 {
222                   g_print ("\ng_filename_from_uri() test %d failed, returned NULL, set non G_CONVERT_ERROR error\n", i);
223                   any_failed = TRUE;
224                 }
225               else if (error->code != from_uri_tests[i].expected_error)
226                 {
227                   g_print ("\ng_filename_from_uri() test %d failed as expected, but set wrong errorcode %d instead of expected %d \n",
228                            i, error->code, from_uri_tests[i].expected_error);
229                   any_failed = TRUE;
230                 }
231             }
232         }
233       else
234         {
235           if (res == NULL || strcmp (res, from_uri_tests[i].expected_filename) != 0)
236             {
237               g_print ("\ng_filename_from_uri() test %d failed, expected result: %s, actual result: %s\n",
238                        i, from_uri_tests[i].expected_filename, (res) ? res : "NULL");
239               any_failed = TRUE;
240             }
241
242           if (from_uri_tests[i].expected_hostname == NULL)
243             {
244               if (hostname != NULL)
245                 {
246                   g_print ("\ng_filename_from_uri() test %d failed, expected no hostname, got: %s\n",
247                            i, hostname);
248                   any_failed = TRUE;
249                 }
250             }
251           else if (hostname == NULL ||
252                    strcmp (hostname, from_uri_tests[i].expected_hostname) != 0)
253             {
254               g_print ("\ng_filename_from_uri() test %d failed, expected hostname: %s, actual result: %s\n",
255                        i, from_uri_tests[i].expected_hostname, (hostname) ? hostname : "NULL");
256               any_failed = TRUE;
257             }
258         }
259       
260       /* Give some output */
261       g_print (".");
262     }
263   g_print ("\n");
264 }
265
266 int
267 main (int   argc,
268       char *argv[])
269 {
270   run_to_uri_tests ();
271   run_from_uri_tests ();
272
273   return any_failed ? 1 : 0;
274 }