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 <errno.h>
28 : : #include <stdlib.h>
29 : : #include <stdio.h>
30 : : #include <unistd.h>
31 : : #include <sys/types.h>
32 : : #include <string.h>
33 : : #include <sys/stat.h>
34 : :
35 : : #define PATTERN_FILE_SIZE 0x10000
36 : : #define TEST_HANDLE_SPECIAL TRUE
37 : :
38 : : enum StructureExtraFlags
39 : : {
40 : : TEST_DELETE_NORMAL = 1 << 0,
41 : : TEST_DELETE_TRASH = 1 << 1,
42 : : TEST_DELETE_NON_EMPTY = 1 << 2,
43 : : TEST_DELETE_FAILURE = 1 << 3,
44 : : TEST_NOT_EXISTS = 1 << 4,
45 : : TEST_ENUMERATE_FILE = 1 << 5,
46 : : TEST_NO_ACCESS = 1 << 6,
47 : : TEST_COPY = 1 << 7,
48 : : TEST_MOVE = 1 << 8,
49 : : TEST_COPY_ERROR_RECURSE = 1 << 9,
50 : : TEST_ALREADY_EXISTS = 1 << 10,
51 : : TEST_TARGET_IS_FILE = 1 << 11,
52 : : TEST_CREATE = 1 << 12,
53 : : TEST_REPLACE = 1 << 13,
54 : : TEST_APPEND = 1 << 14,
55 : : TEST_OPEN = 1 << 15,
56 : : TEST_OVERWRITE = 1 << 16,
57 : : TEST_INVALID_SYMLINK = 1 << 17,
58 : : TEST_HIDDEN = 1 << 18,
59 : : TEST_DOT_HIDDEN = 1 << 19,
60 : : };
61 : :
62 : : struct StructureItem
63 : : {
64 : : const char *filename;
65 : : const char *link_to;
66 : : GFileType file_type;
67 : : GFileCreateFlags create_flags;
68 : : guint32 mode;
69 : : gboolean handle_special;
70 : : enum StructureExtraFlags extra_flags;
71 : : };
72 : :
73 : : #define TEST_DIR_NO_ACCESS "dir_no-access"
74 : : #define TEST_DIR_NO_WRITE "dir_no-write"
75 : : #define TEST_DIR_TARGET "dir-target"
76 : : #define TEST_NAME_NOT_EXISTS "not_exists"
77 : : #define TEST_TARGET_FILE "target-file"
78 : :
79 : :
80 : : static const struct StructureItem sample_struct[] = {
81 : : /* filename link file_type create_flags mode | handle_special | extra_flags */
82 : : {"dir1", NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_DELETE_NORMAL | TEST_DELETE_NON_EMPTY | TEST_REPLACE | TEST_OPEN},
83 : : {"dir1/subdir", NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_COPY_ERROR_RECURSE | TEST_APPEND},
84 : : {"dir2", NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_DELETE_NORMAL | TEST_MOVE | TEST_CREATE},
85 : : {TEST_DIR_TARGET, NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_COPY_ERROR_RECURSE},
86 : : {TEST_DIR_NO_ACCESS, NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_PRIVATE, S_IRUSR + S_IWUSR + S_IRGRP + S_IWGRP + S_IROTH + S_IWOTH, 0, TEST_NO_ACCESS | TEST_OPEN},
87 : : {TEST_DIR_NO_WRITE, NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_PRIVATE, S_IRUSR + S_IXUSR + S_IRGRP + S_IXGRP + S_IROTH + S_IXOTH, 0, 0},
88 : : {TEST_TARGET_FILE, NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_OPEN},
89 : : {"normal_file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_ENUMERATE_FILE | TEST_CREATE | TEST_OVERWRITE},
90 : : {"normal_file-symlink", "normal_file", G_FILE_TYPE_SYMBOLIC_LINK, G_FILE_CREATE_NONE, 0, 0, TEST_ENUMERATE_FILE | TEST_COPY | TEST_OPEN},
91 : : {"executable_file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, S_IRWXU + S_IRWXG + S_IRWXO, 0, TEST_DELETE_TRASH | TEST_COPY | TEST_OPEN | TEST_OVERWRITE | TEST_REPLACE},
92 : : {"private_file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_PRIVATE, 0, 0, TEST_COPY | TEST_OPEN | TEST_OVERWRITE | TEST_APPEND},
93 : : {"normal_file2", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_OVERWRITE | TEST_REPLACE},
94 : : {"readonly_file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, S_IRUSR + S_IRGRP + S_IROTH, 0, TEST_DELETE_NORMAL | TEST_OPEN},
95 : : {"UTF_pr\xcc\x8ci\xcc\x81lis\xcc\x8c z",
96 : : NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_CREATE | TEST_OPEN | TEST_OVERWRITE},
97 : : {"dir_pr\xcc\x8ci\xcc\x81lis\xcc\x8c z",
98 : : NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_DELETE_NORMAL | TEST_CREATE},
99 : : {"pattern_file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_COPY | TEST_OPEN | TEST_APPEND},
100 : : {TEST_NAME_NOT_EXISTS, NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_DELETE_NORMAL | TEST_NOT_EXISTS | TEST_COPY | TEST_OPEN},
101 : : {TEST_NAME_NOT_EXISTS, NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_DELETE_TRASH | TEST_NOT_EXISTS | TEST_MOVE},
102 : : {"not_exists2", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_NOT_EXISTS | TEST_CREATE},
103 : : {"not_exists3", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_NOT_EXISTS | TEST_REPLACE},
104 : : {"not_exists4", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_NOT_EXISTS | TEST_APPEND},
105 : : {"dir_no-execute/file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_DELETE_NORMAL | TEST_DELETE_FAILURE | TEST_NOT_EXISTS | TEST_OPEN},
106 : : {"lost_symlink", "nowhere", G_FILE_TYPE_SYMBOLIC_LINK, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_DELETE_NORMAL | TEST_OPEN | TEST_INVALID_SYMLINK},
107 : : {"dir_hidden", NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, 0},
108 : : {"dir_hidden/.hidden", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, 0},
109 : : {"dir_hidden/.a-hidden-file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_HIDDEN},
110 : : {"dir_hidden/file-in-.hidden1", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_HIDDEN | TEST_DOT_HIDDEN},
111 : : {"dir_hidden/file-in-.hidden2", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_HIDDEN | TEST_DOT_HIDDEN},
112 : : };
113 : :
114 : : static gboolean test_suite;
115 : : static gboolean write_test;
116 : : static gboolean verbose;
117 : : static gboolean posix_compat;
118 : :
119 : : #ifdef G_OS_UNIX
120 : : /*
121 : : * check_cap_dac_override:
122 : : * @tmpdir: A temporary directory in which we can create and delete files
123 : : *
124 : : * Check whether the current process can bypass DAC permissions.
125 : : *
126 : : * Traditionally, "privileged" processes (those with effective uid 0)
127 : : * could do this (and bypass many other checks), and "unprivileged"
128 : : * processes could not.
129 : : *
130 : : * In Linux, the special powers of euid 0 are divided into many
131 : : * capabilities: see `capabilities(7)`. The one we are interested in
132 : : * here is `CAP_DAC_OVERRIDE`.
133 : : *
134 : : * We do this generically instead of actually looking at the capability
135 : : * bits, so that the right thing will happen on non-Linux Unix
136 : : * implementations, in particular if they have something equivalent to
137 : : * but not identical to Linux permissions.
138 : : *
139 : : * Returns: %TRUE if we have Linux `CAP_DAC_OVERRIDE` or equivalent
140 : : * privileges
141 : : */
142 : : static gboolean
143 : 73 : check_cap_dac_override (const char *tmpdir)
144 : : {
145 : : gchar *dac_denies_write;
146 : : gchar *inside;
147 : : gboolean have_cap;
148 : :
149 : 73 : dac_denies_write = g_build_filename (tmpdir, "dac-denies-write", NULL);
150 : 73 : inside = g_build_filename (dac_denies_write, "inside", NULL);
151 : :
152 : 73 : g_assert_no_errno (mkdir (dac_denies_write, S_IRWXU));
153 : 73 : g_assert_no_errno (chmod (dac_denies_write, 0));
154 : :
155 : 73 : if (mkdir (inside, S_IRWXU) == 0)
156 : : {
157 : 0 : g_test_message ("Looks like we have CAP_DAC_OVERRIDE or equivalent");
158 : 0 : g_assert_no_errno (rmdir (inside));
159 : 0 : have_cap = TRUE;
160 : : }
161 : : else
162 : : {
163 : 73 : int saved_errno = errno;
164 : :
165 : 73 : g_test_message ("We do not have CAP_DAC_OVERRIDE or equivalent");
166 : 73 : g_assert_cmpint (saved_errno, ==, EACCES);
167 : 73 : have_cap = FALSE;
168 : : }
169 : :
170 : 73 : g_assert_no_errno (chmod (dac_denies_write, S_IRWXU));
171 : 73 : g_assert_no_errno (rmdir (dac_denies_write));
172 : 73 : g_free (dac_denies_write);
173 : 73 : g_free (inside);
174 : 73 : return have_cap;
175 : : }
176 : : #endif
177 : :
178 : : static GFile *
179 : 10 : create_empty_file (GFile * parent, const char *filename,
180 : : GFileCreateFlags create_flags)
181 : : {
182 : : GFile *child;
183 : : GError *error;
184 : : GFileOutputStream *outs;
185 : :
186 : 10 : child = g_file_get_child (parent, filename);
187 : 10 : g_assert_nonnull (child);
188 : :
189 : 10 : error = NULL;
190 : 10 : outs = g_file_replace (child, NULL, FALSE, create_flags, NULL, &error);
191 : 10 : g_assert_no_error (error);
192 : 10 : g_assert_nonnull (outs);
193 : 10 : error = NULL;
194 : 10 : g_output_stream_close (G_OUTPUT_STREAM (outs), NULL, &error);
195 : 10 : g_object_unref (outs);
196 : 10 : return child;
197 : : }
198 : :
199 : : static GFile *
200 : 8 : create_empty_dir (GFile * parent, const char *filename)
201 : : {
202 : : GFile *child;
203 : : gboolean res;
204 : : GError *error;
205 : :
206 : 8 : child = g_file_get_child (parent, filename);
207 : 8 : g_assert_nonnull (child);
208 : 8 : error = NULL;
209 : 8 : res = g_file_make_directory (child, NULL, &error);
210 : 8 : g_assert_true (res);
211 : 8 : g_assert_no_error (error);
212 : 8 : return child;
213 : : }
214 : :
215 : : static GFile *
216 : 2 : create_symlink (GFile * parent, const char *filename, const char *points_to)
217 : : {
218 : : GFile *child;
219 : : gboolean res;
220 : : GError *error;
221 : :
222 : 2 : child = g_file_get_child (parent, filename);
223 : 2 : g_assert_nonnull (child);
224 : 2 : error = NULL;
225 : 2 : res = g_file_make_symbolic_link (child, points_to, NULL, &error);
226 : 2 : g_assert_true (res);
227 : 2 : g_assert_no_error (error);
228 : 2 : return child;
229 : : }
230 : :
231 : : static void
232 : 1 : test_create_structure (gconstpointer test_data)
233 : : {
234 : : GFile *root;
235 : : GFile *child;
236 : : gboolean res;
237 : 1 : GError *error = NULL;
238 : : GFileOutputStream *outs;
239 : : GDataOutputStream *outds;
240 : : guint i;
241 : : struct StructureItem item;
242 : :
243 : 1 : g_assert_nonnull (test_data);
244 : 1 : g_test_message ("\n Going to create testing structure in '%s'...",
245 : : (char *) test_data);
246 : :
247 : 1 : root = g_file_new_for_commandline_arg ((char *) test_data);
248 : 1 : g_assert_nonnull (root);
249 : :
250 : : /* create root directory */
251 : 1 : g_file_make_directory (root, NULL, &error);
252 : 1 : g_assert_no_error (error);
253 : :
254 : : /* create any other items */
255 : 29 : for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
256 : : {
257 : 28 : item = sample_struct[i];
258 : 28 : if ((item.handle_special)
259 : 20 : || ((!posix_compat)
260 : 0 : && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK)))
261 : 8 : continue;
262 : :
263 : 20 : child = NULL;
264 : 20 : switch (item.file_type)
265 : : {
266 : 10 : case G_FILE_TYPE_REGULAR:
267 : 10 : g_test_message (" Creating file '%s'...", item.filename);
268 : 10 : child = create_empty_file (root, item.filename, item.create_flags);
269 : 10 : break;
270 : 8 : case G_FILE_TYPE_DIRECTORY:
271 : 8 : g_test_message (" Creating directory '%s'...", item.filename);
272 : 8 : child = create_empty_dir (root, item.filename);
273 : 8 : break;
274 : 2 : case G_FILE_TYPE_SYMBOLIC_LINK:
275 : 2 : g_test_message (" Creating symlink '%s' --> '%s'...", item.filename,
276 : : item.link_to);
277 : 2 : child = create_symlink (root, item.filename, item.link_to);
278 : 2 : break;
279 : 0 : case G_FILE_TYPE_UNKNOWN:
280 : : case G_FILE_TYPE_SPECIAL:
281 : : case G_FILE_TYPE_SHORTCUT:
282 : : case G_FILE_TYPE_MOUNTABLE:
283 : : default:
284 : 0 : break;
285 : : }
286 : 20 : g_assert_nonnull (child);
287 : :
288 : 20 : if ((item.mode > 0) && (posix_compat))
289 : : {
290 : : res =
291 : 4 : g_file_set_attribute_uint32 (child, G_FILE_ATTRIBUTE_UNIX_MODE,
292 : : item.mode,
293 : : G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
294 : : NULL, &error);
295 : 4 : g_assert_true (res);
296 : 4 : g_assert_no_error (error);
297 : : }
298 : :
299 : 20 : if ((item.extra_flags & TEST_DOT_HIDDEN) == TEST_DOT_HIDDEN)
300 : : {
301 : : gchar *dir, *path, *basename;
302 : : FILE *f;
303 : :
304 : 2 : dir = g_path_get_dirname (item.filename);
305 : 2 : basename = g_path_get_basename (item.filename);
306 : 2 : path = g_build_filename (test_data, dir, ".hidden", NULL);
307 : :
308 : 2 : f = fopen (path, "a");
309 : 2 : fprintf (f, "%s\n", basename);
310 : 2 : fclose (f);
311 : :
312 : 2 : g_free (dir);
313 : 2 : g_free (path);
314 : 2 : g_free (basename);
315 : : }
316 : :
317 : 20 : g_object_unref (child);
318 : : }
319 : :
320 : : /* create a pattern file */
321 : 1 : g_test_message (" Creating pattern file...");
322 : 1 : child = g_file_get_child (root, "pattern_file");
323 : 1 : g_assert_nonnull (child);
324 : :
325 : : outs =
326 : 1 : g_file_replace (child, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error);
327 : 1 : g_assert_no_error (error);
328 : :
329 : 1 : g_assert_nonnull (outs);
330 : 1 : outds = g_data_output_stream_new (G_OUTPUT_STREAM (outs));
331 : 1 : g_assert_nonnull (outds);
332 : 65537 : for (i = 0; i < PATTERN_FILE_SIZE; i++)
333 : : {
334 : 65536 : g_data_output_stream_put_byte (outds, i % 256, NULL, &error);
335 : 65536 : g_assert_no_error (error);
336 : : }
337 : :
338 : 1 : g_output_stream_close (G_OUTPUT_STREAM (outs), NULL, &error);
339 : 1 : g_assert_no_error (error);
340 : 1 : g_object_unref (outds);
341 : 1 : g_object_unref (outs);
342 : 1 : g_object_unref (child);
343 : 1 : g_test_message (" done.");
344 : :
345 : 1 : g_object_unref (root);
346 : 1 : }
347 : :
348 : : static GFile *
349 : 30 : file_exists (GFile * parent, const char *filename, gboolean * result)
350 : : {
351 : : GFile *child;
352 : : gboolean res;
353 : :
354 : 30 : if (result)
355 : 30 : *result = FALSE;
356 : :
357 : 30 : child = g_file_get_child (parent, filename);
358 : 30 : g_assert_nonnull (child);
359 : 30 : res = g_file_query_exists (child, NULL);
360 : 30 : if (result)
361 : 30 : *result = res;
362 : :
363 : 30 : return child;
364 : : }
365 : :
366 : : static void
367 : 42 : test_attributes (struct StructureItem item, GFileInfo * info)
368 : : {
369 : : GFileType ftype;
370 : : guint32 mode;
371 : : const char *name, *display_name, *edit_name, *copy_name, *symlink_target;
372 : : gboolean utf8_valid;
373 : : gboolean has_attr;
374 : : gboolean is_symlink;
375 : : gboolean is_hidden;
376 : : gboolean can_read, can_write;
377 : :
378 : : /* standard::type */
379 : 42 : has_attr = g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE);
380 : 42 : g_assert_true (has_attr);
381 : 42 : ftype = g_file_info_get_file_type (info);
382 : 42 : g_assert_cmpint (ftype, !=, G_FILE_TYPE_UNKNOWN);
383 : 42 : g_assert_cmpint (ftype, ==, item.file_type);
384 : :
385 : : /* unix::mode */
386 : 42 : if ((item.mode > 0) && (posix_compat))
387 : : {
388 : 8 : mode =
389 : 8 : g_file_info_get_attribute_uint32 (info,
390 : : G_FILE_ATTRIBUTE_UNIX_MODE) & 0xFFF;
391 : 8 : g_assert_cmpint (mode, ==, item.mode);
392 : : }
393 : :
394 : : /* access::can-read */
395 : 42 : if (item.file_type != G_FILE_TYPE_SYMBOLIC_LINK)
396 : : {
397 : : can_read =
398 : 38 : g_file_info_get_attribute_boolean (info,
399 : : G_FILE_ATTRIBUTE_ACCESS_CAN_READ);
400 : 38 : g_assert_true (can_read);
401 : : }
402 : :
403 : : /* access::can-write */
404 : 42 : if ((write_test) && ((item.extra_flags & TEST_OVERWRITE) == TEST_OVERWRITE))
405 : : {
406 : : can_write =
407 : 10 : g_file_info_get_attribute_boolean (info,
408 : : G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE);
409 : 10 : g_assert_true (can_write);
410 : : }
411 : :
412 : : /* standard::name */
413 : 42 : name = g_file_info_get_name (info);
414 : 42 : g_assert_nonnull (name);
415 : :
416 : : /* standard::display-name */
417 : 42 : display_name = g_file_info_get_display_name (info);
418 : 42 : g_assert_nonnull (display_name);
419 : 42 : utf8_valid = g_utf8_validate (display_name, -1, NULL);
420 : 42 : g_assert_true (utf8_valid);
421 : :
422 : : /* standard::edit-name */
423 : 42 : edit_name = g_file_info_get_edit_name (info);
424 : 42 : if (edit_name)
425 : : {
426 : 42 : utf8_valid = g_utf8_validate (edit_name, -1, NULL);
427 : 42 : g_assert_true (utf8_valid);
428 : : }
429 : :
430 : : /* standard::copy-name */
431 : : copy_name =
432 : 42 : g_file_info_get_attribute_string (info,
433 : : G_FILE_ATTRIBUTE_STANDARD_COPY_NAME);
434 : 42 : if (copy_name)
435 : : {
436 : 42 : utf8_valid = g_utf8_validate (copy_name, -1, NULL);
437 : 42 : g_assert_true (utf8_valid);
438 : : }
439 : :
440 : : /* standard::is-symlink */
441 : 42 : if (posix_compat)
442 : : {
443 : 42 : is_symlink = g_file_info_get_is_symlink (info);
444 : 42 : g_assert_cmpint (is_symlink, ==,
445 : : item.file_type == G_FILE_TYPE_SYMBOLIC_LINK);
446 : : }
447 : :
448 : : /* standard::symlink-target */
449 : 42 : if ((item.file_type == G_FILE_TYPE_SYMBOLIC_LINK) && (posix_compat))
450 : : {
451 : 4 : symlink_target = g_file_info_get_symlink_target (info);
452 : 4 : g_assert_cmpstr (symlink_target, ==, item.link_to);
453 : : }
454 : :
455 : : /* standard::is-hidden */
456 : 42 : if ((item.extra_flags & TEST_HIDDEN) == TEST_HIDDEN)
457 : : {
458 : : is_hidden =
459 : 6 : g_file_info_get_attribute_boolean (info,
460 : : G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN);
461 : 6 : g_assert_true (is_hidden);
462 : : }
463 : :
464 : : /* unix::is-mountpoint */
465 : 42 : if (posix_compat)
466 : : {
467 : : gboolean is_mountpoint =
468 : 42 : g_file_info_get_attribute_boolean (info,
469 : : G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT);
470 : 42 : g_assert_false (is_mountpoint);
471 : : }
472 : 42 : }
473 : :
474 : : static void
475 : 1 : test_initial_structure (gconstpointer test_data)
476 : : {
477 : : GFile *root;
478 : : GFile *child;
479 : : gboolean res;
480 : : GError *error;
481 : : GFileInputStream *ins;
482 : : guint i;
483 : : GFileInfo *info;
484 : : guint32 size;
485 : : guchar *buffer;
486 : : gssize read, total_read;
487 : : struct StructureItem item;
488 : :
489 : 1 : g_assert_nonnull (test_data);
490 : 1 : g_test_message (" Testing sample structure in '%s'...", (char *) test_data);
491 : :
492 : 1 : root = g_file_new_for_commandline_arg ((char *) test_data);
493 : 1 : g_assert_nonnull (root);
494 : 1 : res = g_file_query_exists (root, NULL);
495 : 1 : g_assert_true (res);
496 : :
497 : : /* test the structure */
498 : 29 : for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
499 : : {
500 : 28 : item = sample_struct[i];
501 : 28 : if (((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
502 : 28 : || (item.handle_special))
503 : 8 : continue;
504 : :
505 : 20 : g_test_message (" Testing file '%s'...", item.filename);
506 : :
507 : 20 : child = file_exists (root, item.filename, &res);
508 : 20 : g_assert_nonnull (child);
509 : 20 : g_assert_true (res);
510 : :
511 : 20 : error = NULL;
512 : : info =
513 : 20 : g_file_query_info (child, "*", G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
514 : : NULL, &error);
515 : 20 : g_assert_no_error (error);
516 : 20 : g_assert_nonnull (info);
517 : :
518 : 20 : test_attributes (item, info);
519 : :
520 : 20 : g_object_unref (child);
521 : 20 : g_object_unref (info);
522 : : }
523 : :
524 : : /* read and test the pattern file */
525 : 1 : g_test_message (" Testing pattern file...");
526 : 1 : child = file_exists (root, "pattern_file", &res);
527 : 1 : g_assert_nonnull (child);
528 : 1 : g_assert_true (res);
529 : :
530 : 1 : error = NULL;
531 : : info =
532 : 1 : g_file_query_info (child, "*", G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
533 : : &error);
534 : 1 : g_assert_no_error (error);
535 : 1 : g_assert_nonnull (info);
536 : 1 : size = g_file_info_get_size (info);
537 : 1 : g_assert_cmpint (size, ==, PATTERN_FILE_SIZE);
538 : 1 : g_object_unref (info);
539 : :
540 : 1 : error = NULL;
541 : 1 : ins = g_file_read (child, NULL, &error);
542 : 1 : g_assert_nonnull (ins);
543 : 1 : g_assert_no_error (error);
544 : :
545 : 1 : buffer = g_malloc (PATTERN_FILE_SIZE);
546 : 1 : total_read = 0;
547 : :
548 : 2 : while (total_read < PATTERN_FILE_SIZE)
549 : : {
550 : 1 : error = NULL;
551 : : read =
552 : 1 : g_input_stream_read (G_INPUT_STREAM (ins), buffer + total_read,
553 : : PATTERN_FILE_SIZE, NULL, &error);
554 : 1 : g_assert_no_error (error);
555 : 1 : total_read += read;
556 : 1 : g_test_message (" read %"G_GSSIZE_FORMAT" bytes, total = %"G_GSSIZE_FORMAT" of %d.",
557 : : read, total_read, PATTERN_FILE_SIZE);
558 : : }
559 : 1 : g_assert_cmpint (total_read, ==, PATTERN_FILE_SIZE);
560 : :
561 : 1 : error = NULL;
562 : 1 : res = g_input_stream_close (G_INPUT_STREAM (ins), NULL, &error);
563 : 1 : g_assert_no_error (error);
564 : 1 : g_assert_true (res);
565 : :
566 : 65537 : for (i = 0; i < PATTERN_FILE_SIZE; i++)
567 : 65536 : g_assert_cmpint (*(buffer + i), ==, i % 256);
568 : :
569 : 1 : g_object_unref (ins);
570 : 1 : g_object_unref (child);
571 : 1 : g_free (buffer);
572 : 1 : g_object_unref (root);
573 : 1 : }
574 : :
575 : : static void
576 : 9 : traverse_recurse_dirs (GFile * parent, GFile * root)
577 : : {
578 : : gboolean res;
579 : : GError *error;
580 : : GFileEnumerator *enumerator;
581 : : GFileInfo *info;
582 : : GFile *descend;
583 : : char *relative_path;
584 : : guint i;
585 : : gboolean found;
586 : :
587 : 9 : g_assert_nonnull (root);
588 : :
589 : 9 : error = NULL;
590 : : enumerator =
591 : 9 : g_file_enumerate_children (parent, "*",
592 : : G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
593 : : &error);
594 : 9 : g_assert_nonnull (enumerator);
595 : 9 : g_assert_no_error (error);
596 : :
597 : 9 : g_assert_true (g_file_enumerator_get_container (enumerator) == parent);
598 : :
599 : 9 : error = NULL;
600 : 9 : info = g_file_enumerator_next_file (enumerator, NULL, &error);
601 : 31 : while ((info) && (!error))
602 : : {
603 : 22 : descend = g_file_enumerator_get_child (enumerator, info);
604 : 22 : g_assert_nonnull (descend);
605 : 22 : relative_path = g_file_get_relative_path (root, descend);
606 : 22 : g_assert_nonnull (relative_path);
607 : :
608 : 22 : found = FALSE;
609 : 289 : for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
610 : : {
611 : 289 : if (strcmp (sample_struct[i].filename, relative_path) == 0)
612 : : {
613 : : /* test the attributes again */
614 : 22 : test_attributes (sample_struct[i], info);
615 : :
616 : 22 : found = TRUE;
617 : 22 : break;
618 : : }
619 : : }
620 : 22 : g_assert_true (found);
621 : :
622 : 22 : g_test_message (" Found file %s, relative to root: %s",
623 : : g_file_info_get_display_name (info), relative_path);
624 : :
625 : 22 : if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
626 : 8 : traverse_recurse_dirs (descend, root);
627 : :
628 : 22 : g_object_unref (descend);
629 : 22 : error = NULL;
630 : 22 : g_object_unref (info);
631 : 22 : g_free (relative_path);
632 : :
633 : 22 : info = g_file_enumerator_next_file (enumerator, NULL, &error);
634 : : }
635 : 9 : g_assert_no_error (error);
636 : :
637 : 9 : error = NULL;
638 : 9 : res = g_file_enumerator_close (enumerator, NULL, &error);
639 : 9 : g_assert_true (res);
640 : 9 : g_assert_no_error (error);
641 : 9 : g_assert_true (g_file_enumerator_is_closed (enumerator));
642 : :
643 : 9 : g_object_unref (enumerator);
644 : 9 : }
645 : :
646 : : static void
647 : 1 : test_traverse_structure (gconstpointer test_data)
648 : : {
649 : : GFile *root;
650 : : gboolean res;
651 : :
652 : 1 : g_assert_nonnull (test_data);
653 : 1 : g_test_message (" Traversing through the sample structure in '%s'...",
654 : : (char *) test_data);
655 : :
656 : 1 : root = g_file_new_for_commandline_arg ((char *) test_data);
657 : 1 : g_assert_nonnull (root);
658 : 1 : res = g_file_query_exists (root, NULL);
659 : 1 : g_assert_true (res);
660 : :
661 : 1 : traverse_recurse_dirs (root, root);
662 : :
663 : 1 : g_object_unref (root);
664 : 1 : }
665 : :
666 : :
667 : :
668 : :
669 : : static void
670 : 1 : test_enumerate (gconstpointer test_data)
671 : : {
672 : : GFile *root, *child;
673 : : gboolean res;
674 : : GError *error;
675 : : GFileEnumerator *enumerator;
676 : : GFileInfo *info;
677 : : guint i;
678 : : struct StructureItem item;
679 : :
680 : :
681 : 1 : g_assert_nonnull (test_data);
682 : 1 : g_test_message (" Test enumerate '%s'...", (char *) test_data);
683 : :
684 : 1 : root = g_file_new_for_commandline_arg ((char *) test_data);
685 : 1 : g_assert_nonnull (root);
686 : 1 : res = g_file_query_exists (root, NULL);
687 : 1 : g_assert_true (res);
688 : :
689 : :
690 : 29 : for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
691 : : {
692 : 28 : item = sample_struct[i];
693 : 28 : if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
694 : 0 : continue;
695 : :
696 : 28 : if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
697 : 22 : (((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS)
698 : 1 : && posix_compat)
699 : 21 : || ((item.extra_flags & TEST_ENUMERATE_FILE) ==
700 : : TEST_ENUMERATE_FILE))
701 : : {
702 : 9 : g_test_message (" Testing file '%s'", item.filename);
703 : 9 : child = g_file_get_child (root, item.filename);
704 : 9 : g_assert_nonnull (child);
705 : 9 : error = NULL;
706 : : enumerator =
707 : 9 : g_file_enumerate_children (child, "*",
708 : : G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
709 : : NULL, &error);
710 : :
711 : 9 : if ((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS)
712 : : {
713 : 6 : g_assert_null (enumerator);
714 : 6 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
715 : : }
716 : 9 : if ((item.extra_flags & TEST_ENUMERATE_FILE) == TEST_ENUMERATE_FILE)
717 : : {
718 : 2 : g_assert_null (enumerator);
719 : 2 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY);
720 : : }
721 : 9 : if ((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS)
722 : : {
723 : 1 : g_assert_nonnull (enumerator);
724 : :
725 : 1 : error = NULL;
726 : 1 : info = g_file_enumerator_next_file (enumerator, NULL, &error);
727 : 1 : g_assert_null (info);
728 : 1 : g_assert_no_error (error);
729 : : /* no items should be found, no error should be logged */
730 : : }
731 : :
732 : 9 : if (error)
733 : 8 : g_error_free (error);
734 : :
735 : 9 : if (enumerator)
736 : : {
737 : 1 : error = NULL;
738 : 1 : res = g_file_enumerator_close (enumerator, NULL, &error);
739 : 1 : g_assert_true (res);
740 : 1 : g_assert_no_error (error);
741 : :
742 : 1 : g_object_unref (enumerator);
743 : : }
744 : 9 : g_object_unref (child);
745 : : }
746 : : }
747 : 1 : g_object_unref (root);
748 : 1 : }
749 : :
750 : : static void
751 : 72 : do_copy_move (GFile * root, struct StructureItem item, const char *target_dir,
752 : : enum StructureExtraFlags extra_flags)
753 : : {
754 : : GFile *dst_dir, *src_file, *dst_file;
755 : : gboolean res;
756 : : GError *error;
757 : : #ifdef G_OS_UNIX
758 : 72 : gboolean have_cap_dac_override = check_cap_dac_override (g_file_peek_path (root));
759 : : #endif
760 : :
761 : 72 : g_test_message (" do_copy_move: '%s' --> '%s'", item.filename, target_dir);
762 : :
763 : 72 : dst_dir = g_file_get_child (root, target_dir);
764 : 72 : g_assert_nonnull (dst_dir);
765 : 72 : src_file = g_file_get_child (root, item.filename);
766 : 72 : g_assert_nonnull (src_file);
767 : 72 : dst_file = g_file_get_child (dst_dir, item.filename);
768 : 72 : g_assert_nonnull (dst_file);
769 : :
770 : 72 : error = NULL;
771 : 72 : if ((item.extra_flags & TEST_COPY) == TEST_COPY)
772 : : res =
773 : 70 : g_file_copy (src_file, dst_file,
774 : : G_FILE_COPY_NOFOLLOW_SYMLINKS |
775 : : ((extra_flags ==
776 : : TEST_OVERWRITE) ? G_FILE_COPY_OVERWRITE :
777 : : G_FILE_COPY_NONE), NULL, NULL, NULL, &error);
778 : : else
779 : : res =
780 : 2 : g_file_move (src_file, dst_file, G_FILE_COPY_NOFOLLOW_SYMLINKS, NULL,
781 : : NULL, NULL, &error);
782 : :
783 : 72 : if (error)
784 : 59 : g_test_message (" res = %d, error code %d = %s", res, error->code,
785 : 59 : error->message);
786 : :
787 : : /* copying file/directory to itself (".") */
788 : 72 : if (((item.extra_flags & TEST_NOT_EXISTS) != TEST_NOT_EXISTS) &&
789 : : (extra_flags == TEST_ALREADY_EXISTS))
790 : : {
791 : 10 : g_assert_false (res);
792 : 10 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
793 : : }
794 : : /* target file is a file, overwrite is not set */
795 : 62 : else if (((item.extra_flags & TEST_NOT_EXISTS) != TEST_NOT_EXISTS) &&
796 : : (extra_flags == TEST_TARGET_IS_FILE))
797 : : {
798 : 10 : g_assert_false (res);
799 : 10 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY);
800 : : }
801 : : /* source file is directory */
802 : 52 : else if ((item.extra_flags & TEST_COPY_ERROR_RECURSE) ==
803 : : TEST_COPY_ERROR_RECURSE)
804 : : {
805 : 8 : g_assert_false (res);
806 : 8 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE);
807 : : }
808 : : /* source or target path doesn't exist */
809 : 44 : else if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
810 : : (extra_flags == TEST_NOT_EXISTS))
811 : : {
812 : 15 : g_assert_false (res);
813 : 15 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
814 : : }
815 : : /* source or target path permission denied */
816 : 29 : else if (((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS) ||
817 : : (extra_flags == TEST_NO_ACCESS))
818 : : {
819 : : /* This works for root, see bug #552912 */
820 : : #ifdef G_OS_UNIX
821 : 16 : if (have_cap_dac_override)
822 : : {
823 : 0 : g_test_message ("Unable to exercise g_file_copy() or g_file_move() "
824 : : "failing with EACCES: we probably have "
825 : : "CAP_DAC_OVERRIDE");
826 : 0 : g_assert_true (res);
827 : 0 : g_assert_no_error (error);
828 : : }
829 : : else
830 : : #endif
831 : : {
832 : 16 : g_assert_false (res);
833 : 16 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED);
834 : : }
835 : : }
836 : : /* no error should be found, all exceptions defined above */
837 : : else
838 : : {
839 : 13 : g_assert_true (res);
840 : 13 : g_assert_no_error (error);
841 : : }
842 : :
843 : 72 : if (error)
844 : 59 : g_error_free (error);
845 : :
846 : :
847 : 72 : g_object_unref (dst_dir);
848 : 72 : g_object_unref (src_file);
849 : 72 : g_object_unref (dst_file);
850 : 72 : }
851 : :
852 : : static void
853 : 1 : test_copy_move (gconstpointer test_data)
854 : : {
855 : : GFile *root;
856 : : gboolean res;
857 : : guint i;
858 : : struct StructureItem item;
859 : :
860 : 1 : g_assert_nonnull (test_data);
861 : 1 : root = g_file_new_for_commandline_arg ((char *) test_data);
862 : 1 : g_assert_nonnull (root);
863 : 1 : res = g_file_query_exists (root, NULL);
864 : 1 : g_assert_true (res);
865 : :
866 : :
867 : 29 : for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
868 : : {
869 : 28 : item = sample_struct[i];
870 : :
871 : 28 : if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
872 : 0 : continue;
873 : :
874 : 28 : if (((item.extra_flags & TEST_COPY) == TEST_COPY) ||
875 : 17 : ((item.extra_flags & TEST_MOVE) == TEST_MOVE))
876 : : {
877 : : /* test copy/move to a directory, expecting no errors if source files exist */
878 : 13 : do_copy_move (root, item, TEST_DIR_TARGET, 0);
879 : :
880 : : /* some files have been already moved so we can't count with them in the tests */
881 : 13 : if ((item.extra_flags & TEST_COPY) == TEST_COPY)
882 : : {
883 : : /* test overwrite for flagged files */
884 : 11 : if ((item.extra_flags & TEST_OVERWRITE) == TEST_OVERWRITE)
885 : : {
886 : 4 : do_copy_move (root, item, TEST_DIR_TARGET, TEST_OVERWRITE);
887 : : }
888 : : /* source = target, should return G_IO_ERROR_EXISTS */
889 : 11 : do_copy_move (root, item, ".", TEST_ALREADY_EXISTS);
890 : : /* target is file */
891 : 11 : do_copy_move (root, item, TEST_TARGET_FILE,
892 : : TEST_TARGET_IS_FILE);
893 : : /* target path is invalid */
894 : 11 : do_copy_move (root, item, TEST_NAME_NOT_EXISTS,
895 : : TEST_NOT_EXISTS);
896 : :
897 : : /* tests on POSIX-compatible filesystems */
898 : 11 : if (posix_compat)
899 : : {
900 : : /* target directory is not accessible (no execute flag) */
901 : 11 : do_copy_move (root, item, TEST_DIR_NO_ACCESS,
902 : : TEST_NO_ACCESS);
903 : : /* target directory is readonly */
904 : 11 : do_copy_move (root, item, TEST_DIR_NO_WRITE,
905 : : TEST_NO_ACCESS);
906 : : }
907 : : }
908 : : }
909 : : }
910 : 1 : g_object_unref (root);
911 : 1 : }
912 : :
913 : : /* Test that G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT is TRUE for / and for another
914 : : * known mountpoint. The FALSE case is tested for many directories and files by
915 : : * test_initial_structure(), via test_attributes().
916 : : */
917 : : static void
918 : 2 : test_unix_is_mountpoint (gconstpointer data)
919 : : {
920 : 2 : const gchar *path = data;
921 : 2 : GFile *file = g_file_new_for_path (path);
922 : : GFileInfo *info;
923 : : gboolean is_mountpoint;
924 : 2 : GError *error = NULL;
925 : :
926 : 2 : info = g_file_query_info (file, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT,
927 : : G_FILE_QUERY_INFO_NONE, NULL, &error);
928 : 2 : g_assert_no_error (error);
929 : 2 : g_assert_nonnull (info);
930 : :
931 : : is_mountpoint =
932 : 2 : g_file_info_get_attribute_boolean (info,
933 : : G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT);
934 : 2 : g_assert_true (is_mountpoint);
935 : :
936 : 2 : g_clear_object (&info);
937 : 2 : g_clear_object (&file);
938 : 2 : }
939 : :
940 : : static void
941 : 1 : test_create (gconstpointer test_data)
942 : : {
943 : : GFile *root, *child;
944 : : gboolean res;
945 : : GError *error;
946 : : guint i;
947 : : struct StructureItem item;
948 : : GFileOutputStream *os;
949 : :
950 : 1 : g_assert_nonnull (test_data);
951 : :
952 : 1 : root = g_file_new_for_commandline_arg ((char *) test_data);
953 : 1 : g_assert_nonnull (root);
954 : 1 : res = g_file_query_exists (root, NULL);
955 : 1 : g_assert_true (res);
956 : :
957 : 29 : for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
958 : : {
959 : 28 : item = sample_struct[i];
960 : :
961 : 28 : if (((item.extra_flags & TEST_CREATE) == TEST_CREATE) ||
962 : 23 : ((item.extra_flags & TEST_REPLACE) == TEST_REPLACE) ||
963 : 19 : ((item.extra_flags & TEST_APPEND) == TEST_APPEND))
964 : : {
965 : 13 : g_test_message (" test_create: '%s'", item.filename);
966 : :
967 : 13 : child = g_file_get_child (root, item.filename);
968 : 13 : g_assert_nonnull (child);
969 : 13 : error = NULL;
970 : 13 : os = NULL;
971 : :
972 : 13 : if ((item.extra_flags & TEST_CREATE) == TEST_CREATE)
973 : 5 : os = g_file_create (child, item.create_flags, NULL, &error);
974 : 8 : else if ((item.extra_flags & TEST_REPLACE) == TEST_REPLACE)
975 : : os =
976 : 4 : g_file_replace (child, NULL, TRUE, item.create_flags, NULL,
977 : : &error);
978 : 4 : else if ((item.extra_flags & TEST_APPEND) == TEST_APPEND)
979 : 4 : os = g_file_append_to (child, item.create_flags, NULL, &error);
980 : :
981 : :
982 : 13 : if (error)
983 : 6 : g_test_message (" error code %d = %s", error->code, error->message);
984 : :
985 : 13 : if (((item.extra_flags & TEST_NOT_EXISTS) == 0) &&
986 : 10 : ((item.extra_flags & TEST_CREATE) == TEST_CREATE))
987 : : {
988 : 4 : g_assert_null (os);
989 : 4 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
990 : : }
991 : 9 : else if (item.file_type == G_FILE_TYPE_DIRECTORY)
992 : : {
993 : 2 : g_assert_null (os);
994 : 2 : if ((item.extra_flags & TEST_CREATE) == TEST_CREATE)
995 : 0 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
996 : : else
997 : 2 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
998 : : }
999 : : else
1000 : : {
1001 : 7 : g_assert_nonnull (os);
1002 : 7 : g_assert_no_error (error);
1003 : : }
1004 : :
1005 : 13 : if (error)
1006 : 6 : g_error_free (error);
1007 : :
1008 : 13 : if (os)
1009 : : {
1010 : 7 : error = NULL;
1011 : : res =
1012 : 7 : g_output_stream_close (G_OUTPUT_STREAM (os), NULL, &error);
1013 : 7 : if (error)
1014 : 0 : g_test_message (" g_output_stream_close: error %d = %s",
1015 : 0 : error->code, error->message);
1016 : 7 : g_assert_true (res);
1017 : 7 : g_assert_no_error (error);
1018 : 7 : g_object_unref (os);
1019 : : }
1020 : 13 : g_object_unref (child);
1021 : : }
1022 : : }
1023 : 1 : g_object_unref (root);
1024 : 1 : }
1025 : :
1026 : : static void
1027 : 1 : test_open (gconstpointer test_data)
1028 : : {
1029 : : GFile *root, *child;
1030 : : gboolean res;
1031 : : GError *error;
1032 : : guint i;
1033 : : struct StructureItem item;
1034 : : GFileInputStream *input_stream;
1035 : :
1036 : 1 : g_assert_nonnull (test_data);
1037 : :
1038 : 1 : root = g_file_new_for_commandline_arg ((char *) test_data);
1039 : 1 : g_assert_nonnull (root);
1040 : 1 : res = g_file_query_exists (root, NULL);
1041 : 1 : g_assert_true (res);
1042 : :
1043 : 29 : for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
1044 : : {
1045 : 28 : item = sample_struct[i];
1046 : :
1047 : 28 : if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
1048 : 0 : continue;
1049 : :
1050 : 28 : if ((item.extra_flags & TEST_OPEN) == TEST_OPEN)
1051 : : {
1052 : 12 : g_test_message (" test_open: '%s'", item.filename);
1053 : :
1054 : 12 : child = g_file_get_child (root, item.filename);
1055 : 12 : g_assert_nonnull (child);
1056 : 12 : error = NULL;
1057 : 12 : input_stream = g_file_read (child, NULL, &error);
1058 : :
1059 : 12 : if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
1060 : 10 : ((item.extra_flags & TEST_INVALID_SYMLINK) ==
1061 : : TEST_INVALID_SYMLINK))
1062 : : {
1063 : 3 : g_assert_null (input_stream);
1064 : 3 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
1065 : : }
1066 : 9 : else if (item.file_type == G_FILE_TYPE_DIRECTORY)
1067 : : {
1068 : 2 : g_assert_null (input_stream);
1069 : 2 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
1070 : : }
1071 : : else
1072 : : {
1073 : 7 : g_assert_nonnull (input_stream);
1074 : 7 : g_assert_no_error (error);
1075 : : }
1076 : :
1077 : 12 : if (error)
1078 : 5 : g_error_free (error);
1079 : :
1080 : 12 : if (input_stream)
1081 : : {
1082 : 7 : error = NULL;
1083 : : res =
1084 : 7 : g_input_stream_close (G_INPUT_STREAM (input_stream), NULL,
1085 : : &error);
1086 : 7 : g_assert_true (res);
1087 : 7 : g_assert_no_error (error);
1088 : 7 : g_object_unref (input_stream);
1089 : : }
1090 : 12 : g_object_unref (child);
1091 : : }
1092 : : }
1093 : 1 : g_object_unref (root);
1094 : 1 : }
1095 : :
1096 : : static void
1097 : 1 : test_delete (gconstpointer test_data)
1098 : : {
1099 : : GFile *root;
1100 : : GFile *child;
1101 : : gboolean res;
1102 : : GError *error;
1103 : : guint i;
1104 : : struct StructureItem item;
1105 : : gchar *path;
1106 : :
1107 : 1 : g_assert_nonnull (test_data);
1108 : :
1109 : 1 : root = g_file_new_for_commandline_arg ((char *) test_data);
1110 : 1 : g_assert_nonnull (root);
1111 : 1 : res = g_file_query_exists (root, NULL);
1112 : 1 : g_assert_true (res);
1113 : :
1114 : 29 : for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
1115 : : {
1116 : 28 : item = sample_struct[i];
1117 : :
1118 : 28 : if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
1119 : 0 : continue;
1120 : :
1121 : 28 : if (((item.extra_flags & TEST_DELETE_NORMAL) == TEST_DELETE_NORMAL) ||
1122 : 21 : ((item.extra_flags & TEST_DELETE_TRASH) == TEST_DELETE_TRASH))
1123 : : {
1124 : 9 : child = file_exists (root, item.filename, &res);
1125 : 9 : g_assert_nonnull (child);
1126 : : /* we don't care about result here */
1127 : :
1128 : 9 : path = g_file_get_path (child);
1129 : 9 : g_test_message (" Deleting %s, path = %s", item.filename, path);
1130 : 9 : g_free (path);
1131 : :
1132 : 9 : error = NULL;
1133 : 9 : if ((item.extra_flags & TEST_DELETE_NORMAL) == TEST_DELETE_NORMAL)
1134 : 7 : res = g_file_delete (child, NULL, &error);
1135 : : else
1136 : 2 : res = g_file_trash (child, NULL, &error);
1137 : :
1138 : 9 : if ((item.extra_flags & TEST_DELETE_NON_EMPTY) ==
1139 : : TEST_DELETE_NON_EMPTY)
1140 : : {
1141 : 1 : g_assert_false (res);
1142 : 1 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_EMPTY);
1143 : : }
1144 : 9 : if ((item.extra_flags & TEST_DELETE_FAILURE) == TEST_DELETE_FAILURE)
1145 : : {
1146 : 1 : g_assert_false (res);
1147 : 1 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
1148 : : }
1149 : 9 : if ((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS)
1150 : : {
1151 : 3 : g_assert_false (res);
1152 : 3 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
1153 : : }
1154 : :
1155 : 9 : if (error)
1156 : : {
1157 : 6 : g_test_message (" result = %d, error = %s", res, error->message);
1158 : 6 : g_error_free (error);
1159 : : }
1160 : :
1161 : 9 : g_object_unref (child);
1162 : : }
1163 : : }
1164 : 1 : g_object_unref (root);
1165 : 1 : }
1166 : :
1167 : : static void
1168 : 1 : test_make_directory_with_parents (gconstpointer test_data)
1169 : : {
1170 : : GFile *root, *child, *grandchild, *greatgrandchild;
1171 : : gboolean res;
1172 : 1 : GError *error = NULL;
1173 : : #ifdef G_OS_UNIX
1174 : 1 : gboolean have_cap_dac_override = check_cap_dac_override (test_data);
1175 : : #endif
1176 : :
1177 : 1 : g_assert_nonnull (test_data);
1178 : :
1179 : 1 : root = g_file_new_for_commandline_arg ((char *) test_data);
1180 : 1 : g_assert_nonnull (root);
1181 : 1 : res = g_file_query_exists (root, NULL);
1182 : 1 : g_assert_true (res);
1183 : :
1184 : 1 : child = g_file_get_child (root, "a");
1185 : 1 : grandchild = g_file_get_child (child, "b");
1186 : 1 : greatgrandchild = g_file_get_child (grandchild, "c");
1187 : :
1188 : : /* Check that we can successfully make directory hierarchies of
1189 : : * depth 1, 2, or 3
1190 : : */
1191 : 1 : res = g_file_make_directory_with_parents (child, NULL, &error);
1192 : 1 : g_assert_true (res);
1193 : 1 : g_assert_no_error (error);
1194 : 1 : res = g_file_query_exists (child, NULL);
1195 : 1 : g_assert_true (res);
1196 : :
1197 : 1 : g_file_delete (child, NULL, NULL);
1198 : :
1199 : 1 : res = g_file_make_directory_with_parents (grandchild, NULL, &error);
1200 : 1 : g_assert_true (res);
1201 : 1 : g_assert_no_error (error);
1202 : 1 : res = g_file_query_exists (grandchild, NULL);
1203 : 1 : g_assert_true (res);
1204 : :
1205 : 1 : g_file_delete (grandchild, NULL, NULL);
1206 : 1 : g_file_delete (child, NULL, NULL);
1207 : :
1208 : 1 : res = g_file_make_directory_with_parents (greatgrandchild, NULL, &error);
1209 : 1 : g_assert_true (res);
1210 : 1 : g_assert_no_error (error);
1211 : 1 : res = g_file_query_exists (greatgrandchild, NULL);
1212 : 1 : g_assert_true (res);
1213 : :
1214 : 1 : g_file_delete (greatgrandchild, NULL, NULL);
1215 : 1 : g_file_delete (grandchild, NULL, NULL);
1216 : 1 : g_file_delete (child, NULL, NULL);
1217 : :
1218 : : /* Now test failure by trying to create a directory hierarchy
1219 : : * where a ancestor exists but is read-only
1220 : : */
1221 : :
1222 : : /* No obvious way to do this on Windows */
1223 : 1 : if (!posix_compat)
1224 : 0 : goto out;
1225 : :
1226 : : #ifdef G_OS_UNIX
1227 : : /* Permissions are ignored if we have CAP_DAC_OVERRIDE or equivalent,
1228 : : * and in particular if we're root */
1229 : 1 : if (have_cap_dac_override)
1230 : : {
1231 : 0 : g_test_skip ("Unable to exercise g_file_make_directory_with_parents "
1232 : : "failing with EACCES: we probably have "
1233 : : "CAP_DAC_OVERRIDE");
1234 : 0 : goto out;
1235 : : }
1236 : : #endif
1237 : :
1238 : 1 : g_file_make_directory (child, NULL, NULL);
1239 : 1 : g_assert_true (res);
1240 : :
1241 : 1 : res = g_file_set_attribute_uint32 (child,
1242 : : G_FILE_ATTRIBUTE_UNIX_MODE,
1243 : : S_IRUSR + S_IXUSR, /* -r-x------ */
1244 : : G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
1245 : : NULL, NULL);
1246 : 1 : g_assert_true (res);
1247 : :
1248 : 1 : res = g_file_make_directory_with_parents (grandchild, NULL, &error);
1249 : 1 : g_assert_false (res);
1250 : 1 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED);
1251 : 1 : g_clear_error (&error);
1252 : :
1253 : 1 : res = g_file_make_directory_with_parents (greatgrandchild, NULL, &error);
1254 : 1 : g_assert_false (res);
1255 : 1 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED);
1256 : 1 : g_clear_error (&error);
1257 : :
1258 : 1 : out:
1259 : 1 : g_object_unref (greatgrandchild);
1260 : 1 : g_object_unref (grandchild);
1261 : 1 : g_object_unref (child);
1262 : 1 : g_object_unref (root);
1263 : 1 : }
1264 : :
1265 : :
1266 : : static void
1267 : 10 : cleanup_dir_recurse (GFile *parent, GFile *root)
1268 : : {
1269 : : gboolean res;
1270 : : GError *error;
1271 : : GFileEnumerator *enumerator;
1272 : : GFileInfo *info;
1273 : : GFile *descend;
1274 : : char *relative_path;
1275 : :
1276 : 10 : g_assert_nonnull (root);
1277 : :
1278 : : enumerator =
1279 : 10 : g_file_enumerate_children (parent, "*",
1280 : : G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
1281 : : NULL);
1282 : 10 : if (! enumerator)
1283 : 1 : return;
1284 : :
1285 : 9 : error = NULL;
1286 : 9 : info = g_file_enumerator_next_file (enumerator, NULL, &error);
1287 : 42 : while ((info) && (!error))
1288 : : {
1289 : 33 : descend = g_file_enumerator_get_child (enumerator, info);
1290 : 33 : g_assert_nonnull (descend);
1291 : 33 : relative_path = g_file_get_relative_path (root, descend);
1292 : 33 : g_assert_nonnull (relative_path);
1293 : 33 : g_free (relative_path);
1294 : :
1295 : 33 : g_test_message (" deleting '%s'", g_file_info_get_display_name (info));
1296 : :
1297 : 33 : if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
1298 : 8 : cleanup_dir_recurse (descend, root);
1299 : :
1300 : 33 : error = NULL;
1301 : 33 : res = g_file_delete (descend, NULL, &error);
1302 : 33 : g_assert_true (res);
1303 : :
1304 : 33 : g_object_unref (descend);
1305 : 33 : error = NULL;
1306 : 33 : g_object_unref (info);
1307 : :
1308 : 33 : info = g_file_enumerator_next_file (enumerator, NULL, &error);
1309 : : }
1310 : 9 : g_assert_no_error (error);
1311 : :
1312 : 9 : error = NULL;
1313 : 9 : res = g_file_enumerator_close (enumerator, NULL, &error);
1314 : 9 : g_assert_true (res);
1315 : 9 : g_assert_no_error (error);
1316 : :
1317 : 9 : g_object_unref (enumerator);
1318 : : }
1319 : :
1320 : : static void
1321 : 2 : prep_clean_structure (gconstpointer test_data)
1322 : : {
1323 : : GFile *root;
1324 : :
1325 : 2 : g_assert_nonnull (test_data);
1326 : 2 : g_test_message (" Cleaning target testing structure in '%s'...",
1327 : : (char *) test_data);
1328 : :
1329 : 2 : root = g_file_new_for_commandline_arg ((char *) test_data);
1330 : 2 : g_assert_nonnull (root);
1331 : :
1332 : 2 : cleanup_dir_recurse (root, root);
1333 : :
1334 : 2 : g_file_delete (root, NULL, NULL);
1335 : :
1336 : 2 : g_object_unref (root);
1337 : 2 : }
1338 : :
1339 : : int
1340 : 1 : main (int argc, char *argv[])
1341 : : {
1342 : : static gboolean only_create_struct;
1343 : 1 : char *target_path = NULL;
1344 : : GError *error;
1345 : : GOptionContext *context;
1346 : : int retval;
1347 : :
1348 : : static GOptionEntry cmd_entries[] = {
1349 : : {"read-write", 'w', 0, G_OPTION_ARG_NONE, &write_test,
1350 : : "Perform write tests (incl. structure creation)", NULL},
1351 : : {"create-struct", 'c', 0, G_OPTION_ARG_NONE, &only_create_struct,
1352 : : "Only create testing structure (no tests)", NULL},
1353 : : {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL},
1354 : : {"posix", 'x', 0, G_OPTION_ARG_NONE, &posix_compat,
1355 : : "Test POSIX-specific features (unix permissions, symlinks)", NULL},
1356 : : G_OPTION_ENTRY_NULL
1357 : : };
1358 : :
1359 : 1 : test_suite = FALSE;
1360 : 1 : verbose = FALSE;
1361 : 1 : write_test = FALSE;
1362 : 1 : only_create_struct = FALSE;
1363 : 1 : target_path = NULL;
1364 : 1 : posix_compat = FALSE;
1365 : :
1366 : : /* strip all gtester-specific args */
1367 : 1 : g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
1368 : :
1369 : : /* no extra parameters specified, assume we're executed from glib test suite */
1370 : 1 : if (argc < 2)
1371 : : {
1372 : 1 : test_suite = TRUE;
1373 : 1 : verbose = TRUE;
1374 : 1 : write_test = TRUE;
1375 : 1 : only_create_struct = FALSE;
1376 : 1 : target_path = g_build_filename (g_get_tmp_dir (), "testdir_live-g-file", NULL);
1377 : : #ifdef G_PLATFORM_WIN32
1378 : : posix_compat = FALSE;
1379 : : #else
1380 : 1 : posix_compat = TRUE;
1381 : : #endif
1382 : : }
1383 : :
1384 : : /* add trailing args */
1385 : 1 : error = NULL;
1386 : 1 : context = g_option_context_new ("target_path");
1387 : 1 : g_option_context_add_main_entries (context, cmd_entries, NULL);
1388 : 1 : if (!g_option_context_parse (context, &argc, &argv, &error))
1389 : : {
1390 : 0 : g_printerr ("option parsing failed: %s\n", error->message);
1391 : 0 : return g_test_run ();
1392 : : }
1393 : :
1394 : : /* remaining arg should is the target path; we don't care of the extra args here */
1395 : 1 : if (argc >= 2)
1396 : 0 : target_path = g_strdup (argv[1]);
1397 : :
1398 : 1 : if (! target_path)
1399 : : {
1400 : 0 : g_printerr ("error: target path was not specified\n");
1401 : 0 : g_printerr ("%s", g_option_context_get_help (context, TRUE, NULL));
1402 : 0 : return g_test_run ();
1403 : : }
1404 : :
1405 : 1 : g_option_context_free (context);
1406 : :
1407 : : /* Write test - clean target directory first */
1408 : : /* this can be also considered as a test - enumerate + delete */
1409 : 1 : if (write_test || only_create_struct)
1410 : 1 : g_test_add_data_func ("/live-g-file/prep_clean_structure", target_path,
1411 : : prep_clean_structure);
1412 : :
1413 : : /* Write test - create new testing structure */
1414 : 1 : if (write_test || only_create_struct)
1415 : 1 : g_test_add_data_func ("/live-g-file/create_structure", target_path,
1416 : : test_create_structure);
1417 : :
1418 : : /* Read test - test the sample structure - expect defined attributes to be there */
1419 : 1 : if (!only_create_struct)
1420 : 1 : g_test_add_data_func ("/live-g-file/test_initial_structure", target_path,
1421 : : test_initial_structure);
1422 : :
1423 : : /* Read test - test traverse the structure - no special file should appear */
1424 : 1 : if (!only_create_struct)
1425 : 1 : g_test_add_data_func ("/live-g-file/test_traverse_structure", target_path,
1426 : : test_traverse_structure);
1427 : :
1428 : : /* Read test - enumerate */
1429 : 1 : if (!only_create_struct)
1430 : 1 : g_test_add_data_func ("/live-g-file/test_enumerate", target_path,
1431 : : test_enumerate);
1432 : :
1433 : : /* Read test - open (g_file_read()) */
1434 : 1 : if (!only_create_struct)
1435 : 1 : g_test_add_data_func ("/live-g-file/test_open", target_path, test_open);
1436 : :
1437 : 1 : if (posix_compat)
1438 : : {
1439 : 1 : g_test_add_data_func ("/live-g-file/test_unix_is_mountpoint/sysroot",
1440 : : "/",
1441 : : test_unix_is_mountpoint);
1442 : : #ifdef __linux__
1443 : 1 : g_test_add_data_func ("/live-g-file/test_unix_is_mountpoint/proc",
1444 : : "/proc",
1445 : : test_unix_is_mountpoint);
1446 : : #endif
1447 : : }
1448 : :
1449 : : /* Write test - create */
1450 : 1 : if (write_test && (!only_create_struct))
1451 : 1 : g_test_add_data_func ("/live-g-file/test_create", target_path,
1452 : : test_create);
1453 : :
1454 : : /* Write test - copy, move */
1455 : 1 : if (write_test && (!only_create_struct))
1456 : 1 : g_test_add_data_func ("/live-g-file/test_copy_move", target_path,
1457 : : test_copy_move);
1458 : :
1459 : : /* Write test - delete, trash */
1460 : 1 : if (write_test && (!only_create_struct))
1461 : 1 : g_test_add_data_func ("/live-g-file/test_delete", target_path,
1462 : : test_delete);
1463 : :
1464 : : /* Write test - make_directory_with_parents */
1465 : 1 : if (write_test && (!only_create_struct))
1466 : 1 : g_test_add_data_func ("/live-g-file/test_make_directory_with_parents", target_path,
1467 : : test_make_directory_with_parents);
1468 : :
1469 : 1 : if (write_test || only_create_struct)
1470 : 1 : g_test_add_data_func ("/live-g-file/final_clean", target_path,
1471 : : prep_clean_structure);
1472 : :
1473 : 1 : retval = g_test_run ();
1474 : :
1475 : 1 : g_free (target_path);
1476 : :
1477 : 1 : return retval;
1478 : : }
|