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);
278 g_assert_no_error (error);
280 client = g_socket_new (family,
281 G_SOCKET_TYPE_STREAM,
282 G_SOCKET_PROTOCOL_DEFAULT,
284 g_assert_no_error (error);
285 data->client = client;
287 g_assert_cmpint (g_socket_get_family (client), ==, family);
288 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
289 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
291 g_socket_set_blocking (client, FALSE);
292 g_socket_set_timeout (client, 1);
294 if (g_socket_connect (client, addr, NULL, &error))
296 g_assert_no_error (error);
297 g_idle_add (idle_test_ip_async_connected, data);
301 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
302 g_clear_error (&error);
303 source = g_socket_create_source (client, G_IO_OUT, NULL);
304 g_source_set_callback (source, (GSourceFunc)test_ip_async_connected,
306 g_source_attach (source, NULL);
307 g_source_unref (source);
309 g_object_unref (addr);
311 data->loop = g_main_loop_new (NULL, TRUE);
312 g_main_loop_run (data->loop);
313 g_main_loop_unref (data->loop);
315 g_socket_shutdown (client, FALSE, TRUE, &error);
316 g_assert_no_error (error);
318 g_thread_join (data->thread);
320 if (family == G_SOCKET_FAMILY_IPV4)
322 /* Test that reading on a remote-closed socket gets back 0 bytes. */
323 len = g_socket_receive_with_blocking (client, buf, sizeof (buf),
325 g_assert_no_error (error);
326 g_assert_cmpint (len, ==, 0);
330 /* Test that writing to a remote-closed socket gets back CONNECTION_CLOSED. */
331 len = g_socket_send_with_blocking (client, testbuf, strlen (testbuf) + 1,
333 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED);
334 g_assert_cmpint (len, ==, -1);
335 g_clear_error (&error);
338 g_socket_close (client, &error);
339 g_assert_no_error (error);
340 g_socket_close (data->server, &error);
341 g_assert_no_error (error);
343 g_object_unref (data->server);
344 g_object_unref (client);
346 g_slice_free (IPTestData, data);
350 test_ipv4_async (void)
352 test_ip_async (G_SOCKET_FAMILY_IPV4);
356 test_ipv6_async (void)
360 g_test_skip ("No support for IPv6");
364 test_ip_async (G_SOCKET_FAMILY_IPV6);
367 static const gchar testbuf2[] = "0123456789abcdefghijklmnopqrstuvwxyz";
370 test_ip_sync (GSocketFamily family)
373 GError *error = NULL;
375 GSocketAddress *addr;
379 data = create_server (family, echo_server_thread, FALSE);
380 addr = g_socket_get_local_address (data->server, &error);
381 g_assert_no_error (error);
383 client = g_socket_new (family,
384 G_SOCKET_TYPE_STREAM,
385 G_SOCKET_PROTOCOL_DEFAULT,
387 g_assert_no_error (error);
389 g_assert_cmpint (g_socket_get_family (client), ==, family);
390 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
391 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
393 g_socket_set_blocking (client, TRUE);
394 g_socket_set_timeout (client, 1);
396 g_socket_connect (client, addr, NULL, &error);
397 g_assert_no_error (error);
398 g_assert (g_socket_is_connected (client));
399 g_object_unref (addr);
401 /* This adds 1 second to "make check", so let's just only do it once. */
402 if (family == G_SOCKET_FAMILY_IPV4)
404 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
405 g_assert_cmpint (len, ==, -1);
406 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
407 g_clear_error (&error);
410 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
411 g_assert_no_error (error);
412 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
414 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
415 g_assert_no_error (error);
416 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
418 g_assert_cmpstr (testbuf, ==, buf);
421 GOutputVector v[7] = { { NULL, }, };
423 v[0].buffer = testbuf2 + 0;
425 v[1].buffer = testbuf2 + 3;
427 v[2].buffer = testbuf2 + 3 + 5;
429 v[3].buffer = testbuf2 + 3 + 5;
431 v[4].buffer = testbuf2 + 3 + 5 + 6;
433 v[5].buffer = testbuf2 + 3 + 5 + 6 + 2;
435 v[6].buffer = testbuf2 + 3 + 5 + 6 + 2 + 1;
436 v[6].size = strlen (testbuf2) - (3 + 5 + 6 + 2 + 1);
438 len = g_socket_send_message (client, NULL, v, G_N_ELEMENTS (v), NULL, 0, 0, NULL, &error);
439 g_assert_no_error (error);
440 g_assert_cmpint (len, ==, strlen (testbuf2));
442 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
443 g_assert_no_error (error);
444 g_assert_cmpint (len, ==, strlen (testbuf2));
445 g_assert_cmpstr (testbuf2, ==, buf);
448 g_socket_shutdown (client, FALSE, TRUE, &error);
449 g_assert_no_error (error);
451 g_thread_join (data->thread);
453 if (family == G_SOCKET_FAMILY_IPV4)
455 /* Test that reading on a remote-closed socket gets back 0 bytes. */
456 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
457 g_assert_no_error (error);
458 g_assert_cmpint (len, ==, 0);
462 /* Test that writing to a remote-closed socket gets back CONNECTION_CLOSED. */
463 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
464 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED);
465 g_assert_cmpint (len, ==, -1);
466 g_clear_error (&error);
469 g_socket_close (client, &error);
470 g_assert_no_error (error);
471 g_socket_close (data->server, &error);
472 g_assert_no_error (error);
474 g_object_unref (data->server);
475 g_object_unref (client);
477 g_slice_free (IPTestData, data);
481 test_ipv4_sync (void)
483 test_ip_sync (G_SOCKET_FAMILY_IPV4);
487 test_ipv6_sync (void)
491 g_test_skip ("No support for IPv6");
495 test_ip_sync (G_SOCKET_FAMILY_IPV6);
499 graceful_server_thread (gpointer user_data)
501 IPTestData *data = user_data;
503 GError *error = NULL;
506 sock = g_socket_accept (data->server, NULL, &error);
507 g_assert_no_error (error);
509 len = g_socket_send (sock, testbuf, strlen (testbuf) + 1, NULL, &error);
510 g_assert_no_error (error);
511 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
517 test_close_graceful (void)
519 GSocketFamily family = G_SOCKET_FAMILY_IPV4;
521 GError *error = NULL;
522 GSocket *client, *server;
523 GSocketAddress *addr;
527 data = create_server (family, graceful_server_thread, FALSE);
528 addr = g_socket_get_local_address (data->server, &error);
529 g_assert_no_error (error);
531 client = g_socket_new (family,
532 G_SOCKET_TYPE_STREAM,
533 G_SOCKET_PROTOCOL_DEFAULT,
535 g_assert_no_error (error);
537 g_assert_cmpint (g_socket_get_family (client), ==, family);
538 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
539 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
541 g_socket_set_blocking (client, TRUE);
542 g_socket_set_timeout (client, 1);
544 g_socket_connect (client, addr, NULL, &error);
545 g_assert_no_error (error);
546 g_assert (g_socket_is_connected (client));
547 g_object_unref (addr);
549 server = g_thread_join (data->thread);
551 /* similar to g_tcp_connection_set_graceful_disconnect(), but explicit */
552 g_socket_shutdown (server, FALSE, TRUE, &error);
553 g_assert_no_error (error);
555 /* we must timeout */
556 g_socket_condition_wait (client, G_IO_HUP, NULL, &error);
557 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
558 g_clear_error (&error);
560 /* check that the remaining data is received */
561 len = g_socket_receive (client, buf, strlen (testbuf) + 1, NULL, &error);
562 g_assert_no_error (error);
563 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
565 /* and only then the connection is closed */
566 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
567 g_assert_no_error (error);
568 g_assert_cmpint (len, ==, 0);
570 g_socket_close (server, &error);
571 g_assert_no_error (error);
573 g_socket_close (client, &error);
574 g_assert_no_error (error);
576 g_object_unref (server);
577 g_object_unref (data->server);
578 g_object_unref (client);
580 g_slice_free (IPTestData, data);
583 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
585 v4mapped_server_thread (gpointer user_data)
587 IPTestData *data = user_data;
589 GError *error = NULL;
590 GSocketAddress *addr;
592 sock = g_socket_accept (data->server, NULL, &error);
593 g_assert_no_error (error);
595 g_assert_cmpint (g_socket_get_family (sock), ==, G_SOCKET_FAMILY_IPV6);
597 addr = g_socket_get_local_address (sock, &error);
598 g_assert_no_error (error);
599 g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
600 g_object_unref (addr);
602 addr = g_socket_get_remote_address (sock, &error);
603 g_assert_no_error (error);
604 g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
605 g_object_unref (addr);
607 g_socket_close (sock, &error);
608 g_assert_no_error (error);
609 g_object_unref (sock);
614 test_ipv6_v4mapped (void)
617 GError *error = NULL;
619 GSocketAddress *addr, *v4addr;
624 g_test_skip ("No support for IPv6");
628 data = create_server (G_SOCKET_FAMILY_IPV6, v4mapped_server_thread, TRUE);
632 g_test_message ("Test not run: not supported by the OS");
636 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
637 G_SOCKET_TYPE_STREAM,
638 G_SOCKET_PROTOCOL_DEFAULT,
640 g_assert_no_error (error);
642 g_socket_set_blocking (client, TRUE);
643 g_socket_set_timeout (client, 1);
645 addr = g_socket_get_local_address (data->server, &error);
646 g_assert_no_error (error);
647 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
648 v4addr = g_inet_socket_address_new (iaddr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
649 g_object_unref (iaddr);
650 g_object_unref (addr);
652 g_socket_connect (client, v4addr, NULL, &error);
653 g_assert_no_error (error);
654 g_assert (g_socket_is_connected (client));
656 g_thread_join (data->thread);
658 g_socket_close (client, &error);
659 g_assert_no_error (error);
660 g_socket_close (data->server, &error);
661 g_assert_no_error (error);
663 g_object_unref (data->server);
664 g_object_unref (client);
665 g_object_unref (v4addr);
667 g_slice_free (IPTestData, data);
672 test_timed_wait (void)
675 GError *error = NULL;
677 GSocketAddress *addr;
681 data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE);
682 addr = g_socket_get_local_address (data->server, &error);
683 g_assert_no_error (error);
685 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
686 G_SOCKET_TYPE_STREAM,
687 G_SOCKET_PROTOCOL_DEFAULT,
689 g_assert_no_error (error);
691 g_socket_set_blocking (client, TRUE);
692 g_socket_set_timeout (client, 1);
694 g_socket_connect (client, addr, NULL, &error);
695 g_assert_no_error (error);
696 g_object_unref (addr);
698 start_time = g_get_monotonic_time ();
699 g_socket_condition_timed_wait (client, G_IO_IN, 100000 /* 100 ms */,
701 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
702 g_clear_error (&error);
703 poll_duration = g_get_monotonic_time () - start_time;
705 g_assert_cmpint (poll_duration, >=, 98000);
706 g_assert_cmpint (poll_duration, <, 112000);
708 g_socket_close (client, &error);
709 g_assert_no_error (error);
711 g_thread_join (data->thread);
713 g_socket_close (data->server, &error);
714 g_assert_no_error (error);
716 g_object_unref (data->server);
717 g_object_unref (client);
719 g_slice_free (IPTestData, data);
725 struct sockaddr_in6 sin6, gsin6;
726 GSocketAddress *saddr;
727 GInetSocketAddress *isaddr;
729 GError *error = NULL;
731 memset (&sin6, 0, sizeof (sin6));
732 sin6.sin6_family = AF_INET6;
733 sin6.sin6_addr = in6addr_loopback;
734 sin6.sin6_port = g_htons (42);
735 sin6.sin6_scope_id = 17;
736 sin6.sin6_flowinfo = 1729;
738 saddr = g_socket_address_new_from_native (&sin6, sizeof (sin6));
739 g_assert (G_IS_INET_SOCKET_ADDRESS (saddr));
741 isaddr = G_INET_SOCKET_ADDRESS (saddr);
742 iaddr = g_inet_socket_address_get_address (isaddr);
743 g_assert_cmpint (g_inet_address_get_family (iaddr), ==, G_SOCKET_FAMILY_IPV6);
744 g_assert (g_inet_address_get_is_loopback (iaddr));
746 g_assert_cmpint (g_inet_socket_address_get_port (isaddr), ==, 42);
747 g_assert_cmpint (g_inet_socket_address_get_scope_id (isaddr), ==, 17);
748 g_assert_cmpint (g_inet_socket_address_get_flowinfo (isaddr), ==, 1729);
750 g_socket_address_to_native (saddr, &gsin6, sizeof (gsin6), &error);
751 g_assert_no_error (error);
753 g_assert (memcmp (&sin6.sin6_addr, &gsin6.sin6_addr, sizeof (struct in6_addr)) == 0);
754 g_assert_cmpint (sin6.sin6_port, ==, gsin6.sin6_port);
755 g_assert_cmpint (sin6.sin6_scope_id, ==, gsin6.sin6_scope_id);
756 g_assert_cmpint (sin6.sin6_flowinfo, ==, gsin6.sin6_flowinfo);
758 g_object_unref (saddr);
763 test_unix_from_fd (void)
769 fd = socket (AF_UNIX, SOCK_STREAM, 0);
770 g_assert_cmpint (fd, !=, -1);
773 s = g_socket_new_from_fd (fd, &error);
774 g_assert_no_error (error);
775 g_assert_cmpint (g_socket_get_family (s), ==, G_SOCKET_FAMILY_UNIX);
776 g_assert_cmpint (g_socket_get_socket_type (s), ==, G_SOCKET_TYPE_STREAM);
777 g_assert_cmpint (g_socket_get_protocol (s), ==, G_SOCKET_PROTOCOL_DEFAULT);
782 test_unix_connection (void)
787 GSocketConnection *c;
789 fd = socket (AF_UNIX, SOCK_STREAM, 0);
790 g_assert_cmpint (fd, !=, -1);
793 s = g_socket_new_from_fd (fd, &error);
794 g_assert_no_error (error);
795 c = g_socket_connection_factory_create_connection (s);
796 g_assert (G_IS_UNIX_CONNECTION (c));
801 static GSocketConnection *
802 create_connection_for_fd (int fd)
806 GSocketConnection *connection;
808 socket = g_socket_new_from_fd (fd, &err);
809 g_assert_no_error (err);
810 g_assert (G_IS_SOCKET (socket));
811 connection = g_socket_connection_factory_create_connection (socket);
812 g_assert (G_IS_UNIX_CONNECTION (connection));
813 g_object_unref (socket);
817 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
820 test_unix_connection_ancillary_data (void)
824 gint status, fd, len;
829 g_assert_cmpint (status, ==, 0);
831 status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
832 g_assert_cmpint (status, ==, 0);
835 g_assert_cmpint (pid, >=, 0);
837 /* Child: close its copy of the write end of the pipe, receive it
838 * again from the parent over the socket, and write some text to it.
840 * Parent: send the write end of the pipe (still open for the
841 * parent) over the socket, close it, and read some text from the
842 * read end of the pipe.
846 GSocketConnection *connection;
849 connection = create_connection_for_fd (sv[0]);
851 status = close (pv[1]);
852 g_assert_cmpint (status, ==, 0);
855 fd = g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection), NULL,
857 g_assert_no_error (err);
858 g_assert_cmpint (fd, >, -1);
859 g_object_unref (connection);
862 len = write (fd, TEST_DATA, sizeof (TEST_DATA));
863 while (len == -1 && errno == EINTR);
864 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
869 GSocketConnection *connection;
872 connection = create_connection_for_fd (sv[1]);
875 g_unix_connection_send_fd (G_UNIX_CONNECTION (connection), pv[1], NULL,
877 g_assert_no_error (err);
878 g_object_unref (connection);
880 status = close (pv[1]);
881 g_assert_cmpint (status, ==, 0);
883 memset (buffer, 0xff, sizeof buffer);
885 len = read (pv[0], buffer, sizeof buffer);
886 while (len == -1 && errno == EINTR);
888 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
889 g_assert_cmpstr (buffer, ==, TEST_DATA);
891 waitpid (pid, &status, 0);
892 g_assert (WIFEXITED (status));
893 g_assert_cmpint (WEXITSTATUS (status), ==, 0);
896 /* TODO: add test for g_unix_connection_send_credentials() and
897 * g_unix_connection_receive_credentials().
900 #endif /* G_OS_UNIX */
903 test_reuse_tcp (void)
905 GSocket *sock1, *sock2;
906 GError *error = NULL;
908 GSocketAddress *addr;
910 sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
911 G_SOCKET_TYPE_STREAM,
912 G_SOCKET_PROTOCOL_DEFAULT,
914 g_assert_no_error (error);
916 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
917 addr = g_inet_socket_address_new (iaddr, 0);
918 g_object_unref (iaddr);
919 g_socket_bind (sock1, addr, TRUE, &error);
920 g_object_unref (addr);
921 g_assert_no_error (error);
923 g_socket_listen (sock1, &error);
924 g_assert_no_error (error);
926 sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
927 G_SOCKET_TYPE_STREAM,
928 G_SOCKET_PROTOCOL_DEFAULT,
930 g_assert_no_error (error);
932 addr = g_socket_get_local_address (sock1, &error);
933 g_assert_no_error (error);
934 g_socket_bind (sock2, addr, TRUE, &error);
935 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_ADDRESS_IN_USE);
936 g_clear_error (&error);
937 g_object_unref (addr);
939 g_object_unref (sock1);
940 g_object_unref (sock2);
944 test_reuse_udp (void)
946 GSocket *sock1, *sock2;
947 GError *error = NULL;
949 GSocketAddress *addr;
951 sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
952 G_SOCKET_TYPE_DATAGRAM,
953 G_SOCKET_PROTOCOL_DEFAULT,
955 g_assert_no_error (error);
957 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
958 addr = g_inet_socket_address_new (iaddr, 0);
959 g_object_unref (iaddr);
960 g_socket_bind (sock1, addr, TRUE, &error);
961 g_object_unref (addr);
962 g_assert_no_error (error);
964 sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
965 G_SOCKET_TYPE_DATAGRAM,
966 G_SOCKET_PROTOCOL_DEFAULT,
968 g_assert_no_error (error);
970 addr = g_socket_get_local_address (sock1, &error);
971 g_assert_no_error (error);
972 g_socket_bind (sock2, addr, TRUE, &error);
973 g_object_unref (addr);
974 g_assert_no_error (error);
976 g_object_unref (sock1);
977 g_object_unref (sock2);
981 test_get_available (gconstpointer user_data)
983 GSocketType socket_type = GPOINTER_TO_UINT (user_data);
985 GSocket *listener, *server, *client;
987 GSocketAddress *saddr;
988 gchar data[] = "0123456789abcdef";
992 listener = g_socket_new (G_SOCKET_FAMILY_IPV4,
994 G_SOCKET_PROTOCOL_DEFAULT,
996 g_assert_no_error (err);
997 g_assert (G_IS_SOCKET (listener));
999 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1001 G_SOCKET_PROTOCOL_DEFAULT,
1003 g_assert_no_error (err);
1004 g_assert (G_IS_SOCKET (client));
1006 if (socket_type == G_SOCKET_TYPE_STREAM)
1008 g_socket_set_option (client, IPPROTO_TCP, TCP_NODELAY, TRUE, &err);
1009 g_assert_no_error (err);
1012 addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
1013 saddr = g_inet_socket_address_new (addr, 0);
1015 g_socket_bind (listener, saddr, TRUE, &err);
1016 g_assert_no_error (err);
1017 g_object_unref (saddr);
1018 g_object_unref (addr);
1020 saddr = g_socket_get_local_address (listener, &err);
1021 g_assert_no_error (err);
1023 if (socket_type == G_SOCKET_TYPE_STREAM)
1025 g_socket_listen (listener, &err);
1026 g_assert_no_error (err);
1027 g_socket_connect (client, saddr, NULL, &err);
1028 g_assert_no_error (err);
1030 server = g_socket_accept (listener, NULL, &err);
1031 g_assert_no_error (err);
1032 g_socket_set_blocking (server, FALSE);
1033 g_object_unref (listener);
1038 g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
1039 g_assert_no_error (err);
1041 while (!g_socket_condition_wait (server, G_IO_IN, NULL, NULL))
1043 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1045 g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
1046 g_assert_no_error (err);
1048 /* We need to wait until the data has actually been copied into the
1049 * server socket's buffers, but g_socket_condition_wait() won't help
1050 * here since the socket is definitely already readable. So there's
1051 * a race condition in checking its available bytes. In the TCP
1052 * case, we poll for a bit until the new data shows up. In the UDP
1053 * case, there's not much we can do, but at least the failure mode
1054 * is passes-when-it-shouldn't, not fails-when-it-shouldn't.
1056 if (socket_type == G_SOCKET_TYPE_STREAM)
1060 for (tries = 0; tries < 100; tries++)
1062 if (g_socket_get_available_bytes (server) > sizeof (data))
1067 g_assert_cmpint (g_socket_get_available_bytes (server), ==, 2 * sizeof (data));
1072 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1075 g_assert_cmpint (sizeof (buf), >=, 2 * sizeof (data));
1076 nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
1077 g_assert_no_error (err);
1079 if (socket_type == G_SOCKET_TYPE_STREAM)
1081 g_assert_cmpint (nread, ==, 2 * sizeof (data));
1082 g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1086 g_assert_cmpint (nread, ==, sizeof (data));
1087 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1090 nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
1091 if (socket_type == G_SOCKET_TYPE_STREAM)
1093 g_assert_cmpint (nread, ==, -1);
1094 g_assert_error (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
1095 g_clear_error (&err);
1099 g_assert_cmpint (nread, ==, sizeof (data));
1100 g_assert_no_error (err);
1103 g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1105 g_socket_close (server, &err);
1106 g_assert_no_error (err);
1108 g_object_unref (saddr);
1109 g_object_unref (server);
1110 g_object_unref (client);
1118 GError *error = NULL;
1120 g_test_init (&argc, &argv, NULL);
1122 sock = g_socket_new (G_SOCKET_FAMILY_IPV6,
1123 G_SOCKET_TYPE_STREAM,
1124 G_SOCKET_PROTOCOL_DEFAULT,
1128 ipv6_supported = TRUE;
1129 g_object_unref (sock);
1133 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
1134 g_clear_error (&error);
1137 g_test_add_func ("/socket/ipv4_sync", test_ipv4_sync);
1138 g_test_add_func ("/socket/ipv4_async", test_ipv4_async);
1139 g_test_add_func ("/socket/ipv6_sync", test_ipv6_sync);
1140 g_test_add_func ("/socket/ipv6_async", test_ipv6_async);
1141 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
1142 g_test_add_func ("/socket/ipv6_v4mapped", test_ipv6_v4mapped);
1144 g_test_add_func ("/socket/close_graceful", test_close_graceful);
1145 g_test_add_func ("/socket/timed_wait", test_timed_wait);
1146 g_test_add_func ("/socket/address", test_sockaddr);
1148 g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
1149 g_test_add_func ("/socket/unix-connection", test_unix_connection);
1150 g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data);
1152 g_test_add_func ("/socket/reuse/tcp", test_reuse_tcp);
1153 g_test_add_func ("/socket/reuse/udp", test_reuse_udp);
1154 g_test_add_data_func ("/socket/get_available/datagram", GUINT_TO_POINTER (G_SOCKET_TYPE_DATAGRAM),
1155 test_get_available);
1156 g_test_add_data_func ("/socket/get_available/stream", GUINT_TO_POINTER (G_SOCKET_TYPE_STREAM),
1157 test_get_available);
1159 return g_test_run();