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.1 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;
40 GCancellable *cancellable; /* to shut down dgram echo server thread */
44 echo_server_dgram_thread (gpointer user_data)
46 IPTestData *data = user_data;
48 GCancellable *cancellable = data->cancellable;
58 nread = g_socket_receive_from (sock, &sa, buf, sizeof (buf), cancellable, &error);
59 if (error && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
61 g_assert_no_error (error);
62 g_assert_cmpint (nread, >=, 0);
64 nwrote = g_socket_send_to (sock, sa, buf, nread, cancellable, &error);
65 if (error && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
67 g_assert_no_error (error);
68 g_assert_cmpint (nwrote, ==, nread);
73 g_clear_error (&error);
79 echo_server_thread (gpointer user_data)
81 IPTestData *data = user_data;
87 sock = g_socket_accept (data->server, NULL, &error);
88 g_assert_no_error (error);
92 nread = g_socket_receive (sock, buf, sizeof (buf), NULL, &error);
93 g_assert_no_error (error);
94 g_assert_cmpint (nread, >=, 0);
99 nwrote = g_socket_send (sock, buf, nread, NULL, &error);
100 g_assert_no_error (error);
101 g_assert_cmpint (nwrote, ==, nread);
104 g_socket_close (sock, &error);
105 g_assert_no_error (error);
106 g_object_unref (sock);
111 create_server_full (GSocketFamily family,
112 GSocketType socket_type,
113 GThreadFunc server_thread,
119 GSocketAddress *addr;
122 data = g_slice_new (IPTestData);
123 data->family = family;
125 data->server = server = g_socket_new (family,
127 G_SOCKET_PROTOCOL_DEFAULT,
132 g_assert_cmpint (g_socket_get_family (server), ==, family);
133 g_assert_cmpint (g_socket_get_socket_type (server), ==, socket_type);
134 g_assert_cmpint (g_socket_get_protocol (server), ==, G_SOCKET_PROTOCOL_DEFAULT);
136 g_socket_set_blocking (server, TRUE);
138 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
141 g_socket_set_option (data->server, IPPROTO_IPV6, IPV6_V6ONLY, FALSE, NULL);
142 if (!g_socket_speaks_ipv4 (data->server))
148 iaddr = g_inet_address_new_any (family);
150 iaddr = g_inet_address_new_loopback (family);
151 addr = g_inet_socket_address_new (iaddr, 0);
152 g_object_unref (iaddr);
154 g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), ==, 0);
155 if (!g_socket_bind (server, addr, TRUE, error))
157 g_object_unref (addr);
160 g_object_unref (addr);
162 addr = g_socket_get_local_address (server, error);
165 g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), !=, 0);
166 g_object_unref (addr);
168 if (socket_type == G_SOCKET_TYPE_STREAM)
170 if (!g_socket_listen (server, error))
175 data->cancellable = g_cancellable_new ();
178 data->thread = g_thread_new ("server", server_thread, data);
183 g_clear_object (&data->server);
184 g_slice_free (IPTestData, data);
190 create_server (GSocketFamily family,
191 GThreadFunc server_thread,
195 return create_server_full (family, G_SOCKET_TYPE_STREAM, server_thread, v4mapped, error);
198 static const gchar *testbuf = "0123456789abcdef";
201 test_ip_async_read_ready (GSocket *client,
205 IPTestData *data = user_data;
206 GError *error = NULL;
210 g_assert_cmpint (cond, ==, G_IO_IN);
212 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
213 g_assert_no_error (error);
214 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
216 g_assert_cmpstr (testbuf, ==, buf);
218 g_main_loop_quit (data->loop);
224 test_ip_async_write_ready (GSocket *client,
228 IPTestData *data = user_data;
229 GError *error = NULL;
233 g_assert_cmpint (cond, ==, G_IO_OUT);
235 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
236 g_assert_no_error (error);
237 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
239 source = g_socket_create_source (client, G_IO_IN, NULL);
240 g_source_set_callback (source, (GSourceFunc)test_ip_async_read_ready,
242 g_source_attach (source, NULL);
243 g_source_unref (source);
249 test_ip_async_timed_out (GSocket *client,
253 IPTestData *data = user_data;
254 GError *error = NULL;
259 if (data->family == G_SOCKET_FAMILY_IPV4)
261 g_assert_cmpint (cond, ==, G_IO_IN);
262 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
263 g_assert_cmpint (len, ==, -1);
264 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
265 g_clear_error (&error);
268 source = g_socket_create_source (client, G_IO_OUT, NULL);
269 g_source_set_callback (source, (GSourceFunc)test_ip_async_write_ready,
271 g_source_attach (source, NULL);
272 g_source_unref (source);
278 test_ip_async_connected (GSocket *client,
282 IPTestData *data = user_data;
283 GError *error = NULL;
288 g_socket_check_connect_result (client, &error);
289 g_assert_no_error (error);
290 /* We do this after the check_connect_result, since that will give a
291 * more useful assertion in case of error.
293 g_assert_cmpint (cond, ==, G_IO_OUT);
295 g_assert (g_socket_is_connected (client));
297 /* This adds 1 second to "make check", so let's just only do it once. */
298 if (data->family == G_SOCKET_FAMILY_IPV4)
300 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
301 g_assert_cmpint (len, ==, -1);
302 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
303 g_clear_error (&error);
305 source = g_socket_create_source (client, G_IO_IN, NULL);
306 g_source_set_callback (source, (GSourceFunc)test_ip_async_timed_out,
308 g_source_attach (source, NULL);
309 g_source_unref (source);
312 test_ip_async_timed_out (client, 0, data);
318 idle_test_ip_async_connected (gpointer user_data)
320 IPTestData *data = user_data;
322 return test_ip_async_connected (data->client, G_IO_OUT, data);
326 test_ip_async (GSocketFamily family)
329 GError *error = NULL;
331 GSocketAddress *addr;
336 data = create_server (family, echo_server_thread, FALSE, &error);
339 gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
340 g_test_skip (message);
342 g_clear_error (&error);
346 addr = g_socket_get_local_address (data->server, &error);
347 g_assert_no_error (error);
349 client = g_socket_new (family,
350 G_SOCKET_TYPE_STREAM,
351 G_SOCKET_PROTOCOL_DEFAULT,
353 g_assert_no_error (error);
354 data->client = client;
356 g_assert_cmpint (g_socket_get_family (client), ==, family);
357 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
358 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
360 g_socket_set_blocking (client, FALSE);
361 g_socket_set_timeout (client, 1);
363 if (g_socket_connect (client, addr, NULL, &error))
365 g_assert_no_error (error);
366 g_idle_add (idle_test_ip_async_connected, data);
370 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
371 g_clear_error (&error);
372 source = g_socket_create_source (client, G_IO_OUT, NULL);
373 g_source_set_callback (source, (GSourceFunc)test_ip_async_connected,
375 g_source_attach (source, NULL);
376 g_source_unref (source);
378 g_object_unref (addr);
380 data->loop = g_main_loop_new (NULL, TRUE);
381 g_main_loop_run (data->loop);
382 g_main_loop_unref (data->loop);
384 g_socket_shutdown (client, FALSE, TRUE, &error);
385 g_assert_no_error (error);
387 g_thread_join (data->thread);
389 if (family == G_SOCKET_FAMILY_IPV4)
391 /* Test that reading on a remote-closed socket gets back 0 bytes. */
392 len = g_socket_receive_with_blocking (client, buf, sizeof (buf),
394 g_assert_no_error (error);
395 g_assert_cmpint (len, ==, 0);
399 /* Test that writing to a remote-closed socket gets back CONNECTION_CLOSED. */
400 len = g_socket_send_with_blocking (client, testbuf, strlen (testbuf) + 1,
402 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED);
403 g_assert_cmpint (len, ==, -1);
404 g_clear_error (&error);
407 g_socket_close (client, &error);
408 g_assert_no_error (error);
409 g_socket_close (data->server, &error);
410 g_assert_no_error (error);
412 g_object_unref (data->server);
413 g_object_unref (client);
415 g_slice_free (IPTestData, data);
419 test_ipv4_async (void)
421 test_ip_async (G_SOCKET_FAMILY_IPV4);
425 test_ipv6_async (void)
429 g_test_skip ("No support for IPv6");
433 test_ip_async (G_SOCKET_FAMILY_IPV6);
436 static const gchar testbuf2[] = "0123456789abcdefghijklmnopqrstuvwxyz";
439 test_ip_sync (GSocketFamily family)
442 GError *error = NULL;
444 GSocketAddress *addr;
448 data = create_server (family, echo_server_thread, FALSE, &error);
451 gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
452 g_test_skip (message);
454 g_clear_error (&error);
458 addr = g_socket_get_local_address (data->server, &error);
459 g_assert_no_error (error);
461 client = g_socket_new (family,
462 G_SOCKET_TYPE_STREAM,
463 G_SOCKET_PROTOCOL_DEFAULT,
465 g_assert_no_error (error);
467 g_assert_cmpint (g_socket_get_family (client), ==, family);
468 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
469 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
471 g_socket_set_blocking (client, TRUE);
472 g_socket_set_timeout (client, 1);
474 g_socket_connect (client, addr, NULL, &error);
475 g_assert_no_error (error);
476 g_assert (g_socket_is_connected (client));
477 g_object_unref (addr);
479 /* This adds 1 second to "make check", so let's just only do it once. */
480 if (family == G_SOCKET_FAMILY_IPV4)
482 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
483 g_assert_cmpint (len, ==, -1);
484 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
485 g_clear_error (&error);
488 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
489 g_assert_no_error (error);
490 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
492 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
493 g_assert_no_error (error);
494 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
496 g_assert_cmpstr (testbuf, ==, buf);
499 GOutputVector v[7] = { { NULL, }, };
501 v[0].buffer = testbuf2 + 0;
503 v[1].buffer = testbuf2 + 3;
505 v[2].buffer = testbuf2 + 3 + 5;
507 v[3].buffer = testbuf2 + 3 + 5;
509 v[4].buffer = testbuf2 + 3 + 5 + 6;
511 v[5].buffer = testbuf2 + 3 + 5 + 6 + 2;
513 v[6].buffer = testbuf2 + 3 + 5 + 6 + 2 + 1;
514 v[6].size = strlen (testbuf2) - (3 + 5 + 6 + 2 + 1);
516 len = g_socket_send_message (client, NULL, v, G_N_ELEMENTS (v), NULL, 0, 0, NULL, &error);
517 g_assert_no_error (error);
518 g_assert_cmpint (len, ==, strlen (testbuf2));
520 memset (buf, 0, sizeof (buf));
521 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
522 g_assert_no_error (error);
523 g_assert_cmpint (len, ==, strlen (testbuf2));
524 g_assert_cmpstr (testbuf2, ==, buf);
527 g_socket_shutdown (client, FALSE, TRUE, &error);
528 g_assert_no_error (error);
530 g_thread_join (data->thread);
532 if (family == G_SOCKET_FAMILY_IPV4)
534 /* Test that reading on a remote-closed socket gets back 0 bytes. */
535 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
536 g_assert_no_error (error);
537 g_assert_cmpint (len, ==, 0);
541 /* Test that writing to a remote-closed socket gets back CONNECTION_CLOSED. */
542 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
543 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED);
544 g_assert_cmpint (len, ==, -1);
545 g_clear_error (&error);
548 g_socket_close (client, &error);
549 g_assert_no_error (error);
550 g_socket_close (data->server, &error);
551 g_assert_no_error (error);
553 g_object_unref (data->server);
554 g_object_unref (client);
556 g_slice_free (IPTestData, data);
560 test_ipv4_sync (void)
562 test_ip_sync (G_SOCKET_FAMILY_IPV4);
566 test_ipv6_sync (void)
570 g_test_skip ("No support for IPv6");
574 test_ip_sync (G_SOCKET_FAMILY_IPV6);
578 test_ip_sync_dgram (GSocketFamily family)
581 GError *error = NULL;
583 GSocketAddress *dest_addr;
587 data = create_server_full (family, G_SOCKET_TYPE_DATAGRAM,
588 echo_server_dgram_thread, FALSE, &error);
591 gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
592 g_test_skip (message);
594 g_clear_error (&error);
598 dest_addr = g_socket_get_local_address (data->server, &error);
600 client = g_socket_new (family,
601 G_SOCKET_TYPE_DATAGRAM,
602 G_SOCKET_PROTOCOL_DEFAULT,
604 g_assert_no_error (error);
606 g_assert_cmpint (g_socket_get_family (client), ==, family);
607 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_DATAGRAM);
608 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
610 g_socket_set_blocking (client, TRUE);
611 g_socket_set_timeout (client, 1);
613 len = g_socket_send_to (client, dest_addr, testbuf, strlen (testbuf) + 1, NULL, &error);
614 g_assert_no_error (error);
615 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
617 len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
618 g_assert_no_error (error);
619 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
621 g_assert_cmpstr (testbuf, ==, buf);
624 GOutputMessage m[3] = { { NULL, }, };
625 GInputMessage im[3] = { { NULL, }, };
626 GOutputVector v[7] = { { NULL, }, };
627 GInputVector iv[7] = { { NULL, }, };
629 v[0].buffer = testbuf2 + 0;
631 v[1].buffer = testbuf2 + 3;
633 v[2].buffer = testbuf2 + 3 + 5;
635 v[3].buffer = testbuf2 + 3 + 5;
637 v[4].buffer = testbuf2 + 3 + 5 + 6;
639 v[5].buffer = testbuf2 + 3 + 5 + 6 + 2;
641 v[6].buffer = testbuf2 + 3 + 5 + 6 + 2 + 1;
642 v[6].size = strlen (testbuf2) - (3 + 5 + 6 + 2 + 1);
644 iv[0].buffer = buf + 0;
646 iv[1].buffer = buf + 3;
648 iv[2].buffer = buf + 3 + 5;
650 iv[3].buffer = buf + 3 + 5;
652 iv[4].buffer = buf + 3 + 5 + 6;
654 iv[5].buffer = buf + 3 + 5 + 6 + 2;
656 iv[6].buffer = buf + 3 + 5 + 6 + 2 + 1;
657 iv[6].size = sizeof (buf) - (3 + 5 + 6 + 2 + 1);
659 len = g_socket_send_message (client, dest_addr, v, G_N_ELEMENTS (v), NULL, 0, 0, NULL, &error);
660 g_assert_no_error (error);
661 g_assert_cmpint (len, ==, strlen (testbuf2));
663 memset (buf, 0, sizeof (buf));
664 len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
665 g_assert_no_error (error);
666 g_assert_cmpint (len, ==, strlen (testbuf2));
667 g_assert_cmpstr (testbuf2, ==, buf);
669 m[0].vectors = &v[0];
670 m[0].num_vectors = 1;
671 m[0].address = dest_addr;
672 m[1].vectors = &v[0];
673 m[1].num_vectors = 6;
674 m[1].address = dest_addr;
675 m[2].vectors = &v[6];
676 m[2].num_vectors = 1;
677 m[2].address = dest_addr;
679 len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
680 g_assert_no_error (error);
681 g_assert_cmpint (len, ==, G_N_ELEMENTS (m));
682 g_assert_cmpint (m[0].bytes_sent, ==, 3);
683 g_assert_cmpint (m[1].bytes_sent, ==, 17);
684 g_assert_cmpint (m[2].bytes_sent, ==, v[6].size);
686 memset (buf, 0, sizeof (buf));
687 len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
688 g_assert_no_error (error);
689 g_assert_cmpint (len, ==, 3);
691 memset (buf, 0, sizeof (buf));
692 len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
693 g_assert_no_error (error);
694 /* v[0].size + v[1].size + v[2].size + v[3].size + v[4].size + v[5].size */
695 g_assert_cmpint (len, ==, 17);
696 g_assert (memcmp (testbuf2, buf, 17) == 0);
698 memset (buf, 0, sizeof (buf));
699 len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
700 g_assert_no_error (error);
701 g_assert_cmpint (len, ==, v[6].size);
702 g_assert_cmpstr (buf, ==, v[6].buffer);
704 /* reset since we're re-using the message structs */
709 /* now try receiving multiple messages */
710 len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
711 g_assert_no_error (error);
712 g_assert_cmpint (len, ==, G_N_ELEMENTS (m));
713 g_assert_cmpint (m[0].bytes_sent, ==, 3);
714 g_assert_cmpint (m[1].bytes_sent, ==, 17);
715 g_assert_cmpint (m[2].bytes_sent, ==, v[6].size);
717 im[0].vectors = &iv[0];
718 im[0].num_vectors = 1;
719 im[1].vectors = &iv[0];
720 im[1].num_vectors = 6;
721 im[2].vectors = &iv[6];
722 im[2].num_vectors = 1;
724 memset (buf, 0, sizeof (buf));
725 len = g_socket_receive_messages (client, im, G_N_ELEMENTS (im), 0,
727 g_assert_no_error (error);
728 g_assert_cmpint (len, ==, G_N_ELEMENTS (im));
730 g_assert_cmpuint (im[0].bytes_received, ==, 3);
731 /* v[0].size + v[1].size + v[2].size + v[3].size + v[4].size + v[5].size */
732 g_assert_cmpuint (im[1].bytes_received, ==, 17);
733 g_assert_cmpuint (im[2].bytes_received, ==, v[6].size);
735 /* reset since we're re-using the message structs */
740 /* now try to generate an early return by omitting the destination address on [1] */
742 len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
743 g_assert_no_error (error);
744 g_assert_cmpint (len, ==, 1);
746 g_assert_cmpint (m[0].bytes_sent, ==, 3);
747 g_assert_cmpint (m[1].bytes_sent, ==, 0);
748 g_assert_cmpint (m[2].bytes_sent, ==, 0);
750 /* reset since we're re-using the message structs */
755 /* now try to generate an error by omitting all destination addresses */
759 len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
760 /* This error code may vary between platforms and over time; it is not guaranteed API: */
762 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
764 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_CONNECTED);
766 g_clear_error (&error);
767 g_assert_cmpint (len, ==, -1);
769 g_assert_cmpint (m[0].bytes_sent, ==, 0);
770 g_assert_cmpint (m[1].bytes_sent, ==, 0);
771 g_assert_cmpint (m[2].bytes_sent, ==, 0);
773 len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
774 g_assert_cmpint (len, ==, 3);
777 g_cancellable_cancel (data->cancellable);
779 g_thread_join (data->thread);
781 g_socket_close (client, &error);
782 g_assert_no_error (error);
783 g_socket_close (data->server, &error);
784 g_assert_no_error (error);
786 g_object_unref (data->server);
787 g_object_unref (data->cancellable);
788 g_object_unref (client);
789 g_object_unref (dest_addr);
791 g_slice_free (IPTestData, data);
795 test_ipv4_sync_dgram (void)
797 test_ip_sync_dgram (G_SOCKET_FAMILY_IPV4);
801 test_ipv6_sync_dgram (void)
805 g_test_skip ("No support for IPv6");
809 test_ip_sync_dgram (G_SOCKET_FAMILY_IPV6);
813 cancellable_thread_cb (gpointer data)
815 GCancellable *cancellable = data;
817 g_usleep (0.1 * G_USEC_PER_SEC);
818 g_cancellable_cancel (cancellable);
819 g_object_unref (cancellable);
825 test_ip_sync_dgram_timeouts (GSocketFamily family)
827 GError *error = NULL;
828 GSocket *client = NULL;
829 GCancellable *cancellable = NULL;
830 GThread *cancellable_thread = NULL;
834 GSocketAddress *addr;
837 client = g_socket_new (family,
838 G_SOCKET_TYPE_DATAGRAM,
839 G_SOCKET_PROTOCOL_DEFAULT,
841 g_assert_no_error (error);
843 g_assert_cmpint (g_socket_get_family (client), ==, family);
844 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_DATAGRAM);
845 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
848 /* Winsock can't recv() on unbound udp socket */
849 iaddr = g_inet_address_new_loopback (family);
850 addr = g_inet_socket_address_new (iaddr, 0);
851 g_object_unref (iaddr);
852 g_socket_bind (client, addr, TRUE, &error);
853 g_object_unref (addr);
854 g_assert_no_error (error);
857 /* No overall timeout: test the per-operation timeouts instead. */
858 g_socket_set_timeout (client, 0);
860 cancellable = g_cancellable_new ();
862 /* Check for timeouts when no server is running. */
865 GInputMessage im = { NULL, };
866 GInputVector iv = { NULL, };
870 iv.size = sizeof (buf);
875 memset (buf, 0, sizeof (buf));
877 /* Try a non-blocking read. */
878 g_socket_set_blocking (client, FALSE);
879 len = g_socket_receive_messages (client, &im, 1, 0 /* flags */,
881 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
882 g_assert_cmpint (len, ==, -1);
883 g_clear_error (&error);
885 /* Try a timeout read. Can’t really validate the time taken more than
886 * checking it’s positive. */
887 g_socket_set_timeout (client, 1);
888 g_socket_set_blocking (client, TRUE);
889 start_time = g_get_monotonic_time ();
890 len = g_socket_receive_messages (client, &im, 1, 0 /* flags */,
892 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
893 g_assert_cmpint (len, ==, -1);
894 g_assert_cmpint (g_get_monotonic_time () - start_time, >, 0);
895 g_clear_error (&error);
897 /* Try a blocking read, cancelled from another thread. */
898 g_socket_set_timeout (client, 0);
899 cancellable_thread = g_thread_new ("cancellable",
900 cancellable_thread_cb,
901 g_object_ref (cancellable));
903 start_time = g_get_monotonic_time ();
904 len = g_socket_receive_messages (client, &im, 1, 0 /* flags */,
905 cancellable, &error);
906 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
907 g_assert_cmpint (len, ==, -1);
908 g_assert_cmpint (g_get_monotonic_time () - start_time, >, 0);
909 g_clear_error (&error);
911 g_thread_join (cancellable_thread);
914 g_socket_close (client, &error);
915 g_assert_no_error (error);
917 g_object_unref (client);
918 g_object_unref (cancellable);
922 test_ipv4_sync_dgram_timeouts (void)
924 test_ip_sync_dgram_timeouts (G_SOCKET_FAMILY_IPV4);
928 test_ipv6_sync_dgram_timeouts (void)
932 g_test_skip ("No support for IPv6");
936 test_ip_sync_dgram_timeouts (G_SOCKET_FAMILY_IPV6);
940 graceful_server_thread (gpointer user_data)
942 IPTestData *data = user_data;
944 GError *error = NULL;
947 sock = g_socket_accept (data->server, NULL, &error);
948 g_assert_no_error (error);
950 len = g_socket_send (sock, testbuf, strlen (testbuf) + 1, NULL, &error);
951 g_assert_no_error (error);
952 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
958 test_close_graceful (void)
960 GSocketFamily family = G_SOCKET_FAMILY_IPV4;
962 GError *error = NULL;
963 GSocket *client, *server;
964 GSocketAddress *addr;
968 data = create_server (family, graceful_server_thread, FALSE, &error);
971 gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
972 g_test_skip (message);
974 g_clear_error (&error);
978 addr = g_socket_get_local_address (data->server, &error);
979 g_assert_no_error (error);
981 client = g_socket_new (family,
982 G_SOCKET_TYPE_STREAM,
983 G_SOCKET_PROTOCOL_DEFAULT,
985 g_assert_no_error (error);
987 g_assert_cmpint (g_socket_get_family (client), ==, family);
988 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
989 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
991 g_socket_set_blocking (client, TRUE);
992 g_socket_set_timeout (client, 1);
994 g_socket_connect (client, addr, NULL, &error);
995 g_assert_no_error (error);
996 g_assert (g_socket_is_connected (client));
997 g_object_unref (addr);
999 server = g_thread_join (data->thread);
1001 /* similar to g_tcp_connection_set_graceful_disconnect(), but explicit */
1002 g_socket_shutdown (server, FALSE, TRUE, &error);
1003 g_assert_no_error (error);
1005 /* we must timeout */
1006 g_socket_condition_wait (client, G_IO_HUP, NULL, &error);
1007 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
1008 g_clear_error (&error);
1010 /* check that the remaining data is received */
1011 len = g_socket_receive (client, buf, strlen (testbuf) + 1, NULL, &error);
1012 g_assert_no_error (error);
1013 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
1015 /* and only then the connection is closed */
1016 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
1017 g_assert_no_error (error);
1018 g_assert_cmpint (len, ==, 0);
1020 g_socket_close (server, &error);
1021 g_assert_no_error (error);
1023 g_socket_close (client, &error);
1024 g_assert_no_error (error);
1026 g_object_unref (server);
1027 g_object_unref (data->server);
1028 g_object_unref (client);
1030 g_slice_free (IPTestData, data);
1033 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
1035 v4mapped_server_thread (gpointer user_data)
1037 IPTestData *data = user_data;
1039 GError *error = NULL;
1040 GSocketAddress *addr;
1042 sock = g_socket_accept (data->server, NULL, &error);
1043 g_assert_no_error (error);
1045 g_assert_cmpint (g_socket_get_family (sock), ==, G_SOCKET_FAMILY_IPV6);
1047 addr = g_socket_get_local_address (sock, &error);
1048 g_assert_no_error (error);
1049 g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
1050 g_object_unref (addr);
1052 addr = g_socket_get_remote_address (sock, &error);
1053 g_assert_no_error (error);
1054 g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
1055 g_object_unref (addr);
1057 g_socket_close (sock, &error);
1058 g_assert_no_error (error);
1059 g_object_unref (sock);
1064 test_ipv6_v4mapped (void)
1067 GError *error = NULL;
1069 GSocketAddress *addr, *v4addr;
1070 GInetAddress *iaddr;
1072 if (!ipv6_supported)
1074 g_test_skip ("No support for IPv6");
1078 data = create_server (G_SOCKET_FAMILY_IPV6, v4mapped_server_thread, TRUE, &error);
1081 gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
1082 g_test_skip (message);
1084 g_clear_error (&error);
1088 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1089 G_SOCKET_TYPE_STREAM,
1090 G_SOCKET_PROTOCOL_DEFAULT,
1092 g_assert_no_error (error);
1094 g_socket_set_blocking (client, TRUE);
1095 g_socket_set_timeout (client, 1);
1097 addr = g_socket_get_local_address (data->server, &error);
1098 g_assert_no_error (error);
1099 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1100 v4addr = g_inet_socket_address_new (iaddr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
1101 g_object_unref (iaddr);
1102 g_object_unref (addr);
1104 g_socket_connect (client, v4addr, NULL, &error);
1105 g_assert_no_error (error);
1106 g_assert (g_socket_is_connected (client));
1108 g_thread_join (data->thread);
1110 g_socket_close (client, &error);
1111 g_assert_no_error (error);
1112 g_socket_close (data->server, &error);
1113 g_assert_no_error (error);
1115 g_object_unref (data->server);
1116 g_object_unref (client);
1117 g_object_unref (v4addr);
1119 g_slice_free (IPTestData, data);
1124 test_timed_wait (void)
1127 GError *error = NULL;
1129 GSocketAddress *addr;
1133 data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE, &error);
1136 gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
1137 g_test_skip (message);
1139 g_clear_error (&error);
1143 addr = g_socket_get_local_address (data->server, &error);
1144 g_assert_no_error (error);
1146 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1147 G_SOCKET_TYPE_STREAM,
1148 G_SOCKET_PROTOCOL_DEFAULT,
1150 g_assert_no_error (error);
1152 g_socket_set_blocking (client, TRUE);
1153 g_socket_set_timeout (client, 1);
1155 g_socket_connect (client, addr, NULL, &error);
1156 g_assert_no_error (error);
1157 g_object_unref (addr);
1159 start_time = g_get_monotonic_time ();
1160 g_socket_condition_timed_wait (client, G_IO_IN, 100000 /* 100 ms */,
1162 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
1163 g_clear_error (&error);
1164 poll_duration = g_get_monotonic_time () - start_time;
1166 g_assert_cmpint (poll_duration, >=, 98000);
1167 g_assert_cmpint (poll_duration, <, 112000);
1169 g_socket_close (client, &error);
1170 g_assert_no_error (error);
1172 g_thread_join (data->thread);
1174 g_socket_close (data->server, &error);
1175 g_assert_no_error (error);
1177 g_object_unref (data->server);
1178 g_object_unref (client);
1180 g_slice_free (IPTestData, data);
1184 duplicate_fd (int fd)
1189 if (!DuplicateHandle (GetCurrentProcess (),
1191 GetCurrentProcess (),
1195 DUPLICATE_SAME_ACCESS))
1207 test_fd_reuse (void)
1210 GError *error = NULL;
1213 GSocketAddress *addr;
1218 g_test_bug ("741707");
1220 data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE, &error);
1223 gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
1224 g_test_skip (message);
1226 g_clear_error (&error);
1230 addr = g_socket_get_local_address (data->server, &error);
1231 g_assert_no_error (error);
1233 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1234 G_SOCKET_TYPE_STREAM,
1235 G_SOCKET_PROTOCOL_DEFAULT,
1237 g_assert_no_error (error);
1239 g_socket_set_blocking (client, TRUE);
1240 g_socket_set_timeout (client, 1);
1242 g_socket_connect (client, addr, NULL, &error);
1243 g_assert_no_error (error);
1244 g_assert (g_socket_is_connected (client));
1245 g_object_unref (addr);
1247 /* we have to dup otherwise the fd gets closed twice on unref */
1248 fd = duplicate_fd (g_socket_get_fd (client));
1249 client2 = g_socket_new_from_fd (fd, &error);
1250 g_assert_no_error (error);
1252 g_assert_cmpint (g_socket_get_family (client2), ==, g_socket_get_family (client));
1253 g_assert_cmpint (g_socket_get_socket_type (client2), ==, g_socket_get_socket_type (client));
1254 g_assert_cmpint (g_socket_get_protocol (client2), ==, G_SOCKET_PROTOCOL_TCP);
1256 len = g_socket_send (client2, testbuf, strlen (testbuf) + 1, NULL, &error);
1257 g_assert_no_error (error);
1258 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
1260 len = g_socket_receive (client2, buf, sizeof (buf), NULL, &error);
1261 g_assert_no_error (error);
1262 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
1264 g_assert_cmpstr (testbuf, ==, buf);
1266 g_socket_shutdown (client, FALSE, TRUE, &error);
1267 g_assert_no_error (error);
1268 /* The semantics of dup()+shutdown() are ambiguous; this call will succeed
1269 * on Linux, but return ENOTCONN on OS X.
1271 g_socket_shutdown (client2, FALSE, TRUE, NULL);
1273 g_thread_join (data->thread);
1275 g_socket_close (client, &error);
1276 g_assert_no_error (error);
1277 g_socket_close (client2, &error);
1278 g_assert_no_error (error);
1279 g_socket_close (data->server, &error);
1280 g_assert_no_error (error);
1282 g_assert_cmpint (g_socket_get_fd (client), ==, -1);
1283 g_assert_cmpint (g_socket_get_fd (client2), ==, -1);
1284 g_assert_cmpint (g_socket_get_fd (data->server), ==, -1);
1286 g_object_unref (data->server);
1287 g_object_unref (client);
1288 g_object_unref (client2);
1290 g_slice_free (IPTestData, data);
1294 test_sockaddr (void)
1296 struct sockaddr_in6 sin6, gsin6;
1297 GSocketAddress *saddr;
1298 GInetSocketAddress *isaddr;
1299 GInetAddress *iaddr;
1300 GError *error = NULL;
1302 memset (&sin6, 0, sizeof (sin6));
1303 sin6.sin6_family = AF_INET6;
1304 sin6.sin6_addr = in6addr_loopback;
1305 sin6.sin6_port = g_htons (42);
1306 sin6.sin6_scope_id = 17;
1307 sin6.sin6_flowinfo = 1729;
1309 saddr = g_socket_address_new_from_native (&sin6, sizeof (sin6));
1310 g_assert (G_IS_INET_SOCKET_ADDRESS (saddr));
1312 isaddr = G_INET_SOCKET_ADDRESS (saddr);
1313 iaddr = g_inet_socket_address_get_address (isaddr);
1314 g_assert_cmpint (g_inet_address_get_family (iaddr), ==, G_SOCKET_FAMILY_IPV6);
1315 g_assert (g_inet_address_get_is_loopback (iaddr));
1317 g_assert_cmpint (g_inet_socket_address_get_port (isaddr), ==, 42);
1318 g_assert_cmpint (g_inet_socket_address_get_scope_id (isaddr), ==, 17);
1319 g_assert_cmpint (g_inet_socket_address_get_flowinfo (isaddr), ==, 1729);
1321 g_socket_address_to_native (saddr, &gsin6, sizeof (gsin6), &error);
1322 g_assert_no_error (error);
1324 g_assert (memcmp (&sin6.sin6_addr, &gsin6.sin6_addr, sizeof (struct in6_addr)) == 0);
1325 g_assert_cmpint (sin6.sin6_port, ==, gsin6.sin6_port);
1326 g_assert_cmpint (sin6.sin6_scope_id, ==, gsin6.sin6_scope_id);
1327 g_assert_cmpint (sin6.sin6_flowinfo, ==, gsin6.sin6_flowinfo);
1329 g_object_unref (saddr);
1334 test_unix_from_fd (void)
1340 fd = socket (AF_UNIX, SOCK_STREAM, 0);
1341 g_assert_cmpint (fd, !=, -1);
1344 s = g_socket_new_from_fd (fd, &error);
1345 g_assert_no_error (error);
1346 g_assert_cmpint (g_socket_get_family (s), ==, G_SOCKET_FAMILY_UNIX);
1347 g_assert_cmpint (g_socket_get_socket_type (s), ==, G_SOCKET_TYPE_STREAM);
1348 g_assert_cmpint (g_socket_get_protocol (s), ==, G_SOCKET_PROTOCOL_DEFAULT);
1353 test_unix_connection (void)
1358 GSocketConnection *c;
1360 fd = socket (AF_UNIX, SOCK_STREAM, 0);
1361 g_assert_cmpint (fd, !=, -1);
1364 s = g_socket_new_from_fd (fd, &error);
1365 g_assert_no_error (error);
1366 c = g_socket_connection_factory_create_connection (s);
1367 g_assert (G_IS_UNIX_CONNECTION (c));
1372 static GSocketConnection *
1373 create_connection_for_fd (int fd)
1377 GSocketConnection *connection;
1379 socket = g_socket_new_from_fd (fd, &err);
1380 g_assert_no_error (err);
1381 g_assert (G_IS_SOCKET (socket));
1382 connection = g_socket_connection_factory_create_connection (socket);
1383 g_assert (G_IS_UNIX_CONNECTION (connection));
1384 g_object_unref (socket);
1388 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
1391 test_unix_connection_ancillary_data (void)
1395 gint status, fd, len;
1400 g_assert_cmpint (status, ==, 0);
1402 status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
1403 g_assert_cmpint (status, ==, 0);
1406 g_assert_cmpint (pid, >=, 0);
1408 /* Child: close its copy of the write end of the pipe, receive it
1409 * again from the parent over the socket, and write some text to it.
1411 * Parent: send the write end of the pipe (still open for the
1412 * parent) over the socket, close it, and read some text from the
1413 * read end of the pipe.
1417 GSocketConnection *connection;
1420 connection = create_connection_for_fd (sv[0]);
1422 status = close (pv[1]);
1423 g_assert_cmpint (status, ==, 0);
1426 fd = g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection), NULL,
1428 g_assert_no_error (err);
1429 g_assert_cmpint (fd, >, -1);
1430 g_object_unref (connection);
1433 len = write (fd, TEST_DATA, sizeof (TEST_DATA));
1434 while (len == -1 && errno == EINTR);
1435 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
1440 GSocketConnection *connection;
1443 connection = create_connection_for_fd (sv[1]);
1446 g_unix_connection_send_fd (G_UNIX_CONNECTION (connection), pv[1], NULL,
1448 g_assert_no_error (err);
1449 g_object_unref (connection);
1451 status = close (pv[1]);
1452 g_assert_cmpint (status, ==, 0);
1454 memset (buffer, 0xff, sizeof buffer);
1456 len = read (pv[0], buffer, sizeof buffer);
1457 while (len == -1 && errno == EINTR);
1459 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
1460 g_assert_cmpstr (buffer, ==, TEST_DATA);
1462 waitpid (pid, &status, 0);
1463 g_assert (WIFEXITED (status));
1464 g_assert_cmpint (WEXITSTATUS (status), ==, 0);
1467 /* TODO: add test for g_unix_connection_send_credentials() and
1468 * g_unix_connection_receive_credentials().
1473 postmortem_source_cb (GSocket *socket,
1474 GIOCondition condition,
1477 gboolean *been_here = user_data;
1479 g_assert_cmpint (condition, ==, G_IO_NVAL);
1486 test_source_postmortem (void)
1488 GMainContext *context;
1491 GError *error = NULL;
1492 gboolean callback_visited = FALSE;
1494 socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error);
1495 g_assert_no_error (error);
1497 context = g_main_context_new ();
1499 source = g_socket_create_source (socket, G_IO_IN, NULL);
1500 g_source_set_callback (source, (GSourceFunc) postmortem_source_cb,
1501 &callback_visited, NULL);
1502 g_source_attach (source, context);
1503 g_source_unref (source);
1505 g_socket_close (socket, &error);
1506 g_assert_no_error (error);
1507 g_object_unref (socket);
1509 /* Test that, after a socket is closed, its source callback should be called
1511 g_main_context_iteration (context, FALSE);
1512 g_assert (callback_visited);
1513 g_assert (!g_main_context_pending (context));
1515 g_main_context_unref (context);
1518 #endif /* G_OS_UNIX */
1521 test_reuse_tcp (void)
1523 GSocket *sock1, *sock2;
1524 GError *error = NULL;
1525 GInetAddress *iaddr;
1526 GSocketAddress *addr;
1528 sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1529 G_SOCKET_TYPE_STREAM,
1530 G_SOCKET_PROTOCOL_DEFAULT,
1532 g_assert_no_error (error);
1534 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1535 addr = g_inet_socket_address_new (iaddr, 0);
1536 g_object_unref (iaddr);
1537 g_socket_bind (sock1, addr, TRUE, &error);
1538 g_object_unref (addr);
1539 g_assert_no_error (error);
1541 g_socket_listen (sock1, &error);
1542 g_assert_no_error (error);
1544 sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1545 G_SOCKET_TYPE_STREAM,
1546 G_SOCKET_PROTOCOL_DEFAULT,
1548 g_assert_no_error (error);
1550 addr = g_socket_get_local_address (sock1, &error);
1551 g_assert_no_error (error);
1552 g_socket_bind (sock2, addr, TRUE, &error);
1553 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_ADDRESS_IN_USE);
1554 g_clear_error (&error);
1555 g_object_unref (addr);
1557 g_object_unref (sock1);
1558 g_object_unref (sock2);
1562 test_reuse_udp (void)
1564 GSocket *sock1, *sock2;
1565 GError *error = NULL;
1566 GInetAddress *iaddr;
1567 GSocketAddress *addr;
1569 sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1570 G_SOCKET_TYPE_DATAGRAM,
1571 G_SOCKET_PROTOCOL_DEFAULT,
1573 g_assert_no_error (error);
1575 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1576 addr = g_inet_socket_address_new (iaddr, 0);
1577 g_object_unref (iaddr);
1578 g_socket_bind (sock1, addr, TRUE, &error);
1579 g_object_unref (addr);
1580 g_assert_no_error (error);
1582 sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1583 G_SOCKET_TYPE_DATAGRAM,
1584 G_SOCKET_PROTOCOL_DEFAULT,
1586 g_assert_no_error (error);
1588 addr = g_socket_get_local_address (sock1, &error);
1589 g_assert_no_error (error);
1590 g_socket_bind (sock2, addr, TRUE, &error);
1591 g_object_unref (addr);
1592 g_assert_no_error (error);
1594 g_object_unref (sock1);
1595 g_object_unref (sock2);
1599 test_get_available (gconstpointer user_data)
1601 GSocketType socket_type = GPOINTER_TO_UINT (user_data);
1603 GSocket *listener, *server, *client;
1605 GSocketAddress *saddr, *boundaddr;
1606 gchar data[] = "0123456789abcdef";
1610 listener = g_socket_new (G_SOCKET_FAMILY_IPV4,
1612 G_SOCKET_PROTOCOL_DEFAULT,
1614 g_assert_no_error (err);
1615 g_assert (G_IS_SOCKET (listener));
1617 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1619 G_SOCKET_PROTOCOL_DEFAULT,
1621 g_assert_no_error (err);
1622 g_assert (G_IS_SOCKET (client));
1624 if (socket_type == G_SOCKET_TYPE_STREAM)
1626 g_socket_set_option (client, IPPROTO_TCP, TCP_NODELAY, TRUE, &err);
1627 g_assert_no_error (err);
1630 addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
1631 saddr = g_inet_socket_address_new (addr, 0);
1633 g_socket_bind (listener, saddr, TRUE, &err);
1634 g_assert_no_error (err);
1635 g_object_unref (saddr);
1636 g_object_unref (addr);
1638 boundaddr = g_socket_get_local_address (listener, &err);
1639 g_assert_no_error (err);
1641 addr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1642 saddr = g_inet_socket_address_new (addr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (boundaddr)));
1643 g_object_unref (addr);
1644 g_object_unref (boundaddr);
1646 if (socket_type == G_SOCKET_TYPE_STREAM)
1648 g_socket_listen (listener, &err);
1649 g_assert_no_error (err);
1650 g_socket_connect (client, saddr, NULL, &err);
1651 g_assert_no_error (err);
1653 server = g_socket_accept (listener, NULL, &err);
1654 g_assert_no_error (err);
1655 g_socket_set_blocking (server, FALSE);
1656 g_object_unref (listener);
1661 g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
1662 g_assert_no_error (err);
1664 while (!g_socket_condition_wait (server, G_IO_IN, NULL, NULL))
1666 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1668 g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
1669 g_assert_no_error (err);
1671 /* We need to wait until the data has actually been copied into the
1672 * server socket's buffers, but g_socket_condition_wait() won't help
1673 * here since the socket is definitely already readable. So there's
1674 * a race condition in checking its available bytes. In the TCP
1675 * case, we poll for a bit until the new data shows up. In the UDP
1676 * case, there's not much we can do, but at least the failure mode
1677 * is passes-when-it-shouldn't, not fails-when-it-shouldn't.
1679 if (socket_type == G_SOCKET_TYPE_STREAM)
1683 for (tries = 0; tries < 100; tries++)
1685 if (g_socket_get_available_bytes (server) > sizeof (data))
1690 g_assert_cmpint (g_socket_get_available_bytes (server), ==, 2 * sizeof (data));
1695 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1698 g_assert_cmpint (sizeof (buf), >=, 2 * sizeof (data));
1699 nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
1700 g_assert_no_error (err);
1702 if (socket_type == G_SOCKET_TYPE_STREAM)
1704 g_assert_cmpint (nread, ==, 2 * sizeof (data));
1705 g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1709 g_assert_cmpint (nread, ==, sizeof (data));
1710 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1713 nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
1714 if (socket_type == G_SOCKET_TYPE_STREAM)
1716 g_assert_cmpint (nread, ==, -1);
1717 g_assert_error (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
1718 g_clear_error (&err);
1722 g_assert_cmpint (nread, ==, sizeof (data));
1723 g_assert_no_error (err);
1726 g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1728 g_socket_close (server, &err);
1729 g_assert_no_error (err);
1731 g_object_unref (saddr);
1732 g_object_unref (server);
1733 g_object_unref (client);
1739 const guint8 *write_data;
1741 } TestReadWriteData;
1744 test_read_write_write_thread (gpointer user_data)
1746 TestReadWriteData *data = user_data;
1747 gsize bytes_written;
1748 GError *error = NULL;
1751 res = g_output_stream_write_all (data->os, data->write_data, 1024, &bytes_written, NULL, &error);
1752 g_assert_true (res);
1753 g_assert_no_error (error);
1754 g_assert_cmpint (bytes_written, ==, 1024);
1760 test_read_write_read_thread (gpointer user_data)
1762 TestReadWriteData *data = user_data;
1764 GError *error = NULL;
1767 res = g_input_stream_read_all (data->is, data->read_data, 1024, &bytes_read, NULL, &error);
1768 g_assert_true (res);
1769 g_assert_no_error (error);
1770 g_assert_cmpint (bytes_read, ==, 1024);
1776 test_read_write_writev_thread (gpointer user_data)
1778 TestReadWriteData *data = user_data;
1779 gsize bytes_written;
1780 GError *error = NULL;
1782 GOutputVector vectors[3];
1784 vectors[0].buffer = data->write_data;
1785 vectors[0].size = 256;
1786 vectors[1].buffer = data->write_data + 256;
1787 vectors[1].size = 256;
1788 vectors[2].buffer = data->write_data + 512;
1789 vectors[2].size = 512;
1791 res = g_output_stream_writev_all (data->os, vectors, G_N_ELEMENTS (vectors), &bytes_written, NULL, &error);
1792 g_assert_true (res);
1793 g_assert_no_error (error);
1794 g_assert_cmpint (bytes_written, ==, 1024);
1799 /* test if normal read/write/writev via the GSocket*Streams works on TCP sockets */
1801 test_read_write (gconstpointer user_data)
1803 gboolean writev = GPOINTER_TO_INT (user_data);
1805 GSocket *listener, *server, *client;
1807 GSocketAddress *saddr, *boundaddr;
1808 TestReadWriteData data;
1809 guint8 data_write[1024], data_read[1024];
1810 GSocketConnection *server_stream, *client_stream;
1811 GThread *write_thread, *read_thread;
1814 listener = g_socket_new (G_SOCKET_FAMILY_IPV4,
1815 G_SOCKET_TYPE_STREAM,
1816 G_SOCKET_PROTOCOL_DEFAULT,
1818 g_assert_no_error (err);
1819 g_assert (G_IS_SOCKET (listener));
1821 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1822 G_SOCKET_TYPE_STREAM,
1823 G_SOCKET_PROTOCOL_DEFAULT,
1825 g_assert_no_error (err);
1826 g_assert (G_IS_SOCKET (client));
1828 addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
1829 saddr = g_inet_socket_address_new (addr, 0);
1831 g_socket_bind (listener, saddr, TRUE, &err);
1832 g_assert_no_error (err);
1833 g_object_unref (saddr);
1834 g_object_unref (addr);
1836 boundaddr = g_socket_get_local_address (listener, &err);
1837 g_assert_no_error (err);
1839 g_socket_listen (listener, &err);
1840 g_assert_no_error (err);
1842 addr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1843 saddr = g_inet_socket_address_new (addr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (boundaddr)));
1844 g_object_unref (addr);
1845 g_object_unref (boundaddr);
1847 g_socket_connect (client, saddr, NULL, &err);
1848 g_assert_no_error (err);
1850 server = g_socket_accept (listener, NULL, &err);
1851 g_assert_no_error (err);
1852 g_socket_set_blocking (server, FALSE);
1853 g_object_unref (listener);
1855 server_stream = g_socket_connection_factory_create_connection (server);
1856 g_assert_nonnull (server_stream);
1857 client_stream = g_socket_connection_factory_create_connection (client);
1858 g_assert_nonnull (client_stream);
1860 for (i = 0; i < sizeof (data_write); i++)
1863 data.is = g_io_stream_get_input_stream (G_IO_STREAM (server_stream));
1864 data.os = g_io_stream_get_output_stream (G_IO_STREAM (client_stream));
1865 data.read_data = data_read;
1866 data.write_data = data_write;
1869 write_thread = g_thread_new ("writer", test_read_write_writev_thread, &data);
1871 write_thread = g_thread_new ("writer", test_read_write_write_thread, &data);
1872 read_thread = g_thread_new ("reader", test_read_write_read_thread, &data);
1874 g_thread_join (write_thread);
1875 g_thread_join (read_thread);
1877 g_assert_cmpmem (data_write, sizeof data_write, data_read, sizeof data_read);
1879 g_socket_close (server, &err);
1880 g_assert_no_error (err);
1882 g_object_unref (server_stream);
1883 g_object_unref (client_stream);
1885 g_object_unref (saddr);
1886 g_object_unref (server);
1887 g_object_unref (client);
1895 GError *error = NULL;
1897 g_test_init (&argc, &argv, NULL);
1898 g_test_bug_base ("https://bugzilla.gnome.org/");
1900 sock = g_socket_new (G_SOCKET_FAMILY_IPV6,
1901 G_SOCKET_TYPE_STREAM,
1902 G_SOCKET_PROTOCOL_DEFAULT,
1906 ipv6_supported = TRUE;
1907 g_object_unref (sock);
1911 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
1912 g_clear_error (&error);
1915 g_test_add_func ("/socket/ipv4_sync", test_ipv4_sync);
1916 g_test_add_func ("/socket/ipv4_async", test_ipv4_async);
1917 g_test_add_func ("/socket/ipv6_sync", test_ipv6_sync);
1918 g_test_add_func ("/socket/ipv6_async", test_ipv6_async);
1919 g_test_add_func ("/socket/ipv4_sync/datagram", test_ipv4_sync_dgram);
1920 g_test_add_func ("/socket/ipv4_sync/datagram/timeouts", test_ipv4_sync_dgram_timeouts);
1921 g_test_add_func ("/socket/ipv6_sync/datagram", test_ipv6_sync_dgram);
1922 g_test_add_func ("/socket/ipv6_sync/datagram/timeouts", test_ipv6_sync_dgram_timeouts);
1923 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
1924 g_test_add_func ("/socket/ipv6_v4mapped", test_ipv6_v4mapped);
1926 g_test_add_func ("/socket/close_graceful", test_close_graceful);
1927 g_test_add_func ("/socket/timed_wait", test_timed_wait);
1928 g_test_add_func ("/socket/fd_reuse", test_fd_reuse);
1929 g_test_add_func ("/socket/address", test_sockaddr);
1931 g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
1932 g_test_add_func ("/socket/unix-connection", test_unix_connection);
1933 g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data);
1934 g_test_add_func ("/socket/source-postmortem", test_source_postmortem);
1936 g_test_add_func ("/socket/reuse/tcp", test_reuse_tcp);
1937 g_test_add_func ("/socket/reuse/udp", test_reuse_udp);
1938 g_test_add_data_func ("/socket/get_available/datagram", GUINT_TO_POINTER (G_SOCKET_TYPE_DATAGRAM),
1939 test_get_available);
1940 g_test_add_data_func ("/socket/get_available/stream", GUINT_TO_POINTER (G_SOCKET_TYPE_STREAM),
1941 test_get_available);
1942 g_test_add_data_func ("/socket/read_write", GUINT_TO_POINTER (FALSE),
1944 g_test_add_data_func ("/socket/read_writev", GUINT_TO_POINTER (TRUE),
1947 return g_test_run();