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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
28 #include <gio/gnetworking.h>
29 #include <gio/gunixconnection.h>
32 #include "gnetworkingprivate.h"
43 echo_server_thread (gpointer user_data)
45 IPTestData *data = user_data;
51 sock = g_socket_accept (data->server, NULL, &error);
52 g_assert_no_error (error);
56 nread = g_socket_receive (sock, buf, sizeof (buf), NULL, &error);
57 g_assert_no_error (error);
58 g_assert_cmpint (nread, >=, 0);
63 nwrote = g_socket_send (sock, buf, nread, NULL, &error);
64 g_assert_no_error (error);
65 g_assert_cmpint (nwrote, ==, nread);
68 g_socket_close (sock, &error);
69 g_assert_no_error (error);
70 g_object_unref (sock);
75 create_server (GSocketFamily family,
76 GThreadFunc server_thread,
85 data = g_slice_new (IPTestData);
86 data->family = family;
88 data->server = server = g_socket_new (family,
90 G_SOCKET_PROTOCOL_DEFAULT,
92 g_assert_no_error (error);
94 g_assert_cmpint (g_socket_get_family (server), ==, family);
95 g_assert_cmpint (g_socket_get_socket_type (server), ==, G_SOCKET_TYPE_STREAM);
96 g_assert_cmpint (g_socket_get_protocol (server), ==, G_SOCKET_PROTOCOL_DEFAULT);
98 g_socket_set_blocking (server, TRUE);
100 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
103 g_socket_set_option (data->server, IPPROTO_IPV6, IPV6_V6ONLY, FALSE, NULL);
104 if (! g_socket_speaks_ipv4 (data->server))
106 g_object_unref (data->server);
107 g_slice_free (IPTestData, data);
114 iaddr = g_inet_address_new_any (family);
116 iaddr = g_inet_address_new_loopback (family);
117 addr = g_inet_socket_address_new (iaddr, 0);
118 g_object_unref (iaddr);
120 g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), ==, 0);
121 g_socket_bind (server, addr, TRUE, &error);
122 g_assert_no_error (error);
123 g_object_unref (addr);
125 addr = g_socket_get_local_address (server, &error);
126 g_assert_no_error (error);
127 g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), !=, 0);
128 g_object_unref (addr);
130 g_socket_listen (server, &error);
131 g_assert_no_error (error);
133 data->thread = g_thread_new ("server", server_thread, data);
138 static const gchar *testbuf = "0123456789abcdef";
141 test_ip_async_read_ready (GSocket *client,
145 IPTestData *data = user_data;
146 GError *error = NULL;
150 g_assert_cmpint (cond, ==, G_IO_IN);
152 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
153 g_assert_no_error (error);
154 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
156 g_assert_cmpstr (testbuf, ==, buf);
158 g_main_loop_quit (data->loop);
164 test_ip_async_write_ready (GSocket *client,
168 IPTestData *data = user_data;
169 GError *error = NULL;
173 g_assert_cmpint (cond, ==, G_IO_OUT);
175 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
176 g_assert_no_error (error);
177 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
179 source = g_socket_create_source (client, G_IO_IN, NULL);
180 g_source_set_callback (source, (GSourceFunc)test_ip_async_read_ready,
182 g_source_attach (source, NULL);
183 g_source_unref (source);
189 test_ip_async_timed_out (GSocket *client,
193 IPTestData *data = user_data;
194 GError *error = NULL;
199 if (data->family == G_SOCKET_FAMILY_IPV4)
201 g_assert_cmpint (cond, ==, G_IO_IN);
202 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
203 g_assert_cmpint (len, ==, -1);
204 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
205 g_clear_error (&error);
208 source = g_socket_create_source (client, G_IO_OUT, NULL);
209 g_source_set_callback (source, (GSourceFunc)test_ip_async_write_ready,
211 g_source_attach (source, NULL);
212 g_source_unref (source);
218 test_ip_async_connected (GSocket *client,
222 IPTestData *data = user_data;
223 GError *error = NULL;
228 g_socket_check_connect_result (client, &error);
229 g_assert_no_error (error);
230 /* We do this after the check_connect_result, since that will give a
231 * more useful assertion in case of error.
233 g_assert_cmpint (cond, ==, G_IO_OUT);
235 g_assert (g_socket_is_connected (client));
237 /* This adds 1 second to "make check", so let's just only do it once. */
238 if (data->family == G_SOCKET_FAMILY_IPV4)
240 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
241 g_assert_cmpint (len, ==, -1);
242 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
243 g_clear_error (&error);
245 source = g_socket_create_source (client, G_IO_IN, NULL);
246 g_source_set_callback (source, (GSourceFunc)test_ip_async_timed_out,
248 g_source_attach (source, NULL);
249 g_source_unref (source);
252 test_ip_async_timed_out (client, 0, data);
258 idle_test_ip_async_connected (gpointer user_data)
260 IPTestData *data = user_data;
262 return test_ip_async_connected (data->client, G_IO_OUT, data);
266 test_ip_async (GSocketFamily family)
269 GError *error = NULL;
271 GSocketAddress *addr;
276 data = create_server (family, echo_server_thread, FALSE);
277 addr = g_socket_get_local_address (data->server, &error);
279 client = g_socket_new (family,
280 G_SOCKET_TYPE_STREAM,
281 G_SOCKET_PROTOCOL_DEFAULT,
283 g_assert_no_error (error);
284 data->client = client;
286 g_assert_cmpint (g_socket_get_family (client), ==, family);
287 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
288 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
290 g_socket_set_blocking (client, FALSE);
291 g_socket_set_timeout (client, 1);
293 if (g_socket_connect (client, addr, NULL, &error))
295 g_assert_no_error (error);
296 g_idle_add (idle_test_ip_async_connected, data);
300 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
301 g_clear_error (&error);
302 source = g_socket_create_source (client, G_IO_OUT, NULL);
303 g_source_set_callback (source, (GSourceFunc)test_ip_async_connected,
305 g_source_attach (source, NULL);
306 g_source_unref (source);
308 g_object_unref (addr);
310 data->loop = g_main_loop_new (NULL, TRUE);
311 g_main_loop_run (data->loop);
312 g_main_loop_unref (data->loop);
314 g_socket_shutdown (client, FALSE, TRUE, &error);
315 g_assert_no_error (error);
317 g_thread_join (data->thread);
319 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
320 g_assert_no_error (error);
321 g_assert_cmpint (len, ==, 0);
323 g_socket_close (client, &error);
324 g_assert_no_error (error);
325 g_socket_close (data->server, &error);
326 g_assert_no_error (error);
328 g_object_unref (data->server);
329 g_object_unref (client);
331 g_slice_free (IPTestData, data);
335 test_ipv4_async (void)
337 test_ip_async (G_SOCKET_FAMILY_IPV4);
341 test_ipv6_async (void)
343 test_ip_async (G_SOCKET_FAMILY_IPV6);
347 test_ip_sync (GSocketFamily family)
350 GError *error = NULL;
352 GSocketAddress *addr;
356 data = create_server (family, echo_server_thread, FALSE);
357 addr = g_socket_get_local_address (data->server, &error);
359 client = g_socket_new (family,
360 G_SOCKET_TYPE_STREAM,
361 G_SOCKET_PROTOCOL_DEFAULT,
363 g_assert_no_error (error);
365 g_assert_cmpint (g_socket_get_family (client), ==, family);
366 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
367 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
369 g_socket_set_blocking (client, TRUE);
370 g_socket_set_timeout (client, 1);
372 g_socket_connect (client, addr, NULL, &error);
373 g_assert_no_error (error);
374 g_assert (g_socket_is_connected (client));
375 g_object_unref (addr);
377 /* This adds 1 second to "make check", so let's just only do it once. */
378 if (family == G_SOCKET_FAMILY_IPV4)
380 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
381 g_assert_cmpint (len, ==, -1);
382 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
383 g_clear_error (&error);
386 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
387 g_assert_no_error (error);
388 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
390 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
391 g_assert_no_error (error);
392 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
394 g_assert_cmpstr (testbuf, ==, buf);
396 g_socket_shutdown (client, FALSE, TRUE, &error);
397 g_assert_no_error (error);
399 g_thread_join (data->thread);
401 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
402 g_assert_no_error (error);
403 g_assert_cmpint (len, ==, 0);
405 g_socket_close (client, &error);
406 g_assert_no_error (error);
407 g_socket_close (data->server, &error);
408 g_assert_no_error (error);
410 g_object_unref (data->server);
411 g_object_unref (client);
413 g_slice_free (IPTestData, data);
417 test_ipv4_sync (void)
419 test_ip_sync (G_SOCKET_FAMILY_IPV4);
423 test_ipv6_sync (void)
425 test_ip_sync (G_SOCKET_FAMILY_IPV6);
429 graceful_server_thread (gpointer user_data)
431 IPTestData *data = user_data;
433 GError *error = NULL;
436 sock = g_socket_accept (data->server, NULL, &error);
437 g_assert_no_error (error);
439 len = g_socket_send (sock, testbuf, strlen (testbuf) + 1, NULL, &error);
440 g_assert_no_error (error);
441 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
447 test_close_graceful (void)
449 GSocketFamily family = G_SOCKET_FAMILY_IPV4;
451 GError *error = NULL;
452 GSocket *client, *server;
453 GSocketAddress *addr;
457 data = create_server (family, graceful_server_thread, FALSE);
458 addr = g_socket_get_local_address (data->server, &error);
460 client = g_socket_new (family,
461 G_SOCKET_TYPE_STREAM,
462 G_SOCKET_PROTOCOL_DEFAULT,
464 g_assert_no_error (error);
466 g_assert_cmpint (g_socket_get_family (client), ==, family);
467 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
468 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
470 g_socket_set_blocking (client, TRUE);
471 g_socket_set_timeout (client, 1);
473 g_socket_connect (client, addr, NULL, &error);
474 g_assert_no_error (error);
475 g_assert (g_socket_is_connected (client));
476 g_object_unref (addr);
478 server = g_thread_join (data->thread);
480 /* similar to g_tcp_connection_set_graceful_disconnect(), but explicit */
481 g_socket_shutdown (server, FALSE, TRUE, &error);
482 g_assert_no_error (error);
484 /* we must timeout */
485 g_socket_condition_wait (client, G_IO_HUP, NULL, &error);
486 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
487 g_clear_error (&error);
489 /* check that the remaining data is received */
490 len = g_socket_receive (client, buf, strlen (testbuf) + 1, NULL, &error);
491 g_assert_no_error (error);
492 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
494 /* and only then the connection is closed */
495 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
496 g_assert_no_error (error);
497 g_assert_cmpint (len, ==, 0);
499 g_socket_close (server, &error);
500 g_assert_no_error (error);
502 g_socket_close (client, &error);
503 g_assert_no_error (error);
505 g_object_unref (server);
506 g_object_unref (data->server);
507 g_object_unref (client);
509 g_slice_free (IPTestData, data);
512 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
514 v4mapped_server_thread (gpointer user_data)
516 IPTestData *data = user_data;
518 GError *error = NULL;
519 GSocketAddress *addr;
521 sock = g_socket_accept (data->server, NULL, &error);
522 g_assert_no_error (error);
524 g_assert_cmpint (g_socket_get_family (sock), ==, G_SOCKET_FAMILY_IPV6);
526 addr = g_socket_get_local_address (sock, &error);
527 g_assert_no_error (error);
528 g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
529 g_object_unref (addr);
531 addr = g_socket_get_remote_address (sock, &error);
532 g_assert_no_error (error);
533 g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
534 g_object_unref (addr);
536 g_socket_close (sock, &error);
537 g_assert_no_error (error);
538 g_object_unref (sock);
543 test_ipv6_v4mapped (void)
546 GError *error = NULL;
548 GSocketAddress *addr, *v4addr;
551 data = create_server (G_SOCKET_FAMILY_IPV6, v4mapped_server_thread, TRUE);
555 g_test_message ("Test not run: not supported by the OS");
559 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
560 G_SOCKET_TYPE_STREAM,
561 G_SOCKET_PROTOCOL_DEFAULT,
563 g_assert_no_error (error);
565 g_socket_set_blocking (client, TRUE);
566 g_socket_set_timeout (client, 1);
568 addr = g_socket_get_local_address (data->server, &error);
569 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
570 v4addr = g_inet_socket_address_new (iaddr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
571 g_object_unref (iaddr);
572 g_object_unref (addr);
574 g_socket_connect (client, v4addr, NULL, &error);
575 g_assert_no_error (error);
576 g_assert (g_socket_is_connected (client));
578 g_thread_join (data->thread);
580 g_socket_close (client, &error);
581 g_assert_no_error (error);
582 g_socket_close (data->server, &error);
583 g_assert_no_error (error);
585 g_object_unref (data->server);
586 g_object_unref (client);
587 g_object_unref (v4addr);
589 g_slice_free (IPTestData, data);
594 test_timed_wait (void)
597 GError *error = NULL;
599 GSocketAddress *addr;
603 data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE);
604 addr = g_socket_get_local_address (data->server, &error);
606 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
607 G_SOCKET_TYPE_STREAM,
608 G_SOCKET_PROTOCOL_DEFAULT,
610 g_assert_no_error (error);
612 g_socket_set_blocking (client, TRUE);
613 g_socket_set_timeout (client, 1);
615 g_socket_connect (client, addr, NULL, &error);
616 g_assert_no_error (error);
617 g_object_unref (addr);
619 start_time = g_get_monotonic_time ();
620 g_socket_condition_timed_wait (client, G_IO_IN, 100000 /* 100 ms */,
622 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
623 g_clear_error (&error);
624 poll_duration = g_get_monotonic_time () - start_time;
626 g_assert_cmpint (poll_duration, >=, 98000);
627 g_assert_cmpint (poll_duration, <, 112000);
629 g_socket_close (client, &error);
630 g_assert_no_error (error);
632 g_thread_join (data->thread);
634 g_socket_close (data->server, &error);
635 g_assert_no_error (error);
637 g_object_unref (data->server);
638 g_object_unref (client);
640 g_slice_free (IPTestData, data);
646 struct sockaddr_in6 sin6, gsin6;
647 GSocketAddress *saddr;
648 GInetSocketAddress *isaddr;
650 GError *error = NULL;
652 memset (&sin6, 0, sizeof (sin6));
653 sin6.sin6_family = AF_INET6;
654 sin6.sin6_addr = in6addr_loopback;
655 sin6.sin6_port = g_htons (42);
656 sin6.sin6_scope_id = 17;
657 sin6.sin6_flowinfo = 1729;
659 saddr = g_socket_address_new_from_native (&sin6, sizeof (sin6));
660 g_assert (G_IS_INET_SOCKET_ADDRESS (saddr));
662 isaddr = G_INET_SOCKET_ADDRESS (saddr);
663 iaddr = g_inet_socket_address_get_address (isaddr);
664 g_assert_cmpint (g_inet_address_get_family (iaddr), ==, G_SOCKET_FAMILY_IPV6);
665 g_assert (g_inet_address_get_is_loopback (iaddr));
667 g_assert_cmpint (g_inet_socket_address_get_port (isaddr), ==, 42);
668 g_assert_cmpint (g_inet_socket_address_get_scope_id (isaddr), ==, 17);
669 g_assert_cmpint (g_inet_socket_address_get_flowinfo (isaddr), ==, 1729);
671 g_socket_address_to_native (saddr, &gsin6, sizeof (gsin6), &error);
672 g_assert_no_error (error);
674 g_assert (memcmp (&sin6.sin6_addr, &gsin6.sin6_addr, sizeof (struct in6_addr)) == 0);
675 g_assert_cmpint (sin6.sin6_port, ==, gsin6.sin6_port);
676 g_assert_cmpint (sin6.sin6_scope_id, ==, gsin6.sin6_scope_id);
677 g_assert_cmpint (sin6.sin6_flowinfo, ==, gsin6.sin6_flowinfo);
679 g_object_unref (saddr);
684 test_unix_from_fd (void)
690 fd = socket (AF_UNIX, SOCK_STREAM, 0);
691 g_assert_cmpint (fd, !=, -1);
694 s = g_socket_new_from_fd (fd, &error);
695 g_assert_no_error (error);
696 g_assert_cmpint (g_socket_get_family (s), ==, G_SOCKET_FAMILY_UNIX);
697 g_assert_cmpint (g_socket_get_socket_type (s), ==, G_SOCKET_TYPE_STREAM);
698 g_assert_cmpint (g_socket_get_protocol (s), ==, G_SOCKET_PROTOCOL_DEFAULT);
703 test_unix_connection (void)
708 GSocketConnection *c;
710 fd = socket (AF_UNIX, SOCK_STREAM, 0);
711 g_assert_cmpint (fd, !=, -1);
714 s = g_socket_new_from_fd (fd, &error);
715 g_assert_no_error (error);
716 c = g_socket_connection_factory_create_connection (s);
717 g_assert (G_IS_UNIX_CONNECTION (c));
722 static GSocketConnection *
723 create_connection_for_fd (int fd)
727 GSocketConnection *connection;
729 socket = g_socket_new_from_fd (fd, &err);
730 g_assert_no_error (err);
731 g_assert (G_IS_SOCKET (socket));
732 connection = g_socket_connection_factory_create_connection (socket);
733 g_assert (G_IS_UNIX_CONNECTION (connection));
734 g_object_unref (socket);
738 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
741 test_unix_connection_ancillary_data (void)
745 gint status, fd, len;
750 g_assert_cmpint (status, ==, 0);
752 status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
753 g_assert_cmpint (status, ==, 0);
756 g_assert_cmpint (pid, >=, 0);
758 /* Child: close its copy of the write end of the pipe, receive it
759 * again from the parent over the socket, and write some text to it.
761 * Parent: send the write end of the pipe (still open for the
762 * parent) over the socket, close it, and read some text from the
763 * read end of the pipe.
767 GSocketConnection *connection;
770 connection = create_connection_for_fd (sv[0]);
772 status = close (pv[1]);
773 g_assert_cmpint (status, ==, 0);
776 fd = g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection), NULL,
778 g_assert_no_error (err);
779 g_assert_cmpint (fd, >, -1);
780 g_object_unref (connection);
783 len = write (fd, TEST_DATA, sizeof (TEST_DATA));
784 while (len == -1 && errno == EINTR);
785 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
790 GSocketConnection *connection;
793 connection = create_connection_for_fd (sv[1]);
796 g_unix_connection_send_fd (G_UNIX_CONNECTION (connection), pv[1], NULL,
798 g_assert_no_error (err);
799 g_object_unref (connection);
801 status = close (pv[1]);
802 g_assert_cmpint (status, ==, 0);
804 memset (buffer, 0xff, sizeof buffer);
806 len = read (pv[0], buffer, sizeof buffer);
807 while (len == -1 && errno == EINTR);
809 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
810 g_assert_cmpstr (buffer, ==, TEST_DATA);
812 waitpid (pid, &status, 0);
813 g_assert (WIFEXITED (status));
814 g_assert_cmpint (WEXITSTATUS (status), ==, 0);
817 /* TODO: add test for g_unix_connection_send_credentials() and
818 * g_unix_connection_receive_credentials().
821 #endif /* G_OS_UNIX */
824 test_reuse_tcp (void)
826 GSocket *sock1, *sock2;
827 GError *error = NULL;
829 GSocketAddress *addr;
831 sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
832 G_SOCKET_TYPE_STREAM,
833 G_SOCKET_PROTOCOL_DEFAULT,
835 g_assert_no_error (error);
837 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
838 addr = g_inet_socket_address_new (iaddr, 0);
839 g_object_unref (iaddr);
840 g_socket_bind (sock1, addr, TRUE, &error);
841 g_object_unref (addr);
842 g_assert_no_error (error);
844 g_socket_listen (sock1, &error);
845 g_assert_no_error (error);
847 sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
848 G_SOCKET_TYPE_STREAM,
849 G_SOCKET_PROTOCOL_DEFAULT,
851 g_assert_no_error (error);
853 addr = g_socket_get_local_address (sock1, &error);
854 g_assert_no_error (error);
855 g_socket_bind (sock2, addr, TRUE, &error);
856 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_ADDRESS_IN_USE);
857 g_object_unref (addr);
859 g_object_unref (sock1);
860 g_object_unref (sock2);
864 test_reuse_udp (void)
866 GSocket *sock1, *sock2;
867 GError *error = NULL;
869 GSocketAddress *addr;
871 sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
872 G_SOCKET_TYPE_DATAGRAM,
873 G_SOCKET_PROTOCOL_DEFAULT,
875 g_assert_no_error (error);
877 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
878 addr = g_inet_socket_address_new (iaddr, 0);
879 g_object_unref (iaddr);
880 g_socket_bind (sock1, addr, TRUE, &error);
881 g_object_unref (addr);
882 g_assert_no_error (error);
884 sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
885 G_SOCKET_TYPE_DATAGRAM,
886 G_SOCKET_PROTOCOL_DEFAULT,
888 g_assert_no_error (error);
890 addr = g_socket_get_local_address (sock1, &error);
891 g_assert_no_error (error);
892 g_socket_bind (sock2, addr, TRUE, &error);
893 g_object_unref (addr);
894 g_assert_no_error (error);
896 g_object_unref (sock1);
897 g_object_unref (sock2);
901 test_datagram_get_available (void)
904 GSocket *server, *client;
906 GSocketAddress *saddr;
907 gchar data[] = "0123456789abcdef";
911 server = g_socket_new (G_SOCKET_FAMILY_IPV4,
912 G_SOCKET_TYPE_DATAGRAM,
913 G_SOCKET_PROTOCOL_DEFAULT,
915 g_assert_no_error (err);
916 g_assert (G_IS_SOCKET (server));
918 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
919 G_SOCKET_TYPE_DATAGRAM,
920 G_SOCKET_PROTOCOL_DEFAULT,
922 g_assert_no_error (err);
923 g_assert (G_IS_SOCKET (client));
925 addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
926 saddr = g_inet_socket_address_new (addr, 0);
928 g_socket_bind (server, saddr, TRUE, &err);
929 g_assert_no_error (err);
930 g_object_unref (saddr);
931 g_object_unref (addr);
933 saddr = g_socket_get_local_address (server, &err);
934 g_assert_no_error (err);
936 g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
937 g_assert_no_error (err);
939 while (!g_socket_condition_wait (server, G_IO_IN, NULL, NULL))
941 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
943 g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
944 g_assert_no_error (err);
946 /* g_socket_condition_wait() won't help here since the socket is
947 * definitely already readable. So there's a race condition here, but
948 * at least the failure mode is passes-when-it-shouldn't, not
949 * fails-when-it-shouldn't.
952 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
954 g_assert_cmpint (sizeof (buf), >=, 2 * sizeof (data));
955 nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
956 g_assert_cmpint (nread, ==, sizeof (data));
957 g_assert_no_error (err);
959 g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
961 nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
962 g_assert_cmpint (nread, ==, sizeof (data));
963 g_assert_no_error (err);
965 g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
967 g_socket_close (server, &err);
968 g_assert_no_error (err);
970 g_object_unref (saddr);
971 g_object_unref (server);
972 g_object_unref (client);
979 g_test_init (&argc, &argv, NULL);
981 g_test_add_func ("/socket/ipv4_sync", test_ipv4_sync);
982 g_test_add_func ("/socket/ipv4_async", test_ipv4_async);
983 g_test_add_func ("/socket/ipv6_sync", test_ipv6_sync);
984 g_test_add_func ("/socket/ipv6_async", test_ipv6_async);
985 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
986 g_test_add_func ("/socket/ipv6_v4mapped", test_ipv6_v4mapped);
988 g_test_add_func ("/socket/close_graceful", test_close_graceful);
989 g_test_add_func ("/socket/timed_wait", test_timed_wait);
990 g_test_add_func ("/socket/address", test_sockaddr);
992 g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
993 g_test_add_func ("/socket/unix-connection", test_unix_connection);
994 g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data);
996 g_test_add_func ("/socket/reuse/tcp", test_reuse_tcp);
997 g_test_add_func ("/socket/reuse/udp", test_reuse_udp);
998 g_test_add_func ("/socket/datagram_get_available", test_datagram_get_available);
1000 return g_test_run();