LCOV - code coverage report
Current view: top level - glib/glib/tests - uri.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 663 663 100.0 %
Date: 2024-04-16 05:15:53 Functions: 24 24 100.0 %
Branches: 95 96 99.0 %

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

Generated by: LCOV version 1.14