ci: collect test coverage and deploy a html report through gitlab pages
[glib.git] / gio / tests / g-file-info-filesystem-readonly.c
blobc2b0aa5186a54878df40cbb84727745abb4f1b62
1 /* Testcase for bug in GIO function g_file_query_filesystem_info()
2 * Author: Nelson Benítez León
4 * This work is provided "as is"; redistribution and modification
5 * in whole or in part, in any medium, physical or electronic is
6 * permitted without restriction.
8 * This work is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * In no event shall the authors or contributors be liable for any
13 * direct, indirect, incidental, special, exemplary, or consequential
14 * damages (including, but not limited to, procurement of substitute
15 * goods or services; loss of use, data, or profits; or business
16 * interruption) however caused and on any theory of liability, whether
17 * in contract, strict liability, or tort (including negligence or
18 * otherwise) arising in any way out of the use of this software, even
19 * if advised of the possibility of such damage.
22 #include <glib.h>
23 #include <glib/gstdio.h>
24 #include <gio/gio.h>
25 #include <gio/gunixmounts.h>
27 static void
28 test_filesystem_readonly (gconstpointer with_mount_monitor)
30 GFileInfo *file_info;
31 GFile *mounted_file;
32 GUnixMountMonitor *mount_monitor = NULL;
33 gchar *bindfs, *fusermount;
34 gchar *command_mount, *command_mount_ro, *command_umount;
35 gchar *curdir, *dir_to_mount, *dir_mountpoint;
36 gchar *file_in_mount, *file_in_mountpoint;
38 /* installed by package 'bindfs' in Fedora */
39 bindfs = g_find_program_in_path ("bindfs");
41 /* installed by package 'fuse' in Fedora */
42 fusermount = g_find_program_in_path ("fusermount");
44 if (bindfs == NULL || fusermount == NULL)
46 /* We need these because "mount --bind" requires root privileges */
47 g_test_skip ("'bindfs' and 'fusermount' commands are needed to run this test");
48 return;
51 curdir = g_get_current_dir ();
52 dir_to_mount = g_strdup_printf ("%s/dir_bindfs_to_mount", curdir);
53 file_in_mount = g_strdup_printf ("%s/example.txt", dir_to_mount);
54 dir_mountpoint = g_strdup_printf ("%s/dir_bindfs_mountpoint", curdir);
56 g_mkdir (dir_to_mount, 0777);
57 g_mkdir (dir_mountpoint, 0777);
58 if (! g_file_set_contents (file_in_mount, "Example", -1, NULL))
60 g_test_skip ("Failed to create file needed to proceed further with the test");
61 return;
64 if (with_mount_monitor)
65 mount_monitor = g_unix_mount_monitor_get ();
67 /* Use bindfs, which does not need root privileges, to mount the contents of one dir
68 * into another dir (and do the mount as readonly as per passed '-o ro' option) */
69 command_mount_ro = g_strdup_printf ("%s -n -o ro '%s' '%s'", bindfs, dir_to_mount, dir_mountpoint);
70 g_spawn_command_line_sync (command_mount_ro, NULL, NULL, NULL, NULL);
72 /* Let's check now, that the file is in indeed in a readonly filesystem */
73 file_in_mountpoint = g_strdup_printf ("%s/example.txt", dir_mountpoint);
74 mounted_file = g_file_new_for_path (file_in_mountpoint);
76 if (with_mount_monitor)
78 /* Let UnixMountMonitor process its 'mounts-changed'
79 * signal triggered by mount operation above */
80 while (g_main_context_iteration (NULL, FALSE));
83 file_info = g_file_query_filesystem_info (mounted_file,
84 G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, NULL, NULL);
85 if (! g_file_info_get_attribute_boolean (file_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
87 g_test_skip ("Failed to create readonly file needed to proceed further with the test");
88 return;
91 /* Now we unmount, and mount again but this time rw (not readonly) */
92 command_umount = g_strdup_printf ("%s -u '%s'", fusermount, dir_mountpoint);
93 g_spawn_command_line_sync (command_umount, NULL, NULL, NULL, NULL);
94 command_mount = g_strdup_printf ("%s -n '%s' '%s'", bindfs, dir_to_mount, dir_mountpoint);
95 g_spawn_command_line_sync (command_mount, NULL, NULL, NULL, NULL);
97 if (with_mount_monitor)
99 /* Let UnixMountMonitor process its 'mounts-changed' signal
100 * triggered by mount/umount operations above */
101 while (g_main_context_iteration (NULL, FALSE));
104 /* Now let's test if GIO will report the new filesystem state */
105 g_clear_object (&file_info);
106 g_clear_object (&mounted_file);
107 mounted_file = g_file_new_for_path (file_in_mountpoint);
108 file_info = g_file_query_filesystem_info (mounted_file,
109 G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, NULL, NULL);
111 if (g_file_info_get_attribute_boolean (file_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
113 /* ¡¡ GIO still reports filesystem as being Readonly !!
114 * Let's check if that's true by trying to write to file */
115 GFileOutputStream *write_stream;
116 write_stream = g_file_append_to (mounted_file, G_FILE_CREATE_NONE, NULL, NULL);
117 if (write_stream != NULL)
119 /* The file has been opened for writing without error, so ¡¡ GIO IS WRONG !! */
120 g_object_unref (write_stream);
121 g_test_fail (); /* Marking test as FAILED */
125 /* Clean up */
126 g_clear_object (&mount_monitor);
127 g_clear_object (&file_info);
128 g_clear_object (&mounted_file);
129 g_spawn_command_line_sync (command_umount, NULL, NULL, NULL, NULL); /* unmount */
131 g_remove (file_in_mount);
132 g_remove (dir_to_mount);
133 g_remove (dir_mountpoint);
135 g_free (bindfs);
136 g_free (fusermount);
137 g_free (curdir);
138 g_free (dir_to_mount);
139 g_free (dir_mountpoint);
140 g_free (command_mount);
141 g_free (command_mount_ro);
142 g_free (command_umount);
143 g_free (file_in_mount);
144 g_free (file_in_mountpoint);
148 main (int argc, char *argv[])
150 /* To avoid unnecessary D-Bus calls, see http://goo.gl/ir56j2 */
151 g_setenv ("GIO_USE_VFS", "local", FALSE);
153 g_test_init (&argc, &argv, NULL);
155 g_test_bug_base ("http://bugzilla.gnome.org/");
156 g_test_bug ("787731");
158 g_test_add_data_func ("/g-file-info-filesystem-readonly/test-fs-ro",
159 GINT_TO_POINTER (FALSE), test_filesystem_readonly);
161 /* This second test is using a running GUnixMountMonitor, so the calls to:
162 * g_unix_mount_get(&time_read) - To fill the time_read parameter
163 * g_unix_mounts_changed_since()
165 * made from inside g_file_query_filesystem_info() will use the mount_poller_time
166 * from the monitoring of /proc/self/mountinfo , while in the previous test new
167 * created timestamps are returned from those g_unix_mount* functions. */
168 g_test_add_data_func ("/g-file-info-filesystem-readonly/test-fs-ro-with-mount-monitor",
169 GINT_TO_POINTER (TRUE), test_filesystem_readonly);
171 return g_test_run ();