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"
32 static gboolean ipv6_supported;
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)
103 g_socket_set_option (data->server, IPPROTO_IPV6, IPV6_V6ONLY, FALSE, NULL);
104 if (! g_socket_speaks_ipv4 (data->server))
106 g_object_unref (data->server);
107 g_slice_free (IPTestData, data);
114 iaddr = g_inet_address_new_any (family);
116 iaddr = g_inet_address_new_loopback (family);
117 addr = g_inet_socket_address_new (iaddr, 0);
118 g_object_unref (iaddr);
120 g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), ==, 0);
121 g_socket_bind (server, addr, TRUE, &error);
122 g_assert_no_error (error);
123 g_object_unref (addr);
125 addr = g_socket_get_local_address (server, &error);
126 g_assert_no_error (error);
127 g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), !=, 0);
128 g_object_unref (addr);
130 g_socket_listen (server, &error);
131 g_assert_no_error (error);
133 data->thread = g_thread_new ("server", server_thread, data);
138 static const gchar *testbuf = "0123456789abcdef";
141 test_ip_async_read_ready (GSocket *client,
145 IPTestData *data = user_data;
146 GError *error = NULL;
150 g_assert_cmpint (cond, ==, G_IO_IN);
152 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
153 g_assert_no_error (error);
154 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
156 g_assert_cmpstr (testbuf, ==, buf);
158 g_main_loop_quit (data->loop);
164 test_ip_async_write_ready (GSocket *client,
168 IPTestData *data = user_data;
169 GError *error = NULL;
173 g_assert_cmpint (cond, ==, G_IO_OUT);
175 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
176 g_assert_no_error (error);
177 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
179 source = g_socket_create_source (client, G_IO_IN, NULL);
180 g_source_set_callback (source, (GSourceFunc)test_ip_async_read_ready,
182 g_source_attach (source, NULL);
183 g_source_unref (source);
189 test_ip_async_timed_out (GSocket *client,
193 IPTestData *data = user_data;
194 GError *error = NULL;
199 if (data->family == G_SOCKET_FAMILY_IPV4)
201 g_assert_cmpint (cond, ==, G_IO_IN);
202 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
203 g_assert_cmpint (len, ==, -1);
204 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
205 g_clear_error (&error);
208 source = g_socket_create_source (client, G_IO_OUT, NULL);
209 g_source_set_callback (source, (GSourceFunc)test_ip_async_write_ready,
211 g_source_attach (source, NULL);
212 g_source_unref (source);
218 test_ip_async_connected (GSocket *client,
222 IPTestData *data = user_data;
223 GError *error = NULL;
228 g_socket_check_connect_result (client, &error);
229 g_assert_no_error (error);
230 /* We do this after the check_connect_result, since that will give a
231 * more useful assertion in case of error.
233 g_assert_cmpint (cond, ==, G_IO_OUT);
235 g_assert (g_socket_is_connected (client));
237 /* This adds 1 second to "make check", so let's just only do it once. */
238 if (data->family == G_SOCKET_FAMILY_IPV4)
240 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
241 g_assert_cmpint (len, ==, -1);
242 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
243 g_clear_error (&error);
245 source = g_socket_create_source (client, G_IO_IN, NULL);
246 g_source_set_callback (source, (GSourceFunc)test_ip_async_timed_out,
248 g_source_attach (source, NULL);
249 g_source_unref (source);
252 test_ip_async_timed_out (client, 0, data);
258 idle_test_ip_async_connected (gpointer user_data)
260 IPTestData *data = user_data;
262 return test_ip_async_connected (data->client, G_IO_OUT, data);
266 test_ip_async (GSocketFamily family)
269 GError *error = NULL;
271 GSocketAddress *addr;
276 data = create_server (family, echo_server_thread, FALSE);
277 addr = g_socket_get_local_address (data->server, &error);
279 client = g_socket_new (family,
280 G_SOCKET_TYPE_STREAM,
281 G_SOCKET_PROTOCOL_DEFAULT,
283 g_assert_no_error (error);
284 data->client = client;
286 g_assert_cmpint (g_socket_get_family (client), ==, family);
287 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
288 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
290 g_socket_set_blocking (client, FALSE);
291 g_socket_set_timeout (client, 1);
293 if (g_socket_connect (client, addr, NULL, &error))
295 g_assert_no_error (error);
296 g_idle_add (idle_test_ip_async_connected, data);
300 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
301 g_clear_error (&error);
302 source = g_socket_create_source (client, G_IO_OUT, NULL);
303 g_source_set_callback (source, (GSourceFunc)test_ip_async_connected,
305 g_source_attach (source, NULL);
306 g_source_unref (source);
308 g_object_unref (addr);
310 data->loop = g_main_loop_new (NULL, TRUE);
311 g_main_loop_run (data->loop);
312 g_main_loop_unref (data->loop);
314 g_socket_shutdown (client, FALSE, TRUE, &error);
315 g_assert_no_error (error);
317 g_thread_join (data->thread);
319 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
320 g_assert_no_error (error);
321 g_assert_cmpint (len, ==, 0);
323 g_socket_close (client, &error);
324 g_assert_no_error (error);
325 g_socket_close (data->server, &error);
326 g_assert_no_error (error);
328 g_object_unref (data->server);
329 g_object_unref (client);
331 g_slice_free (IPTestData, data);
335 test_ipv4_async (void)
337 test_ip_async (G_SOCKET_FAMILY_IPV4);
341 test_ipv6_async (void)
345 g_test_skip ("No support for IPv6");
349 test_ip_async (G_SOCKET_FAMILY_IPV6);
353 test_ip_sync (GSocketFamily family)
356 GError *error = NULL;
358 GSocketAddress *addr;
362 data = create_server (family, echo_server_thread, FALSE);
363 addr = g_socket_get_local_address (data->server, &error);
365 client = g_socket_new (family,
366 G_SOCKET_TYPE_STREAM,
367 G_SOCKET_PROTOCOL_DEFAULT,
369 g_assert_no_error (error);
371 g_assert_cmpint (g_socket_get_family (client), ==, family);
372 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
373 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
375 g_socket_set_blocking (client, TRUE);
376 g_socket_set_timeout (client, 1);
378 g_socket_connect (client, addr, NULL, &error);
379 g_assert_no_error (error);
380 g_assert (g_socket_is_connected (client));
381 g_object_unref (addr);
383 /* This adds 1 second to "make check", so let's just only do it once. */
384 if (family == G_SOCKET_FAMILY_IPV4)
386 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
387 g_assert_cmpint (len, ==, -1);
388 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
389 g_clear_error (&error);
392 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
393 g_assert_no_error (error);
394 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
396 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
397 g_assert_no_error (error);
398 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
400 g_assert_cmpstr (testbuf, ==, buf);
402 g_socket_shutdown (client, FALSE, TRUE, &error);
403 g_assert_no_error (error);
405 g_thread_join (data->thread);
407 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
408 g_assert_no_error (error);
409 g_assert_cmpint (len, ==, 0);
411 g_socket_close (client, &error);
412 g_assert_no_error (error);
413 g_socket_close (data->server, &error);
414 g_assert_no_error (error);
416 g_object_unref (data->server);
417 g_object_unref (client);
419 g_slice_free (IPTestData, data);
423 test_ipv4_sync (void)
425 test_ip_sync (G_SOCKET_FAMILY_IPV4);
429 test_ipv6_sync (void)
433 g_test_skip ("No support for IPv6");
437 test_ip_sync (G_SOCKET_FAMILY_IPV6);
441 graceful_server_thread (gpointer user_data)
443 IPTestData *data = user_data;
445 GError *error = NULL;
448 sock = g_socket_accept (data->server, NULL, &error);
449 g_assert_no_error (error);
451 len = g_socket_send (sock, testbuf, strlen (testbuf) + 1, NULL, &error);
452 g_assert_no_error (error);
453 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
459 test_close_graceful (void)
461 GSocketFamily family = G_SOCKET_FAMILY_IPV4;
463 GError *error = NULL;
464 GSocket *client, *server;
465 GSocketAddress *addr;
469 data = create_server (family, graceful_server_thread, FALSE);
470 addr = g_socket_get_local_address (data->server, &error);
472 client = g_socket_new (family,
473 G_SOCKET_TYPE_STREAM,
474 G_SOCKET_PROTOCOL_DEFAULT,
476 g_assert_no_error (error);
478 g_assert_cmpint (g_socket_get_family (client), ==, family);
479 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
480 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
482 g_socket_set_blocking (client, TRUE);
483 g_socket_set_timeout (client, 1);
485 g_socket_connect (client, addr, NULL, &error);
486 g_assert_no_error (error);
487 g_assert (g_socket_is_connected (client));
488 g_object_unref (addr);
490 server = g_thread_join (data->thread);
492 /* similar to g_tcp_connection_set_graceful_disconnect(), but explicit */
493 g_socket_shutdown (server, FALSE, TRUE, &error);
494 g_assert_no_error (error);
496 /* we must timeout */
497 g_socket_condition_wait (client, G_IO_HUP, NULL, &error);
498 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
499 g_clear_error (&error);
501 /* check that the remaining data is received */
502 len = g_socket_receive (client, buf, strlen (testbuf) + 1, NULL, &error);
503 g_assert_no_error (error);
504 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
506 /* and only then the connection is closed */
507 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
508 g_assert_no_error (error);
509 g_assert_cmpint (len, ==, 0);
511 g_socket_close (server, &error);
512 g_assert_no_error (error);
514 g_socket_close (client, &error);
515 g_assert_no_error (error);
517 g_object_unref (server);
518 g_object_unref (data->server);
519 g_object_unref (client);
521 g_slice_free (IPTestData, data);
524 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
526 v4mapped_server_thread (gpointer user_data)
528 IPTestData *data = user_data;
530 GError *error = NULL;
531 GSocketAddress *addr;
533 sock = g_socket_accept (data->server, NULL, &error);
534 g_assert_no_error (error);
536 g_assert_cmpint (g_socket_get_family (sock), ==, G_SOCKET_FAMILY_IPV6);
538 addr = g_socket_get_local_address (sock, &error);
539 g_assert_no_error (error);
540 g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
541 g_object_unref (addr);
543 addr = g_socket_get_remote_address (sock, &error);
544 g_assert_no_error (error);
545 g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
546 g_object_unref (addr);
548 g_socket_close (sock, &error);
549 g_assert_no_error (error);
550 g_object_unref (sock);
555 test_ipv6_v4mapped (void)
558 GError *error = NULL;
560 GSocketAddress *addr, *v4addr;
565 g_test_skip ("No support for IPv6");
569 data = create_server (G_SOCKET_FAMILY_IPV6, v4mapped_server_thread, TRUE);
573 g_test_message ("Test not run: not supported by the OS");
577 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
578 G_SOCKET_TYPE_STREAM,
579 G_SOCKET_PROTOCOL_DEFAULT,
581 g_assert_no_error (error);
583 g_socket_set_blocking (client, TRUE);
584 g_socket_set_timeout (client, 1);
586 addr = g_socket_get_local_address (data->server, &error);
587 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
588 v4addr = g_inet_socket_address_new (iaddr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
589 g_object_unref (iaddr);
590 g_object_unref (addr);
592 g_socket_connect (client, v4addr, NULL, &error);
593 g_assert_no_error (error);
594 g_assert (g_socket_is_connected (client));
596 g_thread_join (data->thread);
598 g_socket_close (client, &error);
599 g_assert_no_error (error);
600 g_socket_close (data->server, &error);
601 g_assert_no_error (error);
603 g_object_unref (data->server);
604 g_object_unref (client);
605 g_object_unref (v4addr);
607 g_slice_free (IPTestData, data);
612 test_timed_wait (void)
615 GError *error = NULL;
617 GSocketAddress *addr;
621 data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE);
622 addr = g_socket_get_local_address (data->server, &error);
624 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
625 G_SOCKET_TYPE_STREAM,
626 G_SOCKET_PROTOCOL_DEFAULT,
628 g_assert_no_error (error);
630 g_socket_set_blocking (client, TRUE);
631 g_socket_set_timeout (client, 1);
633 g_socket_connect (client, addr, NULL, &error);
634 g_assert_no_error (error);
635 g_object_unref (addr);
637 start_time = g_get_monotonic_time ();
638 g_socket_condition_timed_wait (client, G_IO_IN, 100000 /* 100 ms */,
640 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
641 g_clear_error (&error);
642 poll_duration = g_get_monotonic_time () - start_time;
644 g_assert_cmpint (poll_duration, >=, 98000);
645 g_assert_cmpint (poll_duration, <, 112000);
647 g_socket_close (client, &error);
648 g_assert_no_error (error);
650 g_thread_join (data->thread);
652 g_socket_close (data->server, &error);
653 g_assert_no_error (error);
655 g_object_unref (data->server);
656 g_object_unref (client);
658 g_slice_free (IPTestData, data);
664 struct sockaddr_in6 sin6, gsin6;
665 GSocketAddress *saddr;
666 GInetSocketAddress *isaddr;
668 GError *error = NULL;
670 memset (&sin6, 0, sizeof (sin6));
671 sin6.sin6_family = AF_INET6;
672 sin6.sin6_addr = in6addr_loopback;
673 sin6.sin6_port = g_htons (42);
674 sin6.sin6_scope_id = 17;
675 sin6.sin6_flowinfo = 1729;
677 saddr = g_socket_address_new_from_native (&sin6, sizeof (sin6));
678 g_assert (G_IS_INET_SOCKET_ADDRESS (saddr));
680 isaddr = G_INET_SOCKET_ADDRESS (saddr);
681 iaddr = g_inet_socket_address_get_address (isaddr);
682 g_assert_cmpint (g_inet_address_get_family (iaddr), ==, G_SOCKET_FAMILY_IPV6);
683 g_assert (g_inet_address_get_is_loopback (iaddr));
685 g_assert_cmpint (g_inet_socket_address_get_port (isaddr), ==, 42);
686 g_assert_cmpint (g_inet_socket_address_get_scope_id (isaddr), ==, 17);
687 g_assert_cmpint (g_inet_socket_address_get_flowinfo (isaddr), ==, 1729);
689 g_socket_address_to_native (saddr, &gsin6, sizeof (gsin6), &error);
690 g_assert_no_error (error);
692 g_assert (memcmp (&sin6.sin6_addr, &gsin6.sin6_addr, sizeof (struct in6_addr)) == 0);
693 g_assert_cmpint (sin6.sin6_port, ==, gsin6.sin6_port);
694 g_assert_cmpint (sin6.sin6_scope_id, ==, gsin6.sin6_scope_id);
695 g_assert_cmpint (sin6.sin6_flowinfo, ==, gsin6.sin6_flowinfo);
697 g_object_unref (saddr);
702 test_unix_from_fd (void)
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 g_assert_cmpint (g_socket_get_family (s), ==, G_SOCKET_FAMILY_UNIX);
715 g_assert_cmpint (g_socket_get_socket_type (s), ==, G_SOCKET_TYPE_STREAM);
716 g_assert_cmpint (g_socket_get_protocol (s), ==, G_SOCKET_PROTOCOL_DEFAULT);
721 test_unix_connection (void)
726 GSocketConnection *c;
728 fd = socket (AF_UNIX, SOCK_STREAM, 0);
729 g_assert_cmpint (fd, !=, -1);
732 s = g_socket_new_from_fd (fd, &error);
733 g_assert_no_error (error);
734 c = g_socket_connection_factory_create_connection (s);
735 g_assert (G_IS_UNIX_CONNECTION (c));
740 static GSocketConnection *
741 create_connection_for_fd (int fd)
745 GSocketConnection *connection;
747 socket = g_socket_new_from_fd (fd, &err);
748 g_assert_no_error (err);
749 g_assert (G_IS_SOCKET (socket));
750 connection = g_socket_connection_factory_create_connection (socket);
751 g_assert (G_IS_UNIX_CONNECTION (connection));
752 g_object_unref (socket);
756 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
759 test_unix_connection_ancillary_data (void)
763 gint status, fd, len;
768 g_assert_cmpint (status, ==, 0);
770 status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
771 g_assert_cmpint (status, ==, 0);
774 g_assert_cmpint (pid, >=, 0);
776 /* Child: close its copy of the write end of the pipe, receive it
777 * again from the parent over the socket, and write some text to it.
779 * Parent: send the write end of the pipe (still open for the
780 * parent) over the socket, close it, and read some text from the
781 * read end of the pipe.
785 GSocketConnection *connection;
788 connection = create_connection_for_fd (sv[0]);
790 status = close (pv[1]);
791 g_assert_cmpint (status, ==, 0);
794 fd = g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection), NULL,
796 g_assert_no_error (err);
797 g_assert_cmpint (fd, >, -1);
798 g_object_unref (connection);
801 len = write (fd, TEST_DATA, sizeof (TEST_DATA));
802 while (len == -1 && errno == EINTR);
803 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
808 GSocketConnection *connection;
811 connection = create_connection_for_fd (sv[1]);
814 g_unix_connection_send_fd (G_UNIX_CONNECTION (connection), pv[1], NULL,
816 g_assert_no_error (err);
817 g_object_unref (connection);
819 status = close (pv[1]);
820 g_assert_cmpint (status, ==, 0);
822 memset (buffer, 0xff, sizeof buffer);
824 len = read (pv[0], buffer, sizeof buffer);
825 while (len == -1 && errno == EINTR);
827 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
828 g_assert_cmpstr (buffer, ==, TEST_DATA);
830 waitpid (pid, &status, 0);
831 g_assert (WIFEXITED (status));
832 g_assert_cmpint (WEXITSTATUS (status), ==, 0);
835 /* TODO: add test for g_unix_connection_send_credentials() and
836 * g_unix_connection_receive_credentials().
839 #endif /* G_OS_UNIX */
842 test_reuse_tcp (void)
844 GSocket *sock1, *sock2;
845 GError *error = NULL;
847 GSocketAddress *addr;
849 sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
850 G_SOCKET_TYPE_STREAM,
851 G_SOCKET_PROTOCOL_DEFAULT,
853 g_assert_no_error (error);
855 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
856 addr = g_inet_socket_address_new (iaddr, 0);
857 g_object_unref (iaddr);
858 g_socket_bind (sock1, addr, TRUE, &error);
859 g_object_unref (addr);
860 g_assert_no_error (error);
862 g_socket_listen (sock1, &error);
863 g_assert_no_error (error);
865 sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
866 G_SOCKET_TYPE_STREAM,
867 G_SOCKET_PROTOCOL_DEFAULT,
869 g_assert_no_error (error);
871 addr = g_socket_get_local_address (sock1, &error);
872 g_assert_no_error (error);
873 g_socket_bind (sock2, addr, TRUE, &error);
874 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_ADDRESS_IN_USE);
875 g_object_unref (addr);
877 g_object_unref (sock1);
878 g_object_unref (sock2);
882 test_reuse_udp (void)
884 GSocket *sock1, *sock2;
885 GError *error = NULL;
887 GSocketAddress *addr;
889 sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
890 G_SOCKET_TYPE_DATAGRAM,
891 G_SOCKET_PROTOCOL_DEFAULT,
893 g_assert_no_error (error);
895 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
896 addr = g_inet_socket_address_new (iaddr, 0);
897 g_object_unref (iaddr);
898 g_socket_bind (sock1, addr, TRUE, &error);
899 g_object_unref (addr);
900 g_assert_no_error (error);
902 sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
903 G_SOCKET_TYPE_DATAGRAM,
904 G_SOCKET_PROTOCOL_DEFAULT,
906 g_assert_no_error (error);
908 addr = g_socket_get_local_address (sock1, &error);
909 g_assert_no_error (error);
910 g_socket_bind (sock2, addr, TRUE, &error);
911 g_object_unref (addr);
912 g_assert_no_error (error);
914 g_object_unref (sock1);
915 g_object_unref (sock2);
919 test_get_available (gconstpointer user_data)
921 GSocketType socket_type = GPOINTER_TO_UINT (user_data);
923 GSocket *listener, *server, *client;
925 GSocketAddress *saddr;
926 gchar data[] = "0123456789abcdef";
930 listener = g_socket_new (G_SOCKET_FAMILY_IPV4,
932 G_SOCKET_PROTOCOL_DEFAULT,
934 g_assert_no_error (err);
935 g_assert (G_IS_SOCKET (listener));
937 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
939 G_SOCKET_PROTOCOL_DEFAULT,
941 g_assert_no_error (err);
942 g_assert (G_IS_SOCKET (client));
944 if (socket_type == G_SOCKET_TYPE_STREAM)
946 g_socket_set_option (client, IPPROTO_TCP, TCP_NODELAY, TRUE, &err);
947 g_assert_no_error (err);
950 addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
951 saddr = g_inet_socket_address_new (addr, 0);
953 g_socket_bind (listener, saddr, TRUE, &err);
954 g_assert_no_error (err);
955 g_object_unref (saddr);
956 g_object_unref (addr);
958 saddr = g_socket_get_local_address (listener, &err);
959 g_assert_no_error (err);
961 if (socket_type == G_SOCKET_TYPE_STREAM)
963 g_socket_listen (listener, &err);
964 g_assert_no_error (err);
965 g_socket_connect (client, saddr, NULL, &err);
966 g_assert_no_error (err);
968 server = g_socket_accept (listener, NULL, &err);
969 g_assert_no_error (err);
970 g_socket_set_blocking (server, FALSE);
971 g_object_unref (listener);
976 g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
977 g_assert_no_error (err);
979 while (!g_socket_condition_wait (server, G_IO_IN, NULL, NULL))
981 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
983 g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
984 g_assert_no_error (err);
986 /* We need to wait until the data has actually been copied into the
987 * server socket's buffers, but g_socket_condition_wait() won't help
988 * here since the socket is definitely already readable. So there's
989 * a race condition in checking its available bytes. In the TCP
990 * case, we poll for a bit until the new data shows up. In the UDP
991 * case, there's not much we can do, but at least the failure mode
992 * is passes-when-it-shouldn't, not fails-when-it-shouldn't.
994 if (socket_type == G_SOCKET_TYPE_STREAM)
998 for (tries = 0; tries < 100; tries++)
1000 if (g_socket_get_available_bytes (server) > sizeof (data))
1005 g_assert_cmpint (g_socket_get_available_bytes (server), ==, 2 * sizeof (data));
1010 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1013 g_assert_cmpint (sizeof (buf), >=, 2 * sizeof (data));
1014 nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
1015 g_assert_no_error (err);
1017 if (socket_type == G_SOCKET_TYPE_STREAM)
1019 g_assert_cmpint (nread, ==, 2 * sizeof (data));
1020 g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1024 g_assert_cmpint (nread, ==, sizeof (data));
1025 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1028 nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
1029 if (socket_type == G_SOCKET_TYPE_STREAM)
1031 g_assert_cmpint (nread, ==, -1);
1032 g_assert_error (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
1033 g_clear_error (&err);
1037 g_assert_cmpint (nread, ==, sizeof (data));
1038 g_assert_no_error (err);
1041 g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1043 g_socket_close (server, &err);
1044 g_assert_no_error (err);
1046 g_object_unref (saddr);
1047 g_object_unref (server);
1048 g_object_unref (client);
1057 g_test_init (&argc, &argv, NULL);
1059 sock = g_socket_new (G_SOCKET_FAMILY_IPV6,
1060 G_SOCKET_TYPE_STREAM,
1061 G_SOCKET_PROTOCOL_DEFAULT,
1065 ipv6_supported = TRUE;
1066 g_object_unref (sock);
1069 g_test_add_func ("/socket/ipv4_sync", test_ipv4_sync);
1070 g_test_add_func ("/socket/ipv4_async", test_ipv4_async);
1071 g_test_add_func ("/socket/ipv6_sync", test_ipv6_sync);
1072 g_test_add_func ("/socket/ipv6_async", test_ipv6_async);
1073 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
1074 g_test_add_func ("/socket/ipv6_v4mapped", test_ipv6_v4mapped);
1076 g_test_add_func ("/socket/close_graceful", test_close_graceful);
1077 g_test_add_func ("/socket/timed_wait", test_timed_wait);
1078 g_test_add_func ("/socket/address", test_sockaddr);
1080 g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
1081 g_test_add_func ("/socket/unix-connection", test_unix_connection);
1082 g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data);
1084 g_test_add_func ("/socket/reuse/tcp", test_reuse_tcp);
1085 g_test_add_func ("/socket/reuse/udp", test_reuse_udp);
1086 g_test_add_data_func ("/socket/get_available/datagram", GUINT_TO_POINTER (G_SOCKET_TYPE_DATAGRAM),
1087 test_get_available);
1088 g_test_add_data_func ("/socket/get_available/stream", GUINT_TO_POINTER (G_SOCKET_TYPE_STREAM),
1089 test_get_available);
1091 return g_test_run();