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);
510 info = g_file_enumerator_next_file (enumerator, NULL, &error);
511 while ((info) && (!error))
513 descend = g_file_get_child (parent, g_file_info_get_name (info));
514 g_assert (descend != NULL);
515 relative_path = g_file_get_relative_path (root, descend);
516 g_assert (relative_path != NULL);
519 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
521 if (strcmp (sample_struct[i].filename, relative_path) == 0)
523 /* test the attributes again */
524 test_attributes (sample_struct[i], info);
530 g_assert_cmpint (found, ==, TRUE);
532 log (" Found file %s, relative to root: %s\n",
533 g_file_info_get_display_name (info), relative_path);
535 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
536 traverse_recurse_dirs (descend, root);
538 g_object_unref (descend);
540 info = g_file_enumerator_next_file (enumerator, NULL, &error);
542 g_assert_no_error (error);
545 res = g_file_enumerator_close (enumerator, NULL, &error);
546 g_assert_cmpint (res, ==, TRUE);
547 g_assert_no_error (error);
551 test_traverse_structure (gconstpointer test_data)
556 g_assert (test_data != NULL);
557 log ("\n Traversing through the sample structure in '%s'...\n",
560 root = g_file_new_for_commandline_arg ((char *) test_data);
561 g_assert (root != NULL);
562 res = g_file_query_exists (root, NULL);
563 g_assert_cmpint (res, ==, TRUE);
565 traverse_recurse_dirs (root, root);
567 g_object_unref (root);
574 test_enumerate (gconstpointer test_data)
579 GFileEnumerator *enumerator;
582 struct StructureItem item;
585 g_assert (test_data != NULL);
586 log ("\n Test enumerate '%s'...\n", (char *) test_data);
588 root = g_file_new_for_commandline_arg ((char *) test_data);
589 g_assert (root != NULL);
590 res = g_file_query_exists (root, NULL);
591 g_assert_cmpint (res, ==, TRUE);
594 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
596 item = sample_struct[i];
597 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
600 if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
601 (((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS)
603 || ((item.extra_flags & TEST_ENUMERATE_FILE) ==
604 TEST_ENUMERATE_FILE))
606 log (" Testing file '%s'\n", item.filename);
607 child = g_file_get_child (root, item.filename);
608 g_assert (child != NULL);
611 g_file_enumerate_children (child, "*",
612 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
615 if ((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS)
617 g_assert (enumerator == NULL);
618 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
620 if ((item.extra_flags & TEST_ENUMERATE_FILE) == TEST_ENUMERATE_FILE)
622 g_assert (enumerator == NULL);
623 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY);
625 if ((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS)
627 g_assert (enumerator != NULL);
630 info = g_file_enumerator_next_file (enumerator, NULL, &error);
631 g_assert (info == NULL);
632 g_assert_no_error (error);
633 /* no items should be found, no error should be logged */
637 g_error_free (error);
642 res = g_file_enumerator_close (enumerator, NULL, &error);
643 g_assert_cmpint (res, ==, TRUE);
644 g_assert_no_error (error);
646 g_object_unref (child);
649 g_object_unref (root);
653 do_copy_move (GFile * root, struct StructureItem item, const char *target_dir,
654 enum StructureExtraFlags extra_flags)
656 GFile *dst_dir, *src_file, *dst_file;
660 log (" do_copy_move: '%s' --> '%s'\n", item.filename, target_dir);
662 dst_dir = g_file_get_child (root, target_dir);
663 g_assert (dst_dir != NULL);
664 src_file = g_file_get_child (root, item.filename);
665 g_assert (src_file != NULL);
666 dst_file = g_file_get_child (dst_dir, item.filename);
667 g_assert (dst_file != NULL);
670 if ((item.extra_flags & TEST_COPY) == TEST_COPY)
672 g_file_copy (src_file, dst_file,
673 G_FILE_COPY_NOFOLLOW_SYMLINKS |
675 TEST_OVERWRITE) ? G_FILE_COPY_OVERWRITE :
676 G_FILE_COPY_NONE), NULL, NULL, NULL, &error);
679 g_file_move (src_file, dst_file, G_FILE_COPY_NOFOLLOW_SYMLINKS, NULL,
683 log (" res = %d, error code %d = %s\n", res, error->code,
686 /* copying file/directory to itself (".") */
687 if (((item.extra_flags & TEST_NOT_EXISTS) != TEST_NOT_EXISTS) &&
688 (extra_flags == TEST_ALREADY_EXISTS))
690 g_assert_cmpint (res, ==, FALSE);
691 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
693 /* target file is a file, overwrite is not set */
694 else if (((item.extra_flags & TEST_NOT_EXISTS) != TEST_NOT_EXISTS) &&
695 (extra_flags == TEST_TARGET_IS_FILE))
697 g_assert_cmpint (res, ==, FALSE);
698 if (item.file_type == G_FILE_TYPE_DIRECTORY)
699 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE);
701 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY);
703 /* source file is directory */
704 else if ((item.extra_flags & TEST_COPY_ERROR_RECURSE) ==
705 TEST_COPY_ERROR_RECURSE)
707 g_assert_cmpint (res, ==, FALSE);
708 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE);
710 /* source or target path doesn't exist */
711 else if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
712 (extra_flags == TEST_NOT_EXISTS))
714 g_assert_cmpint (res, ==, FALSE);
715 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
717 /* source or target path permission denied */
718 else if (((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS) ||
719 (extra_flags == TEST_NO_ACCESS))
721 /* This works for root, see bug #552912 */
722 if (test_suite && getuid () == 0)
724 g_assert_cmpint (res, ==, TRUE);
725 g_assert_no_error (error);
729 g_assert_cmpint (res, ==, FALSE);
730 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED);
733 /* no error should be found, all exceptions defined above */
736 g_assert_cmpint (res, ==, TRUE);
737 g_assert_no_error (error);
741 g_error_free (error);
744 g_object_unref (dst_dir);
745 g_object_unref (src_file);
746 g_object_unref (dst_file);
750 test_copy_move (gconstpointer test_data)
755 struct StructureItem item;
759 g_assert (test_data != NULL);
760 root = g_file_new_for_commandline_arg ((char *) test_data);
761 g_assert (root != NULL);
762 res = g_file_query_exists (root, NULL);
763 g_assert_cmpint (res, ==, TRUE);
766 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
768 item = sample_struct[i];
770 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
773 if (((item.extra_flags & TEST_COPY) == TEST_COPY) ||
774 ((item.extra_flags & TEST_MOVE) == TEST_MOVE))
776 /* test copy/move to a directory, expecting no errors if source files exist */
777 do_copy_move (root, item, TEST_DIR_TARGET, 0);
779 /* some files have been already moved so we can't count with them in the tests */
780 if ((item.extra_flags & TEST_COPY) == TEST_COPY)
782 /* test overwrite for flagged files */
783 if ((item.extra_flags & TEST_OVERWRITE) == TEST_OVERWRITE)
785 do_copy_move (root, item, TEST_DIR_TARGET, TEST_OVERWRITE);
787 /* source = target, should return G_IO_ERROR_EXISTS */
788 do_copy_move (root, item, ".", TEST_ALREADY_EXISTS);
790 do_copy_move (root, item, TEST_TARGET_FILE,
791 TEST_TARGET_IS_FILE);
792 /* target path is invalid */
793 do_copy_move (root, item, TEST_NAME_NOT_EXISTS,
796 /* tests on POSIX-compatible filesystems */
799 /* target directory is not accessible (no execute flag) */
800 do_copy_move (root, item, TEST_DIR_NO_ACCESS,
802 /* target directory is readonly */
803 do_copy_move (root, item, TEST_DIR_NO_WRITE,
809 g_object_unref (root);
813 test_create (gconstpointer test_data)
819 struct StructureItem item;
820 GFileOutputStream *os;
822 g_assert (test_data != NULL);
825 root = g_file_new_for_commandline_arg ((char *) test_data);
826 g_assert (root != NULL);
827 res = g_file_query_exists (root, NULL);
828 g_assert_cmpint (res, ==, TRUE);
830 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
832 item = sample_struct[i];
834 if (((item.extra_flags & TEST_CREATE) == TEST_CREATE) ||
835 ((item.extra_flags & TEST_REPLACE) == TEST_REPLACE) ||
836 ((item.extra_flags & TEST_APPEND) == TEST_APPEND))
838 log (" test_create: '%s'\n", item.filename);
840 child = g_file_get_child (root, item.filename);
841 g_assert (child != NULL);
845 if ((item.extra_flags & TEST_CREATE) == TEST_CREATE)
846 os = g_file_create (child, item.create_flags, NULL, &error);
847 else if ((item.extra_flags & TEST_REPLACE) == TEST_REPLACE)
849 g_file_replace (child, NULL, TRUE, item.create_flags, NULL,
851 else if ((item.extra_flags & TEST_APPEND) == TEST_APPEND)
852 os = g_file_append_to (child, item.create_flags, NULL, &error);
856 log (" error code %d = %s\n", error->code, error->message);
858 if (((item.extra_flags & TEST_NOT_EXISTS) == 0) &&
859 ((item.extra_flags & TEST_CREATE) == TEST_CREATE))
861 g_assert (os == NULL);
862 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
864 else if (item.file_type == G_FILE_TYPE_DIRECTORY)
866 g_assert (os == NULL);
867 if ((item.extra_flags & TEST_CREATE) == TEST_CREATE)
868 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
870 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
874 g_assert (os != NULL);
875 g_assert_no_error (error);
879 g_error_free (error);
885 g_output_stream_close (G_OUTPUT_STREAM (os), NULL, &error);
887 log (" g_output_stream_close: error %d = %s\n",
888 error->code, error->message);
889 g_assert_cmpint (res, ==, TRUE);
890 g_assert_no_error (error);
892 g_object_unref (child);
895 g_object_unref (root);
899 test_open (gconstpointer test_data)
905 struct StructureItem item;
906 GFileInputStream *input_stream;
908 g_assert (test_data != NULL);
911 root = g_file_new_for_commandline_arg ((char *) test_data);
912 g_assert (root != NULL);
913 res = g_file_query_exists (root, NULL);
914 g_assert_cmpint (res, ==, TRUE);
916 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
918 item = sample_struct[i];
920 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
923 if ((item.extra_flags & TEST_OPEN) == TEST_OPEN)
925 log (" test_open: '%s'\n", item.filename);
927 child = g_file_get_child (root, item.filename);
928 g_assert (child != NULL);
930 input_stream = g_file_read (child, NULL, &error);
932 if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
933 ((item.extra_flags & TEST_INVALID_SYMLINK) ==
934 TEST_INVALID_SYMLINK))
936 g_assert (input_stream == NULL);
937 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
939 else if (item.file_type == G_FILE_TYPE_DIRECTORY)
941 g_assert (input_stream == NULL);
942 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
946 g_assert (input_stream != NULL);
947 g_assert_no_error (error);
951 g_error_free (error);
957 g_input_stream_close (G_INPUT_STREAM (input_stream), NULL,
959 g_assert_cmpint (res, ==, TRUE);
960 g_assert_no_error (error);
962 g_object_unref (child);
965 g_object_unref (root);
969 test_delete (gconstpointer test_data)
976 struct StructureItem item;
978 g_assert (test_data != NULL);
981 root = g_file_new_for_commandline_arg ((char *) test_data);
982 g_assert (root != NULL);
983 res = g_file_query_exists (root, NULL);
984 g_assert_cmpint (res, ==, TRUE);
986 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
988 item = sample_struct[i];
990 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
993 if (((item.extra_flags & TEST_DELETE_NORMAL) == TEST_DELETE_NORMAL) ||
994 ((item.extra_flags & TEST_DELETE_TRASH) == TEST_DELETE_TRASH))
996 child = file_exists (root, item.filename, &res);
997 g_assert (child != NULL);
998 /* we don't care about result here */
1000 log (" Deleting %s, path = %s\n", item.filename,
1001 g_file_get_path (child));
1003 if ((item.extra_flags & TEST_DELETE_NORMAL) == TEST_DELETE_NORMAL)
1004 res = g_file_delete (child, NULL, &error);
1006 res = g_file_trash (child, NULL, &error);
1008 if ((item.extra_flags & TEST_DELETE_NON_EMPTY) ==
1009 TEST_DELETE_NON_EMPTY)
1011 g_assert_cmpint (res, ==, FALSE);
1012 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_EMPTY);
1014 if ((item.extra_flags & TEST_DELETE_FAILURE) == TEST_DELETE_FAILURE)
1016 g_assert_cmpint (res, ==, FALSE);
1017 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
1019 if ((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS)
1021 g_assert_cmpint (res, ==, FALSE);
1022 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
1027 log (" result = %d, error = %s\n", res, error->message);
1028 g_error_free (error);
1031 g_object_unref (child);
1034 g_object_unref (root);
1039 cleanup_dir_recurse (GFile *parent, GFile *root)
1043 GFileEnumerator *enumerator;
1046 char *relative_path;
1048 g_assert (root != NULL);
1052 g_file_enumerate_children (parent, "*",
1053 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
1059 info = g_file_enumerator_next_file (enumerator, NULL, &error);
1060 while ((info) && (!error))
1062 descend = g_file_get_child (parent, g_file_info_get_name (info));
1063 g_assert (descend != NULL);
1064 relative_path = g_file_get_relative_path (root, descend);
1065 g_assert (relative_path != NULL);
1067 log (" deleting '%s'\n", g_file_info_get_display_name (info));
1069 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
1070 cleanup_dir_recurse (descend, root);
1073 res = g_file_delete (descend, NULL, &error);
1074 g_assert_cmpint (res, ==, TRUE);
1076 g_object_unref (descend);
1078 info = g_file_enumerator_next_file (enumerator, NULL, &error);
1080 g_assert_no_error (error);
1083 res = g_file_enumerator_close (enumerator, NULL, &error);
1084 g_assert_cmpint (res, ==, TRUE);
1085 g_assert_no_error (error);
1089 prep_clean_structure (gconstpointer test_data)
1093 g_assert (test_data != NULL);
1094 log ("\n Cleaning target testing structure in '%s'...\n",
1095 (char *) test_data);
1097 root = g_file_new_for_commandline_arg ((char *) test_data);
1098 g_assert (root != NULL);
1100 cleanup_dir_recurse (root, root);
1102 g_file_delete (root, NULL, NULL);
1104 g_object_unref (root);
1108 main (int argc, char *argv[])
1110 static gboolean only_create_struct;
1111 const char *target_path;
1113 GOptionContext *context;
1115 static GOptionEntry cmd_entries[] = {
1116 {"read-write", 'w', 0, G_OPTION_ARG_NONE, &write_test,
1117 "Perform write tests (incl. structure creation)", NULL},
1118 {"create-struct", 'c', 0, G_OPTION_ARG_NONE, &only_create_struct,
1119 "Only create testing structure (no tests)", NULL},
1120 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL},
1121 {"posix", 'x', 0, G_OPTION_ARG_NONE, &posix_compat,
1122 "Test POSIX-specific features (unix permissions, symlinks)", NULL},
1129 only_create_struct = FALSE;
1131 posix_compat = FALSE;
1133 /* strip all gtester-specific args */
1135 g_test_init (&argc, &argv, NULL);
1137 /* no extra parameters specified, assume we're executed from glib test suite */
1143 only_create_struct = FALSE;
1144 target_path = DEFAULT_TEST_DIR;
1145 #ifdef G_PLATFORM_WIN32
1146 posix_compat = FALSE;
1148 posix_compat = TRUE;
1152 /* add trailing args */
1154 context = g_option_context_new ("target_path");
1155 g_option_context_add_main_entries (context, cmd_entries, NULL);
1156 if (!g_option_context_parse (context, &argc, &argv, &error))
1158 g_print ("option parsing failed: %s\n", error->message);
1159 return g_test_run ();
1162 /* remaining arg should is the target path; we don't care of the extra args here */
1164 target_path = strdup (argv[1]);
1168 g_print ("error: target path was not specified\n");
1169 g_print ("%s", g_option_context_get_help (context, TRUE, NULL));
1170 return g_test_run ();
1174 /* Write test - clean target directory first */
1175 /* this can be also considered as a test - enumerate + delete */
1176 if (write_test || only_create_struct)
1177 g_test_add_data_func ("/live-g-file/prep_clean_structure", target_path,
1178 prep_clean_structure);
1180 /* Write test - create new testing structure */
1181 if (write_test || only_create_struct)
1182 g_test_add_data_func ("/live-g-file/create_structure", target_path,
1183 test_create_structure);
1185 /* Read test - test the sample structure - expect defined attributes to be there */
1186 if (!only_create_struct)
1187 g_test_add_data_func ("/live-g-file/test_initial_structure", target_path,
1188 test_initial_structure);
1190 /* Read test - test traverse the structure - no special file should appear */
1191 if (!only_create_struct)
1192 g_test_add_data_func ("/live-g-file/test_traverse_structure", target_path,
1193 test_traverse_structure);
1195 /* Read test - enumerate */
1196 if (!only_create_struct)
1197 g_test_add_data_func ("/live-g-file/test_enumerate", target_path,
1200 /* Read test - open (g_file_read()) */
1201 if (!only_create_struct)
1202 g_test_add_data_func ("/live-g-file/test_open", target_path, test_open);
1204 /* Write test - create */
1205 if (write_test && (!only_create_struct))
1206 g_test_add_data_func ("/live-g-file/test_create", target_path,
1209 /* Write test - copy, move */
1210 if (write_test && (!only_create_struct))
1211 g_test_add_data_func ("/live-g-file/test_copy_move", target_path,
1214 /* Write test - delete, trash */
1215 if (write_test && (!only_create_struct))
1216 g_test_add_data_func ("/live-g-file/test_delete", target_path,
1219 if (write_test || only_create_struct)
1220 g_test_add_data_func ("/live-g-file/final_clean", target_path,
1221 prep_clean_structure);
1223 return g_test_run ();