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