Add a generic property test
[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 #include <glib/gstdio.h>
23
24 #ifdef G_OS_UNIX
25 #include <fcntl.h>
26 #include <gio/gunixinputstream.h>
27 #include <gio/gunixoutputstream.h>
28 #endif
29
30 GMainLoop *loop;
31 GPollableInputStream *in;
32 GOutputStream *out;
33
34 static gboolean
35 poll_source_callback (GPollableInputStream *in,
36                       gpointer              user_data)
37 {
38   GError *error = NULL;
39   char buf[2];
40   gssize nread;
41   gboolean *success = user_data;
42
43   nread = g_pollable_input_stream_read_nonblocking (in, buf, 2, NULL, &error);
44   g_assert_no_error (error);
45   g_assert_cmpint (nread, ==, 2);
46   g_assert_cmpstr (buf, ==, "x");
47
48   *success = TRUE;
49   return G_SOURCE_REMOVE;
50 }
51
52 static gboolean
53 check_source_readability_callback (gpointer user_data)
54 {
55   gboolean expected = GPOINTER_TO_INT (user_data);
56   gboolean readable;
57
58   readable = g_pollable_input_stream_is_readable (in);
59   g_assert_cmpint (readable, ==, expected);
60   return G_SOURCE_REMOVE;
61 }
62
63 static gboolean
64 write_callback (gpointer user_data)
65 {
66   char *buf = "x";
67   gssize nwrote;
68   GError *error = NULL;
69
70   nwrote = g_output_stream_write (out, buf, 2, NULL, &error);
71   g_assert_no_error (error);
72   g_assert_cmpint (nwrote, ==, 2);
73 /* Give the pipe a few ticks to propagate the write for sockets. On my
74  * iMac i7, 40 works, 30 doesn't. */
75   g_usleep (80L);
76
77   check_source_readability_callback (GINT_TO_POINTER (TRUE));
78
79   return G_SOURCE_REMOVE;
80 }
81
82 static gboolean
83 check_source_and_quit_callback (gpointer user_data)
84 {
85   check_source_readability_callback (user_data);
86   g_main_loop_quit (loop);
87   return G_SOURCE_REMOVE;
88 }
89
90 static void
91 test_streams (void)
92 {
93   gboolean readable;
94   GError *error = NULL;
95   char buf[1];
96   gssize nread;
97   GSource *poll_source;
98   gboolean success = FALSE;
99
100   g_assert (g_pollable_input_stream_can_poll (in));
101   g_assert (g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (out)));
102
103   readable = g_pollable_input_stream_is_readable (in);
104   g_assert (!readable);
105
106   nread = g_pollable_input_stream_read_nonblocking (in, buf, 1, NULL, &error);
107   g_assert_cmpint (nread, ==, -1);
108   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
109   g_clear_error (&error);
110
111   /* Create 4 sources, in decreasing order of priority:
112    *   1. poll source on @in
113    *   2. idle source that checks if @in is readable once
114    *      (it won't be) and then removes itself
115    *   3. idle source that writes a byte to @out, checks that
116    *      @in is now readable, and removes itself
117    *   4. idle source that checks if @in is readable once
118    *      (it won't be, since the poll source will fire before
119    *      this one does) and then quits the loop.
120    *
121    * If the poll source triggers before it should, then it will get a
122    * %G_IO_ERROR_WOULD_BLOCK, and if check() fails in either
123    * direction, we will catch it at some point.
124    */
125
126   poll_source = g_pollable_input_stream_create_source (in, NULL);
127   g_source_set_priority (poll_source, 1);
128   g_source_set_callback (poll_source, (GSourceFunc) poll_source_callback, &success, NULL);
129   g_source_attach (poll_source, NULL);
130   g_source_unref (poll_source);
131
132   g_idle_add_full (2, check_source_readability_callback, GINT_TO_POINTER (FALSE), NULL);
133   g_idle_add_full (3, write_callback, NULL, NULL);
134   g_idle_add_full (4, check_source_and_quit_callback, GINT_TO_POINTER (FALSE), NULL);
135
136   loop = g_main_loop_new (NULL, FALSE);
137   g_main_loop_run (loop);
138   g_main_loop_unref (loop);
139
140   g_assert_cmpint (success, ==, TRUE);
141 }
142
143 #ifdef G_OS_UNIX
144 static void
145 test_pollable_unix (void)
146 {
147   int pipefds[2], status, fd;
148
149   status = pipe (pipefds);
150   g_assert_cmpint (status, ==, 0);
151
152   in = G_POLLABLE_INPUT_STREAM (g_unix_input_stream_new (pipefds[0], TRUE));
153   out = g_unix_output_stream_new (pipefds[1], TRUE);
154
155   test_streams ();
156
157   g_object_unref (in);
158   g_object_unref (out);
159
160   /* Non-pipe/socket unix streams are not pollable */
161   fd = g_open ("/dev/null", O_RDWR, 0);
162   g_assert_cmpint (fd, !=, -1);
163   in = G_POLLABLE_INPUT_STREAM (g_unix_input_stream_new (fd, FALSE));
164   out = g_unix_output_stream_new (fd, FALSE);
165
166   g_assert (!g_pollable_input_stream_can_poll (in));
167   g_assert (!g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (out)));
168
169   g_object_unref (in);
170   g_object_unref (out);
171   close (fd);
172 }
173
174 static void
175 test_pollable_converter (void)
176 {
177   GConverter *converter;
178   GError *error = NULL;
179   GInputStream *ibase;
180   int pipefds[2], status;
181
182   status = pipe (pipefds);
183   g_assert_cmpint (status, ==, 0);
184
185   ibase = G_INPUT_STREAM (g_unix_input_stream_new (pipefds[0], TRUE));
186   converter = G_CONVERTER (g_charset_converter_new ("UTF-8", "UTF-8", &error));
187   g_assert_no_error (error);
188
189   in = G_POLLABLE_INPUT_STREAM (g_converter_input_stream_new (ibase, converter));
190   g_object_unref (converter);
191   g_object_unref (ibase);
192
193   out = g_unix_output_stream_new (pipefds[1], TRUE);
194
195   test_streams ();
196
197   g_object_unref (in);
198   g_object_unref (out);
199 }
200
201 #endif
202
203 static void
204 client_connected (GObject      *source,
205                   GAsyncResult *result,
206                   gpointer      user_data)
207 {
208   GSocketClient *client = G_SOCKET_CLIENT (source);
209   GSocketConnection **conn = user_data;
210   GError *error = NULL;
211
212   *conn = g_socket_client_connect_finish (client, result, &error);
213   g_assert_no_error (error);
214 }
215
216 static void
217 server_connected (GObject      *source,
218                   GAsyncResult *result,
219                   gpointer      user_data)
220 {
221   GSocketListener *listener = G_SOCKET_LISTENER (source);
222   GSocketConnection **conn = user_data;
223   GError *error = NULL;
224
225   *conn = g_socket_listener_accept_finish (listener, result, NULL, &error);
226   g_assert_no_error (error);
227 }
228
229 static void
230 test_pollable_socket (void)
231 {
232   GInetAddress *iaddr;
233   GSocketAddress *saddr, *effective_address;
234   GSocketListener *listener;
235   GSocketClient *client;
236   GError *error = NULL;
237   GSocketConnection *client_conn = NULL, *server_conn = NULL;
238
239   iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
240   saddr = g_inet_socket_address_new (iaddr, 0);
241   g_object_unref (iaddr);
242
243   listener = g_socket_listener_new ();
244   g_socket_listener_add_address (listener, saddr,
245                                  G_SOCKET_TYPE_STREAM,
246                                  G_SOCKET_PROTOCOL_TCP,
247                                  NULL,
248                                  &effective_address,
249                                  &error);
250   g_assert_no_error (error);
251   g_object_unref (saddr);
252
253   client = g_socket_client_new ();
254
255   g_socket_client_connect_async (client,
256                                  G_SOCKET_CONNECTABLE (effective_address),
257                                  NULL, client_connected, &client_conn);
258   g_socket_listener_accept_async (listener, NULL,
259                                   server_connected, &server_conn);
260
261   while (!client_conn || !server_conn)
262     g_main_context_iteration (NULL, TRUE);
263
264   in = G_POLLABLE_INPUT_STREAM (g_io_stream_get_input_stream (G_IO_STREAM (client_conn)));
265   out = g_io_stream_get_output_stream (G_IO_STREAM (server_conn));
266
267   test_streams ();
268
269   g_object_unref (client_conn);
270   g_object_unref (server_conn);
271   g_object_unref (client);
272   g_object_unref (listener);
273   g_object_unref (effective_address);
274 }
275
276 int
277 main (int   argc,
278       char *argv[])
279 {
280   g_test_init (&argc, &argv, NULL);
281
282 #ifdef G_OS_UNIX
283   g_test_add_func ("/pollable/unix", test_pollable_unix);
284   g_test_add_func ("/pollable/converter", test_pollable_converter);
285 #endif
286   g_test_add_func ("/pollable/socket", test_pollable_socket);
287
288   return g_test_run();
289 }
290