Initial commit
[platform/upstream/glib2.0.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   error = NULL;
510   info = g_file_enumerator_next_file (enumerator, NULL, &error);
511   while ((info) && (!error))
512     {
513       descend = g_file_get_child (parent, g_file_info_get_name (info));
514       g_assert (descend != NULL);
515       relative_path = g_file_get_relative_path (root, descend);
516       g_assert (relative_path != NULL);
517
518       found = FALSE;
519       for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
520         {
521           if (strcmp (sample_struct[i].filename, relative_path) == 0)
522             {
523               /*  test the attributes again  */
524               test_attributes (sample_struct[i], info);
525
526               found = TRUE;
527               break;
528             }
529         }
530       g_assert_cmpint (found, ==, TRUE);
531
532       log ("  Found file %s, relative to root: %s\n",
533            g_file_info_get_display_name (info), relative_path);
534
535       if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
536         traverse_recurse_dirs (descend, root);
537
538       g_object_unref (descend);
539       error = NULL;
540       info = g_file_enumerator_next_file (enumerator, NULL, &error);
541     }
542   g_assert_no_error (error);
543
544   error = NULL;
545   res = g_file_enumerator_close (enumerator, NULL, &error);
546   g_assert_cmpint (res, ==, TRUE);
547   g_assert_no_error (error);
548 }
549
550 static void
551 test_traverse_structure (gconstpointer test_data)
552 {
553   GFile *root;
554   gboolean res;
555
556   g_assert (test_data != NULL);
557   log ("\n  Traversing through the sample structure in '%s'...\n",
558        (char *) test_data);
559
560   root = g_file_new_for_commandline_arg ((char *) test_data);
561   g_assert (root != NULL);
562   res = g_file_query_exists (root, NULL);
563   g_assert_cmpint (res, ==, TRUE);
564
565   traverse_recurse_dirs (root, root);
566
567   g_object_unref (root);
568 }
569
570
571
572
573 static void
574 test_enumerate (gconstpointer test_data)
575 {
576   GFile *root, *child;
577   gboolean res;
578   GError *error;
579   GFileEnumerator *enumerator;
580   GFileInfo *info;
581   guint i;
582   struct StructureItem item;
583
584
585   g_assert (test_data != NULL);
586   log ("\n  Test enumerate '%s'...\n", (char *) test_data);
587
588   root = g_file_new_for_commandline_arg ((char *) test_data);
589   g_assert (root != NULL);
590   res = g_file_query_exists (root, NULL);
591   g_assert_cmpint (res, ==, TRUE);
592
593
594   for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
595     {
596       item = sample_struct[i];
597       if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
598         continue;
599
600       if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
601           (((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS)
602            && posix_compat)
603           || ((item.extra_flags & TEST_ENUMERATE_FILE) ==
604               TEST_ENUMERATE_FILE))
605         {
606           log ("    Testing file '%s'\n", item.filename);
607           child = g_file_get_child (root, item.filename);
608           g_assert (child != NULL);
609           error = NULL;
610           enumerator =
611             g_file_enumerate_children (child, "*",
612                                        G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
613                                        NULL, &error);
614
615           if ((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS)
616             {
617               g_assert (enumerator == NULL);
618               g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
619             }
620           if ((item.extra_flags & TEST_ENUMERATE_FILE) == TEST_ENUMERATE_FILE)
621             {
622               g_assert (enumerator == NULL);
623               g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY);
624             }
625           if ((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS)
626             {
627               g_assert (enumerator != NULL);
628
629               error = NULL;
630               info = g_file_enumerator_next_file (enumerator, NULL, &error);
631               g_assert (info == NULL);
632               g_assert_no_error (error);
633               /*  no items should be found, no error should be logged  */
634             }
635
636           if (error)
637             g_error_free (error);
638
639           if (enumerator)
640             {
641               error = NULL;
642               res = g_file_enumerator_close (enumerator, NULL, &error);
643               g_assert_cmpint (res, ==, TRUE);
644               g_assert_no_error (error);
645             }
646           g_object_unref (child);
647         }
648     }
649   g_object_unref (root);
650 }
651
652 static void
653 do_copy_move (GFile * root, struct StructureItem item, const char *target_dir,
654               enum StructureExtraFlags extra_flags)
655 {
656   GFile *dst_dir, *src_file, *dst_file;
657   gboolean res;
658   GError *error;
659
660   log ("    do_copy_move: '%s' --> '%s'\n", item.filename, target_dir);
661
662   dst_dir = g_file_get_child (root, target_dir);
663   g_assert (dst_dir != NULL);
664   src_file = g_file_get_child (root, item.filename);
665   g_assert (src_file != NULL);
666   dst_file = g_file_get_child (dst_dir, item.filename);
667   g_assert (dst_file != NULL);
668
669   error = NULL;
670   if ((item.extra_flags & TEST_COPY) == TEST_COPY)
671     res =
672       g_file_copy (src_file, dst_file,
673                    G_FILE_COPY_NOFOLLOW_SYMLINKS |
674                    ((extra_flags ==
675                      TEST_OVERWRITE) ? G_FILE_COPY_OVERWRITE :
676                     G_FILE_COPY_NONE), NULL, NULL, NULL, &error);
677   else
678     res =
679       g_file_move (src_file, dst_file, G_FILE_COPY_NOFOLLOW_SYMLINKS, NULL,
680                    NULL, NULL, &error);
681
682   if (error)
683     log ("       res = %d, error code %d = %s\n", res, error->code,
684          error->message);
685
686   /*  copying file/directory to itself (".")  */
687   if (((item.extra_flags & TEST_NOT_EXISTS) != TEST_NOT_EXISTS) &&
688       (extra_flags == TEST_ALREADY_EXISTS))
689     {
690       g_assert_cmpint (res, ==, FALSE);
691       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
692     }
693   /*  target file is a file, overwrite is not set  */
694   else if (((item.extra_flags & TEST_NOT_EXISTS) != TEST_NOT_EXISTS) &&
695            (extra_flags == TEST_TARGET_IS_FILE))
696     {
697       g_assert_cmpint (res, ==, FALSE);
698       if (item.file_type == G_FILE_TYPE_DIRECTORY)
699         g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE);
700       else
701         g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_DIRECTORY);
702     }
703   /*  source file is directory  */
704   else if ((item.extra_flags & TEST_COPY_ERROR_RECURSE) ==
705            TEST_COPY_ERROR_RECURSE)
706     {
707       g_assert_cmpint (res, ==, FALSE);
708       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_RECURSE);
709     }
710   /*  source or target path doesn't exist  */
711   else if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
712            (extra_flags == TEST_NOT_EXISTS))
713     {
714       g_assert_cmpint (res, ==, FALSE);
715       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
716     }
717   /*  source or target path permission denied  */
718   else if (((item.extra_flags & TEST_NO_ACCESS) == TEST_NO_ACCESS) ||
719            (extra_flags == TEST_NO_ACCESS))
720     {
721       /* This works for root, see bug #552912 */
722       if (test_suite && getuid () == 0)
723         {
724           g_assert_cmpint (res, ==, TRUE);
725           g_assert_no_error (error);
726         }
727       else
728         {
729           g_assert_cmpint (res, ==, FALSE);
730           g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED);
731         }
732     }
733   /*  no error should be found, all exceptions defined above  */
734   else
735     {
736       g_assert_cmpint (res, ==, TRUE);
737       g_assert_no_error (error);
738     }
739
740   if (error)
741     g_error_free (error);
742
743
744   g_object_unref (dst_dir);
745   g_object_unref (src_file);
746   g_object_unref (dst_file);
747 }
748
749 static void
750 test_copy_move (gconstpointer test_data)
751 {
752   GFile *root;
753   gboolean res;
754   guint i;
755   struct StructureItem item;
756
757   log ("\n");
758
759   g_assert (test_data != NULL);
760   root = g_file_new_for_commandline_arg ((char *) test_data);
761   g_assert (root != NULL);
762   res = g_file_query_exists (root, NULL);
763   g_assert_cmpint (res, ==, TRUE);
764
765
766   for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
767     {
768       item = sample_struct[i];
769
770       if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
771         continue;
772
773       if (((item.extra_flags & TEST_COPY) == TEST_COPY) ||
774           ((item.extra_flags & TEST_MOVE) == TEST_MOVE))
775         {
776           /*  test copy/move to a directory, expecting no errors if source files exist  */
777           do_copy_move (root, item, TEST_DIR_TARGET, 0);
778
779           /*  some files have been already moved so we can't count with them in the tests  */
780           if ((item.extra_flags & TEST_COPY) == TEST_COPY)
781             {
782               /*  test overwrite for flagged files  */
783               if ((item.extra_flags & TEST_OVERWRITE) == TEST_OVERWRITE)
784                 {
785                   do_copy_move (root, item, TEST_DIR_TARGET, TEST_OVERWRITE);
786                 }
787               /*  source = target, should return G_IO_ERROR_EXISTS  */
788               do_copy_move (root, item, ".", TEST_ALREADY_EXISTS);
789               /*  target is file  */
790               do_copy_move (root, item, TEST_TARGET_FILE,
791                             TEST_TARGET_IS_FILE);
792               /*  target path is invalid  */
793               do_copy_move (root, item, TEST_NAME_NOT_EXISTS,
794                             TEST_NOT_EXISTS);
795
796               /*  tests on POSIX-compatible filesystems  */
797               if (posix_compat)
798                 {
799                   /*  target directory is not accessible (no execute flag)  */
800                   do_copy_move (root, item, TEST_DIR_NO_ACCESS,
801                                 TEST_NO_ACCESS);
802                   /*  target directory is readonly  */
803                   do_copy_move (root, item, TEST_DIR_NO_WRITE,
804                                 TEST_NO_ACCESS);
805                 }
806             }
807         }
808     }
809   g_object_unref (root);
810 }
811
812 static void
813 test_create (gconstpointer test_data)
814 {
815   GFile *root, *child;
816   gboolean res;
817   GError *error;
818   guint i;
819   struct StructureItem item;
820   GFileOutputStream *os;
821
822   g_assert (test_data != NULL);
823   log ("\n");
824
825   root = g_file_new_for_commandline_arg ((char *) test_data);
826   g_assert (root != NULL);
827   res = g_file_query_exists (root, NULL);
828   g_assert_cmpint (res, ==, TRUE);
829
830   for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
831     {
832       item = sample_struct[i];
833
834       if (((item.extra_flags & TEST_CREATE) == TEST_CREATE) ||
835           ((item.extra_flags & TEST_REPLACE) == TEST_REPLACE) ||
836           ((item.extra_flags & TEST_APPEND) == TEST_APPEND))
837         {
838           log ("  test_create: '%s'\n", item.filename);
839
840           child = g_file_get_child (root, item.filename);
841           g_assert (child != NULL);
842           error = NULL;
843           os = NULL;
844
845           if ((item.extra_flags & TEST_CREATE) == TEST_CREATE)
846             os = g_file_create (child, item.create_flags, NULL, &error);
847           else if ((item.extra_flags & TEST_REPLACE) == TEST_REPLACE)
848             os =
849               g_file_replace (child, NULL, TRUE, item.create_flags, NULL,
850                               &error);
851           else if ((item.extra_flags & TEST_APPEND) == TEST_APPEND)
852             os = g_file_append_to (child, item.create_flags, NULL, &error);
853
854
855           if (error)
856             log ("       error code %d = %s\n", error->code, error->message);
857
858           if (((item.extra_flags & TEST_NOT_EXISTS) == 0) &&
859               ((item.extra_flags & TEST_CREATE) == TEST_CREATE))
860             {
861               g_assert (os == NULL);
862               g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
863             }
864           else if (item.file_type == G_FILE_TYPE_DIRECTORY)
865             {
866               g_assert (os == NULL);
867               if ((item.extra_flags & TEST_CREATE) == TEST_CREATE)
868                 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
869               else
870                 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
871             }
872           else
873             {
874               g_assert (os != NULL);
875               g_assert_no_error (error);
876             }
877
878           if (error)
879             g_error_free (error);
880
881           if (os)
882             {
883               error = NULL;
884               res =
885                 g_output_stream_close (G_OUTPUT_STREAM (os), NULL, &error);
886               if (error)
887                 log ("         g_output_stream_close: error %d = %s\n",
888                      error->code, error->message);
889               g_assert_cmpint (res, ==, TRUE);
890               g_assert_no_error (error);
891             }
892           g_object_unref (child);
893         }
894     }
895   g_object_unref (root);
896 }
897
898 static void
899 test_open (gconstpointer test_data)
900 {
901   GFile *root, *child;
902   gboolean res;
903   GError *error;
904   guint i;
905   struct StructureItem item;
906   GFileInputStream *input_stream;
907
908   g_assert (test_data != NULL);
909   log ("\n");
910
911   root = g_file_new_for_commandline_arg ((char *) test_data);
912   g_assert (root != NULL);
913   res = g_file_query_exists (root, NULL);
914   g_assert_cmpint (res, ==, TRUE);
915
916   for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
917     {
918       item = sample_struct[i];
919
920       if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
921         continue;
922
923       if ((item.extra_flags & TEST_OPEN) == TEST_OPEN)
924         {
925           log ("  test_open: '%s'\n", item.filename);
926
927           child = g_file_get_child (root, item.filename);
928           g_assert (child != NULL);
929           error = NULL;
930           input_stream = g_file_read (child, NULL, &error);
931
932           if (((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS) ||
933               ((item.extra_flags & TEST_INVALID_SYMLINK) ==
934                TEST_INVALID_SYMLINK))
935             {
936               g_assert (input_stream == NULL);
937               g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
938             }
939           else if (item.file_type == G_FILE_TYPE_DIRECTORY)
940             {
941               g_assert (input_stream == NULL);
942               g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
943             }
944           else
945             {
946               g_assert (input_stream != NULL);
947               g_assert_no_error (error);
948             }
949
950           if (error)
951             g_error_free (error);
952
953           if (input_stream)
954             {
955               error = NULL;
956               res =
957                 g_input_stream_close (G_INPUT_STREAM (input_stream), NULL,
958                                       &error);
959               g_assert_cmpint (res, ==, TRUE);
960               g_assert_no_error (error);
961             }
962           g_object_unref (child);
963         }
964     }
965   g_object_unref (root);
966 }
967
968 static void
969 test_delete (gconstpointer test_data)
970 {
971   GFile *root;
972   GFile *child;
973   gboolean res;
974   GError *error;
975   guint i;
976   struct StructureItem item;
977
978   g_assert (test_data != NULL);
979   log ("\n");
980
981   root = g_file_new_for_commandline_arg ((char *) test_data);
982   g_assert (root != NULL);
983   res = g_file_query_exists (root, NULL);
984   g_assert_cmpint (res, ==, TRUE);
985
986   for (i = 0; i < G_N_ELEMENTS (sample_struct); i++)
987     {
988       item = sample_struct[i];
989
990       if ((!posix_compat) && (item.file_type == G_FILE_TYPE_SYMBOLIC_LINK))
991         continue;
992
993       if (((item.extra_flags & TEST_DELETE_NORMAL) == TEST_DELETE_NORMAL) ||
994           ((item.extra_flags & TEST_DELETE_TRASH) == TEST_DELETE_TRASH))
995         {
996           child = file_exists (root, item.filename, &res);
997           g_assert (child != NULL);
998           /*  we don't care about result here  */
999
1000           log ("  Deleting %s, path = %s\n", item.filename,
1001                g_file_get_path (child));
1002           error = NULL;
1003           if ((item.extra_flags & TEST_DELETE_NORMAL) == TEST_DELETE_NORMAL)
1004             res = g_file_delete (child, NULL, &error);
1005           else
1006             res = g_file_trash (child, NULL, &error);
1007
1008           if ((item.extra_flags & TEST_DELETE_NON_EMPTY) ==
1009               TEST_DELETE_NON_EMPTY)
1010             {
1011               g_assert_cmpint (res, ==, FALSE);
1012               g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_EMPTY);
1013             }
1014           if ((item.extra_flags & TEST_DELETE_FAILURE) == TEST_DELETE_FAILURE)
1015             {
1016               g_assert_cmpint (res, ==, FALSE);
1017               g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
1018             }
1019           if ((item.extra_flags & TEST_NOT_EXISTS) == TEST_NOT_EXISTS)
1020             {
1021               g_assert_cmpint (res, ==, FALSE);
1022               g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
1023             }
1024
1025           if (error)
1026             {
1027               log ("      result = %d, error = %s\n", res, error->message);
1028               g_error_free (error);
1029             }
1030
1031           g_object_unref (child);
1032         }
1033     }
1034   g_object_unref (root);
1035 }
1036
1037
1038 static void
1039 cleanup_dir_recurse (GFile *parent, GFile *root)
1040 {
1041   gboolean res;
1042   GError *error;
1043   GFileEnumerator *enumerator;
1044   GFileInfo *info;
1045   GFile *descend;
1046   char *relative_path;
1047
1048   g_assert (root != NULL);
1049
1050   error = NULL;
1051   enumerator =
1052     g_file_enumerate_children (parent, "*",
1053                                G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
1054                                &error);
1055   if (! enumerator)
1056           return;
1057
1058   error = NULL;
1059   info = g_file_enumerator_next_file (enumerator, NULL, &error);
1060   while ((info) && (!error))
1061     {
1062       descend = g_file_get_child (parent, g_file_info_get_name (info));
1063       g_assert (descend != NULL);
1064       relative_path = g_file_get_relative_path (root, descend);
1065       g_assert (relative_path != NULL);
1066
1067       log ("    deleting '%s'\n", g_file_info_get_display_name (info));
1068
1069       if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
1070           cleanup_dir_recurse (descend, root);
1071       
1072       error = NULL;
1073       res = g_file_delete (descend, NULL, &error);
1074       g_assert_cmpint (res, ==, TRUE);
1075
1076       g_object_unref (descend);
1077       error = NULL;
1078       info = g_file_enumerator_next_file (enumerator, NULL, &error);
1079     }
1080   g_assert_no_error (error);
1081
1082   error = NULL;
1083   res = g_file_enumerator_close (enumerator, NULL, &error);
1084   g_assert_cmpint (res, ==, TRUE);
1085   g_assert_no_error (error);
1086 }
1087
1088 static void
1089 prep_clean_structure (gconstpointer test_data)
1090 {
1091   GFile *root;
1092   
1093   g_assert (test_data != NULL);
1094   log ("\n  Cleaning target testing structure in '%s'...\n",
1095        (char *) test_data);
1096
1097   root = g_file_new_for_commandline_arg ((char *) test_data);
1098   g_assert (root != NULL);
1099   
1100   cleanup_dir_recurse (root, root);
1101
1102   g_file_delete (root, NULL, NULL);
1103   
1104   g_object_unref (root);
1105 }
1106
1107 int
1108 main (int argc, char *argv[])
1109 {
1110   static gboolean only_create_struct;
1111   const char *target_path;
1112   GError *error;
1113   GOptionContext *context;
1114
1115   static GOptionEntry cmd_entries[] = {
1116     {"read-write", 'w', 0, G_OPTION_ARG_NONE, &write_test,
1117      "Perform write tests (incl. structure creation)", NULL},
1118     {"create-struct", 'c', 0, G_OPTION_ARG_NONE, &only_create_struct,
1119      "Only create testing structure (no tests)", NULL},
1120     {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be verbose", NULL},
1121     {"posix", 'x', 0, G_OPTION_ARG_NONE, &posix_compat,
1122      "Test POSIX-specific features (unix permissions, symlinks)", NULL},
1123     {NULL}
1124   };
1125
1126   test_suite = FALSE;
1127   verbose = FALSE;
1128   write_test = FALSE;
1129   only_create_struct = FALSE;
1130   target_path = NULL;
1131   posix_compat = FALSE;
1132
1133   /*  strip all gtester-specific args  */
1134   g_type_init ();
1135   g_test_init (&argc, &argv, NULL);
1136
1137   /*  no extra parameters specified, assume we're executed from glib test suite  */ 
1138   if (argc < 2)
1139     {
1140           test_suite = TRUE;
1141           verbose = TRUE;
1142           write_test = TRUE;
1143           only_create_struct = FALSE;
1144           target_path = DEFAULT_TEST_DIR;
1145 #ifdef G_PLATFORM_WIN32
1146           posix_compat = FALSE;
1147 #else
1148           posix_compat = TRUE;
1149 #endif
1150     }
1151   
1152   /*  add trailing args  */
1153   error = NULL;
1154   context = g_option_context_new ("target_path");
1155   g_option_context_add_main_entries (context, cmd_entries, NULL);
1156   if (!g_option_context_parse (context, &argc, &argv, &error))
1157     {
1158       g_print ("option parsing failed: %s\n", error->message);
1159       return g_test_run ();
1160     }
1161
1162   /*  remaining arg should is the target path; we don't care of the extra args here  */ 
1163   if (argc >= 2)
1164     target_path = strdup (argv[1]);
1165   
1166   if (! target_path) 
1167     {
1168       g_print ("error: target path was not specified\n");
1169       g_print ("%s", g_option_context_get_help (context, TRUE, NULL));
1170       return g_test_run ();
1171     }
1172
1173   
1174   /*  Write test - clean target directory first  */
1175   /*    this can be also considered as a test - enumerate + delete  */ 
1176   if (write_test || only_create_struct)
1177     g_test_add_data_func ("/live-g-file/prep_clean_structure", target_path,
1178                   prep_clean_structure);
1179   
1180   /*  Write test - create new testing structure  */
1181   if (write_test || only_create_struct)
1182     g_test_add_data_func ("/live-g-file/create_structure", target_path,
1183                           test_create_structure);
1184
1185   /*  Read test - test the sample structure - expect defined attributes to be there  */
1186   if (!only_create_struct)
1187     g_test_add_data_func ("/live-g-file/test_initial_structure", target_path,
1188                           test_initial_structure);
1189
1190   /*  Read test - test traverse the structure - no special file should appear  */
1191   if (!only_create_struct)
1192     g_test_add_data_func ("/live-g-file/test_traverse_structure", target_path,
1193                           test_traverse_structure);
1194
1195   /*  Read test - enumerate  */
1196   if (!only_create_struct)
1197     g_test_add_data_func ("/live-g-file/test_enumerate", target_path,
1198                           test_enumerate);
1199
1200   /*  Read test - open (g_file_read())  */
1201   if (!only_create_struct)
1202     g_test_add_data_func ("/live-g-file/test_open", target_path, test_open);
1203
1204   /*  Write test - create  */
1205   if (write_test && (!only_create_struct))
1206     g_test_add_data_func ("/live-g-file/test_create", target_path,
1207                           test_create);
1208
1209   /*  Write test - copy, move  */
1210   if (write_test && (!only_create_struct))
1211     g_test_add_data_func ("/live-g-file/test_copy_move", target_path,
1212                           test_copy_move);
1213
1214   /*  Write test - delete, trash  */
1215   if (write_test && (!only_create_struct))
1216     g_test_add_data_func ("/live-g-file/test_delete", target_path,
1217                           test_delete);
1218
1219   if (write_test || only_create_struct)
1220     g_test_add_data_func ("/live-g-file/final_clean", target_path,
1221                   prep_clean_structure);
1222
1223   return g_test_run ();
1224
1225 }