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