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.
28 #include <gio/gnetworking.h>
29 #include <gio/gunixconnection.h>
32 #include "gnetworkingprivate.h"
43 echo_server_thread (gpointer user_data)
45 IPTestData *data = user_data;
51 sock = g_socket_accept (data->server, NULL, &error);
52 g_assert_no_error (error);
56 nread = g_socket_receive (sock, buf, sizeof (buf), NULL, &error);
57 g_assert_no_error (error);
58 g_assert_cmpint (nread, >=, 0);
63 nwrote = g_socket_send (sock, buf, nread, NULL, &error);
64 g_assert_no_error (error);
65 g_assert_cmpint (nwrote, ==, nread);
68 g_socket_close (sock, &error);
69 g_assert_no_error (error);
70 g_object_unref (sock);
75 create_server (GSocketFamily family,
76 GThreadFunc server_thread,
85 data = g_slice_new (IPTestData);
86 data->family = family;
88 data->server = server = g_socket_new (family,
90 G_SOCKET_PROTOCOL_DEFAULT,
92 g_assert_no_error (error);
94 g_assert_cmpint (g_socket_get_family (server), ==, family);
95 g_assert_cmpint (g_socket_get_socket_type (server), ==, G_SOCKET_TYPE_STREAM);
96 g_assert_cmpint (g_socket_get_protocol (server), ==, G_SOCKET_PROTOCOL_DEFAULT);
98 g_socket_set_blocking (server, TRUE);
100 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
105 fd = g_socket_get_fd (server);
107 setsockopt (fd, IPPROTO_IPV6, IPV6_V6ONLY, &v6_only, sizeof (v6_only));
112 iaddr = g_inet_address_new_any (family);
114 iaddr = g_inet_address_new_loopback (family);
115 addr = g_inet_socket_address_new (iaddr, 0);
116 g_object_unref (iaddr);
118 g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), ==, 0);
119 g_socket_bind (server, addr, TRUE, &error);
120 g_assert_no_error (error);
121 g_object_unref (addr);
123 addr = g_socket_get_local_address (server, &error);
124 g_assert_no_error (error);
125 g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), !=, 0);
126 g_object_unref (addr);
128 g_socket_listen (server, &error);
129 g_assert_no_error (error);
131 data->thread = g_thread_new ("server", server_thread, data);
136 static const gchar *testbuf = "0123456789abcdef";
139 test_ip_async_read_ready (GSocket *client,
143 IPTestData *data = user_data;
144 GError *error = NULL;
148 g_assert_cmpint (cond, ==, G_IO_IN);
150 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
151 g_assert_no_error (error);
152 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
154 g_assert_cmpstr (testbuf, ==, buf);
156 g_main_loop_quit (data->loop);
162 test_ip_async_write_ready (GSocket *client,
166 IPTestData *data = user_data;
167 GError *error = NULL;
171 g_assert_cmpint (cond, ==, G_IO_OUT);
173 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
174 g_assert_no_error (error);
175 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
177 source = g_socket_create_source (client, G_IO_IN, NULL);
178 g_source_set_callback (source, (GSourceFunc)test_ip_async_read_ready,
180 g_source_attach (source, NULL);
181 g_source_unref (source);
187 test_ip_async_timed_out (GSocket *client,
191 IPTestData *data = user_data;
192 GError *error = NULL;
197 if (data->family == G_SOCKET_FAMILY_IPV4)
199 g_assert_cmpint (cond, ==, G_IO_IN);
200 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
201 g_assert_cmpint (len, ==, -1);
202 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
203 g_clear_error (&error);
206 source = g_socket_create_source (client, G_IO_OUT, NULL);
207 g_source_set_callback (source, (GSourceFunc)test_ip_async_write_ready,
209 g_source_attach (source, NULL);
210 g_source_unref (source);
216 test_ip_async_connected (GSocket *client,
220 IPTestData *data = user_data;
221 GError *error = NULL;
226 g_socket_check_connect_result (client, &error);
227 g_assert_no_error (error);
228 /* We do this after the check_connect_result, since that will give a
229 * more useful assertion in case of error.
231 g_assert_cmpint (cond, ==, G_IO_OUT);
233 g_assert (g_socket_is_connected (client));
235 /* This adds 1 second to "make check", so let's just only do it once. */
236 if (data->family == G_SOCKET_FAMILY_IPV4)
238 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
239 g_assert_cmpint (len, ==, -1);
240 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
241 g_clear_error (&error);
243 source = g_socket_create_source (client, G_IO_IN, NULL);
244 g_source_set_callback (source, (GSourceFunc)test_ip_async_timed_out,
246 g_source_attach (source, NULL);
247 g_source_unref (source);
250 test_ip_async_timed_out (client, 0, data);
256 idle_test_ip_async_connected (gpointer user_data)
258 IPTestData *data = user_data;
260 return test_ip_async_connected (data->client, G_IO_OUT, data);
264 test_ip_async (GSocketFamily family)
267 GError *error = NULL;
269 GSocketAddress *addr;
274 data = create_server (family, echo_server_thread, FALSE);
275 addr = g_socket_get_local_address (data->server, &error);
277 client = g_socket_new (family,
278 G_SOCKET_TYPE_STREAM,
279 G_SOCKET_PROTOCOL_DEFAULT,
281 g_assert_no_error (error);
282 data->client = client;
284 g_assert_cmpint (g_socket_get_family (client), ==, family);
285 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
286 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
288 g_socket_set_blocking (client, FALSE);
289 g_socket_set_timeout (client, 1);
291 if (g_socket_connect (client, addr, NULL, &error))
293 g_assert_no_error (error);
294 g_idle_add (idle_test_ip_async_connected, data);
298 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
299 g_clear_error (&error);
300 source = g_socket_create_source (client, G_IO_OUT, NULL);
301 g_source_set_callback (source, (GSourceFunc)test_ip_async_connected,
303 g_source_attach (source, NULL);
304 g_source_unref (source);
306 g_object_unref (addr);
308 data->loop = g_main_loop_new (NULL, TRUE);
309 g_main_loop_run (data->loop);
310 g_main_loop_unref (data->loop);
312 g_socket_shutdown (client, FALSE, TRUE, &error);
313 g_assert_no_error (error);
315 g_thread_join (data->thread);
317 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
318 g_assert_no_error (error);
319 g_assert_cmpint (len, ==, 0);
321 g_socket_close (client, &error);
322 g_assert_no_error (error);
323 g_socket_close (data->server, &error);
324 g_assert_no_error (error);
326 g_object_unref (data->server);
327 g_object_unref (client);
329 g_slice_free (IPTestData, data);
333 test_ipv4_async (void)
335 test_ip_async (G_SOCKET_FAMILY_IPV4);
339 test_ipv6_async (void)
341 test_ip_async (G_SOCKET_FAMILY_IPV6);
345 test_ip_sync (GSocketFamily family)
348 GError *error = NULL;
350 GSocketAddress *addr;
354 data = create_server (family, echo_server_thread, FALSE);
355 addr = g_socket_get_local_address (data->server, &error);
357 client = g_socket_new (family,
358 G_SOCKET_TYPE_STREAM,
359 G_SOCKET_PROTOCOL_DEFAULT,
361 g_assert_no_error (error);
363 g_assert_cmpint (g_socket_get_family (client), ==, family);
364 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
365 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
367 g_socket_set_blocking (client, TRUE);
368 g_socket_set_timeout (client, 1);
370 g_socket_connect (client, addr, NULL, &error);
371 g_assert_no_error (error);
372 g_assert (g_socket_is_connected (client));
373 g_object_unref (addr);
375 /* This adds 1 second to "make check", so let's just only do it once. */
376 if (family == G_SOCKET_FAMILY_IPV4)
378 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
379 g_assert_cmpint (len, ==, -1);
380 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
381 g_clear_error (&error);
384 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
385 g_assert_no_error (error);
386 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
388 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
389 g_assert_no_error (error);
390 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
392 g_assert_cmpstr (testbuf, ==, buf);
394 g_socket_shutdown (client, FALSE, TRUE, &error);
395 g_assert_no_error (error);
397 g_thread_join (data->thread);
399 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
400 g_assert_no_error (error);
401 g_assert_cmpint (len, ==, 0);
403 g_socket_close (client, &error);
404 g_assert_no_error (error);
405 g_socket_close (data->server, &error);
406 g_assert_no_error (error);
408 g_object_unref (data->server);
409 g_object_unref (client);
411 g_slice_free (IPTestData, data);
415 test_ipv4_sync (void)
417 test_ip_sync (G_SOCKET_FAMILY_IPV4);
421 test_ipv6_sync (void)
423 test_ip_sync (G_SOCKET_FAMILY_IPV6);
427 graceful_server_thread (gpointer user_data)
429 IPTestData *data = user_data;
431 GError *error = NULL;
434 sock = g_socket_accept (data->server, NULL, &error);
435 g_assert_no_error (error);
437 len = g_socket_send (sock, testbuf, strlen (testbuf) + 1, NULL, &error);
438 g_assert_no_error (error);
439 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
445 test_close_graceful (void)
447 GSocketFamily family = G_SOCKET_FAMILY_IPV4;
449 GError *error = NULL;
450 GSocket *client, *server;
451 GSocketAddress *addr;
455 data = create_server (family, graceful_server_thread, FALSE);
456 addr = g_socket_get_local_address (data->server, &error);
458 client = g_socket_new (family,
459 G_SOCKET_TYPE_STREAM,
460 G_SOCKET_PROTOCOL_DEFAULT,
462 g_assert_no_error (error);
464 g_assert_cmpint (g_socket_get_family (client), ==, family);
465 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
466 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
468 g_socket_set_blocking (client, TRUE);
469 g_socket_set_timeout (client, 1);
471 g_socket_connect (client, addr, NULL, &error);
472 g_assert_no_error (error);
473 g_assert (g_socket_is_connected (client));
474 g_object_unref (addr);
476 server = g_thread_join (data->thread);
478 /* similar to g_tcp_connection_set_graceful_disconnect(), but explicit */
479 g_socket_shutdown (server, FALSE, TRUE, &error);
480 g_assert_no_error (error);
482 /* we must timeout */
483 g_socket_condition_wait (client, G_IO_HUP, NULL, &error);
484 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
485 g_clear_error (&error);
487 /* check that the remaining data is received */
488 len = g_socket_receive (client, buf, strlen (testbuf) + 1, NULL, &error);
489 g_assert_no_error (error);
490 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
492 /* and only then the connection is closed */
493 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
494 g_assert_no_error (error);
495 g_assert_cmpint (len, ==, 0);
497 g_socket_close (server, &error);
498 g_assert_no_error (error);
500 g_socket_close (client, &error);
501 g_assert_no_error (error);
503 g_object_unref (server);
504 g_object_unref (data->server);
505 g_object_unref (client);
507 g_slice_free (IPTestData, data);
510 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
512 v4mapped_server_thread (gpointer user_data)
514 IPTestData *data = user_data;
516 GError *error = NULL;
517 GSocketAddress *addr;
519 sock = g_socket_accept (data->server, NULL, &error);
520 g_assert_no_error (error);
522 g_assert_cmpint (g_socket_get_family (sock), ==, G_SOCKET_FAMILY_IPV6);
524 addr = g_socket_get_local_address (sock, &error);
525 g_assert_no_error (error);
526 g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
527 g_object_unref (addr);
529 addr = g_socket_get_remote_address (sock, &error);
530 g_assert_no_error (error);
531 g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
532 g_object_unref (addr);
534 g_socket_close (sock, &error);
535 g_assert_no_error (error);
536 g_object_unref (sock);
541 test_ipv6_v4mapped (void)
544 GError *error = NULL;
546 GSocketAddress *addr, *v4addr;
549 data = create_server (G_SOCKET_FAMILY_IPV6, v4mapped_server_thread, TRUE);
551 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
552 G_SOCKET_TYPE_STREAM,
553 G_SOCKET_PROTOCOL_DEFAULT,
555 g_assert_no_error (error);
557 g_socket_set_blocking (client, TRUE);
558 g_socket_set_timeout (client, 1);
560 addr = g_socket_get_local_address (data->server, &error);
561 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
562 v4addr = g_inet_socket_address_new (iaddr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
563 g_object_unref (iaddr);
564 g_object_unref (addr);
566 g_socket_connect (client, v4addr, NULL, &error);
567 g_assert_no_error (error);
568 g_assert (g_socket_is_connected (client));
570 g_thread_join (data->thread);
572 g_socket_close (client, &error);
573 g_assert_no_error (error);
574 g_socket_close (data->server, &error);
575 g_assert_no_error (error);
577 g_object_unref (data->server);
578 g_object_unref (client);
579 g_object_unref (v4addr);
581 g_slice_free (IPTestData, data);
586 test_timed_wait (void)
589 GError *error = NULL;
591 GSocketAddress *addr;
595 data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE);
596 addr = g_socket_get_local_address (data->server, &error);
598 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
599 G_SOCKET_TYPE_STREAM,
600 G_SOCKET_PROTOCOL_DEFAULT,
602 g_assert_no_error (error);
604 g_socket_set_blocking (client, TRUE);
605 g_socket_set_timeout (client, 1);
607 g_socket_connect (client, addr, NULL, &error);
608 g_assert_no_error (error);
609 g_object_unref (addr);
611 start_time = g_get_monotonic_time ();
612 g_socket_condition_timed_wait (client, G_IO_IN, 100000 /* 100 ms */,
614 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
615 g_clear_error (&error);
616 poll_duration = g_get_monotonic_time () - start_time;
618 g_assert_cmpint (poll_duration, >=, 98000);
619 g_assert_cmpint (poll_duration, <, 112000);
621 g_socket_close (client, &error);
622 g_assert_no_error (error);
624 g_thread_join (data->thread);
626 g_socket_close (data->server, &error);
627 g_assert_no_error (error);
629 g_object_unref (data->server);
630 g_object_unref (client);
632 g_slice_free (IPTestData, data);
638 struct sockaddr_in6 sin6, gsin6;
639 GSocketAddress *saddr;
640 GInetSocketAddress *isaddr;
642 GError *error = NULL;
644 memset (&sin6, 0, sizeof (sin6));
645 sin6.sin6_family = AF_INET6;
646 sin6.sin6_addr = in6addr_loopback;
647 sin6.sin6_port = g_htons (42);
648 sin6.sin6_scope_id = g_htonl (17);
649 sin6.sin6_flowinfo = g_htonl (1729);
651 saddr = g_socket_address_new_from_native (&sin6, sizeof (sin6));
652 g_assert (G_IS_INET_SOCKET_ADDRESS (saddr));
654 isaddr = G_INET_SOCKET_ADDRESS (saddr);
655 iaddr = g_inet_socket_address_get_address (isaddr);
656 g_assert_cmpint (g_inet_address_get_family (iaddr), ==, G_SOCKET_FAMILY_IPV6);
657 g_assert (g_inet_address_get_is_loopback (iaddr));
659 g_assert_cmpint (g_inet_socket_address_get_port (isaddr), ==, 42);
660 g_assert_cmpint (g_inet_socket_address_get_scope_id (isaddr), ==, 17);
661 g_assert_cmpint (g_inet_socket_address_get_flowinfo (isaddr), ==, 1729);
663 g_socket_address_to_native (saddr, &gsin6, sizeof (gsin6), &error);
664 g_assert_no_error (error);
666 g_assert (memcmp (&sin6.sin6_addr, &gsin6.sin6_addr, sizeof (struct in6_addr)) == 0);
667 g_assert_cmpint (sin6.sin6_port, ==, gsin6.sin6_port);
668 g_assert_cmpint (sin6.sin6_scope_id, ==, gsin6.sin6_scope_id);
669 g_assert_cmpint (sin6.sin6_flowinfo, ==, gsin6.sin6_flowinfo);
671 g_object_unref (saddr);
676 test_unix_from_fd (void)
682 fd = socket (AF_UNIX, SOCK_STREAM, 0);
683 g_assert_cmpint (fd, !=, -1);
686 s = g_socket_new_from_fd (fd, &error);
687 g_assert_no_error (error);
688 g_assert_cmpint (g_socket_get_family (s), ==, G_SOCKET_FAMILY_UNIX);
689 g_assert_cmpint (g_socket_get_socket_type (s), ==, G_SOCKET_TYPE_STREAM);
690 g_assert_cmpint (g_socket_get_protocol (s), ==, G_SOCKET_PROTOCOL_DEFAULT);
695 test_unix_connection (void)
700 GSocketConnection *c;
702 fd = socket (AF_UNIX, SOCK_STREAM, 0);
703 g_assert_cmpint (fd, !=, -1);
706 s = g_socket_new_from_fd (fd, &error);
707 g_assert_no_error (error);
708 c = g_socket_connection_factory_create_connection (s);
709 g_assert (G_IS_UNIX_CONNECTION (c));
714 static GSocketConnection *
715 create_connection_for_fd (int fd)
719 GSocketConnection *connection;
721 socket = g_socket_new_from_fd (fd, &err);
722 g_assert_no_error (err);
723 g_assert (G_IS_SOCKET (socket));
724 connection = g_socket_connection_factory_create_connection (socket);
725 g_assert (G_IS_UNIX_CONNECTION (connection));
726 g_object_unref (socket);
730 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
733 test_unix_connection_ancillary_data (void)
737 gint status, fd, len;
742 g_assert_cmpint (status, ==, 0);
744 status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
745 g_assert_cmpint (status, ==, 0);
748 g_assert_cmpint (pid, >=, 0);
750 /* Child: close its copy of the write end of the pipe, receive it
751 * again from the parent over the socket, and write some text to it.
753 * Parent: send the write end of the pipe (still open for the
754 * parent) over the socket, close it, and read some text from the
755 * read end of the pipe.
759 GSocketConnection *connection;
762 connection = create_connection_for_fd (sv[0]);
764 status = close (pv[1]);
765 g_assert_cmpint (status, ==, 0);
768 fd = g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection), NULL,
770 g_assert_no_error (err);
771 g_assert_cmpint (fd, >, -1);
772 g_object_unref (connection);
775 len = write (fd, TEST_DATA, sizeof (TEST_DATA));
776 while (len == -1 && errno == EINTR);
777 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
782 GSocketConnection *connection;
785 connection = create_connection_for_fd (sv[1]);
788 g_unix_connection_send_fd (G_UNIX_CONNECTION (connection), pv[1], NULL,
790 g_assert_no_error (err);
791 g_object_unref (connection);
793 status = close (pv[1]);
794 g_assert_cmpint (status, ==, 0);
796 memset (buffer, 0xff, sizeof buffer);
798 len = read (pv[0], buffer, sizeof buffer);
799 while (len == -1 && errno == EINTR);
801 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
802 g_assert_cmpstr (buffer, ==, TEST_DATA);
804 waitpid (pid, &status, 0);
805 g_assert (WIFEXITED (status));
806 g_assert_cmpint (WEXITSTATUS (status), ==, 0);
809 /* TODO: add test for g_unix_connection_send_credentials() and
810 * g_unix_connection_receive_credentials().
813 #endif /* G_OS_UNIX */
819 g_test_init (&argc, &argv, NULL);
821 g_test_add_func ("/socket/ipv4_sync", test_ipv4_sync);
822 g_test_add_func ("/socket/ipv4_async", test_ipv4_async);
823 g_test_add_func ("/socket/ipv6_sync", test_ipv6_sync);
824 g_test_add_func ("/socket/ipv6_async", test_ipv6_async);
825 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
826 g_test_add_func ("/socket/ipv6_v4mapped", test_ipv6_v4mapped);
828 g_test_add_func ("/socket/close_graceful", test_close_graceful);
829 g_test_add_func ("/socket/timed_wait", test_timed_wait);
830 g_test_add_func ("/socket/address", test_sockaddr);
832 g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
833 g_test_add_func ("/socket/unix-connection", test_unix_connection);
834 g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data);