32203b6c76d485a527863c106bc859e115a0bc23
[platform/upstream/glib.git] / gio / tests / live-g-file.c
1 /* GLib testing framework examples and tests
2  * Copyright (C) 2008 Red Hat, Inc.
3  * Authors: Tomas Bzatek <tbzatek@redhat.com>
4  *
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.
8  *
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.
12  *
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.
21  */
22
23 #include <glib/glib.h>
24 #include <gio/gio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <string.h>
29 #include <sys/stat.h>
30
31 #define DEFAULT_TEST_DIR                "testdir_live-g-file"
32
33 #define PATTERN_FILE_SIZE       0x10000
34 #define TEST_HANDLE_SPECIAL     TRUE
35
36 enum StructureExtraFlags
37 {
38   TEST_DELETE_NORMAL = 1 << 0,
39   TEST_DELETE_TRASH = 1 << 1,
40   TEST_DELETE_NON_EMPTY = 1 << 2,
41   TEST_DELETE_FAILURE = 1 << 3,
42   TEST_NOT_EXISTS = 1 << 4,
43   TEST_ENUMERATE_FILE = 1 << 5,
44   TEST_NO_ACCESS = 1 << 6,
45   TEST_COPY = 1 << 7,
46   TEST_MOVE = 1 << 8,
47   TEST_COPY_ERROR_RECURSE = 1 << 9,
48   TEST_ALREADY_EXISTS = 1 << 10,
49   TEST_TARGET_IS_FILE = 1 << 11,
50   TEST_CREATE = 1 << 12,
51   TEST_REPLACE = 1 << 13,
52   TEST_APPEND = 1 << 14,
53   TEST_OPEN = 1 << 15,
54   TEST_OVERWRITE = 1 << 16,
55   TEST_INVALID_SYMLINK = 1 << 17,
56 };
57
58 struct StructureItem
59 {
60   const char *filename;
61   const char *link_to;
62   GFileType file_type;
63   GFileCreateFlags create_flags;
64   guint32 mode;
65   gboolean handle_special;
66   enum StructureExtraFlags extra_flags;
67 };
68
69 #define TEST_DIR_NO_ACCESS              "dir_no-access"
70 #define TEST_DIR_NO_WRITE               "dir_no-write"
71 #define TEST_DIR_TARGET                 "dir-target"
72 #define TEST_NAME_NOT_EXISTS    "not_exists"
73 #define TEST_TARGET_FILE                "target-file"
74
75
76 static const struct StructureItem sample_struct[] = {
77 /*       filename                               link    file_type                               create_flags            mode | handle_special | extra_flags              */
78     {"dir1",                            NULL,   G_FILE_TYPE_DIRECTORY,  G_FILE_CREATE_NONE, 0, 0, TEST_DELETE_NORMAL | TEST_DELETE_NON_EMPTY | TEST_REPLACE | TEST_OPEN},
79     {"dir1/subdir",                     NULL,   G_FILE_TYPE_DIRECTORY,  G_FILE_CREATE_NONE, 0, 0, TEST_COPY     | TEST_COPY_ERROR_RECURSE | TEST_APPEND},
80     {"dir2",                            NULL,   G_FILE_TYPE_DIRECTORY,  G_FILE_CREATE_NONE, 0, 0, TEST_DELETE_NORMAL | TEST_MOVE | TEST_CREATE},
81     {TEST_DIR_TARGET,           NULL,   G_FILE_TYPE_DIRECTORY,  G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_COPY_ERROR_RECURSE},
82     {TEST_DIR_NO_ACCESS,        NULL,   G_FILE_TYPE_DIRECTORY,  G_FILE_CREATE_PRIVATE, S_IRUSR + S_IWUSR + S_IRGRP + S_IWGRP + S_IROTH + S_IWOTH, 0, TEST_NO_ACCESS | TEST_OPEN},
83     {TEST_DIR_NO_WRITE,         NULL,   G_FILE_TYPE_DIRECTORY,  G_FILE_CREATE_PRIVATE, S_IRUSR + S_IXUSR + S_IRGRP + S_IXGRP + S_IROTH + S_IXOTH, 0, 0},
84     {TEST_TARGET_FILE,          NULL,   G_FILE_TYPE_REGULAR,    G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_OPEN},
85         {"normal_file",                 NULL,   G_FILE_TYPE_REGULAR,    G_FILE_CREATE_NONE, 0, 0, TEST_ENUMERATE_FILE | TEST_CREATE | TEST_OVERWRITE},
86         {"normal_file-symlink", "normal_file",  G_FILE_TYPE_SYMBOLIC_LINK, G_FILE_CREATE_NONE, 0, 0, TEST_ENUMERATE_FILE | TEST_COPY | TEST_OPEN},
87     {"executable_file",         NULL,   G_FILE_TYPE_REGULAR,    G_FILE_CREATE_NONE, S_IRWXU + S_IRWXG + S_IRWXO, 0, TEST_DELETE_TRASH | TEST_COPY | TEST_OPEN | TEST_OVERWRITE | TEST_REPLACE},
88     {"private_file",            NULL,   G_FILE_TYPE_REGULAR,    G_FILE_CREATE_PRIVATE, 0, 0, TEST_COPY | TEST_OPEN | TEST_OVERWRITE | TEST_APPEND},
89     {"normal_file2",            NULL,   G_FILE_TYPE_REGULAR,    G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_OVERWRITE | TEST_REPLACE},
90     {"readonly_file",           NULL,   G_FILE_TYPE_REGULAR,    G_FILE_CREATE_NONE, S_IRUSR + S_IRGRP + S_IROTH, 0, TEST_DELETE_NORMAL | TEST_OPEN},
91     {"UTF_pr\xcc\x8ci\xcc\x81lis\xcc\x8c z",
92                                                 NULL,   G_FILE_TYPE_REGULAR,    G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_CREATE | TEST_OPEN | TEST_OVERWRITE},
93     {"dir_pr\xcc\x8ci\xcc\x81lis\xcc\x8c z",
94                                                 NULL,   G_FILE_TYPE_DIRECTORY,  G_FILE_CREATE_NONE, 0, 0, TEST_DELETE_NORMAL | TEST_CREATE},
95     {"pattern_file",            NULL,   G_FILE_TYPE_REGULAR,    G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_COPY | TEST_OPEN | TEST_APPEND},
96     {TEST_NAME_NOT_EXISTS,      NULL,   G_FILE_TYPE_REGULAR,    G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_DELETE_NORMAL | TEST_NOT_EXISTS | TEST_COPY | TEST_OPEN},
97     {TEST_NAME_NOT_EXISTS,      NULL,   G_FILE_TYPE_REGULAR,    G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_DELETE_TRASH | TEST_NOT_EXISTS | TEST_MOVE},
98     {"not_exists2",                     NULL,   G_FILE_TYPE_REGULAR,    G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_NOT_EXISTS | TEST_CREATE},
99     {"not_exists3",                     NULL,   G_FILE_TYPE_REGULAR,    G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_NOT_EXISTS | TEST_REPLACE},
100     {"not_exists4",                     NULL,   G_FILE_TYPE_REGULAR,    G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_NOT_EXISTS | TEST_APPEND},
101     {"dir_no-execute/file",     NULL,   G_FILE_TYPE_REGULAR,    G_FILE_CREATE_NONE, 0, TEST_HANDLE_SPECIAL, TEST_DELETE_NORMAL | TEST_DELETE_FAILURE | TEST_NOT_EXISTS | TEST_OPEN},
102         {"lost_symlink",                "nowhere",      G_FILE_TYPE_SYMBOLIC_LINK, G_FILE_CREATE_NONE, 0, 0, TEST_COPY | TEST_DELETE_NORMAL | TEST_OPEN | TEST_INVALID_SYMLINK},
103   };
104
105 static gboolean test_suite;
106 static gboolean write_test;
107 static gboolean verbose;
108 static gboolean posix_compat;
109
110 #ifdef G_HAVE_ISO_VARARGS
111 #define log(...) if (verbose)  g_print (__VA_ARGS__)
112 #elif defined(G_HAVE_GNUC_VARARGS)
113 #define log(msg...) if (verbose)  g_print (msg)
114 #else  /* no varargs macros */
115 static void log (const g_char *format, ...)
116 {
117   va_list args;
118   va_start (args, format);
119   if (verbose) g_print (format, args);
120   va_end (args);
121 }
122 #endif
123
124 static GFile *
125 create_empty_file (GFile * parent, const char *filename,
126                    GFileCreateFlags create_flags)
127 {
128   GFile *child;
129   gboolean res;
130   GError *error;
131   GFileOutputStream *outs;
132
133   child = g_file_get_child (parent, filename);
134   g_assert (child != NULL);
135
136   error = NULL;
137   outs = g_file_replace (child, NULL, FALSE, create_flags, NULL, &error);
138   g_assert_no_error (error);
139   g_assert (outs != NULL);
140   error = NULL;
141   res = g_output_stream_close (G_OUTPUT_STREAM (outs), NULL, &error);
142   g_object_unref (outs);
143   return child;
144 }
145
146 static GFile *
147 create_empty_dir (GFile * parent, const char *filename)
148 {
149   GFile *child;
150   gboolean res;
151   GError *error;
152
153   child = g_file_get_child (parent, filename);
154   g_assert (child != NULL);
155   error = NULL;
156   res = g_file_make_directory (child, NULL, &error);
157   g_assert_cmpint (res, ==, TRUE);
158   g_assert_no_error (error);
159   return child;
160 }
161
162 static GFile *
163 create_symlink (GFile * parent, const char *filename, const char *points_to)
164 {
165   GFile *child;
166   gboolean res;
167   GError *error;
168
169   child = g_file_get_child (parent, filename);
170   g_assert (child != NULL);
171   error = NULL;
172   res = g_file_make_symbolic_link (child, points_to, NULL, &error);
173   g_assert_cmpint (res, ==, TRUE);
174   g_assert_no_error (error);
175   return child;
176 }
177
178 static void
179 test_create_structure (gconstpointer test_data)
180 {
181   GFile *root;
182   GFile *child;
183   gboolean res;
184   GError *error;
185   GFileOutputStream *outs;
186   GDataOutputStream *outds;
187   guint i;
188   struct StructureItem item;
189
190   g_assert (test_data != NULL);
191   log ("\n  Going to create testing structure in '%s'...\n",
192        (char *) test_data);
193
194   root = g_file_new_for_commandline_arg ((char *) test_data);
195   g_assert (root != NULL);
196
197   /*  create root directory  */
198   res = g_file_make_directory (root, NULL, NULL);
199   /*  don't care about errors here  */
200
201   /*  create any other items  */
202   for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
203     {
204       item = sample_struct[i];
205       if ((item.handle_special)
206           || ((!posix_compat)
207               && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK)))
208         continue;
209
210       child = NULL;
211       switch (item.file_type)
212         {
213         case G_FILE_TYPE_REGULAR:
214           log ("    Creating file '%s'...\n", item.filename);
215           child = create_empty_file (root, item.filename, item.create_flags);
216           break;
217         case G_FILE_TYPE_DIRECTORY:
218           log ("    Creating directory '%s'...\n", item.filename);
219           child = create_empty_dir (root, item.filename);
220           break;
221         case G_FILE_TYPE_SYMBOLIC_LINK:
222           log ("    Creating symlink '%s' --> '%s'...\n", item.filename,
223                item.link_to);
224           child = create_symlink (root, item.filename, item.link_to);
225           break;
226         case G_FILE_TYPE_UNKNOWN:
227         case G_FILE_TYPE_SPECIAL:
228         case G_FILE_TYPE_SHORTCUT:
229         case G_FILE_TYPE_MOUNTABLE:
230         default:
231           break;
232         }
233       g_assert (child != NULL);
234
235       if ((item.mode > 0) && (posix_compat))
236         {
237           error = NULL;
238           res =
239             g_file_set_attribute_uint32 (child, G_FILE_ATTRIBUTE_UNIX_MODE,
240                                          item.mode,
241                                          G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
242                                          NULL, &error);
243           g_assert_cmpint (res, ==, TRUE);
244           g_assert_no_error (error);
245         }
246
247       g_object_unref (child);
248     }
249
250   /*  create a pattern file  */
251   log ("    Creating pattern file...");
252   child = g_file_get_child (root, "pattern_file");
253   g_assert (child != NULL);
254
255   error = NULL;
256   outs =
257     g_file_replace (child, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error);
258   g_assert_no_error (error);
259
260   g_assert (outs != NULL);
261   outds = g_data_output_stream_new (G_OUTPUT_STREAM (outs));
262   g_assert (outds != NULL);
263   for (i = 0; i < PATTERN_FILE_SIZE; i++)
264     {
265       error = NULL;
266       res = g_data_output_stream_put_byte (outds, i % 256, NULL, &error);
267       g_assert_no_error (error);
268     }
269   error = NULL;
270   res = g_output_stream_close (G_OUTPUT_STREAM (outs), NULL, &error);
271   g_assert_no_error (error);
272   g_object_unref (outds);
273   g_object_unref (outs);
274   g_object_unref (child);
275   log (" done.\n");
276
277   g_object_unref (root);
278 }
279
280 static GFile *
281 file_exists (GFile * parent, const char *filename, gboolean * result)
282 {
283   GFile *child;
284   gboolean res;
285
286   if (result)
287     *result = FALSE;
288
289   child = g_file_get_child (parent, filename);
290   g_assert (child != NULL);
291   res = g_file_query_exists (child, NULL);
292   if (result)
293     *result = res;
294
295   return child;
296 }
297
298 static void
299 test_attributes (struct StructureItem item, GFileInfo * info)
300 {
301   GFileType ftype;
302   guint32 mode;
303   const char *name, *display_name, *edit_name, *copy_name, *symlink_target;
304   gboolean utf8_valid;
305   gboolean has_attr;
306   gboolean is_symlink;
307   gboolean can_read, can_write;
308
309   /*  standard::type  */
310   has_attr = g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE);
311   g_assert_cmpint (has_attr, ==, TRUE);
312   ftype = g_file_info_get_file_type (info);
313   g_assert_cmpint (ftype, !=, G_FILE_TYPE_UNKNOWN);
314   g_assert_cmpint (ftype, ==, item.file_type);
315
316   /*  unix::mode  */
317   if ((item.mode > 0) && (posix_compat))
318     {
319       mode =
320         g_file_info_get_attribute_uint32 (info,
321                                           G_FILE_ATTRIBUTE_UNIX_MODE) & 0xFFF;
322       g_assert_cmpint (mode, ==, item.mode);
323     }
324
325   /*  access::can-read  */
326   if (item.file_type != G_FILE_TYPE_SYMBOLIC_LINK)
327     {
328       can_read =
329         g_file_info_get_attribute_boolean (info,
330                                            G_FILE_ATTRIBUTE_ACCESS_CAN_READ);
331       g_assert_cmpint (can_read, ==, TRUE);
332     }
333
334   /*  access::can-write  */
335   if ((write_test) && ((item.extra_flags & TEST_OVERWRITE) == TEST_OVERWRITE))
336     {
337       can_write =
338         g_file_info_get_attribute_boolean (info,
339                                            G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE);
340       g_assert_cmpint (can_write, ==, TRUE);
341     }
342
343   /*  standard::name  */
344   name = g_file_info_get_name (info);
345   g_assert (name != NULL);
346
347   /*  standard::display-name  */
348   display_name = g_file_info_get_display_name (info);
349   g_assert (display_name != NULL);
350   utf8_valid = g_utf8_validate (display_name, -1, NULL);
351   g_assert_cmpint (utf8_valid, ==, TRUE);
352
353   /*  standard::edit-name  */
354   edit_name = g_file_info_get_edit_name (info);
355   if (edit_name)
356     {
357       utf8_valid = g_utf8_validate (edit_name, -1, NULL);
358       g_assert_cmpint (utf8_valid, ==, TRUE);
359     }
360
361   /*  standard::copy-name  */
362   copy_name =
363     g_file_info_get_attribute_string (info,
364                                       G_FILE_ATTRIBUTE_STANDARD_COPY_NAME);
365   if (copy_name)
366     {
367       utf8_valid = g_utf8_validate (copy_name, -1, NULL);
368       g_assert_cmpint (utf8_valid, ==, TRUE);
369     }
370
371   /*  standard::is-symlink  */
372   if (posix_compat)
373     {
374       is_symlink = g_file_info_get_is_symlink (info);
375       g_assert_cmpint (is_symlink, ==,
376                        item.file_type == G_FILE_TYPE_SYMBOLIC_LINK);
377     }
378
379   /*  standard::symlink-target  */
380   if ((item.file_type == G_FILE_TYPE_SYMBOLIC_LINK) && (posix_compat))
381     {
382       symlink_target = g_file_info_get_symlink_target (info);
383       g_assert_cmpstr (symlink_target, ==, item.link_to);
384     }
385 }
386
387 static void
388 test_initial_structure (gconstpointer test_data)
389 {
390   GFile *root;
391   GFile *child;
392   gboolean res;
393   GError *error;
394   GFileInputStream *ins;
395   guint i;
396   GFileInfo *info;
397   guint32 size;
398   guchar *buffer;
399   gssize read, total_read;
400   struct StructureItem item;
401
402
403   g_assert (test_data != NULL);
404   log ("\n  Testing sample structure in '%s'...\n", (char *) test_data);
405
406   root = g_file_new_for_commandline_arg ((char *) test_data);
407   g_assert (root != NULL);
408   res = g_file_query_exists (root, NULL);
409   g_assert_cmpint (res, ==, TRUE);
410
411   /*  test the structure  */
412   for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
413     {
414       item = sample_struct[i];
415       if (((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
416           || (item.handle_special))
417         continue;
418
419       log ("    Testing file '%s'...\n", item.filename);
420
421       child = file_exists (root, item.filename, &res);
422       g_assert (child != NULL);
423       g_assert_cmpint (res, ==, TRUE);
424
425       error = NULL;
426       info =
427         g_file_query_info (child, "*", G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
428                            NULL, &error);
429       g_assert_no_error (error);
430       g_assert (info != NULL);
431
432       test_attributes (item, info);
433
434       g_object_unref (child);
435     }
436
437   /*  read and test the pattern file  */
438   log ("    Testing pattern file...\n");
439   child = file_exists (root, "pattern_file", &res);
440   g_assert (child != NULL);
441   g_assert_cmpint (res, ==, TRUE);
442
443   error = NULL;
444   info =
445     g_file_query_info (child, "*", G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
446                        &error);
447   g_assert_no_error (error);
448   g_assert (info != NULL);
449   size = g_file_info_get_size (info);
450   g_assert_cmpint (size, ==, PATTERN_FILE_SIZE);
451
452   error = NULL;
453   ins = g_file_read (child, NULL, &error);
454   g_assert (ins != NULL);
455   g_assert_no_error (error);
456
457   buffer = g_malloc (PATTERN_FILE_SIZE);
458   total_read = 0;
459
460   while (total_read < PATTERN_FILE_SIZE)
461     {
462       error = NULL;
463       read =
464         g_input_stream_read (G_INPUT_STREAM (ins), buffer + total_read,
465                              PATTERN_FILE_SIZE, NULL, &error);
466       g_assert_no_error (error);
467       total_read += read;
468       log ("      read %"G_GSSIZE_FORMAT" bytes, total = %"G_GSSIZE_FORMAT" of %d.\n",
469            read, total_read, PATTERN_FILE_SIZE);
470     }
471   g_assert_cmpint (total_read, ==, PATTERN_FILE_SIZE);
472
473   error = NULL;
474   res = g_input_stream_close (G_INPUT_STREAM (ins), NULL, &error);
475   g_assert_no_error (error);
476   g_assert_cmpint (res, ==, TRUE);
477
478   for (i = 0; i < PATTERN_FILE_SIZE; i++)
479     g_assert_cmpint (*(buffer + i), ==, i % 256);
480
481   g_object_unref (ins);
482   g_object_unref (child);
483   g_free (buffer);
484   g_object_unref (root);
485 }
486
487 static void
488 traverse_recurse_dirs (GFile * parent, GFile * root)
489 {
490   gboolean res;
491   GError *error;
492   GFileEnumerator *enumerator;
493   GFileInfo *info;
494   GFile *descend;
495   char *relative_path;
496   guint i;
497   gboolean found;
498
499   g_assert (root != NULL);
500
501   error = NULL;
502   enumerator =
503     g_file_enumerate_children (parent, "*",
504                                G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
505                                &error);
506   g_assert (enumerator != NULL);
507   g_assert_no_error (error);
508
509   g_assert (g_file_enumerator_get_container (enumerator) == parent);
510
511   error = NULL;
512   info = g_file_enumerator_next_file (enumerator, NULL, &error);
513   while ((info) && (!error))
514     {
515       descend = g_file_get_child (parent, g_file_info_get_name (info));
516       g_assert (descend != NULL);
517       relative_path = g_file_get_relative_path (root, descend);
518       g_assert (relative_path != NULL);
519
520       found = FALSE;
521       for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
522         {
523           if (strcmp (sample_struct[i].filename, relative_path) == 0)
524             {
525               /*  test the attributes again  */
526               test_attributes (sample_struct[i], info);
527
528               found = TRUE;
529               break;
530             }
531         }
532       g_assert_cmpint (found, ==, TRUE);
533
534       log ("  Found file %s, relative to root: %s\n",
535            g_file_info_get_display_name (info), relative_path);
536
537       if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
538         traverse_recurse_dirs (descend, root);
539
540       g_object_unref (descend);
541       error = NULL;
542       info = g_file_enumerator_next_file (enumerator, NULL, &error);
543     }
544   g_assert_no_error (error);
545
546   error = NULL;
547   res = g_file_enumerator_close (enumerator, NULL, &error);
548   g_assert_cmpint (res, ==, TRUE);
549   g_assert_no_error (error);
550   g_assert (g_file_enumerator_is_closed (enumerator));
551
552   g_object_unref (enumerator);
553 }
554
555 static void
556 test_traverse_structure (gconstpointer test_data)
557 {
558   GFile *root;
559   gboolean res;
560
561   g_assert (test_data != NULL);
562   log ("\n  Traversing through the sample structure in '%s'...\n",
563        (char *) test_data);
564
565   root = g_file_new_for_commandline_arg ((char *) test_data);
566   g_assert (root != NULL);
567   res = g_file_query_exists (root, NULL);
568   g_assert_cmpint (res, ==, TRUE);
569
570   traverse_recurse_dirs (root, root);
571
572   g_object_unref (root);
573 }
574
575
576
577
578 static void
579 test_enumerate (gconstpointer test_data)
580 {
581   GFile *root, *child;
582   gboolean res;
583   GError *error;
584   GFileEnumerator *enumerator;
585   GFileInfo *info;
586   guint i;
587   struct StructureItem item;
588
589
590   g_assert (test_data != NULL);
591   log ("\n  Test enumerate '%s'...\n", (char *) test_data);
592
593   root = g_file_new_for_commandline_arg ((char *) test_data);
594   g_assert (root != NULL);
595   res = g_file_query_exists (root, NULL);
596   g_assert_cmpint (res, ==, TRUE);
597
598
599   for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
600     {
601       item = sample_struct[i];
602       if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
603         continue;
604
605       if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
606           (((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS)
607            && posix_compat)
608           || ((item.extra_flags & TEST_ENUMERATE_FILE) ==
609               TEST_ENUMERATE_FILE))
610         {
611           log ("    Testing file '%s'\n", item.filename);
612           child = g_file_get_child (root, item.filename);
613           g_assert (child != NULL);
614           error = NULL;
615           enumerator =
616             g_file_enumerate_children (child, "*",
617                                        G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
618                                        NULL, &error);
619
620           if ((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS)
621             {
622               g_assert (enumerator == NULL);
623               g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
624             }
625           if ((item.extra_flags & TEST_ENUMERATE_FILE) == TEST_ENUMERATE_FILE)
626             {
627               g_assert (enumerator == NULL);
628               g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY);
629             }
630           if ((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS)
631             {
632               g_assert (enumerator != NULL);
633
634               error = NULL;
635               info = g_file_enumerator_next_file (enumerator, NULL, &error);
636               g_assert (info == NULL);
637               g_assert_no_error (error);
638               /*  no items should be found, no error should be logged  */
639             }
640
641           if (error)
642             g_error_free (error);
643
644           if (enumerator)
645             {
646               error = NULL;
647               res = g_file_enumerator_close (enumerator, NULL, &error);
648               g_assert_cmpint (res, ==, TRUE);
649               g_assert_no_error (error);
650
651               g_object_unref (enumerator);
652             }
653           g_object_unref (child);
654         }
655     }
656   g_object_unref (root);
657 }
658
659 static void
660 do_copy_move (GFile * root, struct StructureItem item, const char *target_dir,
661               enum StructureExtraFlags extra_flags)
662 {
663   GFile *dst_dir, *src_file, *dst_file;
664   gboolean res;
665   GError *error;
666
667   log ("    do_copy_move: '%s' --> '%s'\n", item.filename, target_dir);
668
669   dst_dir = g_file_get_child (root, target_dir);
670   g_assert (dst_dir != NULL);
671   src_file = g_file_get_child (root, item.filename);
672   g_assert (src_file != NULL);
673   dst_file = g_file_get_child (dst_dir, item.filename);
674   g_assert (dst_file != NULL);
675
676   error = NULL;
677   if ((item.extra_flags & TEST_COPY) == TEST_COPY)
678     res =
679       g_file_copy (src_file, dst_file,
680                    G_FILE_COPY_NOFOLLOW_SYMLINKS |
681                    ((extra_flags ==
682                      TEST_OVERWRITE) ? G_FILE_COPY_OVERWRITE :
683                     G_FILE_COPY_NONE), NULL, NULL, NULL, &error);
684   else
685     res =
686       g_file_move (src_file, dst_file, G_FILE_COPY_NOFOLLOW_SYMLINKS, NULL,
687                    NULL, NULL, &error);
688
689   if (error)
690     log ("       res = %d, error code %d = %s\n", res, error->code,
691          error->message);
692
693   /*  copying file/directory to itself (".")  */
694   if (((item.extra_flags & TEST_NOT_EXISTS) != TEST_NOT_EXISTS) &&
695       (extra_flags == TEST_ALREADY_EXISTS))
696     {
697       g_assert_cmpint (res, ==, FALSE);
698       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
699     }
700   /*  target file is a file, overwrite is not set  */
701   else if (((item.extra_flags & TEST_NOT_EXISTS) != TEST_NOT_EXISTS) &&
702            (extra_flags == TEST_TARGET_IS_FILE))
703     {
704       g_assert_cmpint (res, ==, FALSE);
705       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY);
706     }
707   /*  source file is directory  */
708   else if ((item.extra_flags & TEST_COPY_ERROR_RECURSE) ==
709            TEST_COPY_ERROR_RECURSE)
710     {
711       g_assert_cmpint (res, ==, FALSE);
712       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE);
713     }
714   /*  source or target path doesn't exist  */
715   else if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
716            (extra_flags == TEST_NOT_EXISTS))
717     {
718       g_assert_cmpint (res, ==, FALSE);
719       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
720     }
721   /*  source or target path permission denied  */
722   else if (((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS) ||
723            (extra_flags == TEST_NO_ACCESS))
724     {
725       /* This works for root, see bug #552912 */
726       if (test_suite && getuid () == 0)
727         {
728           g_assert_cmpint (res, ==, TRUE);
729           g_assert_no_error (error);
730         }
731       else
732         {
733           g_assert_cmpint (res, ==, FALSE);
734           g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED);
735         }
736     }
737   /*  no error should be found, all exceptions defined above  */
738   else
739     {
740       g_assert_cmpint (res, ==, TRUE);
741       g_assert_no_error (error);
742     }
743
744   if (error)
745     g_error_free (error);
746
747
748   g_object_unref (dst_dir);
749   g_object_unref (src_file);
750   g_object_unref (dst_file);
751 }
752
753 static void
754 test_copy_move (gconstpointer test_data)
755 {
756   GFile *root;
757   gboolean res;
758   guint i;
759   struct StructureItem item;
760
761   log ("\n");
762
763   g_assert (test_data != NULL);
764   root = g_file_new_for_commandline_arg ((char *) test_data);
765   g_assert (root != NULL);
766   res = g_file_query_exists (root, NULL);
767   g_assert_cmpint (res, ==, TRUE);
768
769
770   for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
771     {
772       item = sample_struct[i];
773
774       if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
775         continue;
776
777       if (((item.extra_flags & TEST_COPY) == TEST_COPY) ||
778           ((item.extra_flags & TEST_MOVE) == TEST_MOVE))
779         {
780           /*  test copy/move to a directory, expecting no errors if source files exist  */
781           do_copy_move (root, item, TEST_DIR_TARGET, 0);
782
783           /*  some files have been already moved so we can't count with them in the tests  */
784           if ((item.extra_flags & TEST_COPY) == TEST_COPY)
785             {
786               /*  test overwrite for flagged files  */
787               if ((item.extra_flags & TEST_OVERWRITE) == TEST_OVERWRITE)
788                 {
789                   do_copy_move (root, item, TEST_DIR_TARGET, TEST_OVERWRITE);
790                 }
791               /*  source = target, should return G_IO_ERROR_EXISTS  */
792               do_copy_move (root, item, ".", TEST_ALREADY_EXISTS);
793               /*  target is file  */
794               do_copy_move (root, item, TEST_TARGET_FILE,
795                             TEST_TARGET_IS_FILE);
796               /*  target path is invalid  */
797               do_copy_move (root, item, TEST_NAME_NOT_EXISTS,
798                             TEST_NOT_EXISTS);
799
800               /*  tests on POSIX-compatible filesystems  */
801               if (posix_compat)
802                 {
803                   /*  target directory is not accessible (no execute flag)  */
804                   do_copy_move (root, item, TEST_DIR_NO_ACCESS,
805                                 TEST_NO_ACCESS);
806                   /*  target directory is readonly  */
807                   do_copy_move (root, item, TEST_DIR_NO_WRITE,
808                                 TEST_NO_ACCESS);
809                 }
810             }
811         }
812     }
813   g_object_unref (root);
814 }
815
816 static void
817 test_create (gconstpointer test_data)
818 {
819   GFile *root, *child;
820   gboolean res;
821   GError *error;
822   guint i;
823   struct StructureItem item;
824   GFileOutputStream *os;
825
826   g_assert (test_data != NULL);
827   log ("\n");
828
829   root = g_file_new_for_commandline_arg ((char *) test_data);
830   g_assert (root != NULL);
831   res = g_file_query_exists (root, NULL);
832   g_assert_cmpint (res, ==, TRUE);
833
834   for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
835     {
836       item = sample_struct[i];
837
838       if (((item.extra_flags & TEST_CREATE) == TEST_CREATE) ||
839           ((item.extra_flags & TEST_REPLACE) == TEST_REPLACE) ||
840           ((item.extra_flags & TEST_APPEND) == TEST_APPEND))
841         {
842           log ("  test_create: '%s'\n", item.filename);
843
844           child = g_file_get_child (root, item.filename);
845           g_assert (child != NULL);
846           error = NULL;
847           os = NULL;
848
849           if ((item.extra_flags & TEST_CREATE) == TEST_CREATE)
850             os = g_file_create (child, item.create_flags, NULL, &error);
851           else if ((item.extra_flags & TEST_REPLACE) == TEST_REPLACE)
852             os =
853               g_file_replace (child, NULL, TRUE, item.create_flags, NULL,
854                               &error);
855           else if ((item.extra_flags & TEST_APPEND) == TEST_APPEND)
856             os = g_file_append_to (child, item.create_flags, NULL, &error);
857
858
859           if (error)
860             log ("       error code %d = %s\n", error->code, error->message);
861
862           if (((item.extra_flags & TEST_NOT_EXISTS) == 0) &&
863               ((item.extra_flags & TEST_CREATE) == TEST_CREATE))
864             {
865               g_assert (os == NULL);
866               g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
867             }
868           else if (item.file_type == G_FILE_TYPE_DIRECTORY)
869             {
870               g_assert (os == NULL);
871               if ((item.extra_flags & TEST_CREATE) == TEST_CREATE)
872                 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
873               else
874                 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
875             }
876           else
877             {
878               g_assert (os != NULL);
879               g_assert_no_error (error);
880             }
881
882           if (error)
883             g_error_free (error);
884
885           if (os)
886             {
887               error = NULL;
888               res =
889                 g_output_stream_close (G_OUTPUT_STREAM (os), NULL, &error);
890               if (error)
891                 log ("         g_output_stream_close: error %d = %s\n",
892                      error->code, error->message);
893               g_assert_cmpint (res, ==, TRUE);
894               g_assert_no_error (error);
895             }
896           g_object_unref (child);
897         }
898     }
899   g_object_unref (root);
900 }
901
902 static void
903 test_open (gconstpointer test_data)
904 {
905   GFile *root, *child;
906   gboolean res;
907   GError *error;
908   guint i;
909   struct StructureItem item;
910   GFileInputStream *input_stream;
911
912   g_assert (test_data != NULL);
913   log ("\n");
914
915   root = g_file_new_for_commandline_arg ((char *) test_data);
916   g_assert (root != NULL);
917   res = g_file_query_exists (root, NULL);
918   g_assert_cmpint (res, ==, TRUE);
919
920   for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
921     {
922       item = sample_struct[i];
923
924       if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
925         continue;
926
927       if ((item.extra_flags & TEST_OPEN) == TEST_OPEN)
928         {
929           log ("  test_open: '%s'\n", item.filename);
930
931           child = g_file_get_child (root, item.filename);
932           g_assert (child != NULL);
933           error = NULL;
934           input_stream = g_file_read (child, NULL, &error);
935
936           if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
937               ((item.extra_flags & TEST_INVALID_SYMLINK) ==
938                TEST_INVALID_SYMLINK))
939             {
940               g_assert (input_stream == NULL);
941               g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
942             }
943           else if (item.file_type == G_FILE_TYPE_DIRECTORY)
944             {
945               g_assert (input_stream == NULL);
946               g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
947             }
948           else
949             {
950               g_assert (input_stream != NULL);
951               g_assert_no_error (error);
952             }
953
954           if (error)
955             g_error_free (error);
956
957           if (input_stream)
958             {
959               error = NULL;
960               res =
961                 g_input_stream_close (G_INPUT_STREAM (input_stream), NULL,
962                                       &error);
963               g_assert_cmpint (res, ==, TRUE);
964               g_assert_no_error (error);
965             }
966           g_object_unref (child);
967         }
968     }
969   g_object_unref (root);
970 }
971
972 static void
973 test_delete (gconstpointer test_data)
974 {
975   GFile *root;
976   GFile *child;
977   gboolean res;
978   GError *error;
979   guint i;
980   struct StructureItem item;
981
982   g_assert (test_data != NULL);
983   log ("\n");
984
985   root = g_file_new_for_commandline_arg ((char *) test_data);
986   g_assert (root != NULL);
987   res = g_file_query_exists (root, NULL);
988   g_assert_cmpint (res, ==, TRUE);
989
990   for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
991     {
992       item = sample_struct[i];
993
994       if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
995         continue;
996
997       if (((item.extra_flags & TEST_DELETE_NORMAL) == TEST_DELETE_NORMAL) ||
998           ((item.extra_flags & TEST_DELETE_TRASH) == TEST_DELETE_TRASH))
999         {
1000           child = file_exists (root, item.filename, &res);
1001           g_assert (child != NULL);
1002           /*  we don't care about result here  */
1003
1004           log ("  Deleting %s, path = %s\n", item.filename,
1005                g_file_get_path (child));
1006           error = NULL;
1007           if ((item.extra_flags & TEST_DELETE_NORMAL) == TEST_DELETE_NORMAL)
1008             res = g_file_delete (child, NULL, &error);
1009           else
1010             res = g_file_trash (child, NULL, &error);
1011
1012           if ((item.extra_flags & TEST_DELETE_NON_EMPTY) ==
1013               TEST_DELETE_NON_EMPTY)
1014             {
1015               g_assert_cmpint (res, ==, FALSE);
1016               g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_EMPTY);
1017             }
1018           if ((item.extra_flags & TEST_DELETE_FAILURE) == TEST_DELETE_FAILURE)
1019             {
1020               g_assert_cmpint (res, ==, FALSE);
1021               g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
1022             }
1023           if ((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS)
1024             {
1025               g_assert_cmpint (res, ==, FALSE);
1026               g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
1027             }
1028
1029           if (error)
1030             {
1031               log ("      result = %d, error = %s\n", res, error->message);
1032               g_error_free (error);
1033             }
1034
1035           g_object_unref (child);
1036         }
1037     }
1038   g_object_unref (root);
1039 }
1040
1041
1042 static void
1043 cleanup_dir_recurse (GFile *parent, GFile *root)
1044 {
1045   gboolean res;
1046   GError *error;
1047   GFileEnumerator *enumerator;
1048   GFileInfo *info;
1049   GFile *descend;
1050   char *relative_path;
1051
1052   g_assert (root != NULL);
1053
1054   error = NULL;
1055   enumerator =
1056     g_file_enumerate_children (parent, "*",
1057                                G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
1058                                &error);
1059   if (! enumerator)
1060           return;
1061
1062   error = NULL;
1063   info = g_file_enumerator_next_file (enumerator, NULL, &error);
1064   while ((info) && (!error))
1065     {
1066       descend = g_file_get_child (parent, g_file_info_get_name (info));
1067       g_assert (descend != NULL);
1068       relative_path = g_file_get_relative_path (root, descend);
1069       g_assert (relative_path != NULL);
1070
1071       log ("    deleting '%s'\n", g_file_info_get_display_name (info));
1072
1073       if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
1074           cleanup_dir_recurse (descend, root);
1075       
1076       error = NULL;
1077       res = g_file_delete (descend, NULL, &error);
1078       g_assert_cmpint (res, ==, TRUE);
1079
1080       g_object_unref (descend);
1081       error = NULL;
1082       info = g_file_enumerator_next_file (enumerator, NULL, &error);
1083     }
1084   g_assert_no_error (error);
1085
1086   error = NULL;
1087   res = g_file_enumerator_close (enumerator, NULL, &error);
1088   g_assert_cmpint (res, ==, TRUE);
1089   g_assert_no_error (error);
1090
1091   g_object_unref (enumerator);
1092 }
1093
1094 static void
1095 prep_clean_structure (gconstpointer test_data)
1096 {
1097   GFile *root;
1098   
1099   g_assert (test_data != NULL);
1100   log ("\n  Cleaning target testing structure in '%s'...\n",
1101        (char *) test_data);
1102
1103   root = g_file_new_for_commandline_arg ((char *) test_data);
1104   g_assert (root != NULL);
1105   
1106   cleanup_dir_recurse (root, root);
1107
1108   g_file_delete (root, NULL, NULL);
1109   
1110   g_object_unref (root);
1111 }
1112
1113 int
1114 main (int argc, char *argv[])
1115 {
1116   static gboolean only_create_struct;
1117   const char *target_path;
1118   GError *error;
1119   GOptionContext *context;
1120
1121   static GOptionEntry cmd_entries[] = {
1122     {"read-write", 'w', 0, G_OPTION_ARG_NONE, &write_test,
1123      "Perform write tests (incl. structure creation)", NULL},
1124     {"create-struct", 'c', 0, G_OPTION_ARG_NONE, &only_create_struct,
1125      "Only create testing structure (no tests)", NULL},
1126     {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL},
1127     {"posix", 'x', 0, G_OPTION_ARG_NONE, &posix_compat,
1128      "Test POSIX-specific features (unix permissions, symlinks)", NULL},
1129     {NULL}
1130   };
1131
1132   test_suite = FALSE;
1133   verbose = FALSE;
1134   write_test = FALSE;
1135   only_create_struct = FALSE;
1136   target_path = NULL;
1137   posix_compat = FALSE;
1138
1139   /*  strip all gtester-specific args  */
1140   g_type_init ();
1141   g_test_init (&argc, &argv, NULL);
1142
1143   /*  no extra parameters specified, assume we're executed from glib test suite  */ 
1144   if (argc < 2)
1145     {
1146           test_suite = TRUE;
1147           verbose = TRUE;
1148           write_test = TRUE;
1149           only_create_struct = FALSE;
1150           target_path = DEFAULT_TEST_DIR;
1151 #ifdef G_PLATFORM_WIN32
1152           posix_compat = FALSE;
1153 #else
1154           posix_compat = TRUE;
1155 #endif
1156     }
1157   
1158   /*  add trailing args  */
1159   error = NULL;
1160   context = g_option_context_new ("target_path");
1161   g_option_context_add_main_entries (context, cmd_entries, NULL);
1162   if (!g_option_context_parse (context, &argc, &argv, &error))
1163     {
1164       g_print ("option parsing failed: %s\n", error->message);
1165       return g_test_run ();
1166     }
1167
1168   /*  remaining arg should is the target path; we don't care of the extra args here  */ 
1169   if (argc >= 2)
1170     target_path = strdup (argv[1]);
1171   
1172   if (! target_path) 
1173     {
1174       g_print ("error: target path was not specified\n");
1175       g_print ("%s", g_option_context_get_help (context, TRUE, NULL));
1176       return g_test_run ();
1177     }
1178
1179   
1180   /*  Write test - clean target directory first  */
1181   /*    this can be also considered as a test - enumerate + delete  */ 
1182   if (write_test || only_create_struct)
1183     g_test_add_data_func ("/live-g-file/prep_clean_structure", target_path,
1184                   prep_clean_structure);
1185   
1186   /*  Write test - create new testing structure  */
1187   if (write_test || only_create_struct)
1188     g_test_add_data_func ("/live-g-file/create_structure", target_path,
1189                           test_create_structure);
1190
1191   /*  Read test - test the sample structure - expect defined attributes to be there  */
1192   if (!only_create_struct)
1193     g_test_add_data_func ("/live-g-file/test_initial_structure", target_path,
1194                           test_initial_structure);
1195
1196   /*  Read test - test traverse the structure - no special file should appear  */
1197   if (!only_create_struct)
1198     g_test_add_data_func ("/live-g-file/test_traverse_structure", target_path,
1199                           test_traverse_structure);
1200
1201   /*  Read test - enumerate  */
1202   if (!only_create_struct)
1203     g_test_add_data_func ("/live-g-file/test_enumerate", target_path,
1204                           test_enumerate);
1205
1206   /*  Read test - open (g_file_read())  */
1207   if (!only_create_struct)
1208     g_test_add_data_func ("/live-g-file/test_open", target_path, test_open);
1209
1210   /*  Write test - create  */
1211   if (write_test && (!only_create_struct))
1212     g_test_add_data_func ("/live-g-file/test_create", target_path,
1213                           test_create);
1214
1215   /*  Write test - copy, move  */
1216   if (write_test && (!only_create_struct))
1217     g_test_add_data_func ("/live-g-file/test_copy_move", target_path,
1218                           test_copy_move);
1219
1220   /*  Write test - delete, trash  */
1221   if (write_test && (!only_create_struct))
1222     g_test_add_data_func ("/live-g-file/test_delete", target_path,
1223                           test_delete);
1224
1225   if (write_test || only_create_struct)
1226     g_test_add_data_func ("/live-g-file/final_clean", target_path,
1227                   prep_clean_structure);
1228
1229   return g_test_run ();
1230
1231 }