2 #include <glib/gstdio.h>
10 #include <io.h> /* for close() */
13 static const char *original_data = "This is some test data that we can put in a file...";
14 static const char *new_data = "new data..";
17 verify_pos (GIOStream *iostream, goffset expected_pos)
21 pos = g_seekable_tell (G_SEEKABLE (iostream));
22 g_assert_cmpint (pos, ==, expected_pos);
24 pos = g_seekable_tell (G_SEEKABLE (g_io_stream_get_input_stream (iostream)));
25 g_assert_cmpint (pos, ==, expected_pos);
27 pos = g_seekable_tell (G_SEEKABLE (g_io_stream_get_output_stream (iostream)));
28 g_assert_cmpint (pos, ==, expected_pos);
32 verify_iostream (GFileIOStream *file_iostream)
44 iostream = G_IO_STREAM (file_iostream);
46 verify_pos (iostream, 0);
48 in = g_io_stream_get_input_stream (iostream);
49 out = g_io_stream_get_output_stream (iostream);
51 res = g_input_stream_read_all (in, buffer, 20, &n_bytes, NULL, NULL);
53 g_assert_cmpmem (buffer, n_bytes, original_data, 20);
55 verify_pos (iostream, 20);
57 res = g_seekable_seek (G_SEEKABLE (iostream),
61 verify_pos (iostream, strlen (original_data) - 10);
63 res = g_input_stream_read_all (in, buffer, 20, &n_bytes, NULL, NULL);
65 g_assert_cmpmem (buffer, n_bytes, original_data + strlen (original_data) - 10, 10);
67 verify_pos (iostream, strlen (original_data));
69 res = g_seekable_seek (G_SEEKABLE (iostream),
73 res = g_input_stream_skip (in, 5, NULL, NULL);
75 verify_pos (iostream, 15);
77 skipped = g_input_stream_skip (in, 10000, NULL, NULL);
78 g_assert_cmpint (skipped, >=, 0);
79 g_assert ((gsize) skipped == strlen (original_data) - 15);
80 verify_pos (iostream, strlen (original_data));
82 res = g_seekable_seek (G_SEEKABLE (iostream),
86 verify_pos (iostream, 10);
88 res = g_output_stream_write_all (out, new_data, strlen (new_data),
89 &n_bytes, NULL, NULL);
91 g_assert_cmpint (n_bytes, ==, strlen (new_data));
93 verify_pos (iostream, 10 + strlen (new_data));
95 res = g_seekable_seek (G_SEEKABLE (iostream),
99 verify_pos (iostream, 0);
101 res = g_input_stream_read_all (in, buffer, strlen (original_data), &n_bytes, NULL, NULL);
103 g_assert_cmpint ((int)n_bytes, ==, strlen (original_data));
106 modified_data = g_strdup (original_data);
107 memcpy (modified_data + 10, new_data, strlen (new_data));
108 g_assert_cmpstr (buffer, ==, modified_data);
110 verify_pos (iostream, strlen (original_data));
112 res = g_seekable_seek (G_SEEKABLE (iostream),
116 verify_pos (iostream, 0);
118 res = g_output_stream_close (out, NULL, NULL);
121 res = g_input_stream_read_all (in, buffer, 15, &n_bytes, NULL, NULL);
123 g_assert_cmpmem (buffer, n_bytes, modified_data, 15);
126 res = g_output_stream_write_all (out, new_data, strlen (new_data),
127 &n_bytes, NULL, &error);
129 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
130 g_error_free (error);
133 res = g_io_stream_close (iostream, NULL, &error);
135 g_assert_no_error (error);
137 g_free (modified_data);
141 test_g_file_open_readwrite (void)
146 GFileIOStream *file_iostream;
151 fd = g_file_open_tmp ("readwrite_XXXXXX",
156 res = g_file_set_contents (tmp_file,
157 original_data, -1, NULL);
160 path = g_build_filename (g_get_tmp_dir (), "g-a-nonexisting-file", NULL);
161 file = g_file_new_for_path (path);
164 file_iostream = g_file_open_readwrite (file, NULL, &error);
165 g_assert (file_iostream == NULL);
166 g_assert (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND));
167 g_error_free (error);
168 g_object_unref (file);
170 file = g_file_new_for_path (tmp_file);
172 file_iostream = g_file_open_readwrite (file, NULL, &error);
173 g_assert (file_iostream != NULL);
174 g_object_unref (file);
176 verify_iostream (file_iostream);
178 g_object_unref (file_iostream);
185 test_g_file_create_readwrite (void)
190 GFileIOStream *file_iostream;
196 fd = g_file_open_tmp ("readwrite_XXXXXX",
201 file = g_file_new_for_path (tmp_file);
203 file_iostream = g_file_create_readwrite (file, 0, NULL, &error);
204 g_assert (file_iostream == NULL);
205 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
206 g_error_free (error);
209 file_iostream = g_file_create_readwrite (file, 0, NULL, &error);
210 g_assert (file_iostream != NULL);
212 out = g_io_stream_get_output_stream (G_IO_STREAM (file_iostream));
213 res = g_output_stream_write_all (out, original_data, strlen (original_data),
214 &n_bytes, NULL, NULL);
216 g_assert_cmpint (n_bytes, ==, strlen (original_data));
218 res = g_seekable_seek (G_SEEKABLE (file_iostream),
223 verify_iostream (file_iostream);
225 g_object_unref (file_iostream);
226 g_object_unref (file);
233 test_g_file_replace_readwrite (void)
235 char *tmp_file, *backup, *data;
238 GFileIOStream *file_iostream;
246 fd = g_file_open_tmp ("readwrite_XXXXXX",
251 res = g_file_set_contents (tmp_file,
255 file = g_file_new_for_path (tmp_file);
257 file_iostream = g_file_replace_readwrite (file, NULL,
258 TRUE, 0, NULL, &error);
259 g_assert (file_iostream != NULL);
261 in = g_io_stream_get_input_stream (G_IO_STREAM (file_iostream));
263 /* Ensure its empty */
264 res = g_input_stream_read_all (in, buffer, sizeof buffer, &n_bytes, NULL, NULL);
266 g_assert_cmpint ((int)n_bytes, ==, 0);
268 out = g_io_stream_get_output_stream (G_IO_STREAM (file_iostream));
269 res = g_output_stream_write_all (out, original_data, strlen (original_data),
270 &n_bytes, NULL, NULL);
272 g_assert_cmpint (n_bytes, ==, strlen (original_data));
274 res = g_seekable_seek (G_SEEKABLE (file_iostream),
279 verify_iostream (file_iostream);
281 g_object_unref (file_iostream);
282 g_object_unref (file);
284 backup = g_strconcat (tmp_file, "~", NULL);
285 res = g_file_get_contents (backup,
289 g_assert_cmpstr (data, ==, new_data);
303 g_test_init (&argc, &argv, NULL);
305 g_test_add_func ("/readwrite/test_g_file_open_readwrite",
306 test_g_file_open_readwrite);
307 g_test_add_func ("/readwrite/test_g_file_create_readwrite",
308 test_g_file_create_readwrite);
309 g_test_add_func ("/readwrite/test_g_file_replace_readwrite",
310 test_g_file_replace_readwrite);