Merge remote-tracking branch 'gvdb/master'
[platform/upstream/glib.git] / gio / tests / unix-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 <gio/gio.h>
23 #include <gio/gunixinputstream.h>
24 #include <gio/gunixoutputstream.h>
25 #include <glib/glib-unix.h>
26 #include <signal.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #define DATA "abcdefghijklmnopqrstuvwxyz"
32
33 int writer_pipe[2], reader_pipe[2];
34 GCancellable *writer_cancel, *reader_cancel, *main_cancel;
35 GMainLoop *loop;
36
37
38 static gpointer
39 writer_thread (gpointer user_data)
40 {
41   GOutputStream *out;
42   gssize nwrote, offset;
43   GError *err = NULL;
44
45   out = g_unix_output_stream_new (writer_pipe[1], TRUE);
46
47   do
48     {
49       g_usleep (10);
50
51       offset = 0;
52       while (offset < (gssize) sizeof (DATA))
53         {
54           nwrote = g_output_stream_write (out, DATA + offset,
55                                           sizeof (DATA) - offset,
56                                           writer_cancel, &err);
57           if (nwrote <= 0 || err != NULL)
58             break;
59           offset += nwrote;
60         }
61
62       g_assert (nwrote > 0 || err != NULL);
63     }
64   while (err == NULL);
65
66   if (g_cancellable_is_cancelled (writer_cancel))
67     {
68       g_cancellable_cancel (main_cancel);
69       g_object_unref (out);
70       return NULL;
71     }
72
73   g_warning ("writer: %s", err->message);
74   g_assert_not_reached ();
75 }
76
77 static gpointer
78 reader_thread (gpointer user_data)
79 {
80   GInputStream *in;
81   gssize nread = 0, total;
82   GError *err = NULL;
83   char buf[sizeof (DATA)];
84
85   in = g_unix_input_stream_new (reader_pipe[0], TRUE);
86
87   do
88     {
89       total = 0;
90       while (total < (gssize) sizeof (DATA))
91         {
92           nread = g_input_stream_read (in, buf + total, sizeof (buf) - total,
93                                        reader_cancel, &err);
94           if (nread <= 0 || err != NULL)
95             break;
96           total += nread;
97         }
98
99       if (err)
100         break;
101
102       if (nread == 0)
103         {
104           g_assert (err == NULL);
105           /* pipe closed */
106           g_object_unref (in);
107           return NULL;
108         }
109
110       g_assert_cmpstr (buf, ==, DATA);
111       g_assert (!g_cancellable_is_cancelled (reader_cancel));
112     }
113   while (err == NULL);
114
115   g_warning ("reader: %s", err->message);
116   g_assert_not_reached ();
117 }
118
119 char main_buf[sizeof (DATA)];
120 gssize main_len, main_offset;
121
122 static void readable (GObject *source, GAsyncResult *res, gpointer user_data);
123 static void writable (GObject *source, GAsyncResult *res, gpointer user_data);
124
125 static void
126 do_main_cancel (GOutputStream *out)
127 {
128   g_output_stream_close (out, NULL, NULL);
129   g_main_loop_quit (loop);
130 }
131
132 static void
133 readable (GObject *source, GAsyncResult *res, gpointer user_data)
134 {
135   GInputStream *in = G_INPUT_STREAM (source);
136   GOutputStream *out = user_data;
137   GError *err = NULL;
138
139   main_len = g_input_stream_read_finish (in, res, &err);
140
141   if (g_cancellable_is_cancelled (main_cancel))
142     {
143       do_main_cancel (out);
144       return;
145     }
146
147   g_assert (err == NULL);
148
149   main_offset = 0;
150   g_output_stream_write_async (out, main_buf, main_len,
151                                G_PRIORITY_DEFAULT, main_cancel,
152                                writable, in);
153 }
154
155 static void
156 writable (GObject *source, GAsyncResult *res, gpointer user_data)
157 {
158   GOutputStream *out = G_OUTPUT_STREAM (source);
159   GInputStream *in = user_data;
160   GError *err = NULL;
161   gssize nwrote;
162
163   nwrote = g_output_stream_write_finish (out, res, &err);
164
165   if (g_cancellable_is_cancelled (main_cancel))
166     {
167       do_main_cancel (out);
168       return;
169     }
170
171   g_assert (err == NULL);
172   g_assert_cmpint (nwrote, <=, main_len - main_offset);
173
174   main_offset += nwrote;
175   if (main_offset == main_len)
176     {
177       g_input_stream_read_async (in, main_buf, sizeof (main_buf),
178                                  G_PRIORITY_DEFAULT, main_cancel,
179                                  readable, out);
180     }
181   else
182     {
183       g_output_stream_write_async (out, main_buf + main_offset,
184                                    main_len - main_offset,
185                                    G_PRIORITY_DEFAULT, main_cancel,
186                                    writable, in);
187     }
188 }
189
190 static gboolean
191 timeout (gpointer cancellable)
192 {
193   g_cancellable_cancel (cancellable);
194   return FALSE;
195 }
196
197 static void
198 test_pipe_io (gconstpointer nonblocking)
199 {
200   GThread *writer, *reader;
201   GInputStream *in;
202   GOutputStream *out;
203
204   /* Split off two (additional) threads, a reader and a writer. From
205    * the writer thread, write data synchronously in small chunks,
206    * which gets read asynchronously by the main thread and then
207    * written asynchronously to the reader thread, which reads it
208    * synchronously. Eventually a timeout in the main thread will cause
209    * it to cancel the writer thread, which will in turn cancel the
210    * read op in the main thread, which will then close the pipe to
211    * the reader thread, causing the read op to fail.
212    */
213
214   g_assert (pipe (writer_pipe) == 0 && pipe (reader_pipe) == 0);
215
216   if (nonblocking)
217     {
218       GError *error = NULL;
219
220       g_unix_set_fd_nonblocking (writer_pipe[0], TRUE, &error);
221       g_assert_no_error (error);
222       g_unix_set_fd_nonblocking (writer_pipe[1], TRUE, &error);
223       g_assert_no_error (error);
224       g_unix_set_fd_nonblocking (reader_pipe[0], TRUE, &error);
225       g_assert_no_error (error);
226       g_unix_set_fd_nonblocking (reader_pipe[1], TRUE, &error);
227       g_assert_no_error (error);
228     }
229
230   writer_cancel = g_cancellable_new ();
231   reader_cancel = g_cancellable_new ();
232   main_cancel = g_cancellable_new ();
233
234   writer = g_thread_new ("writer", writer_thread, NULL);
235   reader = g_thread_new ("reader", reader_thread, NULL);
236
237   in = g_unix_input_stream_new (writer_pipe[0], TRUE);
238   out = g_unix_output_stream_new (reader_pipe[1], TRUE);
239
240   g_input_stream_read_async (in, main_buf, sizeof (main_buf),
241                              G_PRIORITY_DEFAULT, main_cancel,
242                              readable, out);
243
244   g_timeout_add (500, timeout, writer_cancel);
245
246   loop = g_main_loop_new (NULL, TRUE);
247   g_main_loop_run (loop);
248   g_main_loop_unref (loop);
249
250   g_thread_join (reader);
251   g_thread_join (writer);
252
253   g_object_unref (main_cancel);
254   g_object_unref (reader_cancel);
255   g_object_unref (writer_cancel);
256   g_object_unref (in);
257   g_object_unref (out);
258 }
259
260 int
261 main (int   argc,
262       char *argv[])
263 {
264   g_type_init ();
265   g_test_init (&argc, &argv, NULL);
266
267   g_test_add_data_func ("/unix-streams/pipe-io-test",
268                         GINT_TO_POINTER (FALSE),
269                         test_pipe_io);
270   g_test_add_data_func ("/unix-streams/nonblocking-io-test",
271                         GINT_TO_POINTER (TRUE),
272                         test_pipe_io);
273
274   return g_test_run();
275 }