LCOV - code coverage report
Current view: top level - glib/gio/tests - g-file-info-filesystem-readonly.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 27 133 20.3 %
Date: 2024-04-23 05:16:05 Functions: 3 5 60.0 %
Branches: 6 34 17.6 %

           Branch data     Line data    Source code
       1                 :            : /* Testcase for bug in GIO function g_file_query_filesystem_info()
       2                 :            :  * Author: Nelson Benítez León
       3                 :            :  *
       4                 :            :  * SPDX-License-Identifier: LicenseRef-old-glib-tests
       5                 :            :  *
       6                 :            :  * This work is provided "as is"; redistribution and modification
       7                 :            :  * in whole or in part, in any medium, physical or electronic is
       8                 :            :  * permitted without restriction.
       9                 :            :  *
      10                 :            :  * This work is distributed in the hope that it will be useful,
      11                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      12                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
      13                 :            :  *
      14                 :            :  * In no event shall the authors or contributors be liable for any
      15                 :            :  * direct, indirect, incidental, special, exemplary, or consequential
      16                 :            :  * damages (including, but not limited to, procurement of substitute
      17                 :            :  * goods or services; loss of use, data, or profits; or business
      18                 :            :  * interruption) however caused and on any theory of liability, whether
      19                 :            :  * in contract, strict liability, or tort (including negligence or
      20                 :            :  * otherwise) arising in any way out of the use of this software, even
      21                 :            :  * if advised of the possibility of such damage.
      22                 :            :  */
      23                 :            : 
      24                 :            : #include <errno.h>
      25                 :            : #include <glib.h>
      26                 :            : #include <glib/gstdio.h>
      27                 :            : #include <gio/gio.h>
      28                 :            : #include <gio/gunixmounts.h>
      29                 :            : 
      30                 :            : static gboolean
      31                 :          0 : run (GError **error,
      32                 :            :      const gchar *argv0,
      33                 :            :      ...)
      34                 :            : {
      35                 :            :   GPtrArray *args;
      36                 :            :   const gchar *arg;
      37                 :            :   va_list ap;
      38                 :            :   GSubprocess *subprocess;
      39                 :          0 :   gchar *command_line = NULL;
      40                 :            :   gboolean success;
      41                 :            : 
      42                 :          0 :   args = g_ptr_array_new ();
      43                 :            : 
      44                 :          0 :   va_start (ap, argv0);
      45                 :          0 :   g_ptr_array_add (args, (gchar *) argv0);
      46         [ #  # ]:          0 :   while ((arg = va_arg (ap, const gchar *)))
      47                 :          0 :     g_ptr_array_add (args, (gchar *) arg);
      48                 :          0 :   g_ptr_array_add (args, NULL);
      49                 :          0 :   va_end (ap);
      50                 :            : 
      51                 :          0 :   command_line = g_strjoinv (" ", (gchar **) args->pdata);
      52                 :          0 :   g_test_message ("Running command `%s`", command_line);
      53                 :          0 :   g_free (command_line);
      54                 :            : 
      55                 :          0 :   subprocess = g_subprocess_newv ((const gchar * const *) args->pdata, G_SUBPROCESS_FLAGS_NONE, error);
      56                 :          0 :   g_ptr_array_free (args, TRUE);
      57                 :            : 
      58         [ #  # ]:          0 :   if (subprocess == NULL)
      59                 :          0 :     return FALSE;
      60                 :            : 
      61                 :          0 :   success = g_subprocess_wait_check (subprocess, NULL, error);
      62                 :          0 :   g_object_unref (subprocess);
      63                 :            : 
      64                 :          0 :   return success;
      65                 :            : }
      66                 :            : 
      67                 :            : static void
      68                 :          0 : assert_remove (const gchar *file)
      69                 :            : {
      70         [ #  # ]:          0 :   if (g_remove (file) != 0)
      71                 :          0 :     g_error ("failed to remove %s: %s", file, g_strerror (errno));
      72                 :          0 : }
      73                 :            : 
      74                 :            : static gboolean
      75                 :          2 : fuse_module_loaded (void)
      76                 :            : {
      77                 :          2 :   char *contents = NULL;
      78                 :            :   gboolean ret;
      79                 :            : 
      80         [ +  - ]:          2 :   if (!g_file_get_contents ("/proc/modules", &contents, NULL, NULL) ||
      81         [ -  + ]:          2 :       contents == NULL)
      82                 :            :     {
      83                 :          0 :       g_free (contents);
      84                 :          0 :       return FALSE;
      85                 :            :     }
      86                 :            : 
      87                 :          2 :   ret = (strstr (contents, "\nfuse ") != NULL);
      88                 :          2 :   g_free (contents);
      89                 :          2 :   return ret;
      90                 :            : }
      91                 :            : 
      92                 :            : static void
      93                 :          2 : test_filesystem_readonly (gconstpointer with_mount_monitor)
      94                 :            : {
      95                 :            :   GFileInfo *file_info;
      96                 :            :   GFile *mounted_file;
      97                 :          2 :   GUnixMountMonitor *mount_monitor = NULL;
      98                 :            :   gchar *bindfs, *fusermount;
      99                 :            :   gchar *curdir, *dir_to_mount, *dir_mountpoint;
     100                 :            :   gchar *file_in_mount, *file_in_mountpoint;
     101                 :          2 :   GError *error = NULL;
     102                 :            : 
     103                 :            :   /* installed by package 'bindfs' in Fedora */
     104                 :          2 :   bindfs = g_find_program_in_path ("bindfs");
     105                 :            : 
     106                 :            :   /* installed by package 'fuse' in Fedora */
     107                 :          2 :   fusermount = g_find_program_in_path ("fusermount");
     108                 :            : 
     109   [ +  -  -  + ]:          2 :   if (bindfs == NULL || fusermount == NULL)
     110                 :            :     {
     111                 :            :       /* We need these because "mount --bind" requires root privileges */
     112                 :          0 :       g_test_skip ("'bindfs' and 'fusermount' commands are needed to run this test");
     113                 :          0 :       g_free (fusermount);
     114                 :          0 :       g_free (bindfs);
     115                 :          2 :       return;
     116                 :            :     }
     117                 :            : 
     118                 :            :   /* If the fuse module is loaded but there's no /dev/fuse, then we're
     119                 :            :    * we're probably in a rootless container and won't be able to
     120                 :            :    * use bindfs to run our tests */
     121   [ +  -  +  - ]:          4 :   if (fuse_module_loaded () &&
     122                 :          2 :       !g_file_test ("/dev/fuse", G_FILE_TEST_EXISTS))
     123                 :            :     {
     124                 :          2 :       g_test_skip ("fuse support is needed to run this test (rootless container?)");
     125                 :          2 :       g_free (fusermount);
     126                 :          2 :       g_free (bindfs);
     127                 :          2 :       return;
     128                 :            :     }
     129                 :            : 
     130                 :          0 :   curdir = g_get_current_dir ();
     131                 :          0 :   dir_to_mount = g_strdup_printf ("%s/dir_bindfs_to_mount", curdir);
     132                 :          0 :   file_in_mount = g_strdup_printf ("%s/example.txt", dir_to_mount);
     133                 :          0 :   dir_mountpoint = g_strdup_printf ("%s/dir_bindfs_mountpoint", curdir);
     134                 :            : 
     135                 :          0 :   g_mkdir (dir_to_mount, 0777);
     136                 :          0 :   g_mkdir (dir_mountpoint, 0777);
     137         [ #  # ]:          0 :   if (! g_file_set_contents (file_in_mount, "Example", -1, NULL))
     138                 :            :     {
     139                 :          0 :       g_test_skip ("Failed to create file needed to proceed further with the test");
     140                 :            : 
     141                 :          0 :       g_free (dir_mountpoint);
     142                 :          0 :       g_free (file_in_mount);
     143                 :          0 :       g_free (dir_to_mount);
     144                 :          0 :       g_free (curdir);
     145                 :          0 :       g_free (fusermount);
     146                 :          0 :       g_free (bindfs);
     147                 :          0 :       return;
     148                 :            :     }
     149                 :            : 
     150         [ #  # ]:          0 :   if (with_mount_monitor)
     151                 :          0 :     mount_monitor = g_unix_mount_monitor_get ();
     152                 :            : 
     153                 :            :   /* Use bindfs, which does not need root privileges, to mount the contents of one dir
     154                 :            :    * into another dir (and do the mount as readonly as per passed '-o ro' option) */
     155         [ #  # ]:          0 :   if (!run (&error, bindfs, "-n", "-o", "ro", dir_to_mount, dir_mountpoint, NULL))
     156                 :            :     {
     157                 :          0 :       gchar *skip_message = g_strdup_printf ("Failed to run bindfs to set up test: %s", error->message);
     158                 :          0 :       g_test_skip (skip_message);
     159                 :            : 
     160                 :          0 :       g_free (skip_message);
     161                 :          0 :       g_clear_error (&error);
     162                 :            : 
     163                 :          0 :       g_clear_object (&mount_monitor);
     164                 :          0 :       g_free (dir_mountpoint);
     165                 :          0 :       g_free (file_in_mount);
     166                 :          0 :       g_free (dir_to_mount);
     167                 :          0 :       g_free (curdir);
     168                 :          0 :       g_free (fusermount);
     169                 :          0 :       g_free (bindfs);
     170                 :            : 
     171                 :          0 :       return;
     172                 :            :     }
     173                 :            : 
     174                 :            :   /* Let's check now, that the file is in indeed in a readonly filesystem */
     175                 :          0 :   file_in_mountpoint = g_strdup_printf ("%s/example.txt", dir_mountpoint);
     176                 :          0 :   mounted_file = g_file_new_for_path (file_in_mountpoint);
     177                 :            : 
     178         [ #  # ]:          0 :   if (with_mount_monitor)
     179                 :            :     {
     180                 :            :      /* Let UnixMountMonitor process its 'mounts-changed'
     181                 :            :       * signal triggered by mount operation above */
     182         [ #  # ]:          0 :       while (g_main_context_iteration (NULL, FALSE));
     183                 :            :     }
     184                 :            : 
     185                 :          0 :   file_info = g_file_query_filesystem_info (mounted_file,
     186                 :            :                                             G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, NULL, &error);
     187                 :          0 :   g_assert_no_error (error);
     188                 :          0 :   g_assert_nonnull (file_info);
     189         [ #  # ]:          0 :   if (! g_file_info_get_attribute_boolean (file_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
     190                 :            :     {
     191                 :          0 :       g_test_skip ("Failed to create readonly file needed to proceed further with the test");
     192                 :            : 
     193                 :          0 :       g_clear_object (&file_info);
     194                 :          0 :       g_clear_object (&mounted_file);
     195                 :          0 :       g_free (file_in_mountpoint);
     196                 :          0 :       g_clear_object (&mount_monitor);
     197                 :          0 :       g_free (dir_mountpoint);
     198                 :          0 :       g_free (file_in_mount);
     199                 :          0 :       g_free (dir_to_mount);
     200                 :          0 :       g_free (curdir);
     201                 :          0 :       g_free (fusermount);
     202                 :          0 :       g_free (bindfs);
     203                 :            : 
     204                 :          0 :       return;
     205                 :            :     }
     206                 :            : 
     207                 :            :   /* Now we unmount, and mount again but this time rw (not readonly) */
     208                 :          0 :   run (&error, fusermount, "-z", "-u", dir_mountpoint, NULL);
     209                 :          0 :   g_assert_no_error (error);
     210                 :          0 :   run (&error, bindfs, "-n", dir_to_mount, dir_mountpoint, NULL);
     211                 :          0 :   g_assert_no_error (error);
     212                 :            : 
     213         [ #  # ]:          0 :   if (with_mount_monitor)
     214                 :            :     {
     215                 :            :      /* Let UnixMountMonitor process its 'mounts-changed' signal
     216                 :            :       * triggered by mount/umount operations above */
     217         [ #  # ]:          0 :       while (g_main_context_iteration (NULL, FALSE));
     218                 :            :     }
     219                 :            : 
     220                 :            :   /* Now let's test if GIO will report the new filesystem state */
     221                 :          0 :   g_clear_object (&file_info);
     222                 :          0 :   g_clear_object (&mounted_file);
     223                 :          0 :   mounted_file = g_file_new_for_path (file_in_mountpoint);
     224                 :          0 :   file_info = g_file_query_filesystem_info (mounted_file,
     225                 :            :                                             G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, NULL, &error);
     226                 :          0 :   g_assert_no_error (error);
     227                 :          0 :   g_assert_nonnull (file_info);
     228                 :            : 
     229                 :          0 :   g_assert_false (g_file_info_get_attribute_boolean (file_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY));
     230                 :            : 
     231                 :            :   /* Clean up */
     232                 :          0 :   g_clear_object (&mount_monitor);
     233                 :          0 :   g_clear_object (&file_info);
     234                 :          0 :   g_clear_object (&mounted_file);
     235                 :          0 :   run (&error, fusermount, "-z", "-u", dir_mountpoint, NULL);
     236                 :          0 :   g_assert_no_error (error);
     237                 :            : 
     238                 :          0 :   assert_remove (file_in_mount);
     239                 :          0 :   assert_remove (dir_to_mount);
     240                 :          0 :   assert_remove (dir_mountpoint);
     241                 :            : 
     242                 :          0 :   g_free (bindfs);
     243                 :          0 :   g_free (fusermount);
     244                 :          0 :   g_free (curdir);
     245                 :          0 :   g_free (dir_to_mount);
     246                 :          0 :   g_free (dir_mountpoint);
     247                 :          0 :   g_free (file_in_mount);
     248                 :          0 :   g_free (file_in_mountpoint);
     249                 :            : }
     250                 :            : 
     251                 :            : int
     252                 :          1 : main (int argc, char *argv[])
     253                 :            : {
     254                 :            :   /* To avoid unnecessary D-Bus calls, see http://goo.gl/ir56j2 */
     255                 :          1 :   g_setenv ("GIO_USE_VFS", "local", FALSE);
     256                 :            : 
     257                 :          1 :   g_test_init (&argc, &argv, NULL);
     258                 :            : 
     259                 :          1 :   g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=787731");
     260                 :            : 
     261                 :          1 :   g_test_add_data_func ("/g-file-info-filesystem-readonly/test-fs-ro",
     262                 :            :                         GINT_TO_POINTER (FALSE), test_filesystem_readonly);
     263                 :            : 
     264                 :            :   /* This second test is using a running GUnixMountMonitor, so the calls to:
     265                 :            :    *  g_unix_mount_get(&time_read) - To fill the time_read parameter
     266                 :            :    *  g_unix_mounts_changed_since()
     267                 :            :    *
     268                 :            :    * made from inside g_file_query_filesystem_info() will use the mount_poller_time
     269                 :            :    * from the monitoring of /proc/self/mountinfo , while in the previous test new
     270                 :            :    * created timestamps are returned from those g_unix_mount* functions. */
     271                 :          1 :   g_test_add_data_func ("/g-file-info-filesystem-readonly/test-fs-ro-with-mount-monitor",
     272                 :            :                         GINT_TO_POINTER (TRUE), test_filesystem_readonly);
     273                 :            : 
     274                 :          1 :   return g_test_run ();
     275                 :            : }

Generated by: LCOV version 1.14