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>
29 #define DEFAULT_TEST_DIR "testdir_live-g-file"
31 #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 static 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_pr\xcc\x8ci\xcc\x81lis\xcc\x8c z",
90 NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_CREATE | TEST_OPEN | TEST_OVERWRITE},
91 {"dir_pr\xcc\x8ci\xcc\x81lis\xcc\x8c z",
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},
103 static gboolean write_test;
104 static gboolean verbose;
105 static gboolean posix_compat;
107 #define log(msg...) if (verbose) g_print (msg)
110 create_empty_file (GFile * parent, const char *filename,
111 GFileCreateFlags create_flags)
116 GFileOutputStream *outs;
118 child = g_file_get_child (parent, filename);
119 g_assert (child != NULL);
122 outs = g_file_replace (child, NULL, FALSE, create_flags, NULL, &error);
123 g_assert_no_error (error);
124 g_assert (outs != NULL);
126 res = g_output_stream_close (G_OUTPUT_STREAM (outs), NULL, &error);
127 g_object_unref (outs);
132 create_empty_dir (GFile * parent, const char *filename)
138 child = g_file_get_child (parent, filename);
139 g_assert (child != NULL);
141 res = g_file_make_directory (child, NULL, &error);
142 g_assert_cmpint (res, ==, TRUE);
143 g_assert_no_error (error);
148 create_symlink (GFile * parent, const char *filename, const char *points_to)
154 child = g_file_get_child (parent, filename);
155 g_assert (child != NULL);
157 res = g_file_make_symbolic_link (child, points_to, NULL, &error);
158 g_assert_cmpint (res, ==, TRUE);
159 g_assert_no_error (error);
164 test_create_structure (gconstpointer test_data)
170 GFileOutputStream *outs;
171 GDataOutputStream *outds;
173 struct StructureItem item;
175 g_assert (test_data != NULL);
176 log ("\n Going to create testing structure in '%s'...\n",
179 root = g_file_new_for_commandline_arg ((char *) test_data);
180 g_assert (root != NULL);
182 /* create root directory */
183 res = g_file_make_directory (root, NULL, NULL);
184 /* don't care about errors here */
186 /* create any other items */
187 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
189 item = sample_struct[i];
190 if ((item.handle_special)
192 && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK)))
196 switch (item.file_type)
198 case G_FILE_TYPE_REGULAR:
199 log (" Creating file '%s'...\n", item.filename);
200 child = create_empty_file (root, item.filename, item.create_flags);
202 case G_FILE_TYPE_DIRECTORY:
203 log (" Creating directory '%s'...\n", item.filename);
204 child = create_empty_dir (root, item.filename);
206 case G_FILE_TYPE_SYMBOLIC_LINK:
207 log (" Creating symlink '%s' --> '%s'...\n", item.filename,
209 child = create_symlink (root, item.filename, item.link_to);
214 g_assert (child != NULL);
216 if ((item.mode > 0) && (posix_compat))
220 g_file_set_attribute_uint32 (child, G_FILE_ATTRIBUTE_UNIX_MODE,
222 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
224 g_assert_cmpint (res, ==, TRUE);
225 g_assert_no_error (error);
228 g_object_unref (child);
231 /* create a pattern file */
232 log (" Creating pattern file...");
233 child = g_file_get_child (root, "pattern_file");
234 g_assert (child != NULL);
238 g_file_replace (child, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error);
239 g_assert_no_error (error);
241 g_assert (outs != NULL);
242 outds = g_data_output_stream_new (G_OUTPUT_STREAM (outs));
243 g_assert (outds != NULL);
244 for (i = 0; i < PATTERN_FILE_SIZE; i++)
247 res = g_data_output_stream_put_byte (outds, i % 256, NULL, &error);
248 g_assert_no_error (error);
251 res = g_output_stream_close (G_OUTPUT_STREAM (outs), NULL, &error);
252 g_assert_no_error (error);
253 g_object_unref (outds);
254 g_object_unref (outs);
255 g_object_unref (child);
258 g_object_unref (root);
262 file_exists (GFile * parent, const char *filename, gboolean * result)
270 child = g_file_get_child (parent, filename);
271 g_assert (child != NULL);
272 res = g_file_query_exists (child, NULL);
280 test_attributes (struct StructureItem item, GFileInfo * info)
284 const char *name, *display_name, *edit_name, *copy_name, *symlink_target;
288 gboolean can_read, can_write;
291 has_attr = g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE);
292 g_assert_cmpint (has_attr, ==, TRUE);
293 ftype = g_file_info_get_file_type (info);
294 g_assert_cmpint (ftype, !=, G_FILE_TYPE_UNKNOWN);
295 g_assert_cmpint (ftype, ==, item.file_type);
298 if ((item.mode > 0) && (posix_compat))
301 g_file_info_get_attribute_uint32 (info,
302 G_FILE_ATTRIBUTE_UNIX_MODE) & 0xFFF;
303 g_assert_cmpint (mode, ==, item.mode);
306 /* access::can-read */
307 if (item.file_type != G_FILE_TYPE_SYMBOLIC_LINK)
310 g_file_info_get_attribute_boolean (info,
311 G_FILE_ATTRIBUTE_ACCESS_CAN_READ);
312 g_assert_cmpint (can_read, ==, TRUE);
315 /* access::can-write */
316 if ((write_test) && ((item.extra_flags & TEST_OVERWRITE) == TEST_OVERWRITE))
319 g_file_info_get_attribute_boolean (info,
320 G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE);
321 g_assert_cmpint (can_write, ==, TRUE);
325 name = g_file_info_get_name (info);
326 g_assert (name != NULL);
328 /* standard::display-name */
329 display_name = g_file_info_get_display_name (info);
330 g_assert (display_name != NULL);
331 utf8_valid = g_utf8_validate (display_name, -1, NULL);
332 g_assert_cmpint (utf8_valid, ==, TRUE);
334 /* standard::edit-name */
335 edit_name = g_file_info_get_edit_name (info);
338 utf8_valid = g_utf8_validate (edit_name, -1, NULL);
339 g_assert_cmpint (utf8_valid, ==, TRUE);
342 /* standard::copy-name */
344 g_file_info_get_attribute_string (info,
345 G_FILE_ATTRIBUTE_STANDARD_COPY_NAME);
348 utf8_valid = g_utf8_validate (copy_name, -1, NULL);
349 g_assert_cmpint (utf8_valid, ==, TRUE);
352 /* standard::is-symlink */
355 is_symlink = g_file_info_get_is_symlink (info);
356 g_assert_cmpint (is_symlink, ==,
357 item.file_type == G_FILE_TYPE_SYMBOLIC_LINK);
360 /* standard::symlink-target */
361 if ((item.file_type == G_FILE_TYPE_SYMBOLIC_LINK) && (posix_compat))
363 symlink_target = g_file_info_get_symlink_target (info);
364 g_assert_cmpstr (symlink_target, ==, item.link_to);
369 test_initial_structure (gconstpointer test_data)
375 GFileInputStream *ins;
380 gssize read, total_read;
381 struct StructureItem item;
384 g_assert (test_data != NULL);
385 log ("\n Testing sample structure in '%s'...\n", (char *) test_data);
387 root = g_file_new_for_commandline_arg ((char *) test_data);
388 g_assert (root != NULL);
389 res = g_file_query_exists (root, NULL);
390 g_assert_cmpint (res, ==, TRUE);
392 /* test the structure */
393 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
395 item = sample_struct[i];
396 if (((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
397 || (item.handle_special))
400 log (" Testing file '%s'...\n", item.filename);
402 child = file_exists (root, item.filename, &res);
403 g_assert (child != NULL);
404 g_assert_cmpint (res, ==, TRUE);
408 g_file_query_info (child, "*", G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
410 g_assert_no_error (error);
411 g_assert (info != NULL);
413 test_attributes (item, info);
415 g_object_unref (child);
418 /* read and test the pattern file */
419 log (" Testing pattern file...\n");
420 child = file_exists (root, "pattern_file", &res);
421 g_assert (child != NULL);
422 g_assert_cmpint (res, ==, TRUE);
426 g_file_query_info (child, "*", G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
428 g_assert_no_error (error);
429 g_assert (info != NULL);
430 size = g_file_info_get_size (info);
431 g_assert_cmpint (size, ==, PATTERN_FILE_SIZE);
434 ins = g_file_read (child, NULL, &error);
435 g_assert (ins != NULL);
436 g_assert_no_error (error);
438 buffer = g_malloc (PATTERN_FILE_SIZE);
441 while (total_read < PATTERN_FILE_SIZE)
445 g_input_stream_read (G_INPUT_STREAM (ins), buffer + total_read,
446 PATTERN_FILE_SIZE, NULL, &error);
447 g_assert_no_error (error);
449 log (" read %d bytes, total = %d of %d.\n", read, total_read,
452 g_assert_cmpint (total_read, ==, PATTERN_FILE_SIZE);
455 res = g_input_stream_close (G_INPUT_STREAM (ins), NULL, &error);
456 g_assert_no_error (error);
457 g_assert_cmpint (res, ==, TRUE);
459 for (i = 0; i < PATTERN_FILE_SIZE; i++)
460 g_assert_cmpint (*(buffer + i), ==, i % 256);
462 g_object_unref (ins);
463 g_object_unref (child);
465 g_object_unref (root);
469 traverse_recurse_dirs (GFile * parent, GFile * root)
473 GFileEnumerator *enumerator;
480 g_assert (root != NULL);
484 g_file_enumerate_children (parent, "*",
485 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
487 g_assert (enumerator != NULL);
488 g_assert_no_error (error);
491 info = g_file_enumerator_next_file (enumerator, NULL, &error);
492 while ((info) && (!error))
494 descend = g_file_get_child (parent, g_file_info_get_name (info));
495 g_assert (descend != NULL);
496 relative_path = g_file_get_relative_path (root, descend);
497 g_assert (relative_path != NULL);
500 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
502 if (strcmp (sample_struct[i].filename, relative_path) == 0)
504 /* test the attributes again */
505 test_attributes (sample_struct[i], info);
511 g_assert_cmpint (found, ==, TRUE);
513 log (" Found file %s, relative to root: %s\n",
514 g_file_info_get_display_name (info), relative_path);
516 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
517 traverse_recurse_dirs (descend, root);
519 g_object_unref (descend);
521 info = g_file_enumerator_next_file (enumerator, NULL, &error);
523 g_assert_no_error (error);
526 res = g_file_enumerator_close (enumerator, NULL, &error);
527 g_assert_cmpint (res, ==, TRUE);
528 g_assert_no_error (error);
532 test_traverse_structure (gconstpointer test_data)
537 g_assert (test_data != NULL);
538 log ("\n Traversing through the sample structure in '%s'...\n",
541 root = g_file_new_for_commandline_arg ((char *) test_data);
542 g_assert (root != NULL);
543 res = g_file_query_exists (root, NULL);
544 g_assert_cmpint (res, ==, TRUE);
546 traverse_recurse_dirs (root, root);
548 g_object_unref (root);
555 test_enumerate (gconstpointer test_data)
560 GFileEnumerator *enumerator;
563 struct StructureItem item;
566 g_assert (test_data != NULL);
567 log ("\n Test enumerate '%s'...\n", (char *) test_data);
569 root = g_file_new_for_commandline_arg ((char *) test_data);
570 g_assert (root != NULL);
571 res = g_file_query_exists (root, NULL);
572 g_assert_cmpint (res, ==, TRUE);
575 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
577 item = sample_struct[i];
578 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
581 if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
582 (((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS)
584 || ((item.extra_flags & TEST_ENUMERATE_FILE) ==
585 TEST_ENUMERATE_FILE))
587 log (" Testing file '%s'\n", item.filename);
588 child = g_file_get_child (root, item.filename);
589 g_assert (child != NULL);
592 g_file_enumerate_children (child, "*",
593 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
596 if ((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS)
598 g_assert (enumerator == NULL);
599 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
601 if ((item.extra_flags & TEST_ENUMERATE_FILE) == TEST_ENUMERATE_FILE)
603 g_assert (enumerator == NULL);
604 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY);
606 if ((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS)
608 g_assert (enumerator != NULL);
611 info = g_file_enumerator_next_file (enumerator, NULL, &error);
612 g_assert (info == NULL);
613 g_assert_no_error (error);
614 /* no items should be found, no error should be logged */
618 g_error_free (error);
623 res = g_file_enumerator_close (enumerator, NULL, &error);
624 g_assert_cmpint (res, ==, TRUE);
625 g_assert_no_error (error);
627 g_object_unref (child);
630 g_object_unref (root);
634 do_copy_move (GFile * root, struct StructureItem item, const char *target_dir,
635 enum StructureExtraFlags extra_flags)
637 GFile *dst_dir, *src_file, *dst_file;
641 log (" do_copy_move: '%s' --> '%s'\n", item.filename, target_dir);
643 dst_dir = g_file_get_child (root, target_dir);
644 g_assert (dst_dir != NULL);
645 src_file = g_file_get_child (root, item.filename);
646 g_assert (src_file != NULL);
647 dst_file = g_file_get_child (dst_dir, item.filename);
648 g_assert (dst_file != NULL);
651 if ((item.extra_flags & TEST_COPY) == TEST_COPY)
653 g_file_copy (src_file, dst_file,
654 G_FILE_COPY_NOFOLLOW_SYMLINKS |
656 TEST_OVERWRITE) ? G_FILE_COPY_OVERWRITE :
657 G_FILE_COPY_NONE), NULL, NULL, NULL, &error);
660 g_file_move (src_file, dst_file, G_FILE_COPY_NOFOLLOW_SYMLINKS, NULL,
664 log (" res = %d, error code %d = %s\n", res, error->code,
667 /* copying file/directory to itself (".") */
668 if (((item.extra_flags & TEST_NOT_EXISTS) != TEST_NOT_EXISTS) &&
669 (extra_flags == TEST_ALREADY_EXISTS))
671 g_assert_cmpint (res, ==, FALSE);
672 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
674 /* target file is a file, overwrite is not set */
675 else if (((item.extra_flags & TEST_NOT_EXISTS) != TEST_NOT_EXISTS) &&
676 (extra_flags == TEST_TARGET_IS_FILE))
678 g_assert_cmpint (res, ==, FALSE);
679 if (item.file_type == G_FILE_TYPE_DIRECTORY)
680 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE);
682 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY);
684 /* source file is directory */
685 else if ((item.extra_flags & TEST_COPY_ERROR_RECURSE) ==
686 TEST_COPY_ERROR_RECURSE)
688 g_assert_cmpint (res, ==, FALSE);
689 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE);
691 /* source or target path doesn't exist */
692 else if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
693 (extra_flags == TEST_NOT_EXISTS))
695 g_assert_cmpint (res, ==, FALSE);
696 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
698 /* source or target path permission denied */
699 else if (((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS) ||
700 (extra_flags == TEST_NO_ACCESS))
702 g_assert_cmpint (res, ==, FALSE);
703 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED);
705 /* no error should be found, all exceptions defined above */
708 g_assert_cmpint (res, ==, TRUE);
709 g_assert_no_error (error);
713 g_error_free (error);
716 g_object_unref (dst_dir);
717 g_object_unref (src_file);
718 g_object_unref (dst_file);
722 test_copy_move (gconstpointer test_data)
727 struct StructureItem item;
731 g_assert (test_data != NULL);
732 root = g_file_new_for_commandline_arg ((char *) test_data);
733 g_assert (root != NULL);
734 res = g_file_query_exists (root, NULL);
735 g_assert_cmpint (res, ==, TRUE);
738 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
740 item = sample_struct[i];
742 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
745 if (((item.extra_flags & TEST_COPY) == TEST_COPY) ||
746 ((item.extra_flags & TEST_MOVE) == TEST_MOVE))
748 /* test copy/move to a directory, expecting no errors if source files exist */
749 do_copy_move (root, item, TEST_DIR_TARGET, 0);
751 /* some files have been already moved so we can't count with them in the tests */
752 if ((item.extra_flags & TEST_COPY) == TEST_COPY)
754 /* test overwrite for flagged files */
755 if ((item.extra_flags & TEST_OVERWRITE) == TEST_OVERWRITE)
757 do_copy_move (root, item, TEST_DIR_TARGET, TEST_OVERWRITE);
759 /* source = target, should return G_IO_ERROR_EXISTS */
760 do_copy_move (root, item, ".", TEST_ALREADY_EXISTS);
762 do_copy_move (root, item, TEST_TARGET_FILE,
763 TEST_TARGET_IS_FILE);
764 /* target path is invalid */
765 do_copy_move (root, item, TEST_NAME_NOT_EXISTS,
768 /* tests on POSIX-compatible filesystems */
771 /* target directory is not accessible (no execute flag) */
772 do_copy_move (root, item, TEST_DIR_NO_ACCESS,
774 /* target directory is readonly */
775 do_copy_move (root, item, TEST_DIR_NO_WRITE,
781 g_object_unref (root);
785 test_create (gconstpointer test_data)
791 struct StructureItem item;
792 GFileOutputStream *os;
794 g_assert (test_data != NULL);
797 root = g_file_new_for_commandline_arg ((char *) test_data);
798 g_assert (root != NULL);
799 res = g_file_query_exists (root, NULL);
800 g_assert_cmpint (res, ==, TRUE);
802 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
804 item = sample_struct[i];
806 if (((item.extra_flags & TEST_CREATE) == TEST_CREATE) ||
807 ((item.extra_flags & TEST_REPLACE) == TEST_REPLACE) ||
808 ((item.extra_flags & TEST_APPEND) == TEST_APPEND))
810 log (" test_create: '%s'\n", item.filename);
812 child = g_file_get_child (root, item.filename);
813 g_assert (child != NULL);
817 if ((item.extra_flags & TEST_CREATE) == TEST_CREATE)
818 os = g_file_create (child, item.create_flags, NULL, &error);
819 else if ((item.extra_flags & TEST_REPLACE) == TEST_REPLACE)
821 g_file_replace (child, NULL, TRUE, item.create_flags, NULL,
823 else if ((item.extra_flags & TEST_APPEND) == TEST_APPEND)
824 os = g_file_append_to (child, item.create_flags, NULL, &error);
828 log (" error code %d = %s\n", error->code, error->message);
830 if (((item.extra_flags & TEST_NOT_EXISTS) == 0) &&
831 ((item.extra_flags & TEST_CREATE) == TEST_CREATE))
833 g_assert (os == NULL);
834 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
836 else if (item.file_type == G_FILE_TYPE_DIRECTORY)
838 g_assert (os == NULL);
839 if ((item.extra_flags & TEST_CREATE) == TEST_CREATE)
840 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
842 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
846 g_assert (os != NULL);
847 g_assert_no_error (error);
851 g_error_free (error);
857 g_output_stream_close (G_OUTPUT_STREAM (os), NULL, &error);
859 log (" g_output_stream_close: error %d = %s\n",
860 error->code, error->message);
861 g_assert_cmpint (res, ==, TRUE);
862 g_assert_no_error (error);
864 g_object_unref (child);
867 g_object_unref (root);
871 test_open (gconstpointer test_data)
877 struct StructureItem item;
878 GFileInputStream *input_stream;
880 g_assert (test_data != NULL);
883 root = g_file_new_for_commandline_arg ((char *) test_data);
884 g_assert (root != NULL);
885 res = g_file_query_exists (root, NULL);
886 g_assert_cmpint (res, ==, TRUE);
888 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
890 item = sample_struct[i];
892 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
895 if ((item.extra_flags & TEST_OPEN) == TEST_OPEN)
897 log (" test_open: '%s'\n", item.filename);
899 child = g_file_get_child (root, item.filename);
900 g_assert (child != NULL);
902 input_stream = g_file_read (child, NULL, &error);
904 if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
905 ((item.extra_flags & TEST_INVALID_SYMLINK) ==
906 TEST_INVALID_SYMLINK))
908 g_assert (input_stream == NULL);
909 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
911 else if (item.file_type == G_FILE_TYPE_DIRECTORY)
913 g_assert (input_stream == NULL);
914 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
918 g_assert (input_stream != NULL);
919 g_assert_no_error (error);
923 g_error_free (error);
929 g_input_stream_close (G_INPUT_STREAM (input_stream), NULL,
931 g_assert_cmpint (res, ==, TRUE);
932 g_assert_no_error (error);
934 g_object_unref (child);
937 g_object_unref (root);
941 test_delete (gconstpointer test_data)
948 struct StructureItem item;
950 g_assert (test_data != NULL);
953 root = g_file_new_for_commandline_arg ((char *) test_data);
954 g_assert (root != NULL);
955 res = g_file_query_exists (root, NULL);
956 g_assert_cmpint (res, ==, TRUE);
958 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
960 item = sample_struct[i];
962 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
965 if (((item.extra_flags & TEST_DELETE_NORMAL) == TEST_DELETE_NORMAL) ||
966 ((item.extra_flags & TEST_DELETE_TRASH) == TEST_DELETE_TRASH))
968 child = file_exists (root, item.filename, &res);
969 g_assert (child != NULL);
970 /* we don't care about result here */
972 log (" Deleting %s, path = %s\n", item.filename,
973 g_file_get_path (child));
975 if ((item.extra_flags & TEST_DELETE_NORMAL) == TEST_DELETE_NORMAL)
976 res = g_file_delete (child, NULL, &error);
978 res = g_file_trash (child, NULL, &error);
980 if ((item.extra_flags & TEST_DELETE_NON_EMPTY) ==
981 TEST_DELETE_NON_EMPTY)
983 g_assert_cmpint (res, ==, FALSE);
984 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_EMPTY);
986 if ((item.extra_flags & TEST_DELETE_FAILURE) == TEST_DELETE_FAILURE)
988 g_assert_cmpint (res, ==, FALSE);
989 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
991 if ((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS)
993 g_assert_cmpint (res, ==, FALSE);
994 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
999 log (" result = %d, error = %s\n", res, error->message);
1000 g_error_free (error);
1003 g_object_unref (child);
1006 g_object_unref (root);
1011 cleanup_dir_recurse (GFile *parent, GFile *root)
1015 GFileEnumerator *enumerator;
1018 char *relative_path;
1020 g_assert (root != NULL);
1024 g_file_enumerate_children (parent, "*",
1025 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
1031 info = g_file_enumerator_next_file (enumerator, NULL, &error);
1032 while ((info) && (!error))
1034 descend = g_file_get_child (parent, g_file_info_get_name (info));
1035 g_assert (descend != NULL);
1036 relative_path = g_file_get_relative_path (root, descend);
1037 g_assert (relative_path != NULL);
1039 log (" deleting '%s'\n", g_file_info_get_display_name (info));
1041 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
1042 cleanup_dir_recurse (descend, root);
1045 res = g_file_delete (descend, NULL, &error);
1046 g_assert_cmpint (res, ==, TRUE);
1048 g_object_unref (descend);
1050 info = g_file_enumerator_next_file (enumerator, NULL, &error);
1052 g_assert_no_error (error);
1055 res = g_file_enumerator_close (enumerator, NULL, &error);
1056 g_assert_cmpint (res, ==, TRUE);
1057 g_assert_no_error (error);
1061 prep_clean_structure (gconstpointer test_data)
1065 g_assert (test_data != NULL);
1066 log ("\n Cleaning target testing structure in '%s'...\n",
1067 (char *) test_data);
1069 root = g_file_new_for_commandline_arg ((char *) test_data);
1070 g_assert (root != NULL);
1072 cleanup_dir_recurse (root, root);
1074 g_file_delete (root, NULL, NULL);
1076 g_object_unref (root);
1080 main (int argc, char *argv[])
1082 static gboolean only_create_struct;
1083 static char *target_path;
1085 GOptionContext *context;
1087 static GOptionEntry cmd_entries[] = {
1088 {"read-write", 'w', 0, G_OPTION_ARG_NONE, &write_test,
1089 "Perform write tests (incl. structure creation)", NULL},
1090 {"create-struct", 'c', 0, G_OPTION_ARG_NONE, &only_create_struct,
1091 "Only create testing structure (no tests)", NULL},
1092 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL},
1093 {"posix", 'x', 0, G_OPTION_ARG_NONE, &posix_compat,
1094 "Test POSIX-specific features (unix permissions, symlinks)", NULL},
1100 only_create_struct = FALSE;
1102 posix_compat = FALSE;
1104 /* strip all gtester-specific args */
1106 g_test_init (&argc, &argv, NULL);
1108 /* no extra parameters specified, assume we're executed from glib test suite */
1113 only_create_struct = FALSE;
1114 target_path = DEFAULT_TEST_DIR;
1115 #ifdef G_PLATFORM_WIN32
1116 posix_compat = FALSE;
1118 posix_compat = TRUE;
1122 /* add trailing args */
1124 context = g_option_context_new ("target_path");
1125 g_option_context_add_main_entries (context, cmd_entries, NULL);
1126 if (!g_option_context_parse (context, &argc, &argv, &error))
1128 g_print ("option parsing failed: %s\n", error->message);
1129 return g_test_run ();
1132 /* remaining arg should is the target path; we don't care of the extra args here */
1134 target_path = strdup (argv[1]);
1138 g_print ("error: target path was not specified\n");
1139 g_print ("%s", g_option_context_get_help (context, TRUE, NULL));
1140 return g_test_run ();
1144 /* Write test - clean target directory first */
1145 /* this can be also considered as a test - enumerate + delete */
1146 if (write_test || only_create_struct)
1147 g_test_add_data_func ("/live-g-file/prep_clean_structure", target_path,
1148 prep_clean_structure);
1150 /* Write test - create new testing structure */
1151 if (write_test || only_create_struct)
1152 g_test_add_data_func ("/live-g-file/create_structure", target_path,
1153 test_create_structure);
1155 /* Read test - test the sample structure - expect defined attributes to be there */
1156 if (!only_create_struct)
1157 g_test_add_data_func ("/live-g-file/test_initial_structure", target_path,
1158 test_initial_structure);
1160 /* Read test - test traverse the structure - no special file should appear */
1161 if (!only_create_struct)
1162 g_test_add_data_func ("/live-g-file/test_traverse_structure", target_path,
1163 test_traverse_structure);
1165 /* Read test - enumerate */
1166 if (!only_create_struct)
1167 g_test_add_data_func ("/live-g-file/test_enumerate", target_path,
1170 /* Read test - open (g_file_read()) */
1171 if (!only_create_struct)
1172 g_test_add_data_func ("/live-g-file/test_open", target_path, test_open);
1174 /* Write test - create */
1175 if (write_test && (!only_create_struct))
1176 g_test_add_data_func ("/live-g-file/test_create", target_path,
1179 /* Write test - copy, move */
1180 if (write_test && (!only_create_struct))
1181 g_test_add_data_func ("/live-g-file/test_copy_move", target_path,
1184 /* Write test - delete, trash */
1185 if (write_test && (!only_create_struct))
1186 g_test_add_data_func ("/live-g-file/test_delete", target_path,
1189 if (write_test || only_create_struct)
1190 g_test_add_data_func ("/live-g-file/final_clean", target_path,
1191 prep_clean_structure);
1193 return g_test_run ();