2 #include <glib/gstdio.h>
10 static const char *original_data = "This is some test data that we can put in a file...";
11 static const char *new_data = "new data..";
14 verify_pos (GIOStream *iostream, goffset expected_pos)
18 pos = g_seekable_tell (G_SEEKABLE (iostream));
19 g_assert_cmpint (pos, ==, expected_pos);
21 pos = g_seekable_tell (G_SEEKABLE (g_io_stream_get_input_stream (iostream)));
22 g_assert_cmpint (pos, ==, expected_pos);
24 pos = g_seekable_tell (G_SEEKABLE (g_io_stream_get_output_stream (iostream)));
25 g_assert_cmpint (pos, ==, expected_pos);
29 verify_iostream (GFileIOStream *file_iostream)
40 iostream = G_IO_STREAM (file_iostream);
42 verify_pos (iostream, 0);
44 in = g_io_stream_get_input_stream (iostream);
45 out = g_io_stream_get_output_stream (iostream);
47 res = g_input_stream_read_all (in, buffer, 20, &n_bytes, NULL, NULL);
49 g_assert_cmpint ((int)n_bytes, ==, 20);
51 g_assert (memcmp (buffer, original_data, 20) == 0);
53 verify_pos (iostream, 20);
55 res = g_seekable_seek (G_SEEKABLE (iostream),
59 verify_pos (iostream, strlen (original_data) - 10);
61 res = g_input_stream_read_all (in, buffer, 20, &n_bytes, NULL, NULL);
63 g_assert_cmpint ((int)n_bytes, ==, 10);
64 g_assert (memcmp (buffer, original_data + strlen (original_data) - 10, 10) == 0);
66 verify_pos (iostream, strlen (original_data));
68 res = g_seekable_seek (G_SEEKABLE (iostream),
72 res = g_input_stream_skip (in, 5, NULL, NULL);
74 verify_pos (iostream, 15);
76 res = g_input_stream_skip (in, 10000, NULL, NULL);
77 g_assert (res == strlen (original_data) - 15);
78 verify_pos (iostream, strlen (original_data));
80 res = g_seekable_seek (G_SEEKABLE (iostream),
84 verify_pos (iostream, 10);
86 res = g_output_stream_write_all (out, new_data, strlen (new_data),
87 &n_bytes, NULL, NULL);
89 g_assert_cmpint (n_bytes, ==, strlen (new_data));
91 verify_pos (iostream, 10 + strlen (new_data));
93 res = g_seekable_seek (G_SEEKABLE (iostream),
97 verify_pos (iostream, 0);
99 res = g_input_stream_read_all (in, buffer, strlen (original_data), &n_bytes, NULL, NULL);
101 g_assert_cmpint ((int)n_bytes, ==, strlen (original_data));
104 modified_data = g_strdup (original_data);
105 memcpy (modified_data + 10, new_data, strlen (new_data));
106 g_assert_cmpstr (buffer, ==, modified_data);
108 verify_pos (iostream, strlen (original_data));
110 res = g_seekable_seek (G_SEEKABLE (iostream),
114 verify_pos (iostream, 0);
116 res = g_output_stream_close (out, NULL, NULL);
119 res = g_input_stream_read_all (in, buffer, 15, &n_bytes, NULL, NULL);
121 g_assert_cmpint ((int)n_bytes, ==, 15);
122 g_assert (memcmp (buffer, modified_data, 15) == 0);
125 res = g_output_stream_write_all (out, new_data, strlen (new_data),
126 &n_bytes, NULL, &error);
128 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
129 g_error_free (error);
132 res = g_io_stream_close (iostream, NULL, &error);
134 g_assert_no_error (error);
136 g_free (modified_data);
140 test_g_file_open_readwrite (void)
145 GFileIOStream *file_iostream;
150 fd = g_file_open_tmp ("readwrite_XXXXXX",
155 res = g_file_set_contents (tmp_file,
156 original_data, -1, NULL);
159 path = g_build_filename (g_get_tmp_dir (), "g-a-nonexisting-file", NULL);
160 file = g_file_new_for_path (path);
163 file_iostream = g_file_open_readwrite (file, NULL, &error);
164 g_assert (file_iostream == NULL);
165 g_assert (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND));
166 g_error_free (error);
167 g_object_unref (file);
169 file = g_file_new_for_path (tmp_file);
171 file_iostream = g_file_open_readwrite (file, NULL, &error);
172 g_assert (file_iostream != NULL);
173 g_object_unref (file);
175 verify_iostream (file_iostream);
177 g_object_unref (file_iostream);
184 test_g_file_create_readwrite (void)
189 GFileIOStream *file_iostream;
195 fd = g_file_open_tmp ("readwrite_XXXXXX",
200 file = g_file_new_for_path (tmp_file);
202 file_iostream = g_file_create_readwrite (file, 0, NULL, &error);
203 g_assert (file_iostream == NULL);
204 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
205 g_error_free (error);
208 file_iostream = g_file_create_readwrite (file, 0, NULL, &error);
209 g_assert (file_iostream != NULL);
211 out = g_io_stream_get_output_stream (G_IO_STREAM (file_iostream));
212 res = g_output_stream_write_all (out, original_data, strlen (original_data),
213 &n_bytes, NULL, NULL);
215 g_assert_cmpint (n_bytes, ==, strlen (original_data));
217 res = g_seekable_seek (G_SEEKABLE (file_iostream),
222 verify_iostream (file_iostream);
224 g_object_unref (file_iostream);
225 g_object_unref (file);
232 test_g_file_replace_readwrite (void)
234 char *tmp_file, *backup, *data;
237 GFileIOStream *file_iostream;
245 fd = g_file_open_tmp ("readwrite_XXXXXX",
250 res = g_file_set_contents (tmp_file,
254 file = g_file_new_for_path (tmp_file);
256 file_iostream = g_file_replace_readwrite (file, NULL,
257 TRUE, 0, NULL, &error);
258 g_assert (file_iostream != NULL);
260 in = g_io_stream_get_input_stream (G_IO_STREAM (file_iostream));
262 /* Ensure its empty */
263 res = g_input_stream_read_all (in, buffer, sizeof buffer, &n_bytes, NULL, NULL);
265 g_assert_cmpint ((int)n_bytes, ==, 0);
267 out = g_io_stream_get_output_stream (G_IO_STREAM (file_iostream));
268 res = g_output_stream_write_all (out, original_data, strlen (original_data),
269 &n_bytes, NULL, NULL);
271 g_assert_cmpint (n_bytes, ==, strlen (original_data));
273 res = g_seekable_seek (G_SEEKABLE (file_iostream),
278 verify_iostream (file_iostream);
280 g_object_unref (file_iostream);
281 g_object_unref (file);
283 backup = g_strconcat (tmp_file, "~", NULL);
284 res = g_file_get_contents (backup,
288 g_assert_cmpstr (data, ==, new_data);
302 g_test_init (&argc, &argv, NULL);
304 g_test_add_func ("/readwrite/test_g_file_open_readwrite",
305 test_g_file_open_readwrite);
306 g_test_add_func ("/readwrite/test_g_file_create_readwrite",
307 test_g_file_create_readwrite);
308 g_test_add_func ("/readwrite/test_g_file_replace_readwrite",
309 test_g_file_replace_readwrite);