Initial commit
[platform/upstream/glib2.0.git] / gio / tests / readwrite.c
1 #include <glib/glib.h>
2 #include <glib/gstdio.h>
3 #include <gio/gio.h>
4 #include <unistd.h>
5 #include <string.h>
6
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..";
9
10 static void
11 verify_pos (GIOStream *iostream, goffset expected_pos)
12 {
13   goffset pos;
14
15   pos = g_seekable_tell (G_SEEKABLE (iostream));
16   g_assert_cmpint (pos, ==, expected_pos);
17
18   pos = g_seekable_tell (G_SEEKABLE (g_io_stream_get_input_stream (iostream)));
19   g_assert_cmpint (pos, ==, expected_pos);
20
21   pos = g_seekable_tell (G_SEEKABLE (g_io_stream_get_output_stream (iostream)));
22   g_assert_cmpint (pos, ==, expected_pos);
23 }
24
25 static void
26 verify_iostream (GFileIOStream *file_iostream)
27 {
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;
36
37   iostream = G_IO_STREAM (file_iostream);
38
39   verify_pos (iostream, 0);
40
41   in = g_io_stream_get_input_stream (iostream);
42   out = g_io_stream_get_output_stream (iostream);
43
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);
47
48   g_assert (memcmp (buffer, original_data, 20) == 0);
49
50   verify_pos (iostream, 20);
51
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);
57
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);
62
63   verify_pos (iostream, strlen (original_data));
64
65   res = g_seekable_seek (G_SEEKABLE (iostream),
66                          10, G_SEEK_SET,
67                          NULL, NULL);
68
69   verify_pos (iostream, 10);
70
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));
75
76   verify_pos (iostream, 10 + strlen (new_data));
77
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);
83
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;
88
89   modified_data = g_strdup (original_data);
90   memcpy (modified_data + 10, new_data, strlen (new_data));
91   g_assert_cmpstr (buffer, ==, modified_data);
92
93   verify_pos (iostream, strlen (original_data));
94
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);
100
101   res = g_output_stream_close (out, NULL, NULL);
102   g_assert (res);
103
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);
108
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 != NULL);
114   g_assert (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CLOSED));
115
116   error = NULL;
117   res = g_io_stream_close (iostream, NULL, &error);
118   g_assert (res && error == NULL);
119
120   g_free (modified_data);
121 }
122
123 static void
124 test_g_file_open_readwrite (void)
125 {
126   char *tmp_file;
127   int fd;
128   gboolean res;
129   GFileIOStream *file_iostream;
130   char *path;
131   GFile *file;
132   GError *error;
133
134   fd = g_file_open_tmp ("readwrite_XXXXXX",
135                         &tmp_file, NULL);
136   g_assert (fd != -1);
137   close (fd);
138
139   res = g_file_set_contents (tmp_file,
140                              original_data, -1, NULL);
141   g_assert (res);
142
143   path = g_build_filename (g_get_tmp_dir (), "g-a-nonexisting-file", NULL);
144   file = g_file_new_for_path (path);
145   g_free (path);
146   error = NULL;
147   file_iostream = g_file_open_readwrite (file, NULL, &error);
148   g_assert (file_iostream == NULL);
149   g_assert (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND));
150   g_error_free (error);
151   g_object_unref (file);
152
153   file = g_file_new_for_path (tmp_file);
154   error = NULL;
155   file_iostream = g_file_open_readwrite (file, NULL, &error);
156   g_assert (file_iostream != NULL);
157
158   verify_iostream (file_iostream);
159
160   g_object_unref (file_iostream);
161
162   g_unlink (tmp_file);
163   g_free (tmp_file);
164 }
165
166 static void
167 test_g_file_create_readwrite (void)
168 {
169   char *tmp_file;
170   int fd;
171   gboolean res;
172   GFileIOStream *file_iostream;
173   GOutputStream *out;
174   GFile *file;
175   GError *error;
176   gsize n_bytes;
177
178   fd = g_file_open_tmp ("readwrite_XXXXXX",
179                         &tmp_file, NULL);
180   g_assert (fd != -1);
181   close (fd);
182
183   file = g_file_new_for_path (tmp_file);
184   error = NULL;
185   file_iostream = g_file_create_readwrite (file, 0, NULL, &error);
186   g_assert (file_iostream == NULL);
187   g_assert (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS));
188
189   g_unlink (tmp_file);
190   file_iostream = g_file_create_readwrite (file, 0, NULL, &error);
191   g_assert (file_iostream != NULL);
192
193   out = g_io_stream_get_output_stream (G_IO_STREAM (file_iostream));
194   res = g_output_stream_write_all (out, original_data, strlen (original_data),
195                                    &n_bytes, NULL, NULL);
196   g_assert (res);
197   g_assert_cmpint (n_bytes, ==, strlen (original_data));
198
199   res = g_seekable_seek (G_SEEKABLE (file_iostream),
200                          0, G_SEEK_SET,
201                          NULL, NULL);
202   g_assert (res);
203
204   verify_iostream (file_iostream);
205
206   g_object_unref (file_iostream);
207
208   g_unlink (tmp_file);
209   g_free (tmp_file);
210 }
211
212 static void
213 test_g_file_replace_readwrite (void)
214 {
215   char *tmp_file, *backup, *data;
216   int fd;
217   gboolean res;
218   GFileIOStream *file_iostream;
219   GInputStream *in;
220   GOutputStream *out;
221   GFile *file;
222   GError *error;
223   char buffer[1024];
224   gsize n_bytes;
225
226   fd = g_file_open_tmp ("readwrite_XXXXXX",
227                         &tmp_file, NULL);
228   g_assert (fd != -1);
229   close (fd);
230
231   res = g_file_set_contents (tmp_file,
232                              new_data, -1, NULL);
233   g_assert (res);
234
235   file = g_file_new_for_path (tmp_file);
236   error = NULL;
237   file_iostream = g_file_replace_readwrite (file, NULL,
238                                             TRUE, 0, NULL, &error);
239   g_assert (file_iostream != NULL);
240
241   in = g_io_stream_get_input_stream (G_IO_STREAM (file_iostream));
242
243   /* Ensure its empty */
244   res = g_input_stream_read_all (in, buffer, sizeof buffer, &n_bytes, NULL, NULL);
245   g_assert (res);
246   g_assert_cmpint ((int)n_bytes, ==, 0);
247
248   out = g_io_stream_get_output_stream (G_IO_STREAM (file_iostream));
249   res = g_output_stream_write_all (out, original_data, strlen (original_data),
250                                    &n_bytes, NULL, NULL);
251   g_assert (res);
252   g_assert_cmpint (n_bytes, ==, strlen (original_data));
253
254   res = g_seekable_seek (G_SEEKABLE (file_iostream),
255                          0, G_SEEK_SET,
256                          NULL, NULL);
257   g_assert (res);
258
259   verify_iostream (file_iostream);
260
261   g_object_unref (file_iostream);
262
263   backup = g_strconcat (tmp_file, "~", NULL);
264   res = g_file_get_contents (backup,
265                              &data,
266                              NULL, NULL);
267   g_assert (res);
268   g_assert_cmpstr (data, ==, new_data);
269   g_free (data);
270   g_unlink (backup);
271   g_free (backup);
272
273   g_unlink (tmp_file);
274   g_free (tmp_file);
275 }
276
277
278 int
279 main (int   argc,
280       char *argv[])
281 {
282   g_type_init ();
283   g_test_init (&argc, &argv, NULL);
284
285   g_test_add_func ("/readwrite/test_g_file_open_readwrite",
286                    test_g_file_open_readwrite);
287   g_test_add_func ("/readwrite/test_g_file_create_readwrite",
288                    test_g_file_create_readwrite);
289   g_test_add_func ("/readwrite/test_g_file_replace_readwrite",
290                    test_g_file_replace_readwrite);
291
292   return g_test_run();
293 }