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 #ifdef G_HAVE_ISO_VARARGS
108 #define log(...) if (verbose) g_print (__VA_ARGS__)
109 #elif defined(G_HAVE_GNUC_VARARGS)
110 #define log(msg...) if (verbose) g_print (msg)
111 #else /* no varargs macros */
112 static void log (const g_char *format, ...)
115 va_start (args, format);
116 if (verbose) g_print (format, args);
122 create_empty_file (GFile * parent, const char *filename,
123 GFileCreateFlags create_flags)
128 GFileOutputStream *outs;
130 child = g_file_get_child (parent, filename);
131 g_assert (child != NULL);
134 outs = g_file_replace (child, NULL, FALSE, create_flags, NULL, &error);
135 g_assert_no_error (error);
136 g_assert (outs != NULL);
138 res = g_output_stream_close (G_OUTPUT_STREAM (outs), NULL, &error);
139 g_object_unref (outs);
144 create_empty_dir (GFile * parent, const char *filename)
150 child = g_file_get_child (parent, filename);
151 g_assert (child != NULL);
153 res = g_file_make_directory (child, NULL, &error);
154 g_assert_cmpint (res, ==, TRUE);
155 g_assert_no_error (error);
160 create_symlink (GFile * parent, const char *filename, const char *points_to)
166 child = g_file_get_child (parent, filename);
167 g_assert (child != NULL);
169 res = g_file_make_symbolic_link (child, points_to, NULL, &error);
170 g_assert_cmpint (res, ==, TRUE);
171 g_assert_no_error (error);
176 test_create_structure (gconstpointer test_data)
182 GFileOutputStream *outs;
183 GDataOutputStream *outds;
185 struct StructureItem item;
187 g_assert (test_data != NULL);
188 log ("\n Going to create testing structure in '%s'...\n",
191 root = g_file_new_for_commandline_arg ((char *) test_data);
192 g_assert (root != NULL);
194 /* create root directory */
195 res = g_file_make_directory (root, NULL, NULL);
196 /* 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);
223 case G_FILE_TYPE_UNKNOWN:
224 case G_FILE_TYPE_SPECIAL:
225 case G_FILE_TYPE_SHORTCUT:
226 case G_FILE_TYPE_MOUNTABLE:
230 g_assert (child != NULL);
232 if ((item.mode > 0) && (posix_compat))
236 g_file_set_attribute_uint32 (child, G_FILE_ATTRIBUTE_UNIX_MODE,
238 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
240 g_assert_cmpint (res, ==, TRUE);
241 g_assert_no_error (error);
244 g_object_unref (child);
247 /* create a pattern file */
248 log (" Creating pattern file...");
249 child = g_file_get_child (root, "pattern_file");
250 g_assert (child != NULL);
254 g_file_replace (child, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error);
255 g_assert_no_error (error);
257 g_assert (outs != NULL);
258 outds = g_data_output_stream_new (G_OUTPUT_STREAM (outs));
259 g_assert (outds != NULL);
260 for (i = 0; i < PATTERN_FILE_SIZE; i++)
263 res = g_data_output_stream_put_byte (outds, i % 256, NULL, &error);
264 g_assert_no_error (error);
267 res = g_output_stream_close (G_OUTPUT_STREAM (outs), NULL, &error);
268 g_assert_no_error (error);
269 g_object_unref (outds);
270 g_object_unref (outs);
271 g_object_unref (child);
274 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);
385 test_initial_structure (gconstpointer test_data)
391 GFileInputStream *ins;
396 gssize read, total_read;
397 struct StructureItem item;
400 g_assert (test_data != NULL);
401 log ("\n Testing sample structure in '%s'...\n", (char *) test_data);
403 root = g_file_new_for_commandline_arg ((char *) test_data);
404 g_assert (root != NULL);
405 res = g_file_query_exists (root, NULL);
406 g_assert_cmpint (res, ==, TRUE);
408 /* test the structure */
409 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
411 item = sample_struct[i];
412 if (((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
413 || (item.handle_special))
416 log (" Testing file '%s'...\n", item.filename);
418 child = file_exists (root, item.filename, &res);
419 g_assert (child != NULL);
420 g_assert_cmpint (res, ==, TRUE);
424 g_file_query_info (child, "*", G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
426 g_assert_no_error (error);
427 g_assert (info != NULL);
429 test_attributes (item, info);
431 g_object_unref (child);
434 /* read and test the pattern file */
435 log (" Testing pattern file...\n");
436 child = file_exists (root, "pattern_file", &res);
437 g_assert (child != NULL);
438 g_assert_cmpint (res, ==, TRUE);
442 g_file_query_info (child, "*", G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
444 g_assert_no_error (error);
445 g_assert (info != NULL);
446 size = g_file_info_get_size (info);
447 g_assert_cmpint (size, ==, PATTERN_FILE_SIZE);
450 ins = g_file_read (child, NULL, &error);
451 g_assert (ins != NULL);
452 g_assert_no_error (error);
454 buffer = g_malloc (PATTERN_FILE_SIZE);
457 while (total_read < PATTERN_FILE_SIZE)
461 g_input_stream_read (G_INPUT_STREAM (ins), buffer + total_read,
462 PATTERN_FILE_SIZE, NULL, &error);
463 g_assert_no_error (error);
465 log (" read %"G_GSSIZE_FORMAT" bytes, total = %"G_GSSIZE_FORMAT" of %d.\n",
466 read, total_read, PATTERN_FILE_SIZE);
468 g_assert_cmpint (total_read, ==, PATTERN_FILE_SIZE);
471 res = g_input_stream_close (G_INPUT_STREAM (ins), NULL, &error);
472 g_assert_no_error (error);
473 g_assert_cmpint (res, ==, TRUE);
475 for (i = 0; i < PATTERN_FILE_SIZE; i++)
476 g_assert_cmpint (*(buffer + i), ==, i % 256);
478 g_object_unref (ins);
479 g_object_unref (child);
481 g_object_unref (root);
485 traverse_recurse_dirs (GFile * parent, GFile * root)
489 GFileEnumerator *enumerator;
496 g_assert (root != NULL);
500 g_file_enumerate_children (parent, "*",
501 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
503 g_assert (enumerator != NULL);
504 g_assert_no_error (error);
507 info = g_file_enumerator_next_file (enumerator, NULL, &error);
508 while ((info) && (!error))
510 descend = g_file_get_child (parent, g_file_info_get_name (info));
511 g_assert (descend != NULL);
512 relative_path = g_file_get_relative_path (root, descend);
513 g_assert (relative_path != NULL);
516 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
518 if (strcmp (sample_struct[i].filename, relative_path) == 0)
520 /* test the attributes again */
521 test_attributes (sample_struct[i], info);
527 g_assert_cmpint (found, ==, TRUE);
529 log (" Found file %s, relative to root: %s\n",
530 g_file_info_get_display_name (info), relative_path);
532 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
533 traverse_recurse_dirs (descend, root);
535 g_object_unref (descend);
537 info = g_file_enumerator_next_file (enumerator, NULL, &error);
539 g_assert_no_error (error);
542 res = g_file_enumerator_close (enumerator, NULL, &error);
543 g_assert_cmpint (res, ==, TRUE);
544 g_assert_no_error (error);
548 test_traverse_structure (gconstpointer test_data)
553 g_assert (test_data != NULL);
554 log ("\n Traversing through the sample structure in '%s'...\n",
557 root = g_file_new_for_commandline_arg ((char *) test_data);
558 g_assert (root != NULL);
559 res = g_file_query_exists (root, NULL);
560 g_assert_cmpint (res, ==, TRUE);
562 traverse_recurse_dirs (root, root);
564 g_object_unref (root);
571 test_enumerate (gconstpointer test_data)
576 GFileEnumerator *enumerator;
579 struct StructureItem item;
582 g_assert (test_data != NULL);
583 log ("\n Test enumerate '%s'...\n", (char *) test_data);
585 root = g_file_new_for_commandline_arg ((char *) test_data);
586 g_assert (root != NULL);
587 res = g_file_query_exists (root, NULL);
588 g_assert_cmpint (res, ==, TRUE);
591 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
593 item = sample_struct[i];
594 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
597 if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
598 (((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS)
600 || ((item.extra_flags & TEST_ENUMERATE_FILE) ==
601 TEST_ENUMERATE_FILE))
603 log (" Testing file '%s'\n", item.filename);
604 child = g_file_get_child (root, item.filename);
605 g_assert (child != NULL);
608 g_file_enumerate_children (child, "*",
609 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
612 if ((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS)
614 g_assert (enumerator == NULL);
615 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
617 if ((item.extra_flags & TEST_ENUMERATE_FILE) == TEST_ENUMERATE_FILE)
619 g_assert (enumerator == NULL);
620 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY);
622 if ((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS)
624 g_assert (enumerator != NULL);
627 info = g_file_enumerator_next_file (enumerator, NULL, &error);
628 g_assert (info == NULL);
629 g_assert_no_error (error);
630 /* no items should be found, no error should be logged */
634 g_error_free (error);
639 res = g_file_enumerator_close (enumerator, NULL, &error);
640 g_assert_cmpint (res, ==, TRUE);
641 g_assert_no_error (error);
643 g_object_unref (child);
646 g_object_unref (root);
650 do_copy_move (GFile * root, struct StructureItem item, const char *target_dir,
651 enum StructureExtraFlags extra_flags)
653 GFile *dst_dir, *src_file, *dst_file;
657 log (" do_copy_move: '%s' --> '%s'\n", item.filename, target_dir);
659 dst_dir = g_file_get_child (root, target_dir);
660 g_assert (dst_dir != NULL);
661 src_file = g_file_get_child (root, item.filename);
662 g_assert (src_file != NULL);
663 dst_file = g_file_get_child (dst_dir, item.filename);
664 g_assert (dst_file != NULL);
667 if ((item.extra_flags & TEST_COPY) == TEST_COPY)
669 g_file_copy (src_file, dst_file,
670 G_FILE_COPY_NOFOLLOW_SYMLINKS |
672 TEST_OVERWRITE) ? G_FILE_COPY_OVERWRITE :
673 G_FILE_COPY_NONE), NULL, NULL, NULL, &error);
676 g_file_move (src_file, dst_file, G_FILE_COPY_NOFOLLOW_SYMLINKS, NULL,
680 log (" res = %d, error code %d = %s\n", res, error->code,
683 /* copying file/directory to itself (".") */
684 if (((item.extra_flags & TEST_NOT_EXISTS) != TEST_NOT_EXISTS) &&
685 (extra_flags == TEST_ALREADY_EXISTS))
687 g_assert_cmpint (res, ==, FALSE);
688 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
690 /* target file is a file, overwrite is not set */
691 else if (((item.extra_flags & TEST_NOT_EXISTS) != TEST_NOT_EXISTS) &&
692 (extra_flags == TEST_TARGET_IS_FILE))
694 g_assert_cmpint (res, ==, FALSE);
695 if (item.file_type == G_FILE_TYPE_DIRECTORY)
696 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE);
698 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY);
700 /* source file is directory */
701 else if ((item.extra_flags & TEST_COPY_ERROR_RECURSE) ==
702 TEST_COPY_ERROR_RECURSE)
704 g_assert_cmpint (res, ==, FALSE);
705 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE);
707 /* source or target path doesn't exist */
708 else if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
709 (extra_flags == TEST_NOT_EXISTS))
711 g_assert_cmpint (res, ==, FALSE);
712 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
714 /* source or target path permission denied */
715 else if (((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS) ||
716 (extra_flags == TEST_NO_ACCESS))
718 g_assert_cmpint (res, ==, FALSE);
719 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED);
721 /* no error should be found, all exceptions defined above */
724 g_assert_cmpint (res, ==, TRUE);
725 g_assert_no_error (error);
729 g_error_free (error);
732 g_object_unref (dst_dir);
733 g_object_unref (src_file);
734 g_object_unref (dst_file);
738 test_copy_move (gconstpointer test_data)
743 struct StructureItem item;
747 g_assert (test_data != NULL);
748 root = g_file_new_for_commandline_arg ((char *) test_data);
749 g_assert (root != NULL);
750 res = g_file_query_exists (root, NULL);
751 g_assert_cmpint (res, ==, TRUE);
754 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
756 item = sample_struct[i];
758 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
761 if (((item.extra_flags & TEST_COPY) == TEST_COPY) ||
762 ((item.extra_flags & TEST_MOVE) == TEST_MOVE))
764 /* test copy/move to a directory, expecting no errors if source files exist */
765 do_copy_move (root, item, TEST_DIR_TARGET, 0);
767 /* some files have been already moved so we can't count with them in the tests */
768 if ((item.extra_flags & TEST_COPY) == TEST_COPY)
770 /* test overwrite for flagged files */
771 if ((item.extra_flags & TEST_OVERWRITE) == TEST_OVERWRITE)
773 do_copy_move (root, item, TEST_DIR_TARGET, TEST_OVERWRITE);
775 /* source = target, should return G_IO_ERROR_EXISTS */
776 do_copy_move (root, item, ".", TEST_ALREADY_EXISTS);
778 do_copy_move (root, item, TEST_TARGET_FILE,
779 TEST_TARGET_IS_FILE);
780 /* target path is invalid */
781 do_copy_move (root, item, TEST_NAME_NOT_EXISTS,
784 /* tests on POSIX-compatible filesystems */
787 /* target directory is not accessible (no execute flag) */
788 do_copy_move (root, item, TEST_DIR_NO_ACCESS,
790 /* target directory is readonly */
791 do_copy_move (root, item, TEST_DIR_NO_WRITE,
797 g_object_unref (root);
801 test_create (gconstpointer test_data)
807 struct StructureItem item;
808 GFileOutputStream *os;
810 g_assert (test_data != NULL);
813 root = g_file_new_for_commandline_arg ((char *) test_data);
814 g_assert (root != NULL);
815 res = g_file_query_exists (root, NULL);
816 g_assert_cmpint (res, ==, TRUE);
818 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
820 item = sample_struct[i];
822 if (((item.extra_flags & TEST_CREATE) == TEST_CREATE) ||
823 ((item.extra_flags & TEST_REPLACE) == TEST_REPLACE) ||
824 ((item.extra_flags & TEST_APPEND) == TEST_APPEND))
826 log (" test_create: '%s'\n", item.filename);
828 child = g_file_get_child (root, item.filename);
829 g_assert (child != NULL);
833 if ((item.extra_flags & TEST_CREATE) == TEST_CREATE)
834 os = g_file_create (child, item.create_flags, NULL, &error);
835 else if ((item.extra_flags & TEST_REPLACE) == TEST_REPLACE)
837 g_file_replace (child, NULL, TRUE, item.create_flags, NULL,
839 else if ((item.extra_flags & TEST_APPEND) == TEST_APPEND)
840 os = g_file_append_to (child, item.create_flags, NULL, &error);
844 log (" error code %d = %s\n", error->code, error->message);
846 if (((item.extra_flags & TEST_NOT_EXISTS) == 0) &&
847 ((item.extra_flags & TEST_CREATE) == TEST_CREATE))
849 g_assert (os == NULL);
850 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
852 else if (item.file_type == G_FILE_TYPE_DIRECTORY)
854 g_assert (os == NULL);
855 if ((item.extra_flags & TEST_CREATE) == TEST_CREATE)
856 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
858 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
862 g_assert (os != NULL);
863 g_assert_no_error (error);
867 g_error_free (error);
873 g_output_stream_close (G_OUTPUT_STREAM (os), NULL, &error);
875 log (" g_output_stream_close: error %d = %s\n",
876 error->code, error->message);
877 g_assert_cmpint (res, ==, TRUE);
878 g_assert_no_error (error);
880 g_object_unref (child);
883 g_object_unref (root);
887 test_open (gconstpointer test_data)
893 struct StructureItem item;
894 GFileInputStream *input_stream;
896 g_assert (test_data != NULL);
899 root = g_file_new_for_commandline_arg ((char *) test_data);
900 g_assert (root != NULL);
901 res = g_file_query_exists (root, NULL);
902 g_assert_cmpint (res, ==, TRUE);
904 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
906 item = sample_struct[i];
908 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
911 if ((item.extra_flags & TEST_OPEN) == TEST_OPEN)
913 log (" test_open: '%s'\n", item.filename);
915 child = g_file_get_child (root, item.filename);
916 g_assert (child != NULL);
918 input_stream = g_file_read (child, NULL, &error);
920 if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
921 ((item.extra_flags & TEST_INVALID_SYMLINK) ==
922 TEST_INVALID_SYMLINK))
924 g_assert (input_stream == NULL);
925 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
927 else if (item.file_type == G_FILE_TYPE_DIRECTORY)
929 g_assert (input_stream == NULL);
930 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
934 g_assert (input_stream != NULL);
935 g_assert_no_error (error);
939 g_error_free (error);
945 g_input_stream_close (G_INPUT_STREAM (input_stream), NULL,
947 g_assert_cmpint (res, ==, TRUE);
948 g_assert_no_error (error);
950 g_object_unref (child);
953 g_object_unref (root);
957 test_delete (gconstpointer test_data)
964 struct StructureItem item;
966 g_assert (test_data != NULL);
969 root = g_file_new_for_commandline_arg ((char *) test_data);
970 g_assert (root != NULL);
971 res = g_file_query_exists (root, NULL);
972 g_assert_cmpint (res, ==, TRUE);
974 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
976 item = sample_struct[i];
978 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
981 if (((item.extra_flags & TEST_DELETE_NORMAL) == TEST_DELETE_NORMAL) ||
982 ((item.extra_flags & TEST_DELETE_TRASH) == TEST_DELETE_TRASH))
984 child = file_exists (root, item.filename, &res);
985 g_assert (child != NULL);
986 /* we don't care about result here */
988 log (" Deleting %s, path = %s\n", item.filename,
989 g_file_get_path (child));
991 if ((item.extra_flags & TEST_DELETE_NORMAL) == TEST_DELETE_NORMAL)
992 res = g_file_delete (child, NULL, &error);
994 res = g_file_trash (child, NULL, &error);
996 if ((item.extra_flags & TEST_DELETE_NON_EMPTY) ==
997 TEST_DELETE_NON_EMPTY)
999 g_assert_cmpint (res, ==, FALSE);
1000 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_EMPTY);
1002 if ((item.extra_flags & TEST_DELETE_FAILURE) == TEST_DELETE_FAILURE)
1004 g_assert_cmpint (res, ==, FALSE);
1005 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
1007 if ((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS)
1009 g_assert_cmpint (res, ==, FALSE);
1010 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
1015 log (" result = %d, error = %s\n", res, error->message);
1016 g_error_free (error);
1019 g_object_unref (child);
1022 g_object_unref (root);
1027 cleanup_dir_recurse (GFile *parent, GFile *root)
1031 GFileEnumerator *enumerator;
1034 char *relative_path;
1036 g_assert (root != NULL);
1040 g_file_enumerate_children (parent, "*",
1041 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
1047 info = g_file_enumerator_next_file (enumerator, NULL, &error);
1048 while ((info) && (!error))
1050 descend = g_file_get_child (parent, g_file_info_get_name (info));
1051 g_assert (descend != NULL);
1052 relative_path = g_file_get_relative_path (root, descend);
1053 g_assert (relative_path != NULL);
1055 log (" deleting '%s'\n", g_file_info_get_display_name (info));
1057 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
1058 cleanup_dir_recurse (descend, root);
1061 res = g_file_delete (descend, NULL, &error);
1062 g_assert_cmpint (res, ==, TRUE);
1064 g_object_unref (descend);
1066 info = g_file_enumerator_next_file (enumerator, NULL, &error);
1068 g_assert_no_error (error);
1071 res = g_file_enumerator_close (enumerator, NULL, &error);
1072 g_assert_cmpint (res, ==, TRUE);
1073 g_assert_no_error (error);
1077 prep_clean_structure (gconstpointer test_data)
1081 g_assert (test_data != NULL);
1082 log ("\n Cleaning target testing structure in '%s'...\n",
1083 (char *) test_data);
1085 root = g_file_new_for_commandline_arg ((char *) test_data);
1086 g_assert (root != NULL);
1088 cleanup_dir_recurse (root, root);
1090 g_file_delete (root, NULL, NULL);
1092 g_object_unref (root);
1096 main (int argc, char *argv[])
1098 static gboolean only_create_struct;
1099 const char *target_path;
1101 GOptionContext *context;
1103 static GOptionEntry cmd_entries[] = {
1104 {"read-write", 'w', 0, G_OPTION_ARG_NONE, &write_test,
1105 "Perform write tests (incl. structure creation)", NULL},
1106 {"create-struct", 'c', 0, G_OPTION_ARG_NONE, &only_create_struct,
1107 "Only create testing structure (no tests)", NULL},
1108 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL},
1109 {"posix", 'x', 0, G_OPTION_ARG_NONE, &posix_compat,
1110 "Test POSIX-specific features (unix permissions, symlinks)", NULL},
1116 only_create_struct = FALSE;
1118 posix_compat = FALSE;
1120 /* strip all gtester-specific args */
1122 g_test_init (&argc, &argv, NULL);
1124 /* no extra parameters specified, assume we're executed from glib test suite */
1129 only_create_struct = FALSE;
1130 target_path = DEFAULT_TEST_DIR;
1131 #ifdef G_PLATFORM_WIN32
1132 posix_compat = FALSE;
1134 posix_compat = TRUE;
1138 /* add trailing args */
1140 context = g_option_context_new ("target_path");
1141 g_option_context_add_main_entries (context, cmd_entries, NULL);
1142 if (!g_option_context_parse (context, &argc, &argv, &error))
1144 g_print ("option parsing failed: %s\n", error->message);
1145 return g_test_run ();
1148 /* remaining arg should is the target path; we don't care of the extra args here */
1150 target_path = strdup (argv[1]);
1154 g_print ("error: target path was not specified\n");
1155 g_print ("%s", g_option_context_get_help (context, TRUE, NULL));
1156 return g_test_run ();
1160 /* Write test - clean target directory first */
1161 /* this can be also considered as a test - enumerate + delete */
1162 if (write_test || only_create_struct)
1163 g_test_add_data_func ("/live-g-file/prep_clean_structure", target_path,
1164 prep_clean_structure);
1166 /* Write test - create new testing structure */
1167 if (write_test || only_create_struct)
1168 g_test_add_data_func ("/live-g-file/create_structure", target_path,
1169 test_create_structure);
1171 /* Read test - test the sample structure - expect defined attributes to be there */
1172 if (!only_create_struct)
1173 g_test_add_data_func ("/live-g-file/test_initial_structure", target_path,
1174 test_initial_structure);
1176 /* Read test - test traverse the structure - no special file should appear */
1177 if (!only_create_struct)
1178 g_test_add_data_func ("/live-g-file/test_traverse_structure", target_path,
1179 test_traverse_structure);
1181 /* Read test - enumerate */
1182 if (!only_create_struct)
1183 g_test_add_data_func ("/live-g-file/test_enumerate", target_path,
1186 /* Read test - open (g_file_read()) */
1187 if (!only_create_struct)
1188 g_test_add_data_func ("/live-g-file/test_open", target_path, test_open);
1190 /* Write test - create */
1191 if (write_test && (!only_create_struct))
1192 g_test_add_data_func ("/live-g-file/test_create", target_path,
1195 /* Write test - copy, move */
1196 if (write_test && (!only_create_struct))
1197 g_test_add_data_func ("/live-g-file/test_copy_move", target_path,
1200 /* Write test - delete, trash */
1201 if (write_test && (!only_create_struct))
1202 g_test_add_data_func ("/live-g-file/test_delete", target_path,
1205 if (write_test || only_create_struct)
1206 g_test_add_data_func ("/live-g-file/final_clean", target_path,
1207 prep_clean_structure);
1209 return g_test_run ();