1 /* GLib testing framework examples and tests
2 * Copyright (C) 2008 Red Hat, Inc.
3 * Authors: Tomas Bzatek <tbzatek@redhat.com>
5 * This work is provided "as is"; redistribution and modification
6 * in whole or in part, in any medium, physical or electronic is
7 * permitted without restriction.
9 * This work is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * In no event shall the authors or contributors be liable for any
14 * direct, indirect, incidental, special, exemplary, or consequential
15 * damages (including, but not limited to, procurement of substitute
16 * goods or services; loss of use, data, or profits; or business
17 * interruption) however caused and on any theory of liability, whether
18 * in contract, strict liability, or tort (including negligence or
19 * otherwise) arising in any way out of the use of this software, even
20 * if advised of the possibility of such damage.
23 #include <glib/glib.h>
27 #include <sys/types.h>
31 #define DEFAULT_TEST_DIR "testdir_live-g-file"
33 #define PATTERN_FILE_SIZE 0x10000
34 #define TEST_HANDLE_SPECIAL TRUE
36 enum StructureExtraFlags
38 TEST_DELETE_NORMAL = 1 << 0,
39 TEST_DELETE_TRASH = 1 << 1,
40 TEST_DELETE_NON_EMPTY = 1 << 2,
41 TEST_DELETE_FAILURE = 1 << 3,
42 TEST_NOT_EXISTS = 1 << 4,
43 TEST_ENUMERATE_FILE = 1 << 5,
44 TEST_NO_ACCESS = 1 << 6,
47 TEST_COPY_ERROR_RECURSE = 1 << 9,
48 TEST_ALREADY_EXISTS = 1 << 10,
49 TEST_TARGET_IS_FILE = 1 << 11,
50 TEST_CREATE = 1 << 12,
51 TEST_REPLACE = 1 << 13,
52 TEST_APPEND = 1 << 14,
54 TEST_OVERWRITE = 1 << 16,
55 TEST_INVALID_SYMLINK = 1 << 17,
63 GFileCreateFlags create_flags;
65 gboolean handle_special;
66 enum StructureExtraFlags extra_flags;
69 #define TEST_DIR_NO_ACCESS "dir_no-access"
70 #define TEST_DIR_NO_WRITE "dir_no-write"
71 #define TEST_DIR_TARGET "dir-target"
72 #define TEST_NAME_NOT_EXISTS "not_exists"
73 #define TEST_TARGET_FILE "target-file"
76 static const struct StructureItem sample_struct[] = {
77 /* filename link file_type create_flags mode | handle_special | extra_flags */
78 {"dir1", NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_DELETE_NORMAL | TEST_DELETE_NON_EMPTY | TEST_REPLACE | TEST_OPEN},
79 {"dir1/subdir", NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_COPY_ERROR_RECURSE | TEST_APPEND},
80 {"dir2", NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_DELETE_NORMAL | TEST_MOVE | TEST_CREATE},
81 {TEST_DIR_TARGET, NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_COPY_ERROR_RECURSE},
82 {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},
83 {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},
84 {TEST_TARGET_FILE, NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_OPEN},
85 {"normal_file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_ENUMERATE_FILE | TEST_CREATE | TEST_OVERWRITE},
86 {"normal_file-symlink", "normal_file", G_FILE_TYPE_SYMBOLIC_LINK, G_FILE_CREATE_NONE, 0, 0, TEST_ENUMERATE_FILE | TEST_COPY | TEST_OPEN},
87 {"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},
88 {"private_file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_PRIVATE, 0, 0, TEST_COPY | TEST_OPEN | TEST_OVERWRITE | TEST_APPEND},
89 {"normal_file2", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_OVERWRITE | TEST_REPLACE},
90 {"readonly_file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, S_IRUSR + S_IRGRP + S_IROTH, 0, TEST_DELETE_NORMAL | TEST_OPEN},
91 {"UTF_pr\xcc\x8ci\xcc\x81lis\xcc\x8c z",
92 NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_CREATE | TEST_OPEN | TEST_OVERWRITE},
93 {"dir_pr\xcc\x8ci\xcc\x81lis\xcc\x8c z",
94 NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_DELETE_NORMAL | TEST_CREATE},
95 {"pattern_file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_COPY | TEST_OPEN | TEST_APPEND},
96 {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},
97 {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},
98 {"not_exists2", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_NOT_EXISTS | TEST_CREATE},
99 {"not_exists3", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_NOT_EXISTS | TEST_REPLACE},
100 {"not_exists4", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_NOT_EXISTS | TEST_APPEND},
101 {"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},
102 {"lost_symlink", "nowhere", G_FILE_TYPE_SYMBOLIC_LINK, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_DELETE_NORMAL | TEST_OPEN | TEST_INVALID_SYMLINK},
105 static gboolean test_suite;
106 static gboolean write_test;
107 static gboolean verbose;
108 static gboolean posix_compat;
110 #ifdef G_HAVE_ISO_VARARGS
111 #define log(...) if (verbose) g_print (__VA_ARGS__)
112 #elif defined(G_HAVE_GNUC_VARARGS)
113 #define log(msg...) if (verbose) g_print (msg)
114 #else /* no varargs macros */
115 static void log (const g_char *format, ...)
118 va_start (args, format);
119 if (verbose) g_print (format, args);
125 create_empty_file (GFile * parent, const char *filename,
126 GFileCreateFlags create_flags)
131 GFileOutputStream *outs;
133 child = g_file_get_child (parent, filename);
134 g_assert (child != NULL);
137 outs = g_file_replace (child, NULL, FALSE, create_flags, NULL, &error);
138 g_assert_no_error (error);
139 g_assert (outs != NULL);
141 res = g_output_stream_close (G_OUTPUT_STREAM (outs), NULL, &error);
142 g_object_unref (outs);
147 create_empty_dir (GFile * parent, const char *filename)
153 child = g_file_get_child (parent, filename);
154 g_assert (child != NULL);
156 res = g_file_make_directory (child, NULL, &error);
157 g_assert_cmpint (res, ==, TRUE);
158 g_assert_no_error (error);
163 create_symlink (GFile * parent, const char *filename, const char *points_to)
169 child = g_file_get_child (parent, filename);
170 g_assert (child != NULL);
172 res = g_file_make_symbolic_link (child, points_to, NULL, &error);
173 g_assert_cmpint (res, ==, TRUE);
174 g_assert_no_error (error);
179 test_create_structure (gconstpointer test_data)
185 GFileOutputStream *outs;
186 GDataOutputStream *outds;
188 struct StructureItem item;
190 g_assert (test_data != NULL);
191 log ("\n Going to create testing structure in '%s'...\n",
194 root = g_file_new_for_commandline_arg ((char *) test_data);
195 g_assert (root != NULL);
197 /* create root directory */
198 res = g_file_make_directory (root, NULL, NULL);
199 /* don't care about errors here */
201 /* create any other items */
202 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
204 item = sample_struct[i];
205 if ((item.handle_special)
207 && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK)))
211 switch (item.file_type)
213 case G_FILE_TYPE_REGULAR:
214 log (" Creating file '%s'...\n", item.filename);
215 child = create_empty_file (root, item.filename, item.create_flags);
217 case G_FILE_TYPE_DIRECTORY:
218 log (" Creating directory '%s'...\n", item.filename);
219 child = create_empty_dir (root, item.filename);
221 case G_FILE_TYPE_SYMBOLIC_LINK:
222 log (" Creating symlink '%s' --> '%s'...\n", item.filename,
224 child = create_symlink (root, item.filename, item.link_to);
226 case G_FILE_TYPE_UNKNOWN:
227 case G_FILE_TYPE_SPECIAL:
228 case G_FILE_TYPE_SHORTCUT:
229 case G_FILE_TYPE_MOUNTABLE:
233 g_assert (child != NULL);
235 if ((item.mode > 0) && (posix_compat))
239 g_file_set_attribute_uint32 (child, G_FILE_ATTRIBUTE_UNIX_MODE,
241 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
243 g_assert_cmpint (res, ==, TRUE);
244 g_assert_no_error (error);
247 g_object_unref (child);
250 /* create a pattern file */
251 log (" Creating pattern file...");
252 child = g_file_get_child (root, "pattern_file");
253 g_assert (child != NULL);
257 g_file_replace (child, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error);
258 g_assert_no_error (error);
260 g_assert (outs != NULL);
261 outds = g_data_output_stream_new (G_OUTPUT_STREAM (outs));
262 g_assert (outds != NULL);
263 for (i = 0; i < PATTERN_FILE_SIZE; i++)
266 res = g_data_output_stream_put_byte (outds, i % 256, NULL, &error);
267 g_assert_no_error (error);
270 res = g_output_stream_close (G_OUTPUT_STREAM (outs), NULL, &error);
271 g_assert_no_error (error);
272 g_object_unref (outds);
273 g_object_unref (outs);
274 g_object_unref (child);
277 g_object_unref (root);
281 file_exists (GFile * parent, const char *filename, gboolean * result)
289 child = g_file_get_child (parent, filename);
290 g_assert (child != NULL);
291 res = g_file_query_exists (child, NULL);
299 test_attributes (struct StructureItem item, GFileInfo * info)
303 const char *name, *display_name, *edit_name, *copy_name, *symlink_target;
307 gboolean can_read, can_write;
310 has_attr = g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE);
311 g_assert_cmpint (has_attr, ==, TRUE);
312 ftype = g_file_info_get_file_type (info);
313 g_assert_cmpint (ftype, !=, G_FILE_TYPE_UNKNOWN);
314 g_assert_cmpint (ftype, ==, item.file_type);
317 if ((item.mode > 0) && (posix_compat))
320 g_file_info_get_attribute_uint32 (info,
321 G_FILE_ATTRIBUTE_UNIX_MODE) & 0xFFF;
322 g_assert_cmpint (mode, ==, item.mode);
325 /* access::can-read */
326 if (item.file_type != G_FILE_TYPE_SYMBOLIC_LINK)
329 g_file_info_get_attribute_boolean (info,
330 G_FILE_ATTRIBUTE_ACCESS_CAN_READ);
331 g_assert_cmpint (can_read, ==, TRUE);
334 /* access::can-write */
335 if ((write_test) && ((item.extra_flags & TEST_OVERWRITE) == TEST_OVERWRITE))
338 g_file_info_get_attribute_boolean (info,
339 G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE);
340 g_assert_cmpint (can_write, ==, TRUE);
344 name = g_file_info_get_name (info);
345 g_assert (name != NULL);
347 /* standard::display-name */
348 display_name = g_file_info_get_display_name (info);
349 g_assert (display_name != NULL);
350 utf8_valid = g_utf8_validate (display_name, -1, NULL);
351 g_assert_cmpint (utf8_valid, ==, TRUE);
353 /* standard::edit-name */
354 edit_name = g_file_info_get_edit_name (info);
357 utf8_valid = g_utf8_validate (edit_name, -1, NULL);
358 g_assert_cmpint (utf8_valid, ==, TRUE);
361 /* standard::copy-name */
363 g_file_info_get_attribute_string (info,
364 G_FILE_ATTRIBUTE_STANDARD_COPY_NAME);
367 utf8_valid = g_utf8_validate (copy_name, -1, NULL);
368 g_assert_cmpint (utf8_valid, ==, TRUE);
371 /* standard::is-symlink */
374 is_symlink = g_file_info_get_is_symlink (info);
375 g_assert_cmpint (is_symlink, ==,
376 item.file_type == G_FILE_TYPE_SYMBOLIC_LINK);
379 /* standard::symlink-target */
380 if ((item.file_type == G_FILE_TYPE_SYMBOLIC_LINK) && (posix_compat))
382 symlink_target = g_file_info_get_symlink_target (info);
383 g_assert_cmpstr (symlink_target, ==, item.link_to);
388 test_initial_structure (gconstpointer test_data)
394 GFileInputStream *ins;
399 gssize read, total_read;
400 struct StructureItem item;
403 g_assert (test_data != NULL);
404 log ("\n Testing sample structure in '%s'...\n", (char *) test_data);
406 root = g_file_new_for_commandline_arg ((char *) test_data);
407 g_assert (root != NULL);
408 res = g_file_query_exists (root, NULL);
409 g_assert_cmpint (res, ==, TRUE);
411 /* test the structure */
412 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
414 item = sample_struct[i];
415 if (((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
416 || (item.handle_special))
419 log (" Testing file '%s'...\n", item.filename);
421 child = file_exists (root, item.filename, &res);
422 g_assert (child != NULL);
423 g_assert_cmpint (res, ==, TRUE);
427 g_file_query_info (child, "*", G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
429 g_assert_no_error (error);
430 g_assert (info != NULL);
432 test_attributes (item, info);
434 g_object_unref (child);
437 /* read and test the pattern file */
438 log (" Testing pattern file...\n");
439 child = file_exists (root, "pattern_file", &res);
440 g_assert (child != NULL);
441 g_assert_cmpint (res, ==, TRUE);
445 g_file_query_info (child, "*", G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
447 g_assert_no_error (error);
448 g_assert (info != NULL);
449 size = g_file_info_get_size (info);
450 g_assert_cmpint (size, ==, PATTERN_FILE_SIZE);
453 ins = g_file_read (child, NULL, &error);
454 g_assert (ins != NULL);
455 g_assert_no_error (error);
457 buffer = g_malloc (PATTERN_FILE_SIZE);
460 while (total_read < PATTERN_FILE_SIZE)
464 g_input_stream_read (G_INPUT_STREAM (ins), buffer + total_read,
465 PATTERN_FILE_SIZE, NULL, &error);
466 g_assert_no_error (error);
468 log (" read %"G_GSSIZE_FORMAT" bytes, total = %"G_GSSIZE_FORMAT" of %d.\n",
469 read, total_read, PATTERN_FILE_SIZE);
471 g_assert_cmpint (total_read, ==, PATTERN_FILE_SIZE);
474 res = g_input_stream_close (G_INPUT_STREAM (ins), NULL, &error);
475 g_assert_no_error (error);
476 g_assert_cmpint (res, ==, TRUE);
478 for (i = 0; i < PATTERN_FILE_SIZE; i++)
479 g_assert_cmpint (*(buffer + i), ==, i % 256);
481 g_object_unref (ins);
482 g_object_unref (child);
484 g_object_unref (root);
488 traverse_recurse_dirs (GFile * parent, GFile * root)
492 GFileEnumerator *enumerator;
499 g_assert (root != NULL);
503 g_file_enumerate_children (parent, "*",
504 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
506 g_assert (enumerator != NULL);
507 g_assert_no_error (error);
509 g_assert (g_file_enumerator_get_container (enumerator) == parent);
512 info = g_file_enumerator_next_file (enumerator, NULL, &error);
513 while ((info) && (!error))
515 descend = g_file_get_child (parent, g_file_info_get_name (info));
516 g_assert (descend != NULL);
517 relative_path = g_file_get_relative_path (root, descend);
518 g_assert (relative_path != NULL);
521 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
523 if (strcmp (sample_struct[i].filename, relative_path) == 0)
525 /* test the attributes again */
526 test_attributes (sample_struct[i], info);
532 g_assert_cmpint (found, ==, TRUE);
534 log (" Found file %s, relative to root: %s\n",
535 g_file_info_get_display_name (info), relative_path);
537 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
538 traverse_recurse_dirs (descend, root);
540 g_object_unref (descend);
542 info = g_file_enumerator_next_file (enumerator, NULL, &error);
544 g_assert_no_error (error);
547 res = g_file_enumerator_close (enumerator, NULL, &error);
548 g_assert_cmpint (res, ==, TRUE);
549 g_assert_no_error (error);
550 g_assert (g_file_enumerator_is_closed (enumerator));
552 g_object_unref (enumerator);
556 test_traverse_structure (gconstpointer test_data)
561 g_assert (test_data != NULL);
562 log ("\n Traversing through the sample structure in '%s'...\n",
565 root = g_file_new_for_commandline_arg ((char *) test_data);
566 g_assert (root != NULL);
567 res = g_file_query_exists (root, NULL);
568 g_assert_cmpint (res, ==, TRUE);
570 traverse_recurse_dirs (root, root);
572 g_object_unref (root);
579 test_enumerate (gconstpointer test_data)
584 GFileEnumerator *enumerator;
587 struct StructureItem item;
590 g_assert (test_data != NULL);
591 log ("\n Test enumerate '%s'...\n", (char *) test_data);
593 root = g_file_new_for_commandline_arg ((char *) test_data);
594 g_assert (root != NULL);
595 res = g_file_query_exists (root, NULL);
596 g_assert_cmpint (res, ==, TRUE);
599 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
601 item = sample_struct[i];
602 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
605 if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
606 (((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS)
608 || ((item.extra_flags & TEST_ENUMERATE_FILE) ==
609 TEST_ENUMERATE_FILE))
611 log (" Testing file '%s'\n", item.filename);
612 child = g_file_get_child (root, item.filename);
613 g_assert (child != NULL);
616 g_file_enumerate_children (child, "*",
617 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
620 if ((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS)
622 g_assert (enumerator == NULL);
623 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
625 if ((item.extra_flags & TEST_ENUMERATE_FILE) == TEST_ENUMERATE_FILE)
627 g_assert (enumerator == NULL);
628 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY);
630 if ((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS)
632 g_assert (enumerator != NULL);
635 info = g_file_enumerator_next_file (enumerator, NULL, &error);
636 g_assert (info == NULL);
637 g_assert_no_error (error);
638 /* no items should be found, no error should be logged */
642 g_error_free (error);
647 res = g_file_enumerator_close (enumerator, NULL, &error);
648 g_assert_cmpint (res, ==, TRUE);
649 g_assert_no_error (error);
651 g_object_unref (enumerator);
653 g_object_unref (child);
656 g_object_unref (root);
660 do_copy_move (GFile * root, struct StructureItem item, const char *target_dir,
661 enum StructureExtraFlags extra_flags)
663 GFile *dst_dir, *src_file, *dst_file;
667 log (" do_copy_move: '%s' --> '%s'\n", item.filename, target_dir);
669 dst_dir = g_file_get_child (root, target_dir);
670 g_assert (dst_dir != NULL);
671 src_file = g_file_get_child (root, item.filename);
672 g_assert (src_file != NULL);
673 dst_file = g_file_get_child (dst_dir, item.filename);
674 g_assert (dst_file != NULL);
677 if ((item.extra_flags & TEST_COPY) == TEST_COPY)
679 g_file_copy (src_file, dst_file,
680 G_FILE_COPY_NOFOLLOW_SYMLINKS |
682 TEST_OVERWRITE) ? G_FILE_COPY_OVERWRITE :
683 G_FILE_COPY_NONE), NULL, NULL, NULL, &error);
686 g_file_move (src_file, dst_file, G_FILE_COPY_NOFOLLOW_SYMLINKS, NULL,
690 log (" res = %d, error code %d = %s\n", res, error->code,
693 /* copying file/directory to itself (".") */
694 if (((item.extra_flags & TEST_NOT_EXISTS) != TEST_NOT_EXISTS) &&
695 (extra_flags == TEST_ALREADY_EXISTS))
697 g_assert_cmpint (res, ==, FALSE);
698 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
700 /* target file is a file, overwrite is not set */
701 else if (((item.extra_flags & TEST_NOT_EXISTS) != TEST_NOT_EXISTS) &&
702 (extra_flags == TEST_TARGET_IS_FILE))
704 g_assert_cmpint (res, ==, FALSE);
705 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY);
707 /* source file is directory */
708 else if ((item.extra_flags & TEST_COPY_ERROR_RECURSE) ==
709 TEST_COPY_ERROR_RECURSE)
711 g_assert_cmpint (res, ==, FALSE);
712 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE);
714 /* source or target path doesn't exist */
715 else if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
716 (extra_flags == TEST_NOT_EXISTS))
718 g_assert_cmpint (res, ==, FALSE);
719 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
721 /* source or target path permission denied */
722 else if (((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS) ||
723 (extra_flags == TEST_NO_ACCESS))
725 /* This works for root, see bug #552912 */
726 if (test_suite && getuid () == 0)
728 g_assert_cmpint (res, ==, TRUE);
729 g_assert_no_error (error);
733 g_assert_cmpint (res, ==, FALSE);
734 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED);
737 /* no error should be found, all exceptions defined above */
740 g_assert_cmpint (res, ==, TRUE);
741 g_assert_no_error (error);
745 g_error_free (error);
748 g_object_unref (dst_dir);
749 g_object_unref (src_file);
750 g_object_unref (dst_file);
754 test_copy_move (gconstpointer test_data)
759 struct StructureItem item;
763 g_assert (test_data != NULL);
764 root = g_file_new_for_commandline_arg ((char *) test_data);
765 g_assert (root != NULL);
766 res = g_file_query_exists (root, NULL);
767 g_assert_cmpint (res, ==, TRUE);
770 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
772 item = sample_struct[i];
774 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
777 if (((item.extra_flags & TEST_COPY) == TEST_COPY) ||
778 ((item.extra_flags & TEST_MOVE) == TEST_MOVE))
780 /* test copy/move to a directory, expecting no errors if source files exist */
781 do_copy_move (root, item, TEST_DIR_TARGET, 0);
783 /* some files have been already moved so we can't count with them in the tests */
784 if ((item.extra_flags & TEST_COPY) == TEST_COPY)
786 /* test overwrite for flagged files */
787 if ((item.extra_flags & TEST_OVERWRITE) == TEST_OVERWRITE)
789 do_copy_move (root, item, TEST_DIR_TARGET, TEST_OVERWRITE);
791 /* source = target, should return G_IO_ERROR_EXISTS */
792 do_copy_move (root, item, ".", TEST_ALREADY_EXISTS);
794 do_copy_move (root, item, TEST_TARGET_FILE,
795 TEST_TARGET_IS_FILE);
796 /* target path is invalid */
797 do_copy_move (root, item, TEST_NAME_NOT_EXISTS,
800 /* tests on POSIX-compatible filesystems */
803 /* target directory is not accessible (no execute flag) */
804 do_copy_move (root, item, TEST_DIR_NO_ACCESS,
806 /* target directory is readonly */
807 do_copy_move (root, item, TEST_DIR_NO_WRITE,
813 g_object_unref (root);
817 test_create (gconstpointer test_data)
823 struct StructureItem item;
824 GFileOutputStream *os;
826 g_assert (test_data != NULL);
829 root = g_file_new_for_commandline_arg ((char *) test_data);
830 g_assert (root != NULL);
831 res = g_file_query_exists (root, NULL);
832 g_assert_cmpint (res, ==, TRUE);
834 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
836 item = sample_struct[i];
838 if (((item.extra_flags & TEST_CREATE) == TEST_CREATE) ||
839 ((item.extra_flags & TEST_REPLACE) == TEST_REPLACE) ||
840 ((item.extra_flags & TEST_APPEND) == TEST_APPEND))
842 log (" test_create: '%s'\n", item.filename);
844 child = g_file_get_child (root, item.filename);
845 g_assert (child != NULL);
849 if ((item.extra_flags & TEST_CREATE) == TEST_CREATE)
850 os = g_file_create (child, item.create_flags, NULL, &error);
851 else if ((item.extra_flags & TEST_REPLACE) == TEST_REPLACE)
853 g_file_replace (child, NULL, TRUE, item.create_flags, NULL,
855 else if ((item.extra_flags & TEST_APPEND) == TEST_APPEND)
856 os = g_file_append_to (child, item.create_flags, NULL, &error);
860 log (" error code %d = %s\n", error->code, error->message);
862 if (((item.extra_flags & TEST_NOT_EXISTS) == 0) &&
863 ((item.extra_flags & TEST_CREATE) == TEST_CREATE))
865 g_assert (os == NULL);
866 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
868 else if (item.file_type == G_FILE_TYPE_DIRECTORY)
870 g_assert (os == NULL);
871 if ((item.extra_flags & TEST_CREATE) == TEST_CREATE)
872 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
874 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
878 g_assert (os != NULL);
879 g_assert_no_error (error);
883 g_error_free (error);
889 g_output_stream_close (G_OUTPUT_STREAM (os), NULL, &error);
891 log (" g_output_stream_close: error %d = %s\n",
892 error->code, error->message);
893 g_assert_cmpint (res, ==, TRUE);
894 g_assert_no_error (error);
896 g_object_unref (child);
899 g_object_unref (root);
903 test_open (gconstpointer test_data)
909 struct StructureItem item;
910 GFileInputStream *input_stream;
912 g_assert (test_data != NULL);
915 root = g_file_new_for_commandline_arg ((char *) test_data);
916 g_assert (root != NULL);
917 res = g_file_query_exists (root, NULL);
918 g_assert_cmpint (res, ==, TRUE);
920 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
922 item = sample_struct[i];
924 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
927 if ((item.extra_flags & TEST_OPEN) == TEST_OPEN)
929 log (" test_open: '%s'\n", item.filename);
931 child = g_file_get_child (root, item.filename);
932 g_assert (child != NULL);
934 input_stream = g_file_read (child, NULL, &error);
936 if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
937 ((item.extra_flags & TEST_INVALID_SYMLINK) ==
938 TEST_INVALID_SYMLINK))
940 g_assert (input_stream == NULL);
941 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
943 else if (item.file_type == G_FILE_TYPE_DIRECTORY)
945 g_assert (input_stream == NULL);
946 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
950 g_assert (input_stream != NULL);
951 g_assert_no_error (error);
955 g_error_free (error);
961 g_input_stream_close (G_INPUT_STREAM (input_stream), NULL,
963 g_assert_cmpint (res, ==, TRUE);
964 g_assert_no_error (error);
966 g_object_unref (child);
969 g_object_unref (root);
973 test_delete (gconstpointer test_data)
980 struct StructureItem item;
982 g_assert (test_data != NULL);
985 root = g_file_new_for_commandline_arg ((char *) test_data);
986 g_assert (root != NULL);
987 res = g_file_query_exists (root, NULL);
988 g_assert_cmpint (res, ==, TRUE);
990 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
992 item = sample_struct[i];
994 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
997 if (((item.extra_flags & TEST_DELETE_NORMAL) == TEST_DELETE_NORMAL) ||
998 ((item.extra_flags & TEST_DELETE_TRASH) == TEST_DELETE_TRASH))
1000 child = file_exists (root, item.filename, &res);
1001 g_assert (child != NULL);
1002 /* we don't care about result here */
1004 log (" Deleting %s, path = %s\n", item.filename,
1005 g_file_get_path (child));
1007 if ((item.extra_flags & TEST_DELETE_NORMAL) == TEST_DELETE_NORMAL)
1008 res = g_file_delete (child, NULL, &error);
1010 res = g_file_trash (child, NULL, &error);
1012 if ((item.extra_flags & TEST_DELETE_NON_EMPTY) ==
1013 TEST_DELETE_NON_EMPTY)
1015 g_assert_cmpint (res, ==, FALSE);
1016 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_EMPTY);
1018 if ((item.extra_flags & TEST_DELETE_FAILURE) == TEST_DELETE_FAILURE)
1020 g_assert_cmpint (res, ==, FALSE);
1021 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
1023 if ((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS)
1025 g_assert_cmpint (res, ==, FALSE);
1026 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
1031 log (" result = %d, error = %s\n", res, error->message);
1032 g_error_free (error);
1035 g_object_unref (child);
1038 g_object_unref (root);
1043 cleanup_dir_recurse (GFile *parent, GFile *root)
1047 GFileEnumerator *enumerator;
1050 char *relative_path;
1052 g_assert (root != NULL);
1056 g_file_enumerate_children (parent, "*",
1057 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
1063 info = g_file_enumerator_next_file (enumerator, NULL, &error);
1064 while ((info) && (!error))
1066 descend = g_file_get_child (parent, g_file_info_get_name (info));
1067 g_assert (descend != NULL);
1068 relative_path = g_file_get_relative_path (root, descend);
1069 g_assert (relative_path != NULL);
1071 log (" deleting '%s'\n", g_file_info_get_display_name (info));
1073 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
1074 cleanup_dir_recurse (descend, root);
1077 res = g_file_delete (descend, NULL, &error);
1078 g_assert_cmpint (res, ==, TRUE);
1080 g_object_unref (descend);
1082 info = g_file_enumerator_next_file (enumerator, NULL, &error);
1084 g_assert_no_error (error);
1087 res = g_file_enumerator_close (enumerator, NULL, &error);
1088 g_assert_cmpint (res, ==, TRUE);
1089 g_assert_no_error (error);
1091 g_object_unref (enumerator);
1095 prep_clean_structure (gconstpointer test_data)
1099 g_assert (test_data != NULL);
1100 log ("\n Cleaning target testing structure in '%s'...\n",
1101 (char *) test_data);
1103 root = g_file_new_for_commandline_arg ((char *) test_data);
1104 g_assert (root != NULL);
1106 cleanup_dir_recurse (root, root);
1108 g_file_delete (root, NULL, NULL);
1110 g_object_unref (root);
1114 main (int argc, char *argv[])
1116 static gboolean only_create_struct;
1117 const char *target_path;
1119 GOptionContext *context;
1121 static GOptionEntry cmd_entries[] = {
1122 {"read-write", 'w', 0, G_OPTION_ARG_NONE, &write_test,
1123 "Perform write tests (incl. structure creation)", NULL},
1124 {"create-struct", 'c', 0, G_OPTION_ARG_NONE, &only_create_struct,
1125 "Only create testing structure (no tests)", NULL},
1126 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL},
1127 {"posix", 'x', 0, G_OPTION_ARG_NONE, &posix_compat,
1128 "Test POSIX-specific features (unix permissions, symlinks)", NULL},
1135 only_create_struct = FALSE;
1137 posix_compat = FALSE;
1139 /* strip all gtester-specific args */
1141 g_test_init (&argc, &argv, NULL);
1143 /* no extra parameters specified, assume we're executed from glib test suite */
1149 only_create_struct = FALSE;
1150 target_path = DEFAULT_TEST_DIR;
1151 #ifdef G_PLATFORM_WIN32
1152 posix_compat = FALSE;
1154 posix_compat = TRUE;
1158 /* add trailing args */
1160 context = g_option_context_new ("target_path");
1161 g_option_context_add_main_entries (context, cmd_entries, NULL);
1162 if (!g_option_context_parse (context, &argc, &argv, &error))
1164 g_print ("option parsing failed: %s\n", error->message);
1165 return g_test_run ();
1168 /* remaining arg should is the target path; we don't care of the extra args here */
1170 target_path = strdup (argv[1]);
1174 g_print ("error: target path was not specified\n");
1175 g_print ("%s", g_option_context_get_help (context, TRUE, NULL));
1176 return g_test_run ();
1180 /* Write test - clean target directory first */
1181 /* this can be also considered as a test - enumerate + delete */
1182 if (write_test || only_create_struct)
1183 g_test_add_data_func ("/live-g-file/prep_clean_structure", target_path,
1184 prep_clean_structure);
1186 /* Write test - create new testing structure */
1187 if (write_test || only_create_struct)
1188 g_test_add_data_func ("/live-g-file/create_structure", target_path,
1189 test_create_structure);
1191 /* Read test - test the sample structure - expect defined attributes to be there */
1192 if (!only_create_struct)
1193 g_test_add_data_func ("/live-g-file/test_initial_structure", target_path,
1194 test_initial_structure);
1196 /* Read test - test traverse the structure - no special file should appear */
1197 if (!only_create_struct)
1198 g_test_add_data_func ("/live-g-file/test_traverse_structure", target_path,
1199 test_traverse_structure);
1201 /* Read test - enumerate */
1202 if (!only_create_struct)
1203 g_test_add_data_func ("/live-g-file/test_enumerate", target_path,
1206 /* Read test - open (g_file_read()) */
1207 if (!only_create_struct)
1208 g_test_add_data_func ("/live-g-file/test_open", target_path, test_open);
1210 /* Write test - create */
1211 if (write_test && (!only_create_struct))
1212 g_test_add_data_func ("/live-g-file/test_create", target_path,
1215 /* Write test - copy, move */
1216 if (write_test && (!only_create_struct))
1217 g_test_add_data_func ("/live-g-file/test_copy_move", target_path,
1220 /* Write test - delete, trash */
1221 if (write_test && (!only_create_struct))
1222 g_test_add_data_func ("/live-g-file/test_delete", target_path,
1225 if (write_test || only_create_struct)
1226 g_test_add_data_func ("/live-g-file/final_clean", target_path,
1227 prep_clean_structure);
1229 return g_test_run ();