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, see <http://www.gnu.org/licenses/>.
26 #include <gio/gnetworking.h>
27 #include <gio/gunixconnection.h>
30 #include "gnetworkingprivate.h"
41 echo_server_thread (gpointer user_data)
43 IPTestData *data = user_data;
49 sock = g_socket_accept (data->server, NULL, &error);
50 g_assert_no_error (error);
54 nread = g_socket_receive (sock, buf, sizeof (buf), NULL, &error);
55 g_assert_no_error (error);
56 g_assert_cmpint (nread, >=, 0);
61 nwrote = g_socket_send (sock, buf, nread, NULL, &error);
62 g_assert_no_error (error);
63 g_assert_cmpint (nwrote, ==, nread);
66 g_socket_close (sock, &error);
67 g_assert_no_error (error);
68 g_object_unref (sock);
73 create_server (GSocketFamily family,
74 GThreadFunc server_thread,
83 data = g_slice_new (IPTestData);
84 data->family = family;
86 data->server = server = g_socket_new (family,
88 G_SOCKET_PROTOCOL_DEFAULT,
90 g_assert_no_error (error);
92 g_assert_cmpint (g_socket_get_family (server), ==, family);
93 g_assert_cmpint (g_socket_get_socket_type (server), ==, G_SOCKET_TYPE_STREAM);
94 g_assert_cmpint (g_socket_get_protocol (server), ==, G_SOCKET_PROTOCOL_DEFAULT);
96 g_socket_set_blocking (server, TRUE);
98 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
101 g_socket_set_option (data->server, IPPROTO_IPV6, IPV6_V6ONLY, FALSE, NULL);
102 if (! g_socket_speaks_ipv4 (data->server))
104 g_object_unref (data->server);
105 g_slice_free (IPTestData, data);
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);
553 g_test_message ("Test not run: not supported by the OS");
557 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
558 G_SOCKET_TYPE_STREAM,
559 G_SOCKET_PROTOCOL_DEFAULT,
561 g_assert_no_error (error);
563 g_socket_set_blocking (client, TRUE);
564 g_socket_set_timeout (client, 1);
566 addr = g_socket_get_local_address (data->server, &error);
567 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
568 v4addr = g_inet_socket_address_new (iaddr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
569 g_object_unref (iaddr);
570 g_object_unref (addr);
572 g_socket_connect (client, v4addr, NULL, &error);
573 g_assert_no_error (error);
574 g_assert (g_socket_is_connected (client));
576 g_thread_join (data->thread);
578 g_socket_close (client, &error);
579 g_assert_no_error (error);
580 g_socket_close (data->server, &error);
581 g_assert_no_error (error);
583 g_object_unref (data->server);
584 g_object_unref (client);
585 g_object_unref (v4addr);
587 g_slice_free (IPTestData, data);
592 test_timed_wait (void)
595 GError *error = NULL;
597 GSocketAddress *addr;
601 data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE);
602 addr = g_socket_get_local_address (data->server, &error);
604 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
605 G_SOCKET_TYPE_STREAM,
606 G_SOCKET_PROTOCOL_DEFAULT,
608 g_assert_no_error (error);
610 g_socket_set_blocking (client, TRUE);
611 g_socket_set_timeout (client, 1);
613 g_socket_connect (client, addr, NULL, &error);
614 g_assert_no_error (error);
615 g_object_unref (addr);
617 start_time = g_get_monotonic_time ();
618 g_socket_condition_timed_wait (client, G_IO_IN, 100000 /* 100 ms */,
620 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
621 g_clear_error (&error);
622 poll_duration = g_get_monotonic_time () - start_time;
624 g_assert_cmpint (poll_duration, >=, 98000);
625 g_assert_cmpint (poll_duration, <, 112000);
627 g_socket_close (client, &error);
628 g_assert_no_error (error);
630 g_thread_join (data->thread);
632 g_socket_close (data->server, &error);
633 g_assert_no_error (error);
635 g_object_unref (data->server);
636 g_object_unref (client);
638 g_slice_free (IPTestData, data);
644 struct sockaddr_in6 sin6, gsin6;
645 GSocketAddress *saddr;
646 GInetSocketAddress *isaddr;
648 GError *error = NULL;
650 memset (&sin6, 0, sizeof (sin6));
651 sin6.sin6_family = AF_INET6;
652 sin6.sin6_addr = in6addr_loopback;
653 sin6.sin6_port = g_htons (42);
654 sin6.sin6_scope_id = 17;
655 sin6.sin6_flowinfo = 1729;
657 saddr = g_socket_address_new_from_native (&sin6, sizeof (sin6));
658 g_assert (G_IS_INET_SOCKET_ADDRESS (saddr));
660 isaddr = G_INET_SOCKET_ADDRESS (saddr);
661 iaddr = g_inet_socket_address_get_address (isaddr);
662 g_assert_cmpint (g_inet_address_get_family (iaddr), ==, G_SOCKET_FAMILY_IPV6);
663 g_assert (g_inet_address_get_is_loopback (iaddr));
665 g_assert_cmpint (g_inet_socket_address_get_port (isaddr), ==, 42);
666 g_assert_cmpint (g_inet_socket_address_get_scope_id (isaddr), ==, 17);
667 g_assert_cmpint (g_inet_socket_address_get_flowinfo (isaddr), ==, 1729);
669 g_socket_address_to_native (saddr, &gsin6, sizeof (gsin6), &error);
670 g_assert_no_error (error);
672 g_assert (memcmp (&sin6.sin6_addr, &gsin6.sin6_addr, sizeof (struct in6_addr)) == 0);
673 g_assert_cmpint (sin6.sin6_port, ==, gsin6.sin6_port);
674 g_assert_cmpint (sin6.sin6_scope_id, ==, gsin6.sin6_scope_id);
675 g_assert_cmpint (sin6.sin6_flowinfo, ==, gsin6.sin6_flowinfo);
677 g_object_unref (saddr);
682 test_unix_from_fd (void)
688 fd = socket (AF_UNIX, SOCK_STREAM, 0);
689 g_assert_cmpint (fd, !=, -1);
692 s = g_socket_new_from_fd (fd, &error);
693 g_assert_no_error (error);
694 g_assert_cmpint (g_socket_get_family (s), ==, G_SOCKET_FAMILY_UNIX);
695 g_assert_cmpint (g_socket_get_socket_type (s), ==, G_SOCKET_TYPE_STREAM);
696 g_assert_cmpint (g_socket_get_protocol (s), ==, G_SOCKET_PROTOCOL_DEFAULT);
701 test_unix_connection (void)
706 GSocketConnection *c;
708 fd = socket (AF_UNIX, SOCK_STREAM, 0);
709 g_assert_cmpint (fd, !=, -1);
712 s = g_socket_new_from_fd (fd, &error);
713 g_assert_no_error (error);
714 c = g_socket_connection_factory_create_connection (s);
715 g_assert (G_IS_UNIX_CONNECTION (c));
720 static GSocketConnection *
721 create_connection_for_fd (int fd)
725 GSocketConnection *connection;
727 socket = g_socket_new_from_fd (fd, &err);
728 g_assert_no_error (err);
729 g_assert (G_IS_SOCKET (socket));
730 connection = g_socket_connection_factory_create_connection (socket);
731 g_assert (G_IS_UNIX_CONNECTION (connection));
732 g_object_unref (socket);
736 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
739 test_unix_connection_ancillary_data (void)
743 gint status, fd, len;
748 g_assert_cmpint (status, ==, 0);
750 status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
751 g_assert_cmpint (status, ==, 0);
754 g_assert_cmpint (pid, >=, 0);
756 /* Child: close its copy of the write end of the pipe, receive it
757 * again from the parent over the socket, and write some text to it.
759 * Parent: send the write end of the pipe (still open for the
760 * parent) over the socket, close it, and read some text from the
761 * read end of the pipe.
765 GSocketConnection *connection;
768 connection = create_connection_for_fd (sv[0]);
770 status = close (pv[1]);
771 g_assert_cmpint (status, ==, 0);
774 fd = g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection), NULL,
776 g_assert_no_error (err);
777 g_assert_cmpint (fd, >, -1);
778 g_object_unref (connection);
781 len = write (fd, TEST_DATA, sizeof (TEST_DATA));
782 while (len == -1 && errno == EINTR);
783 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
788 GSocketConnection *connection;
791 connection = create_connection_for_fd (sv[1]);
794 g_unix_connection_send_fd (G_UNIX_CONNECTION (connection), pv[1], NULL,
796 g_assert_no_error (err);
797 g_object_unref (connection);
799 status = close (pv[1]);
800 g_assert_cmpint (status, ==, 0);
802 memset (buffer, 0xff, sizeof buffer);
804 len = read (pv[0], buffer, sizeof buffer);
805 while (len == -1 && errno == EINTR);
807 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
808 g_assert_cmpstr (buffer, ==, TEST_DATA);
810 waitpid (pid, &status, 0);
811 g_assert (WIFEXITED (status));
812 g_assert_cmpint (WEXITSTATUS (status), ==, 0);
815 /* TODO: add test for g_unix_connection_send_credentials() and
816 * g_unix_connection_receive_credentials().
819 #endif /* G_OS_UNIX */
822 test_reuse_tcp (void)
824 GSocket *sock1, *sock2;
825 GError *error = NULL;
827 GSocketAddress *addr;
829 sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
830 G_SOCKET_TYPE_STREAM,
831 G_SOCKET_PROTOCOL_DEFAULT,
833 g_assert_no_error (error);
835 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
836 addr = g_inet_socket_address_new (iaddr, 0);
837 g_object_unref (iaddr);
838 g_socket_bind (sock1, addr, TRUE, &error);
839 g_object_unref (addr);
840 g_assert_no_error (error);
842 g_socket_listen (sock1, &error);
843 g_assert_no_error (error);
845 sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
846 G_SOCKET_TYPE_STREAM,
847 G_SOCKET_PROTOCOL_DEFAULT,
849 g_assert_no_error (error);
851 addr = g_socket_get_local_address (sock1, &error);
852 g_assert_no_error (error);
853 g_socket_bind (sock2, addr, TRUE, &error);
854 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_ADDRESS_IN_USE);
855 g_object_unref (addr);
857 g_object_unref (sock1);
858 g_object_unref (sock2);
862 test_reuse_udp (void)
864 GSocket *sock1, *sock2;
865 GError *error = NULL;
867 GSocketAddress *addr;
869 sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
870 G_SOCKET_TYPE_DATAGRAM,
871 G_SOCKET_PROTOCOL_DEFAULT,
873 g_assert_no_error (error);
875 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
876 addr = g_inet_socket_address_new (iaddr, 0);
877 g_object_unref (iaddr);
878 g_socket_bind (sock1, addr, TRUE, &error);
879 g_object_unref (addr);
880 g_assert_no_error (error);
882 sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
883 G_SOCKET_TYPE_DATAGRAM,
884 G_SOCKET_PROTOCOL_DEFAULT,
886 g_assert_no_error (error);
888 addr = g_socket_get_local_address (sock1, &error);
889 g_assert_no_error (error);
890 g_socket_bind (sock2, addr, TRUE, &error);
891 g_object_unref (addr);
892 g_assert_no_error (error);
894 g_object_unref (sock1);
895 g_object_unref (sock2);
899 test_get_available (gconstpointer user_data)
901 GSocketType socket_type = GPOINTER_TO_UINT (user_data);
903 GSocket *listener, *server, *client;
905 GSocketAddress *saddr;
906 gchar data[] = "0123456789abcdef";
910 listener = g_socket_new (G_SOCKET_FAMILY_IPV4,
912 G_SOCKET_PROTOCOL_DEFAULT,
914 g_assert_no_error (err);
915 g_assert (G_IS_SOCKET (listener));
917 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
919 G_SOCKET_PROTOCOL_DEFAULT,
921 g_assert_no_error (err);
922 g_assert (G_IS_SOCKET (client));
924 if (socket_type == G_SOCKET_TYPE_STREAM)
926 g_socket_set_option (client, IPPROTO_TCP, TCP_NODELAY, TRUE, &err);
927 g_assert_no_error (err);
930 addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
931 saddr = g_inet_socket_address_new (addr, 0);
933 g_socket_bind (listener, saddr, TRUE, &err);
934 g_assert_no_error (err);
935 g_object_unref (saddr);
936 g_object_unref (addr);
938 saddr = g_socket_get_local_address (listener, &err);
939 g_assert_no_error (err);
941 if (socket_type == G_SOCKET_TYPE_STREAM)
943 g_socket_listen (listener, &err);
944 g_assert_no_error (err);
945 g_socket_connect (client, saddr, NULL, &err);
946 g_assert_no_error (err);
948 server = g_socket_accept (listener, NULL, &err);
949 g_assert_no_error (err);
950 g_socket_set_blocking (server, FALSE);
951 g_object_unref (listener);
956 g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
957 g_assert_no_error (err);
959 while (!g_socket_condition_wait (server, G_IO_IN, NULL, NULL))
961 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
963 g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
964 g_assert_no_error (err);
966 /* We need to wait until the data has actually been copied into the
967 * server socket's buffers, but g_socket_condition_wait() won't help
968 * here since the socket is definitely already readable. So there's
969 * a race condition in checking its available bytes. In the TCP
970 * case, we poll for a bit until the new data shows up. In the UDP
971 * case, there's not much we can do, but at least the failure mode
972 * is passes-when-it-shouldn't, not fails-when-it-shouldn't.
974 if (socket_type == G_SOCKET_TYPE_STREAM)
978 for (tries = 0; tries < 100; tries++)
980 if (g_socket_get_available_bytes (server) > sizeof (data))
985 g_assert_cmpint (g_socket_get_available_bytes (server), ==, 2 * sizeof (data));
990 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
993 g_assert_cmpint (sizeof (buf), >=, 2 * sizeof (data));
994 nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
995 g_assert_no_error (err);
997 if (socket_type == G_SOCKET_TYPE_STREAM)
999 g_assert_cmpint (nread, ==, 2 * sizeof (data));
1000 g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1004 g_assert_cmpint (nread, ==, sizeof (data));
1005 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1008 nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
1009 if (socket_type == G_SOCKET_TYPE_STREAM)
1011 g_assert_cmpint (nread, ==, -1);
1012 g_assert_error (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
1013 g_clear_error (&err);
1017 g_assert_cmpint (nread, ==, sizeof (data));
1018 g_assert_no_error (err);
1021 g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1023 g_socket_close (server, &err);
1024 g_assert_no_error (err);
1026 g_object_unref (saddr);
1027 g_object_unref (server);
1028 g_object_unref (client);
1035 g_test_init (&argc, &argv, NULL);
1037 g_test_add_func ("/socket/ipv4_sync", test_ipv4_sync);
1038 g_test_add_func ("/socket/ipv4_async", test_ipv4_async);
1039 g_test_add_func ("/socket/ipv6_sync", test_ipv6_sync);
1040 g_test_add_func ("/socket/ipv6_async", test_ipv6_async);
1041 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
1042 g_test_add_func ("/socket/ipv6_v4mapped", test_ipv6_v4mapped);
1044 g_test_add_func ("/socket/close_graceful", test_close_graceful);
1045 g_test_add_func ("/socket/timed_wait", test_timed_wait);
1046 g_test_add_func ("/socket/address", test_sockaddr);
1048 g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
1049 g_test_add_func ("/socket/unix-connection", test_unix_connection);
1050 g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data);
1052 g_test_add_func ("/socket/reuse/tcp", test_reuse_tcp);
1053 g_test_add_func ("/socket/reuse/udp", test_reuse_udp);
1054 g_test_add_data_func ("/socket/get_available/datagram", GUINT_TO_POINTER (G_SOCKET_TYPE_DATAGRAM),
1055 test_get_available);
1056 g_test_add_data_func ("/socket/get_available/stream", GUINT_TO_POINTER (G_SOCKET_TYPE_STREAM),
1057 test_get_available);
1059 return g_test_run();