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