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.1 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, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GLib Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GLib at ftp://ftp.gtk.org/pub/gtk/.
23  */
24 
25 #include <glib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 
30 typedef struct
31 {
32   char *filename;
33   char *hostname;
34   char *expected_result;
35   gint expected_error; /* If failed */
36 } FileToUriTest;
37 
38 FileToUriTest
39 file_to_uri_tests[] = {
40   { "/etc", NULL, "file:///etc", 0 },
41   { "/etc", "", "file:///etc", 0 },
42   { "/etc", "otherhost", "file://otherhost/etc", 0 },
43 #ifdef G_OS_WIN32
44   { "/etc", "localhost", "file:///etc", 0 },
45   { "c:\\windows", NULL, "file:///c:/windows", 0 },
46   { "c:\\windows", "localhost", "file:///c:/windows", 0 },
47   { "c:\\windows", "otherhost", "file://otherhost/c:/windows", 0 },
48   { "\\\\server\\share\\dir", NULL, "file:////server/share/dir", 0 },
49   { "\\\\server\\share\\dir", "localhost", "file:////server/share/dir", 0 },
50 #else
51   { "/etc", "localhost", "file://localhost/etc", 0 },
52   { "c:\\windows", NULL, NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH}, /* it's important to get this error on Unix */
53   { "c:\\windows", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
54   { "c:\\windows", "otherhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
55 #endif
56   { "etc", "localhost", NULL, G_CONVERT_ERROR_NOT_ABSOLUTE_PATH},
57 #ifndef G_PLATFORM_WIN32
58   { "/etc/\xE5\xE4\xF6", NULL, "file:///etc/%E5%E4%F6", 0 },
59   { "/etc/\xC3\xB6\xC3\xA4\xC3\xA5", NULL, "file:///etc/%C3%B6%C3%A4%C3%A5", 0 },
60 #endif
61   { "/etc", "\xC3\xB6\xC3\xA4\xC3\xA5", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
62   { "/etc", "\xE5\xE4\xF6", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
63   { "/etc/file with #%", NULL, "file:///etc/file%20with%20%23%25", 0 },
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", 0 },
69   { "/ABCDEFGHIJKLMNOPQRSTUVWXYZ", NULL, "file:///ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0 },
70   { "/abcdefghijklmnopqrstuvwxyz", NULL, "file:///abcdefghijklmnopqrstuvwxyz", 0 },
71   { "/-_.!~*'()", NULL, "file:///-_.!~*'()", 0 },
72 #ifdef G_OS_WIN32
73   /* As '\\' is a path separator on Win32, it gets turned into '/' in the URI */
74   { "/\"#%<>[\\]^`{|}\x7F", NULL, "file:///%22%23%25%3C%3E%5B/%5D%5E%60%7B%7C%7D%7F", 0 },
75 #else
76   /* On Unix, '\\' is a normal character in the file name */
77   { "/\"#%<>[\\]^`{|}\x7F", NULL, "file:///%22%23%25%3C%3E%5B%5C%5D%5E%60%7B%7C%7D%7F", 0 },
78 #endif
79   { "/;@+$,", NULL, "file:///%3B@+$,", 0 },
80   /* This and some of the following are of course as such illegal file names on Windows,
81    * and would not occur in real life.
82    */
83   { "/:", NULL, "file:///:", 0 },
84   { "/?&=", NULL, "file:///%3F&=", 0 },
85   { "/", "0123456789-", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
86   { "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/", 0 },
87   { "/", "abcdefghijklmnopqrstuvwxyz", "file://abcdefghijklmnopqrstuvwxyz/", 0 },
88   { "/", "_.!~*'()", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
89   { "/", "\"#%<>[\\]^`{|}\x7F", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
90   { "/", ";?&=+$,", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
91   { "/", "/", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
92   { "/", "@:", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
93   { "/", "\x80\xFF", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
94   { "/", "\xC3\x80\xC3\xBF", NULL, G_CONVERT_ERROR_ILLEGAL_SEQUENCE},
95 };
96 
97 
98 typedef struct
99 {
100   char *uri;
101   char *expected_filename;
102   char *expected_hostname;
103   gint expected_error; /* If failed */
104 } FileFromUriTest;
105 
106 FileFromUriTest
107 file_from_uri_tests[] = {
108   { "file:///etc", "/etc", NULL, 0 },
109   { "FILE:///etc", "/etc", NULL, 0 },
110   { "file:/etc", "/etc", NULL, 0 },
111 #ifdef G_OS_WIN32
112   /* On Win32 we don't return "localhost" hostames, just in case
113    * it isn't recognized anyway.
114    */
115   { "file://localhost/etc", "/etc", NULL, 0 },
116   { "file://localhost/etc/%23%25%20file", "/etc/#% file", NULL, 0 },
117   { "file://localhost/\xE5\xE4\xF6", "/\xe5\xe4\xf6", NULL, 0 },
118   { "file://localhost/%E5%E4%F6", "/\xe5\xe4\xf6", NULL, 0 },
119 #else
120   { "file://localhost/etc", "/etc", "localhost", 0 },
121   { "file://localhost/etc/%23%25%20file", "/etc/#% file", "localhost", 0 },
122   { "file://localhost/\xE5\xE4\xF6", "/\xe5\xe4\xf6", "localhost", 0 },
123   { "file://localhost/%E5%E4%F6", "/\xe5\xe4\xf6", "localhost", 0 },
124 #endif
125   { "file://otherhost/etc", "/etc", "otherhost", 0 },
126   { "file://otherhost/etc/%23%25%20file", "/etc/#% file", "otherhost", 0 },
127   { "file://%C3%B6%C3%A4%C3%A5/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
128   { "file:////etc/%C3%B6%C3%C3%C3%A5", "//etc/\xc3\xb6\xc3\xc3\xc3\xa5", NULL, 0 },
129   { "file://\xE5\xE4\xF6/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
130   { "file://%E5%E4%F6/etc", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
131   { "file:///some/file#bad", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
132   { "file://some", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
133   { "", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
134   { "file:test", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
135   { "http://www.yahoo.com/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
136   { "file:////etc", "//etc", NULL, 0 },
137   { "file://///etc", "///etc", NULL, 0 },
138 #ifdef G_OS_WIN32
139   /* URIs with backslashes come from some nonstandard application, but accept them anyhow */
140   { "file:///c:\\foo", "c:\\foo", NULL, 0 },
141   { "file:///c:/foo\\bar", "c:\\foo\\bar", NULL, 0 },
142   /* Accept also the old Netscape drive-letter-and-vertical bar convention */
143   { "file:///c|/foo", "c:\\foo", NULL, 0 },
144   { "file:////server/share/dir", "\\\\server\\share\\dir", NULL, 0 },
145   { "file://localhost//server/share/foo", "\\\\server\\share\\foo", NULL, 0 },
146   { "file://otherhost//server/share/foo", "\\\\server\\share\\foo", "otherhost", 0 },
147 #else
148   { "file:///c:\\foo", "/c:\\foo", NULL, 0 },
149   { "file:///c:/foo", "/c:/foo", NULL, 0 },
150   { "file:////c:/foo", "//c:/foo",  NULL, 0 },
151 #endif
152   { "file://0123456789/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
153   { "file://ABCDEFGHIJKLMNOPQRSTUVWXYZ/", "/", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0 },
154   { "file://abcdefghijklmnopqrstuvwxyz/", "/", "abcdefghijklmnopqrstuvwxyz", 0 },
155   { "file://-_.!~*'()/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
156   { "file://\"<>[\\]^`{|}\x7F/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
157   { "file://;?&=+$,/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
158   { "file://%C3%80%C3%BF/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
159   { "file://@/", 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://%23/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
163   { "file://%2F/", NULL, NULL, G_CONVERT_ERROR_BAD_URI},
164 };
165 
166 static void
run_file_to_uri_tests(void)167 run_file_to_uri_tests (void)
168 {
169   gsize i;
170   gchar *res;
171   GError *error;
172 
173   for (i = 0; i < G_N_ELEMENTS (file_to_uri_tests); i++)
174     {
175       error = NULL;
176       res = g_filename_to_uri (file_to_uri_tests[i].filename,
177                                file_to_uri_tests[i].hostname,
178                                &error);
179 
180       if (res)
181         g_assert_cmpstr (res, ==, file_to_uri_tests[i].expected_result);
182       else
183         g_assert_error (error, G_CONVERT_ERROR, file_to_uri_tests[i].expected_error);
184 
185       g_free (res);
186       g_clear_error (&error);
187     }
188 }
189 
190 static void
run_file_from_uri_tests(void)191 run_file_from_uri_tests (void)
192 {
193   gsize i;
194   gchar *res;
195   gchar *hostname;
196   GError *error;
197 
198   for (i = 0; i < G_N_ELEMENTS (file_from_uri_tests); i++)
199     {
200       error = NULL;
201       res = g_filename_from_uri (file_from_uri_tests[i].uri,
202                                  &hostname,
203                                  &error);
204 
205 #ifdef G_OS_WIN32
206       if (file_from_uri_tests[i].expected_filename)
207         {
208           gchar *p, *slash;
209           p = file_from_uri_tests[i].expected_filename =
210             g_strdup (file_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, ==, file_from_uri_tests[i].expected_filename);
220       else
221         g_assert_error (error, G_CONVERT_ERROR, file_from_uri_tests[i].expected_error);
222       g_assert_cmpstr (hostname, ==, file_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
safe_strcmp_filename(const gchar * a,const gchar * b)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
safe_strcmp_hostname(const gchar * a,const gchar * b)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
run_file_roundtrip_tests(void)271 run_file_roundtrip_tests (void)
272 {
273   gsize i;
274   gchar *uri, *hostname, *res;
275   GError *error;
276 
277   for (i = 0; i < G_N_ELEMENTS (file_to_uri_tests); i++)
278     {
279       if (file_to_uri_tests[i].expected_error != 0)
280         continue;
281 
282       error = NULL;
283       uri = g_filename_to_uri (file_to_uri_tests[i].filename,
284                                file_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_cmpint (safe_strcmp_filename (file_to_uri_tests[i].filename, res), ==, 0);
293       g_assert_cmpint (safe_strcmp_hostname (file_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
run_uri_list_tests(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
test_uri_unescape_string(void)332 test_uri_unescape_string (void)
333 {
334   const struct
335     {
336       /* Inputs */
337       const gchar *escaped;  /* (nullable) */
338       const gchar *illegal_characters;  /* (nullable) */
339       /* Outputs */
340       const gchar *expected_unescaped;  /* (nullable) */
341     }
342   tests[] =
343     {
344       { "%2Babc %4F", NULL, "+abc O" },
345       { "%2Babc %4F", "+", NULL },
346       { "%00abc %4F", "+/", NULL },
347       { "/cursors/none.png", "/", "/cursors/none.png" },
348       { "/cursors%2fbad-subdir/none.png", "/", NULL },
349       { "%0", NULL, NULL },
350       { "%ra", NULL, NULL },
351       { "%2r", NULL, NULL },
352       { "Timm B\344der", NULL, "Timm B\344der" },
353       { NULL, NULL, NULL },  /* actually a valid test, not a delimiter */
354     };
355   gsize i;
356 
357   for (i = 0; i < G_N_ELEMENTS (tests); i++)
358     {
359       gchar *s = NULL;
360 
361       g_test_message ("Test %" G_GSIZE_FORMAT ": %s", i, tests[i].escaped);
362 
363       s = g_uri_unescape_string (tests[i].escaped, tests[i].illegal_characters);
364       g_assert_cmpstr (s, ==, tests[i].expected_unescaped);
365       g_free (s);
366     }
367 }
368 
369 static void
test_uri_unescape_bytes(gconstpointer test_data)370 test_uri_unescape_bytes (gconstpointer test_data)
371 {
372   GError *error = NULL;
373   gboolean use_nul_terminated = GPOINTER_TO_INT (test_data);
374   const struct
375     {
376       /* Inputs */
377       const gchar *escaped;  /* (nullable) */
378       const gchar *illegal;
379       /* Outputs */
380       gssize expected_unescaped_len;  /* -1 => error expected */
381       const guint8 *expected_unescaped;  /* (nullable) */
382     }
383   tests[] =
384     {
385       { "%00%00", NULL, 2, (const guint8 *) "\x00\x00" },
386       { "/cursors/none.png", "/", 17, (const guint8 *) "/cursors/none.png" },
387       { "/cursors%2fbad-subdir/none.png", "/", -1, NULL },
388       { "%%", NULL, -1, NULL },
389       { "%", NULL, -1, NULL },
390     };
391   gsize i;
392 
393   for (i = 0; i < G_N_ELEMENTS (tests); i++)
394     {
395       gssize escaped_len = 0;
396       gchar *escaped = NULL;
397       GBytes *bytes = NULL;
398 
399       g_test_message ("Test %" G_GSIZE_FORMAT ": %s", i, tests[i].escaped);
400 
401       /* The tests get run twice: once with the length unspecified, using a
402        * nul-terminated string; and once with the length specified and a copy of
403        * the string with the trailing nul explicitly removed (to help catch
404        * buffer overflows). */
405       if (use_nul_terminated)
406         {
407           escaped_len = -1;
408           escaped = g_strdup (tests[i].escaped);
409         }
410       else
411         {
412           escaped_len = strlen (tests[i].escaped);  /* no trailing nul */
413           escaped = g_memdup2 (tests[i].escaped, escaped_len);
414         }
415 
416       bytes = g_uri_unescape_bytes (escaped, escaped_len, tests[i].illegal, &error);
417 
418       if (tests[i].expected_unescaped_len < 0)
419         {
420           g_assert_null (bytes);
421           g_assert_error (error, G_URI_ERROR, G_URI_ERROR_FAILED);
422           g_clear_error (&error);
423         }
424       else
425         {
426           g_assert_no_error (error);
427           g_assert_cmpmem (g_bytes_get_data (bytes, NULL),
428                            g_bytes_get_size (bytes),
429                            tests[i].expected_unescaped,
430                            tests[i].expected_unescaped_len);
431         }
432 
433       g_clear_pointer (&bytes, g_bytes_unref);
434       g_free (escaped);
435     }
436 }
437 
438 static void
test_uri_unescape_segment(void)439 test_uri_unescape_segment (void)
440 {
441   const gchar *escaped_segment = "%2Babc %4F---";
442   gchar *s = NULL;
443 
444   s = g_uri_unescape_segment (escaped_segment, escaped_segment + 10, NULL);
445   g_assert_cmpstr (s, ==, "+abc O");
446   g_free (s);
447 
448   s = g_uri_unescape_segment ("%2Babc%00cde", NULL, NULL);
449   g_assert_null (s);
450 }
451 
452 static void
test_uri_escape_string(void)453 test_uri_escape_string (void)
454 {
455   const struct
456     {
457       /* Inputs */
458       const gchar *unescaped;
459       const gchar *reserved_chars_allowed;
460       gboolean allow_utf8;
461       /* Outputs */
462       const gchar *expected_escaped;
463     }
464   tests[] =
465     {
466       { "abcdefgABCDEFG._~", NULL, FALSE, "abcdefgABCDEFG._~" },
467       { ":+ \\?#", NULL, FALSE, "%3A%2B%20%5C%3F%23" },
468       { "a+b:c", "+", FALSE, "a+b%3Ac" },
469       { "a+b:c\303\234", "+", TRUE, "a+b%3Ac\303\234" },
470       /* Incomplete UTF-8 sequence: */
471       { "\xfc\x3b\xd2", NULL, TRUE, "%FC%3B%D2" },
472       /* Invalid sequence: */
473       { "\xc3\xb1\xc3\x28", NULL, TRUE, "ñ%C3%28" },
474     };
475   gsize i;
476 
477   for (i = 0; i < G_N_ELEMENTS (tests); i++)
478     {
479       gchar *s = NULL;
480 
481       g_test_message ("Test %" G_GSIZE_FORMAT ": %s", i, tests[i].unescaped);
482 
483       s = g_uri_escape_string (tests[i].unescaped,
484                                tests[i].reserved_chars_allowed,
485                                tests[i].allow_utf8);
486       g_assert_cmpstr (s, ==, tests[i].expected_escaped);
487       g_free (s);
488     }
489 }
490 
491 static void
test_uri_escape_bytes(void)492 test_uri_escape_bytes (void)
493 {
494   gchar *s = NULL;
495 
496   s = g_uri_escape_bytes ((guchar*)"\0\0", 2, NULL);
497   g_assert_cmpstr (s, ==, "%00%00");
498   g_free (s);
499 }
500 
501 static void
test_uri_scheme(void)502 test_uri_scheme (void)
503 {
504   const gchar *s1, *s2;
505   gchar *s;
506 
507   s = g_uri_parse_scheme ("ftp://ftp.gtk.org");
508   g_assert_cmpstr (s, ==, "ftp");
509   g_free (s);
510 
511   s = g_uri_parse_scheme ("good-scheme.but+weird:gtk.org");
512   g_assert_cmpstr (s, ==, "good-scheme.but+weird");
513   g_free (s);
514 
515   s = g_uri_parse_scheme ("1bad:");
516   g_assert_null (s);
517   s = g_uri_parse_scheme ("bad");
518   g_assert_null (s);
519   s = g_uri_parse_scheme ("99http://host/path");
520   g_assert_null (s);
521   s = g_uri_parse_scheme (".http://host/path");
522   g_assert_null (s);
523   s = g_uri_parse_scheme ("+http://host/path");
524   g_assert_null (s);
525 
526   s1 = g_uri_peek_scheme ("ftp://ftp.gtk.org");
527   g_assert_cmpstr (s1, ==, "ftp");
528   s2 = g_uri_peek_scheme ("FTP://ftp.gtk.org");
529   g_assert_cmpstr (s2, ==, "ftp");
530   g_assert_true (s1 == s2);
531   s1 = g_uri_peek_scheme ("1bad:");
532   g_assert_null (s1);
533   s1 = g_uri_peek_scheme ("bad");
534   g_assert_null (s1);
535 }
536 
537 typedef struct {
538   const gchar *scheme;
539   const gchar *userinfo;
540   const gchar *host;
541   gint         port;
542   const gchar *path;
543   const gchar *query;
544   const gchar *fragment;
545 } UriParts;
546 
547 typedef struct {
548   /* Inputs */
549   const gchar *orig;
550   GUriFlags flags;
551   /* Outputs */
552   gboolean expected_success;
553   gint expected_error_code;       /* unused if @expected_success is true */
554   const UriParts expected_parts;  /* unused if @expected_success is false */
555 } UriAbsoluteTest;
556 
557 static const UriAbsoluteTest absolute_tests[] = {
558   { "foo:", G_URI_FLAGS_NONE, TRUE, 0,
559     { "foo", NULL, NULL, -1, "", NULL, NULL }
560   },
561   { "file:/dev/null", G_URI_FLAGS_NONE, TRUE, 0,
562     { "file", NULL, NULL, -1, "/dev/null", NULL, NULL }
563   },
564   { "file:///dev/null", G_URI_FLAGS_NONE, TRUE, 0,
565     { "file", NULL, "", -1, "/dev/null", NULL, NULL }
566   },
567   { "ftp://user@host/path", G_URI_FLAGS_NONE, TRUE, 0,
568     { "ftp", "user", "host", -1, "/path", NULL, NULL }
569   },
570   { "ftp://user@host:9999/path", G_URI_FLAGS_NONE, TRUE, 0,
571     { "ftp", "user", "host", 9999, "/path", NULL, NULL }
572   },
573   { "ftp://user:password@host/path", G_URI_FLAGS_NONE, TRUE, 0,
574     { "ftp", "user:password", "host", -1, "/path", NULL, NULL }
575   },
576   { "ftp://user:password@host:9999/path", G_URI_FLAGS_NONE, TRUE, 0,
577     { "ftp", "user:password", "host", 9999, "/path", NULL, NULL }
578   },
579   { "ftp://user:password@host", G_URI_FLAGS_NONE, TRUE, 0,
580     { "ftp", "user:password", "host", -1, "", NULL, NULL }
581   },
582   { "http://us%65r@host", G_URI_FLAGS_NONE, TRUE, 0,
583     { "http", "user", "host", -1, "", NULL, NULL }
584   },
585   { "http://us%40r@host", G_URI_FLAGS_NONE, TRUE, 0,
586     { "http", "us@r", "host", -1, "", NULL, NULL }
587   },
588   { "http://us%3ar@host", G_URI_FLAGS_NONE, TRUE, 0,
589     { "http", "us:r", "host", -1, "", NULL, NULL }
590   },
591   { "http://us%2fr@host", G_URI_FLAGS_NONE, TRUE, 0,
592     { "http", "us/r", "host", -1, "", NULL, NULL }
593   },
594   { "http://us%3fr@host", G_URI_FLAGS_NONE, TRUE, 0,
595     { "http", "us?r", "host", -1, "", NULL, NULL }
596   },
597   { "http://host?query", G_URI_FLAGS_NONE, TRUE, 0,
598     { "http", NULL, "host", -1, "", "query", NULL }
599   },
600   { "http://host/path?query=http%3A%2F%2Fhost%2Fpath%3Fchildparam%3Dchildvalue&param=value", G_URI_FLAGS_NONE, TRUE, 0,
601     { "http", NULL, "host", -1, "/path", "query=http://host/path?childparam=childvalue&param=value", NULL }
602   },
603   { "http://control-chars/%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F%7F", G_URI_FLAGS_NONE, TRUE, 0,
604     { "http", NULL, "control-chars", -1, "/\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x7F", NULL, NULL }
605   },
606   { "http://space/%20", G_URI_FLAGS_NONE, TRUE, 0,
607     { "http", NULL, "space", -1, "/ ", NULL, NULL }
608   },
609   { "http://delims/%3C%3E%23%25%22", G_URI_FLAGS_NONE, TRUE, 0,
610     { "http", NULL, "delims", -1, "/<>#%\"", NULL, NULL }
611   },
612   { "http://unwise-chars/%7B%7D%7C%5C%5E%5B%5D%60", G_URI_FLAGS_NONE, TRUE, 0,
613     { "http", NULL, "unwise-chars", -1, "/{}|\\^[]`", NULL, NULL }
614   },
615 
616   /* From RFC 2732 */
617   { "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html", G_URI_FLAGS_NONE, TRUE, 0,
618     { "http", NULL, "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210", 80, "/index.html", NULL, NULL }
619   },
620   { "http://[1080:0:0:0:8:800:200C:417A]/index.html", G_URI_FLAGS_NONE, TRUE, 0,
621     { "http", NULL, "1080:0:0:0:8:800:200C:417A", -1, "/index.html", NULL, NULL }
622   },
623   { "http://[3ffe:2a00:100:7031::1]", G_URI_FLAGS_NONE, TRUE, 0,
624     { "http", NULL, "3ffe:2a00:100:7031::1", -1, "", NULL, NULL }
625   },
626   { "http://[1080::8:800:200C:417A]/foo", G_URI_FLAGS_NONE, TRUE, 0,
627     { "http", NULL, "1080::8:800:200C:417A", -1, "/foo", NULL, NULL }
628   },
629   { "http://[::192.9.5.5]/ipng", G_URI_FLAGS_NONE, TRUE, 0,
630     { "http", NULL, "::192.9.5.5", -1, "/ipng", NULL, NULL }
631   },
632   { "http://[::FFFF:129.144.52.38]:80/index.html", G_URI_FLAGS_NONE, TRUE, 0,
633     { "http", NULL, "::FFFF:129.144.52.38", 80, "/index.html", NULL, NULL }
634   },
635   { "http://[2010:836B:4179::836B:4179]", G_URI_FLAGS_NONE, TRUE, 0,
636     { "http", NULL, "2010:836B:4179::836B:4179", -1, "", NULL, NULL }
637   },
638 
639   /* some problematic URIs that are handled differently in libsoup */
640   { "http://host/path with spaces", G_URI_FLAGS_PARSE_RELAXED, TRUE, 0,
641     { "http", NULL, "host", -1, "/path with spaces", NULL, NULL }
642   },
643   { "  http://host/path", G_URI_FLAGS_PARSE_RELAXED, TRUE, 0,
644     { "http", NULL, "host", -1, "/path", NULL, NULL }
645   },
646   { "http://host/path  ", G_URI_FLAGS_PARSE_RELAXED, TRUE, 0,
647     { "http", NULL, "host", -1, "/path", NULL, NULL }
648   },
649   { "http://host  ", G_URI_FLAGS_PARSE_RELAXED, TRUE, 0,
650     { "http", NULL, "host", -1, "", NULL, NULL }
651   },
652   { "http://host:999  ", G_URI_FLAGS_PARSE_RELAXED, TRUE, 0,
653     { "http", NULL, "host", 999, "", NULL, NULL }
654   },
655   { "http://host/pa\nth", G_URI_FLAGS_PARSE_RELAXED, TRUE, 0,
656     { "http", NULL, "host", -1, "/path", NULL, NULL }
657   },
658   { "http:\r\n//host/path", G_URI_FLAGS_PARSE_RELAXED, TRUE, 0,
659     { "http", NULL, "host", -1, "/path", NULL, NULL }
660   },
661   { "http://\thost/path", G_URI_FLAGS_PARSE_RELAXED, TRUE, 0,
662     { "http", NULL, "host", -1, "/path", NULL, NULL }
663   },
664 
665   /* Bug 594405; 0-length is different from not-present */
666   { "http://host/path?", G_URI_FLAGS_NONE, TRUE, 0,
667     { "http", NULL, "host", -1, "/path", "", NULL }
668   },
669   { "http://host/path#", G_URI_FLAGS_NONE, TRUE, 0,
670     { "http", NULL, "host", -1, "/path", NULL, "" },
671   },
672 
673   /* Bug 590524; ignore bad %-encoding */
674   { "http://host/path%", G_URI_FLAGS_PARSE_RELAXED, TRUE, 0,
675     { "http", NULL, "host", -1, "/path%", NULL, NULL }
676   },
677   { "http://h%ost/path", G_URI_FLAGS_PARSE_RELAXED, TRUE, 0,
678     { "http", NULL, "h%ost", -1, "/path", NULL, NULL }
679   },
680   { "http://host/path%%", G_URI_FLAGS_PARSE_RELAXED, TRUE, 0,
681     { "http", NULL, "host", -1, "/path%%", NULL, NULL }
682   },
683   { "http://host/path%%%", G_URI_FLAGS_PARSE_RELAXED, TRUE, 0,
684     { "http", NULL, "host", -1, "/path%%%", NULL, NULL }
685   },
686   { "http://host/path%/x/", G_URI_FLAGS_PARSE_RELAXED, TRUE, 0,
687     { "http", NULL, "host", -1, "/path%/x/", NULL, NULL }
688   },
689   { "http://host/path%0x/", G_URI_FLAGS_PARSE_RELAXED, TRUE, 0,
690     { "http", NULL, "host", -1, "/path%0x/", NULL, NULL }
691   },
692   { "http://host/path%ax", G_URI_FLAGS_PARSE_RELAXED, TRUE, 0,
693     { "http", NULL, "host", -1, "/path%ax", NULL, NULL }
694   },
695 
696   /* GUri doesn't %-encode non-ASCII characters */
697   { "http://host/p\xc3\xa4th/", G_URI_FLAGS_NONE, TRUE, 0,
698     { "http", NULL, "host", -1, "/p\xc3\xa4th/", NULL, NULL }
699   },
700 
701   { "HTTP:////////////////", G_URI_FLAGS_NONE, TRUE, 0,
702     { "http", NULL, "", -1, "//////////////", NULL, NULL }
703   },
704 
705   { "http://@host", G_URI_FLAGS_NONE, TRUE, 0,
706     { "http", "", "host", -1, "", NULL, NULL }
707   },
708   { "http://:@host", G_URI_FLAGS_NONE, TRUE, 0,
709     { "http", ":", "host", -1, "", NULL, NULL }
710   },
711   { "scheme://foo%3Abar._webdav._tcp.local", G_URI_FLAGS_NONE, TRUE, 0,
712     { "scheme", NULL, "foo:bar._webdav._tcp.local", -1, "", NULL, NULL}
713   },
714 
715   /* ".." past top */
716   { "http://example.com/..", G_URI_FLAGS_NONE, TRUE, 0,
717     { "http", NULL, "example.com", -1, "/", NULL, NULL }
718   },
719 
720   /* scheme parsing */
721   { "foo0://host/path", G_URI_FLAGS_NONE, TRUE, 0,
722     { "foo0", NULL, "host", -1, "/path", NULL, NULL } },
723   { "f0.o://host/path", G_URI_FLAGS_NONE, TRUE, 0,
724     { "f0.o", NULL, "host", -1, "/path", NULL, NULL } },
725   { "http++://host/path", G_URI_FLAGS_NONE, TRUE, 0,
726     { "http++", NULL, "host", -1, "/path", NULL, NULL } },
727   { "http-ish://host/path", G_URI_FLAGS_NONE, TRUE, 0,
728     { "http-ish", NULL, "host", -1, "/path", NULL, NULL } },
729 
730   /* IPv6 scope ID parsing (both correct and incorrect) */
731   { "http://[fe80::dead:beef%]/", G_URI_FLAGS_PARSE_RELAXED, FALSE, G_URI_ERROR_BAD_HOST,
732     { NULL, NULL, NULL, -1, NULL, NULL, NULL } },
733   { "http://[fe80::dead:beef%em1]/", G_URI_FLAGS_PARSE_RELAXED, TRUE, 0,
734     { "http", NULL, "fe80::dead:beef%em1", -1, "/", NULL, NULL } },
735   { "http://[fe80::dead:beef%em1]/", G_URI_FLAGS_NONE, FALSE, G_URI_ERROR_BAD_HOST,
736     { NULL, NULL, NULL, -1, NULL, NULL, NULL } },
737   { "http://[fe80::dead:beef%25em1]/", G_URI_FLAGS_NONE, TRUE, 0,
738     { "http", NULL, "fe80::dead:beef%em1", -1, "/", NULL, NULL } },
739   { "http://[fe80::dead:beef%25em1%20]/", G_URI_FLAGS_NONE, TRUE, 0,
740     { "http", NULL, "fe80::dead:beef%em1 ", -1, "/", NULL, NULL } },
741   { "http://[fe80::dead:beef%25em%31]/", G_URI_FLAGS_NONE, TRUE, 0,
742     { "http", NULL, "fe80::dead:beef%em1", -1, "/", NULL, NULL } },
743   { "http://[fe80::dead:beef%10]/", G_URI_FLAGS_PARSE_RELAXED, TRUE, 0,
744     { "http", NULL, "fe80::dead:beef%10", -1, "/", NULL, NULL } },
745   { "http://[fe80::dead:beef%10]/", G_URI_FLAGS_NONE, FALSE, G_URI_ERROR_BAD_HOST,
746     { NULL, NULL, NULL, -1, NULL, NULL, NULL } },
747   { "http://[fe80::dead:beef%25]/", G_URI_FLAGS_PARSE_RELAXED, TRUE, 0,
748     { "http", NULL, "fe80::dead:beef%25", -1, "/", NULL, NULL } },
749   { "http://[fe80::dead:beef%25]/", G_URI_FLAGS_NONE, FALSE, G_URI_ERROR_BAD_HOST,
750     { NULL, NULL, NULL, -1, NULL, NULL, NULL } },
751   { "http://[192.168.0.1%25em1]/", G_URI_FLAGS_NONE, FALSE, G_URI_ERROR_BAD_HOST,
752     { NULL, NULL, NULL, -1, NULL, NULL, NULL } },
753   { "http://[fe80::dead:beef%2em1]/", G_URI_FLAGS_PARSE_RELAXED, TRUE, 0,
754     { "http", NULL, "fe80::dead:beef%2em1", -1, "/", NULL, NULL } },
755   { "http://[fe80::dead:beef%2em1]/", G_URI_FLAGS_NONE, FALSE, G_URI_ERROR_BAD_HOST,
756     { NULL, NULL, NULL, -1, NULL, NULL, NULL } },
757   { "http://[fe80::dead:beef%25em1%00]/", G_URI_FLAGS_PARSE_RELAXED, FALSE, G_URI_ERROR_BAD_HOST,
758     { NULL, NULL, NULL, -1, NULL, NULL, NULL } },
759   { "http://[fe80::dead:beef%25em1%00]/", G_URI_FLAGS_NONE, FALSE, G_URI_ERROR_BAD_HOST,
760     { NULL, NULL, NULL, -1, NULL, NULL, NULL } },
761 
762   /* Invalid IDN hostname */
763   { "http://xn--mixed-\xc3\xbcp/", G_URI_FLAGS_NONE, FALSE, G_URI_ERROR_BAD_HOST,
764     { NULL, NULL, NULL, -1, NULL, NULL, NULL } },
765 };
766 
767 static void
test_uri_parsing_absolute(void)768 test_uri_parsing_absolute (void)
769 {
770   gsize i;
771 
772   for (i = 0; i < G_N_ELEMENTS (absolute_tests); i++)
773     {
774       const UriAbsoluteTest *test = &absolute_tests[i];
775       GError *error = NULL;
776       GUri *uri;
777 
778       g_test_message ("Test %" G_GSIZE_FORMAT ": %s", i, test->orig);
779 
780       uri = g_uri_parse (test->orig, test->flags, &error);
781       if (test->expected_success)
782         {
783           g_assert_no_error (error);
784 
785           g_assert_cmpstr (g_uri_get_scheme (uri),   ==, test->expected_parts.scheme);
786           g_assert_cmpstr (g_uri_get_userinfo (uri), ==, test->expected_parts.userinfo);
787           g_assert_cmpstr (g_uri_get_host (uri),     ==, test->expected_parts.host);
788           g_assert_cmpint (g_uri_get_port (uri),     ==, test->expected_parts.port);
789           g_assert_cmpstr (g_uri_get_path (uri),     ==, test->expected_parts.path);
790           g_assert_cmpstr (g_uri_get_query (uri),    ==, test->expected_parts.query);
791           g_assert_cmpstr (g_uri_get_fragment (uri), ==, test->expected_parts.fragment);
792         }
793       else
794         {
795           g_assert_error (error, G_URI_ERROR, test->expected_error_code);
796           g_assert_null (uri);
797         }
798 
799       g_clear_pointer (&uri, g_uri_unref);
800       g_clear_error (&error);
801     }
802 }
803 
804 typedef struct {
805   const gchar *orig, *resolved;
806   UriParts parts;
807 } UriRelativeTest;
808 
809 /* This all comes from RFC 3986 */
810 static const char *relative_test_base = "http://a/b/c/d;p?q";
811 static const UriRelativeTest relative_tests[] = {
812   { "g:h", "g:h",
813     { "g", NULL, NULL, -1, "h", NULL, NULL } },
814   { "g", "http://a/b/c/g",
815     { "http", NULL, "a", -1, "/b/c/g", NULL, NULL } },
816   { "./g", "http://a/b/c/g",
817     { "http", NULL, "a", -1, "/b/c/g", NULL, NULL } },
818   { "g/", "http://a/b/c/g/",
819     { "http", NULL, "a", -1, "/b/c/g/", NULL, NULL } },
820   { "/g", "http://a/g",
821     { "http", NULL, "a", -1, "/g", NULL, NULL } },
822   { "//g", "http://g",
823     { "http", NULL, "g", -1, "", NULL, NULL } },
824   { "?y", "http://a/b/c/d;p?y",
825     { "http", NULL, "a", -1, "/b/c/d;p", "y", NULL } },
826   { "g?y", "http://a/b/c/g?y",
827     { "http", NULL, "a", -1, "/b/c/g", "y", NULL } },
828   { "#s", "http://a/b/c/d;p?q#s",
829     { "http", NULL, "a", -1, "/b/c/d;p", "q", "s" } },
830   { "g#s", "http://a/b/c/g#s",
831     { "http", NULL, "a", -1, "/b/c/g", NULL, "s" } },
832   { "g?y#s", "http://a/b/c/g?y#s",
833     { "http", NULL, "a", -1, "/b/c/g", "y", "s" } },
834   { ";x", "http://a/b/c/;x",
835     { "http", NULL, "a", -1, "/b/c/;x", NULL, NULL } },
836   { "g;x", "http://a/b/c/g;x",
837     { "http", NULL, "a", -1, "/b/c/g;x", NULL, NULL } },
838   { "g;x?y#s", "http://a/b/c/g;x?y#s",
839     { "http", NULL, "a", -1, "/b/c/g;x", "y", "s" } },
840   { ".", "http://a/b/c/",
841     { "http", NULL, "a", -1, "/b/c/", NULL, NULL } },
842   { "./", "http://a/b/c/",
843     { "http", NULL, "a", -1, "/b/c/", NULL, NULL } },
844   { "..", "http://a/b/",
845     { "http", NULL, "a", -1, "/b/", NULL, NULL } },
846   { "../", "http://a/b/",
847     { "http", NULL, "a", -1, "/b/", NULL, NULL } },
848   { "../g", "http://a/b/g",
849     { "http", NULL, "a", -1, "/b/g", NULL, NULL } },
850   { "../..", "http://a/",
851     { "http", NULL, "a", -1, "/", NULL, NULL } },
852   { "../../", "http://a/",
853     { "http", NULL, "a", -1, "/", NULL, NULL } },
854   { "../../g", "http://a/g",
855     { "http", NULL, "a", -1, "/g", NULL, NULL } },
856   { "", "http://a/b/c/d;p?q",
857     { "http", NULL, "a", -1, "/b/c/d;p", "q", NULL } },
858   { "../../../g", "http://a/g",
859     { "http", NULL, "a", -1, "/g", NULL, NULL } },
860   { "../../../../g", "http://a/g",
861     { "http", NULL, "a", -1, "/g", NULL, NULL } },
862   { "/./g", "http://a/g",
863     { "http", NULL, "a", -1, "/g", NULL, NULL } },
864   { "/../g", "http://a/g",
865     { "http", NULL, "a", -1, "/g", NULL, NULL } },
866   { "g.", "http://a/b/c/g.",
867     { "http", NULL, "a", -1, "/b/c/g.", NULL, NULL } },
868   { ".g", "http://a/b/c/.g",
869     { "http", NULL, "a", -1, "/b/c/.g", NULL, NULL } },
870   { "g..", "http://a/b/c/g..",
871     { "http", NULL, "a", -1, "/b/c/g..", NULL, NULL } },
872   { "..g", "http://a/b/c/..g",
873     { "http", NULL, "a", -1, "/b/c/..g", NULL, NULL } },
874   { "./../g", "http://a/b/g",
875     { "http", NULL, "a", -1, "/b/g", NULL, NULL } },
876   { "./g/.", "http://a/b/c/g/",
877     { "http", NULL, "a", -1, "/b/c/g/", NULL, NULL } },
878   { "g/./h", "http://a/b/c/g/h",
879     { "http", NULL, "a", -1, "/b/c/g/h", NULL, NULL } },
880   { "g/../h", "http://a/b/c/h",
881     { "http", NULL, "a", -1, "/b/c/h", NULL, NULL } },
882   { "g;x=1/./y", "http://a/b/c/g;x=1/y",
883     { "http", NULL, "a", -1, "/b/c/g;x=1/y", NULL, NULL } },
884   { "g;x=1/../y", "http://a/b/c/y",
885     { "http", NULL, "a", -1, "/b/c/y", NULL, NULL } },
886   { "g?y/./x", "http://a/b/c/g?y/./x",
887     { "http", NULL, "a", -1, "/b/c/g", "y/./x", NULL } },
888   { "g?y/../x", "http://a/b/c/g?y/../x",
889     { "http", NULL, "a", -1, "/b/c/g", "y/../x", NULL } },
890   { "g#s/./x", "http://a/b/c/g#s/./x",
891     { "http", NULL, "a", -1, "/b/c/g", NULL, "s/./x" } },
892   { "g#s/../x", "http://a/b/c/g#s/../x",
893     { "http", NULL, "a", -1, "/b/c/g", NULL, "s/../x" } },
894   { "http:g", "http:g",
895     { "http", NULL, NULL, -1, "g", NULL, NULL } },
896   { "http://a/../..", "http://a/",
897     { "http", NULL, "a", -1, "/", NULL, NULL } },
898   { "ScHeMe://User:P%61ss@HOST.%63om:1234/path/./from/../to%7d/item%2dobj?qu%65ry=something#fr%61gment",
899     "scheme://User:Pass@HOST.com:1234/path/to%7D/item-obj?query=something#fragment",
900     { "scheme", "User:Pass", "HOST.com", 1234, "/path/to}/item-obj", "query=something", "fragment" } },
901   /* Test corner cases of remove_dot_segments */
902   { "http:..", "http:",
903     { "http", NULL, NULL, -1, "", NULL, NULL } },
904   { "http:../", "http:",
905     { "http", NULL, NULL, -1, "", NULL, NULL } },
906   { "http:.", "http:",
907     { "http", NULL, NULL, -1, "", NULL, NULL } },
908   { "http:./", "http:",
909     { "http", NULL, NULL, -1, "", NULL, NULL } },
910   { "http:a/..", "http:/",
911     { "http", NULL, NULL, -1, "/", NULL, NULL } },
912   { "http:a/../", "http:/",
913     { "http", NULL, NULL, -1, "/", NULL, NULL } },
914 };
915 static int num_relative_tests = G_N_ELEMENTS (relative_tests);
916 
917 static void
test_uri_parsing_relative(void)918 test_uri_parsing_relative (void)
919 {
920   int i;
921   GUri *base, *uri;
922   GError *error = NULL;
923   gchar *resolved;
924 
925   base = g_uri_parse (relative_test_base, G_URI_FLAGS_NONE, &error);
926   g_assert_no_error (error);
927 
928   for (i = 0; i < num_relative_tests; i++)
929     {
930       const UriRelativeTest *test = &relative_tests[i];
931       gchar *tostring;
932 
933       uri = g_uri_parse_relative (base, test->orig, G_URI_FLAGS_NONE, &error);
934       g_assert_no_error (error);
935 
936       g_assert_cmpstr (g_uri_get_scheme (uri),   ==, test->parts.scheme);
937       g_assert_cmpstr (g_uri_get_userinfo (uri), ==, test->parts.userinfo);
938       g_assert_cmpstr (g_uri_get_host (uri),     ==, test->parts.host);
939       g_assert_cmpint (g_uri_get_port (uri),     ==, test->parts.port);
940       g_assert_cmpstr (g_uri_get_path (uri),     ==, test->parts.path);
941       g_assert_cmpstr (g_uri_get_query (uri),    ==, test->parts.query);
942       g_assert_cmpstr (g_uri_get_fragment (uri), ==, test->parts.fragment);
943 
944       tostring = g_uri_to_string (uri);
945       g_assert_cmpstr (tostring, ==, test->resolved);
946       g_free (tostring);
947 
948       g_uri_unref (uri);
949 
950       resolved = g_uri_resolve_relative (relative_test_base, test->orig, G_URI_FLAGS_NONE, &error);
951       g_assert_no_error (error);
952       g_assert_cmpstr (resolved, ==, test->resolved);
953       g_free (resolved);
954     }
955   uri = g_uri_parse_relative (base, "%%", G_URI_FLAGS_NONE, &error);
956   g_assert_null (uri);
957   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_PATH);
958   g_clear_error (&error);
959 
960   g_uri_unref (base);
961 
962   resolved = g_uri_resolve_relative (NULL, "http://a", G_URI_FLAGS_NONE, &error);
963   g_assert_no_error (error);
964   g_assert_cmpstr (resolved, ==, "http://a");
965   g_free (resolved);
966 
967   resolved = g_uri_resolve_relative ("http://a", "b", G_URI_FLAGS_NONE, &error);
968   g_assert_no_error (error);
969   g_assert_cmpstr (resolved, ==, "http://a/b");
970   g_free (resolved);
971 
972   resolved = g_uri_resolve_relative (NULL, "a", G_URI_FLAGS_NONE, &error);
973   g_assert_null (resolved);
974   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_FAILED);
975   g_clear_error (&error);
976 
977   resolved = g_uri_resolve_relative ("../b", "a", G_URI_FLAGS_NONE, &error);
978   g_assert_null (resolved);
979   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_FAILED);
980   g_clear_error (&error);
981 
982   resolved = g_uri_resolve_relative ("%%", "a", G_URI_FLAGS_PARSE_RELAXED, &error);
983   g_assert_null (resolved);
984   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_FAILED);
985   g_clear_error (&error);
986 }
987 
988 static void
test_uri_to_string(void)989 test_uri_to_string (void)
990 {
991   GUri *uri;
992   gchar *tostring;
993 
994   uri = g_uri_build (G_URI_FLAGS_NONE, "scheme", "userinfo", "host", 1234,
995                      "/path", "query", "fragment");
996 
997   tostring = g_uri_to_string (uri);
998   g_assert_cmpstr (tostring, ==, "scheme://userinfo@host:1234/path?query#fragment");
999   g_free (tostring);
1000   g_uri_unref (uri);
1001 
1002   uri = g_uri_build (G_URI_FLAGS_NONE, "scheme", NULL, "fe80::dead:beef%em1", -1, "", NULL, NULL);
1003   tostring = g_uri_to_string (uri);
1004   g_assert_cmpstr (tostring, ==, "scheme://[fe80::dead:beef%25em1]");
1005   g_free (tostring);
1006   g_uri_unref (uri);
1007 
1008   uri = g_uri_build_with_user (G_URI_FLAGS_NONE, "scheme", "user", "pass", "auth", "host", 1234,
1009                                "/path", "query", "fragment");
1010   tostring = g_uri_to_string (uri);
1011   g_assert_cmpstr (tostring, ==, "scheme://user:pass;auth@host:1234/path?query#fragment");
1012   g_free (tostring);
1013   tostring = g_uri_to_string_partial (uri, G_URI_HIDE_USERINFO);
1014   g_assert_cmpstr (tostring, ==, "scheme://host:1234/path?query#fragment");
1015   g_free (tostring);
1016   tostring = g_uri_to_string_partial (uri, G_URI_HIDE_QUERY);
1017   g_assert_cmpstr (tostring, ==, "scheme://user:pass;auth@host:1234/path#fragment");
1018   g_free (tostring);
1019   tostring = g_uri_to_string_partial (uri, G_URI_HIDE_FRAGMENT);
1020   g_assert_cmpstr (tostring, ==, "scheme://user:pass;auth@host:1234/path?query");
1021   g_free (tostring);
1022   g_uri_unref (uri);
1023 
1024   uri = g_uri_build_with_user (G_URI_FLAGS_HAS_PASSWORD|G_URI_FLAGS_HAS_AUTH_PARAMS,
1025                                "scheme", "us:er", "pass", "auth", "host", 1234,
1026                                "/path", "query", "fragment");
1027   tostring = g_uri_to_string (uri);
1028   g_assert_cmpstr (tostring, ==, "scheme://us%3Aer:pass;auth@host:1234/path?query#fragment");
1029   g_free (tostring);
1030   tostring = g_uri_to_string_partial (uri, G_URI_HIDE_PASSWORD);
1031   g_assert_cmpstr (tostring, ==, "scheme://us%3Aer;auth@host:1234/path?query#fragment");
1032   g_free (tostring);
1033   tostring = g_uri_to_string_partial (uri, G_URI_HIDE_AUTH_PARAMS);
1034   g_assert_cmpstr (tostring, ==, "scheme://us%3Aer:pass@host:1234/path?query#fragment");
1035   g_free (tostring);
1036   tostring = g_uri_to_string_partial (uri, G_URI_HIDE_QUERY);
1037   g_assert_cmpstr (tostring, ==, "scheme://us%3Aer:pass;auth@host:1234/path#fragment");
1038   g_free (tostring);
1039   g_uri_unref (uri);
1040 }
1041 
1042 static void
test_uri_build(void)1043 test_uri_build (void)
1044 {
1045   GUri *uri;
1046 
1047   uri = g_uri_build (G_URI_FLAGS_NON_DNS, "scheme", "userinfo", "host", 1234,
1048                      "/path", "query", "fragment");
1049 
1050   /* check ref/unref */
1051   g_uri_ref (uri);
1052   g_uri_unref (uri);
1053 
1054   g_assert_cmpint (g_uri_get_flags (uri), ==, G_URI_FLAGS_NON_DNS);
1055   g_assert_cmpstr (g_uri_get_scheme (uri), ==, "scheme");
1056   g_assert_cmpstr (g_uri_get_userinfo (uri), ==, "userinfo");
1057   g_assert_cmpstr (g_uri_get_host (uri), ==, "host");
1058   g_assert_cmpint (g_uri_get_port (uri), ==, 1234);
1059   g_assert_cmpstr (g_uri_get_path (uri), ==, "/path");
1060   g_assert_cmpstr (g_uri_get_query (uri), ==, "query");
1061   g_assert_cmpstr (g_uri_get_fragment (uri), ==, "fragment");
1062   g_assert_cmpstr (g_uri_get_user (uri), ==, NULL);
1063   g_assert_cmpstr (g_uri_get_password (uri), ==, NULL);
1064   g_uri_unref (uri);
1065 
1066   uri = g_uri_build_with_user (G_URI_FLAGS_NON_DNS, "scheme", "user", "password",
1067                                "authparams", "host", 1234,
1068                                "/path", "query", "fragment");
1069 
1070   g_assert_cmpint (g_uri_get_flags (uri), ==, G_URI_FLAGS_NON_DNS | G_URI_FLAGS_HAS_PASSWORD);
1071   g_assert_cmpstr (g_uri_get_scheme (uri), ==, "scheme");
1072   g_assert_cmpstr (g_uri_get_userinfo (uri), ==, "user:password;authparams");
1073   g_assert_cmpstr (g_uri_get_host (uri), ==, "host");
1074   g_assert_cmpint (g_uri_get_port (uri), ==, 1234);
1075   g_assert_cmpstr (g_uri_get_path (uri), ==, "/path");
1076   g_assert_cmpstr (g_uri_get_query (uri), ==, "query");
1077   g_assert_cmpstr (g_uri_get_fragment (uri), ==, "fragment");
1078   g_assert_cmpstr (g_uri_get_user (uri), ==, "user");
1079   g_assert_cmpstr (g_uri_get_password (uri), ==, "password");
1080   g_assert_cmpstr (g_uri_get_auth_params (uri), ==, "authparams");
1081   g_uri_unref (uri);
1082 
1083   uri = g_uri_build_with_user (G_URI_FLAGS_NONE, "scheme", "user\001", "password\002",
1084                                "authparams\003", "host", 1234,
1085                                "/path", "query", "fragment");
1086   g_assert_cmpstr (g_uri_get_userinfo (uri), ==, "user\001:password\002;authparams\003");
1087   g_uri_unref (uri);
1088 
1089   uri = g_uri_build_with_user (G_URI_FLAGS_ENCODED, "scheme", "user%01", "password%02",
1090                                "authparams%03", "host", 1234,
1091                                "/path", "query", "fragment");
1092   g_assert_cmpstr (g_uri_get_userinfo (uri), ==, "user%01:password%02;authparams%03");
1093   g_uri_unref (uri);
1094 
1095   uri = g_uri_build_with_user (G_URI_FLAGS_ENCODED, "scheme", NULL, NULL,
1096                                NULL, "host", 1234,
1097                                "/path", "query", "fragment");
1098   g_assert_null (g_uri_get_userinfo (uri));
1099   g_uri_unref (uri);
1100 
1101   uri = g_uri_build_with_user (G_URI_FLAGS_NONE, "scheme", "user", NULL, NULL,
1102                                "host", 1234,
1103                                "/path", "query", "fragment");
1104   g_assert_cmpstr (g_uri_get_userinfo (uri), ==, "user");
1105   g_uri_unref (uri);
1106 }
1107 
1108 static void
test_uri_split(void)1109 test_uri_split (void)
1110 {
1111   gchar *scheme = NULL;
1112   gchar *userinfo = NULL;
1113   gchar *user = NULL;
1114   gchar *pass = NULL;
1115   gchar *authparams = NULL;
1116   gchar *host = NULL;
1117   gchar *path = NULL;
1118   gchar *query = NULL;
1119   gchar *fragment = NULL;
1120   GError *error = NULL;
1121   gint port;
1122 
1123   g_uri_split ("scheme://user%3Apass%3Bauth@host:1234/path?query#fragment",
1124                G_URI_FLAGS_NONE,
1125                &scheme,
1126                &userinfo,
1127                &host,
1128                &port,
1129                &path,
1130                &query,
1131                &fragment,
1132                &error);
1133   g_assert_no_error (error);
1134   g_assert_cmpstr (scheme, ==, "scheme");
1135   g_assert_cmpstr (userinfo, ==, "user:pass;auth");
1136   g_assert_cmpstr (host, ==, "host");
1137   g_assert_cmpint (port, ==, 1234);
1138   g_assert_cmpstr (path, ==, "/path");
1139   g_assert_cmpstr (query, ==, "query");
1140   g_assert_cmpstr (fragment, ==, "fragment");
1141   g_free (scheme);
1142   g_free (userinfo);
1143   g_free (host);
1144   g_free (path);
1145   g_free (query);
1146   g_free (fragment);
1147 
1148   g_uri_split ("scheme://user%3Apass%3Bauth@h%01st:1234/path?query#fragment",
1149                G_URI_FLAGS_ENCODED,
1150                NULL,
1151                NULL,
1152                &host,
1153                NULL,
1154                NULL,
1155                NULL,
1156                NULL,
1157                &error);
1158   g_assert_no_error (error);
1159   g_assert_cmpstr (host, ==, "h\001st");
1160   g_free (host);
1161 
1162   g_uri_split ("scheme://@@@host:1234/path?query#fragment",
1163                G_URI_FLAGS_ENCODED | G_URI_FLAGS_PARSE_RELAXED,
1164                NULL,
1165                &userinfo,
1166                NULL,
1167                NULL,
1168                NULL,
1169                NULL,
1170                NULL,
1171                &error);
1172   g_assert_no_error (error);
1173   g_assert_cmpstr (userinfo, ==, "@@");
1174   g_free (userinfo);
1175 
1176 
1177   g_uri_split ("http://f;oo/",
1178                G_URI_FLAGS_NONE | G_URI_FLAGS_PARSE_RELAXED,
1179                NULL,
1180                NULL,
1181                NULL,
1182                NULL,
1183                &path,
1184                NULL,
1185                NULL,
1186                &error);
1187   g_assert_no_error (error);
1188   g_assert_cmpstr (path, ==, ";oo/");
1189   g_free (path);
1190 
1191   g_uri_split ("http://h%01st/path?saisons=%C3%89t%C3%A9%2Bhiver",
1192                G_URI_FLAGS_NONE,
1193                NULL,
1194                NULL,
1195                &host,
1196                NULL,
1197                NULL,
1198                &query,
1199                NULL,
1200                &error);
1201   g_assert_no_error (error);
1202   g_assert_cmpstr (host, ==, "h\001st");
1203   g_assert_cmpstr (query, ==, "saisons=Été+hiver");
1204   g_free (host);
1205   g_free (query);
1206 
1207   g_uri_split ("http://h%01st/path?saisons=%C3%89t%C3%A9%2Bhiver",
1208                G_URI_FLAGS_ENCODED_QUERY,
1209                NULL,
1210                NULL,
1211                &host,
1212                NULL,
1213                NULL,
1214                &query,
1215                NULL,
1216                &error);
1217   g_assert_no_error (error);
1218   g_assert_cmpstr (host, ==, "h\001st");
1219   g_assert_cmpstr (query, ==, "saisons=%C3%89t%C3%A9%2Bhiver");
1220   g_free (host);
1221   g_free (query);
1222 
1223   g_uri_split ("http://h%01st/%C3%89t%C3%A9%2Bhiver",
1224                G_URI_FLAGS_ENCODED_PATH,
1225                NULL,
1226                NULL,
1227                NULL,
1228                NULL,
1229                &path,
1230                NULL,
1231                NULL,
1232                &error);
1233   g_assert_no_error (error);
1234   g_assert_cmpstr (path, ==, "/%C3%89t%C3%A9%2Bhiver");
1235   g_free (path);
1236 
1237   g_uri_split ("file:///path/to/some%20file",
1238                G_URI_FLAGS_NONE,
1239                NULL,
1240                NULL,
1241                NULL,
1242                NULL,
1243                &path,
1244                NULL,
1245                NULL,
1246                &error);
1247   g_assert_no_error (error);
1248   g_assert_cmpstr (path, ==, "/path/to/some file");
1249   g_free (path);
1250 
1251   g_uri_split ("http://h%01st/path#%C3%89t%C3%A9%2Bhiver",
1252                G_URI_FLAGS_ENCODED_FRAGMENT,
1253                NULL,
1254                NULL,
1255                NULL,
1256                NULL,
1257                NULL,
1258                NULL,
1259                &fragment,
1260                &error);
1261   g_assert_no_error (error);
1262   g_assert_cmpstr (fragment, ==, "%C3%89t%C3%A9%2Bhiver");
1263   g_free (fragment);
1264 
1265   g_uri_split_with_user ("scheme://user:pass;auth@host:1234/path?query#fragment",
1266                          G_URI_FLAGS_HAS_AUTH_PARAMS|G_URI_FLAGS_HAS_PASSWORD,
1267                          NULL,
1268                          &user,
1269                          &pass,
1270                          &authparams,
1271                          NULL,
1272                          NULL,
1273                          NULL,
1274                          NULL,
1275                          NULL,
1276                          &error);
1277   g_assert_no_error (error);
1278   g_assert_cmpstr (user, ==, "user");
1279   g_assert_cmpstr (pass, ==, "pass");
1280   g_assert_cmpstr (authparams, ==, "auth");
1281   g_free (user);
1282   g_free (pass);
1283   g_free (authparams);
1284 
1285   g_uri_split_network ("scheme://user:pass;auth@host:1234/path?query#fragment",
1286                        G_URI_FLAGS_NONE,
1287                        NULL,
1288                        NULL,
1289                        NULL,
1290                        &error);
1291   g_assert_no_error (error);
1292 
1293   g_uri_split_network ("scheme://user:pass;auth@host:1234/path?query#fragment",
1294                        G_URI_FLAGS_NONE,
1295                        &scheme,
1296                        &host,
1297                        &port,
1298                        &error);
1299   g_assert_no_error (error);
1300   g_assert_cmpstr (scheme, ==, "scheme");
1301   g_assert_cmpstr (host, ==, "host");
1302   g_assert_cmpint (port, ==, 1234);
1303   g_free (scheme);
1304   g_free (host);
1305 
1306   g_uri_split_network ("%00",
1307                        G_URI_FLAGS_NONE, NULL, NULL, NULL, &error);
1308   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_PATH);
1309   g_clear_error (&error);
1310 
1311   g_uri_split_network ("/a",
1312                        G_URI_FLAGS_NONE,
1313                        &scheme,
1314                        &host,
1315                        &port,
1316                        &error);
1317   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_SCHEME);
1318   g_clear_error (&error);
1319 
1320   g_uri_split_network ("schme:#",
1321                        G_URI_FLAGS_NONE,
1322                        &scheme,
1323                        &host,
1324                        &port,
1325                        &error);
1326   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_HOST);
1327   g_clear_error (&error);
1328 
1329   g_uri_split_network ("scheme://[]/a",
1330                        G_URI_FLAGS_NONE, NULL, NULL, NULL, &error);
1331   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_HOST);
1332   g_clear_error (&error);
1333 
1334   g_uri_split_network ("scheme://user%00:pass;auth@host",
1335                        G_URI_FLAGS_HAS_PASSWORD|G_URI_FLAGS_HAS_AUTH_PARAMS,
1336                        NULL, NULL, NULL, &error);
1337   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_USER);
1338   g_clear_error (&error);
1339 
1340   g_uri_split_network ("scheme://user:pass%00;auth@host",
1341                        G_URI_FLAGS_HAS_PASSWORD|G_URI_FLAGS_HAS_AUTH_PARAMS,
1342                        NULL, NULL, NULL, &error);
1343   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_PASSWORD);
1344   g_clear_error (&error);
1345 
1346   g_uri_split_network ("scheme://user:pass;auth@host:1234/path?quer%00y#fragment",
1347                        G_URI_FLAGS_NONE,
1348                        NULL, NULL, NULL, &error);
1349   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_QUERY);
1350   g_clear_error (&error);
1351 
1352   g_uri_split_network ("scheme://use%00r:pass;auth@host:1234/path",
1353                        G_URI_FLAGS_NONE,
1354                        NULL, NULL, NULL, &error);
1355   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_USER);
1356   g_clear_error (&error);
1357 
1358   g_uri_split ("scheme://user:pass;auth@host:1234/path?query#fragm%00ent",
1359                G_URI_FLAGS_NONE,
1360                &scheme,
1361                &userinfo,
1362                &host,
1363                &port,
1364                &path,
1365                &query,
1366                &fragment,
1367                &error);
1368   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_FRAGMENT);
1369   g_clear_error (&error);
1370 
1371   g_uri_split_with_user ("scheme://user:pa%x0s;auth@host:1234/path?query#fragment",
1372                          G_URI_FLAGS_HAS_PASSWORD,
1373                          &scheme,
1374                          &user,
1375                          &pass,
1376                          &authparams,
1377                          &host,
1378                          &port,
1379                          &path,
1380                          &query,
1381                          &fragment,
1382                          &error);
1383   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_PASSWORD);
1384   g_clear_error (&error);
1385 
1386   g_uri_split_with_user ("scheme://user:pass;auth%00@host",
1387                          G_URI_FLAGS_HAS_PASSWORD|G_URI_FLAGS_HAS_AUTH_PARAMS,
1388                          &scheme,
1389                          &user,
1390                          &pass,
1391                          &authparams,
1392                          &host,
1393                          &port,
1394                          &path,
1395                          &query,
1396                          &fragment,
1397                          &error);
1398   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_AUTH_PARAMS);
1399   g_clear_error (&error);
1400 
1401   g_uri_split_network ("scheme://user:pass%00;auth@host",
1402                        G_URI_FLAGS_HAS_PASSWORD|G_URI_FLAGS_HAS_AUTH_PARAMS,
1403                        NULL, NULL, NULL, &error);
1404   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_PASSWORD);
1405   g_clear_error (&error);
1406 
1407   /* Path not started correctly */
1408   g_uri_split("scheme://hostname:123path?query#fragment",
1409               G_URI_FLAGS_NONE,
1410               &scheme,
1411               &userinfo,
1412               &host,
1413               &port,
1414               &path,
1415               &query,
1416               &fragment,
1417               &error);
1418   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_PORT);
1419   g_clear_error (&error);
1420 
1421   /* Brackets that don't close */
1422   g_uri_split("scheme://[01:23:45:67:89:ab:cd:ef:123/path",
1423               G_URI_FLAGS_NONE,
1424               &scheme,
1425               &userinfo,
1426               &host,
1427               &port,
1428               &path,
1429               &query,
1430               &fragment,
1431               &error);
1432   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_HOST);
1433   g_clear_error (&error);
1434 
1435   /* IPv6 hostname without brackets */
1436   g_uri_split("scheme://01:23:45:67:89:ab:cd:ef:123/path",
1437               G_URI_FLAGS_NONE,
1438               &scheme,
1439               &userinfo,
1440               &host,
1441               &port,
1442               &path,
1443               &query,
1444               &fragment,
1445               &error);
1446   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_PORT);
1447   g_clear_error (&error);
1448 }
1449 
1450 static void
test_uri_is_valid(void)1451 test_uri_is_valid (void)
1452 {
1453   GError *error = NULL;
1454 
1455   g_assert_true (g_uri_is_valid ("http://[::192.9.5.5]/ipng", G_URI_FLAGS_NONE, NULL));
1456   g_assert_true (g_uri_is_valid ("http://127.127.127.127/", G_URI_FLAGS_NONE, NULL));
1457   g_assert_true (g_uri_is_valid ("http://127.127.127.b/", G_URI_FLAGS_NONE, NULL));
1458   g_assert_true (g_uri_is_valid ("http://\xc3\x89XAMPLE.COM/", G_URI_FLAGS_NONE, NULL));
1459 
1460   g_assert_true (g_uri_is_valid ("  \r http\t://f oo  \t\n ", G_URI_FLAGS_PARSE_RELAXED, NULL));
1461   g_assert_false (g_uri_is_valid ("  \r http\t://f oo  \t\n ", G_URI_FLAGS_NONE, &error));
1462   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_SCHEME);
1463   g_clear_error (&error);
1464 
1465   g_assert_false (g_uri_is_valid ("http://[::192.9.5.5/ipng", G_URI_FLAGS_NONE, &error));
1466   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_HOST);
1467   g_clear_error (&error);
1468 
1469   g_assert_true (g_uri_is_valid ("http://[fe80::dead:beef%25wef]/", G_URI_FLAGS_NONE, NULL));
1470   g_assert_false (g_uri_is_valid ("http://[fe80::dead:beef%wef%]/", G_URI_FLAGS_NONE, &error));
1471   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_HOST);
1472   g_clear_error (&error);
1473 
1474   g_assert_false (g_uri_is_valid ("http://%00/", G_URI_FLAGS_NON_DNS, &error));
1475   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_HOST);
1476   g_clear_error (&error);
1477 
1478   g_assert_true (g_uri_is_valid ("http://foo/", G_URI_FLAGS_NON_DNS, &error));
1479 
1480   g_assert_false (g_uri_is_valid ("http://%00/", G_URI_FLAGS_NONE, &error));
1481   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_HOST);
1482   g_clear_error (&error);
1483 
1484   g_assert_false (g_uri_is_valid ("http://%30.%30.%30.%30/", G_URI_FLAGS_NONE, &error));
1485   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_HOST);
1486   g_clear_error (&error);
1487 
1488   g_assert_false (g_uri_is_valid ("http://host:port", G_URI_FLAGS_NONE, &error));
1489   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_PORT);
1490   g_clear_error (&error);
1491 
1492   g_assert_false (g_uri_is_valid ("http://host:65536", G_URI_FLAGS_NONE, &error));
1493   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_PORT);
1494   g_clear_error (&error);
1495 
1496   g_assert_false (g_uri_is_valid ("http://host:6553l", G_URI_FLAGS_NONE, &error));
1497   g_assert_error (error, G_URI_ERROR, G_URI_ERROR_BAD_PORT);
1498   g_clear_error (&error);
1499 
1500   g_assert_true (g_uri_is_valid ("data:,Hello", G_URI_FLAGS_NONE, &error));
1501 
1502   g_assert_true (g_uri_is_valid ("B:\\foo.txt", G_URI_FLAGS_NONE, &error));
1503   g_assert_true (g_uri_is_valid ("B:/foo.txt", G_URI_FLAGS_NONE, &error));
1504   g_assert_true (g_uri_is_valid ("B://foo.txt", G_URI_FLAGS_NONE, &error));
1505   g_assert_true (g_uri_is_valid ("B:foo.txt", G_URI_FLAGS_NONE, &error));
1506 
1507   g_assert_true (g_uri_is_valid ("fd://0", G_URI_FLAGS_NONE, &error));
1508   g_assert_true (g_uri_is_valid ("AB:\\foo.txt", G_URI_FLAGS_NONE, &error));
1509   g_assert_true (g_uri_is_valid ("AB:/foo.txt", G_URI_FLAGS_NONE, &error));
1510   g_assert_true (g_uri_is_valid ("AB://foo.txt", G_URI_FLAGS_NONE, &error));
1511   g_assert_true (g_uri_is_valid ("AB:foo.txt", G_URI_FLAGS_NONE, &error));
1512 
1513   g_assert_true (g_uri_is_valid ("ABC:/foo.txt", G_URI_FLAGS_NONE, &error));
1514   g_assert_true (g_uri_is_valid ("ABC://foo.txt", G_URI_FLAGS_NONE, &error));
1515   g_assert_true (g_uri_is_valid ("ABC:foo.txt", G_URI_FLAGS_NONE, &error));
1516 
1517   g_assert_true (g_uri_is_valid ("ABCD:/foo.txt", G_URI_FLAGS_NONE, &error));
1518   g_assert_true (g_uri_is_valid ("ABCD://foo.txt", G_URI_FLAGS_NONE, &error));
1519   g_assert_true (g_uri_is_valid ("ABCD:foo.txt", G_URI_FLAGS_NONE, &error));
1520 }
1521 
1522 static const struct
1523 {
1524   /* Inputs */
1525   const gchar *uri;
1526   gchar *separators;
1527   GUriParamsFlags flags;
1528   /* Outputs */
1529   /* key, value, key, value, …, limited to length 2*expected_n_params */
1530   gssize expected_n_iter;  /* -1 => error expected */
1531   const gchar *expected_iter_key_values[6];
1532   gssize expected_n_params;  /* -1 => error expected */
1533   const gchar *expected_param_key_values[6];
1534 } params_tests[] =
1535   {
1536     { "p1=foo&p2=bar;p3=baz", "&;", G_URI_PARAMS_NONE,
1537       3, { "p1", "foo", "p2", "bar", "p3", "baz" },
1538       3, { "p1", "foo", "p2", "bar", "p3", "baz" }},
1539     { "p1=foo&p2=bar", "", G_URI_PARAMS_NONE,
1540       1, { "p1", "foo&p2=bar" },
1541       1, { "p1", "foo&p2=bar" }},
1542     { "p1=foo&&P1=bar", "&", G_URI_PARAMS_NONE,
1543       1, { "p1", "foo" },
1544       -1, { NULL, }},
1545     { "%00=foo", "&", G_URI_PARAMS_NONE,
1546       0, { NULL, },
1547       -1, { NULL, }},
1548     { "p1=%00", "&", G_URI_PARAMS_NONE,
1549       0, { NULL, },
1550       -1, { NULL, }},
1551     { "p1=foo&p1=bar", "&", G_URI_PARAMS_NONE,
1552       2, { "p1", "foo", "p1", "bar" },
1553       1, { "p1", "bar", NULL, }},
1554     { "p1=foo&P1=bar", "&", G_URI_PARAMS_CASE_INSENSITIVE,
1555       2, { "p1", "foo", "P1", "bar" },
1556       1, { "p1", "bar", NULL, }},
1557     { "=%", "&", G_URI_PARAMS_PARSE_RELAXED,
1558       1, { "", "%", NULL, },
1559       1, { "", "%", NULL, }},
1560     { "=", "&", G_URI_PARAMS_NONE,
1561       1, { "", "", NULL, },
1562       1, { "", "", NULL, }},
1563     { "foo", "&", G_URI_PARAMS_NONE,
1564       0, { NULL, },
1565       -1, { NULL, }},
1566     { "foo=bar+%26+baz&saisons=%C3%89t%C3%A9%2Bhiver", "&", G_URI_PARAMS_WWW_FORM,
1567       2, { "foo", "bar & baz", "saisons", "Été+hiver", NULL, },
1568       2, { "foo", "bar & baz", "saisons", "Été+hiver", NULL, }},
1569     { "foo=bar+%26+baz&saisons=%C3%89t%C3%A9%2Bhiver", "&", G_URI_PARAMS_NONE,
1570       2, { "foo", "bar+&+baz", "saisons", "Été+hiver", NULL, },
1571       2, { "foo", "bar+&+baz", "saisons", "Été+hiver", NULL, }},
1572     { "token=exp=123~acl=/QualityLevels(*~hmac=0cb", "&", G_URI_PARAMS_NONE,
1573       1, { "token", "exp=123~acl=/QualityLevels(*~hmac=0cb", NULL, },
1574       1, { "token", "exp=123~acl=/QualityLevels(*~hmac=0cb", NULL, }},
1575   };
1576 
1577 static void
test_uri_iter_params(gconstpointer test_data)1578 test_uri_iter_params (gconstpointer test_data)
1579 {
1580   GError *err = NULL;
1581   gboolean use_nul_terminated = GPOINTER_TO_INT (test_data);
1582   gsize i, n;
1583 
1584   for (i = 0; i < G_N_ELEMENTS (params_tests); i++)
1585     {
1586       GUriParamsIter iter;
1587       gchar *uri, *attr, *value;
1588       gssize uri_len;
1589 
1590       g_test_message ("URI %" G_GSIZE_FORMAT ": %s", i, params_tests[i].uri);
1591 
1592       g_assert (params_tests[i].expected_n_params < 0 ||
1593                 params_tests[i].expected_n_params <= (gssize) G_N_ELEMENTS (params_tests[i].expected_param_key_values) / 2);
1594 
1595       /* The tests get run twice: once with the length unspecified, using a
1596        * nul-terminated string; and once with the length specified and a copy of
1597        * the string with the trailing nul explicitly removed (to help catch
1598        * buffer overflows). */
1599       if (use_nul_terminated)
1600         {
1601           uri_len = -1;
1602           uri = g_strdup (params_tests[i].uri);
1603         }
1604       else
1605         {
1606           uri_len = strlen (params_tests[i].uri);  /* no trailing nul */
1607           uri = g_memdup2 (params_tests[i].uri, uri_len);
1608         }
1609 
1610       /* Run once without extracting the attr or value, just to check the numbers. */
1611       n = 0;
1612       g_uri_params_iter_init (&iter, params_tests[i].uri, -1, params_tests[i].separators, params_tests[i].flags);
1613       while (g_uri_params_iter_next (&iter, NULL, NULL, &err))
1614         n++;
1615       g_assert_cmpint (n, ==, params_tests[i].expected_n_iter);
1616       if (err)
1617         {
1618           g_assert_error (err, G_URI_ERROR, G_URI_ERROR_FAILED);
1619           g_clear_error (&err);
1620         }
1621 
1622       /* Run again and check the strings too. */
1623       n = 0;
1624       g_uri_params_iter_init (&iter, params_tests[i].uri, -1, params_tests[i].separators, params_tests[i].flags);
1625       while (g_uri_params_iter_next (&iter, &attr, &value, &err))
1626         {
1627           g_assert_cmpstr (attr, ==, params_tests[i].expected_iter_key_values[n * 2]);
1628           g_assert_cmpstr (value, ==, params_tests[i].expected_iter_key_values[n * 2 + 1]);
1629           n++;
1630           g_free (attr);
1631           g_free (value);
1632         }
1633       g_assert_cmpint (n, ==, params_tests[i].expected_n_iter);
1634       if (err)
1635         {
1636           g_assert_error (err, G_URI_ERROR, G_URI_ERROR_FAILED);
1637           g_clear_error (&err);
1638         }
1639 
1640       g_free (uri);
1641     }
1642 }
1643 
1644 static void
test_uri_parse_params(gconstpointer test_data)1645 test_uri_parse_params (gconstpointer test_data)
1646 {
1647   GError *err = NULL;
1648   gboolean use_nul_terminated = GPOINTER_TO_INT (test_data);
1649   gsize i;
1650 
1651   for (i = 0; i < G_N_ELEMENTS (params_tests); i++)
1652     {
1653       GHashTable *params;
1654       gchar *uri = NULL;
1655       gssize uri_len;
1656 
1657       g_test_message ("URI %" G_GSIZE_FORMAT ": %s", i, params_tests[i].uri);
1658 
1659       g_assert (params_tests[i].expected_n_params < 0 ||
1660                 params_tests[i].expected_n_params <= (gssize) G_N_ELEMENTS (params_tests[i].expected_param_key_values) / 2);
1661 
1662       /* The tests get run twice: once with the length unspecified, using a
1663        * nul-terminated string; and once with the length specified and a copy of
1664        * the string with the trailing nul explicitly removed (to help catch
1665        * buffer overflows). */
1666       if (use_nul_terminated)
1667         {
1668           uri_len = -1;
1669           uri = g_strdup (params_tests[i].uri);
1670         }
1671       else
1672         {
1673           uri_len = strlen (params_tests[i].uri);  /* no trailing nul */
1674           uri = g_memdup2 (params_tests[i].uri, uri_len);
1675         }
1676 
1677       params = g_uri_parse_params (uri, uri_len, params_tests[i].separators, params_tests[i].flags, &err);
1678 
1679       if (params_tests[i].expected_n_params < 0)
1680         {
1681           g_assert_null (params);
1682           g_assert_error (err, G_URI_ERROR, G_URI_ERROR_FAILED);
1683           g_clear_error (&err);
1684         }
1685       else
1686         {
1687           gsize j;
1688 
1689           g_assert_no_error (err);
1690           g_assert_cmpint (g_hash_table_size (params), ==, params_tests[i].expected_n_params);
1691 
1692           for (j = 0; j < (gsize) params_tests[i].expected_n_params; j += 2)
1693             g_assert_cmpstr (g_hash_table_lookup (params, params_tests[i].expected_param_key_values[j]), ==,
1694                              params_tests[i].expected_param_key_values[j + 1]);
1695         }
1696 
1697       g_clear_pointer (&params, g_hash_table_unref);
1698       g_free (uri);
1699     }
1700 }
1701 
1702 static void
test_uri_join(void)1703 test_uri_join (void)
1704 {
1705   gchar *uri = NULL;
1706 
1707   uri = g_uri_join (G_URI_FLAGS_NONE, "foo", "some:user@info", "bar", -1, "", NULL, NULL);
1708   g_assert_cmpstr (uri, ==, "foo://some:user%40info@bar");
1709   g_free (uri);
1710 
1711   uri = g_uri_join (G_URI_FLAGS_NONE, NULL, NULL, NULL, -1, "/foo", "abc", NULL);
1712   g_assert_cmpstr (uri, ==, "/foo?abc");
1713   g_free (uri);
1714 
1715   uri = g_uri_join (G_URI_FLAGS_NONE, NULL, NULL, "hostname", -1, "/foo", "abc", NULL);
1716   g_assert_cmpstr (uri, ==, "//hostname/foo?abc");
1717   g_free (uri);
1718 
1719   uri = g_uri_join_with_user (G_URI_FLAGS_NONE, "scheme", "user\001", "pass\002", "authparams\003",
1720                               "host", 9876, "/path", "query", "fragment");
1721   g_assert_cmpstr (uri, ==, "scheme://user%01:pass%02;authparams%03@host:9876/path?query#fragment");
1722   g_free (uri);
1723 
1724   uri = g_uri_join_with_user (G_URI_FLAGS_NONE, "scheme", "user\001", "pass\002", "authparams\003",
1725                               "::192.9.5.5", 9876, "/path", "query", "fragment");
1726   g_assert_cmpstr (uri, ==, "scheme://user%01:pass%02;authparams%03@[::192.9.5.5]:9876/path?query#fragment");
1727   g_free (uri);
1728 
1729   uri = g_uri_join_with_user (G_URI_FLAGS_ENCODED,
1730                               "scheme", "user%01", "pass%02", "authparams%03",
1731                               "::192.9.5.5", 9876, "/path", "query", "fragment");
1732   g_assert_cmpstr (uri, ==,
1733                    "scheme://user%01:pass%02;authparams%03@[::192.9.5.5]:9876/path?query#fragment");
1734   g_free (uri);
1735 
1736   uri = g_uri_join (G_URI_FLAGS_NONE, "scheme", NULL, "foo:bar._webdav._tcp.local", -1, "", NULL, NULL);
1737   g_assert_cmpstr (uri, ==, "scheme://foo%3Abar._webdav._tcp.local");
1738   g_free (uri);
1739 }
1740 
1741 static void
test_uri_join_split_round_trip(void)1742 test_uri_join_split_round_trip (void)
1743 {
1744   GUriFlags flags = G_URI_FLAGS_HAS_PASSWORD | G_URI_FLAGS_HAS_AUTH_PARAMS;
1745   guint i;
1746 
1747   g_test_summary ("Test that joining different URI components survives a round trip");
1748 
1749   /* Each bit in @i indicates whether the corresponding URI field should be set
1750    * or %NULL. */
1751   for (i = 0; i < (1 << 8); i++)
1752     {
1753       gchar *uri = NULL;
1754       const gchar *scheme, *user, *password, *auth_params, *host, *path, *query, *fragment;
1755       gint port;
1756       gchar *scheme_out = NULL, *user_out = NULL, *password_out = NULL;
1757       gchar *auth_params_out = NULL, *host_out = NULL, *path_out = NULL;
1758       gchar *query_out = NULL, *fragment_out = NULL;
1759       gint port_out = -1;
1760       gboolean split_success;
1761       GError *local_error = NULL;
1762 
1763       g_test_message ("Combination %u", i);
1764 
1765       scheme = (i & (1 << 8)) ? "scheme" : NULL;
1766       host = (i & (1 << 4)) ? "host" : NULL;
1767       user = (host != NULL && i & (1 << 7)) ? "user" : NULL;  /* only supported if host is also set */
1768       password = (host != NULL && user != NULL && i & (1 << 6)) ? "password" : NULL;  /* only supported if host and user are also set */
1769       auth_params = (host != NULL && user != NULL && i & (1 << 5)) ? "auth_params" : NULL;  /* only supported if host and user are also set */
1770       port = (host != NULL && i & (1 << 3)) ? 123 : -1;  /* only supported if host is also set */
1771       path = (i & (1 << 2)) ? "/path" : "";  /* the only mandatory component */
1772       query = (i & (1 << 1)) ? "query" : NULL;
1773       fragment = (i & (1 << 0)) ? "fragment" : NULL;
1774 
1775       uri = g_uri_join_with_user (flags, scheme, user, password, auth_params,
1776                                   host, port, path, query, fragment);
1777       g_assert_nonnull (uri);
1778 
1779       split_success = g_uri_split_with_user (uri, flags, &scheme_out, &user_out,
1780                                              &password_out, &auth_params_out,
1781                                              &host_out, &port_out, &path_out,
1782                                              &query_out, &fragment_out,
1783                                              &local_error);
1784       g_assert_no_error (local_error);
1785       g_assert_true (split_success);
1786 
1787       g_assert_cmpstr (scheme, ==, scheme_out);
1788       g_assert_cmpstr (user, ==, user_out);
1789       g_assert_cmpstr (password, ==, password_out);
1790       g_assert_cmpstr (auth_params, ==, auth_params_out);
1791       g_assert_cmpstr (host, ==, host_out);
1792       g_assert_cmpint (port, ==, port_out);
1793       g_assert_cmpstr (path, ==, path_out);
1794       g_assert_cmpstr (query, ==, query_out);
1795       g_assert_cmpstr (fragment, ==, fragment_out);
1796 
1797       g_free (uri);
1798       g_free (scheme_out);
1799       g_free (user_out);
1800       g_free (password_out);
1801       g_free (auth_params_out);
1802       g_free (host_out);
1803       g_free (path_out);
1804       g_free (query_out);
1805       g_free (fragment_out);
1806     }
1807 }
1808 
1809 static const struct
1810 {
1811   /* Inputs */
1812   const gchar *base;
1813   const gchar *uri;
1814   GUriFlags flags;
1815   /* Outputs */
1816   const gchar *uri_string;
1817   const gchar *path;
1818   int port;
1819 } normalize_parse_tests[] =
1820   {
1821     { NULL, "http://foo/path with spaces", G_URI_FLAGS_ENCODED,
1822       "http://foo/path%20with%20spaces", "/path%20with%20spaces", -1 },
1823     { NULL, "http://foo/path with spaces 2", G_URI_FLAGS_ENCODED_PATH,
1824       "http://foo/path%20with%20spaces%202", "/path%20with%20spaces%202", -1 },
1825     { NULL, "http://foo/%aa", G_URI_FLAGS_ENCODED,
1826       "http://foo/%AA", "/%AA", -1 },
1827     { NULL, "http://foo/p\xc3\xa4th/", G_URI_FLAGS_ENCODED | G_URI_FLAGS_PARSE_RELAXED,
1828       "http://foo/p%C3%A4th/", "/p%C3%A4th/", -1 },
1829     { NULL, "http://foo", G_URI_FLAGS_NONE,
1830       "http://foo", "", -1 },
1831     { NULL, "http://foo", G_URI_FLAGS_SCHEME_NORMALIZE,
1832       "http://foo/", "/", 80 },
1833     { NULL, "nothttp://foo", G_URI_FLAGS_SCHEME_NORMALIZE,
1834       "nothttp://foo", "", -1 },
1835     { NULL, "http://foo:80", G_URI_FLAGS_NONE,
1836       "http://foo:80", "", 80 },
1837     { NULL, "http://foo:80", G_URI_FLAGS_SCHEME_NORMALIZE,
1838       "http://foo/", "/", 80 },
1839     { NULL, "http://foo:8080", G_URI_FLAGS_SCHEME_NORMALIZE,
1840       "http://foo:8080/", "/", 8080 },
1841     { NULL, "https://foo:443", G_URI_FLAGS_SCHEME_NORMALIZE,
1842       "https://foo/", "/", 443 },
1843     { NULL, "https://foo:943", G_URI_FLAGS_SCHEME_NORMALIZE,
1844       "https://foo:943/", "/", 943 },
1845     { NULL, "ws://foo", G_URI_FLAGS_SCHEME_NORMALIZE,
1846       "ws://foo/", "/", 80 },
1847     { NULL, "wss://foo:443", G_URI_FLAGS_SCHEME_NORMALIZE,
1848       "wss://foo/", "/", 443 },
1849     { NULL, "ftp://foo", G_URI_FLAGS_NONE,
1850       "ftp://foo", "", -1 },
1851     { NULL, "ftp://foo", G_URI_FLAGS_SCHEME_NORMALIZE,
1852       "ftp://foo", "", 21 },
1853     { NULL, "ftp://foo:21", G_URI_FLAGS_SCHEME_NORMALIZE,
1854       "ftp://foo", "", 21 },
1855     { NULL, "ftp://foo:2100", G_URI_FLAGS_SCHEME_NORMALIZE,
1856       "ftp://foo:2100", "", 2100 },
1857     { NULL, "nothttp://foo:80", G_URI_FLAGS_SCHEME_NORMALIZE,
1858       "nothttp://foo:80", "", 80 },
1859     { "http://foo", "//bar", G_URI_FLAGS_SCHEME_NORMALIZE,
1860       "http://bar/", "/", 80 },
1861     { "http://foo", "//bar:80", G_URI_FLAGS_SCHEME_NORMALIZE,
1862       "http://bar/", "/", 80 },
1863     { "nothttp://foo", "//bar:80", G_URI_FLAGS_SCHEME_NORMALIZE,
1864       "nothttp://bar:80", "", 80 },
1865     { "http://foo", "//bar", G_URI_FLAGS_NONE,
1866       "http://bar", "", -1 },
1867     { "ScHeMe://User:P%61ss@HOST.%63om:1234/path",
1868       "ScHeMe://User:P%61ss@HOST.%63om:1234/path/./from/../to%7d/item%2dobj?qu%65ry=something#fr%61gment",
1869       G_URI_FLAGS_SCHEME_NORMALIZE,
1870       "scheme://User:Pass@HOST.com:1234/path/to%7D/item-obj?query=something#fragment",
1871       "/path/to}/item-obj", 1234 },
1872   };
1873 
1874 static const struct
1875 {
1876   /* Inputs */
1877   const gchar *uri;
1878   GUriFlags flags;
1879   /* Outputs */
1880   const char *scheme;
1881   const gchar *path;
1882   int port;
1883 } normalize_split_tests[] =
1884   {
1885     { "HTTP://foo", G_URI_FLAGS_ENCODED,
1886       "http", "", -1 },
1887     { "HTTP://foo", G_URI_FLAGS_SCHEME_NORMALIZE,
1888       "http", "/", 80 },
1889     { "http://foo:80/", G_URI_FLAGS_SCHEME_NORMALIZE,
1890       "http", "/", 80 },
1891     { "http://foo:8080/bar", G_URI_FLAGS_SCHEME_NORMALIZE,
1892       "http", "/bar", 8080 },
1893     { "ws://foo", G_URI_FLAGS_SCHEME_NORMALIZE,
1894       "ws", "/", 80 },
1895     { "https://foo", G_URI_FLAGS_ENCODED,
1896       "https", "", -1 },
1897     { "https://foo", G_URI_FLAGS_SCHEME_NORMALIZE,
1898       "https", "/", 443 },
1899     { "https://foo:443/", G_URI_FLAGS_SCHEME_NORMALIZE,
1900       "https", "/", 443 },
1901     { "wss://foo", G_URI_FLAGS_SCHEME_NORMALIZE,
1902       "wss", "/", 443 },
1903     { "ftp://foo", G_URI_FLAGS_ENCODED,
1904       "ftp", "", -1 },
1905     { "ftp://foo", G_URI_FLAGS_SCHEME_NORMALIZE,
1906       "ftp", "", 21 },
1907     { "ftp://foo:21", G_URI_FLAGS_SCHEME_NORMALIZE,
1908       "ftp", "", 21 },
1909     { "scheme://foo", G_URI_FLAGS_SCHEME_NORMALIZE,
1910       "scheme", "", -1 },
1911   };
1912 
1913 static const struct
1914 {
1915   /* Inputs */
1916   GUriFlags flags;
1917   const gchar *scheme;
1918   const gchar *host;
1919   int port;
1920   const gchar *path;
1921   /* Outputs */
1922   const gchar *uri;
1923 } normalize_join_tests[] =
1924   {
1925     { G_URI_FLAGS_NONE, "http", "foo", -1, "",
1926       "http://foo" },
1927     { G_URI_FLAGS_SCHEME_NORMALIZE, "http", "foo", -1, "",
1928       "http://foo/" },
1929     { G_URI_FLAGS_SCHEME_NORMALIZE, "http", "foo", 80, "",
1930       "http://foo/" },
1931     { G_URI_FLAGS_SCHEME_NORMALIZE, "http", "foo", 8080, "",
1932       "http://foo:8080/" },
1933     { G_URI_FLAGS_NONE, "http", "foo", 80, "",
1934       "http://foo:80" },
1935     { G_URI_FLAGS_SCHEME_NORMALIZE, "ws", "foo", 80, "",
1936       "ws://foo/" },
1937     { G_URI_FLAGS_NONE, "https", "foo", -1, "",
1938       "https://foo" },
1939     { G_URI_FLAGS_SCHEME_NORMALIZE, "https", "foo", -1, "",
1940       "https://foo/" },
1941     { G_URI_FLAGS_SCHEME_NORMALIZE, "https", "foo", 443, "",
1942       "https://foo/" },
1943     { G_URI_FLAGS_SCHEME_NORMALIZE, "https", "foo", 943, "",
1944       "https://foo:943/" },
1945     { G_URI_FLAGS_NONE, "https", "foo", 443, "",
1946       "https://foo:443" },
1947     { G_URI_FLAGS_SCHEME_NORMALIZE, "wss", "foo", 443, "",
1948       "wss://foo/" },
1949     { G_URI_FLAGS_NONE, "ftp", "foo", -1, "",
1950       "ftp://foo" },
1951     { G_URI_FLAGS_SCHEME_NORMALIZE, "ftp", "foo", -1, "",
1952       "ftp://foo" },
1953     { G_URI_FLAGS_SCHEME_NORMALIZE, "ftp", "foo", 21, "",
1954       "ftp://foo" },
1955     { G_URI_FLAGS_SCHEME_NORMALIZE, "ftp", "foo", 2020, "",
1956       "ftp://foo:2020" },
1957     { G_URI_FLAGS_NONE, "ftp", "foo", 21, "",
1958       "ftp://foo:21" },
1959     { G_URI_FLAGS_SCHEME_NORMALIZE, "scheme", "foo", 80, "",
1960       "scheme://foo:80" },
1961   };
1962 
1963 static void
test_uri_normalize(void)1964 test_uri_normalize (void)
1965 {
1966   gsize i;
1967   int port;
1968   char *path;
1969   char *uri_string;
1970 
1971   for (i = 0; i < G_N_ELEMENTS (normalize_parse_tests); ++i)
1972     {
1973       GUri *uri, *base = NULL;
1974 
1975       if (normalize_parse_tests[i].base)
1976         base = g_uri_parse (normalize_parse_tests[i].base, normalize_parse_tests[i].flags, NULL);
1977 
1978       uri = g_uri_parse_relative (base,
1979                                   normalize_parse_tests[i].uri,
1980                                   normalize_parse_tests[i].flags,
1981                                   NULL);
1982       uri_string = g_uri_to_string (uri);
1983 
1984       g_assert_nonnull (uri);
1985       g_assert_cmpstr (g_uri_get_path (uri), ==, normalize_parse_tests[i].path);
1986       g_assert_cmpint (g_uri_get_port (uri), ==, normalize_parse_tests[i].port);
1987       g_assert_cmpstr (uri_string, ==, normalize_parse_tests[i].uri_string);
1988 
1989       g_free (uri_string);
1990       g_uri_unref (uri);
1991       if (base)
1992         g_uri_unref (base);
1993     }
1994 
1995   for (i = 0; i < G_N_ELEMENTS (normalize_split_tests); ++i)
1996     {
1997       char *scheme;
1998 
1999       /* Testing a codepath where scheme is NULL but internally we still normalize it. */
2000       g_assert_true (g_uri_split (normalize_split_tests[i].uri, normalize_split_tests[i].flags,
2001                                   NULL, NULL, NULL, &port, &path, NULL, NULL, NULL));
2002       g_assert_cmpstr (path, ==, normalize_split_tests[i].path);
2003       g_assert_cmpint (port, ==, normalize_split_tests[i].port);
2004       g_free (path);
2005 
2006       g_assert_true (g_uri_split (normalize_split_tests[i].uri, normalize_split_tests[i].flags,
2007                                   &scheme, NULL, NULL, &port, &path, NULL, NULL, NULL));
2008       g_assert_cmpstr (scheme, ==, normalize_split_tests[i].scheme);
2009       g_assert_cmpstr (path, ==, normalize_split_tests[i].path);
2010       g_assert_cmpint (port, ==, normalize_split_tests[i].port);
2011       g_free (scheme);
2012       g_free (path);
2013     }
2014 
2015   for (i = 0; i < G_N_ELEMENTS (normalize_join_tests); ++i)
2016     {
2017       uri_string = g_uri_join (normalize_join_tests[i].flags, normalize_join_tests[i].scheme, NULL,
2018                                normalize_join_tests[i].host, normalize_join_tests[i].port,
2019                                normalize_join_tests[i].path, NULL, NULL);
2020       g_assert_cmpstr (uri_string, ==, normalize_join_tests[i].uri);
2021       g_free (uri_string);
2022     }
2023 }
2024 
2025 int
main(int argc,char * argv[])2026 main (int   argc,
2027       char *argv[])
2028 {
2029   g_test_init (&argc, &argv, NULL);
2030 
2031   g_test_add_func ("/uri/file-to-uri", run_file_to_uri_tests);
2032   g_test_add_func ("/uri/file-from-uri", run_file_from_uri_tests);
2033   g_test_add_func ("/uri/file-roundtrip", run_file_roundtrip_tests);
2034   g_test_add_func ("/uri/list", run_uri_list_tests);
2035   g_test_add_func ("/uri/unescape-string", test_uri_unescape_string);
2036   g_test_add_data_func ("/uri/unescape-bytes/nul-terminated", GINT_TO_POINTER (TRUE), test_uri_unescape_bytes);
2037   g_test_add_data_func ("/uri/unescape-bytes/length", GINT_TO_POINTER (FALSE), test_uri_unescape_bytes);
2038   g_test_add_func ("/uri/unescape-segment", test_uri_unescape_segment);
2039   g_test_add_func ("/uri/escape-string", test_uri_escape_string);
2040   g_test_add_func ("/uri/escape-bytes", test_uri_escape_bytes);
2041   g_test_add_func ("/uri/scheme", test_uri_scheme);
2042   g_test_add_func ("/uri/parsing/absolute", test_uri_parsing_absolute);
2043   g_test_add_func ("/uri/parsing/relative", test_uri_parsing_relative);
2044   g_test_add_func ("/uri/build", test_uri_build);
2045   g_test_add_func ("/uri/split", test_uri_split);
2046   g_test_add_func ("/uri/is_valid", test_uri_is_valid);
2047   g_test_add_func ("/uri/to-string", test_uri_to_string);
2048   g_test_add_func ("/uri/join", test_uri_join);
2049   g_test_add_func ("/uri/join-split-round-trip", test_uri_join_split_round_trip);
2050   g_test_add_func ("/uri/normalize", test_uri_normalize);
2051   g_test_add_data_func ("/uri/iter-params/nul-terminated", GINT_TO_POINTER (TRUE), test_uri_iter_params);
2052   g_test_add_data_func ("/uri/iter-params/length", GINT_TO_POINTER (FALSE), test_uri_iter_params);
2053   g_test_add_data_func ("/uri/parse-params/nul-terminated", GINT_TO_POINTER (TRUE), test_uri_parse_params);
2054   g_test_add_data_func ("/uri/parse-params/length", GINT_TO_POINTER (FALSE), test_uri_parse_params);
2055 
2056   return g_test_run ();
2057 }
2058