docs: Add GIOModuleScope and GIOModuleScopeFlags
[glib.git] / gio / tests / readwrite.c
blob78362709edff0e65e7d8fc7dbf840c59fc5548a4
1 #include <glib/glib.h>
2 #include <glib/gstdio.h>
3 #include <gio/gio.h>
4 #include <unistd.h>
5 #include <string.h>
7 static const char *original_data = "This is some test data that we can put in a file...";
8 static const char *new_data = "new data..";
10 static void
11 verify_pos (GIOStream *iostream, goffset expected_pos)
13 goffset pos;
15 pos = g_seekable_tell (G_SEEKABLE (iostream));
16 g_assert_cmpint (pos, ==, expected_pos);
18 pos = g_seekable_tell (G_SEEKABLE (g_io_stream_get_input_stream (iostream)));
19 g_assert_cmpint (pos, ==, expected_pos);
21 pos = g_seekable_tell (G_SEEKABLE (g_io_stream_get_output_stream (iostream)));
22 g_assert_cmpint (pos, ==, expected_pos);
25 static void
26 verify_iostream (GFileIOStream *file_iostream)
28 gboolean res;
29 GIOStream *iostream;
30 GError *error;
31 GInputStream *in;
32 GOutputStream *out;
33 char buffer[1024];
34 gsize n_bytes;
35 char *modified_data;
37 iostream = G_IO_STREAM (file_iostream);
39 verify_pos (iostream, 0);
41 in = g_io_stream_get_input_stream (iostream);
42 out = g_io_stream_get_output_stream (iostream);
44 res = g_input_stream_read_all (in, buffer, 20, &n_bytes, NULL, NULL);
45 g_assert (res);
46 g_assert_cmpint ((int)n_bytes, ==, 20);
48 g_assert (memcmp (buffer, original_data, 20) == 0);
50 verify_pos (iostream, 20);
52 res = g_seekable_seek (G_SEEKABLE (iostream),
53 -10, G_SEEK_END,
54 NULL, NULL);
55 g_assert (res);
56 verify_pos (iostream, strlen (original_data) - 10);
58 res = g_input_stream_read_all (in, buffer, 20, &n_bytes, NULL, NULL);
59 g_assert (res);
60 g_assert_cmpint ((int)n_bytes, ==, 10);
61 g_assert (memcmp (buffer, original_data + strlen (original_data) - 10, 10) == 0);
63 verify_pos (iostream, strlen (original_data));
65 res = g_seekable_seek (G_SEEKABLE (iostream),
66 10, G_SEEK_SET,
67 NULL, NULL);
69 verify_pos (iostream, 10);
71 res = g_output_stream_write_all (out, new_data, strlen (new_data),
72 &n_bytes, NULL, NULL);
73 g_assert (res);
74 g_assert_cmpint (n_bytes, ==, strlen (new_data));
76 verify_pos (iostream, 10 + strlen (new_data));
78 res = g_seekable_seek (G_SEEKABLE (iostream),
79 0, G_SEEK_SET,
80 NULL, NULL);
81 g_assert (res);
82 verify_pos (iostream, 0);
84 res = g_input_stream_read_all (in, buffer, strlen (original_data), &n_bytes, NULL, NULL);
85 g_assert (res);
86 g_assert_cmpint ((int)n_bytes, ==, strlen (original_data));
87 buffer[n_bytes] = 0;
89 modified_data = g_strdup (original_data);
90 memcpy (modified_data + 10, new_data, strlen (new_data));
91 g_assert_cmpstr (buffer, ==, modified_data);
93 verify_pos (iostream, strlen (original_data));
95 res = g_seekable_seek (G_SEEKABLE (iostream),
96 0, G_SEEK_SET,
97 NULL, NULL);
98 g_assert (res);
99 verify_pos (iostream, 0);
101 res = g_output_stream_close (out, NULL, NULL);
102 g_assert (res);
104 res = g_input_stream_read_all (in, buffer, 15, &n_bytes, NULL, NULL);
105 g_assert (res);
106 g_assert_cmpint ((int)n_bytes, ==, 15);
107 g_assert (memcmp (buffer, modified_data, 15) == 0);
109 error = NULL;
110 res = g_output_stream_write_all (out, new_data, strlen (new_data),
111 &n_bytes, NULL, &error);
112 g_assert (!res);
113 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
114 g_error_free (error);
116 error = NULL;
117 res = g_io_stream_close (iostream, NULL, &error);
118 g_assert (res);
119 g_assert_no_error (error);
121 g_free (modified_data);
124 static void
125 test_g_file_open_readwrite (void)
127 char *tmp_file;
128 int fd;
129 gboolean res;
130 GFileIOStream *file_iostream;
131 char *path;
132 GFile *file;
133 GError *error;
135 fd = g_file_open_tmp ("readwrite_XXXXXX",
136 &tmp_file, NULL);
137 g_assert (fd != -1);
138 close (fd);
140 res = g_file_set_contents (tmp_file,
141 original_data, -1, NULL);
142 g_assert (res);
144 path = g_build_filename (g_get_tmp_dir (), "g-a-nonexisting-file", NULL);
145 file = g_file_new_for_path (path);
146 g_free (path);
147 error = NULL;
148 file_iostream = g_file_open_readwrite (file, NULL, &error);
149 g_assert (file_iostream == NULL);
150 g_assert (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND));
151 g_error_free (error);
152 g_object_unref (file);
154 file = g_file_new_for_path (tmp_file);
155 error = NULL;
156 file_iostream = g_file_open_readwrite (file, NULL, &error);
157 g_assert (file_iostream != NULL);
158 g_object_unref (file);
160 verify_iostream (file_iostream);
162 g_object_unref (file_iostream);
164 g_unlink (tmp_file);
165 g_free (tmp_file);
168 static void
169 test_g_file_create_readwrite (void)
171 char *tmp_file;
172 int fd;
173 gboolean res;
174 GFileIOStream *file_iostream;
175 GOutputStream *out;
176 GFile *file;
177 GError *error;
178 gsize n_bytes;
180 fd = g_file_open_tmp ("readwrite_XXXXXX",
181 &tmp_file, NULL);
182 g_assert (fd != -1);
183 close (fd);
185 file = g_file_new_for_path (tmp_file);
186 error = NULL;
187 file_iostream = g_file_create_readwrite (file, 0, NULL, &error);
188 g_assert (file_iostream == NULL);
189 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
190 g_error_free (error);
192 g_unlink (tmp_file);
193 file_iostream = g_file_create_readwrite (file, 0, NULL, &error);
194 g_assert (file_iostream != NULL);
196 out = g_io_stream_get_output_stream (G_IO_STREAM (file_iostream));
197 res = g_output_stream_write_all (out, original_data, strlen (original_data),
198 &n_bytes, NULL, NULL);
199 g_assert (res);
200 g_assert_cmpint (n_bytes, ==, strlen (original_data));
202 res = g_seekable_seek (G_SEEKABLE (file_iostream),
203 0, G_SEEK_SET,
204 NULL, NULL);
205 g_assert (res);
207 verify_iostream (file_iostream);
209 g_object_unref (file_iostream);
210 g_object_unref (file);
212 g_unlink (tmp_file);
213 g_free (tmp_file);
216 static void
217 test_g_file_replace_readwrite (void)
219 char *tmp_file, *backup, *data;
220 int fd;
221 gboolean res;
222 GFileIOStream *file_iostream;
223 GInputStream *in;
224 GOutputStream *out;
225 GFile *file;
226 GError *error;
227 char buffer[1024];
228 gsize n_bytes;
230 fd = g_file_open_tmp ("readwrite_XXXXXX",
231 &tmp_file, NULL);
232 g_assert (fd != -1);
233 close (fd);
235 res = g_file_set_contents (tmp_file,
236 new_data, -1, NULL);
237 g_assert (res);
239 file = g_file_new_for_path (tmp_file);
240 error = NULL;
241 file_iostream = g_file_replace_readwrite (file, NULL,
242 TRUE, 0, NULL, &error);
243 g_assert (file_iostream != NULL);
245 in = g_io_stream_get_input_stream (G_IO_STREAM (file_iostream));
247 /* Ensure its empty */
248 res = g_input_stream_read_all (in, buffer, sizeof buffer, &n_bytes, NULL, NULL);
249 g_assert (res);
250 g_assert_cmpint ((int)n_bytes, ==, 0);
252 out = g_io_stream_get_output_stream (G_IO_STREAM (file_iostream));
253 res = g_output_stream_write_all (out, original_data, strlen (original_data),
254 &n_bytes, NULL, NULL);
255 g_assert (res);
256 g_assert_cmpint (n_bytes, ==, strlen (original_data));
258 res = g_seekable_seek (G_SEEKABLE (file_iostream),
259 0, G_SEEK_SET,
260 NULL, NULL);
261 g_assert (res);
263 verify_iostream (file_iostream);
265 g_object_unref (file_iostream);
266 g_object_unref (file);
268 backup = g_strconcat (tmp_file, "~", NULL);
269 res = g_file_get_contents (backup,
270 &data,
271 NULL, NULL);
272 g_assert (res);
273 g_assert_cmpstr (data, ==, new_data);
274 g_free (data);
275 g_unlink (backup);
276 g_free (backup);
278 g_unlink (tmp_file);
279 g_free (tmp_file);
284 main (int argc,
285 char *argv[])
287 g_type_init ();
288 g_test_init (&argc, &argv, NULL);
290 g_test_add_func ("/readwrite/test_g_file_open_readwrite",
291 test_g_file_open_readwrite);
292 g_test_add_func ("/readwrite/test_g_file_create_readwrite",
293 test_g_file_create_readwrite);
294 g_test_add_func ("/readwrite/test_g_file_replace_readwrite",
295 test_g_file_replace_readwrite);
297 return g_test_run();