1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2011 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 #include <glib/gstdio.h>
23 #include "glib-private.h"
25 #include <gio/gcredentialsprivate.h>
26 #include <gio/gunixconnection.h>
33 #include <gio/gnetworking.h>
37 #include "giowin32-afunix.h"
41 #include "gnetworkingprivate.h"
43 static gboolean ipv6_supported;
51 GCancellable *cancellable; /* to shut down dgram echo server thread */
55 echo_server_dgram_thread (gpointer user_data)
57 IPTestData *data = user_data;
59 GCancellable *cancellable = data->cancellable;
69 nread = g_socket_receive_from (sock, &sa, buf, sizeof (buf), cancellable, &error);
70 if (error && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
72 g_assert_no_error (error);
73 g_assert_cmpint (nread, >=, 0);
75 nwrote = g_socket_send_to (sock, sa, buf, nread, cancellable, &error);
76 if (error && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
78 g_assert_no_error (error);
79 g_assert_cmpint (nwrote, ==, nread);
84 g_clear_error (&error);
90 echo_server_thread (gpointer user_data)
92 IPTestData *data = user_data;
98 sock = g_socket_accept (data->server, NULL, &error);
99 g_assert_no_error (error);
103 nread = g_socket_receive (sock, buf, sizeof (buf), NULL, &error);
104 g_assert_no_error (error);
105 g_assert_cmpint (nread, >=, 0);
110 nwrote = g_socket_send (sock, buf, nread, NULL, &error);
111 g_assert_no_error (error);
112 g_assert_cmpint (nwrote, ==, nread);
115 g_socket_close (sock, &error);
116 g_assert_no_error (error);
117 g_object_unref (sock);
122 create_server_full (GSocketFamily family,
123 GSocketType socket_type,
124 GThreadFunc server_thread,
130 GSocketAddress *addr;
133 data = g_slice_new (IPTestData);
134 data->family = family;
136 data->server = server = g_socket_new (family,
138 G_SOCKET_PROTOCOL_DEFAULT,
143 g_assert_cmpint (g_socket_get_family (server), ==, family);
144 g_assert_cmpint (g_socket_get_socket_type (server), ==, socket_type);
145 g_assert_cmpint (g_socket_get_protocol (server), ==, G_SOCKET_PROTOCOL_DEFAULT);
147 g_assert (GLIB_PRIVATE_CALL (g_win32_handle_is_socket) ((HANDLE)(gintptr) g_socket_get_fd (server)));
150 g_socket_set_blocking (server, TRUE);
152 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
155 g_socket_set_option (data->server, IPPROTO_IPV6, IPV6_V6ONLY, FALSE, NULL);
156 if (!g_socket_speaks_ipv4 (data->server))
158 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
159 "IPv6-only server cannot speak IPv4");
166 iaddr = g_inet_address_new_any (family);
168 iaddr = g_inet_address_new_loopback (family);
169 addr = g_inet_socket_address_new (iaddr, 0);
170 g_object_unref (iaddr);
172 g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), ==, 0);
173 if (!g_socket_bind (server, addr, TRUE, error))
175 g_object_unref (addr);
178 g_object_unref (addr);
180 addr = g_socket_get_local_address (server, error);
183 g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), !=, 0);
184 g_object_unref (addr);
186 if (socket_type == G_SOCKET_TYPE_STREAM)
188 if (!g_socket_listen (server, error))
193 data->cancellable = g_cancellable_new ();
196 data->thread = g_thread_new ("server", server_thread, data);
201 g_clear_object (&data->server);
202 g_slice_free (IPTestData, data);
208 create_server (GSocketFamily family,
209 GThreadFunc server_thread,
213 return create_server_full (family, G_SOCKET_TYPE_STREAM, server_thread, v4mapped, error);
216 static const gchar *testbuf = "0123456789abcdef";
219 test_ip_async_read_ready (GSocket *client,
223 IPTestData *data = user_data;
224 GError *error = NULL;
228 g_assert_cmpint (cond, ==, G_IO_IN);
230 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
231 g_assert_no_error (error);
232 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
234 g_assert_cmpstr (testbuf, ==, buf);
236 g_main_loop_quit (data->loop);
242 test_ip_async_write_ready (GSocket *client,
246 IPTestData *data = user_data;
247 GError *error = NULL;
251 g_assert_cmpint (cond, ==, G_IO_OUT);
253 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
254 g_assert_no_error (error);
255 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
257 source = g_socket_create_source (client, G_IO_IN, NULL);
258 g_source_set_callback (source, (GSourceFunc)test_ip_async_read_ready,
260 g_source_attach (source, NULL);
261 g_source_unref (source);
267 test_ip_async_timed_out (GSocket *client,
271 IPTestData *data = user_data;
272 GError *error = NULL;
277 if (data->family == G_SOCKET_FAMILY_IPV4)
279 g_assert_cmpint (cond, ==, G_IO_IN);
280 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
281 g_assert_cmpint (len, ==, -1);
282 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
283 g_clear_error (&error);
286 source = g_socket_create_source (client, G_IO_OUT, NULL);
287 g_source_set_callback (source, (GSourceFunc)test_ip_async_write_ready,
289 g_source_attach (source, NULL);
290 g_source_unref (source);
296 test_ip_async_connected (GSocket *client,
300 IPTestData *data = user_data;
301 GError *error = NULL;
306 g_socket_check_connect_result (client, &error);
307 g_assert_no_error (error);
308 /* We do this after the check_connect_result, since that will give a
309 * more useful assertion in case of error.
311 g_assert_cmpint (cond, ==, G_IO_OUT);
313 g_assert (g_socket_is_connected (client));
315 /* This adds 1 second to "make check", so let's just only do it once. */
316 if (data->family == G_SOCKET_FAMILY_IPV4)
318 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
319 g_assert_cmpint (len, ==, -1);
320 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
321 g_clear_error (&error);
323 source = g_socket_create_source (client, G_IO_IN, NULL);
324 g_source_set_callback (source, (GSourceFunc)test_ip_async_timed_out,
326 g_source_attach (source, NULL);
327 g_source_unref (source);
330 test_ip_async_timed_out (client, 0, data);
336 idle_test_ip_async_connected (gpointer user_data)
338 IPTestData *data = user_data;
340 return test_ip_async_connected (data->client, G_IO_OUT, data);
344 test_ip_async (GSocketFamily family)
347 GError *error = NULL;
349 GSocketAddress *addr;
354 data = create_server (family, echo_server_thread, FALSE, &error);
357 g_test_skip_printf ("Failed to create server: %s", error->message);
358 g_clear_error (&error);
361 g_assert_nonnull (data);
363 addr = g_socket_get_local_address (data->server, &error);
364 g_assert_no_error (error);
366 client = g_socket_new (family,
367 G_SOCKET_TYPE_STREAM,
368 G_SOCKET_PROTOCOL_DEFAULT,
370 g_assert_no_error (error);
371 data->client = client;
373 g_assert_cmpint (g_socket_get_family (client), ==, family);
374 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
375 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
377 g_socket_set_blocking (client, FALSE);
378 g_socket_set_timeout (client, 1);
380 if (g_socket_connect (client, addr, NULL, &error))
382 g_assert_no_error (error);
383 g_idle_add (idle_test_ip_async_connected, data);
387 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
388 g_clear_error (&error);
389 source = g_socket_create_source (client, G_IO_OUT, NULL);
390 g_source_set_callback (source, (GSourceFunc)test_ip_async_connected,
392 g_source_attach (source, NULL);
393 g_source_unref (source);
395 g_object_unref (addr);
397 data->loop = g_main_loop_new (NULL, TRUE);
398 g_main_loop_run (data->loop);
399 g_main_loop_unref (data->loop);
401 g_socket_shutdown (client, FALSE, TRUE, &error);
402 g_assert_no_error (error);
404 g_thread_join (data->thread);
406 if (family == G_SOCKET_FAMILY_IPV4)
408 /* Test that reading on a remote-closed socket gets back 0 bytes. */
409 len = g_socket_receive_with_blocking (client, buf, sizeof (buf),
411 g_assert_no_error (error);
412 g_assert_cmpint (len, ==, 0);
416 /* Test that writing to a remote-closed socket gets back CONNECTION_CLOSED. */
417 len = g_socket_send_with_blocking (client, testbuf, strlen (testbuf) + 1,
419 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED);
420 g_assert_cmpint (len, ==, -1);
421 g_clear_error (&error);
424 g_socket_close (client, &error);
425 g_assert_no_error (error);
426 g_socket_close (data->server, &error);
427 g_assert_no_error (error);
429 g_object_unref (data->server);
430 g_object_unref (client);
432 g_slice_free (IPTestData, data);
436 test_ipv4_async (void)
438 test_ip_async (G_SOCKET_FAMILY_IPV4);
442 test_ipv6_async (void)
446 g_test_skip ("No support for IPv6");
450 test_ip_async (G_SOCKET_FAMILY_IPV6);
453 static const gchar testbuf2[] = "0123456789abcdefghijklmnopqrstuvwxyz";
456 test_ip_sync (GSocketFamily family)
459 GError *error = NULL;
461 GSocketAddress *addr;
465 data = create_server (family, echo_server_thread, FALSE, &error);
468 g_test_skip_printf ("Failed to create server: %s", error->message);
469 g_clear_error (&error);
473 addr = g_socket_get_local_address (data->server, &error);
474 g_assert_no_error (error);
476 client = g_socket_new (family,
477 G_SOCKET_TYPE_STREAM,
478 G_SOCKET_PROTOCOL_DEFAULT,
480 g_assert_no_error (error);
482 g_assert_cmpint (g_socket_get_family (client), ==, family);
483 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
484 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
486 g_assert (GLIB_PRIVATE_CALL (g_win32_handle_is_socket) ((HANDLE)(gintptr) g_socket_get_fd (client)));
488 g_socket_set_blocking (client, TRUE);
489 g_socket_set_timeout (client, 1);
491 g_socket_connect (client, addr, NULL, &error);
492 g_assert_no_error (error);
493 g_assert (g_socket_is_connected (client));
494 g_object_unref (addr);
496 /* This adds 1 second to "make check", so let's just only do it once. */
497 if (family == G_SOCKET_FAMILY_IPV4)
499 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
500 g_assert_cmpint (len, ==, -1);
501 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
502 g_clear_error (&error);
505 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
506 g_assert_no_error (error);
507 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
509 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
510 g_assert_no_error (error);
511 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
513 g_assert_cmpstr (testbuf, ==, buf);
516 GOutputVector v[7] = { { NULL, 0 }, };
518 v[0].buffer = testbuf2 + 0;
520 v[1].buffer = testbuf2 + 3;
522 v[2].buffer = testbuf2 + 3 + 5;
524 v[3].buffer = testbuf2 + 3 + 5;
526 v[4].buffer = testbuf2 + 3 + 5 + 6;
528 v[5].buffer = testbuf2 + 3 + 5 + 6 + 2;
530 v[6].buffer = testbuf2 + 3 + 5 + 6 + 2 + 1;
531 v[6].size = strlen (testbuf2) - (3 + 5 + 6 + 2 + 1);
533 len = g_socket_send_message (client, NULL, v, G_N_ELEMENTS (v), NULL, 0, 0, NULL, &error);
534 g_assert_no_error (error);
535 g_assert_cmpint (len, ==, strlen (testbuf2));
537 memset (buf, 0, sizeof (buf));
538 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
539 g_assert_no_error (error);
540 g_assert_cmpint (len, ==, strlen (testbuf2));
541 g_assert_cmpstr (testbuf2, ==, buf);
544 g_socket_shutdown (client, FALSE, TRUE, &error);
545 g_assert_no_error (error);
547 g_thread_join (data->thread);
549 if (family == G_SOCKET_FAMILY_IPV4)
551 /* Test that reading on a remote-closed socket gets back 0 bytes. */
552 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
553 g_assert_no_error (error);
554 g_assert_cmpint (len, ==, 0);
558 /* Test that writing to a remote-closed socket gets back CONNECTION_CLOSED. */
559 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
560 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED);
561 g_assert_cmpint (len, ==, -1);
562 g_clear_error (&error);
565 g_socket_close (client, &error);
566 g_assert_no_error (error);
567 g_socket_close (data->server, &error);
568 g_assert_no_error (error);
570 g_object_unref (data->server);
571 g_object_unref (client);
573 g_slice_free (IPTestData, data);
577 test_ipv4_sync (void)
579 test_ip_sync (G_SOCKET_FAMILY_IPV4);
583 test_ipv6_sync (void)
587 g_test_skip ("No support for IPv6");
591 test_ip_sync (G_SOCKET_FAMILY_IPV6);
595 test_ip_sync_dgram (GSocketFamily family)
598 GError *error = NULL;
600 GSocketAddress *dest_addr;
604 data = create_server_full (family, G_SOCKET_TYPE_DATAGRAM,
605 echo_server_dgram_thread, FALSE, &error);
608 g_test_skip_printf ("Failed to create server: %s", error->message);
609 g_clear_error (&error);
613 dest_addr = g_socket_get_local_address (data->server, &error);
615 client = g_socket_new (family,
616 G_SOCKET_TYPE_DATAGRAM,
617 G_SOCKET_PROTOCOL_DEFAULT,
619 g_assert_no_error (error);
621 g_assert_cmpint (g_socket_get_family (client), ==, family);
622 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_DATAGRAM);
623 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
625 g_assert (GLIB_PRIVATE_CALL (g_win32_handle_is_socket) ((HANDLE)(gintptr) g_socket_get_fd (client)));
628 g_socket_set_blocking (client, TRUE);
629 g_socket_set_timeout (client, 1);
631 len = g_socket_send_to (client, dest_addr, testbuf, strlen (testbuf) + 1, NULL, &error);
632 g_assert_no_error (error);
633 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
635 len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
636 g_assert_no_error (error);
637 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
639 g_assert_cmpstr (testbuf, ==, buf);
642 GOutputMessage m[3] = { { NULL, NULL, 0, 0, NULL, 0 }, };
643 GInputMessage im[3] = { { NULL, NULL, 0, 0, 0, NULL, 0 }, };
644 GOutputVector v[7] = { { NULL, 0 }, };
645 GInputVector iv[7] = { { NULL, 0 }, };
647 v[0].buffer = testbuf2 + 0;
649 v[1].buffer = testbuf2 + 3;
651 v[2].buffer = testbuf2 + 3 + 5;
653 v[3].buffer = testbuf2 + 3 + 5;
655 v[4].buffer = testbuf2 + 3 + 5 + 6;
657 v[5].buffer = testbuf2 + 3 + 5 + 6 + 2;
659 v[6].buffer = testbuf2 + 3 + 5 + 6 + 2 + 1;
660 v[6].size = strlen (testbuf2) - (3 + 5 + 6 + 2 + 1);
662 iv[0].buffer = buf + 0;
664 iv[1].buffer = buf + 3;
666 iv[2].buffer = buf + 3 + 5;
668 iv[3].buffer = buf + 3 + 5;
670 iv[4].buffer = buf + 3 + 5 + 6;
672 iv[5].buffer = buf + 3 + 5 + 6 + 2;
674 iv[6].buffer = buf + 3 + 5 + 6 + 2 + 1;
675 iv[6].size = sizeof (buf) - (3 + 5 + 6 + 2 + 1);
677 len = g_socket_send_message (client, dest_addr, v, G_N_ELEMENTS (v), NULL, 0, 0, NULL, &error);
678 g_assert_no_error (error);
679 g_assert_cmpint (len, ==, strlen (testbuf2));
681 memset (buf, 0, sizeof (buf));
682 len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
683 g_assert_no_error (error);
684 g_assert_cmpint (len, ==, strlen (testbuf2));
685 g_assert_cmpstr (testbuf2, ==, buf);
687 m[0].vectors = &v[0];
688 m[0].num_vectors = 1;
689 m[0].address = dest_addr;
690 m[1].vectors = &v[0];
691 m[1].num_vectors = 6;
692 m[1].address = dest_addr;
693 m[2].vectors = &v[6];
694 m[2].num_vectors = 1;
695 m[2].address = dest_addr;
697 len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
698 g_assert_no_error (error);
699 g_assert_cmpint (len, ==, G_N_ELEMENTS (m));
700 g_assert_cmpint (m[0].bytes_sent, ==, 3);
701 g_assert_cmpint (m[1].bytes_sent, ==, 17);
702 g_assert_cmpint (m[2].bytes_sent, ==, v[6].size);
704 memset (buf, 0, sizeof (buf));
705 len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
706 g_assert_no_error (error);
707 g_assert_cmpint (len, ==, 3);
709 memset (buf, 0, sizeof (buf));
710 len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
711 g_assert_no_error (error);
712 /* v[0].size + v[1].size + v[2].size + v[3].size + v[4].size + v[5].size */
713 g_assert_cmpint (len, ==, 17);
714 g_assert (memcmp (testbuf2, buf, 17) == 0);
716 memset (buf, 0, sizeof (buf));
717 len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
718 g_assert_no_error (error);
719 g_assert_cmpint (len, ==, v[6].size);
720 g_assert_cmpstr (buf, ==, v[6].buffer);
722 /* reset since we're re-using the message structs */
727 /* now try receiving multiple messages */
728 len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
729 g_assert_no_error (error);
730 g_assert_cmpint (len, ==, G_N_ELEMENTS (m));
731 g_assert_cmpint (m[0].bytes_sent, ==, 3);
732 g_assert_cmpint (m[1].bytes_sent, ==, 17);
733 g_assert_cmpint (m[2].bytes_sent, ==, v[6].size);
735 im[0].vectors = &iv[0];
736 im[0].num_vectors = 1;
737 im[1].vectors = &iv[0];
738 im[1].num_vectors = 6;
739 im[2].vectors = &iv[6];
740 im[2].num_vectors = 1;
742 memset (buf, 0, sizeof (buf));
743 len = g_socket_receive_messages (client, im, G_N_ELEMENTS (im), 0,
745 g_assert_no_error (error);
746 g_assert_cmpint (len, ==, G_N_ELEMENTS (im));
748 g_assert_cmpuint (im[0].bytes_received, ==, 3);
749 /* v[0].size + v[1].size + v[2].size + v[3].size + v[4].size + v[5].size */
750 g_assert_cmpuint (im[1].bytes_received, ==, 17);
751 g_assert_cmpuint (im[2].bytes_received, ==, v[6].size);
753 /* reset since we're re-using the message structs */
758 /* now try to generate an early return by omitting the destination address on [1] */
760 len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
761 g_assert_no_error (error);
762 g_assert_cmpint (len, ==, 1);
764 g_assert_cmpint (m[0].bytes_sent, ==, 3);
765 g_assert_cmpint (m[1].bytes_sent, ==, 0);
766 g_assert_cmpint (m[2].bytes_sent, ==, 0);
768 /* reset since we're re-using the message structs */
773 /* now try to generate an error by omitting all destination addresses */
777 len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
778 /* This error code may vary between platforms and over time; it is not guaranteed API: */
780 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
782 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_CONNECTED);
784 g_clear_error (&error);
785 g_assert_cmpint (len, ==, -1);
787 g_assert_cmpint (m[0].bytes_sent, ==, 0);
788 g_assert_cmpint (m[1].bytes_sent, ==, 0);
789 g_assert_cmpint (m[2].bytes_sent, ==, 0);
791 len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
792 g_assert_cmpint (len, ==, 3);
795 g_cancellable_cancel (data->cancellable);
797 g_thread_join (data->thread);
799 g_socket_close (client, &error);
800 g_assert_no_error (error);
801 g_socket_close (data->server, &error);
802 g_assert_no_error (error);
804 g_object_unref (data->server);
805 g_object_unref (data->cancellable);
806 g_object_unref (client);
807 g_object_unref (dest_addr);
809 g_slice_free (IPTestData, data);
813 test_ipv4_sync_dgram (void)
815 test_ip_sync_dgram (G_SOCKET_FAMILY_IPV4);
819 test_ipv6_sync_dgram (void)
823 g_test_skip ("No support for IPv6");
827 test_ip_sync_dgram (G_SOCKET_FAMILY_IPV6);
831 cancellable_thread_cb (gpointer data)
833 GCancellable *cancellable = data;
835 g_usleep (0.1 * G_USEC_PER_SEC);
836 g_cancellable_cancel (cancellable);
837 g_object_unref (cancellable);
843 test_ip_sync_dgram_timeouts (GSocketFamily family)
845 GError *error = NULL;
846 GSocket *client = NULL;
847 GCancellable *cancellable = NULL;
848 GThread *cancellable_thread = NULL;
852 GSocketAddress *addr;
855 client = g_socket_new (family,
856 G_SOCKET_TYPE_DATAGRAM,
857 G_SOCKET_PROTOCOL_DEFAULT,
859 g_assert_no_error (error);
861 g_assert_cmpint (g_socket_get_family (client), ==, family);
862 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_DATAGRAM);
863 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
865 g_assert (GLIB_PRIVATE_CALL (g_win32_handle_is_socket) ((HANDLE)(gintptr) g_socket_get_fd (client)));
869 /* Winsock can't recv() on unbound udp socket */
870 iaddr = g_inet_address_new_loopback (family);
871 addr = g_inet_socket_address_new (iaddr, 0);
872 g_object_unref (iaddr);
873 g_socket_bind (client, addr, TRUE, &error);
874 g_object_unref (addr);
875 g_assert_no_error (error);
878 /* No overall timeout: test the per-operation timeouts instead. */
879 g_socket_set_timeout (client, 0);
881 cancellable = g_cancellable_new ();
883 /* Check for timeouts when no server is running. */
886 GInputMessage im = { NULL, NULL, 0, 0, 0, NULL, 0 };
887 GInputVector iv = { NULL, 0 };
891 iv.size = sizeof (buf);
896 memset (buf, 0, sizeof (buf));
898 /* Try a non-blocking read. */
899 g_socket_set_blocking (client, FALSE);
900 len = g_socket_receive_messages (client, &im, 1, 0 /* flags */,
902 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
903 g_assert_cmpint (len, ==, -1);
904 g_clear_error (&error);
906 /* Try a timeout read. Can’t really validate the time taken more than
907 * checking it’s positive. */
908 g_socket_set_timeout (client, 1);
909 g_socket_set_blocking (client, TRUE);
910 start_time = g_get_monotonic_time ();
911 len = g_socket_receive_messages (client, &im, 1, 0 /* flags */,
913 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
914 g_assert_cmpint (len, ==, -1);
915 g_assert_cmpint (g_get_monotonic_time () - start_time, >, 0);
916 g_clear_error (&error);
918 /* Try a blocking read, cancelled from another thread. */
919 g_socket_set_timeout (client, 0);
920 cancellable_thread = g_thread_new ("cancellable",
921 cancellable_thread_cb,
922 g_object_ref (cancellable));
924 start_time = g_get_monotonic_time ();
925 len = g_socket_receive_messages (client, &im, 1, 0 /* flags */,
926 cancellable, &error);
927 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
928 g_assert_cmpint (len, ==, -1);
929 g_assert_cmpint (g_get_monotonic_time () - start_time, >, 0);
930 g_clear_error (&error);
932 g_thread_join (cancellable_thread);
935 g_socket_close (client, &error);
936 g_assert_no_error (error);
938 g_object_unref (client);
939 g_object_unref (cancellable);
943 test_ipv4_sync_dgram_timeouts (void)
945 test_ip_sync_dgram_timeouts (G_SOCKET_FAMILY_IPV4);
949 test_ipv6_sync_dgram_timeouts (void)
953 g_test_skip ("No support for IPv6");
957 test_ip_sync_dgram_timeouts (G_SOCKET_FAMILY_IPV6);
961 graceful_server_thread (gpointer user_data)
963 IPTestData *data = user_data;
965 GError *error = NULL;
968 sock = g_socket_accept (data->server, NULL, &error);
969 g_assert_no_error (error);
971 len = g_socket_send (sock, testbuf, strlen (testbuf) + 1, NULL, &error);
972 g_assert_no_error (error);
973 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
979 test_close_graceful (void)
981 GSocketFamily family = G_SOCKET_FAMILY_IPV4;
983 GError *error = NULL;
984 GSocket *client, *server;
985 GSocketAddress *addr;
989 data = create_server (family, graceful_server_thread, FALSE, &error);
992 g_test_skip_printf ("Failed to create server: %s", error->message);
993 g_clear_error (&error);
997 addr = g_socket_get_local_address (data->server, &error);
998 g_assert_no_error (error);
1000 client = g_socket_new (family,
1001 G_SOCKET_TYPE_STREAM,
1002 G_SOCKET_PROTOCOL_DEFAULT,
1004 g_assert_no_error (error);
1006 g_assert_cmpint (g_socket_get_family (client), ==, family);
1007 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
1008 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
1010 g_assert (GLIB_PRIVATE_CALL (g_win32_handle_is_socket) ((HANDLE)(gintptr) g_socket_get_fd (client)));
1013 g_socket_set_blocking (client, TRUE);
1014 g_socket_set_timeout (client, 1);
1016 g_socket_connect (client, addr, NULL, &error);
1017 g_assert_no_error (error);
1018 g_assert (g_socket_is_connected (client));
1019 g_object_unref (addr);
1021 server = g_thread_join (data->thread);
1023 /* similar to g_tcp_connection_set_graceful_disconnect(), but explicit */
1024 g_socket_shutdown (server, FALSE, TRUE, &error);
1025 g_assert_no_error (error);
1027 /* we must timeout */
1028 g_socket_condition_wait (client, G_IO_HUP, NULL, &error);
1029 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
1030 g_clear_error (&error);
1032 /* check that the remaining data is received */
1033 len = g_socket_receive (client, buf, strlen (testbuf) + 1, NULL, &error);
1034 g_assert_no_error (error);
1035 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
1037 /* and only then the connection is closed */
1038 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
1039 g_assert_no_error (error);
1040 g_assert_cmpint (len, ==, 0);
1042 g_socket_close (server, &error);
1043 g_assert_no_error (error);
1045 g_socket_close (client, &error);
1046 g_assert_no_error (error);
1048 g_object_unref (server);
1049 g_object_unref (data->server);
1050 g_object_unref (client);
1052 g_slice_free (IPTestData, data);
1055 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
1057 v4mapped_server_thread (gpointer user_data)
1059 IPTestData *data = user_data;
1061 GError *error = NULL;
1062 GSocketAddress *addr;
1064 sock = g_socket_accept (data->server, NULL, &error);
1065 g_assert_no_error (error);
1067 g_assert_cmpint (g_socket_get_family (sock), ==, G_SOCKET_FAMILY_IPV6);
1069 addr = g_socket_get_local_address (sock, &error);
1070 g_assert_no_error (error);
1071 g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
1072 g_object_unref (addr);
1074 addr = g_socket_get_remote_address (sock, &error);
1075 g_assert_no_error (error);
1076 g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
1077 g_object_unref (addr);
1079 g_socket_close (sock, &error);
1080 g_assert_no_error (error);
1081 g_object_unref (sock);
1086 test_ipv6_v4mapped (void)
1089 GError *error = NULL;
1091 GSocketAddress *addr, *v4addr;
1092 GInetAddress *iaddr;
1094 if (!ipv6_supported)
1096 g_test_skip ("No support for IPv6");
1100 data = create_server (G_SOCKET_FAMILY_IPV6, v4mapped_server_thread, TRUE, &error);
1103 g_test_skip_printf ("Failed to create server: %s", error->message);
1104 g_clear_error (&error);
1108 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1109 G_SOCKET_TYPE_STREAM,
1110 G_SOCKET_PROTOCOL_DEFAULT,
1112 g_assert_no_error (error);
1114 g_socket_set_blocking (client, TRUE);
1115 g_socket_set_timeout (client, 1);
1117 addr = g_socket_get_local_address (data->server, &error);
1118 g_assert_no_error (error);
1119 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1120 v4addr = g_inet_socket_address_new (iaddr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
1121 g_object_unref (iaddr);
1122 g_object_unref (addr);
1124 g_socket_connect (client, v4addr, NULL, &error);
1125 g_assert_no_error (error);
1126 g_assert (g_socket_is_connected (client));
1128 g_thread_join (data->thread);
1130 g_socket_close (client, &error);
1131 g_assert_no_error (error);
1132 g_socket_close (data->server, &error);
1133 g_assert_no_error (error);
1135 g_object_unref (data->server);
1136 g_object_unref (client);
1137 g_object_unref (v4addr);
1139 g_slice_free (IPTestData, data);
1144 test_timed_wait (void)
1147 GError *error = NULL;
1149 GSocketAddress *addr;
1153 if (!g_test_thorough ())
1155 g_test_skip ("Not running timing heavy test");
1159 data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE, &error);
1162 g_test_skip_printf ("Failed to create server: %s", error->message);
1163 g_clear_error (&error);
1167 addr = g_socket_get_local_address (data->server, &error);
1168 g_assert_no_error (error);
1170 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1171 G_SOCKET_TYPE_STREAM,
1172 G_SOCKET_PROTOCOL_DEFAULT,
1174 g_assert_no_error (error);
1176 g_socket_set_blocking (client, TRUE);
1177 g_socket_set_timeout (client, 1);
1179 g_socket_connect (client, addr, NULL, &error);
1180 g_assert_no_error (error);
1181 g_object_unref (addr);
1183 start_time = g_get_monotonic_time ();
1184 g_socket_condition_timed_wait (client, G_IO_IN, 100000 /* 100 ms */,
1186 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
1187 g_clear_error (&error);
1188 poll_duration = g_get_monotonic_time () - start_time;
1190 g_assert_cmpint (poll_duration, >=, 98000);
1191 g_assert_cmpint (poll_duration, <, 112000);
1193 g_socket_close (client, &error);
1194 g_assert_no_error (error);
1196 g_thread_join (data->thread);
1198 g_socket_close (data->server, &error);
1199 g_assert_no_error (error);
1201 g_object_unref (data->server);
1202 g_object_unref (client);
1204 g_slice_free (IPTestData, data);
1208 duplicate_socket_fd (int fd)
1211 WSAPROTOCOL_INFO info;
1213 if (WSADuplicateSocket ((SOCKET)fd,
1214 GetCurrentProcessId (),
1217 gchar *emsg = g_win32_error_message (WSAGetLastError ());
1218 g_test_message ("Error duplicating socket: %s", emsg);
1223 return (int)WSASocket (FROM_PROTOCOL_INFO,
1233 test_fd_reuse (void)
1236 GError *error = NULL;
1239 GSocketAddress *addr;
1244 g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=741707");
1246 data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE, &error);
1249 g_test_skip_printf ("Failed to create server: %s", error->message);
1250 g_clear_error (&error);
1254 addr = g_socket_get_local_address (data->server, &error);
1255 g_assert_no_error (error);
1257 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1258 G_SOCKET_TYPE_STREAM,
1259 G_SOCKET_PROTOCOL_DEFAULT,
1261 g_assert_no_error (error);
1263 g_socket_set_blocking (client, TRUE);
1264 g_socket_set_timeout (client, 1);
1266 g_socket_connect (client, addr, NULL, &error);
1267 g_assert_no_error (error);
1268 g_assert (g_socket_is_connected (client));
1269 g_object_unref (addr);
1271 /* we have to dup otherwise the fd gets closed twice on unref */
1272 fd = duplicate_socket_fd (g_socket_get_fd (client));
1273 client2 = g_socket_new_from_fd (fd, &error);
1274 g_assert_no_error (error);
1276 g_assert_cmpint (g_socket_get_family (client2), ==, g_socket_get_family (client));
1277 g_assert_cmpint (g_socket_get_socket_type (client2), ==, g_socket_get_socket_type (client));
1278 g_assert_cmpint (g_socket_get_protocol (client2), ==, G_SOCKET_PROTOCOL_TCP);
1280 g_assert (GLIB_PRIVATE_CALL (g_win32_handle_is_socket) ((HANDLE)(gintptr) g_socket_get_fd (client)));
1283 len = g_socket_send (client2, testbuf, strlen (testbuf) + 1, NULL, &error);
1284 g_assert_no_error (error);
1285 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
1287 len = g_socket_receive (client2, buf, sizeof (buf), NULL, &error);
1288 g_assert_no_error (error);
1289 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
1291 g_assert_cmpstr (testbuf, ==, buf);
1293 g_socket_shutdown (client, FALSE, TRUE, &error);
1294 g_assert_no_error (error);
1295 /* The semantics of dup()+shutdown() are ambiguous; this call will succeed
1296 * on Linux, but return ENOTCONN on OS X.
1298 g_socket_shutdown (client2, FALSE, TRUE, NULL);
1300 g_thread_join (data->thread);
1302 g_socket_close (client, &error);
1303 g_assert_no_error (error);
1304 g_socket_close (client2, &error);
1305 g_assert_no_error (error);
1306 g_socket_close (data->server, &error);
1307 g_assert_no_error (error);
1309 g_assert_cmpint (g_socket_get_fd (client), ==, -1);
1310 g_assert_cmpint (g_socket_get_fd (client2), ==, -1);
1311 g_assert_cmpint (g_socket_get_fd (data->server), ==, -1);
1313 g_object_unref (data->server);
1314 g_object_unref (client);
1315 g_object_unref (client2);
1317 g_slice_free (IPTestData, data);
1321 test_sockaddr (void)
1323 struct sockaddr_in6 sin6, gsin6;
1324 GSocketAddress *saddr;
1325 GInetSocketAddress *isaddr;
1326 GInetAddress *iaddr;
1327 GError *error = NULL;
1329 memset (&sin6, 0, sizeof (sin6));
1330 sin6.sin6_family = AF_INET6;
1331 sin6.sin6_addr = in6addr_loopback;
1332 sin6.sin6_port = g_htons (42);
1333 sin6.sin6_scope_id = 17;
1334 sin6.sin6_flowinfo = 1729;
1336 saddr = g_socket_address_new_from_native (&sin6, sizeof (sin6));
1337 g_assert (G_IS_INET_SOCKET_ADDRESS (saddr));
1339 isaddr = G_INET_SOCKET_ADDRESS (saddr);
1340 iaddr = g_inet_socket_address_get_address (isaddr);
1341 g_assert_cmpint (g_inet_address_get_family (iaddr), ==, G_SOCKET_FAMILY_IPV6);
1342 g_assert (g_inet_address_get_is_loopback (iaddr));
1344 g_assert_cmpint (g_inet_socket_address_get_port (isaddr), ==, 42);
1345 g_assert_cmpint (g_inet_socket_address_get_scope_id (isaddr), ==, 17);
1346 g_assert_cmpint (g_inet_socket_address_get_flowinfo (isaddr), ==, 1729);
1348 g_socket_address_to_native (saddr, &gsin6, sizeof (gsin6), &error);
1349 g_assert_no_error (error);
1351 g_assert (memcmp (&sin6.sin6_addr, &gsin6.sin6_addr, sizeof (struct in6_addr)) == 0);
1352 g_assert_cmpint (sin6.sin6_port, ==, gsin6.sin6_port);
1353 g_assert_cmpint (sin6.sin6_scope_id, ==, gsin6.sin6_scope_id);
1354 g_assert_cmpint (sin6.sin6_flowinfo, ==, gsin6.sin6_flowinfo);
1356 g_object_unref (saddr);
1360 bind_win32_unixfd (int fd)
1364 struct sockaddr_un addr;
1366 memset (&addr, 0, sizeof addr);
1367 addr.sun_family = AF_UNIX;
1368 len = g_snprintf (addr.sun_path, sizeof addr.sun_path, "%s" G_DIR_SEPARATOR_S "%d.sock", g_get_tmp_dir (), fd);
1369 g_assert_cmpint (len, <=, sizeof addr.sun_path);
1370 ret = bind (fd, (struct sockaddr *)&addr, sizeof addr);
1371 g_assert_cmpint (ret, ==, 0);
1372 g_remove (addr.sun_path);
1377 test_unix_from_fd (void)
1383 fd = socket (AF_UNIX, SOCK_STREAM, 0);
1387 g_test_skip ("AF_UNIX not supported on this Windows system.");
1391 g_assert_cmpint (fd, !=, -1);
1393 bind_win32_unixfd (fd);
1396 s = g_socket_new_from_fd (fd, &error);
1397 g_assert_no_error (error);
1398 g_assert_cmpint (g_socket_get_family (s), ==, G_SOCKET_FAMILY_UNIX);
1399 g_assert_cmpint (g_socket_get_socket_type (s), ==, G_SOCKET_TYPE_STREAM);
1400 g_assert_cmpint (g_socket_get_protocol (s), ==, G_SOCKET_PROTOCOL_DEFAULT);
1402 g_assert (GLIB_PRIVATE_CALL (g_win32_handle_is_socket) ((HANDLE)(gintptr) g_socket_get_fd (s)));
1408 test_unix_connection (void)
1413 GSocketConnection *c;
1415 fd = socket (AF_UNIX, SOCK_STREAM, 0);
1419 g_test_skip ("AF_UNIX not supported on this Windows system.");
1423 g_assert_cmpint (fd, !=, -1);
1425 bind_win32_unixfd (fd);
1428 s = g_socket_new_from_fd (fd, &error);
1429 g_assert_no_error (error);
1430 c = g_socket_connection_factory_create_connection (s);
1431 g_assert (G_IS_UNIX_CONNECTION (c));
1437 static GSocketConnection *
1438 create_connection_for_fd (int fd)
1442 GSocketConnection *connection;
1444 socket = g_socket_new_from_fd (fd, &err);
1445 g_assert_no_error (err);
1446 g_assert (G_IS_SOCKET (socket));
1447 connection = g_socket_connection_factory_create_connection (socket);
1448 g_assert (G_IS_UNIX_CONNECTION (connection));
1449 g_object_unref (socket);
1453 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
1456 test_unix_connection_ancillary_data (void)
1460 gint status, fd, len;
1465 g_assert_cmpint (status, ==, 0);
1467 status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
1468 g_assert_cmpint (status, ==, 0);
1471 g_assert_cmpint (pid, >=, 0);
1473 /* Child: close its copy of the write end of the pipe, receive it
1474 * again from the parent over the socket, and write some text to it.
1476 * Parent: send the write end of the pipe (still open for the
1477 * parent) over the socket, close it, and read some text from the
1478 * read end of the pipe.
1482 GSocketConnection *connection;
1485 connection = create_connection_for_fd (sv[0]);
1487 status = close (pv[1]);
1488 g_assert_cmpint (status, ==, 0);
1491 fd = g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection), NULL,
1493 g_assert_no_error (err);
1494 g_assert_cmpint (fd, >, -1);
1495 g_object_unref (connection);
1498 len = write (fd, TEST_DATA, sizeof (TEST_DATA));
1499 while (len == -1 && errno == EINTR);
1500 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
1505 GSocketConnection *connection;
1508 connection = create_connection_for_fd (sv[1]);
1511 g_unix_connection_send_fd (G_UNIX_CONNECTION (connection), pv[1], NULL,
1513 g_assert_no_error (err);
1514 g_object_unref (connection);
1516 status = close (pv[1]);
1517 g_assert_cmpint (status, ==, 0);
1519 memset (buffer, 0xff, sizeof buffer);
1521 len = read (pv[0], buffer, sizeof buffer);
1522 while (len == -1 && errno == EINTR);
1524 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
1525 g_assert_cmpstr (buffer, ==, TEST_DATA);
1527 waitpid (pid, &status, 0);
1528 g_assert (WIFEXITED (status));
1529 g_assert_cmpint (WEXITSTATUS (status), ==, 0);
1532 /* TODO: add test for g_unix_connection_send_credentials() and
1533 * g_unix_connection_receive_credentials().
1540 test_handle_not_socket (void)
1544 HANDLE hReadPipe, hWritePipe, h;
1547 g_assert_true (CreatePipe (&hReadPipe, &hWritePipe, NULL, 2048));
1548 g_assert_false (GLIB_PRIVATE_CALL (g_win32_handle_is_socket) (hReadPipe));
1549 g_assert_false (GLIB_PRIVATE_CALL (g_win32_handle_is_socket) (hWritePipe));
1550 CloseHandle (hReadPipe);
1551 CloseHandle (hWritePipe);
1553 h = (HANDLE) _get_osfhandle (1);
1554 g_assert_false (GLIB_PRIVATE_CALL (g_win32_handle_is_socket) (h));
1556 fd = g_file_open_tmp (NULL, &name, &err);
1557 g_assert_no_error (err);
1558 h = (HANDLE) _get_osfhandle (fd);
1559 g_assert_false (GLIB_PRIVATE_CALL (g_win32_handle_is_socket) (h));
1561 g_assert_no_error (err);
1568 postmortem_source_cb (GSocket *socket,
1569 GIOCondition condition,
1572 gboolean *been_here = user_data;
1574 g_assert_cmpint (condition, ==, G_IO_NVAL);
1581 test_source_postmortem (void)
1583 GMainContext *context;
1586 GError *error = NULL;
1587 gboolean callback_visited = FALSE;
1589 socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error);
1593 g_test_skip_printf ("AF_UNIX not supported on this Windows system: %s", error->message);
1594 g_clear_error (&error);
1598 g_assert_no_error (error);
1600 context = g_main_context_new ();
1602 source = g_socket_create_source (socket, G_IO_IN, NULL);
1603 g_source_set_callback (source, (GSourceFunc) postmortem_source_cb,
1604 &callback_visited, NULL);
1605 g_source_attach (source, context);
1606 g_source_unref (source);
1608 g_socket_close (socket, &error);
1609 g_assert_no_error (error);
1610 g_object_unref (socket);
1612 /* Test that, after a socket is closed, its source callback should be called
1614 g_main_context_iteration (context, FALSE);
1615 g_assert (callback_visited);
1616 g_assert (!g_main_context_pending (context));
1618 g_main_context_unref (context);
1622 test_reuse_tcp (void)
1624 GSocket *sock1, *sock2;
1625 GError *error = NULL;
1626 GInetAddress *iaddr;
1627 GSocketAddress *addr;
1629 sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1630 G_SOCKET_TYPE_STREAM,
1631 G_SOCKET_PROTOCOL_DEFAULT,
1633 g_assert_no_error (error);
1635 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1636 addr = g_inet_socket_address_new (iaddr, 0);
1637 g_object_unref (iaddr);
1638 g_socket_bind (sock1, addr, TRUE, &error);
1639 g_object_unref (addr);
1640 g_assert_no_error (error);
1642 g_socket_listen (sock1, &error);
1643 g_assert_no_error (error);
1645 sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1646 G_SOCKET_TYPE_STREAM,
1647 G_SOCKET_PROTOCOL_DEFAULT,
1649 g_assert_no_error (error);
1651 addr = g_socket_get_local_address (sock1, &error);
1652 g_assert_no_error (error);
1653 g_socket_bind (sock2, addr, TRUE, &error);
1654 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_ADDRESS_IN_USE);
1655 g_clear_error (&error);
1656 g_object_unref (addr);
1658 g_object_unref (sock1);
1659 g_object_unref (sock2);
1663 test_reuse_udp (void)
1665 GSocket *sock1, *sock2;
1666 GError *error = NULL;
1667 GInetAddress *iaddr;
1668 GSocketAddress *addr;
1670 sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1671 G_SOCKET_TYPE_DATAGRAM,
1672 G_SOCKET_PROTOCOL_DEFAULT,
1674 g_assert_no_error (error);
1676 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1677 addr = g_inet_socket_address_new (iaddr, 0);
1678 g_object_unref (iaddr);
1679 g_socket_bind (sock1, addr, TRUE, &error);
1680 g_object_unref (addr);
1681 g_assert_no_error (error);
1683 sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1684 G_SOCKET_TYPE_DATAGRAM,
1685 G_SOCKET_PROTOCOL_DEFAULT,
1687 g_assert_no_error (error);
1689 addr = g_socket_get_local_address (sock1, &error);
1690 g_assert_no_error (error);
1691 g_socket_bind (sock2, addr, TRUE, &error);
1692 g_object_unref (addr);
1693 g_assert_no_error (error);
1695 g_object_unref (sock1);
1696 g_object_unref (sock2);
1700 test_get_available (gconstpointer user_data)
1702 GSocketType socket_type = GPOINTER_TO_UINT (user_data);
1704 GSocket *listener, *server, *client;
1706 GSocketAddress *saddr, *boundaddr;
1707 gchar data[] = "0123456789abcdef";
1711 listener = g_socket_new (G_SOCKET_FAMILY_IPV4,
1713 G_SOCKET_PROTOCOL_DEFAULT,
1715 g_assert_no_error (err);
1716 g_assert (G_IS_SOCKET (listener));
1718 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1720 G_SOCKET_PROTOCOL_DEFAULT,
1722 g_assert_no_error (err);
1723 g_assert (G_IS_SOCKET (client));
1725 if (socket_type == G_SOCKET_TYPE_STREAM)
1727 g_socket_set_option (client, IPPROTO_TCP, TCP_NODELAY, TRUE, &err);
1728 g_assert_no_error (err);
1731 addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
1732 saddr = g_inet_socket_address_new (addr, 0);
1734 g_socket_bind (listener, saddr, TRUE, &err);
1735 g_assert_no_error (err);
1736 g_object_unref (saddr);
1737 g_object_unref (addr);
1739 boundaddr = g_socket_get_local_address (listener, &err);
1740 g_assert_no_error (err);
1742 addr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1743 saddr = g_inet_socket_address_new (addr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (boundaddr)));
1744 g_object_unref (addr);
1745 g_object_unref (boundaddr);
1747 if (socket_type == G_SOCKET_TYPE_STREAM)
1749 g_socket_listen (listener, &err);
1750 g_assert_no_error (err);
1751 g_socket_connect (client, saddr, NULL, &err);
1752 g_assert_no_error (err);
1754 server = g_socket_accept (listener, NULL, &err);
1755 g_assert_no_error (err);
1756 g_socket_set_blocking (server, FALSE);
1757 g_object_unref (listener);
1762 g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
1763 g_assert_no_error (err);
1765 while (!g_socket_condition_wait (server, G_IO_IN, NULL, NULL))
1767 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1769 g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
1770 g_assert_no_error (err);
1772 /* We need to wait until the data has actually been copied into the
1773 * server socket's buffers, but g_socket_condition_wait() won't help
1774 * here since the socket is definitely already readable. So there's
1775 * a race condition in checking its available bytes. In the TCP
1776 * case, we poll for a bit until the new data shows up. In the UDP
1777 * case, there's not much we can do, but at least the failure mode
1778 * is passes-when-it-shouldn't, not fails-when-it-shouldn't.
1780 if (socket_type == G_SOCKET_TYPE_STREAM)
1784 for (tries = 0; tries < 100; tries++)
1786 gssize res = g_socket_get_available_bytes (server);
1787 if ((res == -1) || ((gsize) res > sizeof (data)))
1792 g_assert_cmpint (g_socket_get_available_bytes (server), ==, 2 * sizeof (data));
1797 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1800 g_assert_cmpint (sizeof (buf), >=, 2 * sizeof (data));
1801 nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
1802 g_assert_no_error (err);
1804 if (socket_type == G_SOCKET_TYPE_STREAM)
1806 g_assert_cmpint (nread, ==, 2 * sizeof (data));
1807 g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1811 g_assert_cmpint (nread, ==, sizeof (data));
1812 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1815 nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
1816 if (socket_type == G_SOCKET_TYPE_STREAM)
1818 g_assert_cmpint (nread, ==, -1);
1819 g_assert_error (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
1820 g_clear_error (&err);
1824 g_assert_cmpint (nread, ==, sizeof (data));
1825 g_assert_no_error (err);
1828 g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1830 g_socket_close (server, &err);
1831 g_assert_no_error (err);
1833 g_object_unref (saddr);
1834 g_object_unref (server);
1835 g_object_unref (client);
1841 const guint8 *write_data;
1843 } TestReadWriteData;
1846 test_read_write_write_thread (gpointer user_data)
1848 TestReadWriteData *data = user_data;
1849 gsize bytes_written;
1850 GError *error = NULL;
1853 res = g_output_stream_write_all (data->os, data->write_data, 1024, &bytes_written, NULL, &error);
1854 g_assert_true (res);
1855 g_assert_no_error (error);
1856 g_assert_cmpint (bytes_written, ==, 1024);
1862 test_read_write_read_thread (gpointer user_data)
1864 TestReadWriteData *data = user_data;
1866 GError *error = NULL;
1869 res = g_input_stream_read_all (data->is, data->read_data, 1024, &bytes_read, NULL, &error);
1870 g_assert_true (res);
1871 g_assert_no_error (error);
1872 g_assert_cmpint (bytes_read, ==, 1024);
1878 test_read_write_writev_thread (gpointer user_data)
1880 TestReadWriteData *data = user_data;
1881 gsize bytes_written;
1882 GError *error = NULL;
1884 GOutputVector vectors[3];
1886 vectors[0].buffer = data->write_data;
1887 vectors[0].size = 256;
1888 vectors[1].buffer = data->write_data + 256;
1889 vectors[1].size = 256;
1890 vectors[2].buffer = data->write_data + 512;
1891 vectors[2].size = 512;
1893 res = g_output_stream_writev_all (data->os, vectors, G_N_ELEMENTS (vectors), &bytes_written, NULL, &error);
1894 g_assert_true (res);
1895 g_assert_no_error (error);
1896 g_assert_cmpint (bytes_written, ==, 1024);
1901 /* test if normal read/write/writev via the GSocket*Streams works on TCP sockets */
1903 test_read_write (gconstpointer user_data)
1905 gboolean writev = GPOINTER_TO_INT (user_data);
1907 GSocket *listener, *server, *client;
1909 GSocketAddress *saddr, *boundaddr;
1910 TestReadWriteData data;
1911 guint8 data_write[1024], data_read[1024];
1912 GSocketConnection *server_stream, *client_stream;
1913 GThread *write_thread, *read_thread;
1916 listener = g_socket_new (G_SOCKET_FAMILY_IPV4,
1917 G_SOCKET_TYPE_STREAM,
1918 G_SOCKET_PROTOCOL_DEFAULT,
1920 g_assert_no_error (err);
1921 g_assert (G_IS_SOCKET (listener));
1923 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1924 G_SOCKET_TYPE_STREAM,
1925 G_SOCKET_PROTOCOL_DEFAULT,
1927 g_assert_no_error (err);
1928 g_assert (G_IS_SOCKET (client));
1930 addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
1931 saddr = g_inet_socket_address_new (addr, 0);
1933 g_socket_bind (listener, saddr, TRUE, &err);
1934 g_assert_no_error (err);
1935 g_object_unref (saddr);
1936 g_object_unref (addr);
1938 boundaddr = g_socket_get_local_address (listener, &err);
1939 g_assert_no_error (err);
1941 g_socket_listen (listener, &err);
1942 g_assert_no_error (err);
1944 addr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1945 saddr = g_inet_socket_address_new (addr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (boundaddr)));
1946 g_object_unref (addr);
1947 g_object_unref (boundaddr);
1949 g_socket_connect (client, saddr, NULL, &err);
1950 g_assert_no_error (err);
1952 server = g_socket_accept (listener, NULL, &err);
1953 g_assert_no_error (err);
1954 g_socket_set_blocking (server, FALSE);
1955 g_object_unref (listener);
1957 server_stream = g_socket_connection_factory_create_connection (server);
1958 g_assert_nonnull (server_stream);
1959 client_stream = g_socket_connection_factory_create_connection (client);
1960 g_assert_nonnull (client_stream);
1962 for (i = 0; i < sizeof (data_write); i++)
1965 data.is = g_io_stream_get_input_stream (G_IO_STREAM (server_stream));
1966 data.os = g_io_stream_get_output_stream (G_IO_STREAM (client_stream));
1967 data.read_data = data_read;
1968 data.write_data = data_write;
1971 write_thread = g_thread_new ("writer", test_read_write_writev_thread, &data);
1973 write_thread = g_thread_new ("writer", test_read_write_write_thread, &data);
1974 read_thread = g_thread_new ("reader", test_read_write_read_thread, &data);
1976 g_thread_join (write_thread);
1977 g_thread_join (read_thread);
1979 g_assert_cmpmem (data_write, sizeof data_write, data_read, sizeof data_read);
1981 g_socket_close (server, &err);
1982 g_assert_no_error (err);
1984 g_object_unref (server_stream);
1985 g_object_unref (client_stream);
1987 g_object_unref (saddr);
1988 g_object_unref (server);
1989 g_object_unref (client);
1994 test_nosigpipe (void)
1997 GError *error = NULL;
2000 sock = g_socket_new (AF_INET,
2001 G_SOCKET_TYPE_STREAM,
2002 G_SOCKET_PROTOCOL_DEFAULT,
2004 g_assert_no_error (error);
2006 g_socket_get_option (sock, SOL_SOCKET, SO_NOSIGPIPE, &value, &error);
2007 g_assert_no_error (error);
2008 g_assert_true (value);
2010 g_object_unref (sock);
2014 #if G_CREDENTIALS_SUPPORTED
2015 static gpointer client_setup_thread (gpointer user_data);
2018 test_credentials_tcp_client (void)
2020 const GSocketFamily family = G_SOCKET_FAMILY_IPV4;
2022 GError *error = NULL;
2024 GSocketAddress *addr;
2025 GCredentials *creds;
2027 data = create_server (family, echo_server_thread, FALSE, &error);
2030 g_test_skip_printf ("Failed to create server: %s", error->message);
2031 g_clear_error (&error);
2035 addr = g_socket_get_local_address (data->server, &error);
2036 g_assert_no_error (error);
2038 client = g_socket_new (family,
2039 G_SOCKET_TYPE_STREAM,
2040 G_SOCKET_PROTOCOL_DEFAULT,
2042 g_assert_no_error (error);
2044 g_socket_set_blocking (client, TRUE);
2045 g_socket_set_timeout (client, 1);
2047 g_socket_connect (client, addr, NULL, &error);
2048 g_assert_no_error (error);
2049 g_object_unref (addr);
2051 creds = g_socket_get_credentials (client, &error);
2054 gchar *str = g_credentials_to_string (creds);
2055 g_test_message ("Supported on this OS: %s", str);
2057 g_clear_object (&creds);
2061 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
2062 g_test_message ("Unsupported on this OS: %s", error->message);
2063 g_clear_error (&error);
2066 g_socket_close (client, &error);
2067 g_assert_no_error (error);
2069 g_thread_join (data->thread);
2071 g_socket_close (data->server, &error);
2072 g_assert_no_error (error);
2074 g_object_unref (data->server);
2075 g_object_unref (client);
2077 g_slice_free (IPTestData, data);
2081 test_credentials_tcp_server (void)
2083 const GSocketFamily family = G_SOCKET_FAMILY_IPV4;
2086 GError *error = NULL;
2087 GSocketAddress *addr = NULL;
2088 GInetAddress *iaddr = NULL;
2089 GSocket *sock = NULL;
2090 GCredentials *creds;
2092 data = g_slice_new0 (IPTestData);
2093 data->family = family;
2094 data->server = server = g_socket_new (family,
2095 G_SOCKET_TYPE_STREAM,
2096 G_SOCKET_PROTOCOL_DEFAULT,
2101 g_socket_set_blocking (server, TRUE);
2103 iaddr = g_inet_address_new_loopback (family);
2104 addr = g_inet_socket_address_new (iaddr, 0);
2106 if (!g_socket_bind (server, addr, TRUE, &error))
2109 if (!g_socket_listen (server, &error))
2112 data->thread = g_thread_new ("client", client_setup_thread, data);
2114 sock = g_socket_accept (server, NULL, &error);
2115 g_assert_no_error (error);
2117 creds = g_socket_get_credentials (sock, &error);
2120 gchar *str = g_credentials_to_string (creds);
2121 g_test_message ("Supported on this OS: %s", str);
2123 g_clear_object (&creds);
2127 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
2128 g_test_message ("Unsupported on this OS: %s", error->message);
2129 g_clear_error (&error);
2135 g_test_skip_printf ("Failed to create server: %s", error->message);
2140 g_clear_error (&error);
2142 g_clear_object (&sock);
2143 g_clear_object (&addr);
2144 g_clear_object (&iaddr);
2146 g_clear_pointer (&data->thread, g_thread_join);
2147 g_clear_object (&data->server);
2148 g_clear_object (&data->client);
2150 g_slice_free (IPTestData, data);
2155 client_setup_thread (gpointer user_data)
2157 IPTestData *data = user_data;
2158 GSocketAddress *addr;
2160 GError *error = NULL;
2162 addr = g_socket_get_local_address (data->server, &error);
2163 g_assert_no_error (error);
2165 data->client = client = g_socket_new (data->family,
2166 G_SOCKET_TYPE_STREAM,
2167 G_SOCKET_PROTOCOL_DEFAULT,
2169 g_assert_no_error (error);
2171 g_socket_set_blocking (client, TRUE);
2172 g_socket_set_timeout (client, 1);
2174 g_socket_connect (client, addr, NULL, &error);
2175 g_assert_no_error (error);
2177 g_object_unref (addr);
2184 * _g_win32_socketpair:
2186 * Create a pair of connected sockets, similar to POSIX/BSD socketpair().
2188 * Windows does not (yet) provide a socketpair() function. However, since the
2189 * introduction of AF_UNIX sockets, it is possible to implement a fairly close
2193 _g_win32_socketpair (gint domain,
2198 struct sockaddr_un addr = { 0, };
2200 SOCKET listener = INVALID_SOCKET;
2201 SOCKET client = INVALID_SOCKET;
2202 SOCKET server = INVALID_SOCKET;
2207 g_return_val_if_fail (sv != NULL, -1);
2209 addr.sun_family = AF_UNIX;
2210 socklen = sizeof (addr);
2212 tmpfd = g_file_open_tmp (NULL, &path, NULL);
2215 WSASetLastError (WSAEACCES);
2219 g_close (tmpfd, NULL);
2221 if (strlen (path) >= sizeof (addr.sun_path))
2223 WSASetLastError (WSAEACCES);
2227 strncpy (addr.sun_path, path, sizeof (addr.sun_path) - 1);
2229 listener = socket (domain, type, protocol);
2230 if (listener == INVALID_SOCKET)
2233 if (DeleteFile (path) == 0)
2235 if (GetLastError () != ERROR_FILE_NOT_FOUND)
2239 if (bind (listener, (struct sockaddr *) &addr, socklen) == SOCKET_ERROR)
2242 if (listen (listener, 1) == SOCKET_ERROR)
2245 client = socket (domain, type, protocol);
2246 if (client == INVALID_SOCKET)
2250 if (ioctlsocket (client, FIONBIO, &arg) == SOCKET_ERROR)
2253 if (connect (client, (struct sockaddr *) &addr, socklen) == SOCKET_ERROR &&
2254 WSAGetLastError () != WSAEWOULDBLOCK)
2257 server = accept (listener, NULL, NULL);
2258 if (server == INVALID_SOCKET)
2262 if (ioctlsocket (client, FIONBIO, &arg) == SOCKET_ERROR)
2265 if (WSAIoctl (server, SIO_AF_UNIX_GETPEERPID,
2267 &arg, sizeof (arg), &br,
2268 NULL, NULL) == SOCKET_ERROR || arg != GetCurrentProcessId ())
2270 WSASetLastError (WSAEACCES);
2275 server = INVALID_SOCKET;
2277 client = INVALID_SOCKET;
2281 if (listener != INVALID_SOCKET)
2282 closesocket (listener);
2283 if (client != INVALID_SOCKET)
2284 closesocket (client);
2285 if (server != INVALID_SOCKET)
2286 closesocket (server);
2292 #endif /* G_OS_WIN32 */
2295 test_credentials_unix_socketpair (void)
2300 GError *error = NULL;
2301 GCredentials *creds;
2304 status = _g_win32_socketpair (PF_UNIX, SOCK_STREAM, 0, fds);
2307 g_test_skip ("AF_UNIX not supported on this Windows system.");
2311 status = socketpair (PF_UNIX, SOCK_STREAM, 0, fds);
2313 g_assert_cmpint (status, ==, 0);
2315 sock[0] = g_socket_new_from_fd (fds[0], &error);
2316 g_assert_no_error (error);
2317 sock[1] = g_socket_new_from_fd (fds[1], &error);
2318 g_assert_no_error (error);
2320 creds = g_socket_get_credentials (sock[0], &error);
2323 gchar *str = g_credentials_to_string (creds);
2324 g_test_message ("Supported on this OS: %s", str);
2326 g_clear_object (&creds);
2330 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
2331 g_test_message ("Unsupported on this OS: %s", error->message);
2332 g_clear_error (&error);
2335 g_object_unref (sock[0]);
2336 g_object_unref (sock[1]);
2345 GError *error = NULL;
2347 g_test_init (&argc, &argv, NULL);
2349 sock = g_socket_new (G_SOCKET_FAMILY_IPV6,
2350 G_SOCKET_TYPE_STREAM,
2351 G_SOCKET_PROTOCOL_DEFAULT,
2355 ipv6_supported = TRUE;
2356 g_object_unref (sock);
2360 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
2361 g_clear_error (&error);
2364 g_test_add_func ("/socket/ipv4_sync", test_ipv4_sync);
2365 g_test_add_func ("/socket/ipv4_async", test_ipv4_async);
2366 g_test_add_func ("/socket/ipv6_sync", test_ipv6_sync);
2367 g_test_add_func ("/socket/ipv6_async", test_ipv6_async);
2368 g_test_add_func ("/socket/ipv4_sync/datagram", test_ipv4_sync_dgram);
2369 g_test_add_func ("/socket/ipv4_sync/datagram/timeouts", test_ipv4_sync_dgram_timeouts);
2370 g_test_add_func ("/socket/ipv6_sync/datagram", test_ipv6_sync_dgram);
2371 g_test_add_func ("/socket/ipv6_sync/datagram/timeouts", test_ipv6_sync_dgram_timeouts);
2372 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
2373 g_test_add_func ("/socket/ipv6_v4mapped", test_ipv6_v4mapped);
2375 g_test_add_func ("/socket/close_graceful", test_close_graceful);
2376 g_test_add_func ("/socket/timed_wait", test_timed_wait);
2377 g_test_add_func ("/socket/fd_reuse", test_fd_reuse);
2378 g_test_add_func ("/socket/address", test_sockaddr);
2379 g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
2380 g_test_add_func ("/socket/unix-connection", test_unix_connection);
2382 g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data);
2385 g_test_add_func ("/socket/win32-handle-not-socket", test_handle_not_socket);
2387 g_test_add_func ("/socket/source-postmortem", test_source_postmortem);
2388 g_test_add_func ("/socket/reuse/tcp", test_reuse_tcp);
2389 g_test_add_func ("/socket/reuse/udp", test_reuse_udp);
2390 g_test_add_data_func ("/socket/get_available/datagram", GUINT_TO_POINTER (G_SOCKET_TYPE_DATAGRAM),
2391 test_get_available);
2392 g_test_add_data_func ("/socket/get_available/stream", GUINT_TO_POINTER (G_SOCKET_TYPE_STREAM),
2393 test_get_available);
2394 g_test_add_data_func ("/socket/read_write", GUINT_TO_POINTER (FALSE),
2396 g_test_add_data_func ("/socket/read_writev", GUINT_TO_POINTER (TRUE),
2399 g_test_add_func ("/socket/nosigpipe", test_nosigpipe);
2401 #if G_CREDENTIALS_SUPPORTED
2402 g_test_add_func ("/socket/credentials/tcp_client", test_credentials_tcp_client);
2403 g_test_add_func ("/socket/credentials/tcp_server", test_credentials_tcp_server);
2404 g_test_add_func ("/socket/credentials/unix_socketpair", test_credentials_unix_socketpair);
2407 return g_test_run();