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