Simplify subprocesses in tests
[platform/upstream/glib.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       if (from_uri_tests[i].expected_filename)
208         {
209           gchar *p, *slash;
210           p = from_uri_tests[i].expected_filename = g_strdup (from_uri_tests[i].expected_filename);
211           while ((slash = strchr (p, '/')) != NULL)
212             {
213               *slash = '\\';
214               p = slash + 1;
215             }
216         }
217 #endif
218       if (res)
219         g_assert_cmpstr (res, ==, from_uri_tests[i].expected_filename);
220       else
221         g_assert_error (error, G_CONVERT_ERROR, from_uri_tests[i].expected_error);
222       g_assert_cmpstr (hostname, ==, from_uri_tests[i].expected_hostname);
223
224       g_free (res);
225       g_free (hostname);
226       g_clear_error (&error);
227     }
228 }
229
230 static gint
231 safe_strcmp_filename (const gchar *a, const gchar *b)
232 {
233 #ifndef G_OS_WIN32
234   return g_strcmp0 (a, b);
235 #else
236   if (!a || !b)
237     return g_strcmp0 (a, b);
238   else
239     {
240       while (*a && *b)
241         {
242           if ((G_IS_DIR_SEPARATOR (*a) && G_IS_DIR_SEPARATOR (*b)) ||
243               *a == *b)
244             a++, b++;
245           else
246             return (*a - *b);
247         }
248       return (*a - *b);
249     }
250 #endif
251 }
252
253 static gint
254 safe_strcmp_hostname (const gchar *a, const gchar *b)
255 {
256   if (a == NULL)
257     a = "";
258   if (b == NULL)
259     b = "";
260 #ifndef G_OS_WIN32
261   return strcmp (a, b);
262 #else
263   if (strcmp (a, "localhost") == 0 && !*b)
264     return 0;
265   else
266     return strcmp (a, b);
267 #endif
268 }
269
270 static void
271 run_roundtrip_tests (void)
272 {
273   int i;
274   gchar *uri, *hostname, *res;
275   GError *error;
276
277   for (i = 0; i < G_N_ELEMENTS (to_uri_tests); i++)
278     {
279       if (to_uri_tests[i].expected_error != 0)
280         continue;
281
282       error = NULL;
283       uri = g_filename_to_uri (to_uri_tests[i].filename,
284                                to_uri_tests[i].hostname,
285                                &error);
286       g_assert_no_error (error);
287
288       hostname = NULL;
289       res = g_filename_from_uri (uri, &hostname, &error);
290       g_assert_no_error (error);
291
292       g_assert (safe_strcmp_filename (to_uri_tests[i].filename, res) == 0);
293       g_assert (safe_strcmp_hostname (to_uri_tests[i].hostname, hostname) == 0);
294       g_free (res);
295       g_free (uri);
296       g_free (hostname);
297     }
298 }
299
300 static void
301 run_uri_list_tests (void)
302 {
303   /* straight from the RFC */
304   gchar *list =
305     "# urn:isbn:0-201-08372-8\r\n"
306     "http://www.huh.org/books/foo.html\r\n"
307     "http://www.huh.org/books/foo.pdf   \r\n"
308     "   ftp://ftp.foo.org/books/foo.txt\r\n";
309   gchar *expected_uris[] = {
310     "http://www.huh.org/books/foo.html",
311     "http://www.huh.org/books/foo.pdf",
312     "ftp://ftp.foo.org/books/foo.txt"
313   };
314
315   gchar **uris;
316   gint j;
317
318   uris = g_uri_list_extract_uris (list);
319   g_assert_cmpint (g_strv_length (uris), ==, 3);
320
321   for (j = 0; j < 3; j++)
322     g_assert_cmpstr (uris[j], ==, expected_uris[j]);
323
324   g_strfreev (uris);
325
326   uris = g_uri_list_extract_uris ("# just hot air\r\n# more hot air");
327   g_assert_cmpint (g_strv_length (uris), ==, 0);
328   g_strfreev (uris);
329 }
330
331 static void
332 test_uri_unescape (void)
333 {
334   gchar *s;
335
336   s = g_uri_unescape_string ("%2Babc %4F",  NULL);
337   g_assert_cmpstr (s, ==, "+abc O");
338   g_free (s);
339   g_assert_cmpstr (g_uri_unescape_string ("%2Babc %4F",  "+"), ==, NULL);
340   g_assert_cmpstr (g_uri_unescape_string ("%00abc %4F",  "+/"), ==, NULL);
341   g_assert_cmpstr (g_uri_unescape_string ("%0",  NULL), ==, NULL);
342   g_assert_cmpstr (g_uri_unescape_string ("%ra",  NULL), ==, NULL);
343   g_assert_cmpstr (g_uri_unescape_string ("%2r",  NULL), ==, NULL);
344   g_assert_cmpstr (g_uri_unescape_string (NULL,  NULL), ==, NULL);
345 }
346
347 static void
348 test_uri_escape (void)
349 {
350   gchar *s;
351
352   s = g_uri_escape_string ("abcdefgABCDEFG._~", NULL, FALSE);
353   g_assert_cmpstr (s, ==, "abcdefgABCDEFG._~");
354   g_free (s);
355   s = g_uri_escape_string (":+ \\?#", NULL, FALSE);
356   g_assert_cmpstr (s, ==, "%3A%2B%20%5C%3F%23");
357   g_free (s);
358   s = g_uri_escape_string ("a+b:c", "+", FALSE);
359   g_assert_cmpstr (s, ==, "a+b%3Ac");
360   g_free (s);
361   s = g_uri_escape_string ("a+b:c\303\234", "+", TRUE);
362   g_assert_cmpstr (s, ==, "a+b%3Ac\303\234");
363   g_free (s);
364 }
365
366 static void
367 test_uri_scheme (void)
368 {
369   gchar *s;
370
371   s = g_uri_parse_scheme ("ftp://ftp.gtk.org");
372   g_assert_cmpstr (s, ==, "ftp");
373   g_free (s);
374   s = g_uri_parse_scheme ("1bad:");
375   g_assert (s == NULL);
376   s = g_uri_parse_scheme ("bad");
377   g_assert (s == NULL);
378 }
379
380 int
381 main (int   argc,
382       char *argv[])
383 {
384   g_test_init (&argc, &argv, NULL);
385
386   g_test_add_func ("/uri/to-uri", run_to_uri_tests);
387   g_test_add_func ("/uri/from-uri", run_from_uri_tests);
388   g_test_add_func ("/uri/roundtrip", run_roundtrip_tests);
389   g_test_add_func ("/uri/list", run_uri_list_tests);
390   g_test_add_func ("/uri/unescape", test_uri_unescape);
391   g_test_add_func ("/uri/escape", test_uri_escape);
392   g_test_add_func ("/uri/scheme", test_uri_scheme);
393
394   return g_test_run ();
395 }