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 : : const char *curdir;
100 : 2 : char *dir_to_mount = NULL, *dir_mountpoint = NULL;
101 : : gchar *file_in_mount, *file_in_mountpoint;
102 : 2 : GError *error = NULL;
103 : :
104 : : /* installed by package 'bindfs' in Fedora */
105 : 2 : bindfs = g_find_program_in_path ("bindfs");
106 : :
107 : : /* installed by package 'fuse' in Fedora */
108 : 2 : fusermount = g_find_program_in_path ("fusermount");
109 : :
110 : 2 : if (bindfs == NULL || fusermount == NULL)
111 : : {
112 : : /* We need these because "mount --bind" requires root privileges */
113 : 0 : g_test_skip ("'bindfs' and 'fusermount' commands are needed to run this test");
114 : 0 : g_free (fusermount);
115 : 0 : g_free (bindfs);
116 : 2 : return;
117 : : }
118 : :
119 : : /* If the fuse module is loaded but there's no /dev/fuse, then we're
120 : : * we're probably in a rootless container and won't be able to
121 : : * use bindfs to run our tests */
122 : 4 : if (fuse_module_loaded () &&
123 : 2 : !g_file_test ("/dev/fuse", G_FILE_TEST_EXISTS))
124 : : {
125 : 2 : g_test_skip ("fuse support is needed to run this test (rootless container?)");
126 : 2 : g_free (fusermount);
127 : 2 : g_free (bindfs);
128 : 2 : return;
129 : : }
130 : :
131 : 0 : curdir = g_get_tmp_dir ();
132 : 0 : dir_to_mount = g_build_filename (curdir, "dir_bindfs_to_mount", NULL);
133 : 0 : file_in_mount = g_build_filename (dir_to_mount, "example.txt", NULL);
134 : 0 : dir_mountpoint = g_build_filename (curdir, "dir_bindfs_mountpoint", NULL);
135 : :
136 : 0 : g_mkdir (dir_to_mount, 0777);
137 : 0 : g_mkdir (dir_mountpoint, 0777);
138 : 0 : if (! g_file_set_contents (file_in_mount, "Example", -1, NULL))
139 : : {
140 : 0 : g_test_skip ("Failed to create file needed to proceed further with the test");
141 : :
142 : 0 : g_free (dir_mountpoint);
143 : 0 : g_free (file_in_mount);
144 : 0 : g_free (dir_to_mount);
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 (fusermount);
168 : 0 : g_free (bindfs);
169 : :
170 : 0 : return;
171 : : }
172 : :
173 : : /* Let's check now, that the file is in indeed in a readonly filesystem */
174 : 0 : file_in_mountpoint = g_build_filename (dir_mountpoint, "example.txt", NULL);
175 : 0 : mounted_file = g_file_new_for_path (file_in_mountpoint);
176 : :
177 : 0 : if (with_mount_monitor)
178 : : {
179 : : /* Let UnixMountMonitor process its 'mounts-changed'
180 : : * signal triggered by mount operation above */
181 : 0 : while (g_main_context_iteration (NULL, FALSE));
182 : : }
183 : :
184 : 0 : file_info = g_file_query_filesystem_info (mounted_file,
185 : : G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, NULL, &error);
186 : 0 : g_assert_no_error (error);
187 : 0 : g_assert_nonnull (file_info);
188 : 0 : if (! g_file_info_get_attribute_boolean (file_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY))
189 : : {
190 : 0 : g_test_skip ("Failed to create readonly file needed to proceed further with the test");
191 : :
192 : 0 : g_clear_object (&file_info);
193 : 0 : g_clear_object (&mounted_file);
194 : 0 : g_free (file_in_mountpoint);
195 : 0 : g_clear_object (&mount_monitor);
196 : 0 : g_free (dir_mountpoint);
197 : 0 : g_free (file_in_mount);
198 : 0 : g_free (dir_to_mount);
199 : 0 : g_free (fusermount);
200 : 0 : g_free (bindfs);
201 : :
202 : 0 : return;
203 : : }
204 : :
205 : : /* Now we unmount, and mount again but this time rw (not readonly) */
206 : 0 : run (&error, fusermount, "-z", "-u", dir_mountpoint, NULL);
207 : 0 : g_assert_no_error (error);
208 : 0 : run (&error, bindfs, "-n", dir_to_mount, dir_mountpoint, NULL);
209 : 0 : g_assert_no_error (error);
210 : :
211 : 0 : if (with_mount_monitor)
212 : : {
213 : : /* Let UnixMountMonitor process its 'mounts-changed' signal
214 : : * triggered by mount/umount operations above */
215 : 0 : while (g_main_context_iteration (NULL, FALSE));
216 : : }
217 : :
218 : : /* Now let's test if GIO will report the new filesystem state */
219 : 0 : g_clear_object (&file_info);
220 : 0 : g_clear_object (&mounted_file);
221 : 0 : mounted_file = g_file_new_for_path (file_in_mountpoint);
222 : 0 : file_info = g_file_query_filesystem_info (mounted_file,
223 : : G_FILE_ATTRIBUTE_FILESYSTEM_READONLY, NULL, &error);
224 : 0 : g_assert_no_error (error);
225 : 0 : g_assert_nonnull (file_info);
226 : :
227 : 0 : g_assert_false (g_file_info_get_attribute_boolean (file_info, G_FILE_ATTRIBUTE_FILESYSTEM_READONLY));
228 : :
229 : : /* Clean up */
230 : 0 : g_clear_object (&mount_monitor);
231 : 0 : g_clear_object (&file_info);
232 : 0 : g_clear_object (&mounted_file);
233 : 0 : run (&error, fusermount, "-z", "-u", dir_mountpoint, NULL);
234 : 0 : g_assert_no_error (error);
235 : :
236 : 0 : assert_remove (file_in_mount);
237 : 0 : assert_remove (dir_to_mount);
238 : 0 : assert_remove (dir_mountpoint);
239 : :
240 : 0 : g_free (bindfs);
241 : 0 : g_free (fusermount);
242 : 0 : g_free (dir_to_mount);
243 : 0 : g_free (dir_mountpoint);
244 : 0 : g_free (file_in_mount);
245 : 0 : g_free (file_in_mountpoint);
246 : : }
247 : :
248 : : int
249 : 1 : main (int argc, char *argv[])
250 : : {
251 : : /* To avoid unnecessary D-Bus calls, see http://goo.gl/ir56j2 */
252 : 1 : g_setenv ("GIO_USE_VFS", "local", FALSE);
253 : :
254 : 1 : g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
255 : :
256 : 1 : g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=787731");
257 : :
258 : 1 : g_test_add_data_func ("/g-file-info-filesystem-readonly/test-fs-ro",
259 : : GINT_TO_POINTER (FALSE), test_filesystem_readonly);
260 : :
261 : : /* This second test is using a running GUnixMountMonitor, so the calls to:
262 : : * g_unix_mount_get(&time_read) - To fill the time_read parameter
263 : : * g_unix_mounts_changed_since()
264 : : *
265 : : * made from inside g_file_query_filesystem_info() will use the mount_poller_time
266 : : * from the monitoring of /proc/self/mountinfo , while in the previous test new
267 : : * created timestamps are returned from those g_unix_mount* functions. */
268 : 1 : g_test_add_data_func ("/g-file-info-filesystem-readonly/test-fs-ro-with-mount-monitor",
269 : : GINT_TO_POINTER (TRUE), test_filesystem_readonly);
270 : :
271 : 1 : return g_test_run ();
272 : : }
|