GDesktopAppInfo: rewrite content type code
[platform/upstream/glib.git] / gio / tests / readwrite.c
1 #include <glib/glib.h>
2 #include <glib/gstdio.h>
3 #include <gio/gio.h>
4 #include <string.h>
5
6 #ifdef G_OS_UNIX
7 #include <unistd.h>
8 #endif
9
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..";
12
13 static void
14 verify_pos (GIOStream *iostream, goffset expected_pos)
15 {
16   goffset pos;
17
18   pos = g_seekable_tell (G_SEEKABLE (iostream));
19   g_assert_cmpint (pos, ==, expected_pos);
20
21   pos = g_seekable_tell (G_SEEKABLE (g_io_stream_get_input_stream (iostream)));
22   g_assert_cmpint (pos, ==, expected_pos);
23
24   pos = g_seekable_tell (G_SEEKABLE (g_io_stream_get_output_stream (iostream)));
25   g_assert_cmpint (pos, ==, expected_pos);
26 }
27
28 static void
29 verify_iostream (GFileIOStream *file_iostream)
30 {
31   gboolean res;
32   GIOStream *iostream;
33   GError *error;
34   GInputStream *in;
35   GOutputStream *out;
36   char buffer[1024];
37   gsize n_bytes;
38   char *modified_data;
39
40   iostream = G_IO_STREAM (file_iostream);
41
42   verify_pos (iostream, 0);
43
44   in = g_io_stream_get_input_stream (iostream);
45   out = g_io_stream_get_output_stream (iostream);
46
47   res = g_input_stream_read_all (in, buffer, 20, &n_bytes, NULL, NULL);
48   g_assert (res);
49   g_assert_cmpint ((int)n_bytes, ==, 20);
50
51   g_assert (memcmp (buffer, original_data, 20) == 0);
52
53   verify_pos (iostream, 20);
54
55   res = g_seekable_seek (G_SEEKABLE (iostream),
56                          -10, G_SEEK_END,
57                          NULL, NULL);
58   g_assert (res);
59   verify_pos (iostream, strlen (original_data) - 10);
60
61   res = g_input_stream_read_all (in, buffer, 20, &n_bytes, NULL, NULL);
62   g_assert (res);
63   g_assert_cmpint ((int)n_bytes, ==, 10);
64   g_assert (memcmp (buffer, original_data + strlen (original_data) - 10, 10) == 0);
65
66   verify_pos (iostream, strlen (original_data));
67
68   res = g_seekable_seek (G_SEEKABLE (iostream),
69                          10, G_SEEK_SET,
70                          NULL, NULL);
71
72   res = g_input_stream_skip (in, 5, NULL, NULL);
73   g_assert (res == 5);
74   verify_pos (iostream, 15);
75
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));
79
80   res = g_seekable_seek (G_SEEKABLE (iostream),
81                          10, G_SEEK_SET,
82                          NULL, NULL);
83
84   verify_pos (iostream, 10);
85
86   res = g_output_stream_write_all (out, new_data, strlen (new_data),
87                                    &n_bytes, NULL, NULL);
88   g_assert (res);
89   g_assert_cmpint (n_bytes, ==, strlen (new_data));
90
91   verify_pos (iostream, 10 + strlen (new_data));
92
93   res = g_seekable_seek (G_SEEKABLE (iostream),
94                          0, G_SEEK_SET,
95                          NULL, NULL);
96   g_assert (res);
97   verify_pos (iostream, 0);
98
99   res = g_input_stream_read_all (in, buffer, strlen (original_data), &n_bytes, NULL, NULL);
100   g_assert (res);
101   g_assert_cmpint ((int)n_bytes, ==, strlen (original_data));
102   buffer[n_bytes] = 0;
103
104   modified_data = g_strdup (original_data);
105   memcpy (modified_data + 10, new_data, strlen (new_data));
106   g_assert_cmpstr (buffer, ==, modified_data);
107
108   verify_pos (iostream, strlen (original_data));
109
110   res = g_seekable_seek (G_SEEKABLE (iostream),
111                          0, G_SEEK_SET,
112                          NULL, NULL);
113   g_assert (res);
114   verify_pos (iostream, 0);
115
116   res = g_output_stream_close (out, NULL, NULL);
117   g_assert (res);
118
119   res = g_input_stream_read_all (in, buffer, 15, &n_bytes, NULL, NULL);
120   g_assert (res);
121   g_assert_cmpint ((int)n_bytes, ==, 15);
122   g_assert (memcmp (buffer, modified_data, 15) == 0);
123
124   error = NULL;
125   res = g_output_stream_write_all (out, new_data, strlen (new_data),
126                                    &n_bytes, NULL, &error);
127   g_assert (!res);
128   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
129   g_error_free (error);
130
131   error = NULL;
132   res = g_io_stream_close (iostream, NULL, &error);
133   g_assert (res);
134   g_assert_no_error (error);
135
136   g_free (modified_data);
137 }
138
139 static void
140 test_g_file_open_readwrite (void)
141 {
142   char *tmp_file;
143   int fd;
144   gboolean res;
145   GFileIOStream *file_iostream;
146   char *path;
147   GFile *file;
148   GError *error;
149
150   fd = g_file_open_tmp ("readwrite_XXXXXX",
151                         &tmp_file, NULL);
152   g_assert (fd != -1);
153   close (fd);
154
155   res = g_file_set_contents (tmp_file,
156                              original_data, -1, NULL);
157   g_assert (res);
158
159   path = g_build_filename (g_get_tmp_dir (), "g-a-nonexisting-file", NULL);
160   file = g_file_new_for_path (path);
161   g_free (path);
162   error = NULL;
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);
168
169   file = g_file_new_for_path (tmp_file);
170   error = NULL;
171   file_iostream = g_file_open_readwrite (file, NULL, &error);
172   g_assert (file_iostream != NULL);
173   g_object_unref (file);
174
175   verify_iostream (file_iostream);
176
177   g_object_unref (file_iostream);
178
179   g_unlink (tmp_file);
180   g_free (tmp_file);
181 }
182
183 static void
184 test_g_file_create_readwrite (void)
185 {
186   char *tmp_file;
187   int fd;
188   gboolean res;
189   GFileIOStream *file_iostream;
190   GOutputStream *out;
191   GFile *file;
192   GError *error;
193   gsize n_bytes;
194
195   fd = g_file_open_tmp ("readwrite_XXXXXX",
196                         &tmp_file, NULL);
197   g_assert (fd != -1);
198   close (fd);
199
200   file = g_file_new_for_path (tmp_file);
201   error = NULL;
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);
206
207   g_unlink (tmp_file);
208   file_iostream = g_file_create_readwrite (file, 0, NULL, &error);
209   g_assert (file_iostream != NULL);
210
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);
214   g_assert (res);
215   g_assert_cmpint (n_bytes, ==, strlen (original_data));
216
217   res = g_seekable_seek (G_SEEKABLE (file_iostream),
218                          0, G_SEEK_SET,
219                          NULL, NULL);
220   g_assert (res);
221
222   verify_iostream (file_iostream);
223
224   g_object_unref (file_iostream);
225   g_object_unref (file);
226
227   g_unlink (tmp_file);
228   g_free (tmp_file);
229 }
230
231 static void
232 test_g_file_replace_readwrite (void)
233 {
234   char *tmp_file, *backup, *data;
235   int fd;
236   gboolean res;
237   GFileIOStream *file_iostream;
238   GInputStream *in;
239   GOutputStream *out;
240   GFile *file;
241   GError *error;
242   char buffer[1024];
243   gsize n_bytes;
244
245   fd = g_file_open_tmp ("readwrite_XXXXXX",
246                         &tmp_file, NULL);
247   g_assert (fd != -1);
248   close (fd);
249
250   res = g_file_set_contents (tmp_file,
251                              new_data, -1, NULL);
252   g_assert (res);
253
254   file = g_file_new_for_path (tmp_file);
255   error = NULL;
256   file_iostream = g_file_replace_readwrite (file, NULL,
257                                             TRUE, 0, NULL, &error);
258   g_assert (file_iostream != NULL);
259
260   in = g_io_stream_get_input_stream (G_IO_STREAM (file_iostream));
261
262   /* Ensure its empty */
263   res = g_input_stream_read_all (in, buffer, sizeof buffer, &n_bytes, NULL, NULL);
264   g_assert (res);
265   g_assert_cmpint ((int)n_bytes, ==, 0);
266
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);
270   g_assert (res);
271   g_assert_cmpint (n_bytes, ==, strlen (original_data));
272
273   res = g_seekable_seek (G_SEEKABLE (file_iostream),
274                          0, G_SEEK_SET,
275                          NULL, NULL);
276   g_assert (res);
277
278   verify_iostream (file_iostream);
279
280   g_object_unref (file_iostream);
281   g_object_unref (file);
282
283   backup = g_strconcat (tmp_file, "~", NULL);
284   res = g_file_get_contents (backup,
285                              &data,
286                              NULL, NULL);
287   g_assert (res);
288   g_assert_cmpstr (data, ==, new_data);
289   g_free (data);
290   g_unlink (backup);
291   g_free (backup);
292
293   g_unlink (tmp_file);
294   g_free (tmp_file);
295 }
296
297
298 int
299 main (int   argc,
300       char *argv[])
301 {
302   g_test_init (&argc, &argv, NULL);
303
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);
310
311   return g_test_run();
312 }