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>
33 #include "gnetworkingprivate.h"
44 echo_server_thread (gpointer user_data)
46 IPTestData *data = user_data;
52 sock = g_socket_accept (data->server, NULL, &error);
53 g_assert_no_error (error);
57 nread = g_socket_receive (sock, buf, sizeof (buf), NULL, &error);
58 g_assert_no_error (error);
59 g_assert_cmpint (nread, >=, 0);
64 nwrote = g_socket_send (sock, buf, nread, NULL, &error);
65 g_assert_no_error (error);
66 g_assert_cmpint (nwrote, ==, nread);
69 g_socket_close (sock, &error);
70 g_assert_no_error (error);
71 g_object_unref (sock);
76 create_server (GSocketFamily family,
77 GThreadFunc server_thread,
86 data = g_slice_new (IPTestData);
87 data->family = family;
89 data->server = server = g_socket_new (family,
91 G_SOCKET_PROTOCOL_DEFAULT,
93 g_assert_no_error (error);
95 g_assert_cmpint (g_socket_get_family (server), ==, family);
96 g_assert_cmpint (g_socket_get_socket_type (server), ==, G_SOCKET_TYPE_STREAM);
97 g_assert_cmpint (g_socket_get_protocol (server), ==, G_SOCKET_PROTOCOL_DEFAULT);
99 g_socket_set_blocking (server, TRUE);
101 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
106 fd = g_socket_get_fd (server);
108 setsockopt (fd, IPPROTO_IPV6, IPV6_V6ONLY, &v6_only, sizeof (v6_only));
113 iaddr = g_inet_address_new_any (family);
115 iaddr = g_inet_address_new_loopback (family);
116 addr = g_inet_socket_address_new (iaddr, 0);
117 g_object_unref (iaddr);
119 g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), ==, 0);
120 g_socket_bind (server, addr, TRUE, &error);
121 g_assert_no_error (error);
122 g_object_unref (addr);
124 addr = g_socket_get_local_address (server, &error);
125 g_assert_no_error (error);
126 g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), !=, 0);
127 g_object_unref (addr);
129 g_socket_listen (server, &error);
130 g_assert_no_error (error);
132 data->thread = g_thread_new ("server", server_thread, data);
137 static const gchar *testbuf = "0123456789abcdef";
140 test_ip_async_read_ready (GSocket *client,
144 IPTestData *data = user_data;
145 GError *error = NULL;
149 g_assert_cmpint (cond, ==, G_IO_IN);
151 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
152 g_assert_no_error (error);
153 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
155 g_assert_cmpstr (testbuf, ==, buf);
157 g_main_loop_quit (data->loop);
163 test_ip_async_write_ready (GSocket *client,
167 IPTestData *data = user_data;
168 GError *error = NULL;
172 g_assert_cmpint (cond, ==, G_IO_OUT);
174 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
175 g_assert_no_error (error);
176 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
178 source = g_socket_create_source (client, G_IO_IN, NULL);
179 g_source_set_callback (source, (GSourceFunc)test_ip_async_read_ready,
181 g_source_attach (source, NULL);
182 g_source_unref (source);
188 test_ip_async_timed_out (GSocket *client,
192 IPTestData *data = user_data;
193 GError *error = NULL;
198 if (data->family == G_SOCKET_FAMILY_IPV4)
200 g_assert_cmpint (cond, ==, G_IO_IN);
201 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
202 g_assert_cmpint (len, ==, -1);
203 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
204 g_clear_error (&error);
207 source = g_socket_create_source (client, G_IO_OUT, NULL);
208 g_source_set_callback (source, (GSourceFunc)test_ip_async_write_ready,
210 g_source_attach (source, NULL);
211 g_source_unref (source);
217 test_ip_async_connected (GSocket *client,
221 IPTestData *data = user_data;
222 GError *error = NULL;
227 g_socket_check_connect_result (client, &error);
228 g_assert_no_error (error);
229 /* We do this after the check_connect_result, since that will give a
230 * more useful assertion in case of error.
232 g_assert_cmpint (cond, ==, G_IO_OUT);
234 g_assert (g_socket_is_connected (client));
236 /* This adds 1 second to "make check", so let's just only do it once. */
237 if (data->family == G_SOCKET_FAMILY_IPV4)
239 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
240 g_assert_cmpint (len, ==, -1);
241 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
242 g_clear_error (&error);
244 source = g_socket_create_source (client, G_IO_IN, NULL);
245 g_source_set_callback (source, (GSourceFunc)test_ip_async_timed_out,
247 g_source_attach (source, NULL);
248 g_source_unref (source);
251 test_ip_async_timed_out (client, 0, data);
257 idle_test_ip_async_connected (gpointer user_data)
259 IPTestData *data = user_data;
261 return test_ip_async_connected (data->client, G_IO_OUT, data);
265 test_ip_async (GSocketFamily family)
268 GError *error = NULL;
270 GSocketAddress *addr;
275 data = create_server (family, echo_server_thread, FALSE);
276 addr = g_socket_get_local_address (data->server, &error);
278 client = g_socket_new (family,
279 G_SOCKET_TYPE_STREAM,
280 G_SOCKET_PROTOCOL_DEFAULT,
282 g_assert_no_error (error);
283 data->client = client;
285 g_assert_cmpint (g_socket_get_family (client), ==, family);
286 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
287 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
289 g_socket_set_blocking (client, FALSE);
290 g_socket_set_timeout (client, 1);
292 if (g_socket_connect (client, addr, NULL, &error))
294 g_assert_no_error (error);
295 g_idle_add (idle_test_ip_async_connected, data);
299 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
300 g_clear_error (&error);
301 source = g_socket_create_source (client, G_IO_OUT, NULL);
302 g_source_set_callback (source, (GSourceFunc)test_ip_async_connected,
304 g_source_attach (source, NULL);
305 g_source_unref (source);
307 g_object_unref (addr);
309 data->loop = g_main_loop_new (NULL, TRUE);
310 g_main_loop_run (data->loop);
311 g_main_loop_unref (data->loop);
313 g_socket_shutdown (client, FALSE, TRUE, &error);
314 g_assert_no_error (error);
316 g_thread_join (data->thread);
318 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
319 g_assert_no_error (error);
320 g_assert_cmpint (len, ==, 0);
322 g_socket_close (client, &error);
323 g_assert_no_error (error);
324 g_socket_close (data->server, &error);
325 g_assert_no_error (error);
327 g_object_unref (data->server);
328 g_object_unref (client);
330 g_slice_free (IPTestData, data);
334 test_ipv4_async (void)
336 test_ip_async (G_SOCKET_FAMILY_IPV4);
340 test_ipv6_async (void)
342 test_ip_async (G_SOCKET_FAMILY_IPV6);
346 test_ip_sync (GSocketFamily family)
349 GError *error = NULL;
351 GSocketAddress *addr;
355 data = create_server (family, echo_server_thread, FALSE);
356 addr = g_socket_get_local_address (data->server, &error);
358 client = g_socket_new (family,
359 G_SOCKET_TYPE_STREAM,
360 G_SOCKET_PROTOCOL_DEFAULT,
362 g_assert_no_error (error);
364 g_assert_cmpint (g_socket_get_family (client), ==, family);
365 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
366 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
368 g_socket_set_blocking (client, TRUE);
369 g_socket_set_timeout (client, 1);
371 g_socket_connect (client, addr, NULL, &error);
372 g_assert_no_error (error);
373 g_assert (g_socket_is_connected (client));
374 g_object_unref (addr);
376 /* This adds 1 second to "make check", so let's just only do it once. */
377 if (family == G_SOCKET_FAMILY_IPV4)
379 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
380 g_assert_cmpint (len, ==, -1);
381 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
382 g_clear_error (&error);
385 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
386 g_assert_no_error (error);
387 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
389 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
390 g_assert_no_error (error);
391 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
393 g_assert_cmpstr (testbuf, ==, buf);
395 g_socket_shutdown (client, FALSE, TRUE, &error);
396 g_assert_no_error (error);
398 g_thread_join (data->thread);
400 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
401 g_assert_no_error (error);
402 g_assert_cmpint (len, ==, 0);
404 g_socket_close (client, &error);
405 g_assert_no_error (error);
406 g_socket_close (data->server, &error);
407 g_assert_no_error (error);
409 g_object_unref (data->server);
410 g_object_unref (client);
412 g_slice_free (IPTestData, data);
416 test_ipv4_sync (void)
418 test_ip_sync (G_SOCKET_FAMILY_IPV4);
422 test_ipv6_sync (void)
424 test_ip_sync (G_SOCKET_FAMILY_IPV6);
427 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
429 v4mapped_server_thread (gpointer user_data)
431 IPTestData *data = user_data;
433 GError *error = NULL;
434 GSocketAddress *addr;
436 sock = g_socket_accept (data->server, NULL, &error);
437 g_assert_no_error (error);
439 g_assert_cmpint (g_socket_get_family (sock), ==, G_SOCKET_FAMILY_IPV6);
441 addr = g_socket_get_local_address (sock, &error);
442 g_assert_no_error (error);
443 g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
444 g_object_unref (addr);
446 addr = g_socket_get_remote_address (sock, &error);
447 g_assert_no_error (error);
448 g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
449 g_object_unref (addr);
451 g_socket_close (sock, &error);
452 g_assert_no_error (error);
453 g_object_unref (sock);
458 test_ipv6_v4mapped (void)
461 GError *error = NULL;
463 GSocketAddress *addr, *v4addr;
466 data = create_server (G_SOCKET_FAMILY_IPV6, v4mapped_server_thread, TRUE);
468 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
469 G_SOCKET_TYPE_STREAM,
470 G_SOCKET_PROTOCOL_DEFAULT,
472 g_assert_no_error (error);
474 g_socket_set_blocking (client, TRUE);
475 g_socket_set_timeout (client, 1);
477 addr = g_socket_get_local_address (data->server, &error);
478 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
479 v4addr = g_inet_socket_address_new (iaddr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
480 g_object_unref (iaddr);
481 g_object_unref (addr);
483 g_socket_connect (client, v4addr, NULL, &error);
484 g_assert_no_error (error);
485 g_assert (g_socket_is_connected (client));
487 g_thread_join (data->thread);
489 g_socket_close (client, &error);
490 g_assert_no_error (error);
491 g_socket_close (data->server, &error);
492 g_assert_no_error (error);
494 g_object_unref (data->server);
495 g_object_unref (client);
496 g_object_unref (v4addr);
498 g_slice_free (IPTestData, data);
504 test_unix_from_fd (void)
510 fd = socket (AF_UNIX, SOCK_STREAM, 0);
511 g_assert_cmpint (fd, !=, -1);
514 s = g_socket_new_from_fd (fd, &error);
515 g_assert_no_error (error);
516 g_assert_cmpint (g_socket_get_family (s), ==, G_SOCKET_FAMILY_UNIX);
517 g_assert_cmpint (g_socket_get_socket_type (s), ==, G_SOCKET_TYPE_STREAM);
518 g_assert_cmpint (g_socket_get_protocol (s), ==, G_SOCKET_PROTOCOL_DEFAULT);
523 test_unix_connection (void)
528 GSocketConnection *c;
530 fd = socket (AF_UNIX, SOCK_STREAM, 0);
531 g_assert_cmpint (fd, !=, -1);
534 s = g_socket_new_from_fd (fd, &error);
535 g_assert_no_error (error);
536 c = g_socket_connection_factory_create_connection (s);
537 g_assert (G_IS_UNIX_CONNECTION (c));
542 static GSocketConnection *
543 create_connection_for_fd (int fd)
547 GSocketConnection *connection;
549 socket = g_socket_new_from_fd (fd, &err);
550 g_assert_no_error (err);
551 g_assert (G_IS_SOCKET (socket));
552 connection = g_socket_connection_factory_create_connection (socket);
553 g_assert (G_IS_UNIX_CONNECTION (connection));
554 g_object_unref (socket);
558 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
561 test_unix_connection_ancillary_data (void)
565 gint status, fd, len;
570 g_assert_cmpint (status, ==, 0);
572 status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
573 g_assert_cmpint (status, ==, 0);
576 g_assert_cmpint (pid, >=, 0);
578 /* Child: close its copy of the write end of the pipe, receive it
579 * again from the parent over the socket, and write some text to it.
581 * Parent: send the write end of the pipe (still open for the
582 * parent) over the socket, close it, and read some text from the
583 * read end of the pipe.
587 GSocketConnection *connection;
590 connection = create_connection_for_fd (sv[0]);
592 status = close (pv[1]);
593 g_assert_cmpint (status, ==, 0);
596 fd = g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection), NULL,
598 g_assert_no_error (err);
599 g_assert_cmpint (fd, >, -1);
600 g_object_unref (connection);
603 len = write (fd, TEST_DATA, sizeof (TEST_DATA));
604 while (len == -1 && errno == EINTR);
605 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
610 GSocketConnection *connection;
613 connection = create_connection_for_fd (sv[1]);
616 g_unix_connection_send_fd (G_UNIX_CONNECTION (connection), pv[1], NULL,
618 g_assert_no_error (err);
619 g_object_unref (connection);
621 status = close (pv[1]);
622 g_assert_cmpint (status, ==, 0);
624 memset (buffer, 0xff, sizeof buffer);
626 len = read (pv[0], buffer, sizeof buffer);
627 while (len == -1 && errno == EINTR);
629 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
630 g_assert_cmpstr (buffer, ==, TEST_DATA);
632 waitpid (pid, &status, 0);
633 g_assert (WIFEXITED (status));
634 g_assert_cmpint (WEXITSTATUS (status), ==, 0);
637 /* TODO: add test for g_unix_connection_send_credentials() and
638 * g_unix_connection_receive_credentials().
641 #endif /* G_OS_UNIX */
648 g_test_init (&argc, &argv, NULL);
650 g_test_add_func ("/socket/ipv4_sync", test_ipv4_sync);
651 g_test_add_func ("/socket/ipv4_async", test_ipv4_async);
652 g_test_add_func ("/socket/ipv6_sync", test_ipv6_sync);
653 g_test_add_func ("/socket/ipv6_async", test_ipv6_async);
654 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
655 g_test_add_func ("/socket/ipv6_v4mapped", test_ipv6_v4mapped);
658 g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
659 g_test_add_func ("/socket/unix-connection", test_unix_connection);
660 g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data);