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;
172 const gchar *peeked_path;
174 path = g_file_get_path (file);
175 peeked_path = g_file_peek_path (file);
176 g_assert_cmpstr (data->monitor_path, ==, path);
177 g_assert_cmpstr (path, ==, peeked_path);
180 if (event_type == G_FILE_MONITOR_EVENT_CREATED)
181 data->monitor_created++;
182 if (event_type == G_FILE_MONITOR_EVENT_DELETED)
183 data->monitor_deleted++;
184 if (event_type == G_FILE_MONITOR_EVENT_CHANGED)
185 data->monitor_changed++;
190 quit_idle (gpointer user_data)
192 CreateDeleteData *data = user_data;
194 g_source_remove (data->timeout);
195 g_main_loop_quit (data->loop);
201 iclosed_cb (GObject *source,
205 CreateDeleteData *data = user_data;
210 ret = g_input_stream_close_finish (data->istream, res, &error);
211 g_assert_no_error (error);
214 g_assert (g_input_stream_is_closed (data->istream));
216 ret = g_file_delete (data->file, NULL, &error);
218 g_assert_no_error (error);
220 /* work around file monitor bug:
221 * inotify events are only processed every 1000 ms, regardless
222 * of the rate limit set on the file monitor
224 g_timeout_add (2000, quit_idle, data);
228 read_cb (GObject *source,
232 CreateDeleteData *data = user_data;
237 size = g_input_stream_read_finish (data->istream, res, &error);
238 g_assert_no_error (error);
241 if (data->pos < strlen (data->data))
243 g_input_stream_read_async (data->istream,
244 data->buffer + data->pos,
245 strlen (data->data) - data->pos,
253 g_assert_cmpstr (data->buffer, ==, data->data);
254 g_assert (!g_input_stream_is_closed (data->istream));
255 g_input_stream_close_async (data->istream, 0, NULL, iclosed_cb, data);
260 ipending_cb (GObject *source,
264 CreateDeleteData *data = user_data;
268 g_input_stream_read_finish (data->istream, res, &error);
269 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
270 g_error_free (error);
274 skipped_cb (GObject *source,
278 CreateDeleteData *data = user_data;
283 size = g_input_stream_skip_finish (data->istream, res, &error);
284 g_assert_no_error (error);
285 g_assert_cmpint (size, ==, data->pos);
287 g_input_stream_read_async (data->istream,
288 data->buffer + data->pos,
289 strlen (data->data) - data->pos,
294 /* check that we get a pending error */
295 g_input_stream_read_async (data->istream,
296 data->buffer + data->pos,
297 strlen (data->data) - data->pos,
305 opened_cb (GObject *source,
309 GFileInputStream *base;
310 CreateDeleteData *data = user_data;
314 base = g_file_read_finish (data->file, res, &error);
315 g_assert_no_error (error);
317 if (data->buffersize == 0)
318 data->istream = G_INPUT_STREAM (g_object_ref (base));
320 data->istream = g_buffered_input_stream_new_sized (G_INPUT_STREAM (base), data->buffersize);
321 g_object_unref (base);
323 data->buffer = g_new0 (gchar, strlen (data->data) + 1);
325 /* copy initial segment directly, then skip */
326 memcpy (data->buffer, data->data, 10);
329 g_input_stream_skip_async (data->istream,
338 oclosed_cb (GObject *source,
342 CreateDeleteData *data = user_data;
347 ret = g_output_stream_close_finish (data->ostream, res, &error);
348 g_assert_no_error (error);
350 g_assert (g_output_stream_is_closed (data->ostream));
352 g_file_read_async (data->file, 0, NULL, opened_cb, data);
356 written_cb (GObject *source,
360 CreateDeleteData *data = user_data;
365 size = g_output_stream_write_finish (data->ostream, res, &error);
366 g_assert_no_error (error);
369 if (data->pos < strlen (data->data))
371 g_output_stream_write_async (data->ostream,
372 data->data + data->pos,
373 strlen (data->data) - data->pos,
381 g_assert (!g_output_stream_is_closed (data->ostream));
382 g_output_stream_close_async (data->ostream, 0, NULL, oclosed_cb, data);
387 opending_cb (GObject *source,
391 CreateDeleteData *data = user_data;
395 g_output_stream_write_finish (data->ostream, res, &error);
396 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
397 g_error_free (error);
401 created_cb (GObject *source,
405 GFileOutputStream *base;
406 CreateDeleteData *data = user_data;
410 base = g_file_create_finish (G_FILE (source), res, &error);
411 g_assert_no_error (error);
412 g_assert (g_file_query_exists (data->file, NULL));
414 if (data->buffersize == 0)
415 data->ostream = G_OUTPUT_STREAM (g_object_ref (base));
417 data->ostream = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), data->buffersize);
418 g_object_unref (base);
420 g_output_stream_write_async (data->ostream,
427 /* check that we get a pending error */
428 g_output_stream_write_async (data->ostream,
438 stop_timeout (gpointer data)
440 g_assert_not_reached ();
446 * This test does a fully async create-write-read-delete.
450 test_create_delete (gconstpointer d)
453 CreateDeleteData *data;
454 GFileIOStream *iostream;
456 data = g_new0 (CreateDeleteData, 1);
458 data->buffersize = GPOINTER_TO_INT (d);
459 data->data = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ0123456789";
462 data->file = g_file_new_tmp ("g_file_create_delete_XXXXXX",
464 g_assert (data->file != NULL);
465 g_object_unref (iostream);
467 data->monitor_path = g_file_get_path (data->file);
468 remove (data->monitor_path);
470 g_assert (!g_file_query_exists (data->file, NULL));
473 data->monitor = g_file_monitor_file (data->file, 0, NULL, &error);
474 g_assert_no_error (error);
476 /* This test doesn't work with GPollFileMonitor, because it assumes
477 * that the monitor will notice a create immediately followed by a
478 * delete, rather than coalescing them into nothing.
480 if (!strcmp (G_OBJECT_TYPE_NAME (data->monitor), "GPollFileMonitor"))
482 g_test_skip ("skipping test for this GFileMonitor implementation");
486 g_file_monitor_set_rate_limit (data->monitor, 100);
488 g_signal_connect (data->monitor, "changed", G_CALLBACK (monitor_changed), data);
490 data->loop = g_main_loop_new (NULL, FALSE);
492 data->timeout = g_timeout_add (10000, stop_timeout, NULL);
494 g_file_create_async (data->file, 0, 0, NULL, created_cb, data);
496 g_main_loop_run (data->loop);
498 g_assert_cmpint (data->monitor_created, ==, 1);
499 g_assert_cmpint (data->monitor_deleted, ==, 1);
500 g_assert_cmpint (data->monitor_changed, >, 0);
502 g_assert (!g_file_monitor_is_cancelled (data->monitor));
503 g_file_monitor_cancel (data->monitor);
504 g_assert (g_file_monitor_is_cancelled (data->monitor));
506 g_main_loop_unref (data->loop);
507 g_object_unref (data->ostream);
508 g_object_unref (data->istream);
511 g_object_unref (data->monitor);
512 g_object_unref (data->file);
513 g_free (data->monitor_path);
514 g_free (data->buffer);
518 static const gchar *replace_data =
520 " * g_file_replace_contents_async:\n"
521 " * @file: input #GFile.\n"
522 " * @contents: string of contents to replace the file with.\n"
523 " * @length: the length of @contents in bytes.\n"
524 " * @etag: (nullable): a new <link linkend=\"gfile-etag\">entity tag</link> for the @file, or %NULL\n"
525 " * @make_backup: %TRUE if a backup should be created.\n"
526 " * @flags: a set of #GFileCreateFlags.\n"
527 " * @cancellable: optional #GCancellable object, %NULL to ignore.\n"
528 " * @callback: a #GAsyncReadyCallback to call when the request is satisfied\n"
529 " * @user_data: the data to pass to callback function\n"
531 " * Starts an asynchronous replacement of @file with the given \n"
532 " * @contents of @length bytes. @etag will replace the document's\n"
533 " * current entity tag.\n"
535 " * When this operation has completed, @callback will be called with\n"
536 " * @user_user data, and the operation can be finalized with \n"
537 " * g_file_replace_contents_finish().\n"
539 " * If @cancellable is not %NULL, then the operation can be cancelled by\n"
540 " * triggering the cancellable object from another thread. If the operation\n"
541 " * was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. \n"
543 " * If @make_backup is %TRUE, this function will attempt to \n"
544 " * make a backup of @file.\n"
555 static void replaced_cb (GObject *source,
560 loaded_cb (GObject *source,
564 ReplaceLoadData *data = user_data;
571 ret = g_file_load_contents_finish (data->file, res, &contents, &length, NULL, &error);
573 g_assert_no_error (error);
574 g_assert_cmpint (length, ==, strlen (data->data));
575 g_assert_cmpstr (contents, ==, data->data);
582 data->data = "pi pa po";
584 g_file_replace_contents_async (data->file,
597 ret = g_file_delete (data->file, NULL, &error);
598 g_assert_no_error (error);
600 g_assert (!g_file_query_exists (data->file, NULL));
602 g_main_loop_quit (data->loop);
607 replaced_cb (GObject *source,
611 ReplaceLoadData *data = user_data;
615 g_file_replace_contents_finish (data->file, res, NULL, &error);
616 g_assert_no_error (error);
618 g_file_load_contents_async (data->file, NULL, loaded_cb, data);
622 test_replace_load (void)
624 ReplaceLoadData *data;
626 GFileIOStream *iostream;
628 data = g_new0 (ReplaceLoadData, 1);
630 data->data = replace_data;
632 data->file = g_file_new_tmp ("g_file_replace_load_XXXXXX",
634 g_assert (data->file != NULL);
635 g_object_unref (iostream);
637 path = g_file_peek_path (data->file);
640 g_assert (!g_file_query_exists (data->file, NULL));
642 data->loop = g_main_loop_new (NULL, FALSE);
644 g_file_replace_contents_async (data->file,
654 g_main_loop_run (data->loop);
656 g_main_loop_unref (data->loop);
657 g_object_unref (data->file);
662 test_replace_cancel (void)
664 GFile *tmpdir, *file;
665 GFileOutputStream *ostream;
666 GFileEnumerator *fenum;
668 GCancellable *cancellable;
672 GError *error = NULL;
674 g_test_bug ("629301");
676 path = g_dir_make_tmp ("g_file_replace_cancel_XXXXXX", &error);
677 g_assert_no_error (error);
678 tmpdir = g_file_new_for_path (path);
681 file = g_file_get_child (tmpdir, "file");
682 g_file_replace_contents (file,
684 strlen (replace_data),
685 NULL, FALSE, 0, NULL,
687 g_assert_no_error (error);
689 ostream = g_file_replace (file, NULL, TRUE, 0, NULL, &error);
690 g_assert_no_error (error);
692 g_output_stream_write_all (G_OUTPUT_STREAM (ostream),
693 replace_data, strlen (replace_data),
694 &nwrote, NULL, &error);
695 g_assert_no_error (error);
696 g_assert_cmpint (nwrote, ==, strlen (replace_data));
698 /* At this point there should be two files; the original and the
701 fenum = g_file_enumerate_children (tmpdir, NULL, 0, NULL, &error);
702 g_assert_no_error (error);
704 info = g_file_enumerator_next_file (fenum, NULL, &error);
705 g_assert_no_error (error);
706 g_assert (info != NULL);
707 g_object_unref (info);
708 info = g_file_enumerator_next_file (fenum, NULL, &error);
709 g_assert_no_error (error);
710 g_assert (info != NULL);
711 g_object_unref (info);
713 g_file_enumerator_close (fenum, NULL, &error);
714 g_assert_no_error (error);
715 g_object_unref (fenum);
717 /* Also test the g_file_enumerator_iterate() API */
718 fenum = g_file_enumerate_children (tmpdir, NULL, 0, NULL, &error);
719 g_assert_no_error (error);
724 gboolean ret = g_file_enumerator_iterate (fenum, &info, NULL, NULL, &error);
726 g_assert_no_error (error);
731 g_assert_cmpint (count, ==, 2);
733 g_file_enumerator_close (fenum, NULL, &error);
734 g_assert_no_error (error);
735 g_object_unref (fenum);
737 /* Now test just getting child from the g_file_enumerator_iterate() API */
738 fenum = g_file_enumerate_children (tmpdir, "standard::name", 0, NULL, &error);
739 g_assert_no_error (error);
745 gboolean ret = g_file_enumerator_iterate (fenum, NULL, &child, NULL, &error);
748 g_assert_no_error (error);
753 g_assert (G_IS_FILE (child));
756 g_assert_cmpint (count, ==, 2);
758 g_file_enumerator_close (fenum, NULL, &error);
759 g_assert_no_error (error);
760 g_object_unref (fenum);
762 /* Make sure the temporary gets deleted even if we cancel. */
763 cancellable = g_cancellable_new ();
764 g_cancellable_cancel (cancellable);
765 g_output_stream_close (G_OUTPUT_STREAM (ostream), cancellable, &error);
766 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
767 g_clear_error (&error);
769 g_object_unref (cancellable);
770 g_object_unref (ostream);
772 g_file_delete (file, NULL, &error);
773 g_assert_no_error (error);
774 g_object_unref (file);
776 /* This will only succeed if the temp file was deleted. */
777 g_file_delete (tmpdir, NULL, &error);
778 g_assert_no_error (error);
779 g_object_unref (tmpdir);
783 on_file_deleted (GObject *object,
784 GAsyncResult *result,
787 GError *local_error = NULL;
788 GMainLoop *loop = user_data;
790 (void) g_file_delete_finish ((GFile*)object, result, &local_error);
791 g_assert_no_error (local_error);
793 g_main_loop_quit (loop);
797 test_async_delete (void)
800 GFileIOStream *iostream;
801 GError *local_error = NULL;
802 GError **error = &local_error;
805 file = g_file_new_tmp ("g_file_delete_XXXXXX",
807 g_assert_no_error (local_error);
808 g_object_unref (iostream);
810 g_assert (g_file_query_exists (file, NULL));
812 loop = g_main_loop_new (NULL, TRUE);
814 g_file_delete_async (file, G_PRIORITY_DEFAULT, NULL, on_file_deleted, loop);
816 g_main_loop_run (loop);
818 g_assert (!g_file_query_exists (file, NULL));
820 g_main_loop_unref (loop);
821 g_object_unref (file);
826 test_copy_preserve_mode (void)
830 GFileInfo *dest_info;
831 GFileIOStream *iostream;
832 GError *local_error = NULL;
833 GError **error = &local_error;
834 guint32 romode = S_IFREG | 0600;
837 tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
839 g_assert_no_error (local_error);
840 g_io_stream_close ((GIOStream*)iostream, NULL, error);
841 g_assert_no_error (local_error);
842 g_clear_object (&iostream);
844 g_file_set_attribute (tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_ATTRIBUTE_TYPE_UINT32,
845 &romode, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
847 g_assert_no_error (local_error);
849 dest_tmpfile = g_file_new_tmp ("tmp-copy-preserve-modeXXXXXX",
851 g_assert_no_error (local_error);
852 g_io_stream_close ((GIOStream*)iostream, NULL, error);
853 g_assert_no_error (local_error);
854 g_clear_object (&iostream);
856 g_file_copy (tmpfile, dest_tmpfile, G_FILE_COPY_OVERWRITE | G_FILE_COPY_NOFOLLOW_SYMLINKS | G_FILE_COPY_ALL_METADATA,
857 NULL, NULL, NULL, error);
858 g_assert_no_error (local_error);
860 dest_info = g_file_query_info (dest_tmpfile, G_FILE_ATTRIBUTE_UNIX_MODE, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
862 g_assert_no_error (local_error);
864 dest_mode = g_file_info_get_attribute_uint32 (dest_info, G_FILE_ATTRIBUTE_UNIX_MODE);
866 g_assert_cmpint (dest_mode, ==, romode);
868 (void) g_file_delete (tmpfile, NULL, NULL);
869 (void) g_file_delete (dest_tmpfile, NULL, NULL);
871 g_clear_object (&tmpfile);
872 g_clear_object (&dest_tmpfile);
873 g_clear_object (&dest_info);
878 splice_to_string (GInputStream *stream,
881 GMemoryOutputStream *buffer = NULL;
884 buffer = (GMemoryOutputStream*)g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
885 if (g_output_stream_splice ((GOutputStream*)buffer, stream, 0, NULL, error) < 0)
888 if (!g_output_stream_write ((GOutputStream*)buffer, "\0", 1, NULL, error))
891 if (!g_output_stream_close ((GOutputStream*)buffer, NULL, error))
894 ret = g_memory_output_stream_steal_data (buffer);
896 g_clear_object (&buffer);
901 get_size_from_du (const gchar *path)
907 GError *error = NULL;
909 du = g_subprocess_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE,
911 "du", "--bytes", "-s", path, NULL);
912 g_assert_no_error (error);
914 result = splice_to_string (g_subprocess_get_stdout_pipe (du), &error);
915 g_assert_no_error (error);
917 size = g_ascii_strtoll (result, &endptr, 10);
933 GError *error = NULL;
937 path = g_test_build_filename (G_TEST_DIST, "desktop-files", NULL);
938 file = g_file_new_for_path (path);
940 if (g_find_program_in_path ("du"))
942 size = get_size_from_du (path);
946 g_test_message ("du not found, skipping byte measurement");
950 ok = g_file_measure_disk_usage (file,
951 G_FILE_MEASURE_APPARENT_SIZE,
960 g_assert_no_error (error);
963 g_assert_cmpuint (num_bytes, ==, size);
964 g_assert_cmpuint (num_dirs, ==, 6);
965 g_assert_cmpuint (num_files, ==, 31);
967 g_object_unref (file);
972 guint64 expected_bytes;
973 guint64 expected_dirs;
974 guint64 expected_files;
976 guint64 progress_bytes;
977 guint64 progress_dirs;
978 guint64 progress_files;
982 measure_progress (gboolean reporting,
983 guint64 current_size,
988 MeasureData *data = user_data;
990 data->progress_count += 1;
992 g_assert_cmpuint (current_size, >=, data->progress_bytes);
993 g_assert_cmpuint (num_dirs, >=, data->progress_dirs);
994 g_assert_cmpuint (num_files, >=, data->progress_files);
996 data->progress_bytes = current_size;
997 data->progress_dirs = num_dirs;
998 data->progress_files = num_files;
1002 measure_done (GObject *source,
1006 MeasureData *data = user_data;
1007 guint64 num_bytes, num_dirs, num_files;
1008 GError *error = NULL;
1011 ok = g_file_measure_disk_usage_finish (G_FILE (source), res, &num_bytes, &num_dirs, &num_files, &error);
1013 g_assert_no_error (error);
1015 if (data->expected_bytes > 0)
1016 g_assert_cmpuint (data->expected_bytes, ==, num_bytes);
1017 g_assert_cmpuint (data->expected_dirs, ==, num_dirs);
1018 g_assert_cmpuint (data->expected_files, ==, num_files);
1020 g_assert_cmpuint (data->progress_count, >, 0);
1021 g_assert_cmpuint (num_bytes, >=, data->progress_bytes);
1022 g_assert_cmpuint (num_dirs, >=, data->progress_dirs);
1023 g_assert_cmpuint (num_files, >=, data->progress_files);
1026 g_object_unref (source);
1030 test_measure_async (void)
1036 data = g_new (MeasureData, 1);
1038 data->progress_count = 0;
1039 data->progress_bytes = 0;
1040 data->progress_files = 0;
1041 data->progress_dirs = 0;
1043 path = g_test_build_filename (G_TEST_DIST, "desktop-files", NULL);
1044 file = g_file_new_for_path (path);
1046 if (g_find_program_in_path ("du"))
1048 data->expected_bytes = get_size_from_du (path);
1052 g_test_message ("du not found, skipping byte measurement");
1053 data->expected_bytes = 0;
1058 data->expected_dirs = 6;
1059 data->expected_files = 31;
1061 g_file_measure_disk_usage_async (file,
1062 G_FILE_MEASURE_APPARENT_SIZE,
1064 measure_progress, data,
1065 measure_done, data);
1069 test_load_bytes (void)
1071 gchar filename[] = "g_file_load_bytes_XXXXXX";
1072 GError *error = NULL;
1079 fd = g_mkstemp (filename);
1080 g_assert_cmpint (fd, !=, -1);
1081 len = strlen ("test_load_bytes");
1082 ret = write (fd, "test_load_bytes", len);
1083 g_assert_cmpint (ret, ==, len);
1086 file = g_file_new_for_path (filename);
1087 bytes = g_file_load_bytes (file, NULL, NULL, &error);
1088 g_assert_no_error (error);
1089 g_assert (bytes != NULL);
1090 g_assert_cmpint (len, ==, g_bytes_get_size (bytes));
1091 g_assert_cmpstr ("test_load_bytes", ==, (gchar *)g_bytes_get_data (bytes, NULL));
1093 g_file_delete (file, NULL, NULL);
1095 g_bytes_unref (bytes);
1096 g_object_unref (file);
1101 GMainLoop *main_loop;
1104 } LoadBytesAsyncData;
1107 test_load_bytes_cb (GObject *object,
1108 GAsyncResult *result,
1111 GFile *file = G_FILE (object);
1112 LoadBytesAsyncData *data = user_data;
1113 GError *error = NULL;
1115 data->bytes = g_file_load_bytes_finish (file, result, NULL, &error);
1116 g_assert_no_error (error);
1117 g_assert (data->bytes != NULL);
1119 g_main_loop_quit (data->main_loop);
1123 test_load_bytes_async (void)
1125 LoadBytesAsyncData data = { 0 };
1126 gchar filename[] = "g_file_load_bytes_XXXXXX";
1131 fd = g_mkstemp (filename);
1132 g_assert_cmpint (fd, !=, -1);
1133 len = strlen ("test_load_bytes_async");
1134 ret = write (fd, "test_load_bytes_async", len);
1135 g_assert_cmpint (ret, ==, len);
1138 data.main_loop = g_main_loop_new (NULL, FALSE);
1139 data.file = g_file_new_for_path (filename);
1141 g_file_load_bytes_async (data.file, NULL, test_load_bytes_cb, &data);
1142 g_main_loop_run (data.main_loop);
1144 g_assert_cmpint (len, ==, g_bytes_get_size (data.bytes));
1145 g_assert_cmpstr ("test_load_bytes_async", ==, (gchar *)g_bytes_get_data (data.bytes, NULL));
1147 g_file_delete (data.file, NULL, NULL);
1148 g_object_unref (data.file);
1149 g_bytes_unref (data.bytes);
1150 g_main_loop_unref (data.main_loop);
1154 main (int argc, char *argv[])
1156 g_test_init (&argc, &argv, NULL);
1158 g_test_bug_base ("http://bugzilla.gnome.org/");
1160 g_test_add_func ("/file/basic", test_basic);
1161 g_test_add_func ("/file/build-filename", test_build_filename);
1162 g_test_add_func ("/file/parent", test_parent);
1163 g_test_add_func ("/file/child", test_child);
1164 g_test_add_func ("/file/type", test_type);
1165 g_test_add_func ("/file/parse-name", test_parse_name);
1166 g_test_add_data_func ("/file/async-create-delete/0", GINT_TO_POINTER (0), test_create_delete);
1167 g_test_add_data_func ("/file/async-create-delete/1", GINT_TO_POINTER (1), test_create_delete);
1168 g_test_add_data_func ("/file/async-create-delete/10", GINT_TO_POINTER (10), test_create_delete);
1169 g_test_add_data_func ("/file/async-create-delete/25", GINT_TO_POINTER (25), test_create_delete);
1170 g_test_add_data_func ("/file/async-create-delete/4096", GINT_TO_POINTER (4096), test_create_delete);
1171 g_test_add_func ("/file/replace-load", test_replace_load);
1172 g_test_add_func ("/file/replace-cancel", test_replace_cancel);
1173 g_test_add_func ("/file/async-delete", test_async_delete);
1175 g_test_add_func ("/file/copy-preserve-mode", test_copy_preserve_mode);
1177 g_test_add_func ("/file/measure", test_measure);
1178 g_test_add_func ("/file/measure-async", test_measure_async);
1179 g_test_add_func ("/file/load-bytes", test_load_bytes);
1180 g_test_add_func ("/file/load-bytes-async", test_load_bytes_async);
1182 return g_test_run ();