Tizen 2.1 base
[platform/upstream/glib2.0.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   g_file_monitor_set_rate_limit (data->monitor, 100);
424
425   g_signal_connect (data->monitor, "changed", G_CALLBACK (monitor_changed), data);
426
427   data->loop = g_main_loop_new (NULL, FALSE);
428
429   data->timeout = g_timeout_add (5000, stop_timeout, NULL);
430
431   g_file_create_async (data->file, 0, 0, NULL, created_cb, data);
432
433   g_main_loop_run (data->loop);
434
435   g_assert_cmpint (data->monitor_created, ==, 1);
436   g_assert_cmpint (data->monitor_deleted, ==, 1);
437   g_assert_cmpint (data->monitor_changed, >, 0);
438
439   g_assert (!g_file_monitor_is_cancelled (data->monitor));
440   g_file_monitor_cancel (data->monitor);
441   g_assert (g_file_monitor_is_cancelled (data->monitor));
442
443   g_main_loop_unref (data->loop);
444   g_object_unref (data->monitor);
445   g_object_unref (data->ostream);
446   g_object_unref (data->istream);
447   g_object_unref (data->file);
448   free (data->monitor_path);
449   g_free (data->buffer);
450   g_free (data);
451 }
452
453 typedef struct
454 {
455   GFile *file;
456   const gchar *data;
457   GMainLoop *loop;
458   gboolean again;
459 } ReplaceLoadData;
460
461 static void replaced_cb (GObject      *source,
462                          GAsyncResult *res,
463                          gpointer      user_data);
464
465 static void
466 loaded_cb (GObject      *source,
467            GAsyncResult *res,
468            gpointer      user_data)
469 {
470   ReplaceLoadData *data = user_data;
471   gboolean ret;
472   GError *error;
473   gchar *contents;
474   gsize length;
475
476   error = NULL;
477   ret = g_file_load_contents_finish (data->file, res, &contents, &length, NULL, &error);
478   g_assert (ret);
479   g_assert_no_error (error);
480   g_assert_cmpint (length, ==, strlen (data->data));
481   g_assert_cmpstr (contents, ==, data->data);
482
483   g_free (contents);
484
485   if (data->again)
486     {
487       data->again = FALSE;
488       data->data = "pi pa po";
489
490       g_file_replace_contents_async (data->file,
491                                      data->data,
492                                      strlen (data->data),
493                                      NULL,
494                                      FALSE,
495                                      0,
496                                      NULL,
497                                      replaced_cb,
498                                      data);
499     }
500   else
501     {
502        error = NULL;
503        ret = g_file_delete (data->file, NULL, &error);
504        g_assert_no_error (error);
505        g_assert (ret);
506        g_assert (!g_file_query_exists (data->file, NULL));
507
508        g_main_loop_quit (data->loop);
509     }
510 }
511
512 static void
513 replaced_cb (GObject      *source,
514              GAsyncResult *res,
515              gpointer      user_data)
516 {
517   ReplaceLoadData *data = user_data;
518   GError *error;
519
520   error = NULL;
521   g_file_replace_contents_finish (data->file, res, NULL, &error);
522   g_assert_no_error (error);
523
524   g_file_load_contents_async (data->file, NULL, loaded_cb, data);
525 }
526
527 static void
528 test_replace_load (void)
529 {
530   ReplaceLoadData *data;
531   gchar *path;
532   GFileIOStream *iostream;
533
534   data = g_new0 (ReplaceLoadData, 1);
535   data->again = TRUE;
536   data->data =
537     "/**\n"
538     " * g_file_replace_contents_async:\n"
539     " * @file: input #GFile.\n"
540     " * @contents: string of contents to replace the file with.\n"
541     " * @length: the length of @contents in bytes.\n"
542     " * @etag: (allow-none): a new <link linkend=\"gfile-etag\">entity tag</link> for the @file, or %NULL\n"
543     " * @make_backup: %TRUE if a backup should be created.\n"
544     " * @flags: a set of #GFileCreateFlags.\n"
545     " * @cancellable: optional #GCancellable object, %NULL to ignore.\n"
546     " * @callback: a #GAsyncReadyCallback to call when the request is satisfied\n"
547     " * @user_data: the data to pass to callback function\n"
548     " * \n"
549     " * Starts an asynchronous replacement of @file with the given \n"
550     " * @contents of @length bytes. @etag will replace the document's\n"
551     " * current entity tag.\n"
552     " * \n"
553     " * When this operation has completed, @callback will be called with\n"
554     " * @user_user data, and the operation can be finalized with \n"
555     " * g_file_replace_contents_finish().\n"
556     " * \n"
557     " * If @cancellable is not %NULL, then the operation can be cancelled by\n"
558     " * triggering the cancellable object from another thread. If the operation\n"
559     " * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. \n"
560     " * \n"
561     " * If @make_backup is %TRUE, this function will attempt to \n"
562     " * make a backup of @file.\n"
563     " **/\n";
564
565   data->file = g_file_new_tmp ("g_file_replace_load_XXXXXX",
566                                &iostream, NULL);
567   g_assert (data->file != NULL);
568   g_object_unref (iostream);
569
570   path = g_file_get_path (data->file);
571   remove (path);
572
573   g_assert (!g_file_query_exists (data->file, NULL));
574
575   data->loop = g_main_loop_new (NULL, FALSE);
576
577   g_file_replace_contents_async (data->file,
578                                  data->data,
579                                  strlen (data->data),
580                                  NULL,
581                                  FALSE,
582                                  0,
583                                  NULL,
584                                  replaced_cb,
585                                  data);
586
587   g_main_loop_run (data->loop);
588
589   g_main_loop_unref (data->loop);
590   g_object_unref (data->file);
591   g_free (data);
592   free (path);
593 }
594
595 int
596 main (int argc, char *argv[])
597 {
598   g_type_init ();
599
600   g_test_init (&argc, &argv, NULL);
601
602   g_test_add_func ("/file/basic", test_basic);
603   g_test_add_func ("/file/parent", test_parent);
604   g_test_add_func ("/file/child", test_child);
605   g_test_add_func ("/file/type", test_type);
606   g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);
607   g_test_add_data_func ("/file/async-create-delete/1", GINT_TO_POINTER (1), test_create_delete);
608   g_test_add_data_func ("/file/async-create-delete/10", GINT_TO_POINTER (10), test_create_delete);
609   g_test_add_data_func ("/file/async-create-delete/25", GINT_TO_POINTER (25), test_create_delete);
610   g_test_add_data_func ("/file/async-create-delete/4096", GINT_TO_POINTER (4096), test_create_delete);
611   g_test_add_func ("/file/replace-load", test_replace_load);
612
613   return g_test_run ();
614 }