LCOV - code coverage report
Current view: top level - glib/gio/tests - g-file.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 195 196 99.5 %
Date: 2024-04-23 05:16:05 Functions: 15 15 100.0 %
Branches: 45 48 93.8 %

           Branch data     Line data    Source code
       1                 :            : /* GLib testing framework examples and tests
       2                 :            :  * Copyright (C) 2008 Red Hat, Inc.
       3                 :            :  * Authors: Tomas Bzatek <tbzatek@redhat.com>
       4                 :            :  *
       5                 :            :  * SPDX-License-Identifier: LicenseRef-old-glib-tests
       6                 :            :  *
       7                 :            :  * This work is provided "as is"; redistribution and modification
       8                 :            :  * in whole or in part, in any medium, physical or electronic is
       9                 :            :  * permitted without restriction.
      10                 :            :  *
      11                 :            :  * This work 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.
      14                 :            :  *
      15                 :            :  * In no event shall the authors or contributors be liable for any
      16                 :            :  * direct, indirect, incidental, special, exemplary, or consequential
      17                 :            :  * damages (including, but not limited to, procurement of substitute
      18                 :            :  * goods or services; loss of use, data, or profits; or business
      19                 :            :  * interruption) however caused and on any theory of liability, whether
      20                 :            :  * in contract, strict liability, or tort (including negligence or
      21                 :            :  * otherwise) arising in any way out of the use of this software, even
      22                 :            :  * if advised of the possibility of such damage.
      23                 :            :  */
      24                 :            : 
      25                 :            : #include <glib/glib.h>
      26                 :            : #include <gio/gio.h>
      27                 :            : #include <stdlib.h>
      28                 :            : #include <string.h>
      29                 :            : 
      30                 :            : struct TestPathsWithOper {
      31                 :            :   const char *path1;
      32                 :            :   gboolean equal;
      33                 :            :   gboolean use_uri;
      34                 :            :   const char *path2;
      35                 :            :   const char *path3;
      36                 :            : };
      37                 :            : 
      38                 :            : 
      39                 :            : 
      40                 :            : /* TODO:
      41                 :            :  *   - test on Windows
      42                 :            :  * 
      43                 :            :  **/
      44                 :            : 
      45                 :            : static void
      46                 :          1 : test_g_file_new_null (void)
      47                 :            : {
      48                 :          1 :   const char *paths[] = {"/",
      49                 :            :                          "/tmp///",
      50                 :            :                          "/non-existent-file",
      51                 :            :                          "/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88",
      52                 :            :                          NULL
      53                 :            :   };
      54                 :          1 :   const char *uris[] = {"file:///",
      55                 :            :                         "file:///tmp///",
      56                 :            :                         "non-existent-uri:///some-dir/",
      57                 :            :                         "file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88",
      58                 :            :                         NULL
      59                 :            :   };
      60                 :            :   
      61                 :          1 :   GFile *file = NULL;
      62                 :            :   
      63                 :          1 :   int i = 0;
      64         [ +  + ]:          5 :   while (paths[i])
      65                 :            :     {
      66                 :          4 :       file = g_file_new_for_path (paths[i++]);
      67                 :          4 :       g_assert (file != NULL);
      68                 :          4 :       g_object_unref (file);
      69                 :            :     }
      70                 :            :   
      71                 :          1 :   i = 0;
      72         [ +  + ]:          5 :   while (uris[i])
      73                 :            :     {
      74                 :          4 :       file = g_file_new_for_uri (uris[i++]);
      75                 :          4 :       g_assert (file != NULL);
      76                 :          4 :       g_object_unref(file);
      77                 :            :     }
      78                 :          1 : }
      79                 :            : 
      80                 :            : 
      81                 :            : 
      82                 :            : static gboolean
      83                 :         28 : compare_two_files (const gboolean use_uri, const char *path1, const char *path2)
      84                 :            : {
      85                 :         28 :   GFile *file1 = NULL;
      86                 :         28 :   GFile *file2 = NULL;
      87                 :            :   gboolean equal;
      88                 :            : 
      89         [ +  + ]:         28 :   if (use_uri)
      90                 :            :     {
      91                 :         13 :       file1 = g_file_new_for_uri (path1);
      92                 :         13 :       file2 = g_file_new_for_uri (path2);
      93                 :            :     }
      94                 :            :   else
      95                 :            :     {
      96                 :         15 :       file1 = g_file_new_for_path (path1);
      97                 :         15 :       file2 = g_file_new_for_path (path2);
      98                 :            :     }
      99                 :            : 
     100                 :         28 :   g_assert (file1 != NULL);
     101                 :         28 :   g_assert (file2 != NULL);
     102                 :            :   
     103                 :         28 :   equal = g_file_equal (file1, file2);
     104                 :            :   
     105                 :         28 :   g_object_unref (file1);
     106                 :         28 :   g_object_unref (file2);
     107                 :            :   
     108                 :         28 :   return equal;
     109                 :            : }
     110                 :            : 
     111                 :            : static void
     112                 :          1 : test_g_file_new_for_path (void)
     113                 :            : {
     114                 :          1 :   const struct TestPathsWithOper cmp_paths[] =
     115                 :            :     {
     116                 :            :       {"/", TRUE, 0, "/./", NULL },
     117                 :            :       {"//", TRUE, 0, "//", NULL },
     118                 :            :       {"//", TRUE, 0, "//./", NULL },
     119                 :            :       {"/", TRUE, 0, "/.//", NULL },
     120                 :            :       {"/", TRUE, 0, "/././", NULL },
     121                 :            :       {"/tmp", TRUE, 0, "/tmp/d/../", NULL },
     122                 :            :       {"/", TRUE, 0, "/somedir/../", NULL },
     123                 :            :       {"/", FALSE, 0, "/somedir/.../", NULL },
     124                 :            :       {"//tmp/dir1", TRUE, 0, "//tmp/dir1", NULL },
     125                 :            :       {"/tmp/dir1", TRUE, 0, "///tmp/dir1", NULL },
     126                 :            :       {"/tmp/dir1", TRUE, 0, "////tmp/dir1", NULL },
     127                 :            :       {"/tmp/dir1", TRUE, 0, "/tmp/./dir1", NULL },
     128                 :            :       {"/tmp/dir1", TRUE, 0, "/tmp//dir1", NULL },
     129                 :            :       {"/tmp/dir1", TRUE, 0, "/tmp///dir1///", NULL },
     130                 :            :       {"/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", TRUE, 0, "/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88/", NULL }
     131                 :            :     };
     132                 :            : 
     133                 :            :   guint i;
     134         [ +  + ]:         16 :   for (i = 0; i < G_N_ELEMENTS (cmp_paths); i++)
     135                 :            :     {
     136                 :         15 :       gboolean equal = compare_two_files (FALSE, cmp_paths[i].path1, cmp_paths[i].path2);
     137                 :         15 :       g_assert_cmpint (equal, ==, cmp_paths[i].equal);
     138                 :            :     }
     139                 :          1 : }
     140                 :            : 
     141                 :            : 
     142                 :            : 
     143                 :            : static void
     144                 :          1 : test_g_file_new_for_uri (void)
     145                 :            : {
     146                 :          1 :   const struct TestPathsWithOper cmp_uris[] = {
     147                 :            :     {"file:///", TRUE, 0, "file:///./", NULL },
     148                 :            :     {"file:////", TRUE, 0, "file:////", NULL },
     149                 :            :     {"file:////", TRUE, 0, "file:////./", NULL },
     150                 :            :     {"file:///", TRUE, 0, "file:///.//", NULL },
     151                 :            :     {"file:///", TRUE, 0, "file:///././", NULL },
     152                 :            :     {"file:///tmp", TRUE, 0, "file:///tmp/d/../", NULL },
     153                 :            :     {"file:///", TRUE, 0, "file:///somedir/../", NULL },
     154                 :            :     {"file:///", FALSE, 0, "file:///somedir/.../", NULL },
     155                 :            :     {"file:////tmp/dir1", TRUE, 0, "file:////tmp/dir1", NULL },
     156                 :            :     {"file:///tmp/dir1", TRUE, 0, "file:///tmp/./dir1", NULL },
     157                 :            :     {"file:///tmp/dir1", TRUE, 0, "file:///tmp//dir1", NULL },
     158                 :            :     {"file:///tmp/dir1", TRUE, 0, "file:///tmp///dir1///", NULL },
     159                 :            :     {"file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88", TRUE, 0, "file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88/", NULL }
     160                 :            :   };
     161                 :            :   
     162                 :            :   guint i;
     163         [ +  + ]:         14 :   for (i = 0; i < G_N_ELEMENTS (cmp_uris); i++)
     164                 :            :     {
     165                 :         13 :       gboolean equal = compare_two_files (TRUE, cmp_uris[i].path1, cmp_uris[i].path2);
     166                 :         13 :       g_assert_cmpint (equal, ==, cmp_uris[i].equal);
     167                 :            :     }
     168                 :          1 : }
     169                 :            : 
     170                 :            : 
     171                 :            : 
     172                 :            : static gboolean
     173                 :          5 : dup_equals (const gboolean use_uri, const char *path)
     174                 :            : {
     175                 :          5 :   GFile *file1 = NULL;
     176                 :          5 :   GFile *file2 = NULL;
     177                 :            :   gboolean equal;
     178                 :            :   
     179         [ +  + ]:          5 :   if (use_uri) 
     180                 :          2 :     file1 = g_file_new_for_uri (path);
     181                 :            :   else
     182                 :          3 :     file1 = g_file_new_for_path (path);
     183                 :            :         
     184                 :          5 :   g_assert (file1 != NULL);
     185                 :            :   
     186                 :          5 :   file2 = g_file_dup (file1);
     187                 :            :   
     188                 :          5 :   g_assert (file2 != NULL);
     189                 :            :   
     190                 :          5 :   equal = g_file_equal (file1, file2);
     191                 :            :   
     192                 :          5 :   g_object_unref (file1);
     193                 :          5 :   g_object_unref (file2);
     194                 :            :         
     195                 :          5 :   return equal;
     196                 :            : }
     197                 :            : 
     198                 :            : static void
     199                 :          1 : test_g_file_dup (void)
     200                 :            : {
     201                 :          1 :   const struct TestPathsWithOper dup_paths[] =
     202                 :            :     {
     203                 :            :       {"/", 0, FALSE, "", NULL },
     204                 :            :       {"file:///", 0, TRUE, "", NULL },
     205                 :            :       {"totalnonsense", 0, FALSE, "", NULL },
     206                 :            :       {"/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", 0, FALSE, "", NULL },
     207                 :            :       {"file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88", 0, TRUE, "", NULL },
     208                 :            :     };
     209                 :            :   
     210                 :            :   guint i;
     211         [ +  + ]:          6 :   for (i = 0; i < G_N_ELEMENTS (dup_paths); i++)
     212                 :            :     {
     213                 :          5 :       gboolean equal = dup_equals (dup_paths[i].use_uri, dup_paths[i].path1);
     214                 :          5 :       g_assert (equal == TRUE);
     215                 :            :     }
     216                 :          1 : }
     217                 :            : 
     218                 :            : 
     219                 :            : 
     220                 :            : static gboolean
     221                 :          5 : parse_check_utf8 (const gboolean use_uri, const char *path, const char *result_parse_name)
     222                 :            : {
     223                 :          5 :   GFile *file1 = NULL;
     224                 :          5 :   GFile *file2 = NULL;
     225                 :            :   char *parsed_name;
     226                 :            :   gboolean is_utf8_valid;
     227                 :            :   gboolean equal;
     228                 :            :   
     229         [ +  + ]:          5 :   if (use_uri)
     230                 :          2 :     file1 = g_file_new_for_uri (path);
     231                 :            :   else
     232                 :          3 :     file1 = g_file_new_for_path (path);
     233                 :            :         
     234                 :          5 :   g_assert (file1 != NULL);
     235                 :            : 
     236                 :          5 :   parsed_name = g_file_get_parse_name (file1);
     237                 :            :   
     238                 :          5 :   g_assert (parsed_name != NULL);
     239                 :            :   
     240                 :            :   /* UTF-8 validation */
     241                 :          5 :   is_utf8_valid = g_utf8_validate (parsed_name, -1, NULL);
     242                 :          5 :   g_assert (is_utf8_valid == TRUE);
     243                 :            : 
     244         [ +  + ]:          5 :   if (result_parse_name)
     245                 :          3 :     g_assert_cmpstr (parsed_name, ==, result_parse_name);
     246                 :            :   
     247                 :          5 :   file2 = g_file_parse_name (parsed_name);
     248                 :            :   
     249                 :          5 :   g_assert (file2 != NULL);
     250                 :            : 
     251                 :          5 :   equal = g_file_equal (file1, file2);
     252                 :            :         
     253                 :          5 :   g_object_unref (file1);
     254                 :          5 :   g_object_unref (file2);
     255                 :            :   
     256                 :          5 :   g_free (parsed_name);
     257                 :            :   
     258                 :          5 :   return equal;
     259                 :            : }
     260                 :            : 
     261                 :            : static void
     262                 :          1 : test_g_file_get_parse_name_utf8 (void)
     263                 :            : {
     264                 :          1 :   const struct TestPathsWithOper strings[] =
     265                 :            :     {
     266                 :            :       {G_DIR_SEPARATOR_S, 0, FALSE, G_DIR_SEPARATOR_S, NULL },
     267                 :            :       {"file:///", 0, TRUE, G_DIR_SEPARATOR_S, NULL },
     268                 :            :       {"totalnonsense", 0, FALSE, NULL, NULL },
     269                 :            :       {"/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", 0, FALSE, NULL, NULL  /* Depends on local file encoding */},
     270                 :            :       {"file:///invalid%08/UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88/", 0, TRUE, "file:///invalid%08/UTF-8%20p\xc5\x99\xc3\xadli\xc5\xa1%20\xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd%20k\xc5\xaf\xc5\x88", NULL },
     271                 :            :     };
     272                 :            : 
     273                 :            :   guint i;
     274         [ +  + ]:          6 :   for (i = 0; i < G_N_ELEMENTS (strings); i++)
     275                 :            :     {
     276                 :          5 :       gboolean equal = parse_check_utf8 (strings[i].use_uri, strings[i].path1, strings[i].path2);
     277                 :          5 :       g_assert (equal == TRUE);
     278                 :            :     }
     279                 :          1 : }
     280                 :            : 
     281                 :            : static char *
     282                 :          5 : resolve_arg (const gboolean is_uri_only, const char *arg)
     283                 :            : {
     284                 :          5 :   GFile *file1 = NULL;
     285                 :          5 :   char *uri = NULL;
     286                 :          5 :   char *path = NULL;
     287                 :          5 :   char *s = NULL;
     288                 :            :   
     289                 :          5 :   file1 = g_file_new_for_commandline_arg (arg);
     290                 :          5 :   g_assert (file1 != NULL);
     291                 :            :         
     292                 :            :   /*  Test if we get URI string */
     293                 :          5 :   uri = g_file_get_uri (file1);
     294                 :          5 :   g_assert_cmpstr (uri, !=, NULL);
     295                 :          5 :   g_printerr ("%s\n",uri);
     296                 :            :         
     297                 :            :   /*  Test if we get correct value of the local path */
     298                 :          5 :   path = g_file_get_path (file1);
     299         [ -  + ]:          5 :   if (is_uri_only) 
     300                 :          0 :     g_assert_cmpstr (path, ==, NULL);
     301                 :            :   else
     302                 :          5 :     g_assert (g_path_is_absolute (path) == TRUE);
     303                 :            : 
     304                 :            :   /*  Get the URI scheme and compare it with expected one */
     305                 :          5 :   s = g_file_get_uri_scheme (file1);
     306                 :            :         
     307                 :          5 :   g_object_unref (file1);
     308                 :          5 :   g_free (uri);
     309                 :          5 :   g_free (path);
     310                 :            :   
     311                 :          5 :   return s;
     312                 :            : }
     313                 :            : 
     314                 :            : static void
     315                 :          1 : test_g_file_new_for_commandline_arg (void)
     316                 :            : {
     317                 :            :   /*  TestPathsWithOper.use_uri represents IsURIOnly here */
     318                 :          1 :   const struct TestPathsWithOper arg_data[] =
     319                 :            :     {
     320                 :            :       {"./", 0, FALSE, "file", NULL },
     321                 :            :       {"../", 0, FALSE, "file", NULL },
     322                 :            :       {"/tmp", 0, FALSE, "file", NULL },
     323                 :            :       {"//UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", 0, FALSE, "file", NULL },
     324                 :            :       {"file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88/", 0, FALSE, "file", NULL },
     325                 :            : #if 0
     326                 :            :       {"http://www.gtk.org/", 0, TRUE, "http", NULL },
     327                 :            :       {"ftp://user:pass@ftp.gimp.org/", 0, TRUE, "ftp", NULL },
     328                 :            : #endif
     329                 :            :     };
     330                 :            :   GFile *file;
     331                 :            :   char *resolved;
     332                 :            :   char *cwd;
     333                 :            :   guint i;
     334                 :            :   
     335         [ +  + ]:          6 :   for (i = 0; i < G_N_ELEMENTS (arg_data); i++)
     336                 :            :     {
     337                 :          5 :       char *s = resolve_arg (arg_data[i].use_uri, arg_data[i].path1);
     338                 :          5 :       g_assert_cmpstr (s, ==, arg_data[i].path2);
     339                 :          5 :       g_free (s);
     340                 :            :     }
     341                 :            :   
     342                 :            :   /* Manual test for getting correct cwd */
     343                 :          1 :   file = g_file_new_for_commandline_arg ("./");
     344                 :          1 :   resolved = g_file_get_path (file);
     345                 :          1 :   cwd = g_get_current_dir ();
     346                 :          1 :   g_assert_cmpstr (resolved, ==, cwd);
     347                 :          1 :   g_object_unref (file);
     348                 :          1 :   g_free (resolved);
     349                 :          1 :   g_free (cwd);
     350                 :          1 : }
     351                 :            : 
     352                 :            : static char*
     353                 :         16 : get_relative_path (const gboolean use_uri, const gboolean should_have_prefix, const char *dir1, const char *dir2)
     354                 :            : {
     355                 :         16 :   GFile *file1 = NULL;
     356                 :         16 :   GFile *file2 = NULL;
     357                 :         16 :   GFile *file3 = NULL;
     358                 :         16 :   gboolean has_prefix = FALSE;
     359                 :         16 :   char *relative_path = NULL;
     360                 :            :   
     361         [ +  + ]:         16 :   if (use_uri)
     362                 :            :     {
     363                 :          8 :       file1 = g_file_new_for_uri (dir1);
     364                 :          8 :       file2 = g_file_new_for_uri (dir2);
     365                 :            :     }
     366                 :            :   else
     367                 :            :     {
     368                 :          8 :       file1 = g_file_new_for_path (dir1);
     369                 :          8 :       file2 = g_file_new_for_path (dir2);
     370                 :            :     }
     371                 :            :   
     372                 :         16 :   g_assert (file1 != NULL);
     373                 :         16 :   g_assert (file2 != NULL);
     374                 :            :   
     375                 :         16 :   has_prefix = g_file_has_prefix (file2, file1);
     376                 :         16 :   g_printerr ("%s %s\n", dir1, dir2);
     377                 :         16 :   g_assert (has_prefix == should_have_prefix);
     378                 :            : 
     379                 :         16 :   relative_path = g_file_get_relative_path (file1, file2);
     380         [ +  + ]:         16 :   if (should_have_prefix)
     381                 :            :     {
     382                 :         12 :       g_assert (relative_path != NULL);
     383                 :            :       
     384                 :         12 :       file3 = g_file_resolve_relative_path (file1, relative_path);
     385                 :         12 :       g_assert (g_file_equal (file2, file3) == TRUE);
     386                 :            :     }
     387                 :            :         
     388         [ +  - ]:         16 :   if (file1)
     389                 :         16 :     g_object_unref (file1);
     390         [ +  - ]:         16 :   if (file2)
     391                 :         16 :     g_object_unref (file2);
     392         [ +  + ]:         16 :   if (file3)
     393                 :         12 :     g_object_unref (file3);
     394                 :            : 
     395                 :         16 :   return relative_path;
     396                 :            : }
     397                 :            : 
     398                 :            : static void
     399                 :          1 : test_g_file_has_prefix (void)
     400                 :            : {
     401                 :            :   /*  TestPathsWithOper.equal represents here if the dir belongs to the directory structure  */
     402                 :          1 :   const struct TestPathsWithOper dirs[] =
     403                 :            :     {
     404                 :            :       /* path1            equal  uri     path2    path3  */
     405                 :            :       {"/dir1", TRUE, FALSE, "/dir1/dir2/dir3/", "dir2" G_DIR_SEPARATOR_S "dir3"},
     406                 :            :       {"/dir1/", TRUE, FALSE, "/dir1/dir2/dir3/", "dir2" G_DIR_SEPARATOR_S "dir3"},
     407                 :            :       {"/dir1", TRUE, FALSE, "/dir1/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
     408                 :            :       {"/dir1/", TRUE, FALSE, "/dir1/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
     409                 :            :       {"/tmp/", FALSE, FALSE, "/something/", NULL},
     410                 :            :       {"/dir1/dir2", FALSE, FALSE, "/dir1/", NULL},
     411                 :            :       {"//dir1/new", TRUE, FALSE, "//dir1/new/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
     412                 :            :       {"/dir/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", TRUE, FALSE, "/dir/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88/dir2", "dir2"},
     413                 :            :       {"file:///dir1", TRUE, TRUE, "file:///dir1/dir2/dir3/", "dir2" G_DIR_SEPARATOR_S "dir3"},
     414                 :            :       {"file:///dir1/", TRUE, TRUE, "file:///dir1/dir2/dir3/", "dir2" G_DIR_SEPARATOR_S "dir3"},
     415                 :            :       {"file:///dir1", TRUE, TRUE, "file:///dir1/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
     416                 :            :       {"file:///dir1/", TRUE, TRUE, "file:///dir1/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
     417                 :            :       {"file:///tmp/", FALSE, TRUE, "file:///something/", NULL},
     418                 :            :       {"file:///dir1/dir2", FALSE, TRUE, "file:///dir1/", NULL},
     419                 :            :       {"file:////dir1/new", TRUE, TRUE, "file:////dir1/new/dir2/dir3", "dir2" G_DIR_SEPARATOR_S "dir3"},
     420                 :            :       {"file:///dir/UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88", TRUE, TRUE, "file:///dir/UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88/dir2", "dir2"},
     421                 :            : #if 0
     422                 :            :       {"dav://www.gtk.org/plan/", TRUE, TRUE, "dav://www.gtk.org/plan/meetings/20071218.txt", "meetings/20071218.txt"},
     423                 :            :       {"dav://www.gtk.org/plan/meetings", TRUE, TRUE, "dav://www.gtk.org/plan/meetings/20071218.txt", "20071218.txt"},
     424                 :            : #endif
     425                 :            :     };
     426                 :            :   
     427                 :            :   guint i;
     428         [ +  + ]:         17 :   for (i = 0; i < G_N_ELEMENTS (dirs); i++)
     429                 :            :     {
     430                 :         16 :       char *s = get_relative_path (dirs[i].use_uri, dirs[i].equal, dirs[i].path1, dirs[i].path2);
     431         [ +  + ]:         16 :       if (dirs[i].equal) 
     432                 :         12 :         g_assert_cmpstr (s, ==, dirs[i].path3);
     433                 :         16 :       g_free (s);
     434                 :            :     }
     435                 :          1 : }
     436                 :            : 
     437                 :            : static void
     438                 :          9 : roundtrip_parent_child (const gboolean use_uri, const gboolean under_root_descending,
     439                 :            :                         const char *path, const char *dir_holder)
     440                 :            : {
     441                 :          9 :   GFile *files[6] = {NULL};
     442                 :            :   guint i;
     443                 :            :   
     444         [ +  + ]:          9 :   if (use_uri)
     445                 :            :     {
     446                 :          5 :       files[0] = g_file_new_for_uri (path);
     447                 :          5 :       files[1] = g_file_new_for_uri (path);
     448                 :            :     }
     449                 :            :   else
     450                 :            :     {
     451                 :          4 :       files[0] = g_file_new_for_path (path);
     452                 :          4 :       files[1] = g_file_new_for_path (path);
     453                 :            :     }
     454                 :            : 
     455                 :          9 :   g_assert (files[0] != NULL);
     456                 :          9 :   g_assert (files[1] != NULL);
     457                 :            :   
     458                 :          9 :   files[2] = g_file_get_child (files[1], dir_holder); 
     459                 :          9 :   g_assert (files[2] != NULL);
     460                 :            :   
     461                 :          9 :   files[3] = g_file_get_parent (files[2]);
     462                 :          9 :   g_assert (files[3] != NULL);
     463                 :          9 :   g_assert (g_file_equal (files[3], files[0]) == TRUE);
     464                 :            :   
     465                 :          9 :   files[4] = g_file_get_parent (files[3]);
     466                 :            :   /*  Don't go lower beyond the root */
     467         [ +  + ]:          9 :   if (under_root_descending) 
     468                 :          2 :     g_assert (files[4] == NULL);
     469                 :            :   else
     470                 :            :     {
     471                 :          7 :       g_assert (files[4] != NULL);
     472                 :            :       
     473                 :          7 :       files[5] = g_file_get_child (files[4], dir_holder); 
     474                 :          7 :       g_assert (files[5] != NULL);
     475                 :          7 :       g_assert (g_file_equal (files[5], files[0]) == TRUE);
     476                 :            :     }
     477                 :            :   
     478         [ +  + ]:         63 :   for (i = 0; i < G_N_ELEMENTS (files); i++)
     479                 :            :     {
     480         [ +  + ]:         54 :       if (files[i])
     481                 :         50 :         g_object_unref (files[i]);
     482                 :            :     }
     483                 :          9 : }
     484                 :            : 
     485                 :            : static void
     486                 :          1 : test_g_file_get_parent_child (void)
     487                 :            : {
     488                 :          1 :   const struct TestPathsWithOper paths[] =
     489                 :            :     {
     490                 :            :       /* path     root_desc   uri  dir_holder */
     491                 :            :       {"/dir1/dir", FALSE, FALSE, "dir", NULL },
     492                 :            :       {"/dir", FALSE, FALSE, "dir", NULL },
     493                 :            :       {"/", TRUE, FALSE, "dir", NULL },
     494                 :            :       {"/UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88/", FALSE, FALSE, "UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", NULL },
     495                 :            :       {"file:///dir1/dir", FALSE, TRUE, "dir", NULL },
     496                 :            :       {"file:///dir", FALSE, TRUE, "dir", NULL },
     497                 :            :       {"file:///", TRUE, TRUE, "dir", NULL },
     498                 :            :       {"file:///UTF-8%20p%C5%99%C3%ADli%C5%A1%20%C5%BElu%C5%A5ou%C4%8Dk%C3%BD%20k%C5%AF%C5%88/", FALSE, TRUE, "UTF-8 p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88", NULL },
     499                 :            :       {"dav://www.gtk.org/plan/meetings", FALSE, TRUE, "meetings", NULL },
     500                 :            :     };
     501                 :            : 
     502                 :            :   guint i;
     503         [ +  + ]:         10 :   for (i = 0; i < G_N_ELEMENTS (paths); i++)
     504                 :          9 :     roundtrip_parent_child (paths[i].use_uri, paths[i].equal, paths[i].path1, paths[i].path2);
     505                 :          1 : }
     506                 :            : 
     507                 :            : int
     508                 :          1 : main (int   argc,
     509                 :            :       char *argv[])
     510                 :            : {
     511                 :          1 :   g_test_init (&argc, &argv, NULL);
     512                 :            :   
     513                 :            :   
     514                 :            :   /*  Testing whether g_file_new_for_path() or g_file_new_for_uri() always returns non-NULL result  */
     515                 :          1 :   g_test_add_func ("/g-file/test_g_file_new_null", test_g_file_new_null);
     516                 :            :   
     517                 :            :   /*  Testing whether the g_file_new_for_path() correctly canonicalizes strings and two files equals (g_file_equal()) */
     518                 :          1 :   g_test_add_func ("/g-file/test_g_file_new_for_path", test_g_file_new_for_path);
     519                 :            : 
     520                 :            :   /*  Testing whether the g_file_new_for_uri() correctly canonicalizes strings and two files equals (g_file_equal()) */
     521                 :          1 :   g_test_add_func ("/g-file/test_g_file_new_for_uri", test_g_file_new_for_uri);
     522                 :            : 
     523                 :            :   /*  Testing g_file_dup() equals original file via g_file_equal()  */
     524                 :          1 :   g_test_add_func ("/g-file/test_g_file_dup", test_g_file_dup);
     525                 :            : 
     526                 :            :   /*  Testing g_file_get_parse_name() to return correct UTF-8 string    */
     527                 :          1 :   g_test_add_func ("/g-file/test_g_file_get_parse_name_utf8", test_g_file_get_parse_name_utf8);
     528                 :            :   
     529                 :            :   /*  Testing g_file_new_for_commandline_arg() for correct relavive path resolution and correct path/URI guess   */
     530                 :          1 :   g_test_add_func ("/g-file/test_g_file_new_for_commandline_arg", test_g_file_new_for_commandline_arg);
     531                 :            :   
     532                 :            :   /*  Testing g_file_has_prefix(), g_file_get_relative_path() and g_file_resolve_relative_path() to return and process correct relative paths         */
     533                 :          1 :   g_test_add_func ("/g-file/test_g_file_has_prefix", test_g_file_has_prefix);
     534                 :            :   
     535                 :            :   /*  Testing g_file_get_parent() and g_file_get_child()            */
     536                 :          1 :   g_test_add_func ("/g-file/test_g_file_get_parent_child", test_g_file_get_parent_child);
     537                 :            :   
     538                 :          1 :   return g_test_run();
     539                 :            : }

Generated by: LCOV version 1.14