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 DEFAULT_TEST_DIR "testdir_live-g-file"
32 #define PATTERN_FILE_SIZE 0x10000
33 #define TEST_HANDLE_SPECIAL TRUE
35 enum StructureExtraFlags
37 TEST_DELETE_NORMAL = 1 << 0,
38 TEST_DELETE_TRASH = 1 << 1,
39 TEST_DELETE_NON_EMPTY = 1 << 2,
40 TEST_DELETE_FAILURE = 1 << 3,
41 TEST_NOT_EXISTS = 1 << 4,
42 TEST_ENUMERATE_FILE = 1 << 5,
43 TEST_NO_ACCESS = 1 << 6,
46 TEST_COPY_ERROR_RECURSE = 1 << 9,
47 TEST_ALREADY_EXISTS = 1 << 10,
48 TEST_TARGET_IS_FILE = 1 << 11,
49 TEST_CREATE = 1 << 12,
50 TEST_REPLACE = 1 << 13,
51 TEST_APPEND = 1 << 14,
53 TEST_OVERWRITE = 1 << 16,
54 TEST_INVALID_SYMLINK = 1 << 17,
62 GFileCreateFlags create_flags;
64 gboolean handle_special;
65 enum StructureExtraFlags extra_flags;
68 #define TEST_DIR_NO_ACCESS "dir_no-access"
69 #define TEST_DIR_NO_WRITE "dir_no-write"
70 #define TEST_DIR_TARGET "dir-target"
71 #define TEST_NAME_NOT_EXISTS "not_exists"
72 #define TEST_TARGET_FILE "target-file"
75 static const struct StructureItem sample_struct[] = {
76 /* filename link file_type create_flags mode | handle_special | extra_flags */
77 {"dir1", NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_DELETE_NORMAL | TEST_DELETE_NON_EMPTY | TEST_REPLACE | TEST_OPEN},
78 {"dir1/subdir", NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_COPY_ERROR_RECURSE | TEST_APPEND},
79 {"dir2", NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_DELETE_NORMAL | TEST_MOVE | TEST_CREATE},
80 {TEST_DIR_TARGET, NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_COPY_ERROR_RECURSE},
81 {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},
82 {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},
83 {TEST_TARGET_FILE, NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_OPEN},
84 {"normal_file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_ENUMERATE_FILE | TEST_CREATE | TEST_OVERWRITE},
85 {"normal_file-symlink", "normal_file", G_FILE_TYPE_SYMBOLIC_LINK, G_FILE_CREATE_NONE, 0, 0, TEST_ENUMERATE_FILE | TEST_COPY | TEST_OPEN},
86 {"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},
87 {"private_file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_PRIVATE, 0, 0, TEST_COPY | TEST_OPEN | TEST_OVERWRITE | TEST_APPEND},
88 {"normal_file2", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_OVERWRITE | TEST_REPLACE},
89 {"readonly_file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, S_IRUSR + S_IRGRP + S_IROTH, 0, TEST_DELETE_NORMAL | TEST_OPEN},
90 {"UTF_p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88",
91 NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_CREATE | TEST_OPEN | TEST_OVERWRITE},
92 {"dir_p\xc5\x99\xc3\xadli\xc5\xa1 \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88",
93 NULL, G_FILE_TYPE_DIRECTORY, G_FILE_CREATE_NONE, 0, 0, TEST_DELETE_NORMAL | TEST_CREATE},
94 {"pattern_file", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_COPY | TEST_OPEN | TEST_APPEND},
95 {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},
96 {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},
97 {"not_exists2", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_NOT_EXISTS | TEST_CREATE},
98 {"not_exists3", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_NOT_EXISTS | TEST_REPLACE},
99 {"not_exists4", NULL, G_FILE_TYPE_REGULAR, G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_NOT_EXISTS | TEST_APPEND},
100 {"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},
101 {"lost_symlink", "nowhere", G_FILE_TYPE_SYMBOLIC_LINK, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_DELETE_NORMAL | TEST_OPEN | TEST_INVALID_SYMLINK},
104 static gboolean write_test;
105 static gboolean verbose;
106 static gboolean posix_compat;
108 #define log(msg...) if (verbose) g_print (msg)
111 create_empty_file (GFile * parent, const char *filename,
112 GFileCreateFlags create_flags)
117 GFileOutputStream *outs;
119 child = g_file_get_child (parent, filename);
120 g_assert (child != NULL);
123 outs = g_file_replace (child, NULL, FALSE, create_flags, NULL, &error);
124 g_assert (error == NULL);
125 g_assert (outs != NULL);
127 res = g_output_stream_close (G_OUTPUT_STREAM (outs), NULL, &error);
128 g_object_unref (outs);
133 create_empty_dir (GFile * parent, const char *filename)
139 child = g_file_get_child (parent, filename);
140 g_assert (child != NULL);
142 res = g_file_make_directory (child, NULL, &error);
143 g_assert_cmpint (res, ==, TRUE);
144 g_assert (error == NULL);
149 create_symlink (GFile * parent, const char *filename, const char *points_to)
155 child = g_file_get_child (parent, filename);
156 g_assert (child != NULL);
158 res = g_file_make_symbolic_link (child, points_to, NULL, &error);
159 g_assert_cmpint (res, ==, TRUE);
160 g_assert (error == NULL);
165 test_create_structure (gconstpointer test_data)
171 GFileOutputStream *outs;
172 GDataOutputStream *outds;
174 struct StructureItem item;
176 g_assert (test_data != NULL);
177 log ("\n Going to create testing structure in '%s'...\n",
180 root = g_file_new_for_commandline_arg ((char *) test_data);
181 g_assert (root != NULL);
183 /* create root directory */
184 res = g_file_make_directory (root, NULL, NULL);
185 /* don't care about errors here */
187 /* create any other items */
188 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
190 item = sample_struct[i];
191 if ((item.handle_special)
193 && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK)))
197 switch (item.file_type)
199 case G_FILE_TYPE_REGULAR:
200 log (" Creating file '%s'...\n", item.filename);
201 child = create_empty_file (root, item.filename, item.create_flags);
203 case G_FILE_TYPE_DIRECTORY:
204 log (" Creating directory '%s'...\n", item.filename);
205 child = create_empty_dir (root, item.filename);
207 case G_FILE_TYPE_SYMBOLIC_LINK:
208 log (" Creating symlink '%s' --> '%s'...\n", item.filename,
210 child = create_symlink (root, item.filename, item.link_to);
215 g_assert (child != NULL);
217 if ((item.mode > 0) && (posix_compat))
221 g_file_set_attribute_uint32 (child, G_FILE_ATTRIBUTE_UNIX_MODE,
223 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
225 g_assert_cmpint (res, ==, TRUE);
226 g_assert (error == NULL);
229 g_object_unref (child);
232 /* create a pattern file */
233 log (" Creating pattern file...");
234 child = g_file_get_child (root, "pattern_file");
235 g_assert (child != NULL);
239 g_file_replace (child, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error);
240 g_assert (error == NULL);
242 g_assert (outs != NULL);
243 outds = g_data_output_stream_new (G_OUTPUT_STREAM (outs));
244 g_assert (outds != NULL);
245 for (i = 0; i < PATTERN_FILE_SIZE; i++)
248 res = g_data_output_stream_put_byte (outds, i % 256, NULL, &error);
249 g_assert (error == NULL);
252 res = g_output_stream_close (G_OUTPUT_STREAM (outs), NULL, &error);
253 g_assert (error == NULL);
254 g_object_unref (outds);
255 g_object_unref (outs);
256 g_object_unref (child);
259 g_object_unref (root);
263 file_exists (GFile * parent, const char *filename, gboolean * result)
271 child = g_file_get_child (parent, filename);
272 g_assert (child != NULL);
273 res = g_file_query_exists (child, NULL);
281 test_attributes (struct StructureItem item, GFileInfo * info)
285 const char *name, *display_name, *edit_name, *copy_name, *symlink_target;
289 gboolean can_read, can_write;
292 has_attr = g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE);
293 g_assert_cmpint (has_attr, ==, TRUE);
294 ftype = g_file_info_get_file_type (info);
295 g_assert_cmpint (ftype, !=, G_FILE_TYPE_UNKNOWN);
296 g_assert_cmpint (ftype, ==, item.file_type);
299 if ((item.mode > 0) && (posix_compat))
302 g_file_info_get_attribute_uint32 (info,
303 G_FILE_ATTRIBUTE_UNIX_MODE) & 0xFFF;
304 g_assert_cmpint (mode, ==, item.mode);
307 /* access::can-read */
308 if (item.file_type != G_FILE_TYPE_SYMBOLIC_LINK)
311 g_file_info_get_attribute_boolean (info,
312 G_FILE_ATTRIBUTE_ACCESS_CAN_READ);
313 g_assert_cmpint (can_read, ==, TRUE);
316 /* access::can-write */
317 if ((write_test) && ((item.extra_flags & TEST_OVERWRITE) == TEST_OVERWRITE))
320 g_file_info_get_attribute_boolean (info,
321 G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE);
322 g_assert_cmpint (can_write, ==, TRUE);
326 name = g_file_info_get_name (info);
327 g_assert (name != NULL);
329 /* standard::display-name */
330 display_name = g_file_info_get_display_name (info);
331 g_assert (display_name != NULL);
332 utf8_valid = g_utf8_validate (display_name, -1, NULL);
333 g_assert_cmpint (utf8_valid, ==, TRUE);
335 /* standard::edit-name */
336 edit_name = g_file_info_get_edit_name (info);
339 utf8_valid = g_utf8_validate (edit_name, -1, NULL);
340 g_assert_cmpint (utf8_valid, ==, TRUE);
343 /* standard::copy-name */
345 g_file_info_get_attribute_string (info,
346 G_FILE_ATTRIBUTE_STANDARD_COPY_NAME);
349 utf8_valid = g_utf8_validate (copy_name, -1, NULL);
350 g_assert_cmpint (utf8_valid, ==, TRUE);
353 /* standard::is-symlink */
356 is_symlink = g_file_info_get_is_symlink (info);
357 g_assert_cmpint (is_symlink, ==,
358 item.file_type == G_FILE_TYPE_SYMBOLIC_LINK);
361 /* standard::symlink-target */
362 if ((item.file_type == G_FILE_TYPE_SYMBOLIC_LINK) && (posix_compat))
364 symlink_target = g_file_info_get_symlink_target (info);
365 g_assert_cmpstr (symlink_target, ==, item.link_to);
370 test_initial_structure (gconstpointer test_data)
376 GFileInputStream *ins;
381 gssize read, total_read;
382 struct StructureItem item;
385 g_assert (test_data != NULL);
386 log ("\n Testing sample structure in '%s'...\n", (char *) test_data);
388 root = g_file_new_for_commandline_arg ((char *) test_data);
389 g_assert (root != NULL);
390 res = g_file_query_exists (root, NULL);
391 g_assert_cmpint (res, ==, TRUE);
393 /* test the structure */
394 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
396 item = sample_struct[i];
397 if (((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
398 || (item.handle_special))
401 log (" Testing file '%s'...\n", item.filename);
403 child = file_exists (root, item.filename, &res);
404 g_assert (child != NULL);
405 g_assert_cmpint (res, ==, TRUE);
409 g_file_query_info (child, "*", G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
411 g_assert (error == NULL);
412 g_assert (info != NULL);
414 test_attributes (item, info);
416 g_object_unref (child);
419 /* read and test the pattern file */
420 log (" Testing pattern file...\n");
421 child = file_exists (root, "pattern_file", &res);
422 g_assert (child != NULL);
423 g_assert_cmpint (res, ==, TRUE);
427 g_file_query_info (child, "*", G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
429 g_assert (error == NULL);
430 g_assert (info != NULL);
431 size = g_file_info_get_size (info);
432 g_assert_cmpint (size, ==, PATTERN_FILE_SIZE);
435 ins = g_file_read (child, NULL, &error);
436 g_assert (ins != NULL);
437 g_assert (error == NULL);
439 buffer = g_malloc (PATTERN_FILE_SIZE);
442 while (total_read < PATTERN_FILE_SIZE)
446 g_input_stream_read (G_INPUT_STREAM (ins), buffer + total_read,
447 PATTERN_FILE_SIZE, NULL, &error);
448 g_assert (error == NULL);
450 log (" read %d bytes, total = %d of %d.\n", read, total_read,
453 g_assert_cmpint (total_read, ==, PATTERN_FILE_SIZE);
456 res = g_input_stream_close (G_INPUT_STREAM (ins), NULL, &error);
457 g_assert (error == NULL);
458 g_assert_cmpint (res, ==, TRUE);
460 for (i = 0; i < PATTERN_FILE_SIZE; i++)
461 g_assert_cmpint (*(buffer + i), ==, i % 256);
463 g_object_unref (ins);
464 g_object_unref (child);
466 g_object_unref (root);
470 traverse_recurse_dirs (GFile * parent, GFile * root)
474 GFileEnumerator *enumerator;
481 g_assert (root != NULL);
485 g_file_enumerate_children (parent, "*",
486 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
488 g_assert (enumerator != NULL);
489 g_assert (error == NULL);
492 info = g_file_enumerator_next_file (enumerator, NULL, &error);
493 while ((info) && (!error))
495 descend = g_file_get_child (parent, g_file_info_get_name (info));
496 g_assert (descend != NULL);
497 relative_path = g_file_get_relative_path (root, descend);
498 g_assert (relative_path != NULL);
501 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
503 if (strcmp (sample_struct[i].filename, relative_path) == 0)
505 /* test the attributes again */
506 test_attributes (sample_struct[i], info);
512 g_assert_cmpint (found, ==, TRUE);
514 log (" Found file %s, relative to root: %s\n",
515 g_file_info_get_display_name (info), relative_path);
517 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
518 traverse_recurse_dirs (descend, root);
520 g_object_unref (descend);
522 info = g_file_enumerator_next_file (enumerator, NULL, &error);
524 g_assert (error == NULL);
527 res = g_file_enumerator_close (enumerator, NULL, &error);
528 g_assert_cmpint (res, ==, TRUE);
529 g_assert (error == NULL);
533 test_traverse_structure (gconstpointer test_data)
538 g_assert (test_data != NULL);
539 log ("\n Traversing through the sample structure in '%s'...\n",
542 root = g_file_new_for_commandline_arg ((char *) test_data);
543 g_assert (root != NULL);
544 res = g_file_query_exists (root, NULL);
545 g_assert_cmpint (res, ==, TRUE);
547 traverse_recurse_dirs (root, root);
549 g_object_unref (root);
556 test_enumerate (gconstpointer test_data)
561 GFileEnumerator *enumerator;
564 struct StructureItem item;
567 g_assert (test_data != NULL);
568 log ("\n Test enumerate '%s'...\n", (char *) test_data);
570 root = g_file_new_for_commandline_arg ((char *) test_data);
571 g_assert (root != NULL);
572 res = g_file_query_exists (root, NULL);
573 g_assert_cmpint (res, ==, TRUE);
576 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
578 item = sample_struct[i];
579 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
582 if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
583 (((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS)
585 || ((item.extra_flags & TEST_ENUMERATE_FILE) ==
586 TEST_ENUMERATE_FILE))
588 log (" Testing file '%s'\n", item.filename);
589 child = g_file_get_child (root, item.filename);
590 g_assert (child != NULL);
593 g_file_enumerate_children (child, "*",
594 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
597 if ((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS)
599 g_assert (enumerator == NULL);
600 g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_FOUND);
602 if ((item.extra_flags & TEST_ENUMERATE_FILE) == TEST_ENUMERATE_FILE)
604 g_assert (enumerator == NULL);
605 g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_DIRECTORY);
607 if ((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS)
609 g_assert (enumerator != NULL);
612 info = g_file_enumerator_next_file (enumerator, NULL, &error);
613 g_assert (info == NULL);
614 g_assert (error == NULL);
615 /* no items should be found, no error should be logged */
619 g_error_free (error);
624 res = g_file_enumerator_close (enumerator, NULL, &error);
625 g_assert_cmpint (res, ==, TRUE);
626 g_assert (error == NULL);
628 g_object_unref (child);
631 g_object_unref (root);
635 do_copy_move (GFile * root, struct StructureItem item, const char *target_dir,
636 enum StructureExtraFlags extra_flags)
638 GFile *dst_dir, *src_file, *dst_file;
642 log (" do_copy_move: '%s' --> '%s'\n", item.filename, target_dir);
644 dst_dir = g_file_get_child (root, target_dir);
645 g_assert (dst_dir != NULL);
646 src_file = g_file_get_child (root, item.filename);
647 g_assert (src_file != NULL);
648 dst_file = g_file_get_child (dst_dir, item.filename);
649 g_assert (dst_file != NULL);
652 if ((item.extra_flags & TEST_COPY) == TEST_COPY)
654 g_file_copy (src_file, dst_file,
655 G_FILE_COPY_NOFOLLOW_SYMLINKS |
657 TEST_OVERWRITE) ? G_FILE_COPY_OVERWRITE :
658 G_FILE_COPY_NONE), NULL, NULL, NULL, &error);
661 g_file_move (src_file, dst_file, G_FILE_COPY_NOFOLLOW_SYMLINKS, NULL,
665 log (" res = %d, error code %d = %s\n", res, error->code,
668 /* copying file/directory to itself (".") */
669 if (((item.extra_flags & TEST_NOT_EXISTS) != TEST_NOT_EXISTS) &&
670 (extra_flags == TEST_ALREADY_EXISTS))
672 g_assert_cmpint (res, ==, FALSE);
673 g_assert_cmpint (error->code, ==, G_IO_ERROR_EXISTS);
675 /* target file is a file, overwrite is not set */
676 else if (((item.extra_flags & TEST_NOT_EXISTS) != TEST_NOT_EXISTS) &&
677 (extra_flags == TEST_TARGET_IS_FILE))
679 g_assert_cmpint (res, ==, FALSE);
680 if (item.file_type == G_FILE_TYPE_DIRECTORY)
681 g_assert_cmpint (error->code, ==, G_IO_ERROR_WOULD_RECURSE);
683 g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_DIRECTORY);
685 /* source file is directory */
686 else if ((item.extra_flags & TEST_COPY_ERROR_RECURSE) ==
687 TEST_COPY_ERROR_RECURSE)
689 g_assert_cmpint (res, ==, FALSE);
690 g_assert_cmpint (error->code, ==, G_IO_ERROR_WOULD_RECURSE);
692 /* source or target path doesn't exist */
693 else if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
694 (extra_flags == TEST_NOT_EXISTS))
696 g_assert_cmpint (res, ==, FALSE);
697 g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_FOUND);
699 /* source or target path permission denied */
700 else if (((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS) ||
701 (extra_flags == TEST_NO_ACCESS))
703 g_assert_cmpint (res, ==, FALSE);
704 g_assert_cmpint (error->code, ==, G_IO_ERROR_PERMISSION_DENIED);
706 /* no error should be found, all exceptions defined above */
709 g_assert_cmpint (res, ==, TRUE);
710 g_assert (error == NULL);
714 g_error_free (error);
717 g_object_unref (dst_dir);
718 g_object_unref (src_file);
719 g_object_unref (dst_file);
723 test_copy_move (gconstpointer test_data)
728 struct StructureItem item;
732 g_assert (test_data != NULL);
733 root = g_file_new_for_commandline_arg ((char *) test_data);
734 g_assert (root != NULL);
735 res = g_file_query_exists (root, NULL);
736 g_assert_cmpint (res, ==, TRUE);
739 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
741 item = sample_struct[i];
743 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
746 if (((item.extra_flags & TEST_COPY) == TEST_COPY) ||
747 ((item.extra_flags & TEST_MOVE) == TEST_MOVE))
749 /* test copy/move to a directory, expecting no errors if source files exist */
750 do_copy_move (root, item, TEST_DIR_TARGET, 0);
752 /* some files have been already moved so we can't count with them in the tests */
753 if ((item.extra_flags & TEST_COPY) == TEST_COPY)
755 /* test overwrite for flagged files */
756 if ((item.extra_flags & TEST_OVERWRITE) == TEST_OVERWRITE)
758 do_copy_move (root, item, TEST_DIR_TARGET, TEST_OVERWRITE);
760 /* source = target, should return G_IO_ERROR_EXISTS */
761 do_copy_move (root, item, ".", TEST_ALREADY_EXISTS);
763 do_copy_move (root, item, TEST_TARGET_FILE,
764 TEST_TARGET_IS_FILE);
765 /* target path is invalid */
766 do_copy_move (root, item, TEST_NAME_NOT_EXISTS,
769 /* tests on POSIX-compatible filesystems */
772 /* target directory is not accessible (no execute flag) */
773 do_copy_move (root, item, TEST_DIR_NO_ACCESS,
775 /* target directory is readonly */
776 do_copy_move (root, item, TEST_DIR_NO_WRITE,
782 g_object_unref (root);
786 test_create (gconstpointer test_data)
792 struct StructureItem item;
793 GFileOutputStream *os;
795 g_assert (test_data != NULL);
798 root = g_file_new_for_commandline_arg ((char *) test_data);
799 g_assert (root != NULL);
800 res = g_file_query_exists (root, NULL);
801 g_assert_cmpint (res, ==, TRUE);
803 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
805 item = sample_struct[i];
807 if (((item.extra_flags & TEST_CREATE) == TEST_CREATE) ||
808 ((item.extra_flags & TEST_REPLACE) == TEST_REPLACE) ||
809 ((item.extra_flags & TEST_APPEND) == TEST_APPEND))
811 log (" test_create: '%s'\n", item.filename);
813 child = g_file_get_child (root, item.filename);
814 g_assert (child != NULL);
818 if ((item.extra_flags & TEST_CREATE) == TEST_CREATE)
819 os = g_file_create (child, item.create_flags, NULL, &error);
820 else if ((item.extra_flags & TEST_REPLACE) == TEST_REPLACE)
822 g_file_replace (child, NULL, TRUE, item.create_flags, NULL,
824 else if ((item.extra_flags & TEST_APPEND) == TEST_APPEND)
825 os = g_file_append_to (child, item.create_flags, NULL, &error);
829 log (" error code %d = %s\n", error->code, error->message);
831 if (((item.extra_flags & TEST_NOT_EXISTS) == 0) &&
832 ((item.extra_flags & TEST_CREATE) == TEST_CREATE))
834 g_assert (os == NULL);
835 g_assert (error != NULL);
836 g_assert_cmpint (error->code, ==, G_IO_ERROR_EXISTS);
838 else if (item.file_type == G_FILE_TYPE_DIRECTORY)
840 g_assert (os == NULL);
841 g_assert (error != NULL);
842 if ((item.extra_flags & TEST_CREATE) == TEST_CREATE)
843 g_assert_cmpint (error->code, ==, G_IO_ERROR_EXISTS);
845 g_assert_cmpint (error->code, ==, G_IO_ERROR_IS_DIRECTORY);
849 g_assert (os != NULL);
850 g_assert (error == NULL);
854 g_error_free (error);
860 g_output_stream_close (G_OUTPUT_STREAM (os), NULL, &error);
862 log (" g_output_stream_close: error %d = %s\n",
863 error->code, error->message);
864 g_assert_cmpint (res, ==, TRUE);
865 g_assert (error == NULL);
867 g_object_unref (child);
870 g_object_unref (root);
874 test_open (gconstpointer test_data)
880 struct StructureItem item;
881 GFileInputStream *input_stream;
883 g_assert (test_data != NULL);
886 root = g_file_new_for_commandline_arg ((char *) test_data);
887 g_assert (root != NULL);
888 res = g_file_query_exists (root, NULL);
889 g_assert_cmpint (res, ==, TRUE);
891 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
893 item = sample_struct[i];
895 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
898 if ((item.extra_flags & TEST_OPEN) == TEST_OPEN)
900 log (" test_open: '%s'\n", item.filename);
902 child = g_file_get_child (root, item.filename);
903 g_assert (child != NULL);
905 input_stream = g_file_read (child, NULL, &error);
907 if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
908 ((item.extra_flags & TEST_INVALID_SYMLINK) ==
909 TEST_INVALID_SYMLINK))
911 g_assert (input_stream == NULL);
912 g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_FOUND);
914 else if (item.file_type == G_FILE_TYPE_DIRECTORY)
916 g_assert (input_stream == NULL);
917 g_assert_cmpint (error->code, ==, G_IO_ERROR_IS_DIRECTORY);
921 g_assert (input_stream != NULL);
922 g_assert (error == NULL);
926 g_error_free (error);
932 g_input_stream_close (G_INPUT_STREAM (input_stream), NULL,
934 g_assert_cmpint (res, ==, TRUE);
935 g_assert (error == NULL);
937 g_object_unref (child);
940 g_object_unref (root);
944 test_delete (gconstpointer test_data)
951 struct StructureItem item;
953 g_assert (test_data != NULL);
956 root = g_file_new_for_commandline_arg ((char *) test_data);
957 g_assert (root != NULL);
958 res = g_file_query_exists (root, NULL);
959 g_assert_cmpint (res, ==, TRUE);
961 for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
963 item = sample_struct[i];
965 if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
968 if (((item.extra_flags & TEST_DELETE_NORMAL) == TEST_DELETE_NORMAL) ||
969 ((item.extra_flags & TEST_DELETE_TRASH) == TEST_DELETE_TRASH))
971 child = file_exists (root, item.filename, &res);
972 g_assert (child != NULL);
973 /* we don't care about result here */
975 log (" Deleting %s, path = %s\n", item.filename,
976 g_file_get_path (child));
978 if ((item.extra_flags & TEST_DELETE_NORMAL) == TEST_DELETE_NORMAL)
979 res = g_file_delete (child, NULL, &error);
981 res = g_file_trash (child, NULL, &error);
983 if ((item.extra_flags & TEST_DELETE_NON_EMPTY) ==
984 TEST_DELETE_NON_EMPTY)
986 g_assert_cmpint (res, ==, FALSE);
987 g_assert (error != NULL);
988 g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_EMPTY);
990 if ((item.extra_flags & TEST_DELETE_FAILURE) == TEST_DELETE_FAILURE)
992 g_assert_cmpint (res, ==, FALSE);
993 g_assert (error != NULL);
994 g_assert_cmpint (error->code, !=, 0);
996 if ((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS)
998 g_assert_cmpint (res, ==, FALSE);
999 g_assert (error != NULL);
1000 g_assert_cmpint (error->code, ==, G_IO_ERROR_NOT_FOUND);
1005 log (" result = %d, error = %s\n", res, error->message);
1006 g_error_free (error);
1009 g_object_unref (child);
1012 g_object_unref (root);
1017 cleanup_dir_recurse (GFile *parent, GFile *root)
1021 GFileEnumerator *enumerator;
1024 char *relative_path;
1026 g_assert (root != NULL);
1030 g_file_enumerate_children (parent, "*",
1031 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
1037 info = g_file_enumerator_next_file (enumerator, NULL, &error);
1038 while ((info) && (!error))
1040 descend = g_file_get_child (parent, g_file_info_get_name (info));
1041 g_assert (descend != NULL);
1042 relative_path = g_file_get_relative_path (root, descend);
1043 g_assert (relative_path != NULL);
1045 log (" deleting '%s'\n", g_file_info_get_display_name (info));
1047 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
1048 cleanup_dir_recurse (descend, root);
1051 res = g_file_delete (descend, NULL, &error);
1052 g_assert_cmpint (res, ==, TRUE);
1054 g_object_unref (descend);
1056 info = g_file_enumerator_next_file (enumerator, NULL, &error);
1058 g_assert (error == NULL);
1061 res = g_file_enumerator_close (enumerator, NULL, &error);
1062 g_assert_cmpint (res, ==, TRUE);
1063 g_assert (error == NULL);
1067 prep_clean_structure (gconstpointer test_data)
1071 g_assert (test_data != NULL);
1072 log ("\n Cleaning target testing structure in '%s'...\n",
1073 (char *) test_data);
1075 root = g_file_new_for_commandline_arg ((char *) test_data);
1076 g_assert (root != NULL);
1078 cleanup_dir_recurse (root, root);
1080 g_file_delete (root, NULL, NULL);
1082 g_object_unref (root);
1086 main (int argc, char *argv[])
1088 static gboolean only_create_struct;
1089 static char *target_path;
1091 GOptionContext *context;
1093 static GOptionEntry cmd_entries[] = {
1094 {"read-write", 'w', 0, G_OPTION_ARG_NONE, &write_test,
1095 "Perform write tests (incl. structure creation)", NULL},
1096 {"create-struct", 'c', 0, G_OPTION_ARG_NONE, &only_create_struct,
1097 "Only create testing structure (no tests)", NULL},
1098 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL},
1099 {"posix", 'x', 0, G_OPTION_ARG_NONE, &posix_compat,
1100 "Test POSIX-specific features (unix permissions, symlinks)", NULL},
1106 only_create_struct = FALSE;
1108 posix_compat = FALSE;
1110 /* strip all gtester-specific args */
1112 g_test_init (&argc, &argv, NULL);
1114 /* no extra parameters specified, assume we're executed from glib test suite */
1119 only_create_struct = FALSE;
1120 target_path = DEFAULT_TEST_DIR;
1121 #ifdef G_PLATFORM_WIN32
1122 posix_compat = FALSE;
1124 posix_compat = TRUE;
1128 /* add trailing args */
1130 context = g_option_context_new ("target_path");
1131 g_option_context_add_main_entries (context, cmd_entries, NULL);
1132 if (!g_option_context_parse (context, &argc, &argv, &error))
1134 g_print ("option parsing failed: %s\n", error->message);
1135 return g_test_run ();
1138 /* remaining arg should is the target path; we don't care of the extra args here */
1140 target_path = strdup (argv[1]);
1144 g_print ("error: target path was not specified\n");
1145 g_print (g_option_context_get_help (context, TRUE, NULL));
1146 return g_test_run ();
1150 /* Write test - clean target directory first */
1151 /* this can be also considered as a test - enumerate + delete */
1152 if (write_test || only_create_struct)
1153 g_test_add_data_func ("/live-g-file/prep_clean_structure", target_path,
1154 prep_clean_structure);
1156 /* Write test - create new testing structure */
1157 if (write_test || only_create_struct)
1158 g_test_add_data_func ("/live-g-file/create_structure", target_path,
1159 test_create_structure);
1161 /* Read test - test the sample structure - expect defined attributes to be there */
1162 if (!only_create_struct)
1163 g_test_add_data_func ("/live-g-file/test_initial_structure", target_path,
1164 test_initial_structure);
1166 /* Read test - test traverse the structure - no special file should appear */
1167 if (!only_create_struct)
1168 g_test_add_data_func ("/live-g-file/test_traverse_structure", target_path,
1169 test_traverse_structure);
1171 /* Read test - enumerate */
1172 if (!only_create_struct)
1173 g_test_add_data_func ("/live-g-file/test_enumerate", target_path,
1176 /* Read test - open (g_file_read()) */
1177 if (!only_create_struct)
1178 g_test_add_data_func ("/live-g-file/test_open", target_path, test_open);
1180 /* Write test - create */
1181 if (write_test && (!only_create_struct))
1182 g_test_add_data_func ("/live-g-file/test_create", target_path,
1185 /* Write test - copy, move */
1186 if (write_test && (!only_create_struct))
1187 g_test_add_data_func ("/live-g-file/test_copy_move", target_path,
1190 /* Write test - delete, trash */
1191 if (write_test && (!only_create_struct))
1192 g_test_add_data_func ("/live-g-file/test_delete", target_path,
1195 if (write_test || only_create_struct)
1196 g_test_add_data_func ("/live-g-file/final_clean", target_path,
1197 prep_clean_structure);
1199 return g_test_run ();