LCOV - code coverage report
Current view: top level - glib/gio/tests - vfs.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 62 62 100.0 %
Date: 2024-04-23 05:16:05 Functions: 6 6 100.0 %
Branches: 1 2 50.0 %

           Branch data     Line data    Source code
       1                 :            : 
       2                 :            : /* Unit tests for GVfs
       3                 :            :  * Copyright (C) 2011 Red Hat, Inc
       4                 :            :  * Author: Matthias Clasen
       5                 :            :  *
       6                 :            :  * SPDX-License-Identifier: LicenseRef-old-glib-tests
       7                 :            :  *
       8                 :            :  * This work is provided "as is"; redistribution and modification
       9                 :            :  * in whole or in part, in any medium, physical or electronic is
      10                 :            :  * permitted without restriction.
      11                 :            :  *
      12                 :            :  * This work is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
      15                 :            :  *
      16                 :            :  * In no event shall the authors or contributors be liable for any
      17                 :            :  * direct, indirect, incidental, special, exemplary, or consequential
      18                 :            :  * damages (including, but not limited to, procurement of substitute
      19                 :            :  * goods or services; loss of use, data, or profits; or business
      20                 :            :  * interruption) however caused and on any theory of liability, whether
      21                 :            :  * in contract, strict liability, or tort (including negligence or
      22                 :            :  * otherwise) arising in any way out of the use of this software, even
      23                 :            :  * if advised of the possibility of such damage.
      24                 :            :  */
      25                 :            : 
      26                 :            : #include <gio/gio.h>
      27                 :            : 
      28                 :            : static GFile *
      29                 :          1 : test_vfs_parse_name (GVfs       *vfs,
      30                 :            :                      const char *parse_name,
      31                 :            :                      gpointer    user_data)
      32                 :            : {
      33                 :          1 :   GFile *file = NULL;
      34                 :            : 
      35         [ +  - ]:          1 :   if (g_strcmp0 ((parse_name), "testfile") == 0)
      36                 :            :     {
      37                 :          1 :       file = g_file_new_for_uri ("file:///");
      38                 :          1 :       g_object_set_data (G_OBJECT (file), "testfile", GINT_TO_POINTER (1));
      39                 :            :     }
      40                 :            : 
      41                 :          1 :   return file;
      42                 :            : }
      43                 :            : 
      44                 :            : static GFile *
      45                 :          1 : test_vfs_lookup (GVfs       *vfs,
      46                 :            :                  const char *uri,
      47                 :            :                  gpointer    user_data)
      48                 :            : {
      49                 :            :   GFile *file;
      50                 :          1 :   file = g_file_new_for_uri ("file:///");
      51                 :          1 :   g_object_set_data (G_OBJECT (file), "testfile", GINT_TO_POINTER (1));
      52                 :            : 
      53                 :          1 :   return file;
      54                 :            : }
      55                 :            : 
      56                 :            : static void
      57                 :          1 : test_register_scheme (void)
      58                 :            : {
      59                 :            :   GVfs *vfs;
      60                 :            :   GFile *file;
      61                 :            :   const gchar * const *schemes;
      62                 :            :   gboolean res;
      63                 :            : 
      64                 :          1 :   vfs = g_vfs_get_default ();
      65                 :          1 :   g_assert_nonnull (vfs);
      66                 :          1 :   g_assert_true (g_vfs_is_active (vfs));
      67                 :            : 
      68                 :          1 :   schemes = g_vfs_get_supported_uri_schemes (vfs);
      69                 :          1 :   g_assert_false (g_strv_contains (schemes, "test"));
      70                 :            : 
      71                 :          1 :   res = g_vfs_unregister_uri_scheme (vfs, "test");
      72                 :          1 :   g_assert_false (res);
      73                 :            : 
      74                 :          1 :   res = g_vfs_register_uri_scheme (vfs, "test",
      75                 :            :                                    test_vfs_lookup, NULL, NULL,
      76                 :            :                                    test_vfs_parse_name, NULL, NULL);
      77                 :          1 :   g_assert_true (res);
      78                 :            : 
      79                 :          1 :   schemes = g_vfs_get_supported_uri_schemes (vfs);
      80                 :          1 :   g_assert_true (g_strv_contains (schemes, "test"));
      81                 :            : 
      82                 :          1 :   file = g_file_new_for_uri ("test:///foo");
      83                 :          1 :   g_assert_cmpint (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (file), "testfile")), ==, 1);
      84                 :          1 :   g_object_unref (file);
      85                 :            : 
      86                 :          1 :   file = g_file_parse_name ("testfile");
      87                 :          1 :   g_assert_cmpint (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (file), "testfile")), ==, 1);
      88                 :          1 :   g_object_unref (file);
      89                 :            : 
      90                 :          1 :   res = g_vfs_register_uri_scheme (vfs, "test",
      91                 :            :                                    test_vfs_lookup, NULL, NULL,
      92                 :            :                                    test_vfs_parse_name, NULL, NULL);
      93                 :          1 :   g_assert_false (res);
      94                 :            : 
      95                 :          1 :   res = g_vfs_unregister_uri_scheme (vfs, "test");
      96                 :          1 :   g_assert_true (res);
      97                 :            : 
      98                 :          1 :   file = g_file_new_for_uri ("test:///foo");
      99                 :          1 :   g_assert_null (g_object_get_data (G_OBJECT (file), "testfile"));
     100                 :          1 :   g_object_unref (file);
     101                 :          1 : }
     102                 :            : 
     103                 :            : static void
     104                 :          1 : test_local (void)
     105                 :            : {
     106                 :            :   GVfs *vfs;
     107                 :            :   GFile *file;
     108                 :            :   gchar **schemes;
     109                 :            : 
     110                 :          1 :   vfs = g_vfs_get_local ();
     111                 :          1 :   g_assert_true (g_vfs_is_active (vfs));
     112                 :            : 
     113                 :          1 :   file = g_vfs_get_file_for_uri (vfs, "not a good uri");
     114                 :          1 :   g_assert_true (G_IS_FILE (file));
     115                 :          1 :   g_object_unref (file);
     116                 :            : 
     117                 :          1 :   schemes = (gchar **)g_vfs_get_supported_uri_schemes (vfs);
     118                 :            : 
     119                 :          1 :   g_assert_cmpuint (g_strv_length (schemes), >, 0);
     120                 :          1 :   g_assert_cmpstr (schemes[0], ==, "file");
     121                 :          1 : }
     122                 :            : 
     123                 :            : static void
     124                 :          1 : test_resource_malformed_escaping (void)
     125                 :            : {
     126                 :            :   GVfs *vfs;
     127                 :            :   GFile *file;
     128                 :            : 
     129                 :          1 :   g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/3090");
     130                 :          1 :   g_test_summary ("Test that g_vfs_get_file_for_uri() returns an invalid file for an invalid URI");
     131                 :            : 
     132                 :          1 :   vfs = g_vfs_get_local ();
     133                 :          1 :   g_assert_true (g_vfs_is_active (vfs));
     134                 :            : 
     135                 :          1 :   file = g_vfs_get_file_for_uri (vfs, "resource:///%not-valid-escaping/gtk.css");
     136                 :          1 :   g_assert_true (G_IS_FILE (file));
     137                 :            : 
     138                 :            :   /* This only returns %NULL if the file was constructed with an invalid URI: */
     139                 :          1 :   g_assert_null (g_file_get_uri_scheme (file));
     140                 :            : 
     141                 :          1 :   g_object_unref (file);
     142                 :          1 : }
     143                 :            : 
     144                 :            : int
     145                 :          1 : main (int argc, char *argv[])
     146                 :            : {
     147                 :          1 :   g_test_init (&argc, &argv, NULL);
     148                 :            : 
     149                 :          1 :   g_test_add_func ("/gvfs/local", test_local);
     150                 :          1 :   g_test_add_func ("/gvfs/register-scheme", test_register_scheme);
     151                 :          1 :   g_test_add_func ("/gvfs/resource/malformed-escaping", test_resource_malformed_escaping);
     152                 :            : 
     153                 :          1 :   return g_test_run ();
     154                 :            : }

Generated by: LCOV version 1.14