Moving files to packaging and extracing new tarball.
[profile/ivi/glib2.git] / glib / tests / uri.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 #include <glib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31
32 typedef struct
33 {
34   char *filename;
35   char *hostname;
36   char *expected_result;
37   GConvertError expected_error; /* If failed */
38 }  ToUriTest;
39
40 ToUriTest
41 to_uri_tests[] = {
42   { "/etc", NULL, "file:///etc"},
43   { "/etc", "", "file:///etc"},
44   { "/etc", "otherhost", "file://otherhost/etc"},
45 #ifdef G_OS_WIN32
46   { "/etc", "localhost", "file:///etc"},
47   { "c:\\windows", NULL, "file:///c:/windows"},
48   { "c:\\windows", "localhost", "file:///c:/windows"},
49   { "c:\\windows", "otherhost", "file://otherhost/c:/windows"},
50   { "\\\\server\\share\\dir", NULL, "file:////server/share/dir"},
51   { "\\\\server\\share\\dir", "localhost", "file:////server/share/dir"},
52 #else
53   { "/etc", "localhost", "file://localhost/etc"},
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 #ifndef G_PLATFORM_WIN32
60   { "/etc/\xE5\xE4\xF6", NULL, "file:///etc/%E5%E4%F6" },
61   { "/etc/\xC3\xB6\xC3\xA4\xC3\xA5", NULL, "file:///etc/%C3%B6%C3%A4%C3%A5"},
62 #endif
63   { "/etc", "\xC3\xB6\xC3\xA4\xC3\xA5", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
64   { "/etc", "\xE5\xE4\xF6", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
65   { "/etc/file with #%", NULL, "file:///etc/file%20with%20%23%25"},
66   { "", NULL, NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
67   { "", "", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
68   { "", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
69   { "", "otherhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
70   { "/0123456789", NULL, "file:///0123456789"},
71   { "/ABCDEFGHIJKLMNOPQRSTUVWXYZ", NULL, "file:///ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
72   { "/abcdefghijklmnopqrstuvwxyz", NULL, "file:///abcdefghijklmnopqrstuvwxyz"},
73   { "/-_.!~*'()", NULL, "file:///-_.!~*'()"},
74 #ifdef G_OS_WIN32
75   /* As '\\' is a path separator on Win32, it gets turned into '/' in the URI */
76   { "/\"#%<>[\\]^`{|}\x7F", NULL, "file:///%22%23%25%3C%3E%5B/%5D%5E%60%7B%7C%7D%7F"},
77 #else
78   /* On Unix, '\\' is a normal character in the file name */
79   { "/\"#%<>[\\]^`{|}\x7F", NULL, "file:///%22%23%25%3C%3E%5B%5C%5D%5E%60%7B%7C%7D%7F"},
80 #endif
81   { "/;@+$,", NULL, "file:///%3B@+$,"},
82   /* This and some of the following are of course as such illegal file names on Windows,
83    * and would not occur in real life.
84    */
85   { "/:", NULL, "file:///:"},
86   { "/?&=", NULL, "file:///%3F&="}, 
87   { "/", "0123456789-", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
88   { "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/"},
89   { "/", "abcdefghijklmnopqrstuvwxyz", "file://abcdefghijklmnopqrstuvwxyz/"},
90   { "/", "_.!~*'()", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
91   { "/", "\"#%<>[\\]^`{|}\x7F", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
92   { "/", ";?&=+$,", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
93   { "/", "/", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
94   { "/", "@:", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
95   { "/", "\x80\xFF", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
96   { "/", "\xC3\x80\xC3\xBF", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
97 };
98
99
100 typedef struct
101 {
102   char *uri;
103   char *expected_filename;
104   char *expected_hostname;
105   GConvertError expected_error; /* If failed */
106 }  FromUriTest;
107
108 FromUriTest
109 from_uri_tests[] = {
110   { "file:///etc", "/etc"},
111   { "file:/etc", "/etc"},
112 #ifdef G_OS_WIN32
113   /* On Win32 we don't return "localhost" hostames, just in case
114    * it isn't recognized anyway.
115    */
116   { "file://localhost/etc", "/etc", NULL},
117   { "file://localhost/etc/%23%25%20file", "/etc/#% file", NULL},
118   { "file://localhost/\xE5\xE4\xF6", "/\xe5\xe4\xf6", NULL},
119   { "file://localhost/%E5%E4%F6", "/\xe5\xe4\xf6", NULL},
120 #else
121   { "file://localhost/etc", "/etc", "localhost"},
122   { "file://localhost/etc/%23%25%20file", "/etc/#% file", "localhost"},
123   { "file://localhost/\xE5\xE4\xF6", "/\xe5\xe4\xf6", "localhost"},
124   { "file://localhost/%E5%E4%F6", "/\xe5\xe4\xf6", "localhost"},
125 #endif
126   { "file://otherhost/etc", "/etc", "otherhost"},
127   { "file://otherhost/etc/%23%25%20file", "/etc/#% file", "otherhost"},
128   { "file://%C3%B6%C3%A4%C3%A5/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
129   { "file:////etc/%C3%B6%C3%C3%C3%A5", "//etc/\xc3\xb6\xc3\xc3\xc3\xa5", NULL},
130   { "file://\xE5\xE4\xF6/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
131   { "file://%E5%E4%F6/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
132   { "file:///some/file#bad", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
133   { "file://some", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
134   { "", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
135   { "file:test", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
136   { "http://www.yahoo.com/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
137   { "file:////etc", "//etc"},
138   { "file://///etc", "///etc"},
139 #ifdef G_OS_WIN32
140   /* URIs with backslashes come from some nonstandard application, but accept them anyhow */
141   { "file:///c:\\foo", "c:\\foo"},
142   { "file:///c:/foo\\bar", "c:\\foo\\bar"},
143   /* Accept also the old Netscape drive-letter-and-vertical bar convention */
144   { "file:///c|/foo", "c:\\foo"},
145   { "file:////server/share/dir", "\\\\server\\share\\dir"},
146   { "file://localhost//server/share/foo", "\\\\server\\share\\foo"},
147   { "file://otherhost//server/share/foo", "\\\\server\\share\\foo", "otherhost"},
148 #else
149   { "file:///c:\\foo", "/c:\\foo"},
150   { "file:///c:/foo", "/c:/foo"},
151   { "file:////c:/foo", "//c:/foo"},
152 #endif
153   { "file://0123456789/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
154   { "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/", "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ"},
155   { "file://abcdefghijklmnopqrstuvwxyz/", "/", "abcdefghijklmnopqrstuvwxyz"},
156   { "file://-_.!~*'()/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
157   { "file://\"<>[\\]^`{|}\x7F/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
158   { "file://;?&=+$,/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
159   { "file://%C3%80%C3%BF/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
160   { "file://@/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
161   { "file://:/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
162   { "file://#/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
163   { "file://%23/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
164   { "file://%2F/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
165 };
166
167 static void
168 run_to_uri_tests (void)
169 {
170   int i;
171   gchar *res;
172   GError *error;
173
174   for (i = 0; i < G_N_ELEMENTS (to_uri_tests); i++)
175     {
176       error = NULL;
177       res = g_filename_to_uri (to_uri_tests[i].filename,
178                                to_uri_tests[i].hostname,
179                                &error);
180
181       if (res)
182         g_assert_cmpstr (res, ==, to_uri_tests[i].expected_result);
183       else
184         g_assert_error (error, G_CONVERT_ERROR, to_uri_tests[i].expected_error);
185
186       g_free (res);
187       g_clear_error (&error);
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 #ifdef G_OS_WIN32
207       {
208         gchar *p, *slash;
209         p = from_uri_tests[i].expected_filename = g_strdup (from_uri_tests[i].expected_filename);
210         while ((slash = strchr (p, '/')) != NULL)
211           {
212             *slash = '\\';
213             p = slash + 1;
214           }
215       }
216 #endif
217       if (res)
218         g_assert_cmpstr (res, ==, from_uri_tests[i].expected_filename);
219       else
220         g_assert_error (error, G_CONVERT_ERROR, from_uri_tests[i].expected_error);
221       g_assert_cmpstr (hostname, ==, from_uri_tests[i].expected_hostname);
222
223       g_free (res);
224       g_free (hostname);
225       g_clear_error (&error);
226     }
227 }
228
229 static gint
230 safe_strcmp_filename (const gchar *a, const gchar *b)
231 {
232 #ifndef G_OS_WIN32
233   return g_strcmp0 (a, b);
234 #else
235   if (!a || !b)
236     return g_strcmp0 (a, b);
237   else
238     {
239       while (*a && *b)
240         {
241           if ((G_IS_DIR_SEPARATOR (*a) && G_IS_DIR_SEPARATOR (*b)) ||
242               *a == *b)
243             a++, b++;
244           else
245             return (*a - *b);
246         }
247       return (*a - *b);
248     }
249 #endif
250 }
251
252 static gint
253 safe_strcmp_hostname (const gchar *a, const gchar *b)
254 {
255   if (a == NULL)
256     a = "";
257   if (b == NULL)
258     b = "";
259 #ifndef G_OS_WIN32
260   return g_strcmp0 (a, b);
261 #else
262   if (g_strcmp0 (a, "localhost") == 0 && b == NULL)
263     return 0;
264   else
265     return g_strcmp0 (a, b);
266 #endif
267 }
268
269 static void
270 run_roundtrip_tests (void)
271 {
272   int i;
273   gchar *uri, *hostname, *res;
274   GError *error;
275
276   for (i = 0; i < G_N_ELEMENTS (to_uri_tests); i++)
277     {
278       if (to_uri_tests[i].expected_error != 0)
279         continue;
280
281       error = NULL;
282       uri = g_filename_to_uri (to_uri_tests[i].filename,
283                                to_uri_tests[i].hostname,
284                                &error);
285       g_assert_no_error (error);
286
287       hostname = NULL;
288       res = g_filename_from_uri (uri, &hostname, &error);
289       g_assert_no_error (error);
290
291       g_assert (safe_strcmp_filename (to_uri_tests[i].filename, res) == 0);
292       g_assert (safe_strcmp_hostname (to_uri_tests[i].hostname, hostname) == 0);
293       g_free (res);
294       g_free (uri);
295       g_free (hostname);
296     }
297 }
298
299 static void
300 run_uri_list_tests (void)
301 {
302   /* straight from the RFC */
303   gchar *list =
304     "# urn:isbn:0-201-08372-8\r\n"
305     "http://www.huh.org/books/foo.html\r\n"
306     "http://www.huh.org/books/foo.pdf   \r\n"
307     "   ftp://ftp.foo.org/books/foo.txt\r\n";
308   gchar *expected_uris[] = {
309     "http://www.huh.org/books/foo.html",
310     "http://www.huh.org/books/foo.pdf",
311     "ftp://ftp.foo.org/books/foo.txt"
312   };
313
314   gchar **uris;
315   gint j;
316
317   uris = g_uri_list_extract_uris (list);
318   g_assert_cmpint (g_strv_length (uris), ==, 3);
319
320   for (j = 0; j < 3; j++)
321     g_assert_cmpstr (uris[j], ==, expected_uris[j]);
322
323   g_strfreev (uris);
324
325   uris = g_uri_list_extract_uris ("# just hot air\r\n# more hot air");
326   g_assert_cmpint (g_strv_length (uris), ==, 0);
327   g_strfreev (uris);
328 }
329
330 static void
331 test_uri_unescape (void)
332 {
333   gchar *s;
334
335   s = g_uri_unescape_string ("%2Babc %4F",  NULL);
336   g_assert_cmpstr (s, ==, "+abc O");
337   g_free (s);
338   g_assert_cmpstr (g_uri_unescape_string ("%2Babc %4F",  "+"), ==, NULL);
339   g_assert_cmpstr (g_uri_unescape_string ("%00abc %4F",  "+/"), ==, NULL);
340   g_assert_cmpstr (g_uri_unescape_string ("%0",  NULL), ==, NULL);
341   g_assert_cmpstr (g_uri_unescape_string ("%ra",  NULL), ==, NULL);
342   g_assert_cmpstr (g_uri_unescape_string ("%2r",  NULL), ==, NULL);
343   g_assert_cmpstr (g_uri_unescape_string (NULL,  NULL), ==, NULL);
344 }
345
346 static void
347 test_uri_escape (void)
348 {
349   gchar *s;
350
351   s = g_uri_escape_string ("abcdefgABCDEFG._~", NULL, FALSE);
352   g_assert_cmpstr (s, ==, "abcdefgABCDEFG._~");
353   g_free (s);
354   s = g_uri_escape_string (":+ \\?#", NULL, FALSE);
355   g_assert_cmpstr (s, ==, "%3A%2B%20%5C%3F%23");
356   g_free (s);
357   s = g_uri_escape_string ("a+b:c", "+", FALSE);
358   g_assert_cmpstr (s, ==, "a+b%3Ac");
359   g_free (s);
360 }
361
362 static void
363 test_uri_scheme (void)
364 {
365   gchar *s;
366
367   s = g_uri_parse_scheme ("ftp://ftp.gtk.org");
368   g_assert_cmpstr (s, ==, "ftp");
369   g_free (s);
370   s = g_uri_parse_scheme ("1bad:");
371   g_assert (s == NULL);
372   s = g_uri_parse_scheme ("bad");
373   g_assert (s == NULL);
374 }
375
376 int
377 main (int   argc,
378       char *argv[])
379 {
380   g_test_init (&argc, &argv, NULL);
381
382   g_test_add_func ("/uri/to-uri", run_to_uri_tests);
383   g_test_add_func ("/uri/from-uri", run_from_uri_tests);
384   g_test_add_func ("/uri/roundtrip", run_roundtrip_tests);
385   g_test_add_func ("/uri/list", run_uri_list_tests);
386   g_test_add_func ("/uri/unescape", test_uri_unescape);
387   g_test_add_func ("/uri/escape", test_uri_escape);
388   g_test_add_func ("/uri/scheme", test_uri_scheme);
389
390   return g_test_run ();
391 }