1 /* GLib testing framework examples and tests
3 * Copyright (C) 2010 Red Hat, Inc.
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.
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.
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.
22 #include <glib/gstdio.h>
26 #include <gio/gunixinputstream.h>
27 #include <gio/gunixoutputstream.h>
31 GPollableInputStream *in;
35 poll_source_callback (GPollableInputStream *in,
41 gboolean *success = user_data;
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");
49 return G_SOURCE_REMOVE;
53 check_source_readability_callback (gpointer user_data)
55 gboolean expected = GPOINTER_TO_INT (user_data);
58 readable = g_pollable_input_stream_is_readable (in);
59 g_assert_cmpint (readable, ==, expected);
60 return G_SOURCE_REMOVE;
64 write_callback (gpointer user_data)
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. */
77 check_source_readability_callback (GINT_TO_POINTER (TRUE));
79 return G_SOURCE_REMOVE;
83 check_source_and_quit_callback (gpointer user_data)
85 check_source_readability_callback (user_data);
86 g_main_loop_quit (loop);
87 return G_SOURCE_REMOVE;
98 gboolean success = FALSE;
100 g_assert (g_pollable_input_stream_can_poll (in));
101 g_assert (g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (out)));
103 readable = g_pollable_input_stream_is_readable (in);
104 g_assert (!readable);
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);
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.
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.
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);
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);
136 loop = g_main_loop_new (NULL, FALSE);
137 g_main_loop_run (loop);
138 g_main_loop_unref (loop);
140 g_assert_cmpint (success, ==, TRUE);
145 test_pollable_unix (void)
147 int pipefds[2], status, fd;
149 status = pipe (pipefds);
150 g_assert_cmpint (status, ==, 0);
152 in = G_POLLABLE_INPUT_STREAM (g_unix_input_stream_new (pipefds[0], TRUE));
153 out = g_unix_output_stream_new (pipefds[1], TRUE);
158 g_object_unref (out);
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);
166 g_assert (!g_pollable_input_stream_can_poll (in));
167 g_assert (!g_pollable_output_stream_can_poll (G_POLLABLE_OUTPUT_STREAM (out)));
170 g_object_unref (out);
175 test_pollable_converter (void)
177 GConverter *converter;
178 GError *error = NULL;
180 int pipefds[2], status;
182 status = pipe (pipefds);
183 g_assert_cmpint (status, ==, 0);
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);
189 in = G_POLLABLE_INPUT_STREAM (g_converter_input_stream_new (ibase, converter));
190 g_object_unref (converter);
191 g_object_unref (ibase);
193 out = g_unix_output_stream_new (pipefds[1], TRUE);
198 g_object_unref (out);
204 client_connected (GObject *source,
205 GAsyncResult *result,
208 GSocketClient *client = G_SOCKET_CLIENT (source);
209 GSocketConnection **conn = user_data;
210 GError *error = NULL;
212 *conn = g_socket_client_connect_finish (client, result, &error);
213 g_assert_no_error (error);
217 server_connected (GObject *source,
218 GAsyncResult *result,
221 GSocketListener *listener = G_SOCKET_LISTENER (source);
222 GSocketConnection **conn = user_data;
223 GError *error = NULL;
225 *conn = g_socket_listener_accept_finish (listener, result, NULL, &error);
226 g_assert_no_error (error);
230 test_pollable_socket (void)
233 GSocketAddress *saddr, *effective_address;
234 GSocketListener *listener;
235 GSocketClient *client;
236 GError *error = NULL;
237 GSocketConnection *client_conn = NULL, *server_conn = NULL;
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);
243 listener = g_socket_listener_new ();
244 g_socket_listener_add_address (listener, saddr,
245 G_SOCKET_TYPE_STREAM,
246 G_SOCKET_PROTOCOL_TCP,
250 g_assert_no_error (error);
251 g_object_unref (saddr);
253 client = g_socket_client_new ();
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);
261 while (!client_conn || !server_conn)
262 g_main_context_iteration (NULL, TRUE);
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));
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);
280 g_test_init (&argc, &argv, NULL);
283 g_test_add_func ("/pollable/unix", test_pollable_unix);
284 g_test_add_func ("/pollable/converter", test_pollable_converter);
286 g_test_add_func ("/pollable/socket", test_pollable_socket);