Add pollable input/output streams
[platform/upstream/glib.git] / gio / tests / pollable.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2010 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <gio/gio.h>
22
23 #ifdef G_OS_UNIX
24 #include <gio/gunixinputstream.h>
25 #include <gio/gunixoutputstream.h>
26 #endif
27
28 GMainLoop *loop;
29 GPollableInputStream *in;
30 GOutputStream *out;
31
32 static gboolean
33 poll_source_callback (GPollableInputStream *in,
34                       gpointer              user_data)
35 {
36   GError *error = NULL;
37   char buf[2];
38   gssize nread;
39   gboolean *success = user_data;
40
41   nread = g_pollable_input_stream_read_nonblocking (in, buf, 2, NULL, &error);
42   g_assert_no_error (error);
43   g_assert_cmpint (nread, ==, 2);
44   g_assert_cmpstr (buf, ==, "x");
45
46   *success = TRUE;
47   return FALSE;
48 }
49
50 static gboolean
51 check_source_readability_callback (gpointer user_data)
52 {
53   gboolean expected = GPOINTER_TO_INT (user_data);
54   gboolean readable;
55
56   readable = g_pollable_input_stream_is_readable (in);
57   g_assert_cmpint (readable, ==, expected);
58   return FALSE;
59 }
60
61 static gboolean
62 write_callback (gpointer user_data)
63 {
64   char *buf = "x";
65   gssize nwrote;
66   GError *error = NULL;
67
68   nwrote = g_output_stream_write (out, buf, 2, NULL, &error);
69   g_assert_no_error (error);
70   g_assert_cmpint (nwrote, ==, 2);
71
72   check_source_readability_callback (GINT_TO_POINTER (TRUE));
73
74   return FALSE;
75 }
76
77 static gboolean
78 check_source_and_quit_callback (gpointer user_data)
79 {
80   check_source_readability_callback (user_data);
81   g_main_loop_quit (loop);
82   return FALSE;
83 }
84
85 static void
86 test_streams (void)
87 {
88   gboolean readable;
89   GError *error = NULL;
90   char buf[1];
91   gssize nread;
92   GSource *poll_source;
93   gboolean success = FALSE;
94
95   readable = g_pollable_input_stream_is_readable (in);
96   g_assert (!readable);
97
98   nread = g_pollable_input_stream_read_nonblocking (in, buf, 1, NULL, &error);
99   g_assert_cmpint (nread, ==, -1);
100   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
101   g_clear_error (&error);
102
103   /* Create 4 sources, in decreasing order of priority:
104    *   1. poll source on @in
105    *   2. idle source that checks if @in is readable once
106    *      (it won't be) and then removes itself
107    *   3. idle source that writes a byte to @out, checks that
108    *      @in is now readable, and removes itself
109    *   4. idle source that checks if @in is readable once
110    *      (it won't be, since the poll source will fire before
111    *      this one does) and then quits the loop.
112    *
113    * If the poll source triggers before it should, then it will get a
114    * %G_IO_ERROR_WOULD_BLOCK, and if check() fails in either
115    * direction, we will catch it at some point.
116    */
117
118   poll_source = g_pollable_input_stream_create_source (in, NULL);
119   g_source_set_priority (poll_source, 1);
120   g_source_set_callback (poll_source, (GSourceFunc) poll_source_callback, &success, NULL);
121   g_source_attach (poll_source, NULL);
122   g_source_unref (poll_source);
123
124   g_idle_add_full (2, check_source_readability_callback, GINT_TO_POINTER (FALSE), NULL);
125   g_idle_add_full (3, write_callback, NULL, NULL);
126   g_idle_add_full (4, check_source_and_quit_callback, GINT_TO_POINTER (FALSE), NULL);
127
128   loop = g_main_loop_new (NULL, FALSE);
129   g_main_loop_run (loop);
130   g_main_loop_unref (loop);
131
132   g_assert_cmpint (success, ==, TRUE);
133 }
134
135 #ifdef G_OS_UNIX
136 static void
137 test_pollable_unix (void)
138 {
139   int pipefds[2], status;
140
141   status = pipe (pipefds);
142   g_assert_cmpint (status, ==, 0);
143
144   in = G_POLLABLE_INPUT_STREAM (g_unix_input_stream_new (pipefds[0], TRUE));
145   out = g_unix_output_stream_new (pipefds[1], TRUE);
146
147   test_streams ();
148
149   g_object_unref (in);
150   g_object_unref (out);
151 }
152 #endif
153
154 static void
155 client_connected (GObject      *source,
156                   GAsyncResult *result,
157                   gpointer      user_data)
158 {
159   GSocketClient *client = G_SOCKET_CLIENT (source);
160   GSocketConnection **conn = user_data;
161   GError *error = NULL;
162
163   *conn = g_socket_client_connect_finish (client, result, &error);
164   g_assert_no_error (error);
165 }
166
167 static void
168 server_connected (GObject      *source,
169                   GAsyncResult *result,
170                   gpointer      user_data)
171 {
172   GSocketListener *listener = G_SOCKET_LISTENER (source);
173   GSocketConnection **conn = user_data;
174   GError *error = NULL;
175
176   *conn = g_socket_listener_accept_finish (listener, result, NULL, &error);
177   g_assert_no_error (error);
178 }
179
180 static void
181 test_pollable_socket (void)
182 {
183   GInetAddress *iaddr;
184   GSocketAddress *saddr, *effective_address;
185   GSocketListener *listener;
186   GSocketClient *client;
187   GError *error = NULL;
188   GSocketConnection *client_conn = NULL, *server_conn = NULL;
189
190   iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
191   saddr = g_inet_socket_address_new (iaddr, 0);
192   g_object_unref (iaddr);
193
194   listener = g_socket_listener_new ();
195   g_socket_listener_add_address (listener, saddr,
196                                  G_SOCKET_TYPE_STREAM,
197                                  G_SOCKET_PROTOCOL_TCP,
198                                  NULL,
199                                  &effective_address,
200                                  &error);
201   g_assert_no_error (error);
202   g_object_unref (saddr);
203
204   client = g_socket_client_new ();
205
206   g_socket_client_connect_async (client,
207                                  G_SOCKET_CONNECTABLE (effective_address),
208                                  NULL, client_connected, &client_conn);
209   g_socket_listener_accept_async (listener, NULL,
210                                   server_connected, &server_conn);
211
212   while (!client_conn || !server_conn)
213     g_main_context_iteration (NULL, TRUE);
214
215   in = G_POLLABLE_INPUT_STREAM (g_io_stream_get_input_stream (G_IO_STREAM (client_conn)));
216   out = g_io_stream_get_output_stream (G_IO_STREAM (server_conn));
217
218   test_streams ();
219
220   g_object_unref (client_conn);
221   g_object_unref (server_conn);
222   g_object_unref (client);
223   g_object_unref (listener);
224 }
225
226 int
227 main (int   argc,
228       char *argv[])
229 {
230   g_type_init ();
231   g_test_init (&argc, &argv, NULL);
232
233 #ifdef G_OS_UNIX
234   g_test_add_func ("/pollable/unix", test_pollable_unix);
235 #endif
236   g_test_add_func ("/pollable/socket", test_pollable_socket);
237
238   return g_test_run();
239 }
240