Imported Upstream version 2.65.0
[platform/upstream/glib.git] / gio / tests / file.c
1 #include <string.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <gio/gio.h>
5 #include <gio/gfiledescriptorbased.h>
6 #ifdef G_OS_UNIX
7 #include <sys/stat.h>
8 #endif
9
10 static void
11 test_basic_for_file (GFile       *file,
12                      const gchar *suffix)
13 {
14   gchar *s;
15
16   s = g_file_get_basename (file);
17   g_assert_cmpstr (s, ==, "testfile");
18   g_free (s);
19
20   s = g_file_get_uri (file);
21   g_assert_true (g_str_has_prefix (s, "file://"));
22   g_assert_true (g_str_has_suffix (s, suffix));
23   g_free (s);
24
25   g_assert_true (g_file_has_uri_scheme (file, "file"));
26   s = g_file_get_uri_scheme (file);
27   g_assert_cmpstr (s, ==, "file");
28   g_free (s);
29 }
30
31 static void
32 test_basic (void)
33 {
34   GFile *file;
35
36   file = g_file_new_for_path ("./some/directory/testfile");
37   test_basic_for_file (file, "/some/directory/testfile");
38   g_object_unref (file);
39 }
40
41 static void
42 test_build_filename (void)
43 {
44   GFile *file;
45
46   file = g_file_new_build_filename (".", "some", "directory", "testfile", NULL);
47   test_basic_for_file (file, "/some/directory/testfile");
48   g_object_unref (file);
49
50   file = g_file_new_build_filename ("testfile", NULL);
51   test_basic_for_file (file, "/testfile");
52   g_object_unref (file);
53 }
54
55 static void
56 test_parent (void)
57 {
58   GFile *file;
59   GFile *file2;
60   GFile *parent;
61   GFile *root;
62
63   file = g_file_new_for_path ("./some/directory/testfile");
64   file2 = g_file_new_for_path ("./some/directory");
65   root = g_file_new_for_path ("/");
66
67   g_assert_true (g_file_has_parent (file, file2));
68
69   parent = g_file_get_parent (file);
70   g_assert_true (g_file_equal (parent, file2));
71   g_object_unref (parent);
72
73   g_assert_null (g_file_get_parent (root));
74
75   g_object_unref (file);
76   g_object_unref (file2);
77   g_object_unref (root);
78 }
79
80 static void
81 test_child (void)
82 {
83   GFile *file;
84   GFile *child;
85   GFile *child2;
86
87   file = g_file_new_for_path ("./some/directory");
88   child = g_file_get_child (file, "child");
89   g_assert_true (g_file_has_parent (child, file));
90
91   child2 = g_file_get_child_for_display_name (file, "child2", NULL);
92   g_assert_true (g_file_has_parent (child2, file));
93
94   g_object_unref (child);
95   g_object_unref (child2);
96   g_object_unref (file);
97 }
98
99 static void
100 test_type (void)
101 {
102   GFile *datapath_f;
103   GFile *file;
104   GFileType type;
105   GError *error = NULL;
106
107   datapath_f = g_file_new_for_path (g_test_get_dir (G_TEST_DIST));
108
109   file = g_file_get_child (datapath_f, "g-icon.c");
110   type = g_file_query_file_type (file, 0, NULL);
111   g_assert_cmpint (type, ==, G_FILE_TYPE_REGULAR);
112   g_object_unref (file);
113
114   file = g_file_get_child (datapath_f, "cert-tests");
115   type = g_file_query_file_type (file, 0, NULL);
116   g_assert_cmpint (type, ==, G_FILE_TYPE_DIRECTORY);
117
118   g_file_read (file, NULL, &error);
119   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
120   g_error_free (error);
121   g_object_unref (file);
122
123   g_object_unref (datapath_f);
124 }
125
126 static void
127 test_parse_name (void)
128 {
129   GFile *file;
130   gchar *name;
131
132   file = g_file_new_for_uri ("file://somewhere");
133   name = g_file_get_parse_name (file);
134   g_assert_cmpstr (name, ==, "file://somewhere");
135   g_object_unref (file);
136   g_free (name);
137
138   file = g_file_parse_name ("~foo");
139   name = g_file_get_parse_name (file);
140   g_assert_nonnull (name);
141   g_object_unref (file);
142   g_free (name);
143 }
144
145 typedef struct
146 {
147   GMainContext *context;
148   GFile *file;
149   GFileMonitor *monitor;
150   GOutputStream *ostream;
151   GInputStream *istream;
152   gint buffersize;
153   gint monitor_created;
154   gint monitor_deleted;
155   gint monitor_changed;
156   gchar *monitor_path;
157   gint pos;
158   const gchar *data;
159   gchar *buffer;
160   guint timeout;
161   gboolean file_deleted;
162   gboolean timed_out;
163 } CreateDeleteData;
164
165 static void
166 monitor_changed (GFileMonitor      *monitor,
167                  GFile             *file,
168                  GFile             *other_file,
169                  GFileMonitorEvent  event_type,
170                  gpointer           user_data)
171 {
172   CreateDeleteData *data = user_data;
173   gchar *path;
174   const gchar *peeked_path;
175
176   path = g_file_get_path (file);
177   peeked_path = g_file_peek_path (file);
178   g_assert_cmpstr (data->monitor_path, ==, path);
179   g_assert_cmpstr (path, ==, peeked_path);
180   g_free (path);
181
182   if (event_type == G_FILE_MONITOR_EVENT_CREATED)
183     data->monitor_created++;
184   if (event_type == G_FILE_MONITOR_EVENT_DELETED)
185     data->monitor_deleted++;
186   if (event_type == G_FILE_MONITOR_EVENT_CHANGED)
187     data->monitor_changed++;
188
189   g_main_context_wakeup (data->context);
190 }
191
192 static void
193 iclosed_cb (GObject      *source,
194             GAsyncResult *res,
195             gpointer      user_data)
196 {
197   CreateDeleteData *data = user_data;
198   GError *error;
199   gboolean ret;
200
201   error = NULL;
202   ret = g_input_stream_close_finish (data->istream, res, &error);
203   g_assert_no_error (error);
204   g_assert_true (ret);
205
206   g_assert_true (g_input_stream_is_closed (data->istream));
207
208   ret = g_file_delete (data->file, NULL, &error);
209   g_assert_true (ret);
210   g_assert_no_error (error);
211
212   data->file_deleted = TRUE;
213   g_main_context_wakeup (data->context);
214 }
215
216 static void
217 read_cb (GObject      *source,
218          GAsyncResult *res,
219          gpointer      user_data)
220 {
221   CreateDeleteData *data = user_data;
222   GError *error;
223   gssize size;
224
225   error = NULL;
226   size = g_input_stream_read_finish (data->istream, res, &error);
227   g_assert_no_error (error);
228
229   data->pos += size;
230   if (data->pos < strlen (data->data))
231     {
232       g_input_stream_read_async (data->istream,
233                                  data->buffer + data->pos,
234                                  strlen (data->data) - data->pos,
235                                  0,
236                                  NULL,
237                                  read_cb,
238                                  data);
239     }
240   else
241     {
242       g_assert_cmpstr (data->buffer, ==, data->data);
243       g_assert_false (g_input_stream_is_closed (data->istream));
244       g_input_stream_close_async (data->istream, 0, NULL, iclosed_cb, data);
245     }
246 }
247
248 static void
249 ipending_cb (GObject      *source,
250              GAsyncResult *res,
251              gpointer      user_data)
252 {
253   CreateDeleteData *data = user_data;
254   GError *error;
255
256   error = NULL;
257   g_input_stream_read_finish (data->istream, res, &error);
258   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
259   g_error_free (error);
260 }
261
262 static void
263 skipped_cb (GObject      *source,
264             GAsyncResult *res,
265             gpointer      user_data)
266 {
267   CreateDeleteData *data = user_data;
268   GError *error;
269   gssize size;
270
271   error = NULL;
272   size = g_input_stream_skip_finish (data->istream, res, &error);
273   g_assert_no_error (error);
274   g_assert_cmpint (size, ==, data->pos);
275
276   g_input_stream_read_async (data->istream,
277                              data->buffer + data->pos,
278                              strlen (data->data) - data->pos,
279                              0,
280                              NULL,
281                              read_cb,
282                              data);
283   /* check that we get a pending error */
284   g_input_stream_read_async (data->istream,
285                              data->buffer + data->pos,
286                              strlen (data->data) - data->pos,
287                              0,
288                              NULL,
289                              ipending_cb,
290                              data);
291 }
292
293 static void
294 opened_cb (GObject      *source,
295            GAsyncResult *res,
296            gpointer      user_data)
297 {
298   GFileInputStream *base;
299   CreateDeleteData *data = user_data;
300   GError *error;
301
302   error = NULL;
303   base = g_file_read_finish (data->file, res, &error);
304   g_assert_no_error (error);
305
306   if (data->buffersize == 0)
307     data->istream = G_INPUT_STREAM (g_object_ref (base));
308   else
309     data->istream = g_buffered_input_stream_new_sized (G_INPUT_STREAM (base), data->buffersize);
310   g_object_unref (base);
311
312   data->buffer = g_new0 (gchar, strlen (data->data) + 1);
313
314   /* copy initial segment directly, then skip */
315   memcpy (data->buffer, data->data, 10);
316   data->pos = 10;
317
318   g_input_stream_skip_async (data->istream,
319                              10,
320                              0,
321                              NULL,
322                              skipped_cb,
323                              data);
324 }
325
326 static void
327 oclosed_cb (GObject      *source,
328             GAsyncResult *res,
329             gpointer      user_data)
330 {
331   CreateDeleteData *data = user_data;
332   GError *error;
333   gboolean ret;
334
335   error = NULL;
336   ret = g_output_stream_close_finish (data->ostream, res, &error);
337   g_assert_no_error (error);
338   g_assert_true (ret);
339   g_assert_true (g_output_stream_is_closed (data->ostream));
340
341   g_file_read_async (data->file, 0, NULL, opened_cb, data);
342 }
343
344 static void
345 written_cb (GObject      *source,
346             GAsyncResult *res,
347             gpointer      user_data)
348 {
349   CreateDeleteData *data = user_data;
350   gssize size;
351   GError *error;
352
353   error = NULL;
354   size = g_output_stream_write_finish (data->ostream, res, &error);
355   g_assert_no_error (error);
356
357   data->pos += size;
358   if (data->pos < strlen (data->data))
359     {
360       g_output_stream_write_async (data->ostream,
361                                    data->data + data->pos,
362                                    strlen (data->data) - data->pos,
363                                    0,
364                                    NULL,
365                                    written_cb,
366                                    data);
367     }
368   else
369     {
370       g_assert_false (g_output_stream_is_closed (data->ostream));
371       g_output_stream_close_async (data->ostream, 0, NULL, oclosed_cb, data);
372     }
373 }
374
375 static void
376 opending_cb (GObject      *source,
377              GAsyncResult *res,
378              gpointer      user_data)
379 {
380   CreateDeleteData *data = user_data;
381   GError *error;
382
383   error = NULL;
384   g_output_stream_write_finish (data->ostream, res, &error);
385   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
386   g_error_free (error);
387 }
388
389 static void
390 created_cb (GObject      *source,
391             GAsyncResult *res,
392             gpointer      user_data)
393 {
394   GFileOutputStream *base;
395   CreateDeleteData *data = user_data;
396   GError *error;
397
398   error = NULL;
399   base = g_file_create_finish (G_FILE (source), res, &error);
400   g_assert_no_error (error);
401   g_assert_true (g_file_query_exists (data->file, NULL));
402
403   if (data->buffersize == 0)
404     data->ostream = G_OUTPUT_STREAM (g_object_ref (base));
405   else
406     data->ostream = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), data->buffersize);
407   g_object_unref (base);
408
409   g_output_stream_write_async (data->ostream,
410                                data->data,
411                                strlen (data->data),
412                                0,
413                                NULL,
414                                written_cb,
415                                data);
416   /* check that we get a pending error */
417   g_output_stream_write_async (data->ostream,
418                                data->data,
419                                strlen (data->data),
420                                0,
421                                NULL,
422                                opending_cb,
423                                data);
424 }
425
426 static gboolean
427 stop_timeout (gpointer user_data)
428 {
429   CreateDeleteData *data = user_data;
430
431   data->timed_out = TRUE;
432   g_main_context_wakeup (data->context);
433
434   return G_SOURCE_REMOVE;
435 }
436
437 /*
438  * This test does a fully async create-write-read-delete.
439  * Callbackistan.
440  */
441 static void
442 test_create_delete (gconstpointer d)
443 {
444   GError *error;
445   CreateDeleteData *data;
446   GFileIOStream *iostream;
447
448   data = g_new0 (CreateDeleteData, 1);
449
450   data->buffersize = GPOINTER_TO_INT (d);
451   data->data = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
452   data->pos = 0;
453
454   data->file = g_file_new_tmp ("g_file_create_delete_XXXXXX",
455                                &iostream, NULL);
456   g_assert_nonnull (data->file);
457   g_object_unref (iostream);
458
459   data->monitor_path = g_file_get_path (data->file);
460   remove (data->monitor_path);
461
462   g_assert_false (g_file_query_exists (data->file, NULL));
463
464   error = NULL;
465   data->monitor = g_file_monitor_file (data->file, 0, NULL, &error);
466   g_assert_no_error (error);
467
468   /* This test doesn't work with GPollFileMonitor, because it assumes
469    * that the monitor will notice a create immediately followed by a
470    * delete, rather than coalescing them into nothing.
471    */
472   /* This test also doesn't work with GKqueueFileMonitor because of
473    * the same reason. Kqueue is able to return a kevent when a file is
474    * created or deleted in a directory. However, the kernel doesn't tell
475    * the program file names, so GKqueueFileMonitor has to calculate the
476    * difference itself. This is usually too slow for rapid file creation
477    * and deletion tests.
478    */
479   if (strcmp (G_OBJECT_TYPE_NAME (data->monitor), "GPollFileMonitor") == 0 ||
480       strcmp (G_OBJECT_TYPE_NAME (data->monitor), "GKqueueFileMonitor") == 0)
481     {
482       g_test_skip ("skipping test for this GFileMonitor implementation");
483       goto skip;
484     }
485
486   g_file_monitor_set_rate_limit (data->monitor, 100);
487
488   g_signal_connect (data->monitor, "changed", G_CALLBACK (monitor_changed), data);
489
490   /* Use the global default main context */
491   data->context = NULL;
492   data->timeout = g_timeout_add_seconds (10, stop_timeout, data);
493
494   g_file_create_async (data->file, 0, 0, NULL, created_cb, data);
495
496   while (!data->timed_out &&
497          (data->monitor_created == 0 ||
498           data->monitor_deleted == 0 ||
499           data->monitor_changed == 0 ||
500           !data->file_deleted))
501     g_main_context_iteration (data->context, TRUE);
502
503   g_source_remove (data->timeout);
504
505   g_assert_false (data->timed_out);
506   g_assert_true (data->file_deleted);
507   g_assert_cmpint (data->monitor_created, ==, 1);
508   g_assert_cmpint (data->monitor_deleted, ==, 1);
509   g_assert_cmpint (data->monitor_changed, >, 0);
510
511   g_assert_false (g_file_monitor_is_cancelled (data->monitor));
512   g_file_monitor_cancel (data->monitor);
513   g_assert_true (g_file_monitor_is_cancelled (data->monitor));
514
515   g_clear_pointer (&data->context, g_main_context_unref);
516   g_object_unref (data->ostream);
517   g_object_unref (data->istream);
518
519  skip:
520   g_object_unref (data->monitor);
521   g_object_unref (data->file);
522   g_free (data->monitor_path);
523   g_free (data->buffer);
524   g_free (data);
525 }
526
527 static const gchar *original_data =
528     "/**\n"
529     " * g_file_replace_contents_async:\n"
530     "**/\n";
531
532 static const gchar *replace_data =
533     "/**\n"
534     " * g_file_replace_contents_async:\n"
535     " * @file: input #GFile.\n"
536     " * @contents: string of contents to replace the file with.\n"
537     " * @length: the length of @contents in bytes.\n"
538     " * @etag: (nullable): a new <link linkend=\"gfile-etag\">entity tag</link> for the @file, or %NULL\n"
539     " * @make_backup: %TRUE if a backup should be created.\n"
540     " * @flags: a set of #GFileCreateFlags.\n"
541     " * @cancellable: optional #GCancellable object, %NULL to ignore.\n"
542     " * @callback: a #GAsyncReadyCallback to call when the request is satisfied\n"
543     " * @user_data: the data to pass to callback function\n"
544     " * \n"
545     " * Starts an asynchronous replacement of @file with the given \n"
546     " * @contents of @length bytes. @etag will replace the document's\n"
547     " * current entity tag.\n"
548     " * \n"
549     " * When this operation has completed, @callback will be called with\n"
550     " * @user_user data, and the operation can be finalized with \n"
551     " * g_file_replace_contents_finish().\n"
552     " * \n"
553     " * If @cancellable is not %NULL, then the operation can be cancelled by\n"
554     " * triggering the cancellable object from another thread. If the operation\n"
555     " * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. \n"
556     " * \n"
557     " * If @make_backup is %TRUE, this function will attempt to \n"
558     " * make a backup of @file.\n"
559     " **/\n";
560
561 typedef struct
562 {
563   GFile *file;
564   const gchar *data;
565   GMainLoop *loop;
566   gboolean again;
567 } ReplaceLoadData;
568
569 static void replaced_cb (GObject      *source,
570                          GAsyncResult *res,
571                          gpointer      user_data);
572
573 static void
574 loaded_cb (GObject      *source,
575            GAsyncResult *res,
576            gpointer      user_data)
577 {
578   ReplaceLoadData *data = user_data;
579   gboolean ret;
580   GError *error;
581   gchar *contents;
582   gsize length;
583
584   error = NULL;
585   ret = g_file_load_contents_finish (data->file, res, &contents, &length, NULL, &error);
586   g_assert_true (ret);
587   g_assert_no_error (error);
588   g_assert_cmpint (length, ==, strlen (data->data));
589   g_assert_cmpstr (contents, ==, data->data);
590
591   g_free (contents);
592
593   if (data->again)
594     {
595       data->again = FALSE;
596       data->data = "pi pa po";
597
598       g_file_replace_contents_async (data->file,
599                                      data->data,
600                                      strlen (data->data),
601                                      NULL,
602                                      FALSE,
603                                      0,
604                                      NULL,
605                                      replaced_cb,
606                                      data);
607     }
608   else
609     {
610        error = NULL;
611        ret = g_file_delete (data->file, NULL, &error);
612        g_assert_no_error (error);
613        g_assert_true (ret);
614        g_assert_false (g_file_query_exists (data->file, NULL));
615
616        g_main_loop_quit (data->loop);
617     }
618 }
619
620 static void
621 replaced_cb (GObject      *source,
622              GAsyncResult *res,
623              gpointer      user_data)
624 {
625   ReplaceLoadData *data = user_data;
626   GError *error;
627
628   error = NULL;
629   g_file_replace_contents_finish (data->file, res, NULL, &error);
630   g_assert_no_error (error);
631
632   g_file_load_contents_async (data->file, NULL, loaded_cb, data);
633 }
634
635 static void
636 test_replace_load (void)
637 {
638   ReplaceLoadData *data;
639   const gchar *path;
640   GFileIOStream *iostream;
641
642   data = g_new0 (ReplaceLoadData, 1);
643   data->again = TRUE;
644   data->data = replace_data;
645
646   data->file = g_file_new_tmp ("g_file_replace_load_XXXXXX",
647                                &iostream, NULL);
648   g_assert_nonnull (data->file);
649   g_object_unref (iostream);
650
651   path = g_file_peek_path (data->file);
652   remove (path);
653
654   g_assert_false (g_file_query_exists (data->file, NULL));
655
656   data->loop = g_main_loop_new (NULL, FALSE);
657
658   g_file_replace_contents_async (data->file,
659                                  data->data,
660                                  strlen (data->data),
661                                  NULL,
662                                  FALSE,
663                                  0,
664                                  NULL,
665                                  replaced_cb,
666                                  data);
667
668   g_main_loop_run (data->loop);
669
670   g_main_loop_unref (data->loop);
671   g_object_unref (data->file);
672   g_free (data);
673 }
674
675 static void
676 test_replace_cancel (void)
677 {
678   GFile *tmpdir, *file;
679   GFileOutputStream *ostream;
680   GFileEnumerator *fenum;
681   GFileInfo *info;
682   GCancellable *cancellable;
683   gchar *path;
684   gchar *contents;
685   gsize nwrote, length;
686   guint count;
687   GError *error = NULL;
688
689   g_test_bug ("629301");
690
691   path = g_dir_make_tmp ("g_file_replace_cancel_XXXXXX", &error);
692   g_assert_no_error (error);
693   tmpdir = g_file_new_for_path (path);
694   g_free (path);
695
696   file = g_file_get_child (tmpdir, "file");
697   g_file_replace_contents (file,
698                            original_data,
699                            strlen (original_data),
700                            NULL, FALSE, 0, NULL,
701                            NULL, &error);
702   g_assert_no_error (error);
703
704   ostream = g_file_replace (file, NULL, TRUE, 0, NULL, &error);
705   g_assert_no_error (error);
706
707   g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
708                              replace_data, strlen (replace_data),
709                              &nwrote, NULL, &error);
710   g_assert_no_error (error);
711   g_assert_cmpint (nwrote, ==, strlen (replace_data));
712
713   /* At this point there should be two files; the original and the
714    * temporary.
715    */
716   fenum = g_file_enumerate_children (tmpdir, NULL, 0, NULL, &error);
717   g_assert_no_error (error);
718
719   info = g_file_enumerator_next_file (fenum, NULL, &error);
720   g_assert_no_error (error);
721   g_assert_nonnull (info);
722   g_object_unref (info);
723   info = g_file_enumerator_next_file (fenum, NULL, &error);
724   g_assert_no_error (error);
725   g_assert_nonnull (info);
726   g_object_unref (info);
727
728   g_file_enumerator_close (fenum, NULL, &error);
729   g_assert_no_error (error);
730   g_object_unref (fenum);
731
732   /* Also test the g_file_enumerator_iterate() API */
733   fenum = g_file_enumerate_children (tmpdir, NULL, 0, NULL, &error);
734   g_assert_no_error (error);
735   count = 0;
736
737   while (TRUE)
738     {
739       gboolean ret = g_file_enumerator_iterate (fenum, &info, NULL, NULL, &error);
740       g_assert_true (ret);
741       g_assert_no_error (error);
742       if (!info)
743         break;
744       count++;
745     }
746   g_assert_cmpint (count, ==, 2);
747
748   g_file_enumerator_close (fenum, NULL, &error);
749   g_assert_no_error (error);
750   g_object_unref (fenum);
751
752   /* Now test just getting child from the g_file_enumerator_iterate() API */
753   fenum = g_file_enumerate_children (tmpdir, "standard::name", 0, NULL, &error);
754   g_assert_no_error (error);
755   count = 0;
756
757   while (TRUE)
758     {
759       GFile *child;
760       gboolean ret = g_file_enumerator_iterate (fenum, NULL, &child, NULL, &error);
761
762       g_assert_true (ret);
763       g_assert_no_error (error);
764
765       if (!child)
766         break;
767
768       g_assert_true (G_IS_FILE (child));
769       count++;
770     }
771   g_assert_cmpint (count, ==, 2);
772
773   g_file_enumerator_close (fenum, NULL, &error);
774   g_assert_no_error (error);
775   g_object_unref (fenum);
776
777   /* Make sure the temporary gets deleted even if we cancel. */
778   cancellable = g_cancellable_new ();
779   g_cancellable_cancel (cancellable);
780   g_output_stream_close (G_OUTPUT_STREAM (ostream), cancellable, &error);
781   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
782   g_clear_error (&error);
783
784   g_object_unref (cancellable);
785   g_object_unref (ostream);
786
787   /* Make sure that file contents wasn't actually replaced. */
788   g_file_load_contents (file,
789                         NULL,
790                         &contents,
791                         &length,
792                         NULL,
793                         &error);
794   g_assert_no_error (error);
795   g_assert_cmpstr (contents, ==, original_data);
796   g_free (contents);
797
798   g_file_delete (file, NULL, &error);
799   g_assert_no_error (error);
800   g_object_unref (file);
801
802   /* This will only succeed if the temp file was deleted. */
803   g_file_delete (tmpdir, NULL, &error);
804   g_assert_no_error (error);
805   g_object_unref (tmpdir);
806 }
807
808 static void
809 on_file_deleted (GObject      *object,
810                  GAsyncResult *result,
811                  gpointer      user_data)
812 {
813   GError *local_error = NULL;
814   GMainLoop *loop = user_data;
815
816   (void) g_file_delete_finish ((GFile*)object, result, &local_error);
817   g_assert_no_error (local_error);
818
819   g_main_loop_quit (loop);
820 }
821
822 static void
823 test_async_delete (void)
824 {
825   GFile *file;
826   GFileIOStream *iostream;
827   GError *local_error = NULL;
828   GError **error = &local_error;
829   GMainLoop *loop;
830
831   file = g_file_new_tmp ("g_file_delete_XXXXXX",
832                          &iostream, error);
833   g_assert_no_error (local_error);
834   g_object_unref (iostream);
835
836   g_assert_true (g_file_query_exists (file, NULL));
837
838   loop = g_main_loop_new (NULL, TRUE);
839
840   g_file_delete_async (file, G_PRIORITY_DEFAULT, NULL, on_file_deleted, loop);
841
842   g_main_loop_run (loop);
843
844   g_assert_false (g_file_query_exists (file, NULL));
845
846   g_main_loop_unref (loop);
847   g_object_unref (file);
848 }
849
850 static void
851 test_copy_preserve_mode (void)
852 {
853 #ifdef G_OS_UNIX
854   mode_t current_umask = umask (0);
855   const struct
856     {
857       guint32 source_mode;
858       guint32 expected_destination_mode;
859       gboolean create_destination_before_copy;
860       GFileCopyFlags copy_flags;
861     }
862   vectors[] =
863     {
864       /* Overwriting the destination file should copy the permissions from the
865        * source file, even if %G_FILE_COPY_ALL_METADATA is set: */
866       { 0600, 0600, TRUE, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA },
867       { 0600, 0600, TRUE, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS },
868       /* The same behaviour should hold if the destination file is not being
869        * overwritten because it doesn’t already exist: */
870       { 0600, 0600, FALSE, G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA },
871       { 0600, 0600, FALSE, G_FILE_COPY_NOFOLLOW_SYMLINKS },
872       /* Anything with %G_FILE_COPY_TARGET_DEFAULT_PERMS should use the current
873        * umask for the destination file: */
874       { 0600, 0666 & ~current_umask, TRUE, G_FILE_COPY_TARGET_DEFAULT_PERMS | G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA },
875       { 0600, 0666 & ~current_umask, TRUE, G_FILE_COPY_TARGET_DEFAULT_PERMS | G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS },
876       { 0600, 0666 & ~current_umask, FALSE, G_FILE_COPY_TARGET_DEFAULT_PERMS | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA },
877       { 0600, 0666 & ~current_umask, FALSE, G_FILE_COPY_TARGET_DEFAULT_PERMS | G_FILE_COPY_NOFOLLOW_SYMLINKS },
878     };
879   gsize i;
880
881   /* Reset the umask after querying it above. There’s no way to query it without
882    * changing it. */
883   umask (current_umask);
884   g_test_message ("Current umask: %u", current_umask);
885
886   for (i = 0; i < G_N_ELEMENTS (vectors); i++)
887     {
888       GFile *tmpfile;
889       GFile *dest_tmpfile;
890       GFileInfo *dest_info;
891       GFileIOStream *iostream;
892       GError *local_error = NULL;
893       guint32 romode = vectors[i].source_mode;
894       guint32 dest_mode;
895
896       g_test_message ("Vector %" G_GSIZE_FORMAT, i);
897
898       tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
899                                 &iostream, &local_error);
900       g_assert_no_error (local_error);
901       g_io_stream_close ((GIOStream*)iostream, NULL, &local_error);
902       g_assert_no_error (local_error);
903       g_clear_object (&iostream);
904
905       g_file_set_attribute (tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_ATTRIBUTE_TYPE_UINT32,
906                             &romode, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
907                             NULL, &local_error);
908       g_assert_no_error (local_error);
909
910       dest_tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
911                                      &iostream, &local_error);
912       g_assert_no_error (local_error);
913       g_io_stream_close ((GIOStream*)iostream, NULL, &local_error);
914       g_assert_no_error (local_error);
915       g_clear_object (&iostream);
916
917       if (!vectors[i].create_destination_before_copy)
918         {
919           g_file_delete (dest_tmpfile, NULL, &local_error);
920           g_assert_no_error (local_error);
921         }
922
923       g_file_copy (tmpfile, dest_tmpfile, vectors[i].copy_flags,
924                    NULL, NULL, NULL, &local_error);
925       g_assert_no_error (local_error);
926
927       dest_info = g_file_query_info (dest_tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
928                                      NULL, &local_error);
929       g_assert_no_error (local_error);
930
931       dest_mode = g_file_info_get_attribute_uint32 (dest_info, G_FILE_ATTRIBUTE_UNIX_MODE);
932
933       g_assert_cmpint (dest_mode & ~S_IFMT, ==, vectors[i].expected_destination_mode);
934       g_assert_cmpint (dest_mode & S_IFMT, ==, S_IFREG);
935
936       (void) g_file_delete (tmpfile, NULL, NULL);
937       (void) g_file_delete (dest_tmpfile, NULL, NULL);
938
939       g_clear_object (&tmpfile);
940       g_clear_object (&dest_tmpfile);
941       g_clear_object (&dest_info);
942     }
943 #else  /* if !G_OS_UNIX */
944   g_test_skip ("File permissions tests can only be run on Unix")
945 #endif
946 }
947
948 static gchar *
949 splice_to_string (GInputStream   *stream,
950                   GError        **error)
951 {
952   GMemoryOutputStream *buffer = NULL;
953   char *ret = NULL;
954
955   buffer = (GMemoryOutputStream*)g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
956   if (g_output_stream_splice ((GOutputStream*)buffer, stream, 0, NULL, error) < 0)
957     goto out;
958
959   if (!g_output_stream_write ((GOutputStream*)buffer, "\0", 1, NULL, error))
960     goto out;
961
962   if (!g_output_stream_close ((GOutputStream*)buffer, NULL, error))
963     goto out;
964
965   ret = g_memory_output_stream_steal_data (buffer);
966  out:
967   g_clear_object (&buffer);
968   return ret;
969 }
970
971 static gboolean
972 get_size_from_du (const gchar *path, guint64 *size)
973 {
974   GSubprocess *du;
975   gboolean ok;
976   gchar *result;
977   gchar *endptr;
978   GError *error = NULL;
979   gchar *du_path = NULL;
980
981   /* If we can’t find du, don’t try and run the test. */
982   du_path = g_find_program_in_path ("du");
983   if (du_path == NULL)
984     return FALSE;
985   g_free (du_path);
986
987   du = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE,
988                          &error,
989                          "du", "--bytes", "-s", path, NULL);
990   g_assert_no_error (error);
991
992   result = splice_to_string (g_subprocess_get_stdout_pipe (du), &error);
993   g_assert_no_error (error);
994
995   *size = g_ascii_strtoll (result, &endptr, 10);
996
997   g_subprocess_wait (du, NULL, &error);
998   g_assert_no_error (error);
999
1000   ok = g_subprocess_get_successful (du);
1001
1002   g_object_unref (du);
1003   g_free (result);
1004
1005   return ok;
1006 }
1007
1008 static void
1009 test_measure (void)
1010 {
1011   GFile *file;
1012   guint64 size;
1013   guint64 num_bytes;
1014   guint64 num_dirs;
1015   guint64 num_files;
1016   GError *error = NULL;
1017   gboolean ok;
1018   gchar *path;
1019
1020   path = g_test_build_filename (G_TEST_DIST, "desktop-files", NULL);
1021   file = g_file_new_for_path (path);
1022
1023   if (!get_size_from_du (path, &size))
1024     {
1025       g_test_message ("du not found or fail to run, skipping byte measurement");
1026       size = 0;
1027     }
1028
1029   ok = g_file_measure_disk_usage (file,
1030                                   G_FILE_MEASURE_APPARENT_SIZE,
1031                                   NULL,
1032                                   NULL,
1033                                   NULL,
1034                                   &num_bytes,
1035                                   &num_dirs,
1036                                   &num_files,
1037                                   &error);
1038   g_assert_true (ok);
1039   g_assert_no_error (error);
1040
1041   if (size > 0)
1042     g_assert_cmpuint (num_bytes, ==, size);
1043   g_assert_cmpuint (num_dirs, ==, 6);
1044   g_assert_cmpuint (num_files, ==, 31);
1045
1046   g_object_unref (file);
1047   g_free (path);
1048 }
1049
1050 typedef struct {
1051   guint64 expected_bytes;
1052   guint64 expected_dirs;
1053   guint64 expected_files;
1054   gint progress_count;
1055   guint64 progress_bytes;
1056   guint64 progress_dirs;
1057   guint64 progress_files;
1058 } MeasureData;
1059
1060 static void
1061 measure_progress (gboolean reporting,
1062                   guint64  current_size,
1063                   guint64  num_dirs,
1064                   guint64  num_files,
1065                   gpointer user_data)
1066 {
1067   MeasureData *data = user_data;
1068
1069   data->progress_count += 1;
1070
1071   g_assert_cmpuint (current_size, >=, data->progress_bytes);
1072   g_assert_cmpuint (num_dirs, >=, data->progress_dirs);
1073   g_assert_cmpuint (num_files, >=, data->progress_files);
1074
1075   data->progress_bytes = current_size;
1076   data->progress_dirs = num_dirs;
1077   data->progress_files = num_files;
1078 }
1079
1080 static void
1081 measure_done (GObject      *source,
1082               GAsyncResult *res,
1083               gpointer      user_data)
1084 {
1085   MeasureData *data = user_data;
1086   guint64 num_bytes, num_dirs, num_files;
1087   GError *error = NULL;
1088   gboolean ok;
1089
1090   ok = g_file_measure_disk_usage_finish (G_FILE (source), res, &num_bytes, &num_dirs, &num_files, &error);
1091   g_assert_true (ok);
1092   g_assert_no_error (error);
1093
1094   if (data->expected_bytes > 0)
1095     g_assert_cmpuint (data->expected_bytes, ==, num_bytes);
1096   g_assert_cmpuint (data->expected_dirs, ==, num_dirs);
1097   g_assert_cmpuint (data->expected_files, ==, num_files);
1098
1099   g_assert_cmpuint (data->progress_count, >, 0);
1100   g_assert_cmpuint (num_bytes, >=, data->progress_bytes);
1101   g_assert_cmpuint (num_dirs, >=, data->progress_dirs);
1102   g_assert_cmpuint (num_files, >=, data->progress_files);
1103
1104   g_free (data);
1105   g_object_unref (source);
1106 }
1107
1108 static void
1109 test_measure_async (void)
1110 {
1111   gchar *path;
1112   GFile *file;
1113   MeasureData *data;
1114
1115   data = g_new (MeasureData, 1);
1116
1117   data->progress_count = 0;
1118   data->progress_bytes = 0;
1119   data->progress_files = 0;
1120   data->progress_dirs = 0;
1121
1122   path = g_test_build_filename (G_TEST_DIST, "desktop-files", NULL);
1123   file = g_file_new_for_path (path);
1124
1125   if (!get_size_from_du (path, &data->expected_bytes))
1126     {
1127       g_test_message ("du not found or fail to run, skipping byte measurement");
1128       data->expected_bytes = 0;
1129     }
1130
1131   g_free (path);
1132
1133   data->expected_dirs = 6;
1134   data->expected_files = 31;
1135
1136   g_file_measure_disk_usage_async (file,
1137                                    G_FILE_MEASURE_APPARENT_SIZE,
1138                                    0, NULL,
1139                                    measure_progress, data,
1140                                    measure_done, data);
1141 }
1142
1143 static void
1144 test_load_bytes (void)
1145 {
1146   gchar filename[] = "g_file_load_bytes_XXXXXX";
1147   GError *error = NULL;
1148   GBytes *bytes;
1149   GFile *file;
1150   int len;
1151   int fd;
1152   int ret;
1153
1154   fd = g_mkstemp (filename);
1155   g_assert_cmpint (fd, !=, -1);
1156   len = strlen ("test_load_bytes");
1157   ret = write (fd, "test_load_bytes", len);
1158   g_assert_cmpint (ret, ==, len);
1159   close (fd);
1160
1161   file = g_file_new_for_path (filename);
1162   bytes = g_file_load_bytes (file, NULL, NULL, &error);
1163   g_assert_no_error (error);
1164   g_assert_nonnull (bytes);
1165   g_assert_cmpint (len, ==, g_bytes_get_size (bytes));
1166   g_assert_cmpstr ("test_load_bytes", ==, (gchar *)g_bytes_get_data (bytes, NULL));
1167
1168   g_file_delete (file, NULL, NULL);
1169
1170   g_bytes_unref (bytes);
1171   g_object_unref (file);
1172 }
1173
1174 typedef struct
1175 {
1176   GMainLoop *main_loop;
1177   GFile *file;
1178   GBytes *bytes;
1179 } LoadBytesAsyncData;
1180
1181 static void
1182 test_load_bytes_cb (GObject      *object,
1183                     GAsyncResult *result,
1184                     gpointer      user_data)
1185 {
1186   GFile *file = G_FILE (object);
1187   LoadBytesAsyncData *data = user_data;
1188   GError *error = NULL;
1189
1190   data->bytes = g_file_load_bytes_finish (file, result, NULL, &error);
1191   g_assert_no_error (error);
1192   g_assert_nonnull (data->bytes);
1193
1194   g_main_loop_quit (data->main_loop);
1195 }
1196
1197 static void
1198 test_load_bytes_async (void)
1199 {
1200   LoadBytesAsyncData data = { 0 };
1201   gchar filename[] = "g_file_load_bytes_XXXXXX";
1202   int len;
1203   int fd;
1204   int ret;
1205
1206   fd = g_mkstemp (filename);
1207   g_assert_cmpint (fd, !=, -1);
1208   len = strlen ("test_load_bytes_async");
1209   ret = write (fd, "test_load_bytes_async", len);
1210   g_assert_cmpint (ret, ==, len);
1211   close (fd);
1212
1213   data.main_loop = g_main_loop_new (NULL, FALSE);
1214   data.file = g_file_new_for_path (filename);
1215
1216   g_file_load_bytes_async (data.file, NULL, test_load_bytes_cb, &data);
1217   g_main_loop_run (data.main_loop);
1218
1219   g_assert_cmpint (len, ==, g_bytes_get_size (data.bytes));
1220   g_assert_cmpstr ("test_load_bytes_async", ==, (gchar *)g_bytes_get_data (data.bytes, NULL));
1221
1222   g_file_delete (data.file, NULL, NULL);
1223   g_object_unref (data.file);
1224   g_bytes_unref (data.bytes);
1225   g_main_loop_unref (data.main_loop);
1226 }
1227
1228 static void
1229 test_writev_helper (GOutputVector *vectors,
1230                     gsize          n_vectors,
1231                     gboolean       use_bytes_written,
1232                     const guint8  *expected_contents,
1233                     gsize          expected_length)
1234 {
1235   GFile *file;
1236   GFileIOStream *iostream = NULL;
1237   GOutputStream *ostream;
1238   GError *error = NULL;
1239   gsize bytes_written = 0;
1240   gboolean res;
1241   guint8 *contents;
1242   gsize length;
1243
1244   file = g_file_new_tmp ("g_file_writev_XXXXXX",
1245                          &iostream, NULL);
1246   g_assert_nonnull (file);
1247   g_assert_nonnull (iostream);
1248
1249   ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
1250
1251   res = g_output_stream_writev_all (ostream, vectors, n_vectors, use_bytes_written ? &bytes_written : NULL, NULL, &error);
1252   g_assert_no_error (error);
1253   g_assert_true (res);
1254   if (use_bytes_written)
1255     g_assert_cmpuint (bytes_written, ==, expected_length);
1256
1257   res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
1258   g_assert_no_error (error);
1259   g_assert_true (res);
1260   g_object_unref (iostream);
1261
1262   res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
1263   g_assert_no_error (error);
1264   g_assert_true (res);
1265
1266   g_assert_cmpmem (contents, length, expected_contents, expected_length);
1267
1268   g_free (contents);
1269
1270   g_file_delete (file, NULL, NULL);
1271   g_object_unref (file);
1272 }
1273
1274 /* Test that writev() on local file output streams works on a non-empty vector */
1275 static void
1276 test_writev (void)
1277 {
1278   GOutputVector vectors[3];
1279   const guint8 buffer[] = {1, 2, 3, 4, 5,
1280                            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
1281                            1, 2, 3};
1282
1283   vectors[0].buffer = buffer;
1284   vectors[0].size = 5;
1285
1286   vectors[1].buffer = buffer + 5;
1287   vectors[1].size = 12;
1288
1289   vectors[2].buffer = buffer + 5 + 12;
1290   vectors[2].size = 3;
1291
1292   test_writev_helper (vectors, G_N_ELEMENTS (vectors), TRUE, buffer, sizeof buffer);
1293 }
1294
1295 /* Test that writev() on local file output streams works on a non-empty vector without returning bytes_written */
1296 static void
1297 test_writev_no_bytes_written (void)
1298 {
1299   GOutputVector vectors[3];
1300   const guint8 buffer[] = {1, 2, 3, 4, 5,
1301                            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
1302                            1, 2, 3};
1303
1304   vectors[0].buffer = buffer;
1305   vectors[0].size = 5;
1306
1307   vectors[1].buffer = buffer + 5;
1308   vectors[1].size = 12;
1309
1310   vectors[2].buffer = buffer + 5 + 12;
1311   vectors[2].size = 3;
1312
1313   test_writev_helper (vectors, G_N_ELEMENTS (vectors), FALSE, buffer, sizeof buffer);
1314 }
1315
1316 /* Test that writev() on local file output streams works on 0 vectors */
1317 static void
1318 test_writev_no_vectors (void)
1319 {
1320   test_writev_helper (NULL, 0, TRUE, NULL, 0);
1321 }
1322
1323 /* Test that writev() on local file output streams works on empty vectors */
1324 static void
1325 test_writev_empty_vectors (void)
1326 {
1327   GOutputVector vectors[3];
1328
1329   vectors[0].buffer = NULL;
1330   vectors[0].size = 0;
1331   vectors[1].buffer = NULL;
1332   vectors[1].size = 0;
1333   vectors[2].buffer = NULL;
1334   vectors[2].size = 0;
1335
1336   test_writev_helper (vectors, G_N_ELEMENTS (vectors), TRUE, NULL, 0);
1337 }
1338
1339 /* Test that writev() fails if the sum of sizes in the vector is too big */
1340 static void
1341 test_writev_too_big_vectors (void)
1342 {
1343   GFile *file;
1344   GFileIOStream *iostream = NULL;
1345   GOutputStream *ostream;
1346   GError *error = NULL;
1347   gsize bytes_written = 0;
1348   gboolean res;
1349   guint8 *contents;
1350   gsize length;
1351   GOutputVector vectors[3];
1352
1353   vectors[0].buffer = (void*) 1;
1354   vectors[0].size = G_MAXSIZE / 2;
1355
1356   vectors[1].buffer = (void*) 1;
1357   vectors[1].size = G_MAXSIZE / 2;
1358
1359   vectors[2].buffer = (void*) 1;
1360   vectors[2].size = G_MAXSIZE / 2;
1361
1362   file = g_file_new_tmp ("g_file_writev_XXXXXX",
1363                          &iostream, NULL);
1364   g_assert_nonnull (file);
1365   g_assert_nonnull (iostream);
1366
1367   ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
1368
1369   res = g_output_stream_writev_all (ostream, vectors, G_N_ELEMENTS (vectors), &bytes_written, NULL, &error);
1370   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1371   g_assert_cmpuint (bytes_written, ==, 0);
1372   g_assert_false (res);
1373   g_clear_error (&error);
1374
1375   res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
1376   g_assert_no_error (error);
1377   g_assert_true (res);
1378   g_object_unref (iostream);
1379
1380   res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
1381   g_assert_no_error (error);
1382   g_assert_true (res);
1383
1384   g_assert_cmpmem (contents, length, NULL, 0);
1385
1386   g_free (contents);
1387
1388   g_file_delete (file, NULL, NULL);
1389   g_object_unref (file);
1390 }
1391
1392 typedef struct
1393 {
1394   gsize bytes_written;
1395   GOutputVector *vectors;
1396   gsize n_vectors;
1397   GError *error;
1398   gboolean done;
1399 } WritevAsyncData;
1400
1401 static void
1402 test_writev_async_cb (GObject      *object,
1403                       GAsyncResult *result,
1404                       gpointer      user_data)
1405 {
1406   GOutputStream *ostream = G_OUTPUT_STREAM (object);
1407   WritevAsyncData *data = user_data;
1408   GError *error = NULL;
1409   gsize bytes_written;
1410   gboolean res;
1411
1412   res = g_output_stream_writev_finish (ostream, result, &bytes_written, &error);
1413   g_assert_true (res);
1414   g_assert_no_error (error);
1415   data->bytes_written += bytes_written;
1416
1417   /* skip vectors that have been written in full */
1418   while (data->n_vectors > 0 && bytes_written >= data->vectors[0].size)
1419     {
1420       bytes_written -= data->vectors[0].size;
1421       ++data->vectors;
1422       --data->n_vectors;
1423     }
1424   /* skip partially written vector data */
1425   if (bytes_written > 0 && data->n_vectors > 0)
1426     {
1427       data->vectors[0].size -= bytes_written;
1428       data->vectors[0].buffer = ((guint8 *) data->vectors[0].buffer) + bytes_written;
1429     }
1430
1431   if (data->n_vectors > 0)
1432     g_output_stream_writev_async (ostream, data->vectors, data->n_vectors, 0, NULL, test_writev_async_cb, &data);
1433 }
1434
1435 /* Test that writev_async() on local file output streams works on a non-empty vector */
1436 static void
1437 test_writev_async (void)
1438 {
1439   WritevAsyncData data = { 0 };
1440   GFile *file;
1441   GFileIOStream *iostream = NULL;
1442   GOutputVector vectors[3];
1443   const guint8 buffer[] = {1, 2, 3, 4, 5,
1444                            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
1445                            1, 2, 3};
1446   GOutputStream *ostream;
1447   GError *error = NULL;
1448   gboolean res;
1449   guint8 *contents;
1450   gsize length;
1451
1452   vectors[0].buffer = buffer;
1453   vectors[0].size = 5;
1454
1455   vectors[1].buffer = buffer + 5;
1456   vectors[1].size = 12;
1457
1458   vectors[2].buffer = buffer + 5  + 12;
1459   vectors[2].size = 3;
1460
1461   file = g_file_new_tmp ("g_file_writev_XXXXXX",
1462                          &iostream, NULL);
1463   g_assert_nonnull (file);
1464   g_assert_nonnull (iostream);
1465
1466   data.vectors = vectors;
1467   data.n_vectors = G_N_ELEMENTS (vectors);
1468
1469   ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
1470
1471   g_output_stream_writev_async (ostream, data.vectors, data.n_vectors, 0, NULL, test_writev_async_cb, &data);
1472
1473   while (data.n_vectors > 0)
1474     g_main_context_iteration (NULL, TRUE);
1475
1476   g_assert_cmpuint (data.bytes_written, ==, sizeof buffer);
1477
1478   res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
1479   g_assert_no_error (error);
1480   g_assert_true (res);
1481   g_object_unref (iostream);
1482
1483   res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
1484   g_assert_no_error (error);
1485   g_assert_true (res);
1486
1487   g_assert_cmpmem (contents, length, buffer, sizeof buffer);
1488
1489   g_free (contents);
1490
1491   g_file_delete (file, NULL, NULL);
1492   g_object_unref (file);
1493 }
1494
1495 static void
1496 test_writev_all_cb (GObject      *object,
1497                     GAsyncResult *result,
1498                     gpointer      user_data)
1499 {
1500   GOutputStream *ostream = G_OUTPUT_STREAM (object);
1501   WritevAsyncData *data = user_data;
1502
1503   g_output_stream_writev_all_finish (ostream, result, &data->bytes_written, &data->error);
1504   data->done = TRUE;
1505 }
1506
1507 /* Test that writev_async_all() on local file output streams works on a non-empty vector */
1508 static void
1509 test_writev_async_all (void)
1510 {
1511   WritevAsyncData data = { 0 };
1512   GFile *file;
1513   GFileIOStream *iostream = NULL;
1514   GOutputStream *ostream;
1515   GOutputVector vectors[3];
1516   const guint8 buffer[] = {1, 2, 3, 4, 5,
1517                            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
1518                            1, 2, 3};
1519   GError *error = NULL;
1520   gboolean res;
1521   guint8 *contents;
1522   gsize length;
1523
1524   vectors[0].buffer = buffer;
1525   vectors[0].size = 5;
1526
1527   vectors[1].buffer = buffer + 5;
1528   vectors[1].size = 12;
1529
1530   vectors[2].buffer = buffer + 5  + 12;
1531   vectors[2].size = 3;
1532
1533   file = g_file_new_tmp ("g_file_writev_XXXXXX",
1534                          &iostream, NULL);
1535   g_assert_nonnull (file);
1536   g_assert_nonnull (iostream);
1537
1538   ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
1539
1540   g_output_stream_writev_all_async (ostream, vectors, G_N_ELEMENTS (vectors), 0, NULL, test_writev_all_cb, &data);
1541
1542   while (!data.done)
1543     g_main_context_iteration (NULL, TRUE);
1544
1545   g_assert_cmpuint (data.bytes_written, ==, sizeof buffer);
1546   g_assert_no_error (data.error);
1547
1548   res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
1549   g_assert_no_error (error);
1550   g_assert_true (res);
1551   g_object_unref (iostream);
1552
1553   res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
1554   g_assert_no_error (error);
1555   g_assert_true (res);
1556
1557   g_assert_cmpmem (contents, length, buffer, sizeof buffer);
1558
1559   g_free (contents);
1560
1561   g_file_delete (file, NULL, NULL);
1562   g_object_unref (file);
1563 }
1564
1565 /* Test that writev_async_all() on local file output streams handles cancellation correctly */
1566 static void
1567 test_writev_async_all_cancellation (void)
1568 {
1569   WritevAsyncData data = { 0 };
1570   GFile *file;
1571   GFileIOStream *iostream = NULL;
1572   GOutputVector vectors[3];
1573   const guint8 buffer[] = {1, 2, 3, 4, 5,
1574                            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
1575                            1, 2, 3};
1576   GOutputStream *ostream;
1577   GError *error = NULL;
1578   gboolean res;
1579   guint8 *contents;
1580   gsize length;
1581   GCancellable *cancellable;
1582
1583   vectors[0].buffer = buffer;
1584   vectors[0].size = 5;
1585
1586   vectors[1].buffer = buffer + 5;
1587   vectors[1].size = 12;
1588
1589   vectors[2].buffer = buffer + 5  + 12;
1590   vectors[2].size = 3;
1591
1592   file = g_file_new_tmp ("g_file_writev_XXXXXX",
1593                          &iostream, NULL);
1594   g_assert_nonnull (file);
1595   g_assert_nonnull (iostream);
1596
1597   ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
1598
1599   cancellable = g_cancellable_new ();
1600   g_cancellable_cancel (cancellable);
1601
1602   g_output_stream_writev_all_async (ostream, vectors, G_N_ELEMENTS (vectors), 0, cancellable, test_writev_all_cb, &data);
1603
1604   while (!data.done)
1605     g_main_context_iteration (NULL, TRUE);
1606
1607   g_assert_cmpuint (data.bytes_written, ==, 0);
1608   g_assert_error (data.error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
1609   g_clear_error (&data.error);
1610
1611   res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
1612   g_assert_no_error (error);
1613   g_assert_true (res);
1614   g_object_unref (iostream);
1615
1616   res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
1617   g_assert_no_error (error);
1618   g_assert_true (res);
1619   g_assert_cmpuint (length, ==, 0);
1620
1621   g_free (contents);
1622
1623   g_file_delete (file, NULL, NULL);
1624   g_object_unref (file);
1625   g_object_unref (cancellable);
1626 }
1627
1628 /* Test that writev_async_all() with empty vectors is handled correctly */
1629 static void
1630 test_writev_async_all_empty_vectors (void)
1631 {
1632   WritevAsyncData data = { 0 };
1633   GFile *file;
1634   GFileIOStream *iostream = NULL;
1635   GOutputVector vectors[3];
1636   GOutputStream *ostream;
1637   GError *error = NULL;
1638   gboolean res;
1639   guint8 *contents;
1640   gsize length;
1641
1642   vectors[0].buffer = NULL;
1643   vectors[0].size = 0;
1644
1645   vectors[1].buffer = NULL;
1646   vectors[1].size = 0;
1647
1648   vectors[2].buffer = NULL;
1649   vectors[2].size = 0;
1650
1651   file = g_file_new_tmp ("g_file_writev_XXXXXX",
1652                          &iostream, NULL);
1653   g_assert_nonnull (file);
1654   g_assert_nonnull (iostream);
1655
1656   ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
1657
1658   g_output_stream_writev_all_async (ostream, vectors, G_N_ELEMENTS (vectors), 0, NULL, test_writev_all_cb, &data);
1659
1660   while (!data.done)
1661     g_main_context_iteration (NULL, TRUE);
1662
1663   g_assert_cmpuint (data.bytes_written, ==, 0);
1664   g_assert_no_error (data.error);
1665   g_clear_error (&data.error);
1666
1667   res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
1668   g_assert_no_error (error);
1669   g_assert_true (res);
1670   g_object_unref (iostream);
1671
1672   res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
1673   g_assert_no_error (error);
1674   g_assert_true (res);
1675   g_assert_cmpuint (length, ==, 0);
1676
1677   g_free (contents);
1678
1679   g_file_delete (file, NULL, NULL);
1680   g_object_unref (file);
1681 }
1682
1683 /* Test that writev_async_all() with no vectors is handled correctly */
1684 static void
1685 test_writev_async_all_no_vectors (void)
1686 {
1687   WritevAsyncData data = { 0 };
1688   GFile *file;
1689   GFileIOStream *iostream = NULL;
1690   GOutputStream *ostream;
1691   GError *error = NULL;
1692   gboolean res;
1693   guint8 *contents;
1694   gsize length;
1695
1696   file = g_file_new_tmp ("g_file_writev_XXXXXX",
1697                          &iostream, NULL);
1698   g_assert_nonnull (file);
1699   g_assert_nonnull (iostream);
1700
1701   ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
1702
1703   g_output_stream_writev_all_async (ostream, NULL, 0, 0, NULL, test_writev_all_cb, &data);
1704
1705   while (!data.done)
1706     g_main_context_iteration (NULL, TRUE);
1707
1708   g_assert_cmpuint (data.bytes_written, ==, 0);
1709   g_assert_no_error (data.error);
1710   g_clear_error (&data.error);
1711
1712   res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
1713   g_assert_no_error (error);
1714   g_assert_true (res);
1715   g_object_unref (iostream);
1716
1717   res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
1718   g_assert_no_error (error);
1719   g_assert_true (res);
1720   g_assert_cmpuint (length, ==, 0);
1721
1722   g_free (contents);
1723
1724   g_file_delete (file, NULL, NULL);
1725   g_object_unref (file);
1726 }
1727
1728 /* Test that writev_async_all() with too big vectors is handled correctly */
1729 static void
1730 test_writev_async_all_too_big_vectors (void)
1731 {
1732   WritevAsyncData data = { 0 };
1733   GFile *file;
1734   GFileIOStream *iostream = NULL;
1735   GOutputVector vectors[3];
1736   GOutputStream *ostream;
1737   GError *error = NULL;
1738   gboolean res;
1739   guint8 *contents;
1740   gsize length;
1741
1742   vectors[0].buffer = (void*) 1;
1743   vectors[0].size = G_MAXSIZE / 2;
1744
1745   vectors[1].buffer = (void*) 1;
1746   vectors[1].size = G_MAXSIZE / 2;
1747
1748   vectors[2].buffer = (void*) 1;
1749   vectors[2].size = G_MAXSIZE / 2;
1750
1751   file = g_file_new_tmp ("g_file_writev_XXXXXX",
1752                          &iostream, NULL);
1753   g_assert_nonnull (file);
1754   g_assert_nonnull (iostream);
1755
1756   ostream = g_io_stream_get_output_stream (G_IO_STREAM (iostream));
1757
1758   g_output_stream_writev_all_async (ostream, vectors, G_N_ELEMENTS (vectors), 0, NULL, test_writev_all_cb, &data);
1759
1760   while (!data.done)
1761     g_main_context_iteration (NULL, TRUE);
1762
1763   g_assert_cmpuint (data.bytes_written, ==, 0);
1764   g_assert_error (data.error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1765   g_clear_error (&data.error);
1766
1767   res = g_io_stream_close (G_IO_STREAM (iostream), NULL, &error);
1768   g_assert_no_error (error);
1769   g_assert_true (res);
1770   g_object_unref (iostream);
1771
1772   res = g_file_load_contents (file, NULL, (gchar **) &contents, &length, NULL, &error);
1773   g_assert_no_error (error);
1774   g_assert_true (res);
1775   g_assert_cmpuint (length, ==, 0);
1776
1777   g_free (contents);
1778
1779   g_file_delete (file, NULL, NULL);
1780   g_object_unref (file);
1781 }
1782
1783 int
1784 main (int argc, char *argv[])
1785 {
1786   g_test_init (&argc, &argv, NULL);
1787
1788   g_test_bug_base ("http://bugzilla.gnome.org/");
1789
1790   g_test_add_func ("/file/basic", test_basic);
1791   g_test_add_func ("/file/build-filename", test_build_filename);
1792   g_test_add_func ("/file/parent", test_parent);
1793   g_test_add_func ("/file/child", test_child);
1794   g_test_add_func ("/file/type", test_type);
1795   g_test_add_func ("/file/parse-name", test_parse_name);
1796   g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);
1797   g_test_add_data_func ("/file/async-create-delete/1", GINT_TO_POINTER (1), test_create_delete);
1798   g_test_add_data_func ("/file/async-create-delete/10", GINT_TO_POINTER (10), test_create_delete);
1799   g_test_add_data_func ("/file/async-create-delete/25", GINT_TO_POINTER (25), test_create_delete);
1800   g_test_add_data_func ("/file/async-create-delete/4096", GINT_TO_POINTER (4096), test_create_delete);
1801   g_test_add_func ("/file/replace-load", test_replace_load);
1802   g_test_add_func ("/file/replace-cancel", test_replace_cancel);
1803   g_test_add_func ("/file/async-delete", test_async_delete);
1804   g_test_add_func ("/file/copy-preserve-mode", test_copy_preserve_mode);
1805   g_test_add_func ("/file/measure", test_measure);
1806   g_test_add_func ("/file/measure-async", test_measure_async);
1807   g_test_add_func ("/file/load-bytes", test_load_bytes);
1808   g_test_add_func ("/file/load-bytes-async", test_load_bytes_async);
1809   g_test_add_func ("/file/writev", test_writev);
1810   g_test_add_func ("/file/writev/no-bytes-written", test_writev_no_bytes_written);
1811   g_test_add_func ("/file/writev/no-vectors", test_writev_no_vectors);
1812   g_test_add_func ("/file/writev/empty-vectors", test_writev_empty_vectors);
1813   g_test_add_func ("/file/writev/too-big-vectors", test_writev_too_big_vectors);
1814   g_test_add_func ("/file/writev/async", test_writev_async);
1815   g_test_add_func ("/file/writev/async_all", test_writev_async_all);
1816   g_test_add_func ("/file/writev/async_all-empty-vectors", test_writev_async_all_empty_vectors);
1817   g_test_add_func ("/file/writev/async_all-no-vectors", test_writev_async_all_no_vectors);
1818   g_test_add_func ("/file/writev/async_all-to-big-vectors", test_writev_async_all_too_big_vectors);
1819   g_test_add_func ("/file/writev/async_all-cancellation", test_writev_async_all_cancellation);
1820
1821   return g_test_run ();
1822 }