1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2011 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.
25 #include <sys/types.h>
26 #include <sys/socket.h>
30 #include <gio/gunixconnection.h>
42 echo_server_thread (gpointer user_data)
44 IPTestData *data = user_data;
50 sock = g_socket_accept (data->server, NULL, &error);
51 g_assert_no_error (error);
55 nread = g_socket_receive (sock, buf, sizeof (buf), NULL, &error);
56 g_assert_no_error (error);
57 g_assert_cmpint (nread, >=, 0);
62 nwrote = g_socket_send (sock, buf, nread, NULL, &error);
63 g_assert_no_error (error);
64 g_assert_cmpint (nwrote, ==, nread);
67 g_socket_close (sock, &error);
68 g_assert_no_error (error);
73 create_server (GSocketFamily family)
81 data = g_slice_new (IPTestData);
82 data->family = family;
84 data->server = server = g_socket_new (family,
86 G_SOCKET_PROTOCOL_DEFAULT,
88 g_assert_no_error (error);
90 g_assert_cmpint (g_socket_get_family (server), ==, family);
91 g_assert_cmpint (g_socket_get_socket_type (server), ==, G_SOCKET_TYPE_STREAM);
92 g_assert_cmpint (g_socket_get_protocol (server), ==, G_SOCKET_PROTOCOL_DEFAULT);
94 g_socket_set_blocking (server, TRUE);
96 iaddr = g_inet_address_new_loopback (family);
97 addr = g_inet_socket_address_new (iaddr, 0);
98 g_object_unref (iaddr);
100 g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), ==, 0);
101 g_socket_bind (server, addr, TRUE, &error);
102 g_assert_no_error (error);
103 g_object_unref (addr);
105 addr = g_socket_get_local_address (server, &error);
106 g_assert_no_error (error);
107 g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), !=, 0);
109 g_socket_listen (server, &error);
110 g_assert_no_error (error);
112 data->thread = g_thread_create (echo_server_thread, data, TRUE, &error);
113 g_assert_no_error (error);
118 static const gchar *testbuf = "0123456789abcdef";
121 test_ip_async_read_ready (GSocket *client,
125 IPTestData *data = user_data;
126 GError *error = NULL;
130 g_assert_cmpint (cond, ==, G_IO_IN);
132 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
133 g_assert_no_error (error);
134 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
136 g_assert_cmpstr (testbuf, ==, buf);
138 g_main_loop_quit (data->loop);
144 test_ip_async_write_ready (GSocket *client,
148 IPTestData *data = user_data;
149 GError *error = NULL;
153 g_assert_cmpint (cond, ==, G_IO_OUT);
155 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
156 g_assert_no_error (error);
157 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
159 source = g_socket_create_source (client, G_IO_IN, NULL);
160 g_source_set_callback (source, (GSourceFunc)test_ip_async_read_ready,
162 g_source_attach (source, NULL);
163 g_source_unref (source);
169 test_ip_async_timed_out (GSocket *client,
173 IPTestData *data = user_data;
174 GError *error = NULL;
179 if (data->family == G_SOCKET_FAMILY_IPV4)
181 g_assert_cmpint (cond, ==, 0);
182 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
183 g_assert_cmpint (len, ==, -1);
184 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
185 g_clear_error (&error);
188 source = g_socket_create_source (client, G_IO_OUT, NULL);
189 g_source_set_callback (source, (GSourceFunc)test_ip_async_write_ready,
191 g_source_attach (source, NULL);
192 g_source_unref (source);
198 test_ip_async_connected (GSocket *client,
202 IPTestData *data = user_data;
203 GError *error = NULL;
208 g_socket_check_connect_result (client, &error);
209 g_assert_no_error (error);
210 /* We do this after the check_connect_result, since that will give a
211 * more useful assertion in case of error.
213 g_assert_cmpint (cond, ==, G_IO_OUT);
215 g_assert (g_socket_is_connected (client));
217 /* This adds 1 second to "make check", so let's just only do it once. */
218 if (data->family == G_SOCKET_FAMILY_IPV4)
220 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
221 g_assert_cmpint (len, ==, -1);
222 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
223 g_clear_error (&error);
225 source = g_socket_create_source (client, G_IO_IN, NULL);
226 g_source_set_callback (source, (GSourceFunc)test_ip_async_timed_out,
228 g_source_attach (source, NULL);
229 g_source_unref (source);
232 test_ip_async_timed_out (client, 0, data);
238 idle_test_ip_async_connected (gpointer user_data)
240 IPTestData *data = user_data;
242 return test_ip_async_connected (data->client, G_IO_OUT, data);
246 test_ip_async (GSocketFamily family)
249 GError *error = NULL;
251 GSocketAddress *addr;
256 data = create_server (family);
257 addr = g_socket_get_local_address (data->server, &error);
259 client = g_socket_new (family,
260 G_SOCKET_TYPE_STREAM,
261 G_SOCKET_PROTOCOL_DEFAULT,
263 g_assert_no_error (error);
264 data->client = client;
266 g_assert_cmpint (g_socket_get_family (client), ==, family);
267 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
268 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
270 g_socket_set_blocking (client, FALSE);
271 g_socket_set_timeout (client, 1);
273 if (g_socket_connect (client, addr, NULL, &error))
275 g_assert_no_error (error);
276 g_idle_add (idle_test_ip_async_connected, data);
280 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
281 g_clear_error (&error);
282 source = g_socket_create_source (client, G_IO_OUT, NULL);
283 g_source_set_callback (source, (GSourceFunc)test_ip_async_connected,
285 g_source_attach (source, NULL);
286 g_source_unref (source);
289 data->loop = g_main_loop_new (NULL, TRUE);
290 g_main_loop_run (data->loop);
291 g_main_loop_unref (data->loop);
293 g_socket_shutdown (client, FALSE, TRUE, &error);
294 g_assert_no_error (error);
296 g_thread_join (data->thread);
298 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
299 g_assert_no_error (error);
300 g_assert_cmpint (len, ==, 0);
302 g_socket_close (client, &error);
303 g_assert_no_error (error);
304 g_socket_close (data->server, &error);
305 g_assert_no_error (error);
307 g_object_unref (data->server);
308 g_object_unref (client);
310 g_slice_free (IPTestData, data);
314 test_ipv4_async (void)
316 test_ip_async (G_SOCKET_FAMILY_IPV4);
320 test_ipv6_async (void)
322 test_ip_async (G_SOCKET_FAMILY_IPV6);
326 test_ip_sync (GSocketFamily family)
329 GError *error = NULL;
331 GSocketAddress *addr;
335 data = create_server (family);
336 addr = g_socket_get_local_address (data->server, &error);
338 client = g_socket_new (family,
339 G_SOCKET_TYPE_STREAM,
340 G_SOCKET_PROTOCOL_DEFAULT,
342 g_assert_no_error (error);
344 g_assert_cmpint (g_socket_get_family (client), ==, family);
345 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
346 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
348 g_socket_set_blocking (client, TRUE);
349 g_socket_set_timeout (client, 1);
351 g_socket_connect (client, addr, NULL, &error);
352 g_assert_no_error (error);
353 g_assert (g_socket_is_connected (client));
355 /* This adds 1 second to "make check", so let's just only do it once. */
356 if (family == G_SOCKET_FAMILY_IPV4)
358 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
359 g_assert_cmpint (len, ==, -1);
360 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
361 g_clear_error (&error);
364 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
365 g_assert_no_error (error);
366 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
368 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
369 g_assert_no_error (error);
370 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
372 g_assert_cmpstr (testbuf, ==, buf);
374 g_socket_shutdown (client, FALSE, TRUE, &error);
375 g_assert_no_error (error);
377 g_thread_join (data->thread);
379 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
380 g_assert_no_error (error);
381 g_assert_cmpint (len, ==, 0);
383 g_socket_close (client, &error);
384 g_assert_no_error (error);
385 g_socket_close (data->server, &error);
386 g_assert_no_error (error);
388 g_object_unref (data->server);
389 g_object_unref (client);
391 g_slice_free (IPTestData, data);
395 test_ipv4_sync (void)
397 test_ip_sync (G_SOCKET_FAMILY_IPV4);
401 test_ipv6_sync (void)
403 test_ip_sync (G_SOCKET_FAMILY_IPV6);
408 test_unix_from_fd (void)
414 fd = socket (AF_UNIX, SOCK_STREAM, 0);
415 g_assert_cmpint (fd, !=, -1);
418 s = g_socket_new_from_fd (fd, &error);
419 g_assert_no_error (error);
420 g_assert_cmpint (g_socket_get_family (s), ==, G_SOCKET_FAMILY_UNIX);
421 g_assert_cmpint (g_socket_get_socket_type (s), ==, G_SOCKET_TYPE_STREAM);
422 g_assert_cmpint (g_socket_get_protocol (s), ==, G_SOCKET_PROTOCOL_DEFAULT);
427 test_unix_connection (void)
432 GSocketConnection *c;
434 fd = socket (AF_UNIX, SOCK_STREAM, 0);
435 g_assert_cmpint (fd, !=, -1);
438 s = g_socket_new_from_fd (fd, &error);
439 g_assert_no_error (error);
440 c = g_socket_connection_factory_create_connection (s);
441 g_assert (G_IS_UNIX_CONNECTION (c));
446 static GSocketConnection *
447 create_connection_for_fd (int fd)
451 GSocketConnection *connection;
453 socket = g_socket_new_from_fd (fd, &err);
454 g_assert_no_error (err);
455 g_assert (G_IS_SOCKET (socket));
456 connection = g_socket_connection_factory_create_connection (socket);
457 g_assert (G_IS_UNIX_CONNECTION (connection));
458 g_object_unref (socket);
462 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
465 test_unix_connection_ancillary_data (void)
469 gint status, fd, len;
474 g_assert_cmpint (status, ==, 0);
476 status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
477 g_assert_cmpint (status, ==, 0);
480 g_assert_cmpint (pid, >=, 0);
482 /* Child: close its copy of the write end of the pipe, receive it
483 * again from the parent over the socket, and write some text to it.
485 * Parent: send the write end of the pipe (still open for the
486 * parent) over the socket, close it, and read some text from the
487 * read end of the pipe.
491 GSocketConnection *connection;
494 connection = create_connection_for_fd (sv[0]);
496 status = close (pv[1]);
497 g_assert_cmpint (status, ==, 0);
500 fd = g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection), NULL,
502 g_assert_no_error (err);
503 g_assert_cmpint (fd, >, -1);
504 g_object_unref (connection);
507 len = write (fd, TEST_DATA, sizeof (TEST_DATA));
508 while (len == -1 && errno == EINTR);
509 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
514 GSocketConnection *connection;
517 connection = create_connection_for_fd (sv[1]);
520 g_unix_connection_send_fd (G_UNIX_CONNECTION (connection), pv[1], NULL,
522 g_assert_no_error (err);
523 g_object_unref (connection);
525 status = close (pv[1]);
526 g_assert_cmpint (status, ==, 0);
528 memset (buffer, 0xff, sizeof buffer);
530 len = read (pv[0], buffer, sizeof buffer);
531 while (len == -1 && errno == EINTR);
533 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
534 g_assert_cmpstr (buffer, ==, TEST_DATA);
536 waitpid (pid, &status, 0);
537 g_assert (WIFEXITED (status));
538 g_assert_cmpint (WEXITSTATUS (status), ==, 0);
541 /* TODO: add test for g_unix_connection_send_credentials() and
542 * g_unix_connection_receive_credentials().
545 #endif /* G_OS_UNIX */
552 g_test_init (&argc, &argv, NULL);
554 g_test_add_func ("/socket/ipv4_sync", test_ipv4_sync);
555 g_test_add_func ("/socket/ipv4_async", test_ipv4_async);
556 g_test_add_func ("/socket/ipv6_sync", test_ipv6_sync);
557 g_test_add_func ("/socket/ipv6_sync", test_ipv6_async);
559 g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
560 g_test_add_func ("/socket/unix-connection", test_unix_connection);
561 g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data);