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/gtestutils.h>
24 #include <glib/glib.h>
30 #define PATTERN_FILE_SIZE 0x10000
32 #define TEST_HANDLE_SPECIAL TRUE
34 enum StructureExtraFlags
36 TEST_DELETE_NORMAL = 1 << 0,
37 TEST_DELETE_TRASH = 1 << 1,
38 TEST_DELETE_NON_EMPTY = 1 << 2,
39 TEST_DELETE_FAILURE = 1 << 3,
40 TEST_NOT_EXISTS = 1 << 4,
41 TEST_ENUMERATE_FILE = 1 << 5,
42 TEST_NO_ACCESS = 1 << 6,
45 TEST_COPY_ERROR_RECURSE = 1 << 9,
46 TEST_ALREADY_EXISTS = 1 << 10,
47 TEST_TARGET_IS_FILE = 1 << 11,
48 TEST_CREATE = 1 << 12,
49 TEST_REPLACE = 1 << 13,
50 TEST_APPEND = 1 << 14,
52 TEST_OVERWRITE = 1 << 16,
53 TEST_INVALID_SYMLINK = 1 << 17,
61 GFileCreateFlags create_flags;
63 gboolean handle_special;
64 enum StructureExtraFlags extra_flags;
67 #define TEST_DIR_NO_ACCESS "dir_no-access"
68 #define TEST_DIR_NO_WRITE "dir_no-write"
69 #define TEST_DIR_TARGET "dir-target"
70 #define TEST_NAME_NOT_EXISTS "not_exists"
71 #define TEST_TARGET_FILE "target-file"
74 const struct StructureItem sample_struct[] = {
75 /* filename link file_type create_flags mode | handle_special | extra_flags */
76 {"dir1", NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_DELETE_NORMAL | TEST_DELETE_NON_EMPTY | TEST_REPLACE | TEST_OPEN},
77 {"dir1/subdir", NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_COPY_ERROR_RECURSE | TEST_APPEND},
78 {"dir2", NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_DELETE_NORMAL | TEST_MOVE | TEST_CREATE},
79 {TEST_DIR_TARGET, NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_COPY_ERROR_RECURSE},
80 {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},
81 {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},
82 {TEST_TARGET_FILE, NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_OPEN},
83 {"normal_file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_ENUMERATE_FILE | TEST_CREATE | TEST_OVERWRITE},
84 {"normal_file-symlink", "normal_file", G_FILE_TYPE_SYMBOLIC_LINK, G_FILE_CREATE_NONE, 0, 0, TEST_ENUMERATE_FILE | TEST_COPY | TEST_OPEN},
85 {"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},
86 {"private_file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_PRIVATE, 0, 0, TEST_COPY | TEST_OPEN | TEST_OVERWRITE | TEST_APPEND},
87 {"normal_file2", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_OVERWRITE | TEST_REPLACE},
88 {"readonly_file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, S_IRUSR + S_IRGRP + S_IROTH, 0, TEST_DELETE_NORMAL | TEST_OPEN},
89 {"UTF_p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88",
90 NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_CREATE | TEST_OPEN | TEST_OVERWRITE},
91 {"dir_p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88",
92 NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_DELETE_NORMAL | TEST_CREATE},
93 {"pattern_file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_COPY | TEST_OPEN | TEST_APPEND},
94 {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},
95 {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},
96 {"not_exists2", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_NOT_EXISTS | TEST_CREATE},
97 {"not_exists3", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_NOT_EXISTS | TEST_REPLACE},
98 {"not_exists4", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_NOT_EXISTS | TEST_APPEND},
99 {"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},
100 {"lost_symlink", "nowhere", G_FILE_TYPE_SYMBOLIC_LINK, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_DELETE_NORMAL | TEST_OPEN | TEST_INVALID_SYMLINK},
106 static gboolean write_test;
107 static gboolean verbose;
108 static gboolean posix_compat;
110 #define log(msg...) if (verbose) g_print (msg)
119 create_empty_file (GFile * parent, const char *filename,
120 GFileCreateFlags create_flags)
125 GFileOutputStream *outs;
128 child = g_file_get_child (parent, filename);
129 g_assert (child != NULL);
132 outs = g_file_replace (child, NULL, FALSE, create_flags, NULL, &error);
133 g_assert (error == NULL);
134 g_assert (outs != NULL);
136 res = g_output_stream_close (G_OUTPUT_STREAM (outs), NULL, &error);
137 g_object_unref (outs);
142 create_empty_dir (GFile * parent, const char *filename)
148 child = g_file_get_child (parent, filename);
149 g_assert (child != NULL);
151 res = g_file_make_directory (child, NULL, &error);
152 g_assert_cmpint (res, ==, TRUE);
153 g_assert (error == NULL);
158 create_symlink (GFile * parent, const char *filename, const char *points_to)
164 child = g_file_get_child (parent, filename);
165 g_assert (child != NULL);
167 res = g_file_make_symbolic_link (child, points_to, NULL, &error);
168 g_assert_cmpint (res, ==, TRUE);
169 g_assert (error == NULL);
174 test_create_structure (gconstpointer test_data)
180 GFileOutputStream *outs;
181 GDataOutputStream *outds;
183 struct StructureItem item;
186 g_assert (test_data != NULL);
187 log ("\n Going to create testing structure in '%s'...\n",
190 root = g_file_new_for_commandline_arg ((char *) test_data);
191 g_assert (root != NULL);
193 /* create root directory */
194 res = g_file_make_directory (root, NULL, NULL);
195 /* don't care about errors here */
198 /* create any other items */
199 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
201 item = sample_struct[i];
202 if ((item.handle_special)
204 && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK)))
208 switch (item.file_type)
210 case G_FILE_TYPE_REGULAR:
211 log (" Creating file '%s'...\n", item.filename);
212 child = create_empty_file (root, item.filename, item.create_flags);
214 case G_FILE_TYPE_DIRECTORY:
215 log (" Creating directory '%s'...\n", item.filename);
216 child = create_empty_dir (root, item.filename);
218 case G_FILE_TYPE_SYMBOLIC_LINK:
219 log (" Creating symlink '%s' --> '%s'...\n", item.filename,
221 child = create_symlink (root, item.filename, item.link_to);
226 g_assert (child != NULL);
228 if ((item.mode > 0) && (posix_compat))
232 g_file_set_attribute_uint32 (child, G_FILE_ATTRIBUTE_UNIX_MODE,
234 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
236 g_assert_cmpint (res, ==, TRUE);
237 g_assert (error == NULL);
240 g_object_unref (child);
243 /* create a pattern file */
244 log (" Creating pattern file...");
245 child = g_file_get_child (root, "pattern_file");
246 g_assert (child != NULL);
250 g_file_replace (child, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error);
251 g_assert (error == NULL);
253 g_assert (outs != NULL);
254 outds = g_data_output_stream_new (G_OUTPUT_STREAM (outs));
255 g_assert (outds != NULL);
256 for (i = 0; i < PATTERN_FILE_SIZE; i++)
259 res = g_data_output_stream_put_byte (outds, i % 256, NULL, &error);
260 g_assert (error == NULL);
263 res = g_output_stream_close (G_OUTPUT_STREAM (outs), NULL, &error);
264 g_assert (error == NULL);
265 g_object_unref (outds);
266 g_object_unref (outs);
267 g_object_unref (child);
270 g_object_unref (root);
278 file_exists (GFile * parent, const char *filename, gboolean * result)
286 child = g_file_get_child (parent, filename);
287 g_assert (child != NULL);
288 res = g_file_query_exists (child, NULL);
296 test_attributes (struct StructureItem item, GFileInfo * info)
300 const char *name, *display_name, *edit_name, *copy_name, *symlink_target;
304 gboolean can_read, can_write;
307 has_attr = g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE);
308 g_assert_cmpint (has_attr, ==, TRUE);
309 ftype = g_file_info_get_file_type (info);
310 g_assert_cmpint (ftype, !=, G_FILE_TYPE_UNKNOWN);
311 g_assert_cmpint (ftype, ==, item.file_type);
314 if ((item.mode > 0) && (posix_compat))
317 g_file_info_get_attribute_uint32 (info,
318 G_FILE_ATTRIBUTE_UNIX_MODE) & 0xFFF;
319 g_assert_cmpint (mode, ==, item.mode);
322 /* access::can-read */
323 if (item.file_type != G_FILE_TYPE_SYMBOLIC_LINK)
326 g_file_info_get_attribute_boolean (info,
327 G_FILE_ATTRIBUTE_ACCESS_CAN_READ);
328 g_assert_cmpint (can_read, ==, TRUE);
331 /* access::can-write */
332 if ((write_test) && ((item.extra_flags & TEST_OVERWRITE) == TEST_OVERWRITE))
335 g_file_info_get_attribute_boolean (info,
336 G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE);
337 g_assert_cmpint (can_write, ==, TRUE);
341 name = g_file_info_get_name (info);
342 g_assert (name != NULL);
344 /* standard::display-name */
345 display_name = g_file_info_get_display_name (info);
346 g_assert (display_name != NULL);
347 utf8_valid = g_utf8_validate (display_name, -1, NULL);
348 g_assert_cmpint (utf8_valid, ==, TRUE);
350 /* standard::edit-name */
351 edit_name = g_file_info_get_edit_name (info);
354 utf8_valid = g_utf8_validate (edit_name, -1, NULL);
355 g_assert_cmpint (utf8_valid, ==, TRUE);
358 /* standard::copy-name */
360 g_file_info_get_attribute_string (info,
361 G_FILE_ATTRIBUTE_STANDARD_COPY_NAME);
364 utf8_valid = g_utf8_validate (copy_name, -1, NULL);
365 g_assert_cmpint (utf8_valid, ==, TRUE);
368 /* standard::is-symlink */
371 is_symlink = g_file_info_get_is_symlink (info);
372 g_assert_cmpint (is_symlink, ==,
373 item.file_type == G_FILE_TYPE_SYMBOLIC_LINK);
376 /* standard::symlink-target */
377 if ((item.file_type == G_FILE_TYPE_SYMBOLIC_LINK) && (posix_compat))
379 symlink_target = g_file_info_get_symlink_target (info);
380 g_assert_cmpstr (symlink_target, ==, item.link_to);
386 test_initial_structure (gconstpointer test_data)
392 GFileInputStream *ins;
397 gssize read, total_read;
398 struct StructureItem item;
401 g_assert (test_data != NULL);
402 log ("\n Testing sample structure in '%s'...\n", (char *) test_data);
404 root = g_file_new_for_commandline_arg ((char *) test_data);
405 g_assert (root != NULL);
406 res = g_file_query_exists (root, NULL);
407 g_assert_cmpint (res, ==, TRUE);
410 /* test the structure */
411 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
413 item = sample_struct[i];
414 if (((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
415 || (item.handle_special))
418 log (" Testing file '%s'...\n", item.filename);
420 child = file_exists (root, item.filename, &res);
421 g_assert (child != NULL);
422 g_assert_cmpint (res, ==, TRUE);
426 g_file_query_info (child, "*", G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
428 g_assert (error == NULL);
429 g_assert (info != NULL);
431 test_attributes (item, info);
433 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 (error == NULL);
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 (error == NULL);
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 (error == NULL);
468 log (" read %d bytes, total = %d of %d.\n", read, total_read,
471 g_assert_cmpint (total_read, ==, PATTERN_FILE_SIZE);
474 res = g_input_stream_close (G_INPUT_STREAM (ins), NULL, &error);
475 g_assert (error == NULL);
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);
493 traverse_recurse_dirs (GFile * parent, GFile * root)
497 GFileEnumerator *enumerator;
505 g_assert (root != NULL);
509 g_file_enumerate_children (parent, "*",
510 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
512 g_assert (enumerator != NULL);
513 g_assert (error == NULL);
516 info = g_file_enumerator_next_file (enumerator, NULL, &error);
517 while ((info) && (!error))
519 descend = g_file_get_child (parent, g_file_info_get_name (info));
520 g_assert (descend != NULL);
521 relative_path = g_file_get_relative_path (root, descend);
522 g_assert (relative_path != NULL);
525 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
527 if (strcmp (sample_struct[i].filename, relative_path) == 0)
529 /* test the attributes again */
530 test_attributes (sample_struct[i], info);
536 g_assert_cmpint (found, ==, TRUE);
538 log (" Found file %s, relative to root: %s\n",
539 g_file_info_get_display_name (info), relative_path);
541 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
542 traverse_recurse_dirs (descend, root);
544 g_object_unref (descend);
546 info = g_file_enumerator_next_file (enumerator, NULL, &error);
548 g_assert (error == NULL);
552 res = g_file_enumerator_close (enumerator, NULL, &error);
553 g_assert_cmpint (res, ==, TRUE);
554 g_assert (error == NULL);
558 test_traverse_structure (gconstpointer test_data)
563 g_assert (test_data != NULL);
564 log ("\n Traversing through the sample structure in '%s'...\n",
567 root = g_file_new_for_commandline_arg ((char *) test_data);
568 g_assert (root != NULL);
569 res = g_file_query_exists (root, NULL);
570 g_assert_cmpint (res, ==, TRUE);
572 traverse_recurse_dirs (root, root);
574 g_object_unref (root);
581 test_enumerate (gconstpointer test_data)
586 GFileEnumerator *enumerator;
589 struct StructureItem item;
592 g_assert (test_data != NULL);
593 log ("\n Test enumerate '%s'...\n", (char *) test_data);
595 root = g_file_new_for_commandline_arg ((char *) test_data);
596 g_assert (root != NULL);
597 res = g_file_query_exists (root, NULL);
598 g_assert_cmpint (res, ==, TRUE);
601 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
603 item = sample_struct[i];
604 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
607 if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
608 (((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS)
610 || ((item.extra_flags & TEST_ENUMERATE_FILE) ==
611 TEST_ENUMERATE_FILE))
613 log (" Testing file '%s'\n", item.filename);
614 child = g_file_get_child (root, item.filename);
615 g_assert (child != NULL);
618 g_file_enumerate_children (child, "*",
619 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
622 if ((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS)
624 g_assert (enumerator == NULL);
625 g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_FOUND);
627 if ((item.extra_flags & TEST_ENUMERATE_FILE) == TEST_ENUMERATE_FILE)
629 g_assert (enumerator == NULL);
630 g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_DIRECTORY);
632 if ((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS)
634 g_assert (enumerator != NULL);
637 info = g_file_enumerator_next_file (enumerator, NULL, &error);
638 g_assert (info == NULL);
639 g_assert (error == NULL);
640 /* no items should be found, no error should be logged */
644 g_error_free (error);
649 res = g_file_enumerator_close (enumerator, NULL, &error);
650 g_assert_cmpint (res, ==, TRUE);
651 g_assert (error == NULL);
653 g_object_unref (child);
656 g_object_unref (root);
665 do_copy_move (GFile * root, struct StructureItem item, const char *target_dir,
666 enum StructureExtraFlags extra_flags)
668 GFile *dst_dir, *src_file, *dst_file;
672 log (" do_copy_move: '%s' --> '%s'\n", item.filename, target_dir);
674 dst_dir = g_file_get_child (root, target_dir);
675 g_assert (dst_dir != NULL);
676 src_file = g_file_get_child (root, item.filename);
677 g_assert (src_file != NULL);
678 dst_file = g_file_get_child (dst_dir, item.filename);
679 g_assert (dst_file != NULL);
682 if ((item.extra_flags & TEST_COPY) == TEST_COPY)
684 g_file_copy (src_file, dst_file,
685 G_FILE_COPY_NOFOLLOW_SYMLINKS |
687 TEST_OVERWRITE) ? G_FILE_COPY_OVERWRITE :
688 G_FILE_COPY_NONE), NULL, NULL, NULL, &error);
691 g_file_move (src_file, dst_file, G_FILE_COPY_NOFOLLOW_SYMLINKS, NULL,
695 log (" res = %d, error code %d = %s\n", res, error->code,
699 /* copying file/directory to itself (".") */
700 if (((item.extra_flags & TEST_NOT_EXISTS) != TEST_NOT_EXISTS) &&
701 (extra_flags == TEST_ALREADY_EXISTS))
703 g_assert_cmpint (res, ==, FALSE);
704 g_assert_cmpint (error->code, ==, G_IO_ERROR_EXISTS);
706 /* target file is a file, overwrite is not set */
707 else if (((item.extra_flags & TEST_NOT_EXISTS) != TEST_NOT_EXISTS) &&
708 (extra_flags == TEST_TARGET_IS_FILE))
710 g_assert_cmpint (res, ==, FALSE);
711 if (item.file_type == G_FILE_TYPE_DIRECTORY)
712 g_assert_cmpint (error->code, ==, G_IO_ERROR_WOULD_RECURSE);
714 g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_DIRECTORY);
716 /* source file is directory */
717 else if ((item.extra_flags & TEST_COPY_ERROR_RECURSE) ==
718 TEST_COPY_ERROR_RECURSE)
720 g_assert_cmpint (res, ==, FALSE);
721 g_assert_cmpint (error->code, ==, G_IO_ERROR_WOULD_RECURSE);
723 /* source or target path doesn't exist */
724 else if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
725 (extra_flags == TEST_NOT_EXISTS))
727 g_assert_cmpint (res, ==, FALSE);
728 g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_FOUND);
730 /* source or target path permission denied */
731 else if (((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS) ||
732 (extra_flags == TEST_NO_ACCESS))
734 g_assert_cmpint (res, ==, FALSE);
735 g_assert_cmpint (error->code, ==, G_IO_ERROR_PERMISSION_DENIED);
737 /* no error should be found, all exceptions defined above */
740 g_assert_cmpint (res, ==, TRUE);
741 g_assert (error == NULL);
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;
764 g_assert (test_data != NULL);
765 root = g_file_new_for_commandline_arg ((char *) test_data);
766 g_assert (root != NULL);
767 res = g_file_query_exists (root, NULL);
768 g_assert_cmpint (res, ==, TRUE);
771 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
773 item = sample_struct[i];
775 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
778 if (((item.extra_flags & TEST_COPY) == TEST_COPY) ||
779 ((item.extra_flags & TEST_MOVE) == TEST_MOVE))
781 /* test copy/move to a directory, expecting no errors if source files exist */
782 do_copy_move (root, item, TEST_DIR_TARGET, 0);
784 /* some files have been already moved so we can't count with them in the tests */
785 if ((item.extra_flags & TEST_COPY) == TEST_COPY)
787 /* test overwrite for flagged files */
788 if ((item.extra_flags & TEST_OVERWRITE) == TEST_OVERWRITE)
790 do_copy_move (root, item, TEST_DIR_TARGET, TEST_OVERWRITE);
792 /* source = target, should return G_IO_ERROR_EXISTS */
793 do_copy_move (root, item, ".", TEST_ALREADY_EXISTS);
795 do_copy_move (root, item, TEST_TARGET_FILE,
796 TEST_TARGET_IS_FILE);
797 /* target path is invalid */
798 do_copy_move (root, item, TEST_NAME_NOT_EXISTS,
801 /* tests on POSIX-compatible filesystems */
804 /* target directory is not accessible (no execute flag) */
805 do_copy_move (root, item, TEST_DIR_NO_ACCESS,
807 /* target directory is readonly */
808 do_copy_move (root, item, TEST_DIR_NO_WRITE,
814 g_object_unref (root);
822 test_create (gconstpointer test_data)
828 struct StructureItem item;
829 GFileOutputStream *os;
832 g_assert (test_data != NULL);
835 root = g_file_new_for_commandline_arg ((char *) test_data);
836 g_assert (root != NULL);
837 res = g_file_query_exists (root, NULL);
838 g_assert_cmpint (res, ==, TRUE);
841 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
843 item = sample_struct[i];
845 if (((item.extra_flags & TEST_CREATE) == TEST_CREATE) ||
846 ((item.extra_flags & TEST_REPLACE) == TEST_REPLACE) ||
847 ((item.extra_flags & TEST_APPEND) == TEST_APPEND))
849 log (" test_create: '%s'\n", item.filename);
851 child = g_file_get_child (root, item.filename);
852 g_assert (child != NULL);
856 if ((item.extra_flags & TEST_CREATE) == TEST_CREATE)
857 os = g_file_create (child, item.create_flags, NULL, &error);
858 else if ((item.extra_flags & TEST_REPLACE) == TEST_REPLACE)
860 g_file_replace (child, NULL, TRUE, item.create_flags, NULL,
862 else if ((item.extra_flags & TEST_APPEND) == TEST_APPEND)
863 os = g_file_append_to (child, item.create_flags, NULL, &error);
867 log (" error code %d = %s\n", error->code, error->message);
870 if (((item.extra_flags & TEST_NOT_EXISTS) == 0) &&
871 ((item.extra_flags & TEST_CREATE) == TEST_CREATE))
873 g_assert (os == NULL);
874 g_assert (error != NULL);
875 g_assert_cmpint (error->code, ==, G_IO_ERROR_EXISTS);
877 else if (item.file_type == G_FILE_TYPE_DIRECTORY)
879 g_assert (os == NULL);
880 g_assert (error != NULL);
881 if ((item.extra_flags & TEST_CREATE) == TEST_CREATE)
882 g_assert_cmpint (error->code, ==, G_IO_ERROR_EXISTS);
884 g_assert_cmpint (error->code, ==, G_IO_ERROR_IS_DIRECTORY);
888 g_assert (os != NULL);
889 g_assert (error == NULL);
893 g_error_free (error);
899 g_output_stream_close (G_OUTPUT_STREAM (os), NULL, &error);
901 log (" g_output_stream_close: error %d = %s\n",
902 error->code, error->message);
903 g_assert_cmpint (res, ==, TRUE);
904 g_assert (error == NULL);
906 g_object_unref (child);
909 g_object_unref (root);
916 test_open (gconstpointer test_data)
922 struct StructureItem item;
923 GFileInputStream *input_stream;
926 g_assert (test_data != NULL);
929 root = g_file_new_for_commandline_arg ((char *) test_data);
930 g_assert (root != NULL);
931 res = g_file_query_exists (root, NULL);
932 g_assert_cmpint (res, ==, TRUE);
935 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
937 item = sample_struct[i];
939 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
942 if ((item.extra_flags & TEST_OPEN) == TEST_OPEN)
944 log (" test_open: '%s'\n", item.filename);
946 child = g_file_get_child (root, item.filename);
947 g_assert (child != NULL);
949 input_stream = g_file_read (child, NULL, &error);
951 if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
952 ((item.extra_flags & TEST_INVALID_SYMLINK) ==
953 TEST_INVALID_SYMLINK))
955 g_assert (input_stream == NULL);
956 g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_FOUND);
958 else if (item.file_type == G_FILE_TYPE_DIRECTORY)
960 g_assert (input_stream == NULL);
961 g_assert_cmpint (error->code, ==, G_IO_ERROR_IS_DIRECTORY);
965 g_assert (input_stream != NULL);
966 g_assert (error == NULL);
970 g_error_free (error);
976 g_input_stream_close (G_INPUT_STREAM (input_stream), NULL,
978 g_assert_cmpint (res, ==, TRUE);
979 g_assert (error == NULL);
981 g_object_unref (child);
984 g_object_unref (root);
992 test_delete (gconstpointer test_data)
999 struct StructureItem item;
1002 g_assert (test_data != NULL);
1005 root = g_file_new_for_commandline_arg ((char *) test_data);
1006 g_assert (root != NULL);
1007 res = g_file_query_exists (root, NULL);
1008 g_assert_cmpint (res, ==, TRUE);
1010 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
1012 item = sample_struct[i];
1014 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
1017 if (((item.extra_flags & TEST_DELETE_NORMAL) == TEST_DELETE_NORMAL) ||
1018 ((item.extra_flags & TEST_DELETE_TRASH) == TEST_DELETE_TRASH))
1020 child = file_exists (root, item.filename, &res);
1021 g_assert (child != NULL);
1022 /* we don't care about result here */
1024 log (" Deleting %s, path = %s\n", item.filename,
1025 g_file_get_path (child));
1027 if ((item.extra_flags & TEST_DELETE_NORMAL) == TEST_DELETE_NORMAL)
1028 res = g_file_delete (child, NULL, &error);
1030 res = g_file_trash (child, NULL, &error);
1032 if ((item.extra_flags & TEST_DELETE_NON_EMPTY) ==
1033 TEST_DELETE_NON_EMPTY)
1035 g_assert_cmpint (res, ==, FALSE);
1036 g_assert (error != NULL);
1037 g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_EMPTY);
1039 if ((item.extra_flags & TEST_DELETE_FAILURE) == TEST_DELETE_FAILURE)
1041 g_assert_cmpint (res, ==, FALSE);
1042 g_assert (error != NULL);
1043 g_assert_cmpint (error->code, !=, 0);
1045 if ((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS)
1047 g_assert_cmpint (res, ==, FALSE);
1048 g_assert (error != NULL);
1049 g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_FOUND);
1054 log (" result = %d, error = %s\n", res, error->message);
1055 g_error_free (error);
1059 g_object_unref (child);
1062 g_object_unref (root);
1072 main (int argc, char *argv[])
1074 static gboolean create_struct;
1075 static char *target_path;
1077 GOptionContext *context;
1079 static GOptionEntry cmd_entries[] = {
1080 {"read-write", 'w', 0, G_OPTION_ARG_NONE, &write_test,
1081 "Perform write tests (incl. structure creation)", NULL},
1082 {"create-struct", 'c', 0, G_OPTION_ARG_NONE, &create_struct,
1083 "Only create testing structure (no tests)", NULL},
1084 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL},
1085 {"posix", 'x', 0, G_OPTION_ARG_NONE, &posix_compat,
1086 "Test POSIX-specific features (unix permissions, symlinks)", NULL},
1092 create_struct = FALSE;
1094 posix_compat = FALSE;
1096 /* strip all gtester-specific args */
1098 g_test_init (&argc, &argv, NULL);
1100 /* add trailing args */
1102 context = g_option_context_new ("target_path");
1103 g_option_context_add_main_entries (context, cmd_entries, NULL);
1104 if (!g_option_context_parse (context, &argc, &argv, &error))
1106 g_print ("option parsing failed: %s\n", error->message);
1107 return g_test_run ();
1110 /* missing mandatory arg for target dir */
1113 g_print (g_option_context_get_help (context, TRUE, NULL));
1114 return g_test_run ();
1116 target_path = strdup (argv[1]);
1120 /* Write test - create new testing structure */
1121 if (write_test || create_struct)
1122 g_test_add_data_func ("/live-g-file/create_structure", target_path,
1123 test_create_structure);
1125 /* Read test - test the sample structure - expect defined attributes to be there */
1127 g_test_add_data_func ("/live-g-file/test_initial_structure", target_path,
1128 test_initial_structure);
1130 /* Read test - test traverse the structure - no special file should appear */
1132 g_test_add_data_func ("/live-g-file/test_traverse_structure", target_path,
1133 test_traverse_structure);
1135 /* Read test - enumerate */
1137 g_test_add_data_func ("/live-g-file/test_enumerate", target_path,
1140 /* Read test - open (g_file_read()) */
1142 g_test_add_data_func ("/live-g-file/test_open", target_path, test_open);
1144 /* Write test - create */
1145 if (write_test && (!create_struct))
1146 g_test_add_data_func ("/live-g-file/test_create", target_path,
1149 /* Write test - copy, move */
1150 if (write_test && (!create_struct))
1151 g_test_add_data_func ("/live-g-file/test_copy_move", target_path,
1154 /* Write test - delete, trash */
1155 if (write_test && (!create_struct))
1156 g_test_add_data_func ("/live-g-file/test_delete", target_path,
1159 return g_test_run ();