Add GWin32InputStream and GWin32OutputStream classes
[platform/upstream/glib.git] / gio / tests / win32-streams.c
1 /* GLib testing framework examples and tests
2  * Copyright (C) 2008 Red Hat, Inc
3  *
4  * This work is provided "as is"; redistribution and modification
5  * in whole or in part, in any medium, physical or electronic is
6  * permitted without restriction.
7  *
8  * This work is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * In no event shall the authors or contributors be liable for any
13  * direct, indirect, incidental, special, exemplary, or consequential
14  * damages (including, but not limited to, procurement of substitute
15  * goods or services; loss of use, data, or profits; or business
16  * interruption) however caused and on any theory of liability, whether
17  * in contract, strict liability, or tort (including negligence or
18  * otherwise) arising in any way out of the use of this software, even
19  * if advised of the possibility of such damage.
20  */
21
22 #include <glib/glib.h>
23 #include <gio/gio.h>
24 #include <gio/gwin32inputstream.h>
25 #include <gio/gwin32outputstream.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <io.h>
30
31 #include <windows.h>
32
33 #define DATA "abcdefghijklmnopqrstuvwxyz"
34
35 int writer_pipe[2], reader_pipe[2];
36 GCancellable *writer_cancel, *reader_cancel, *main_cancel;
37 GMainLoop *loop;
38
39 static gpointer
40 writer_thread (gpointer user_data)
41 {
42   GOutputStream *out;
43   gssize nwrote, offset;
44   GError *err = NULL;
45   HANDLE out_handle;
46
47   g_assert (DuplicateHandle (GetCurrentProcess (),
48                              (HANDLE) (gintptr) _get_osfhandle (writer_pipe[1]),
49                              GetCurrentProcess (),
50                              &out_handle,
51                              0, FALSE,
52                              DUPLICATE_SAME_ACCESS));
53   close (writer_pipe[1]);
54
55   out = g_win32_output_stream_new (out_handle, TRUE);
56   do
57     {
58       g_usleep (10);
59
60       offset = 0;
61       while (offset < (gssize) sizeof (DATA))
62         {
63           nwrote = g_output_stream_write (out, DATA + offset,
64                                           sizeof (DATA) - offset,
65                                           writer_cancel, &err);
66           if (nwrote <= 0 || err != NULL)
67             break;
68           offset += nwrote;
69         }
70
71       g_assert (nwrote > 0 || err != NULL);
72     }
73   while (err == NULL);
74
75   if (g_cancellable_is_cancelled (writer_cancel))
76     {
77       g_cancellable_cancel (main_cancel);
78       g_object_unref (out);
79       return NULL;
80     }
81
82   g_warning ("writer: %s", err->message);
83   g_assert_not_reached ();
84 }
85
86 static gpointer
87 reader_thread (gpointer user_data)
88 {
89   GInputStream *in;
90   gssize nread = 0, total;
91   GError *err = NULL;
92   char buf[sizeof (DATA)];
93   HANDLE in_handle;
94
95   g_assert (DuplicateHandle (GetCurrentProcess (),
96                              (HANDLE) (gintptr) _get_osfhandle (reader_pipe[0]),
97                              GetCurrentProcess (),
98                              &in_handle,
99                              0, FALSE,
100                              DUPLICATE_SAME_ACCESS));
101   close (reader_pipe[0]);
102
103   in = g_win32_input_stream_new (in_handle, TRUE);
104
105   do
106     {
107       total = 0;
108       while (total < (gssize) sizeof (DATA))
109         {
110           nread = g_input_stream_read (in, buf + total, sizeof (buf) - total,
111                                        reader_cancel, &err);
112           if (nread <= 0 || err != NULL)
113             break;
114           total += nread;
115         }
116
117       if (err)
118         break;
119
120       if (nread == 0)
121         {
122           g_assert (err == NULL);
123           /* pipe closed */
124           g_object_unref (in);
125           return NULL;
126         }
127
128       g_assert_cmpstr (buf, ==, DATA);
129       g_assert (!g_cancellable_is_cancelled (reader_cancel));
130     }
131   while (err == NULL);
132
133   g_warning ("reader: %s", err->message);
134   g_assert_not_reached ();
135 }
136
137 char main_buf[sizeof (DATA)];
138 gssize main_len, main_offset;
139
140 static void readable (GObject *source, GAsyncResult *res, gpointer user_data);
141 static void writable (GObject *source, GAsyncResult *res, gpointer user_data);
142
143 static void
144 do_main_cancel (GOutputStream *out)
145 {
146   g_output_stream_close (out, NULL, NULL);
147   g_main_loop_quit (loop);
148 }
149
150 static void
151 readable (GObject *source, GAsyncResult *res, gpointer user_data)
152 {
153   GInputStream *in = G_INPUT_STREAM (source);
154   GOutputStream *out = user_data;
155   GError *err = NULL;
156
157   main_len = g_input_stream_read_finish (in, res, &err);
158
159   if (g_cancellable_is_cancelled (main_cancel))
160     {
161       do_main_cancel (out);
162       return;
163     }
164
165   g_assert (err == NULL);
166
167   main_offset = 0;
168   g_output_stream_write_async (out, main_buf, main_len,
169                                G_PRIORITY_DEFAULT, main_cancel,
170                                writable, in);
171 }
172
173 static void
174 writable (GObject *source, GAsyncResult *res, gpointer user_data)
175 {
176   GOutputStream *out = G_OUTPUT_STREAM (source);
177   GInputStream *in = user_data;
178   GError *err = NULL;
179   gssize nwrote;
180
181   nwrote = g_output_stream_write_finish (out, res, &err);
182
183   if (g_cancellable_is_cancelled (main_cancel))
184     {
185       do_main_cancel (out);
186       return;
187     }
188
189   g_assert (err == NULL);
190   g_assert_cmpint (nwrote, <=, main_len - main_offset);
191
192   main_offset += nwrote;
193   if (main_offset == main_len)
194     {
195       g_input_stream_read_async (in, main_buf, sizeof (main_buf),
196                                  G_PRIORITY_DEFAULT, main_cancel,
197                                  readable, out);
198     }
199   else
200     {
201       g_output_stream_write_async (out, main_buf + main_offset,
202                                    main_len - main_offset,
203                                    G_PRIORITY_DEFAULT, main_cancel,
204                                    writable, in);
205     }
206 }
207
208 static gboolean
209 timeout (gpointer cancellable)
210 {
211   g_cancellable_cancel (cancellable);
212   return FALSE;
213 }
214
215 static void
216 test_pipe_io (void)
217 {
218   GThread *writer, *reader;
219   GInputStream *in;
220   GOutputStream *out;
221   HANDLE in_handle, out_handle;
222
223   /* Split off two (additional) threads, a reader and a writer. From
224    * the writer thread, write data synchronously in small chunks,
225    * which gets read asynchronously by the main thread and then
226    * written asynchronously to the reader thread, which reads it
227    * synchronously. Eventually a timeout in the main thread will cause
228    * it to cancel the writer thread, which will in turn cancel the
229    * read op in the main thread, which will then close the pipe to
230    * the reader thread, causing the read op to fail.
231    */
232
233   g_assert (_pipe (writer_pipe, 10, _O_BINARY) == 0 && _pipe (reader_pipe, 10, _O_BINARY) == 0);
234
235   writer_cancel = g_cancellable_new ();
236   reader_cancel = g_cancellable_new ();
237   main_cancel = g_cancellable_new ();
238
239   writer = g_thread_create (writer_thread, NULL, TRUE, NULL);
240   reader = g_thread_create (reader_thread, NULL, TRUE, NULL);
241
242   g_assert (DuplicateHandle (GetCurrentProcess (),
243                              (HANDLE) (gintptr) _get_osfhandle (writer_pipe[0]),
244                              GetCurrentProcess (),
245                              &in_handle,
246                              0, FALSE,
247                              DUPLICATE_SAME_ACCESS));
248   close (writer_pipe[0]);
249
250   g_assert (DuplicateHandle (GetCurrentProcess (),
251                              (HANDLE) (gintptr) _get_osfhandle (reader_pipe[1]),
252                              GetCurrentProcess (),
253                              &out_handle,
254                              0, FALSE,
255                              DUPLICATE_SAME_ACCESS));
256   close (reader_pipe[1]);
257
258   in = g_win32_input_stream_new (in_handle, TRUE);
259   out = g_win32_output_stream_new (out_handle, TRUE);
260
261   g_input_stream_read_async (in, main_buf, sizeof (main_buf),
262                              G_PRIORITY_DEFAULT, main_cancel,
263                              readable, out);
264
265   g_timeout_add (500, timeout, writer_cancel);
266
267   loop = g_main_loop_new (NULL, TRUE);
268   g_main_loop_run (loop);
269   g_main_loop_unref (loop);
270
271   g_thread_join (reader);
272   g_thread_join (writer);
273
274   g_object_unref (main_cancel);
275   g_object_unref (reader_cancel);
276   g_object_unref (writer_cancel);
277   g_object_unref (in);
278   g_object_unref (out);
279 }
280
281 int
282 main (int   argc,
283       char *argv[])
284 {
285   g_thread_init (NULL);
286   g_type_init ();
287   g_test_init (&argc, &argv, NULL);
288
289   g_test_add_func ("/win32-streams/pipe-io-test", test_pipe_io);
290
291   return g_test_run();
292 }