1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2011 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 #include <gio/gnetworking.h>
27 #include <gio/gunixconnection.h>
30 #include "gnetworkingprivate.h"
32 static gboolean ipv6_supported;
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,
118 GError *error = NULL;
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,
129 g_assert_no_error (error);
131 g_assert_cmpint (g_socket_get_family (server), ==, family);
132 g_assert_cmpint (g_socket_get_socket_type (server), ==, socket_type);
133 g_assert_cmpint (g_socket_get_protocol (server), ==, G_SOCKET_PROTOCOL_DEFAULT);
135 g_socket_set_blocking (server, TRUE);
137 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
140 g_socket_set_option (data->server, IPPROTO_IPV6, IPV6_V6ONLY, FALSE, NULL);
141 if (! g_socket_speaks_ipv4 (data->server))
143 g_object_unref (data->server);
144 g_slice_free (IPTestData, data);
151 iaddr = g_inet_address_new_any (family);
153 iaddr = g_inet_address_new_loopback (family);
154 addr = g_inet_socket_address_new (iaddr, 0);
155 g_object_unref (iaddr);
157 g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), ==, 0);
158 g_socket_bind (server, addr, TRUE, &error);
159 g_assert_no_error (error);
160 g_object_unref (addr);
162 addr = g_socket_get_local_address (server, &error);
163 g_assert_no_error (error);
164 g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), !=, 0);
165 g_object_unref (addr);
167 if (socket_type == G_SOCKET_TYPE_STREAM)
169 g_socket_listen (server, &error);
170 g_assert_no_error (error);
174 data->cancellable = g_cancellable_new ();
177 data->thread = g_thread_new ("server", server_thread, data);
183 create_server (GSocketFamily family,
184 GThreadFunc server_thread,
187 return create_server_full (family, G_SOCKET_TYPE_STREAM, server_thread, v4mapped);
190 static const gchar *testbuf = "0123456789abcdef";
193 test_ip_async_read_ready (GSocket *client,
197 IPTestData *data = user_data;
198 GError *error = NULL;
202 g_assert_cmpint (cond, ==, G_IO_IN);
204 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
205 g_assert_no_error (error);
206 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
208 g_assert_cmpstr (testbuf, ==, buf);
210 g_main_loop_quit (data->loop);
216 test_ip_async_write_ready (GSocket *client,
220 IPTestData *data = user_data;
221 GError *error = NULL;
225 g_assert_cmpint (cond, ==, G_IO_OUT);
227 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
228 g_assert_no_error (error);
229 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
231 source = g_socket_create_source (client, G_IO_IN, NULL);
232 g_source_set_callback (source, (GSourceFunc)test_ip_async_read_ready,
234 g_source_attach (source, NULL);
235 g_source_unref (source);
241 test_ip_async_timed_out (GSocket *client,
245 IPTestData *data = user_data;
246 GError *error = NULL;
251 if (data->family == G_SOCKET_FAMILY_IPV4)
253 g_assert_cmpint (cond, ==, G_IO_IN);
254 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
255 g_assert_cmpint (len, ==, -1);
256 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
257 g_clear_error (&error);
260 source = g_socket_create_source (client, G_IO_OUT, NULL);
261 g_source_set_callback (source, (GSourceFunc)test_ip_async_write_ready,
263 g_source_attach (source, NULL);
264 g_source_unref (source);
270 test_ip_async_connected (GSocket *client,
274 IPTestData *data = user_data;
275 GError *error = NULL;
280 g_socket_check_connect_result (client, &error);
281 g_assert_no_error (error);
282 /* We do this after the check_connect_result, since that will give a
283 * more useful assertion in case of error.
285 g_assert_cmpint (cond, ==, G_IO_OUT);
287 g_assert (g_socket_is_connected (client));
289 /* This adds 1 second to "make check", so let's just only do it once. */
290 if (data->family == G_SOCKET_FAMILY_IPV4)
292 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
293 g_assert_cmpint (len, ==, -1);
294 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
295 g_clear_error (&error);
297 source = g_socket_create_source (client, G_IO_IN, NULL);
298 g_source_set_callback (source, (GSourceFunc)test_ip_async_timed_out,
300 g_source_attach (source, NULL);
301 g_source_unref (source);
304 test_ip_async_timed_out (client, 0, data);
310 idle_test_ip_async_connected (gpointer user_data)
312 IPTestData *data = user_data;
314 return test_ip_async_connected (data->client, G_IO_OUT, data);
318 test_ip_async (GSocketFamily family)
321 GError *error = NULL;
323 GSocketAddress *addr;
328 data = create_server (family, echo_server_thread, FALSE);
329 addr = g_socket_get_local_address (data->server, &error);
330 g_assert_no_error (error);
332 client = g_socket_new (family,
333 G_SOCKET_TYPE_STREAM,
334 G_SOCKET_PROTOCOL_DEFAULT,
336 g_assert_no_error (error);
337 data->client = client;
339 g_assert_cmpint (g_socket_get_family (client), ==, family);
340 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
341 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
343 g_socket_set_blocking (client, FALSE);
344 g_socket_set_timeout (client, 1);
346 if (g_socket_connect (client, addr, NULL, &error))
348 g_assert_no_error (error);
349 g_idle_add (idle_test_ip_async_connected, data);
353 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
354 g_clear_error (&error);
355 source = g_socket_create_source (client, G_IO_OUT, NULL);
356 g_source_set_callback (source, (GSourceFunc)test_ip_async_connected,
358 g_source_attach (source, NULL);
359 g_source_unref (source);
361 g_object_unref (addr);
363 data->loop = g_main_loop_new (NULL, TRUE);
364 g_main_loop_run (data->loop);
365 g_main_loop_unref (data->loop);
367 g_socket_shutdown (client, FALSE, TRUE, &error);
368 g_assert_no_error (error);
370 g_thread_join (data->thread);
372 if (family == G_SOCKET_FAMILY_IPV4)
374 /* Test that reading on a remote-closed socket gets back 0 bytes. */
375 len = g_socket_receive_with_blocking (client, buf, sizeof (buf),
377 g_assert_no_error (error);
378 g_assert_cmpint (len, ==, 0);
382 /* Test that writing to a remote-closed socket gets back CONNECTION_CLOSED. */
383 len = g_socket_send_with_blocking (client, testbuf, strlen (testbuf) + 1,
385 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED);
386 g_assert_cmpint (len, ==, -1);
387 g_clear_error (&error);
390 g_socket_close (client, &error);
391 g_assert_no_error (error);
392 g_socket_close (data->server, &error);
393 g_assert_no_error (error);
395 g_object_unref (data->server);
396 g_object_unref (client);
398 g_slice_free (IPTestData, data);
402 test_ipv4_async (void)
404 test_ip_async (G_SOCKET_FAMILY_IPV4);
408 test_ipv6_async (void)
412 g_test_skip ("No support for IPv6");
416 test_ip_async (G_SOCKET_FAMILY_IPV6);
419 static const gchar testbuf2[] = "0123456789abcdefghijklmnopqrstuvwxyz";
422 test_ip_sync (GSocketFamily family)
425 GError *error = NULL;
427 GSocketAddress *addr;
431 data = create_server (family, echo_server_thread, FALSE);
432 addr = g_socket_get_local_address (data->server, &error);
433 g_assert_no_error (error);
435 client = g_socket_new (family,
436 G_SOCKET_TYPE_STREAM,
437 G_SOCKET_PROTOCOL_DEFAULT,
439 g_assert_no_error (error);
441 g_assert_cmpint (g_socket_get_family (client), ==, family);
442 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
443 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
445 g_socket_set_blocking (client, TRUE);
446 g_socket_set_timeout (client, 1);
448 g_socket_connect (client, addr, NULL, &error);
449 g_assert_no_error (error);
450 g_assert (g_socket_is_connected (client));
451 g_object_unref (addr);
453 /* This adds 1 second to "make check", so let's just only do it once. */
454 if (family == G_SOCKET_FAMILY_IPV4)
456 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
457 g_assert_cmpint (len, ==, -1);
458 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
459 g_clear_error (&error);
462 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
463 g_assert_no_error (error);
464 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
466 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
467 g_assert_no_error (error);
468 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
470 g_assert_cmpstr (testbuf, ==, buf);
473 GOutputVector v[7] = { { NULL, }, };
475 v[0].buffer = testbuf2 + 0;
477 v[1].buffer = testbuf2 + 3;
479 v[2].buffer = testbuf2 + 3 + 5;
481 v[3].buffer = testbuf2 + 3 + 5;
483 v[4].buffer = testbuf2 + 3 + 5 + 6;
485 v[5].buffer = testbuf2 + 3 + 5 + 6 + 2;
487 v[6].buffer = testbuf2 + 3 + 5 + 6 + 2 + 1;
488 v[6].size = strlen (testbuf2) - (3 + 5 + 6 + 2 + 1);
490 len = g_socket_send_message (client, NULL, v, G_N_ELEMENTS (v), NULL, 0, 0, NULL, &error);
491 g_assert_no_error (error);
492 g_assert_cmpint (len, ==, strlen (testbuf2));
494 memset (buf, 0, sizeof (buf));
495 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
496 g_assert_no_error (error);
497 g_assert_cmpint (len, ==, strlen (testbuf2));
498 g_assert_cmpstr (testbuf2, ==, buf);
501 g_socket_shutdown (client, FALSE, TRUE, &error);
502 g_assert_no_error (error);
504 g_thread_join (data->thread);
506 if (family == G_SOCKET_FAMILY_IPV4)
508 /* Test that reading on a remote-closed socket gets back 0 bytes. */
509 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
510 g_assert_no_error (error);
511 g_assert_cmpint (len, ==, 0);
515 /* Test that writing to a remote-closed socket gets back CONNECTION_CLOSED. */
516 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
517 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED);
518 g_assert_cmpint (len, ==, -1);
519 g_clear_error (&error);
522 g_socket_close (client, &error);
523 g_assert_no_error (error);
524 g_socket_close (data->server, &error);
525 g_assert_no_error (error);
527 g_object_unref (data->server);
528 g_object_unref (client);
530 g_slice_free (IPTestData, data);
534 test_ipv4_sync (void)
536 test_ip_sync (G_SOCKET_FAMILY_IPV4);
540 test_ipv6_sync (void)
544 g_test_skip ("No support for IPv6");
548 test_ip_sync (G_SOCKET_FAMILY_IPV6);
552 test_ip_sync_dgram (GSocketFamily family)
555 GError *error = NULL;
557 GSocketAddress *dest_addr;
561 data = create_server_full (family, G_SOCKET_TYPE_DATAGRAM,
562 echo_server_dgram_thread, FALSE);
564 dest_addr = g_socket_get_local_address (data->server, &error);
566 client = g_socket_new (family,
567 G_SOCKET_TYPE_DATAGRAM,
568 G_SOCKET_PROTOCOL_DEFAULT,
570 g_assert_no_error (error);
572 g_assert_cmpint (g_socket_get_family (client), ==, family);
573 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_DATAGRAM);
574 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
576 g_socket_set_blocking (client, TRUE);
577 g_socket_set_timeout (client, 1);
579 len = g_socket_send_to (client, dest_addr, testbuf, strlen (testbuf) + 1, NULL, &error);
580 g_assert_no_error (error);
581 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
583 len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
584 g_assert_no_error (error);
585 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
587 g_assert_cmpstr (testbuf, ==, buf);
590 GOutputMessage m[3] = { { NULL, }, };
591 GOutputVector v[7] = { { NULL, }, };
593 v[0].buffer = testbuf2 + 0;
595 v[1].buffer = testbuf2 + 3;
597 v[2].buffer = testbuf2 + 3 + 5;
599 v[3].buffer = testbuf2 + 3 + 5;
601 v[4].buffer = testbuf2 + 3 + 5 + 6;
603 v[5].buffer = testbuf2 + 3 + 5 + 6 + 2;
605 v[6].buffer = testbuf2 + 3 + 5 + 6 + 2 + 1;
606 v[6].size = strlen (testbuf2) - (3 + 5 + 6 + 2 + 1);
608 len = g_socket_send_message (client, dest_addr, v, G_N_ELEMENTS (v), NULL, 0, 0, NULL, &error);
609 g_assert_no_error (error);
610 g_assert_cmpint (len, ==, strlen (testbuf2));
612 memset (buf, 0, sizeof (buf));
613 len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
614 g_assert_no_error (error);
615 g_assert_cmpint (len, ==, strlen (testbuf2));
616 g_assert_cmpstr (testbuf2, ==, buf);
618 m[0].vectors = &v[0];
619 m[0].num_vectors = 1;
620 m[0].address = dest_addr;
621 m[1].vectors = &v[0];
622 m[1].num_vectors = 6;
623 m[1].address = dest_addr;
624 m[2].vectors = &v[6];
625 m[2].num_vectors = 1;
626 m[2].address = dest_addr;
628 len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
629 g_assert_no_error (error);
630 g_assert_cmpint (len, ==, G_N_ELEMENTS (m));
631 g_assert_cmpint (m[0].bytes_sent, ==, 3);
632 g_assert_cmpint (m[1].bytes_sent, ==, 17);
633 g_assert_cmpint (m[2].bytes_sent, ==, v[6].size);
635 memset (buf, 0, sizeof (buf));
636 len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
637 g_assert_no_error (error);
638 g_assert_cmpint (len, ==, 3);
640 memset (buf, 0, sizeof (buf));
641 len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
642 g_assert_no_error (error);
643 /* v[0].size + v[1].size + v[2].size + v[3].size + v[4].size + v[5].size */
644 g_assert_cmpint (len, ==, 17);
645 g_assert (memcmp (testbuf2, buf, 17) == 0);
647 memset (buf, 0, sizeof (buf));
648 len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
649 g_assert_no_error (error);
650 g_assert_cmpint (len, ==, v[6].size);
651 g_assert_cmpstr (buf, ==, v[6].buffer);
653 /* reset since we're re-using the message structs */
658 /* now try to generate an error by omitting the destination address on [1] */
660 len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
661 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
662 g_clear_error (&error);
663 g_assert_cmpint (len, ==, -1);
665 g_assert_cmpint (m[0].bytes_sent, ==, 3);
666 g_assert_cmpint (m[1].bytes_sent, ==, 0);
667 g_assert_cmpint (m[2].bytes_sent, ==, 0);
669 len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
670 g_assert_cmpint (len, ==, 3);
673 g_cancellable_cancel (data->cancellable);
675 g_thread_join (data->thread);
677 g_socket_close (client, &error);
678 g_assert_no_error (error);
679 g_socket_close (data->server, &error);
680 g_assert_no_error (error);
682 g_object_unref (data->server);
683 g_object_unref (data->cancellable);
684 g_object_unref (client);
685 g_object_unref (dest_addr);
687 g_slice_free (IPTestData, data);
691 test_ipv4_sync_dgram (void)
693 test_ip_sync_dgram (G_SOCKET_FAMILY_IPV4);
697 test_ipv6_sync_dgram (void)
701 g_test_skip ("No support for IPv6");
705 test_ip_sync_dgram (G_SOCKET_FAMILY_IPV6);
709 graceful_server_thread (gpointer user_data)
711 IPTestData *data = user_data;
713 GError *error = NULL;
716 sock = g_socket_accept (data->server, NULL, &error);
717 g_assert_no_error (error);
719 len = g_socket_send (sock, testbuf, strlen (testbuf) + 1, NULL, &error);
720 g_assert_no_error (error);
721 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
727 test_close_graceful (void)
729 GSocketFamily family = G_SOCKET_FAMILY_IPV4;
731 GError *error = NULL;
732 GSocket *client, *server;
733 GSocketAddress *addr;
737 data = create_server (family, graceful_server_thread, FALSE);
738 addr = g_socket_get_local_address (data->server, &error);
739 g_assert_no_error (error);
741 client = g_socket_new (family,
742 G_SOCKET_TYPE_STREAM,
743 G_SOCKET_PROTOCOL_DEFAULT,
745 g_assert_no_error (error);
747 g_assert_cmpint (g_socket_get_family (client), ==, family);
748 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
749 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
751 g_socket_set_blocking (client, TRUE);
752 g_socket_set_timeout (client, 1);
754 g_socket_connect (client, addr, NULL, &error);
755 g_assert_no_error (error);
756 g_assert (g_socket_is_connected (client));
757 g_object_unref (addr);
759 server = g_thread_join (data->thread);
761 /* similar to g_tcp_connection_set_graceful_disconnect(), but explicit */
762 g_socket_shutdown (server, FALSE, TRUE, &error);
763 g_assert_no_error (error);
765 /* we must timeout */
766 g_socket_condition_wait (client, G_IO_HUP, NULL, &error);
767 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
768 g_clear_error (&error);
770 /* check that the remaining data is received */
771 len = g_socket_receive (client, buf, strlen (testbuf) + 1, NULL, &error);
772 g_assert_no_error (error);
773 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
775 /* and only then the connection is closed */
776 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
777 g_assert_no_error (error);
778 g_assert_cmpint (len, ==, 0);
780 g_socket_close (server, &error);
781 g_assert_no_error (error);
783 g_socket_close (client, &error);
784 g_assert_no_error (error);
786 g_object_unref (server);
787 g_object_unref (data->server);
788 g_object_unref (client);
790 g_slice_free (IPTestData, data);
793 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
795 v4mapped_server_thread (gpointer user_data)
797 IPTestData *data = user_data;
799 GError *error = NULL;
800 GSocketAddress *addr;
802 sock = g_socket_accept (data->server, NULL, &error);
803 g_assert_no_error (error);
805 g_assert_cmpint (g_socket_get_family (sock), ==, G_SOCKET_FAMILY_IPV6);
807 addr = g_socket_get_local_address (sock, &error);
808 g_assert_no_error (error);
809 g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
810 g_object_unref (addr);
812 addr = g_socket_get_remote_address (sock, &error);
813 g_assert_no_error (error);
814 g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
815 g_object_unref (addr);
817 g_socket_close (sock, &error);
818 g_assert_no_error (error);
819 g_object_unref (sock);
824 test_ipv6_v4mapped (void)
827 GError *error = NULL;
829 GSocketAddress *addr, *v4addr;
834 g_test_skip ("No support for IPv6");
838 data = create_server (G_SOCKET_FAMILY_IPV6, v4mapped_server_thread, TRUE);
842 g_test_message ("Test not run: not supported by the OS");
846 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
847 G_SOCKET_TYPE_STREAM,
848 G_SOCKET_PROTOCOL_DEFAULT,
850 g_assert_no_error (error);
852 g_socket_set_blocking (client, TRUE);
853 g_socket_set_timeout (client, 1);
855 addr = g_socket_get_local_address (data->server, &error);
856 g_assert_no_error (error);
857 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
858 v4addr = g_inet_socket_address_new (iaddr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
859 g_object_unref (iaddr);
860 g_object_unref (addr);
862 g_socket_connect (client, v4addr, NULL, &error);
863 g_assert_no_error (error);
864 g_assert (g_socket_is_connected (client));
866 g_thread_join (data->thread);
868 g_socket_close (client, &error);
869 g_assert_no_error (error);
870 g_socket_close (data->server, &error);
871 g_assert_no_error (error);
873 g_object_unref (data->server);
874 g_object_unref (client);
875 g_object_unref (v4addr);
877 g_slice_free (IPTestData, data);
882 test_timed_wait (void)
885 GError *error = NULL;
887 GSocketAddress *addr;
891 data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE);
892 addr = g_socket_get_local_address (data->server, &error);
893 g_assert_no_error (error);
895 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
896 G_SOCKET_TYPE_STREAM,
897 G_SOCKET_PROTOCOL_DEFAULT,
899 g_assert_no_error (error);
901 g_socket_set_blocking (client, TRUE);
902 g_socket_set_timeout (client, 1);
904 g_socket_connect (client, addr, NULL, &error);
905 g_assert_no_error (error);
906 g_object_unref (addr);
908 start_time = g_get_monotonic_time ();
909 g_socket_condition_timed_wait (client, G_IO_IN, 100000 /* 100 ms */,
911 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
912 g_clear_error (&error);
913 poll_duration = g_get_monotonic_time () - start_time;
915 g_assert_cmpint (poll_duration, >=, 98000);
916 g_assert_cmpint (poll_duration, <, 112000);
918 g_socket_close (client, &error);
919 g_assert_no_error (error);
921 g_thread_join (data->thread);
923 g_socket_close (data->server, &error);
924 g_assert_no_error (error);
926 g_object_unref (data->server);
927 g_object_unref (client);
929 g_slice_free (IPTestData, data);
935 struct sockaddr_in6 sin6, gsin6;
936 GSocketAddress *saddr;
937 GInetSocketAddress *isaddr;
939 GError *error = NULL;
941 memset (&sin6, 0, sizeof (sin6));
942 sin6.sin6_family = AF_INET6;
943 sin6.sin6_addr = in6addr_loopback;
944 sin6.sin6_port = g_htons (42);
945 sin6.sin6_scope_id = 17;
946 sin6.sin6_flowinfo = 1729;
948 saddr = g_socket_address_new_from_native (&sin6, sizeof (sin6));
949 g_assert (G_IS_INET_SOCKET_ADDRESS (saddr));
951 isaddr = G_INET_SOCKET_ADDRESS (saddr);
952 iaddr = g_inet_socket_address_get_address (isaddr);
953 g_assert_cmpint (g_inet_address_get_family (iaddr), ==, G_SOCKET_FAMILY_IPV6);
954 g_assert (g_inet_address_get_is_loopback (iaddr));
956 g_assert_cmpint (g_inet_socket_address_get_port (isaddr), ==, 42);
957 g_assert_cmpint (g_inet_socket_address_get_scope_id (isaddr), ==, 17);
958 g_assert_cmpint (g_inet_socket_address_get_flowinfo (isaddr), ==, 1729);
960 g_socket_address_to_native (saddr, &gsin6, sizeof (gsin6), &error);
961 g_assert_no_error (error);
963 g_assert (memcmp (&sin6.sin6_addr, &gsin6.sin6_addr, sizeof (struct in6_addr)) == 0);
964 g_assert_cmpint (sin6.sin6_port, ==, gsin6.sin6_port);
965 g_assert_cmpint (sin6.sin6_scope_id, ==, gsin6.sin6_scope_id);
966 g_assert_cmpint (sin6.sin6_flowinfo, ==, gsin6.sin6_flowinfo);
968 g_object_unref (saddr);
973 test_unix_from_fd (void)
979 fd = socket (AF_UNIX, SOCK_STREAM, 0);
980 g_assert_cmpint (fd, !=, -1);
983 s = g_socket_new_from_fd (fd, &error);
984 g_assert_no_error (error);
985 g_assert_cmpint (g_socket_get_family (s), ==, G_SOCKET_FAMILY_UNIX);
986 g_assert_cmpint (g_socket_get_socket_type (s), ==, G_SOCKET_TYPE_STREAM);
987 g_assert_cmpint (g_socket_get_protocol (s), ==, G_SOCKET_PROTOCOL_DEFAULT);
992 test_unix_connection (void)
997 GSocketConnection *c;
999 fd = socket (AF_UNIX, SOCK_STREAM, 0);
1000 g_assert_cmpint (fd, !=, -1);
1003 s = g_socket_new_from_fd (fd, &error);
1004 g_assert_no_error (error);
1005 c = g_socket_connection_factory_create_connection (s);
1006 g_assert (G_IS_UNIX_CONNECTION (c));
1011 static GSocketConnection *
1012 create_connection_for_fd (int fd)
1016 GSocketConnection *connection;
1018 socket = g_socket_new_from_fd (fd, &err);
1019 g_assert_no_error (err);
1020 g_assert (G_IS_SOCKET (socket));
1021 connection = g_socket_connection_factory_create_connection (socket);
1022 g_assert (G_IS_UNIX_CONNECTION (connection));
1023 g_object_unref (socket);
1027 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
1030 test_unix_connection_ancillary_data (void)
1034 gint status, fd, len;
1039 g_assert_cmpint (status, ==, 0);
1041 status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
1042 g_assert_cmpint (status, ==, 0);
1045 g_assert_cmpint (pid, >=, 0);
1047 /* Child: close its copy of the write end of the pipe, receive it
1048 * again from the parent over the socket, and write some text to it.
1050 * Parent: send the write end of the pipe (still open for the
1051 * parent) over the socket, close it, and read some text from the
1052 * read end of the pipe.
1056 GSocketConnection *connection;
1059 connection = create_connection_for_fd (sv[0]);
1061 status = close (pv[1]);
1062 g_assert_cmpint (status, ==, 0);
1065 fd = g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection), NULL,
1067 g_assert_no_error (err);
1068 g_assert_cmpint (fd, >, -1);
1069 g_object_unref (connection);
1072 len = write (fd, TEST_DATA, sizeof (TEST_DATA));
1073 while (len == -1 && errno == EINTR);
1074 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
1079 GSocketConnection *connection;
1082 connection = create_connection_for_fd (sv[1]);
1085 g_unix_connection_send_fd (G_UNIX_CONNECTION (connection), pv[1], NULL,
1087 g_assert_no_error (err);
1088 g_object_unref (connection);
1090 status = close (pv[1]);
1091 g_assert_cmpint (status, ==, 0);
1093 memset (buffer, 0xff, sizeof buffer);
1095 len = read (pv[0], buffer, sizeof buffer);
1096 while (len == -1 && errno == EINTR);
1098 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
1099 g_assert_cmpstr (buffer, ==, TEST_DATA);
1101 waitpid (pid, &status, 0);
1102 g_assert (WIFEXITED (status));
1103 g_assert_cmpint (WEXITSTATUS (status), ==, 0);
1106 /* TODO: add test for g_unix_connection_send_credentials() and
1107 * g_unix_connection_receive_credentials().
1110 #endif /* G_OS_UNIX */
1113 test_reuse_tcp (void)
1115 GSocket *sock1, *sock2;
1116 GError *error = NULL;
1117 GInetAddress *iaddr;
1118 GSocketAddress *addr;
1120 sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1121 G_SOCKET_TYPE_STREAM,
1122 G_SOCKET_PROTOCOL_DEFAULT,
1124 g_assert_no_error (error);
1126 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1127 addr = g_inet_socket_address_new (iaddr, 0);
1128 g_object_unref (iaddr);
1129 g_socket_bind (sock1, addr, TRUE, &error);
1130 g_object_unref (addr);
1131 g_assert_no_error (error);
1133 g_socket_listen (sock1, &error);
1134 g_assert_no_error (error);
1136 sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1137 G_SOCKET_TYPE_STREAM,
1138 G_SOCKET_PROTOCOL_DEFAULT,
1140 g_assert_no_error (error);
1142 addr = g_socket_get_local_address (sock1, &error);
1143 g_assert_no_error (error);
1144 g_socket_bind (sock2, addr, TRUE, &error);
1145 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_ADDRESS_IN_USE);
1146 g_clear_error (&error);
1147 g_object_unref (addr);
1149 g_object_unref (sock1);
1150 g_object_unref (sock2);
1154 test_reuse_udp (void)
1156 GSocket *sock1, *sock2;
1157 GError *error = NULL;
1158 GInetAddress *iaddr;
1159 GSocketAddress *addr;
1161 sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1162 G_SOCKET_TYPE_DATAGRAM,
1163 G_SOCKET_PROTOCOL_DEFAULT,
1165 g_assert_no_error (error);
1167 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1168 addr = g_inet_socket_address_new (iaddr, 0);
1169 g_object_unref (iaddr);
1170 g_socket_bind (sock1, addr, TRUE, &error);
1171 g_object_unref (addr);
1172 g_assert_no_error (error);
1174 sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1175 G_SOCKET_TYPE_DATAGRAM,
1176 G_SOCKET_PROTOCOL_DEFAULT,
1178 g_assert_no_error (error);
1180 addr = g_socket_get_local_address (sock1, &error);
1181 g_assert_no_error (error);
1182 g_socket_bind (sock2, addr, TRUE, &error);
1183 g_object_unref (addr);
1184 g_assert_no_error (error);
1186 g_object_unref (sock1);
1187 g_object_unref (sock2);
1191 test_get_available (gconstpointer user_data)
1193 GSocketType socket_type = GPOINTER_TO_UINT (user_data);
1195 GSocket *listener, *server, *client;
1197 GSocketAddress *saddr;
1198 gchar data[] = "0123456789abcdef";
1202 listener = g_socket_new (G_SOCKET_FAMILY_IPV4,
1204 G_SOCKET_PROTOCOL_DEFAULT,
1206 g_assert_no_error (err);
1207 g_assert (G_IS_SOCKET (listener));
1209 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1211 G_SOCKET_PROTOCOL_DEFAULT,
1213 g_assert_no_error (err);
1214 g_assert (G_IS_SOCKET (client));
1216 if (socket_type == G_SOCKET_TYPE_STREAM)
1218 g_socket_set_option (client, IPPROTO_TCP, TCP_NODELAY, TRUE, &err);
1219 g_assert_no_error (err);
1222 addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
1223 saddr = g_inet_socket_address_new (addr, 0);
1225 g_socket_bind (listener, saddr, TRUE, &err);
1226 g_assert_no_error (err);
1227 g_object_unref (saddr);
1228 g_object_unref (addr);
1230 saddr = g_socket_get_local_address (listener, &err);
1231 g_assert_no_error (err);
1233 if (socket_type == G_SOCKET_TYPE_STREAM)
1235 g_socket_listen (listener, &err);
1236 g_assert_no_error (err);
1237 g_socket_connect (client, saddr, NULL, &err);
1238 g_assert_no_error (err);
1240 server = g_socket_accept (listener, NULL, &err);
1241 g_assert_no_error (err);
1242 g_socket_set_blocking (server, FALSE);
1243 g_object_unref (listener);
1248 g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
1249 g_assert_no_error (err);
1251 while (!g_socket_condition_wait (server, G_IO_IN, NULL, NULL))
1253 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1255 g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
1256 g_assert_no_error (err);
1258 /* We need to wait until the data has actually been copied into the
1259 * server socket's buffers, but g_socket_condition_wait() won't help
1260 * here since the socket is definitely already readable. So there's
1261 * a race condition in checking its available bytes. In the TCP
1262 * case, we poll for a bit until the new data shows up. In the UDP
1263 * case, there's not much we can do, but at least the failure mode
1264 * is passes-when-it-shouldn't, not fails-when-it-shouldn't.
1266 if (socket_type == G_SOCKET_TYPE_STREAM)
1270 for (tries = 0; tries < 100; tries++)
1272 if (g_socket_get_available_bytes (server) > sizeof (data))
1277 g_assert_cmpint (g_socket_get_available_bytes (server), ==, 2 * sizeof (data));
1282 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1285 g_assert_cmpint (sizeof (buf), >=, 2 * sizeof (data));
1286 nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
1287 g_assert_no_error (err);
1289 if (socket_type == G_SOCKET_TYPE_STREAM)
1291 g_assert_cmpint (nread, ==, 2 * sizeof (data));
1292 g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1296 g_assert_cmpint (nread, ==, sizeof (data));
1297 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1300 nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
1301 if (socket_type == G_SOCKET_TYPE_STREAM)
1303 g_assert_cmpint (nread, ==, -1);
1304 g_assert_error (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
1305 g_clear_error (&err);
1309 g_assert_cmpint (nread, ==, sizeof (data));
1310 g_assert_no_error (err);
1313 g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1315 g_socket_close (server, &err);
1316 g_assert_no_error (err);
1318 g_object_unref (saddr);
1319 g_object_unref (server);
1320 g_object_unref (client);
1328 GError *error = NULL;
1330 g_test_init (&argc, &argv, NULL);
1332 sock = g_socket_new (G_SOCKET_FAMILY_IPV6,
1333 G_SOCKET_TYPE_STREAM,
1334 G_SOCKET_PROTOCOL_DEFAULT,
1338 ipv6_supported = TRUE;
1339 g_object_unref (sock);
1343 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
1344 g_clear_error (&error);
1347 g_test_add_func ("/socket/ipv4_sync", test_ipv4_sync);
1348 g_test_add_func ("/socket/ipv4_async", test_ipv4_async);
1349 g_test_add_func ("/socket/ipv6_sync", test_ipv6_sync);
1350 g_test_add_func ("/socket/ipv6_async", test_ipv6_async);
1351 g_test_add_func ("/socket/ipv4_sync/datagram", test_ipv4_sync_dgram);
1352 g_test_add_func ("/socket/ipv6_sync/datagram", test_ipv6_sync_dgram);
1353 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
1354 g_test_add_func ("/socket/ipv6_v4mapped", test_ipv6_v4mapped);
1356 g_test_add_func ("/socket/close_graceful", test_close_graceful);
1357 g_test_add_func ("/socket/timed_wait", test_timed_wait);
1358 g_test_add_func ("/socket/address", test_sockaddr);
1360 g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
1361 g_test_add_func ("/socket/unix-connection", test_unix_connection);
1362 g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data);
1364 g_test_add_func ("/socket/reuse/tcp", test_reuse_tcp);
1365 g_test_add_func ("/socket/reuse/udp", test_reuse_udp);
1366 g_test_add_data_func ("/socket/get_available/datagram", GUINT_TO_POINTER (G_SOCKET_TYPE_DATAGRAM),
1367 test_get_available);
1368 g_test_add_data_func ("/socket/get_available/stream", GUINT_TO_POINTER (G_SOCKET_TYPE_STREAM),
1369 test_get_available);
1371 return g_test_run();