gio/tests: fix leaks
[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
7 static void
8 test_basic (void)
9 {
10   GFile *file;
11   gchar *s;
12
13   file = g_file_new_for_path ("./some/directory/testfile");
14
15   s = g_file_get_basename (file);
16   g_assert_cmpstr (s, ==, "testfile");
17   g_free (s);
18
19   s = g_file_get_uri (file);
20   g_assert (g_str_has_prefix (s, "file://"));
21   g_assert (g_str_has_suffix (s, "/some/directory/testfile"));
22   g_free (s);
23
24   g_assert (g_file_has_uri_scheme (file, "file"));
25   s = g_file_get_uri_scheme (file);
26   g_assert_cmpstr (s, ==, "file");
27   g_free (s);
28
29   g_object_unref (file);
30 }
31
32 static void
33 test_parent (void)
34 {
35   GFile *file;
36   GFile *file2;
37   GFile *parent;
38   GFile *root;
39
40   file = g_file_new_for_path ("./some/directory/testfile");
41   file2 = g_file_new_for_path ("./some/directory");
42   root = g_file_new_for_path ("/");
43
44   g_assert (g_file_has_parent (file, file2));
45
46   parent = g_file_get_parent (file);
47   g_assert (g_file_equal (parent, file2));
48   g_object_unref (parent);
49
50   g_assert (g_file_get_parent (root) == NULL);
51
52   g_object_unref (file);
53   g_object_unref (file2);
54   g_object_unref (root);
55 }
56
57 static void
58 test_child (void)
59 {
60   GFile *file;
61   GFile *child;
62   GFile *child2;
63
64   file = g_file_new_for_path ("./some/directory");
65   child = g_file_get_child (file, "child");
66   g_assert (g_file_has_parent (child, file));
67
68   child2 = g_file_get_child_for_display_name (file, "child2", NULL);
69   g_assert (g_file_has_parent (child2, file));
70
71   g_object_unref (child);
72   g_object_unref (child2);
73   g_object_unref (file);
74 }
75
76 static void
77 test_type (void)
78 {
79   GFile *file;
80   GFileType type;
81   GError *error = NULL;
82
83   file = g_file_new_for_path (SRCDIR "/file.c");
84   type = g_file_query_file_type (file, 0, NULL);
85   g_assert_cmpint (type, ==, G_FILE_TYPE_REGULAR);
86   g_object_unref (file);
87
88   file = g_file_new_for_path (SRCDIR "/schema-tests");
89   type = g_file_query_file_type (file, 0, NULL);
90   g_assert_cmpint (type, ==, G_FILE_TYPE_DIRECTORY);
91
92   g_file_read (file, NULL, &error);
93   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
94   g_error_free (error);
95   g_object_unref (file);
96 }
97
98
99 typedef struct
100 {
101   GFile *file;
102   GFileMonitor *monitor;
103   GOutputStream *ostream;
104   GInputStream *istream;
105   GMainLoop *loop;
106   gint buffersize;
107   gint monitor_created;
108   gint monitor_deleted;
109   gint monitor_changed;
110   gchar *monitor_path;
111   gint pos;
112   gchar *data;
113   gchar *buffer;
114   guint timeout;
115 } CreateDeleteData;
116
117 static void
118 monitor_changed (GFileMonitor      *monitor,
119                  GFile             *file,
120                  GFile             *other_file,
121                  GFileMonitorEvent  event_type,
122                  gpointer           user_data)
123 {
124   CreateDeleteData *data = user_data;
125   gchar *path;
126
127   path = g_file_get_path (file);
128   g_assert_cmpstr (data->monitor_path, ==, path);
129   g_free (path);
130
131   if (event_type == G_FILE_MONITOR_EVENT_CREATED)
132     data->monitor_created++;
133   if (event_type == G_FILE_MONITOR_EVENT_DELETED)
134     data->monitor_deleted++;
135   if (event_type == G_FILE_MONITOR_EVENT_CHANGED)
136     data->monitor_changed++;
137 }
138
139
140 static gboolean
141 quit_idle (gpointer user_data)
142 {
143   CreateDeleteData *data = user_data;
144
145   g_source_remove (data->timeout); 
146   g_main_loop_quit (data->loop);
147
148   return FALSE;
149 }
150
151 static void
152 iclosed_cb (GObject      *source,
153             GAsyncResult *res,
154             gpointer      user_data)
155 {
156   CreateDeleteData *data = user_data;
157   GError *error;
158   gboolean ret;
159
160   error = NULL;
161   ret = g_input_stream_close_finish (data->istream, res, &error);
162   g_assert_no_error (error);
163   g_assert (ret);
164
165   g_assert (g_input_stream_is_closed (data->istream));
166
167   ret = g_file_delete (data->file, NULL, &error);
168   g_assert (ret);
169   g_assert_no_error (error);
170
171   /* work around file monitor bug:
172    * inotify events are only processed every 1000 ms, regardless
173    * of the rate limit set on the file monitor
174    */
175   g_timeout_add (2000, quit_idle, data);
176 }
177
178 static void
179 read_cb (GObject      *source,
180          GAsyncResult *res,
181          gpointer      user_data)
182 {
183   CreateDeleteData *data = user_data;
184   GError *error;
185   gssize size;
186
187   error = NULL;
188   size = g_input_stream_read_finish (data->istream, res, &error);
189   g_assert_no_error (error);
190
191   data->pos += size;
192   if (data->pos < strlen (data->data))
193     {
194       g_input_stream_read_async (data->istream,
195                                  data->buffer + data->pos,
196                                  strlen (data->data) - data->pos,
197                                  0,
198                                  NULL,
199                                  read_cb,
200                                  data);
201     }
202   else
203     {
204       g_assert_cmpstr (data->buffer, ==, data->data);
205       g_assert (!g_input_stream_is_closed (data->istream));
206       g_input_stream_close_async (data->istream, 0, NULL, iclosed_cb, data);
207     }
208 }
209
210 static void
211 ipending_cb (GObject      *source,
212              GAsyncResult *res,
213              gpointer      user_data)
214 {
215   CreateDeleteData *data = user_data;
216   GError *error;
217
218   error = NULL;
219   g_input_stream_read_finish (data->istream, res, &error);
220   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
221   g_error_free (error);
222 }
223
224 static void
225 skipped_cb (GObject      *source,
226             GAsyncResult *res,
227             gpointer      user_data)
228 {
229   CreateDeleteData *data = user_data;
230   GError *error;
231   gssize size;
232
233   error = NULL;
234   size = g_input_stream_skip_finish (data->istream, res, &error);
235   g_assert_no_error (error);
236   g_assert_cmpint (size, ==, data->pos);
237
238   g_input_stream_read_async (data->istream,
239                              data->buffer + data->pos,
240                              strlen (data->data) - data->pos,
241                              0,
242                              NULL,
243                              read_cb,
244                              data);
245   /* check that we get a pending error */
246   g_input_stream_read_async (data->istream,
247                              data->buffer + data->pos,
248                              strlen (data->data) - data->pos,
249                              0,
250                              NULL,
251                              ipending_cb,
252                              data);
253 }
254
255 static void
256 opened_cb (GObject      *source,
257            GAsyncResult *res,
258            gpointer      user_data)
259 {
260   GFileInputStream *base;
261   CreateDeleteData *data = user_data;
262   GError *error;
263
264   error = NULL;
265   base = g_file_read_finish (data->file, res, &error);
266   g_assert_no_error (error);
267
268   if (data->buffersize == 0)
269     data->istream = G_INPUT_STREAM (g_object_ref (base));
270   else
271     data->istream = g_buffered_input_stream_new_sized (G_INPUT_STREAM (base), data->buffersize);
272   g_object_unref (base);
273
274   data->buffer = g_new0 (gchar, strlen (data->data) + 1);
275
276   /* copy initial segment directly, then skip */
277   memcpy (data->buffer, data->data, 10);
278   data->pos = 10;
279
280   g_input_stream_skip_async (data->istream,
281                              10,
282                              0,
283                              NULL,
284                              skipped_cb,
285                              data);
286 }
287
288 static void
289 oclosed_cb (GObject      *source,
290             GAsyncResult *res,
291             gpointer      user_data)
292 {
293   CreateDeleteData *data = user_data;
294   GError *error;
295   gboolean ret;
296
297   error = NULL;
298   ret = g_output_stream_close_finish (data->ostream, res, &error);
299   g_assert_no_error (error);
300   g_assert (ret);
301   g_assert (g_output_stream_is_closed (data->ostream));
302
303   g_file_read_async (data->file, 0, NULL, opened_cb, data);
304 }
305
306 static void
307 written_cb (GObject      *source,
308             GAsyncResult *res,
309             gpointer      user_data)
310 {
311   CreateDeleteData *data = user_data;
312   gssize size;
313   GError *error;
314
315   error = NULL;
316   size = g_output_stream_write_finish (data->ostream, res, &error);
317   g_assert_no_error (error);
318
319   data->pos += size;
320   if (data->pos < strlen (data->data))
321     {
322       g_output_stream_write_async (data->ostream,
323                                    data->data + data->pos,
324                                    strlen (data->data) - data->pos,
325                                    0,
326                                    NULL,
327                                    written_cb,
328                                    data);
329     }
330   else
331     {
332       g_assert (!g_output_stream_is_closed (data->ostream));
333       g_output_stream_close_async (data->ostream, 0, NULL, oclosed_cb, data);
334     }
335 }
336
337 static void
338 opending_cb (GObject      *source,
339              GAsyncResult *res,
340              gpointer      user_data)
341 {
342   CreateDeleteData *data = user_data;
343   GError *error;
344
345   error = NULL;
346   g_output_stream_write_finish (data->ostream, res, &error);
347   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
348   g_error_free (error);
349 }
350
351 static void
352 created_cb (GObject      *source,
353             GAsyncResult *res,
354             gpointer      user_data)
355 {
356   GFileOutputStream *base;
357   CreateDeleteData *data = user_data;
358   GError *error;
359
360   error = NULL;
361   base = g_file_create_finish (G_FILE (source), res, &error);
362   g_assert_no_error (error);
363   g_assert (g_file_query_exists  (data->file, NULL));
364
365   if (data->buffersize == 0)
366     data->ostream = G_OUTPUT_STREAM (g_object_ref (base));
367   else
368     data->ostream = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), data->buffersize);
369   g_object_unref (base);
370
371   g_output_stream_write_async (data->ostream,
372                                data->data,
373                                strlen (data->data),
374                                0,
375                                NULL,
376                                written_cb,
377                                data);
378   /* check that we get a pending error */
379   g_output_stream_write_async (data->ostream,
380                                data->data,
381                                strlen (data->data),
382                                0,
383                                NULL,
384                                opending_cb,
385                                data);
386 }
387
388 static gboolean
389 stop_timeout (gpointer data)
390 {
391   g_assert_not_reached ();
392
393   return FALSE;
394 }
395
396 /*
397  * This test does a fully async create-write-read-delete.
398  * Callbackistan.
399  */
400 static void
401 test_create_delete (gconstpointer d)
402 {
403   GError *error;
404   CreateDeleteData *data;
405   GFileIOStream *iostream;
406
407   data = g_new0 (CreateDeleteData, 1);
408
409   data->buffersize = GPOINTER_TO_INT (d);
410   data->data = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
411   data->pos = 0;
412
413   data->file = g_file_new_tmp ("g_file_create_delete_XXXXXX",
414                                &iostream, NULL);
415   g_assert (data->file != NULL);
416   g_object_unref (iostream);
417
418   data->monitor_path = g_file_get_path (data->file);
419   remove (data->monitor_path);
420
421   g_assert (!g_file_query_exists  (data->file, NULL));
422
423   error = NULL;
424   data->monitor = g_file_monitor_file (data->file, 0, NULL, &error);
425   g_assert_no_error (error);
426
427   /* This test doesn't work with GPollFileMonitor, because it assumes
428    * that the monitor will notice a create immediately followed by a
429    * delete, rather than coalescing them into nothing.
430    */
431   if (!strcmp (G_OBJECT_TYPE_NAME (data->monitor), "GPollFileMonitor"))
432     {
433       g_print ("skipping test for this GFileMonitor implementation");
434       goto skip;
435     }
436
437   g_file_monitor_set_rate_limit (data->monitor, 100);
438
439   g_signal_connect (data->monitor, "changed", G_CALLBACK (monitor_changed), data);
440
441   data->loop = g_main_loop_new (NULL, FALSE);
442
443   data->timeout = g_timeout_add (5000, stop_timeout, NULL);
444
445   g_file_create_async (data->file, 0, 0, NULL, created_cb, data);
446
447   g_main_loop_run (data->loop);
448
449   g_assert_cmpint (data->monitor_created, ==, 1);
450   g_assert_cmpint (data->monitor_deleted, ==, 1);
451   g_assert_cmpint (data->monitor_changed, >, 0);
452
453   g_assert (!g_file_monitor_is_cancelled (data->monitor));
454   g_file_monitor_cancel (data->monitor);
455   g_assert (g_file_monitor_is_cancelled (data->monitor));
456
457   g_main_loop_unref (data->loop);
458   g_object_unref (data->ostream);
459   g_object_unref (data->istream);
460
461  skip:
462   g_object_unref (data->monitor);
463   g_object_unref (data->file);
464   free (data->monitor_path);
465   g_free (data->buffer);
466   g_free (data);
467 }
468
469 typedef struct
470 {
471   GFile *file;
472   const gchar *data;
473   GMainLoop *loop;
474   gboolean again;
475 } ReplaceLoadData;
476
477 static void replaced_cb (GObject      *source,
478                          GAsyncResult *res,
479                          gpointer      user_data);
480
481 static void
482 loaded_cb (GObject      *source,
483            GAsyncResult *res,
484            gpointer      user_data)
485 {
486   ReplaceLoadData *data = user_data;
487   gboolean ret;
488   GError *error;
489   gchar *contents;
490   gsize length;
491
492   error = NULL;
493   ret = g_file_load_contents_finish (data->file, res, &contents, &length, NULL, &error);
494   g_assert (ret);
495   g_assert_no_error (error);
496   g_assert_cmpint (length, ==, strlen (data->data));
497   g_assert_cmpstr (contents, ==, data->data);
498
499   g_free (contents);
500
501   if (data->again)
502     {
503       data->again = FALSE;
504       data->data = "pi pa po";
505
506       g_file_replace_contents_async (data->file,
507                                      data->data,
508                                      strlen (data->data),
509                                      NULL,
510                                      FALSE,
511                                      0,
512                                      NULL,
513                                      replaced_cb,
514                                      data);
515     }
516   else
517     {
518        error = NULL;
519        ret = g_file_delete (data->file, NULL, &error);
520        g_assert_no_error (error);
521        g_assert (ret);
522        g_assert (!g_file_query_exists (data->file, NULL));
523
524        g_main_loop_quit (data->loop);
525     }
526 }
527
528 static void
529 replaced_cb (GObject      *source,
530              GAsyncResult *res,
531              gpointer      user_data)
532 {
533   ReplaceLoadData *data = user_data;
534   GError *error;
535
536   error = NULL;
537   g_file_replace_contents_finish (data->file, res, NULL, &error);
538   g_assert_no_error (error);
539
540   g_file_load_contents_async (data->file, NULL, loaded_cb, data);
541 }
542
543 static void
544 test_replace_load (void)
545 {
546   ReplaceLoadData *data;
547   gchar *path;
548   GFileIOStream *iostream;
549
550   data = g_new0 (ReplaceLoadData, 1);
551   data->again = TRUE;
552   data->data =
553     "/**\n"
554     " * g_file_replace_contents_async:\n"
555     " * @file: input #GFile.\n"
556     " * @contents: string of contents to replace the file with.\n"
557     " * @length: the length of @contents in bytes.\n"
558     " * @etag: (allow-none): a new <link linkend=\"gfile-etag\">entity tag</link> for the @file, or %NULL\n"
559     " * @make_backup: %TRUE if a backup should be created.\n"
560     " * @flags: a set of #GFileCreateFlags.\n"
561     " * @cancellable: optional #GCancellable object, %NULL to ignore.\n"
562     " * @callback: a #GAsyncReadyCallback to call when the request is satisfied\n"
563     " * @user_data: the data to pass to callback function\n"
564     " * \n"
565     " * Starts an asynchronous replacement of @file with the given \n"
566     " * @contents of @length bytes. @etag will replace the document's\n"
567     " * current entity tag.\n"
568     " * \n"
569     " * When this operation has completed, @callback will be called with\n"
570     " * @user_user data, and the operation can be finalized with \n"
571     " * g_file_replace_contents_finish().\n"
572     " * \n"
573     " * If @cancellable is not %NULL, then the operation can be cancelled by\n"
574     " * triggering the cancellable object from another thread. If the operation\n"
575     " * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. \n"
576     " * \n"
577     " * If @make_backup is %TRUE, this function will attempt to \n"
578     " * make a backup of @file.\n"
579     " **/\n";
580
581   data->file = g_file_new_tmp ("g_file_replace_load_XXXXXX",
582                                &iostream, NULL);
583   g_assert (data->file != NULL);
584   g_object_unref (iostream);
585
586   path = g_file_get_path (data->file);
587   remove (path);
588
589   g_assert (!g_file_query_exists (data->file, NULL));
590
591   data->loop = g_main_loop_new (NULL, FALSE);
592
593   g_file_replace_contents_async (data->file,
594                                  data->data,
595                                  strlen (data->data),
596                                  NULL,
597                                  FALSE,
598                                  0,
599                                  NULL,
600                                  replaced_cb,
601                                  data);
602
603   g_main_loop_run (data->loop);
604
605   g_main_loop_unref (data->loop);
606   g_object_unref (data->file);
607   g_free (data);
608   free (path);
609 }
610
611 static void
612 on_file_deleted (GObject      *object,
613                  GAsyncResult *result,
614                  gpointer      user_data)
615 {
616   GError *local_error = NULL;
617   GMainLoop *loop = user_data;
618
619   (void) g_file_delete_finish ((GFile*)object, result, &local_error);
620   g_assert_no_error (local_error);
621
622   g_main_loop_quit (loop);
623 }
624
625 static void
626 test_async_delete (void)
627 {
628   GFile *file;
629   GFileIOStream *iostream;
630   GError *local_error = NULL;
631   GError **error = &local_error;
632   GMainLoop *loop;
633
634   file = g_file_new_tmp ("g_file_delete_XXXXXX",
635                          &iostream, error);
636   g_assert_no_error (local_error);
637   g_object_unref (iostream);
638
639   g_assert (g_file_query_exists (file, NULL));
640
641   loop = g_main_loop_new (NULL, TRUE);
642
643   g_file_delete_async (file, G_PRIORITY_DEFAULT, NULL, on_file_deleted, loop);
644
645   g_main_loop_run (loop);
646
647   g_assert (!g_file_query_exists (file, NULL));
648
649   g_main_loop_unref (loop);
650   g_object_unref (file);
651 }
652
653 int
654 main (int argc, char *argv[])
655 {
656   g_type_init ();
657
658   g_test_init (&argc, &argv, NULL);
659
660   g_test_add_func ("/file/basic", test_basic);
661   g_test_add_func ("/file/parent", test_parent);
662   g_test_add_func ("/file/child", test_child);
663   g_test_add_func ("/file/type", test_type);
664   g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);
665   g_test_add_data_func ("/file/async-create-delete/1", GINT_TO_POINTER (1), test_create_delete);
666   g_test_add_data_func ("/file/async-create-delete/10", GINT_TO_POINTER (10), test_create_delete);
667   g_test_add_data_func ("/file/async-create-delete/25", GINT_TO_POINTER (25), test_create_delete);
668   g_test_add_data_func ("/file/async-create-delete/4096", GINT_TO_POINTER (4096), test_create_delete);
669   g_test_add_func ("/file/replace-load", test_replace_load);
670   g_test_add_func ("/file/async-delete", test_async_delete);
671
672   return g_test_run ();
673 }