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))
144 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
145 "IPv6-only server cannot speak IPv4");
152 iaddr = g_inet_address_new_any (family);
154 iaddr = g_inet_address_new_loopback (family);
155 addr = g_inet_socket_address_new (iaddr, 0);
156 g_object_unref (iaddr);
158 g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), ==, 0);
159 if (!g_socket_bind (server, addr, TRUE, error))
161 g_object_unref (addr);
164 g_object_unref (addr);
166 addr = g_socket_get_local_address (server, error);
169 g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), !=, 0);
170 g_object_unref (addr);
172 if (socket_type == G_SOCKET_TYPE_STREAM)
174 if (!g_socket_listen (server, error))
179 data->cancellable = g_cancellable_new ();
182 data->thread = g_thread_new ("server", server_thread, data);
187 g_clear_object (&data->server);
188 g_slice_free (IPTestData, data);
194 create_server (GSocketFamily family,
195 GThreadFunc server_thread,
199 return create_server_full (family, G_SOCKET_TYPE_STREAM, server_thread, v4mapped, error);
202 static const gchar *testbuf = "0123456789abcdef";
205 test_ip_async_read_ready (GSocket *client,
209 IPTestData *data = user_data;
210 GError *error = NULL;
214 g_assert_cmpint (cond, ==, G_IO_IN);
216 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
217 g_assert_no_error (error);
218 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
220 g_assert_cmpstr (testbuf, ==, buf);
222 g_main_loop_quit (data->loop);
228 test_ip_async_write_ready (GSocket *client,
232 IPTestData *data = user_data;
233 GError *error = NULL;
237 g_assert_cmpint (cond, ==, G_IO_OUT);
239 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
240 g_assert_no_error (error);
241 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
243 source = g_socket_create_source (client, G_IO_IN, NULL);
244 g_source_set_callback (source, (GSourceFunc)test_ip_async_read_ready,
246 g_source_attach (source, NULL);
247 g_source_unref (source);
253 test_ip_async_timed_out (GSocket *client,
257 IPTestData *data = user_data;
258 GError *error = NULL;
263 if (data->family == G_SOCKET_FAMILY_IPV4)
265 g_assert_cmpint (cond, ==, G_IO_IN);
266 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
267 g_assert_cmpint (len, ==, -1);
268 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
269 g_clear_error (&error);
272 source = g_socket_create_source (client, G_IO_OUT, NULL);
273 g_source_set_callback (source, (GSourceFunc)test_ip_async_write_ready,
275 g_source_attach (source, NULL);
276 g_source_unref (source);
282 test_ip_async_connected (GSocket *client,
286 IPTestData *data = user_data;
287 GError *error = NULL;
292 g_socket_check_connect_result (client, &error);
293 g_assert_no_error (error);
294 /* We do this after the check_connect_result, since that will give a
295 * more useful assertion in case of error.
297 g_assert_cmpint (cond, ==, G_IO_OUT);
299 g_assert (g_socket_is_connected (client));
301 /* This adds 1 second to "make check", so let's just only do it once. */
302 if (data->family == G_SOCKET_FAMILY_IPV4)
304 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
305 g_assert_cmpint (len, ==, -1);
306 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
307 g_clear_error (&error);
309 source = g_socket_create_source (client, G_IO_IN, NULL);
310 g_source_set_callback (source, (GSourceFunc)test_ip_async_timed_out,
312 g_source_attach (source, NULL);
313 g_source_unref (source);
316 test_ip_async_timed_out (client, 0, data);
322 idle_test_ip_async_connected (gpointer user_data)
324 IPTestData *data = user_data;
326 return test_ip_async_connected (data->client, G_IO_OUT, data);
330 test_ip_async (GSocketFamily family)
333 GError *error = NULL;
335 GSocketAddress *addr;
340 data = create_server (family, echo_server_thread, FALSE, &error);
343 gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
344 g_test_skip (message);
346 g_clear_error (&error);
349 g_assert_nonnull (data);
351 addr = g_socket_get_local_address (data->server, &error);
352 g_assert_no_error (error);
354 client = g_socket_new (family,
355 G_SOCKET_TYPE_STREAM,
356 G_SOCKET_PROTOCOL_DEFAULT,
358 g_assert_no_error (error);
359 data->client = client;
361 g_assert_cmpint (g_socket_get_family (client), ==, family);
362 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
363 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
365 g_socket_set_blocking (client, FALSE);
366 g_socket_set_timeout (client, 1);
368 if (g_socket_connect (client, addr, NULL, &error))
370 g_assert_no_error (error);
371 g_idle_add (idle_test_ip_async_connected, data);
375 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
376 g_clear_error (&error);
377 source = g_socket_create_source (client, G_IO_OUT, NULL);
378 g_source_set_callback (source, (GSourceFunc)test_ip_async_connected,
380 g_source_attach (source, NULL);
381 g_source_unref (source);
383 g_object_unref (addr);
385 data->loop = g_main_loop_new (NULL, TRUE);
386 g_main_loop_run (data->loop);
387 g_main_loop_unref (data->loop);
389 g_socket_shutdown (client, FALSE, TRUE, &error);
390 g_assert_no_error (error);
392 g_thread_join (data->thread);
394 if (family == G_SOCKET_FAMILY_IPV4)
396 /* Test that reading on a remote-closed socket gets back 0 bytes. */
397 len = g_socket_receive_with_blocking (client, buf, sizeof (buf),
399 g_assert_no_error (error);
400 g_assert_cmpint (len, ==, 0);
404 /* Test that writing to a remote-closed socket gets back CONNECTION_CLOSED. */
405 len = g_socket_send_with_blocking (client, testbuf, strlen (testbuf) + 1,
407 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED);
408 g_assert_cmpint (len, ==, -1);
409 g_clear_error (&error);
412 g_socket_close (client, &error);
413 g_assert_no_error (error);
414 g_socket_close (data->server, &error);
415 g_assert_no_error (error);
417 g_object_unref (data->server);
418 g_object_unref (client);
420 g_slice_free (IPTestData, data);
424 test_ipv4_async (void)
426 test_ip_async (G_SOCKET_FAMILY_IPV4);
430 test_ipv6_async (void)
434 g_test_skip ("No support for IPv6");
438 test_ip_async (G_SOCKET_FAMILY_IPV6);
441 static const gchar testbuf2[] = "0123456789abcdefghijklmnopqrstuvwxyz";
444 test_ip_sync (GSocketFamily family)
447 GError *error = NULL;
449 GSocketAddress *addr;
453 data = create_server (family, echo_server_thread, FALSE, &error);
456 gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
457 g_test_skip (message);
459 g_clear_error (&error);
463 addr = g_socket_get_local_address (data->server, &error);
464 g_assert_no_error (error);
466 client = g_socket_new (family,
467 G_SOCKET_TYPE_STREAM,
468 G_SOCKET_PROTOCOL_DEFAULT,
470 g_assert_no_error (error);
472 g_assert_cmpint (g_socket_get_family (client), ==, family);
473 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
474 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
476 g_socket_set_blocking (client, TRUE);
477 g_socket_set_timeout (client, 1);
479 g_socket_connect (client, addr, NULL, &error);
480 g_assert_no_error (error);
481 g_assert (g_socket_is_connected (client));
482 g_object_unref (addr);
484 /* This adds 1 second to "make check", so let's just only do it once. */
485 if (family == G_SOCKET_FAMILY_IPV4)
487 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
488 g_assert_cmpint (len, ==, -1);
489 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
490 g_clear_error (&error);
493 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
494 g_assert_no_error (error);
495 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
497 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
498 g_assert_no_error (error);
499 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
501 g_assert_cmpstr (testbuf, ==, buf);
504 GOutputVector v[7] = { { NULL, }, };
506 v[0].buffer = testbuf2 + 0;
508 v[1].buffer = testbuf2 + 3;
510 v[2].buffer = testbuf2 + 3 + 5;
512 v[3].buffer = testbuf2 + 3 + 5;
514 v[4].buffer = testbuf2 + 3 + 5 + 6;
516 v[5].buffer = testbuf2 + 3 + 5 + 6 + 2;
518 v[6].buffer = testbuf2 + 3 + 5 + 6 + 2 + 1;
519 v[6].size = strlen (testbuf2) - (3 + 5 + 6 + 2 + 1);
521 len = g_socket_send_message (client, NULL, v, G_N_ELEMENTS (v), NULL, 0, 0, NULL, &error);
522 g_assert_no_error (error);
523 g_assert_cmpint (len, ==, strlen (testbuf2));
525 memset (buf, 0, sizeof (buf));
526 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
527 g_assert_no_error (error);
528 g_assert_cmpint (len, ==, strlen (testbuf2));
529 g_assert_cmpstr (testbuf2, ==, buf);
532 g_socket_shutdown (client, FALSE, TRUE, &error);
533 g_assert_no_error (error);
535 g_thread_join (data->thread);
537 if (family == G_SOCKET_FAMILY_IPV4)
539 /* Test that reading on a remote-closed socket gets back 0 bytes. */
540 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
541 g_assert_no_error (error);
542 g_assert_cmpint (len, ==, 0);
546 /* Test that writing to a remote-closed socket gets back CONNECTION_CLOSED. */
547 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
548 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED);
549 g_assert_cmpint (len, ==, -1);
550 g_clear_error (&error);
553 g_socket_close (client, &error);
554 g_assert_no_error (error);
555 g_socket_close (data->server, &error);
556 g_assert_no_error (error);
558 g_object_unref (data->server);
559 g_object_unref (client);
561 g_slice_free (IPTestData, data);
565 test_ipv4_sync (void)
567 test_ip_sync (G_SOCKET_FAMILY_IPV4);
571 test_ipv6_sync (void)
575 g_test_skip ("No support for IPv6");
579 test_ip_sync (G_SOCKET_FAMILY_IPV6);
583 test_ip_sync_dgram (GSocketFamily family)
586 GError *error = NULL;
588 GSocketAddress *dest_addr;
592 data = create_server_full (family, G_SOCKET_TYPE_DATAGRAM,
593 echo_server_dgram_thread, FALSE, &error);
596 gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
597 g_test_skip (message);
599 g_clear_error (&error);
603 dest_addr = g_socket_get_local_address (data->server, &error);
605 client = g_socket_new (family,
606 G_SOCKET_TYPE_DATAGRAM,
607 G_SOCKET_PROTOCOL_DEFAULT,
609 g_assert_no_error (error);
611 g_assert_cmpint (g_socket_get_family (client), ==, family);
612 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_DATAGRAM);
613 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
615 g_socket_set_blocking (client, TRUE);
616 g_socket_set_timeout (client, 1);
618 len = g_socket_send_to (client, dest_addr, testbuf, strlen (testbuf) + 1, NULL, &error);
619 g_assert_no_error (error);
620 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
622 len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
623 g_assert_no_error (error);
624 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
626 g_assert_cmpstr (testbuf, ==, buf);
629 GOutputMessage m[3] = { { NULL, }, };
630 GInputMessage im[3] = { { NULL, }, };
631 GOutputVector v[7] = { { NULL, }, };
632 GInputVector iv[7] = { { NULL, }, };
634 v[0].buffer = testbuf2 + 0;
636 v[1].buffer = testbuf2 + 3;
638 v[2].buffer = testbuf2 + 3 + 5;
640 v[3].buffer = testbuf2 + 3 + 5;
642 v[4].buffer = testbuf2 + 3 + 5 + 6;
644 v[5].buffer = testbuf2 + 3 + 5 + 6 + 2;
646 v[6].buffer = testbuf2 + 3 + 5 + 6 + 2 + 1;
647 v[6].size = strlen (testbuf2) - (3 + 5 + 6 + 2 + 1);
649 iv[0].buffer = buf + 0;
651 iv[1].buffer = buf + 3;
653 iv[2].buffer = buf + 3 + 5;
655 iv[3].buffer = buf + 3 + 5;
657 iv[4].buffer = buf + 3 + 5 + 6;
659 iv[5].buffer = buf + 3 + 5 + 6 + 2;
661 iv[6].buffer = buf + 3 + 5 + 6 + 2 + 1;
662 iv[6].size = sizeof (buf) - (3 + 5 + 6 + 2 + 1);
664 len = g_socket_send_message (client, dest_addr, v, G_N_ELEMENTS (v), NULL, 0, 0, NULL, &error);
665 g_assert_no_error (error);
666 g_assert_cmpint (len, ==, strlen (testbuf2));
668 memset (buf, 0, sizeof (buf));
669 len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
670 g_assert_no_error (error);
671 g_assert_cmpint (len, ==, strlen (testbuf2));
672 g_assert_cmpstr (testbuf2, ==, buf);
674 m[0].vectors = &v[0];
675 m[0].num_vectors = 1;
676 m[0].address = dest_addr;
677 m[1].vectors = &v[0];
678 m[1].num_vectors = 6;
679 m[1].address = dest_addr;
680 m[2].vectors = &v[6];
681 m[2].num_vectors = 1;
682 m[2].address = dest_addr;
684 len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
685 g_assert_no_error (error);
686 g_assert_cmpint (len, ==, G_N_ELEMENTS (m));
687 g_assert_cmpint (m[0].bytes_sent, ==, 3);
688 g_assert_cmpint (m[1].bytes_sent, ==, 17);
689 g_assert_cmpint (m[2].bytes_sent, ==, v[6].size);
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 g_assert_cmpint (len, ==, 3);
696 memset (buf, 0, sizeof (buf));
697 len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
698 g_assert_no_error (error);
699 /* v[0].size + v[1].size + v[2].size + v[3].size + v[4].size + v[5].size */
700 g_assert_cmpint (len, ==, 17);
701 g_assert (memcmp (testbuf2, buf, 17) == 0);
703 memset (buf, 0, sizeof (buf));
704 len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
705 g_assert_no_error (error);
706 g_assert_cmpint (len, ==, v[6].size);
707 g_assert_cmpstr (buf, ==, v[6].buffer);
709 /* reset since we're re-using the message structs */
714 /* now try receiving multiple messages */
715 len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
716 g_assert_no_error (error);
717 g_assert_cmpint (len, ==, G_N_ELEMENTS (m));
718 g_assert_cmpint (m[0].bytes_sent, ==, 3);
719 g_assert_cmpint (m[1].bytes_sent, ==, 17);
720 g_assert_cmpint (m[2].bytes_sent, ==, v[6].size);
722 im[0].vectors = &iv[0];
723 im[0].num_vectors = 1;
724 im[1].vectors = &iv[0];
725 im[1].num_vectors = 6;
726 im[2].vectors = &iv[6];
727 im[2].num_vectors = 1;
729 memset (buf, 0, sizeof (buf));
730 len = g_socket_receive_messages (client, im, G_N_ELEMENTS (im), 0,
732 g_assert_no_error (error);
733 g_assert_cmpint (len, ==, G_N_ELEMENTS (im));
735 g_assert_cmpuint (im[0].bytes_received, ==, 3);
736 /* v[0].size + v[1].size + v[2].size + v[3].size + v[4].size + v[5].size */
737 g_assert_cmpuint (im[1].bytes_received, ==, 17);
738 g_assert_cmpuint (im[2].bytes_received, ==, v[6].size);
740 /* reset since we're re-using the message structs */
745 /* now try to generate an early return by omitting the destination address on [1] */
747 len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
748 g_assert_no_error (error);
749 g_assert_cmpint (len, ==, 1);
751 g_assert_cmpint (m[0].bytes_sent, ==, 3);
752 g_assert_cmpint (m[1].bytes_sent, ==, 0);
753 g_assert_cmpint (m[2].bytes_sent, ==, 0);
755 /* reset since we're re-using the message structs */
760 /* now try to generate an error by omitting all destination addresses */
764 len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
765 /* This error code may vary between platforms and over time; it is not guaranteed API: */
767 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
769 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_CONNECTED);
771 g_clear_error (&error);
772 g_assert_cmpint (len, ==, -1);
774 g_assert_cmpint (m[0].bytes_sent, ==, 0);
775 g_assert_cmpint (m[1].bytes_sent, ==, 0);
776 g_assert_cmpint (m[2].bytes_sent, ==, 0);
778 len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
779 g_assert_cmpint (len, ==, 3);
782 g_cancellable_cancel (data->cancellable);
784 g_thread_join (data->thread);
786 g_socket_close (client, &error);
787 g_assert_no_error (error);
788 g_socket_close (data->server, &error);
789 g_assert_no_error (error);
791 g_object_unref (data->server);
792 g_object_unref (data->cancellable);
793 g_object_unref (client);
794 g_object_unref (dest_addr);
796 g_slice_free (IPTestData, data);
800 test_ipv4_sync_dgram (void)
802 test_ip_sync_dgram (G_SOCKET_FAMILY_IPV4);
806 test_ipv6_sync_dgram (void)
810 g_test_skip ("No support for IPv6");
814 test_ip_sync_dgram (G_SOCKET_FAMILY_IPV6);
818 cancellable_thread_cb (gpointer data)
820 GCancellable *cancellable = data;
822 g_usleep (0.1 * G_USEC_PER_SEC);
823 g_cancellable_cancel (cancellable);
824 g_object_unref (cancellable);
830 test_ip_sync_dgram_timeouts (GSocketFamily family)
832 GError *error = NULL;
833 GSocket *client = NULL;
834 GCancellable *cancellable = NULL;
835 GThread *cancellable_thread = NULL;
839 GSocketAddress *addr;
842 client = g_socket_new (family,
843 G_SOCKET_TYPE_DATAGRAM,
844 G_SOCKET_PROTOCOL_DEFAULT,
846 g_assert_no_error (error);
848 g_assert_cmpint (g_socket_get_family (client), ==, family);
849 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_DATAGRAM);
850 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
853 /* Winsock can't recv() on unbound udp socket */
854 iaddr = g_inet_address_new_loopback (family);
855 addr = g_inet_socket_address_new (iaddr, 0);
856 g_object_unref (iaddr);
857 g_socket_bind (client, addr, TRUE, &error);
858 g_object_unref (addr);
859 g_assert_no_error (error);
862 /* No overall timeout: test the per-operation timeouts instead. */
863 g_socket_set_timeout (client, 0);
865 cancellable = g_cancellable_new ();
867 /* Check for timeouts when no server is running. */
870 GInputMessage im = { NULL, };
871 GInputVector iv = { NULL, };
875 iv.size = sizeof (buf);
880 memset (buf, 0, sizeof (buf));
882 /* Try a non-blocking read. */
883 g_socket_set_blocking (client, FALSE);
884 len = g_socket_receive_messages (client, &im, 1, 0 /* flags */,
886 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
887 g_assert_cmpint (len, ==, -1);
888 g_clear_error (&error);
890 /* Try a timeout read. Can’t really validate the time taken more than
891 * checking it’s positive. */
892 g_socket_set_timeout (client, 1);
893 g_socket_set_blocking (client, TRUE);
894 start_time = g_get_monotonic_time ();
895 len = g_socket_receive_messages (client, &im, 1, 0 /* flags */,
897 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
898 g_assert_cmpint (len, ==, -1);
899 g_assert_cmpint (g_get_monotonic_time () - start_time, >, 0);
900 g_clear_error (&error);
902 /* Try a blocking read, cancelled from another thread. */
903 g_socket_set_timeout (client, 0);
904 cancellable_thread = g_thread_new ("cancellable",
905 cancellable_thread_cb,
906 g_object_ref (cancellable));
908 start_time = g_get_monotonic_time ();
909 len = g_socket_receive_messages (client, &im, 1, 0 /* flags */,
910 cancellable, &error);
911 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
912 g_assert_cmpint (len, ==, -1);
913 g_assert_cmpint (g_get_monotonic_time () - start_time, >, 0);
914 g_clear_error (&error);
916 g_thread_join (cancellable_thread);
919 g_socket_close (client, &error);
920 g_assert_no_error (error);
922 g_object_unref (client);
923 g_object_unref (cancellable);
927 test_ipv4_sync_dgram_timeouts (void)
929 test_ip_sync_dgram_timeouts (G_SOCKET_FAMILY_IPV4);
933 test_ipv6_sync_dgram_timeouts (void)
937 g_test_skip ("No support for IPv6");
941 test_ip_sync_dgram_timeouts (G_SOCKET_FAMILY_IPV6);
945 graceful_server_thread (gpointer user_data)
947 IPTestData *data = user_data;
949 GError *error = NULL;
952 sock = g_socket_accept (data->server, NULL, &error);
953 g_assert_no_error (error);
955 len = g_socket_send (sock, testbuf, strlen (testbuf) + 1, NULL, &error);
956 g_assert_no_error (error);
957 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
963 test_close_graceful (void)
965 GSocketFamily family = G_SOCKET_FAMILY_IPV4;
967 GError *error = NULL;
968 GSocket *client, *server;
969 GSocketAddress *addr;
973 data = create_server (family, graceful_server_thread, FALSE, &error);
976 gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
977 g_test_skip (message);
979 g_clear_error (&error);
983 addr = g_socket_get_local_address (data->server, &error);
984 g_assert_no_error (error);
986 client = g_socket_new (family,
987 G_SOCKET_TYPE_STREAM,
988 G_SOCKET_PROTOCOL_DEFAULT,
990 g_assert_no_error (error);
992 g_assert_cmpint (g_socket_get_family (client), ==, family);
993 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
994 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
996 g_socket_set_blocking (client, TRUE);
997 g_socket_set_timeout (client, 1);
999 g_socket_connect (client, addr, NULL, &error);
1000 g_assert_no_error (error);
1001 g_assert (g_socket_is_connected (client));
1002 g_object_unref (addr);
1004 server = g_thread_join (data->thread);
1006 /* similar to g_tcp_connection_set_graceful_disconnect(), but explicit */
1007 g_socket_shutdown (server, FALSE, TRUE, &error);
1008 g_assert_no_error (error);
1010 /* we must timeout */
1011 g_socket_condition_wait (client, G_IO_HUP, NULL, &error);
1012 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
1013 g_clear_error (&error);
1015 /* check that the remaining data is received */
1016 len = g_socket_receive (client, buf, strlen (testbuf) + 1, NULL, &error);
1017 g_assert_no_error (error);
1018 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
1020 /* and only then the connection is closed */
1021 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
1022 g_assert_no_error (error);
1023 g_assert_cmpint (len, ==, 0);
1025 g_socket_close (server, &error);
1026 g_assert_no_error (error);
1028 g_socket_close (client, &error);
1029 g_assert_no_error (error);
1031 g_object_unref (server);
1032 g_object_unref (data->server);
1033 g_object_unref (client);
1035 g_slice_free (IPTestData, data);
1038 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
1040 v4mapped_server_thread (gpointer user_data)
1042 IPTestData *data = user_data;
1044 GError *error = NULL;
1045 GSocketAddress *addr;
1047 sock = g_socket_accept (data->server, NULL, &error);
1048 g_assert_no_error (error);
1050 g_assert_cmpint (g_socket_get_family (sock), ==, G_SOCKET_FAMILY_IPV6);
1052 addr = g_socket_get_local_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 addr = g_socket_get_remote_address (sock, &error);
1058 g_assert_no_error (error);
1059 g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
1060 g_object_unref (addr);
1062 g_socket_close (sock, &error);
1063 g_assert_no_error (error);
1064 g_object_unref (sock);
1069 test_ipv6_v4mapped (void)
1072 GError *error = NULL;
1074 GSocketAddress *addr, *v4addr;
1075 GInetAddress *iaddr;
1077 if (!ipv6_supported)
1079 g_test_skip ("No support for IPv6");
1083 data = create_server (G_SOCKET_FAMILY_IPV6, v4mapped_server_thread, TRUE, &error);
1086 gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
1087 g_test_skip (message);
1089 g_clear_error (&error);
1093 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1094 G_SOCKET_TYPE_STREAM,
1095 G_SOCKET_PROTOCOL_DEFAULT,
1097 g_assert_no_error (error);
1099 g_socket_set_blocking (client, TRUE);
1100 g_socket_set_timeout (client, 1);
1102 addr = g_socket_get_local_address (data->server, &error);
1103 g_assert_no_error (error);
1104 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1105 v4addr = g_inet_socket_address_new (iaddr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
1106 g_object_unref (iaddr);
1107 g_object_unref (addr);
1109 g_socket_connect (client, v4addr, NULL, &error);
1110 g_assert_no_error (error);
1111 g_assert (g_socket_is_connected (client));
1113 g_thread_join (data->thread);
1115 g_socket_close (client, &error);
1116 g_assert_no_error (error);
1117 g_socket_close (data->server, &error);
1118 g_assert_no_error (error);
1120 g_object_unref (data->server);
1121 g_object_unref (client);
1122 g_object_unref (v4addr);
1124 g_slice_free (IPTestData, data);
1129 test_timed_wait (void)
1132 GError *error = NULL;
1134 GSocketAddress *addr;
1138 if (!g_test_thorough ())
1140 g_test_skip ("Not running timing heavy test");
1144 data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE, &error);
1147 gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
1148 g_test_skip (message);
1150 g_clear_error (&error);
1154 addr = g_socket_get_local_address (data->server, &error);
1155 g_assert_no_error (error);
1157 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1158 G_SOCKET_TYPE_STREAM,
1159 G_SOCKET_PROTOCOL_DEFAULT,
1161 g_assert_no_error (error);
1163 g_socket_set_blocking (client, TRUE);
1164 g_socket_set_timeout (client, 1);
1166 g_socket_connect (client, addr, NULL, &error);
1167 g_assert_no_error (error);
1168 g_object_unref (addr);
1170 start_time = g_get_monotonic_time ();
1171 g_socket_condition_timed_wait (client, G_IO_IN, 100000 /* 100 ms */,
1173 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
1174 g_clear_error (&error);
1175 poll_duration = g_get_monotonic_time () - start_time;
1177 g_assert_cmpint (poll_duration, >=, 98000);
1178 g_assert_cmpint (poll_duration, <, 112000);
1180 g_socket_close (client, &error);
1181 g_assert_no_error (error);
1183 g_thread_join (data->thread);
1185 g_socket_close (data->server, &error);
1186 g_assert_no_error (error);
1188 g_object_unref (data->server);
1189 g_object_unref (client);
1191 g_slice_free (IPTestData, data);
1195 duplicate_fd (int fd)
1200 if (!DuplicateHandle (GetCurrentProcess (),
1202 GetCurrentProcess (),
1206 DUPLICATE_SAME_ACCESS))
1218 test_fd_reuse (void)
1221 GError *error = NULL;
1224 GSocketAddress *addr;
1229 g_test_bug ("741707");
1231 data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE, &error);
1234 gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
1235 g_test_skip (message);
1237 g_clear_error (&error);
1241 addr = g_socket_get_local_address (data->server, &error);
1242 g_assert_no_error (error);
1244 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1245 G_SOCKET_TYPE_STREAM,
1246 G_SOCKET_PROTOCOL_DEFAULT,
1248 g_assert_no_error (error);
1250 g_socket_set_blocking (client, TRUE);
1251 g_socket_set_timeout (client, 1);
1253 g_socket_connect (client, addr, NULL, &error);
1254 g_assert_no_error (error);
1255 g_assert (g_socket_is_connected (client));
1256 g_object_unref (addr);
1258 /* we have to dup otherwise the fd gets closed twice on unref */
1259 fd = duplicate_fd (g_socket_get_fd (client));
1260 client2 = g_socket_new_from_fd (fd, &error);
1261 g_assert_no_error (error);
1263 g_assert_cmpint (g_socket_get_family (client2), ==, g_socket_get_family (client));
1264 g_assert_cmpint (g_socket_get_socket_type (client2), ==, g_socket_get_socket_type (client));
1265 g_assert_cmpint (g_socket_get_protocol (client2), ==, G_SOCKET_PROTOCOL_TCP);
1267 len = g_socket_send (client2, testbuf, strlen (testbuf) + 1, NULL, &error);
1268 g_assert_no_error (error);
1269 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
1271 len = g_socket_receive (client2, buf, sizeof (buf), NULL, &error);
1272 g_assert_no_error (error);
1273 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
1275 g_assert_cmpstr (testbuf, ==, buf);
1277 g_socket_shutdown (client, FALSE, TRUE, &error);
1278 g_assert_no_error (error);
1279 /* The semantics of dup()+shutdown() are ambiguous; this call will succeed
1280 * on Linux, but return ENOTCONN on OS X.
1282 g_socket_shutdown (client2, FALSE, TRUE, NULL);
1284 g_thread_join (data->thread);
1286 g_socket_close (client, &error);
1287 g_assert_no_error (error);
1288 g_socket_close (client2, &error);
1289 g_assert_no_error (error);
1290 g_socket_close (data->server, &error);
1291 g_assert_no_error (error);
1293 g_assert_cmpint (g_socket_get_fd (client), ==, -1);
1294 g_assert_cmpint (g_socket_get_fd (client2), ==, -1);
1295 g_assert_cmpint (g_socket_get_fd (data->server), ==, -1);
1297 g_object_unref (data->server);
1298 g_object_unref (client);
1299 g_object_unref (client2);
1301 g_slice_free (IPTestData, data);
1305 test_sockaddr (void)
1307 struct sockaddr_in6 sin6, gsin6;
1308 GSocketAddress *saddr;
1309 GInetSocketAddress *isaddr;
1310 GInetAddress *iaddr;
1311 GError *error = NULL;
1313 memset (&sin6, 0, sizeof (sin6));
1314 sin6.sin6_family = AF_INET6;
1315 sin6.sin6_addr = in6addr_loopback;
1316 sin6.sin6_port = g_htons (42);
1317 sin6.sin6_scope_id = 17;
1318 sin6.sin6_flowinfo = 1729;
1320 saddr = g_socket_address_new_from_native (&sin6, sizeof (sin6));
1321 g_assert (G_IS_INET_SOCKET_ADDRESS (saddr));
1323 isaddr = G_INET_SOCKET_ADDRESS (saddr);
1324 iaddr = g_inet_socket_address_get_address (isaddr);
1325 g_assert_cmpint (g_inet_address_get_family (iaddr), ==, G_SOCKET_FAMILY_IPV6);
1326 g_assert (g_inet_address_get_is_loopback (iaddr));
1328 g_assert_cmpint (g_inet_socket_address_get_port (isaddr), ==, 42);
1329 g_assert_cmpint (g_inet_socket_address_get_scope_id (isaddr), ==, 17);
1330 g_assert_cmpint (g_inet_socket_address_get_flowinfo (isaddr), ==, 1729);
1332 g_socket_address_to_native (saddr, &gsin6, sizeof (gsin6), &error);
1333 g_assert_no_error (error);
1335 g_assert (memcmp (&sin6.sin6_addr, &gsin6.sin6_addr, sizeof (struct in6_addr)) == 0);
1336 g_assert_cmpint (sin6.sin6_port, ==, gsin6.sin6_port);
1337 g_assert_cmpint (sin6.sin6_scope_id, ==, gsin6.sin6_scope_id);
1338 g_assert_cmpint (sin6.sin6_flowinfo, ==, gsin6.sin6_flowinfo);
1340 g_object_unref (saddr);
1345 test_unix_from_fd (void)
1351 fd = socket (AF_UNIX, SOCK_STREAM, 0);
1352 g_assert_cmpint (fd, !=, -1);
1355 s = g_socket_new_from_fd (fd, &error);
1356 g_assert_no_error (error);
1357 g_assert_cmpint (g_socket_get_family (s), ==, G_SOCKET_FAMILY_UNIX);
1358 g_assert_cmpint (g_socket_get_socket_type (s), ==, G_SOCKET_TYPE_STREAM);
1359 g_assert_cmpint (g_socket_get_protocol (s), ==, G_SOCKET_PROTOCOL_DEFAULT);
1364 test_unix_connection (void)
1369 GSocketConnection *c;
1371 fd = socket (AF_UNIX, SOCK_STREAM, 0);
1372 g_assert_cmpint (fd, !=, -1);
1375 s = g_socket_new_from_fd (fd, &error);
1376 g_assert_no_error (error);
1377 c = g_socket_connection_factory_create_connection (s);
1378 g_assert (G_IS_UNIX_CONNECTION (c));
1383 static GSocketConnection *
1384 create_connection_for_fd (int fd)
1388 GSocketConnection *connection;
1390 socket = g_socket_new_from_fd (fd, &err);
1391 g_assert_no_error (err);
1392 g_assert (G_IS_SOCKET (socket));
1393 connection = g_socket_connection_factory_create_connection (socket);
1394 g_assert (G_IS_UNIX_CONNECTION (connection));
1395 g_object_unref (socket);
1399 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
1402 test_unix_connection_ancillary_data (void)
1406 gint status, fd, len;
1411 g_assert_cmpint (status, ==, 0);
1413 status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
1414 g_assert_cmpint (status, ==, 0);
1417 g_assert_cmpint (pid, >=, 0);
1419 /* Child: close its copy of the write end of the pipe, receive it
1420 * again from the parent over the socket, and write some text to it.
1422 * Parent: send the write end of the pipe (still open for the
1423 * parent) over the socket, close it, and read some text from the
1424 * read end of the pipe.
1428 GSocketConnection *connection;
1431 connection = create_connection_for_fd (sv[0]);
1433 status = close (pv[1]);
1434 g_assert_cmpint (status, ==, 0);
1437 fd = g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection), NULL,
1439 g_assert_no_error (err);
1440 g_assert_cmpint (fd, >, -1);
1441 g_object_unref (connection);
1444 len = write (fd, TEST_DATA, sizeof (TEST_DATA));
1445 while (len == -1 && errno == EINTR);
1446 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
1451 GSocketConnection *connection;
1454 connection = create_connection_for_fd (sv[1]);
1457 g_unix_connection_send_fd (G_UNIX_CONNECTION (connection), pv[1], NULL,
1459 g_assert_no_error (err);
1460 g_object_unref (connection);
1462 status = close (pv[1]);
1463 g_assert_cmpint (status, ==, 0);
1465 memset (buffer, 0xff, sizeof buffer);
1467 len = read (pv[0], buffer, sizeof buffer);
1468 while (len == -1 && errno == EINTR);
1470 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
1471 g_assert_cmpstr (buffer, ==, TEST_DATA);
1473 waitpid (pid, &status, 0);
1474 g_assert (WIFEXITED (status));
1475 g_assert_cmpint (WEXITSTATUS (status), ==, 0);
1478 /* TODO: add test for g_unix_connection_send_credentials() and
1479 * g_unix_connection_receive_credentials().
1484 postmortem_source_cb (GSocket *socket,
1485 GIOCondition condition,
1488 gboolean *been_here = user_data;
1490 g_assert_cmpint (condition, ==, G_IO_NVAL);
1497 test_source_postmortem (void)
1499 GMainContext *context;
1502 GError *error = NULL;
1503 gboolean callback_visited = FALSE;
1505 socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error);
1506 g_assert_no_error (error);
1508 context = g_main_context_new ();
1510 source = g_socket_create_source (socket, G_IO_IN, NULL);
1511 g_source_set_callback (source, (GSourceFunc) postmortem_source_cb,
1512 &callback_visited, NULL);
1513 g_source_attach (source, context);
1514 g_source_unref (source);
1516 g_socket_close (socket, &error);
1517 g_assert_no_error (error);
1518 g_object_unref (socket);
1520 /* Test that, after a socket is closed, its source callback should be called
1522 g_main_context_iteration (context, FALSE);
1523 g_assert (callback_visited);
1524 g_assert (!g_main_context_pending (context));
1526 g_main_context_unref (context);
1529 #endif /* G_OS_UNIX */
1532 test_reuse_tcp (void)
1534 GSocket *sock1, *sock2;
1535 GError *error = NULL;
1536 GInetAddress *iaddr;
1537 GSocketAddress *addr;
1539 sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1540 G_SOCKET_TYPE_STREAM,
1541 G_SOCKET_PROTOCOL_DEFAULT,
1543 g_assert_no_error (error);
1545 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1546 addr = g_inet_socket_address_new (iaddr, 0);
1547 g_object_unref (iaddr);
1548 g_socket_bind (sock1, addr, TRUE, &error);
1549 g_object_unref (addr);
1550 g_assert_no_error (error);
1552 g_socket_listen (sock1, &error);
1553 g_assert_no_error (error);
1555 sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1556 G_SOCKET_TYPE_STREAM,
1557 G_SOCKET_PROTOCOL_DEFAULT,
1559 g_assert_no_error (error);
1561 addr = g_socket_get_local_address (sock1, &error);
1562 g_assert_no_error (error);
1563 g_socket_bind (sock2, addr, TRUE, &error);
1564 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_ADDRESS_IN_USE);
1565 g_clear_error (&error);
1566 g_object_unref (addr);
1568 g_object_unref (sock1);
1569 g_object_unref (sock2);
1573 test_reuse_udp (void)
1575 GSocket *sock1, *sock2;
1576 GError *error = NULL;
1577 GInetAddress *iaddr;
1578 GSocketAddress *addr;
1580 sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1581 G_SOCKET_TYPE_DATAGRAM,
1582 G_SOCKET_PROTOCOL_DEFAULT,
1584 g_assert_no_error (error);
1586 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1587 addr = g_inet_socket_address_new (iaddr, 0);
1588 g_object_unref (iaddr);
1589 g_socket_bind (sock1, addr, TRUE, &error);
1590 g_object_unref (addr);
1591 g_assert_no_error (error);
1593 sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1594 G_SOCKET_TYPE_DATAGRAM,
1595 G_SOCKET_PROTOCOL_DEFAULT,
1597 g_assert_no_error (error);
1599 addr = g_socket_get_local_address (sock1, &error);
1600 g_assert_no_error (error);
1601 g_socket_bind (sock2, addr, TRUE, &error);
1602 g_object_unref (addr);
1603 g_assert_no_error (error);
1605 g_object_unref (sock1);
1606 g_object_unref (sock2);
1610 test_get_available (gconstpointer user_data)
1612 GSocketType socket_type = GPOINTER_TO_UINT (user_data);
1614 GSocket *listener, *server, *client;
1616 GSocketAddress *saddr, *boundaddr;
1617 gchar data[] = "0123456789abcdef";
1621 listener = g_socket_new (G_SOCKET_FAMILY_IPV4,
1623 G_SOCKET_PROTOCOL_DEFAULT,
1625 g_assert_no_error (err);
1626 g_assert (G_IS_SOCKET (listener));
1628 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1630 G_SOCKET_PROTOCOL_DEFAULT,
1632 g_assert_no_error (err);
1633 g_assert (G_IS_SOCKET (client));
1635 if (socket_type == G_SOCKET_TYPE_STREAM)
1637 g_socket_set_option (client, IPPROTO_TCP, TCP_NODELAY, TRUE, &err);
1638 g_assert_no_error (err);
1641 addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
1642 saddr = g_inet_socket_address_new (addr, 0);
1644 g_socket_bind (listener, saddr, TRUE, &err);
1645 g_assert_no_error (err);
1646 g_object_unref (saddr);
1647 g_object_unref (addr);
1649 boundaddr = g_socket_get_local_address (listener, &err);
1650 g_assert_no_error (err);
1652 addr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1653 saddr = g_inet_socket_address_new (addr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (boundaddr)));
1654 g_object_unref (addr);
1655 g_object_unref (boundaddr);
1657 if (socket_type == G_SOCKET_TYPE_STREAM)
1659 g_socket_listen (listener, &err);
1660 g_assert_no_error (err);
1661 g_socket_connect (client, saddr, NULL, &err);
1662 g_assert_no_error (err);
1664 server = g_socket_accept (listener, NULL, &err);
1665 g_assert_no_error (err);
1666 g_socket_set_blocking (server, FALSE);
1667 g_object_unref (listener);
1672 g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
1673 g_assert_no_error (err);
1675 while (!g_socket_condition_wait (server, G_IO_IN, NULL, NULL))
1677 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1679 g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
1680 g_assert_no_error (err);
1682 /* We need to wait until the data has actually been copied into the
1683 * server socket's buffers, but g_socket_condition_wait() won't help
1684 * here since the socket is definitely already readable. So there's
1685 * a race condition in checking its available bytes. In the TCP
1686 * case, we poll for a bit until the new data shows up. In the UDP
1687 * case, there's not much we can do, but at least the failure mode
1688 * is passes-when-it-shouldn't, not fails-when-it-shouldn't.
1690 if (socket_type == G_SOCKET_TYPE_STREAM)
1694 for (tries = 0; tries < 100; tries++)
1696 if (g_socket_get_available_bytes (server) > sizeof (data))
1701 g_assert_cmpint (g_socket_get_available_bytes (server), ==, 2 * sizeof (data));
1706 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1709 g_assert_cmpint (sizeof (buf), >=, 2 * sizeof (data));
1710 nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
1711 g_assert_no_error (err);
1713 if (socket_type == G_SOCKET_TYPE_STREAM)
1715 g_assert_cmpint (nread, ==, 2 * sizeof (data));
1716 g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1720 g_assert_cmpint (nread, ==, sizeof (data));
1721 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1724 nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
1725 if (socket_type == G_SOCKET_TYPE_STREAM)
1727 g_assert_cmpint (nread, ==, -1);
1728 g_assert_error (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
1729 g_clear_error (&err);
1733 g_assert_cmpint (nread, ==, sizeof (data));
1734 g_assert_no_error (err);
1737 g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1739 g_socket_close (server, &err);
1740 g_assert_no_error (err);
1742 g_object_unref (saddr);
1743 g_object_unref (server);
1744 g_object_unref (client);
1750 const guint8 *write_data;
1752 } TestReadWriteData;
1755 test_read_write_write_thread (gpointer user_data)
1757 TestReadWriteData *data = user_data;
1758 gsize bytes_written;
1759 GError *error = NULL;
1762 res = g_output_stream_write_all (data->os, data->write_data, 1024, &bytes_written, NULL, &error);
1763 g_assert_true (res);
1764 g_assert_no_error (error);
1765 g_assert_cmpint (bytes_written, ==, 1024);
1771 test_read_write_read_thread (gpointer user_data)
1773 TestReadWriteData *data = user_data;
1775 GError *error = NULL;
1778 res = g_input_stream_read_all (data->is, data->read_data, 1024, &bytes_read, NULL, &error);
1779 g_assert_true (res);
1780 g_assert_no_error (error);
1781 g_assert_cmpint (bytes_read, ==, 1024);
1787 test_read_write_writev_thread (gpointer user_data)
1789 TestReadWriteData *data = user_data;
1790 gsize bytes_written;
1791 GError *error = NULL;
1793 GOutputVector vectors[3];
1795 vectors[0].buffer = data->write_data;
1796 vectors[0].size = 256;
1797 vectors[1].buffer = data->write_data + 256;
1798 vectors[1].size = 256;
1799 vectors[2].buffer = data->write_data + 512;
1800 vectors[2].size = 512;
1802 res = g_output_stream_writev_all (data->os, vectors, G_N_ELEMENTS (vectors), &bytes_written, NULL, &error);
1803 g_assert_true (res);
1804 g_assert_no_error (error);
1805 g_assert_cmpint (bytes_written, ==, 1024);
1810 /* test if normal read/write/writev via the GSocket*Streams works on TCP sockets */
1812 test_read_write (gconstpointer user_data)
1814 gboolean writev = GPOINTER_TO_INT (user_data);
1816 GSocket *listener, *server, *client;
1818 GSocketAddress *saddr, *boundaddr;
1819 TestReadWriteData data;
1820 guint8 data_write[1024], data_read[1024];
1821 GSocketConnection *server_stream, *client_stream;
1822 GThread *write_thread, *read_thread;
1825 listener = g_socket_new (G_SOCKET_FAMILY_IPV4,
1826 G_SOCKET_TYPE_STREAM,
1827 G_SOCKET_PROTOCOL_DEFAULT,
1829 g_assert_no_error (err);
1830 g_assert (G_IS_SOCKET (listener));
1832 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1833 G_SOCKET_TYPE_STREAM,
1834 G_SOCKET_PROTOCOL_DEFAULT,
1836 g_assert_no_error (err);
1837 g_assert (G_IS_SOCKET (client));
1839 addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
1840 saddr = g_inet_socket_address_new (addr, 0);
1842 g_socket_bind (listener, saddr, TRUE, &err);
1843 g_assert_no_error (err);
1844 g_object_unref (saddr);
1845 g_object_unref (addr);
1847 boundaddr = g_socket_get_local_address (listener, &err);
1848 g_assert_no_error (err);
1850 g_socket_listen (listener, &err);
1851 g_assert_no_error (err);
1853 addr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1854 saddr = g_inet_socket_address_new (addr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (boundaddr)));
1855 g_object_unref (addr);
1856 g_object_unref (boundaddr);
1858 g_socket_connect (client, saddr, NULL, &err);
1859 g_assert_no_error (err);
1861 server = g_socket_accept (listener, NULL, &err);
1862 g_assert_no_error (err);
1863 g_socket_set_blocking (server, FALSE);
1864 g_object_unref (listener);
1866 server_stream = g_socket_connection_factory_create_connection (server);
1867 g_assert_nonnull (server_stream);
1868 client_stream = g_socket_connection_factory_create_connection (client);
1869 g_assert_nonnull (client_stream);
1871 for (i = 0; i < sizeof (data_write); i++)
1874 data.is = g_io_stream_get_input_stream (G_IO_STREAM (server_stream));
1875 data.os = g_io_stream_get_output_stream (G_IO_STREAM (client_stream));
1876 data.read_data = data_read;
1877 data.write_data = data_write;
1880 write_thread = g_thread_new ("writer", test_read_write_writev_thread, &data);
1882 write_thread = g_thread_new ("writer", test_read_write_write_thread, &data);
1883 read_thread = g_thread_new ("reader", test_read_write_read_thread, &data);
1885 g_thread_join (write_thread);
1886 g_thread_join (read_thread);
1888 g_assert_cmpmem (data_write, sizeof data_write, data_read, sizeof data_read);
1890 g_socket_close (server, &err);
1891 g_assert_no_error (err);
1893 g_object_unref (server_stream);
1894 g_object_unref (client_stream);
1896 g_object_unref (saddr);
1897 g_object_unref (server);
1898 g_object_unref (client);
1906 GError *error = NULL;
1908 g_test_init (&argc, &argv, NULL);
1909 g_test_bug_base ("https://bugzilla.gnome.org/");
1911 sock = g_socket_new (G_SOCKET_FAMILY_IPV6,
1912 G_SOCKET_TYPE_STREAM,
1913 G_SOCKET_PROTOCOL_DEFAULT,
1917 ipv6_supported = TRUE;
1918 g_object_unref (sock);
1922 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
1923 g_clear_error (&error);
1926 g_test_add_func ("/socket/ipv4_sync", test_ipv4_sync);
1927 g_test_add_func ("/socket/ipv4_async", test_ipv4_async);
1928 g_test_add_func ("/socket/ipv6_sync", test_ipv6_sync);
1929 g_test_add_func ("/socket/ipv6_async", test_ipv6_async);
1930 g_test_add_func ("/socket/ipv4_sync/datagram", test_ipv4_sync_dgram);
1931 g_test_add_func ("/socket/ipv4_sync/datagram/timeouts", test_ipv4_sync_dgram_timeouts);
1932 g_test_add_func ("/socket/ipv6_sync/datagram", test_ipv6_sync_dgram);
1933 g_test_add_func ("/socket/ipv6_sync/datagram/timeouts", test_ipv6_sync_dgram_timeouts);
1934 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
1935 g_test_add_func ("/socket/ipv6_v4mapped", test_ipv6_v4mapped);
1937 g_test_add_func ("/socket/close_graceful", test_close_graceful);
1938 g_test_add_func ("/socket/timed_wait", test_timed_wait);
1939 g_test_add_func ("/socket/fd_reuse", test_fd_reuse);
1940 g_test_add_func ("/socket/address", test_sockaddr);
1942 g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
1943 g_test_add_func ("/socket/unix-connection", test_unix_connection);
1944 g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data);
1945 g_test_add_func ("/socket/source-postmortem", test_source_postmortem);
1947 g_test_add_func ("/socket/reuse/tcp", test_reuse_tcp);
1948 g_test_add_func ("/socket/reuse/udp", test_reuse_udp);
1949 g_test_add_data_func ("/socket/get_available/datagram", GUINT_TO_POINTER (G_SOCKET_TYPE_DATAGRAM),
1950 test_get_available);
1951 g_test_add_data_func ("/socket/get_available/stream", GUINT_TO_POINTER (G_SOCKET_TYPE_STREAM),
1952 test_get_available);
1953 g_test_add_data_func ("/socket/read_write", GUINT_TO_POINTER (FALSE),
1955 g_test_add_data_func ("/socket/read_writev", GUINT_TO_POINTER (TRUE),
1958 return g_test_run();