5 #include <gio/gfiledescriptorbased.h>
11 test_basic_for_file (GFile *file,
16 s = g_file_get_basename (file);
17 g_assert_cmpstr (s, ==, "testfile");
20 s = g_file_get_uri (file);
21 g_assert (g_str_has_prefix (s, "file://"));
22 g_assert (g_str_has_suffix (s, suffix));
25 g_assert (g_file_has_uri_scheme (file, "file"));
26 s = g_file_get_uri_scheme (file);
27 g_assert_cmpstr (s, ==, "file");
36 file = g_file_new_for_path ("./some/directory/testfile");
37 test_basic_for_file (file, "/some/directory/testfile");
38 g_object_unref (file);
42 test_build_filename (void)
46 file = g_file_new_build_filename (".", "some", "directory", "testfile", NULL);
47 test_basic_for_file (file, "/some/directory/testfile");
48 g_object_unref (file);
50 file = g_file_new_build_filename ("testfile", NULL);
51 test_basic_for_file (file, "/testfile");
52 g_object_unref (file);
63 file = g_file_new_for_path ("./some/directory/testfile");
64 file2 = g_file_new_for_path ("./some/directory");
65 root = g_file_new_for_path ("/");
67 g_assert (g_file_has_parent (file, file2));
69 parent = g_file_get_parent (file);
70 g_assert (g_file_equal (parent, file2));
71 g_object_unref (parent);
73 g_assert (g_file_get_parent (root) == NULL);
75 g_object_unref (file);
76 g_object_unref (file2);
77 g_object_unref (root);
87 file = g_file_new_for_path ("./some/directory");
88 child = g_file_get_child (file, "child");
89 g_assert (g_file_has_parent (child, file));
91 child2 = g_file_get_child_for_display_name (file, "child2", NULL);
92 g_assert (g_file_has_parent (child2, file));
94 g_object_unref (child);
95 g_object_unref (child2);
96 g_object_unref (file);
105 GError *error = NULL;
107 datapath_f = g_file_new_for_path (g_test_get_dir (G_TEST_DIST));
109 file = g_file_get_child (datapath_f, "g-icon.c");
110 type = g_file_query_file_type (file, 0, NULL);
111 g_assert_cmpint (type, ==, G_FILE_TYPE_REGULAR);
112 g_object_unref (file);
114 file = g_file_get_child (datapath_f, "cert-tests");
115 type = g_file_query_file_type (file, 0, NULL);
116 g_assert_cmpint (type, ==, G_FILE_TYPE_DIRECTORY);
118 g_file_read (file, NULL, &error);
119 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_IS_DIRECTORY);
120 g_error_free (error);
121 g_object_unref (file);
123 g_object_unref (datapath_f);
127 test_parse_name (void)
132 file = g_file_new_for_uri ("file://somewhere");
133 name = g_file_get_parse_name (file);
134 g_assert_cmpstr (name, ==, "file://somewhere");
135 g_object_unref (file);
138 file = g_file_parse_name ("~foo");
139 name = g_file_get_parse_name (file);
140 g_assert (name != NULL);
141 g_object_unref (file);
148 GFileMonitor *monitor;
149 GOutputStream *ostream;
150 GInputStream *istream;
153 gint monitor_created;
154 gint monitor_deleted;
155 gint monitor_changed;
164 monitor_changed (GFileMonitor *monitor,
167 GFileMonitorEvent event_type,
170 CreateDeleteData *data = user_data;
173 path = g_file_get_path (file);
174 g_assert_cmpstr (data->monitor_path, ==, path);
177 if (event_type == G_FILE_MONITOR_EVENT_CREATED)
178 data->monitor_created++;
179 if (event_type == G_FILE_MONITOR_EVENT_DELETED)
180 data->monitor_deleted++;
181 if (event_type == G_FILE_MONITOR_EVENT_CHANGED)
182 data->monitor_changed++;
187 quit_idle (gpointer user_data)
189 CreateDeleteData *data = user_data;
191 g_source_remove (data->timeout);
192 g_main_loop_quit (data->loop);
198 iclosed_cb (GObject *source,
202 CreateDeleteData *data = user_data;
207 ret = g_input_stream_close_finish (data->istream, res, &error);
208 g_assert_no_error (error);
211 g_assert (g_input_stream_is_closed (data->istream));
213 ret = g_file_delete (data->file, NULL, &error);
215 g_assert_no_error (error);
217 /* work around file monitor bug:
218 * inotify events are only processed every 1000 ms, regardless
219 * of the rate limit set on the file monitor
221 g_timeout_add (2000, quit_idle, data);
225 read_cb (GObject *source,
229 CreateDeleteData *data = user_data;
234 size = g_input_stream_read_finish (data->istream, res, &error);
235 g_assert_no_error (error);
238 if (data->pos < strlen (data->data))
240 g_input_stream_read_async (data->istream,
241 data->buffer + data->pos,
242 strlen (data->data) - data->pos,
250 g_assert_cmpstr (data->buffer, ==, data->data);
251 g_assert (!g_input_stream_is_closed (data->istream));
252 g_input_stream_close_async (data->istream, 0, NULL, iclosed_cb, data);
257 ipending_cb (GObject *source,
261 CreateDeleteData *data = user_data;
265 g_input_stream_read_finish (data->istream, res, &error);
266 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
267 g_error_free (error);
271 skipped_cb (GObject *source,
275 CreateDeleteData *data = user_data;
280 size = g_input_stream_skip_finish (data->istream, res, &error);
281 g_assert_no_error (error);
282 g_assert_cmpint (size, ==, data->pos);
284 g_input_stream_read_async (data->istream,
285 data->buffer + data->pos,
286 strlen (data->data) - data->pos,
291 /* check that we get a pending error */
292 g_input_stream_read_async (data->istream,
293 data->buffer + data->pos,
294 strlen (data->data) - data->pos,
302 opened_cb (GObject *source,
306 GFileInputStream *base;
307 CreateDeleteData *data = user_data;
311 base = g_file_read_finish (data->file, res, &error);
312 g_assert_no_error (error);
314 if (data->buffersize == 0)
315 data->istream = G_INPUT_STREAM (g_object_ref (base));
317 data->istream = g_buffered_input_stream_new_sized (G_INPUT_STREAM (base), data->buffersize);
318 g_object_unref (base);
320 data->buffer = g_new0 (gchar, strlen (data->data) + 1);
322 /* copy initial segment directly, then skip */
323 memcpy (data->buffer, data->data, 10);
326 g_input_stream_skip_async (data->istream,
335 oclosed_cb (GObject *source,
339 CreateDeleteData *data = user_data;
344 ret = g_output_stream_close_finish (data->ostream, res, &error);
345 g_assert_no_error (error);
347 g_assert (g_output_stream_is_closed (data->ostream));
349 g_file_read_async (data->file, 0, NULL, opened_cb, data);
353 written_cb (GObject *source,
357 CreateDeleteData *data = user_data;
362 size = g_output_stream_write_finish (data->ostream, res, &error);
363 g_assert_no_error (error);
366 if (data->pos < strlen (data->data))
368 g_output_stream_write_async (data->ostream,
369 data->data + data->pos,
370 strlen (data->data) - data->pos,
378 g_assert (!g_output_stream_is_closed (data->ostream));
379 g_output_stream_close_async (data->ostream, 0, NULL, oclosed_cb, data);
384 opending_cb (GObject *source,
388 CreateDeleteData *data = user_data;
392 g_output_stream_write_finish (data->ostream, res, &error);
393 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
394 g_error_free (error);
398 created_cb (GObject *source,
402 GFileOutputStream *base;
403 CreateDeleteData *data = user_data;
407 base = g_file_create_finish (G_FILE (source), res, &error);
408 g_assert_no_error (error);
409 g_assert (g_file_query_exists (data->file, NULL));
411 if (data->buffersize == 0)
412 data->ostream = G_OUTPUT_STREAM (g_object_ref (base));
414 data->ostream = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), data->buffersize);
415 g_object_unref (base);
417 g_output_stream_write_async (data->ostream,
424 /* check that we get a pending error */
425 g_output_stream_write_async (data->ostream,
435 stop_timeout (gpointer data)
437 g_assert_not_reached ();
443 * This test does a fully async create-write-read-delete.
447 test_create_delete (gconstpointer d)
450 CreateDeleteData *data;
451 GFileIOStream *iostream;
453 data = g_new0 (CreateDeleteData, 1);
455 data->buffersize = GPOINTER_TO_INT (d);
456 data->data = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
459 data->file = g_file_new_tmp ("g_file_create_delete_XXXXXX",
461 g_assert (data->file != NULL);
462 g_object_unref (iostream);
464 data->monitor_path = g_file_get_path (data->file);
465 remove (data->monitor_path);
467 g_assert (!g_file_query_exists (data->file, NULL));
470 data->monitor = g_file_monitor_file (data->file, 0, NULL, &error);
471 g_assert_no_error (error);
473 /* This test doesn't work with GPollFileMonitor, because it assumes
474 * that the monitor will notice a create immediately followed by a
475 * delete, rather than coalescing them into nothing.
477 if (!strcmp (G_OBJECT_TYPE_NAME (data->monitor), "GPollFileMonitor"))
479 g_test_skip ("skipping test for this GFileMonitor implementation");
483 g_file_monitor_set_rate_limit (data->monitor, 100);
485 g_signal_connect (data->monitor, "changed", G_CALLBACK (monitor_changed), data);
487 data->loop = g_main_loop_new (NULL, FALSE);
489 data->timeout = g_timeout_add (10000, stop_timeout, NULL);
491 g_file_create_async (data->file, 0, 0, NULL, created_cb, data);
493 g_main_loop_run (data->loop);
495 g_assert_cmpint (data->monitor_created, ==, 1);
496 g_assert_cmpint (data->monitor_deleted, ==, 1);
497 g_assert_cmpint (data->monitor_changed, >, 0);
499 g_assert (!g_file_monitor_is_cancelled (data->monitor));
500 g_file_monitor_cancel (data->monitor);
501 g_assert (g_file_monitor_is_cancelled (data->monitor));
503 g_main_loop_unref (data->loop);
504 g_object_unref (data->ostream);
505 g_object_unref (data->istream);
508 g_object_unref (data->monitor);
509 g_object_unref (data->file);
510 free (data->monitor_path);
511 g_free (data->buffer);
515 static const gchar *replace_data =
517 " * g_file_replace_contents_async:\n"
518 " * @file: input #GFile.\n"
519 " * @contents: string of contents to replace the file with.\n"
520 " * @length: the length of @contents in bytes.\n"
521 " * @etag: (nullable): a new <link linkend=\"gfile-etag\">entity tag</link> for the @file, or %NULL\n"
522 " * @make_backup: %TRUE if a backup should be created.\n"
523 " * @flags: a set of #GFileCreateFlags.\n"
524 " * @cancellable: optional #GCancellable object, %NULL to ignore.\n"
525 " * @callback: a #GAsyncReadyCallback to call when the request is satisfied\n"
526 " * @user_data: the data to pass to callback function\n"
528 " * Starts an asynchronous replacement of @file with the given \n"
529 " * @contents of @length bytes. @etag will replace the document's\n"
530 " * current entity tag.\n"
532 " * When this operation has completed, @callback will be called with\n"
533 " * @user_user data, and the operation can be finalized with \n"
534 " * g_file_replace_contents_finish().\n"
536 " * If @cancellable is not %NULL, then the operation can be cancelled by\n"
537 " * triggering the cancellable object from another thread. If the operation\n"
538 " * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. \n"
540 " * If @make_backup is %TRUE, this function will attempt to \n"
541 " * make a backup of @file.\n"
552 static void replaced_cb (GObject *source,
557 loaded_cb (GObject *source,
561 ReplaceLoadData *data = user_data;
568 ret = g_file_load_contents_finish (data->file, res, &contents, &length, NULL, &error);
570 g_assert_no_error (error);
571 g_assert_cmpint (length, ==, strlen (data->data));
572 g_assert_cmpstr (contents, ==, data->data);
579 data->data = "pi pa po";
581 g_file_replace_contents_async (data->file,
594 ret = g_file_delete (data->file, NULL, &error);
595 g_assert_no_error (error);
597 g_assert (!g_file_query_exists (data->file, NULL));
599 g_main_loop_quit (data->loop);
604 replaced_cb (GObject *source,
608 ReplaceLoadData *data = user_data;
612 g_file_replace_contents_finish (data->file, res, NULL, &error);
613 g_assert_no_error (error);
615 g_file_load_contents_async (data->file, NULL, loaded_cb, data);
619 test_replace_load (void)
621 ReplaceLoadData *data;
623 GFileIOStream *iostream;
625 data = g_new0 (ReplaceLoadData, 1);
627 data->data = replace_data;
629 data->file = g_file_new_tmp ("g_file_replace_load_XXXXXX",
631 g_assert (data->file != NULL);
632 g_object_unref (iostream);
634 path = g_file_get_path (data->file);
637 g_assert (!g_file_query_exists (data->file, NULL));
639 data->loop = g_main_loop_new (NULL, FALSE);
641 g_file_replace_contents_async (data->file,
651 g_main_loop_run (data->loop);
653 g_main_loop_unref (data->loop);
654 g_object_unref (data->file);
660 test_replace_cancel (void)
662 GFile *tmpdir, *file;
663 GFileOutputStream *ostream;
664 GFileEnumerator *fenum;
666 GCancellable *cancellable;
670 GError *error = NULL;
672 g_test_bug ("629301");
674 path = g_dir_make_tmp ("g_file_replace_cancel_XXXXXX", &error);
675 g_assert_no_error (error);
676 tmpdir = g_file_new_for_path (path);
679 file = g_file_get_child (tmpdir, "file");
680 g_file_replace_contents (file,
682 strlen (replace_data),
683 NULL, FALSE, 0, NULL,
685 g_assert_no_error (error);
687 ostream = g_file_replace (file, NULL, TRUE, 0, NULL, &error);
688 g_assert_no_error (error);
690 g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
691 replace_data, strlen (replace_data),
692 &nwrote, NULL, &error);
693 g_assert_no_error (error);
694 g_assert_cmpint (nwrote, ==, strlen (replace_data));
696 /* At this point there should be two files; the original and the
699 fenum = g_file_enumerate_children (tmpdir, NULL, 0, NULL, &error);
700 g_assert_no_error (error);
702 info = g_file_enumerator_next_file (fenum, NULL, &error);
703 g_assert_no_error (error);
704 g_assert (info != NULL);
705 g_object_unref (info);
706 info = g_file_enumerator_next_file (fenum, NULL, &error);
707 g_assert_no_error (error);
708 g_assert (info != NULL);
709 g_object_unref (info);
711 g_file_enumerator_close (fenum, NULL, &error);
712 g_assert_no_error (error);
713 g_object_unref (fenum);
715 /* Also test the g_file_enumerator_iterate() API */
716 fenum = g_file_enumerate_children (tmpdir, NULL, 0, NULL, &error);
717 g_assert_no_error (error);
722 gboolean ret = g_file_enumerator_iterate (fenum, &info, NULL, NULL, &error);
724 g_assert_no_error (error);
729 g_assert_cmpint (count, ==, 2);
731 g_file_enumerator_close (fenum, NULL, &error);
732 g_assert_no_error (error);
733 g_object_unref (fenum);
735 /* Now test just getting child from the g_file_enumerator_iterate() API */
736 fenum = g_file_enumerate_children (tmpdir, "standard::name", 0, NULL, &error);
737 g_assert_no_error (error);
743 gboolean ret = g_file_enumerator_iterate (fenum, NULL, &child, NULL, &error);
746 g_assert_no_error (error);
751 g_assert (G_IS_FILE (child));
754 g_assert_cmpint (count, ==, 2);
756 g_file_enumerator_close (fenum, NULL, &error);
757 g_assert_no_error (error);
758 g_object_unref (fenum);
760 /* Make sure the temporary gets deleted even if we cancel. */
761 cancellable = g_cancellable_new ();
762 g_cancellable_cancel (cancellable);
763 g_output_stream_close (G_OUTPUT_STREAM (ostream), cancellable, &error);
764 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
765 g_clear_error (&error);
767 g_object_unref (cancellable);
768 g_object_unref (ostream);
770 g_file_delete (file, NULL, &error);
771 g_assert_no_error (error);
772 g_object_unref (file);
774 /* This will only succeed if the temp file was deleted. */
775 g_file_delete (tmpdir, NULL, &error);
776 g_assert_no_error (error);
777 g_object_unref (tmpdir);
781 on_file_deleted (GObject *object,
782 GAsyncResult *result,
785 GError *local_error = NULL;
786 GMainLoop *loop = user_data;
788 (void) g_file_delete_finish ((GFile*)object, result, &local_error);
789 g_assert_no_error (local_error);
791 g_main_loop_quit (loop);
795 test_async_delete (void)
798 GFileIOStream *iostream;
799 GError *local_error = NULL;
800 GError **error = &local_error;
803 file = g_file_new_tmp ("g_file_delete_XXXXXX",
805 g_assert_no_error (local_error);
806 g_object_unref (iostream);
808 g_assert (g_file_query_exists (file, NULL));
810 loop = g_main_loop_new (NULL, TRUE);
812 g_file_delete_async (file, G_PRIORITY_DEFAULT, NULL, on_file_deleted, loop);
814 g_main_loop_run (loop);
816 g_assert (!g_file_query_exists (file, NULL));
818 g_main_loop_unref (loop);
819 g_object_unref (file);
824 test_copy_preserve_mode (void)
828 GFileInfo *dest_info;
829 GFileIOStream *iostream;
830 GError *local_error = NULL;
831 GError **error = &local_error;
832 guint32 romode = S_IFREG | 0600;
835 tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
837 g_assert_no_error (local_error);
838 g_io_stream_close ((GIOStream*)iostream, NULL, error);
839 g_assert_no_error (local_error);
840 g_clear_object (&iostream);
842 g_file_set_attribute (tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_ATTRIBUTE_TYPE_UINT32,
843 &romode, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
845 g_assert_no_error (local_error);
847 dest_tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
849 g_assert_no_error (local_error);
850 g_io_stream_close ((GIOStream*)iostream, NULL, error);
851 g_assert_no_error (local_error);
852 g_clear_object (&iostream);
854 g_file_copy (tmpfile, dest_tmpfile, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA,
855 NULL, NULL, NULL, error);
856 g_assert_no_error (local_error);
858 dest_info = g_file_query_info (dest_tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
860 g_assert_no_error (local_error);
862 dest_mode = g_file_info_get_attribute_uint32 (dest_info, G_FILE_ATTRIBUTE_UNIX_MODE);
864 g_assert_cmpint (dest_mode, ==, romode);
866 (void) g_file_delete (tmpfile, NULL, NULL);
867 (void) g_file_delete (dest_tmpfile, NULL, NULL);
869 g_clear_object (&tmpfile);
870 g_clear_object (&dest_tmpfile);
871 g_clear_object (&dest_info);
876 splice_to_string (GInputStream *stream,
879 GMemoryOutputStream *buffer = NULL;
882 buffer = (GMemoryOutputStream*)g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
883 if (g_output_stream_splice ((GOutputStream*)buffer, stream, 0, NULL, error) < 0)
886 if (!g_output_stream_write ((GOutputStream*)buffer, "\0", 1, NULL, error))
889 if (!g_output_stream_close ((GOutputStream*)buffer, NULL, error))
892 ret = g_memory_output_stream_steal_data (buffer);
894 g_clear_object (&buffer);
899 get_size_from_du (const gchar *path)
905 GError *error = NULL;
907 du = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE,
909 "du", "--bytes", "-s", path, NULL);
910 g_assert_no_error (error);
912 result = splice_to_string (g_subprocess_get_stdout_pipe (du), &error);
913 g_assert_no_error (error);
915 size = g_ascii_strtoll (result, &endptr, 10);
931 GError *error = NULL;
935 path = g_test_build_filename (G_TEST_DIST, "desktop-files", NULL);
936 file = g_file_new_for_path (path);
938 if (g_find_program_in_path ("du"))
940 size = get_size_from_du (path);
944 g_test_message ("du not found, skipping byte measurement");
948 ok = g_file_measure_disk_usage (file,
949 G_FILE_MEASURE_APPARENT_SIZE,
958 g_assert_no_error (error);
961 g_assert_cmpuint (num_bytes, ==, size);
962 g_assert_cmpuint (num_dirs, ==, 6);
963 g_assert_cmpuint (num_files, ==, 30);
965 g_object_unref (file);
970 guint64 expected_bytes;
971 guint64 expected_dirs;
972 guint64 expected_files;
974 guint64 progress_bytes;
975 guint64 progress_dirs;
976 guint64 progress_files;
980 measure_progress (gboolean reporting,
981 guint64 current_size,
986 MeasureData *data = user_data;
988 data->progress_count += 1;
990 g_assert_cmpuint (current_size, >=, data->progress_bytes);
991 g_assert_cmpuint (num_dirs, >=, data->progress_dirs);
992 g_assert_cmpuint (num_files, >=, data->progress_files);
994 data->progress_bytes = current_size;
995 data->progress_dirs = num_dirs;
996 data->progress_files = num_files;
1000 measure_done (GObject *source,
1004 MeasureData *data = user_data;
1005 guint64 num_bytes, num_dirs, num_files;
1006 GError *error = NULL;
1009 ok = g_file_measure_disk_usage_finish (G_FILE (source), res, &num_bytes, &num_dirs, &num_files, &error);
1011 g_assert_no_error (error);
1013 if (data->expected_bytes > 0)
1014 g_assert_cmpuint (data->expected_bytes, ==, num_bytes);
1015 g_assert_cmpuint (data->expected_dirs, ==, num_dirs);
1016 g_assert_cmpuint (data->expected_files, ==, num_files);
1018 g_assert_cmpuint (data->progress_count, >, 0);
1019 g_assert_cmpuint (num_bytes, >=, data->progress_bytes);
1020 g_assert_cmpuint (num_dirs, >=, data->progress_dirs);
1021 g_assert_cmpuint (num_files, >=, data->progress_files);
1024 g_object_unref (source);
1028 test_measure_async (void)
1034 data = g_new (MeasureData, 1);
1036 data->progress_count = 0;
1037 data->progress_bytes = 0;
1038 data->progress_files = 0;
1039 data->progress_dirs = 0;
1041 path = g_test_build_filename (G_TEST_DIST, "desktop-files", NULL);
1042 file = g_file_new_for_path (path);
1044 if (g_find_program_in_path ("du"))
1046 data->expected_bytes = get_size_from_du (path);
1050 g_test_message ("du not found, skipping byte measurement");
1051 data->expected_bytes = 0;
1056 data->expected_dirs = 6;
1057 data->expected_files = 30;
1059 g_file_measure_disk_usage_async (file,
1060 G_FILE_MEASURE_APPARENT_SIZE,
1062 measure_progress, data,
1063 measure_done, data);
1067 test_load_bytes (void)
1069 gchar filename[] = "g_file_load_bytes_XXXXXX";
1070 GError *error = NULL;
1077 fd = g_mkstemp (filename);
1078 g_assert_cmpint (fd, !=, -1);
1079 len = strlen ("test_load_bytes");
1080 ret = write (fd, "test_load_bytes", len);
1081 g_assert_cmpint (ret, ==, len);
1084 file = g_file_new_for_path (filename);
1085 bytes = g_file_load_bytes (file, NULL, NULL, &error);
1086 g_assert_no_error (error);
1087 g_assert (bytes != NULL);
1088 g_assert_cmpint (len, ==, g_bytes_get_size (bytes));
1089 g_assert_cmpstr ("test_load_bytes", ==, (gchar *)g_bytes_get_data (bytes, NULL));
1091 g_file_delete (file, NULL, NULL);
1093 g_bytes_unref (bytes);
1094 g_object_unref (file);
1099 GMainLoop *main_loop;
1102 } LoadBytesAsyncData;
1105 test_load_bytes_cb (GObject *object,
1106 GAsyncResult *result,
1109 GFile *file = G_FILE (object);
1110 LoadBytesAsyncData *data = user_data;
1111 GError *error = NULL;
1113 data->bytes = g_file_load_bytes_finish (file, result, NULL, &error);
1114 g_assert_no_error (error);
1115 g_assert (data->bytes != NULL);
1117 g_main_loop_quit (data->main_loop);
1121 test_load_bytes_async (void)
1123 LoadBytesAsyncData data = { 0 };
1124 gchar filename[] = "g_file_load_bytes_XXXXXX";
1129 fd = g_mkstemp (filename);
1130 g_assert_cmpint (fd, !=, -1);
1131 len = strlen ("test_load_bytes_async");
1132 ret = write (fd, "test_load_bytes_async", len);
1133 g_assert_cmpint (ret, ==, len);
1136 data.main_loop = g_main_loop_new (NULL, FALSE);
1137 data.file = g_file_new_for_path (filename);
1139 g_file_load_bytes_async (data.file, NULL, test_load_bytes_cb, &data);
1140 g_main_loop_run (data.main_loop);
1142 g_assert_cmpint (len, ==, g_bytes_get_size (data.bytes));
1143 g_assert_cmpstr ("test_load_bytes_async", ==, (gchar *)g_bytes_get_data (data.bytes, NULL));
1145 g_file_delete (data.file, NULL, NULL);
1146 g_object_unref (data.file);
1147 g_bytes_unref (data.bytes);
1148 g_main_loop_unref (data.main_loop);
1152 main (int argc, char *argv[])
1154 g_test_init (&argc, &argv, NULL);
1156 g_test_bug_base ("http://bugzilla.gnome.org/");
1158 g_test_add_func ("/file/basic", test_basic);
1159 g_test_add_func ("/file/build-filename", test_build_filename);
1160 g_test_add_func ("/file/parent", test_parent);
1161 g_test_add_func ("/file/child", test_child);
1162 g_test_add_func ("/file/type", test_type);
1163 g_test_add_func ("/file/parse-name", test_parse_name);
1164 g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);
1165 g_test_add_data_func ("/file/async-create-delete/1", GINT_TO_POINTER (1), test_create_delete);
1166 g_test_add_data_func ("/file/async-create-delete/10", GINT_TO_POINTER (10), test_create_delete);
1167 g_test_add_data_func ("/file/async-create-delete/25", GINT_TO_POINTER (25), test_create_delete);
1168 g_test_add_data_func ("/file/async-create-delete/4096", GINT_TO_POINTER (4096), test_create_delete);
1169 g_test_add_func ("/file/replace-load", test_replace_load);
1170 g_test_add_func ("/file/replace-cancel", test_replace_cancel);
1171 g_test_add_func ("/file/async-delete", test_async_delete);
1173 g_test_add_func ("/file/copy-preserve-mode", test_copy_preserve_mode);
1175 g_test_add_func ("/file/measure", test_measure);
1176 g_test_add_func ("/file/measure-async", test_measure_async);
1177 g_test_add_func ("/file/load-bytes", test_load_bytes);
1178 g_test_add_func ("/file/load-bytes-async", test_load_bytes_async);
1180 return g_test_run ();