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