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)
105 fd = g_socket_get_fd (server);
107 setsockopt (fd, IPPROTO_IPV6, IPV6_V6ONLY, &v6_only, sizeof (v6_only));
108 if (! g_socket_speaks_ipv4 (data->server))
110 g_object_unref (data->server);
111 g_slice_free (IPTestData, data);
118 iaddr = g_inet_address_new_any (family);
120 iaddr = g_inet_address_new_loopback (family);
121 addr = g_inet_socket_address_new (iaddr, 0);
122 g_object_unref (iaddr);
124 g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), ==, 0);
125 g_socket_bind (server, addr, TRUE, &error);
126 g_assert_no_error (error);
127 g_object_unref (addr);
129 addr = g_socket_get_local_address (server, &error);
130 g_assert_no_error (error);
131 g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), !=, 0);
132 g_object_unref (addr);
134 g_socket_listen (server, &error);
135 g_assert_no_error (error);
137 data->thread = g_thread_new ("server", server_thread, data);
142 static const gchar *testbuf = "0123456789abcdef";
145 test_ip_async_read_ready (GSocket *client,
149 IPTestData *data = user_data;
150 GError *error = NULL;
154 g_assert_cmpint (cond, ==, G_IO_IN);
156 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
157 g_assert_no_error (error);
158 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
160 g_assert_cmpstr (testbuf, ==, buf);
162 g_main_loop_quit (data->loop);
168 test_ip_async_write_ready (GSocket *client,
172 IPTestData *data = user_data;
173 GError *error = NULL;
177 g_assert_cmpint (cond, ==, G_IO_OUT);
179 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
180 g_assert_no_error (error);
181 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
183 source = g_socket_create_source (client, G_IO_IN, NULL);
184 g_source_set_callback (source, (GSourceFunc)test_ip_async_read_ready,
186 g_source_attach (source, NULL);
187 g_source_unref (source);
193 test_ip_async_timed_out (GSocket *client,
197 IPTestData *data = user_data;
198 GError *error = NULL;
203 if (data->family == G_SOCKET_FAMILY_IPV4)
205 g_assert_cmpint (cond, ==, G_IO_IN);
206 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
207 g_assert_cmpint (len, ==, -1);
208 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
209 g_clear_error (&error);
212 source = g_socket_create_source (client, G_IO_OUT, NULL);
213 g_source_set_callback (source, (GSourceFunc)test_ip_async_write_ready,
215 g_source_attach (source, NULL);
216 g_source_unref (source);
222 test_ip_async_connected (GSocket *client,
226 IPTestData *data = user_data;
227 GError *error = NULL;
232 g_socket_check_connect_result (client, &error);
233 g_assert_no_error (error);
234 /* We do this after the check_connect_result, since that will give a
235 * more useful assertion in case of error.
237 g_assert_cmpint (cond, ==, G_IO_OUT);
239 g_assert (g_socket_is_connected (client));
241 /* This adds 1 second to "make check", so let's just only do it once. */
242 if (data->family == G_SOCKET_FAMILY_IPV4)
244 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
245 g_assert_cmpint (len, ==, -1);
246 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
247 g_clear_error (&error);
249 source = g_socket_create_source (client, G_IO_IN, NULL);
250 g_source_set_callback (source, (GSourceFunc)test_ip_async_timed_out,
252 g_source_attach (source, NULL);
253 g_source_unref (source);
256 test_ip_async_timed_out (client, 0, data);
262 idle_test_ip_async_connected (gpointer user_data)
264 IPTestData *data = user_data;
266 return test_ip_async_connected (data->client, G_IO_OUT, data);
270 test_ip_async (GSocketFamily family)
273 GError *error = NULL;
275 GSocketAddress *addr;
280 data = create_server (family, echo_server_thread, FALSE);
281 addr = g_socket_get_local_address (data->server, &error);
283 client = g_socket_new (family,
284 G_SOCKET_TYPE_STREAM,
285 G_SOCKET_PROTOCOL_DEFAULT,
287 g_assert_no_error (error);
288 data->client = client;
290 g_assert_cmpint (g_socket_get_family (client), ==, family);
291 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
292 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
294 g_socket_set_blocking (client, FALSE);
295 g_socket_set_timeout (client, 1);
297 if (g_socket_connect (client, addr, NULL, &error))
299 g_assert_no_error (error);
300 g_idle_add (idle_test_ip_async_connected, data);
304 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
305 g_clear_error (&error);
306 source = g_socket_create_source (client, G_IO_OUT, NULL);
307 g_source_set_callback (source, (GSourceFunc)test_ip_async_connected,
309 g_source_attach (source, NULL);
310 g_source_unref (source);
312 g_object_unref (addr);
314 data->loop = g_main_loop_new (NULL, TRUE);
315 g_main_loop_run (data->loop);
316 g_main_loop_unref (data->loop);
318 g_socket_shutdown (client, FALSE, TRUE, &error);
319 g_assert_no_error (error);
321 g_thread_join (data->thread);
323 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
324 g_assert_no_error (error);
325 g_assert_cmpint (len, ==, 0);
327 g_socket_close (client, &error);
328 g_assert_no_error (error);
329 g_socket_close (data->server, &error);
330 g_assert_no_error (error);
332 g_object_unref (data->server);
333 g_object_unref (client);
335 g_slice_free (IPTestData, data);
339 test_ipv4_async (void)
341 test_ip_async (G_SOCKET_FAMILY_IPV4);
345 test_ipv6_async (void)
347 test_ip_async (G_SOCKET_FAMILY_IPV6);
351 test_ip_sync (GSocketFamily family)
354 GError *error = NULL;
356 GSocketAddress *addr;
360 data = create_server (family, echo_server_thread, FALSE);
361 addr = g_socket_get_local_address (data->server, &error);
363 client = g_socket_new (family,
364 G_SOCKET_TYPE_STREAM,
365 G_SOCKET_PROTOCOL_DEFAULT,
367 g_assert_no_error (error);
369 g_assert_cmpint (g_socket_get_family (client), ==, family);
370 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
371 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
373 g_socket_set_blocking (client, TRUE);
374 g_socket_set_timeout (client, 1);
376 g_socket_connect (client, addr, NULL, &error);
377 g_assert_no_error (error);
378 g_assert (g_socket_is_connected (client));
379 g_object_unref (addr);
381 /* This adds 1 second to "make check", so let's just only do it once. */
382 if (family == G_SOCKET_FAMILY_IPV4)
384 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
385 g_assert_cmpint (len, ==, -1);
386 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
387 g_clear_error (&error);
390 len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
391 g_assert_no_error (error);
392 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
394 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
395 g_assert_no_error (error);
396 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
398 g_assert_cmpstr (testbuf, ==, buf);
400 g_socket_shutdown (client, FALSE, TRUE, &error);
401 g_assert_no_error (error);
403 g_thread_join (data->thread);
405 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
406 g_assert_no_error (error);
407 g_assert_cmpint (len, ==, 0);
409 g_socket_close (client, &error);
410 g_assert_no_error (error);
411 g_socket_close (data->server, &error);
412 g_assert_no_error (error);
414 g_object_unref (data->server);
415 g_object_unref (client);
417 g_slice_free (IPTestData, data);
421 test_ipv4_sync (void)
423 test_ip_sync (G_SOCKET_FAMILY_IPV4);
427 test_ipv6_sync (void)
429 test_ip_sync (G_SOCKET_FAMILY_IPV6);
433 graceful_server_thread (gpointer user_data)
435 IPTestData *data = user_data;
437 GError *error = NULL;
440 sock = g_socket_accept (data->server, NULL, &error);
441 g_assert_no_error (error);
443 len = g_socket_send (sock, testbuf, strlen (testbuf) + 1, NULL, &error);
444 g_assert_no_error (error);
445 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
451 test_close_graceful (void)
453 GSocketFamily family = G_SOCKET_FAMILY_IPV4;
455 GError *error = NULL;
456 GSocket *client, *server;
457 GSocketAddress *addr;
461 data = create_server (family, graceful_server_thread, FALSE);
462 addr = g_socket_get_local_address (data->server, &error);
464 client = g_socket_new (family,
465 G_SOCKET_TYPE_STREAM,
466 G_SOCKET_PROTOCOL_DEFAULT,
468 g_assert_no_error (error);
470 g_assert_cmpint (g_socket_get_family (client), ==, family);
471 g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
472 g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
474 g_socket_set_blocking (client, TRUE);
475 g_socket_set_timeout (client, 1);
477 g_socket_connect (client, addr, NULL, &error);
478 g_assert_no_error (error);
479 g_assert (g_socket_is_connected (client));
480 g_object_unref (addr);
482 server = g_thread_join (data->thread);
484 /* similar to g_tcp_connection_set_graceful_disconnect(), but explicit */
485 g_socket_shutdown (server, FALSE, TRUE, &error);
486 g_assert_no_error (error);
488 /* we must timeout */
489 g_socket_condition_wait (client, G_IO_HUP, NULL, &error);
490 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
491 g_clear_error (&error);
493 /* check that the remaining data is received */
494 len = g_socket_receive (client, buf, strlen (testbuf) + 1, NULL, &error);
495 g_assert_no_error (error);
496 g_assert_cmpint (len, ==, strlen (testbuf) + 1);
498 /* and only then the connection is closed */
499 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
500 g_assert_no_error (error);
501 g_assert_cmpint (len, ==, 0);
503 g_socket_close (server, &error);
504 g_assert_no_error (error);
506 g_socket_close (client, &error);
507 g_assert_no_error (error);
509 g_object_unref (server);
510 g_object_unref (data->server);
511 g_object_unref (client);
513 g_slice_free (IPTestData, data);
516 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
518 v4mapped_server_thread (gpointer user_data)
520 IPTestData *data = user_data;
522 GError *error = NULL;
523 GSocketAddress *addr;
525 sock = g_socket_accept (data->server, NULL, &error);
526 g_assert_no_error (error);
528 g_assert_cmpint (g_socket_get_family (sock), ==, G_SOCKET_FAMILY_IPV6);
530 addr = g_socket_get_local_address (sock, &error);
531 g_assert_no_error (error);
532 g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
533 g_object_unref (addr);
535 addr = g_socket_get_remote_address (sock, &error);
536 g_assert_no_error (error);
537 g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
538 g_object_unref (addr);
540 g_socket_close (sock, &error);
541 g_assert_no_error (error);
542 g_object_unref (sock);
547 test_ipv6_v4mapped (void)
550 GError *error = NULL;
552 GSocketAddress *addr, *v4addr;
555 data = create_server (G_SOCKET_FAMILY_IPV6, v4mapped_server_thread, TRUE);
559 g_test_message ("Test not run: not supported by the OS");
563 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
564 G_SOCKET_TYPE_STREAM,
565 G_SOCKET_PROTOCOL_DEFAULT,
567 g_assert_no_error (error);
569 g_socket_set_blocking (client, TRUE);
570 g_socket_set_timeout (client, 1);
572 addr = g_socket_get_local_address (data->server, &error);
573 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
574 v4addr = g_inet_socket_address_new (iaddr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
575 g_object_unref (iaddr);
576 g_object_unref (addr);
578 g_socket_connect (client, v4addr, NULL, &error);
579 g_assert_no_error (error);
580 g_assert (g_socket_is_connected (client));
582 g_thread_join (data->thread);
584 g_socket_close (client, &error);
585 g_assert_no_error (error);
586 g_socket_close (data->server, &error);
587 g_assert_no_error (error);
589 g_object_unref (data->server);
590 g_object_unref (client);
591 g_object_unref (v4addr);
593 g_slice_free (IPTestData, data);
598 test_timed_wait (void)
601 GError *error = NULL;
603 GSocketAddress *addr;
607 data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE);
608 addr = g_socket_get_local_address (data->server, &error);
610 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
611 G_SOCKET_TYPE_STREAM,
612 G_SOCKET_PROTOCOL_DEFAULT,
614 g_assert_no_error (error);
616 g_socket_set_blocking (client, TRUE);
617 g_socket_set_timeout (client, 1);
619 g_socket_connect (client, addr, NULL, &error);
620 g_assert_no_error (error);
621 g_object_unref (addr);
623 start_time = g_get_monotonic_time ();
624 g_socket_condition_timed_wait (client, G_IO_IN, 100000 /* 100 ms */,
626 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
627 g_clear_error (&error);
628 poll_duration = g_get_monotonic_time () - start_time;
630 g_assert_cmpint (poll_duration, >=, 98000);
631 g_assert_cmpint (poll_duration, <, 112000);
633 g_socket_close (client, &error);
634 g_assert_no_error (error);
636 g_thread_join (data->thread);
638 g_socket_close (data->server, &error);
639 g_assert_no_error (error);
641 g_object_unref (data->server);
642 g_object_unref (client);
644 g_slice_free (IPTestData, data);
650 struct sockaddr_in6 sin6, gsin6;
651 GSocketAddress *saddr;
652 GInetSocketAddress *isaddr;
654 GError *error = NULL;
656 memset (&sin6, 0, sizeof (sin6));
657 sin6.sin6_family = AF_INET6;
658 sin6.sin6_addr = in6addr_loopback;
659 sin6.sin6_port = g_htons (42);
660 sin6.sin6_scope_id = 17;
661 sin6.sin6_flowinfo = 1729;
663 saddr = g_socket_address_new_from_native (&sin6, sizeof (sin6));
664 g_assert (G_IS_INET_SOCKET_ADDRESS (saddr));
666 isaddr = G_INET_SOCKET_ADDRESS (saddr);
667 iaddr = g_inet_socket_address_get_address (isaddr);
668 g_assert_cmpint (g_inet_address_get_family (iaddr), ==, G_SOCKET_FAMILY_IPV6);
669 g_assert (g_inet_address_get_is_loopback (iaddr));
671 g_assert_cmpint (g_inet_socket_address_get_port (isaddr), ==, 42);
672 g_assert_cmpint (g_inet_socket_address_get_scope_id (isaddr), ==, 17);
673 g_assert_cmpint (g_inet_socket_address_get_flowinfo (isaddr), ==, 1729);
675 g_socket_address_to_native (saddr, &gsin6, sizeof (gsin6), &error);
676 g_assert_no_error (error);
678 g_assert (memcmp (&sin6.sin6_addr, &gsin6.sin6_addr, sizeof (struct in6_addr)) == 0);
679 g_assert_cmpint (sin6.sin6_port, ==, gsin6.sin6_port);
680 g_assert_cmpint (sin6.sin6_scope_id, ==, gsin6.sin6_scope_id);
681 g_assert_cmpint (sin6.sin6_flowinfo, ==, gsin6.sin6_flowinfo);
683 g_object_unref (saddr);
688 test_unix_from_fd (void)
694 fd = socket (AF_UNIX, SOCK_STREAM, 0);
695 g_assert_cmpint (fd, !=, -1);
698 s = g_socket_new_from_fd (fd, &error);
699 g_assert_no_error (error);
700 g_assert_cmpint (g_socket_get_family (s), ==, G_SOCKET_FAMILY_UNIX);
701 g_assert_cmpint (g_socket_get_socket_type (s), ==, G_SOCKET_TYPE_STREAM);
702 g_assert_cmpint (g_socket_get_protocol (s), ==, G_SOCKET_PROTOCOL_DEFAULT);
707 test_unix_connection (void)
712 GSocketConnection *c;
714 fd = socket (AF_UNIX, SOCK_STREAM, 0);
715 g_assert_cmpint (fd, !=, -1);
718 s = g_socket_new_from_fd (fd, &error);
719 g_assert_no_error (error);
720 c = g_socket_connection_factory_create_connection (s);
721 g_assert (G_IS_UNIX_CONNECTION (c));
726 static GSocketConnection *
727 create_connection_for_fd (int fd)
731 GSocketConnection *connection;
733 socket = g_socket_new_from_fd (fd, &err);
734 g_assert_no_error (err);
735 g_assert (G_IS_SOCKET (socket));
736 connection = g_socket_connection_factory_create_connection (socket);
737 g_assert (G_IS_UNIX_CONNECTION (connection));
738 g_object_unref (socket);
742 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
745 test_unix_connection_ancillary_data (void)
749 gint status, fd, len;
754 g_assert_cmpint (status, ==, 0);
756 status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
757 g_assert_cmpint (status, ==, 0);
760 g_assert_cmpint (pid, >=, 0);
762 /* Child: close its copy of the write end of the pipe, receive it
763 * again from the parent over the socket, and write some text to it.
765 * Parent: send the write end of the pipe (still open for the
766 * parent) over the socket, close it, and read some text from the
767 * read end of the pipe.
771 GSocketConnection *connection;
774 connection = create_connection_for_fd (sv[0]);
776 status = close (pv[1]);
777 g_assert_cmpint (status, ==, 0);
780 fd = g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection), NULL,
782 g_assert_no_error (err);
783 g_assert_cmpint (fd, >, -1);
784 g_object_unref (connection);
787 len = write (fd, TEST_DATA, sizeof (TEST_DATA));
788 while (len == -1 && errno == EINTR);
789 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
794 GSocketConnection *connection;
797 connection = create_connection_for_fd (sv[1]);
800 g_unix_connection_send_fd (G_UNIX_CONNECTION (connection), pv[1], NULL,
802 g_assert_no_error (err);
803 g_object_unref (connection);
805 status = close (pv[1]);
806 g_assert_cmpint (status, ==, 0);
808 memset (buffer, 0xff, sizeof buffer);
810 len = read (pv[0], buffer, sizeof buffer);
811 while (len == -1 && errno == EINTR);
813 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
814 g_assert_cmpstr (buffer, ==, TEST_DATA);
816 waitpid (pid, &status, 0);
817 g_assert (WIFEXITED (status));
818 g_assert_cmpint (WEXITSTATUS (status), ==, 0);
821 /* TODO: add test for g_unix_connection_send_credentials() and
822 * g_unix_connection_receive_credentials().
825 #endif /* G_OS_UNIX */
828 test_reuse_tcp (void)
830 GSocket *sock1, *sock2;
831 GError *error = NULL;
833 GSocketAddress *addr;
835 sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
836 G_SOCKET_TYPE_STREAM,
837 G_SOCKET_PROTOCOL_DEFAULT,
839 g_assert_no_error (error);
841 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
842 addr = g_inet_socket_address_new (iaddr, 0);
843 g_object_unref (iaddr);
844 g_socket_bind (sock1, addr, TRUE, &error);
845 g_object_unref (addr);
846 g_assert_no_error (error);
848 g_socket_listen (sock1, &error);
849 g_assert_no_error (error);
851 sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
852 G_SOCKET_TYPE_STREAM,
853 G_SOCKET_PROTOCOL_DEFAULT,
855 g_assert_no_error (error);
857 addr = g_socket_get_local_address (sock1, &error);
858 g_assert_no_error (error);
859 g_socket_bind (sock2, addr, TRUE, &error);
860 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_ADDRESS_IN_USE);
861 g_object_unref (addr);
863 g_object_unref (sock1);
864 g_object_unref (sock2);
868 test_reuse_udp (void)
870 GSocket *sock1, *sock2;
871 GError *error = NULL;
873 GSocketAddress *addr;
875 sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
876 G_SOCKET_TYPE_DATAGRAM,
877 G_SOCKET_PROTOCOL_DEFAULT,
879 g_assert_no_error (error);
881 iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
882 addr = g_inet_socket_address_new (iaddr, 0);
883 g_object_unref (iaddr);
884 g_socket_bind (sock1, addr, TRUE, &error);
885 g_object_unref (addr);
886 g_assert_no_error (error);
888 sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
889 G_SOCKET_TYPE_DATAGRAM,
890 G_SOCKET_PROTOCOL_DEFAULT,
892 g_assert_no_error (error);
894 addr = g_socket_get_local_address (sock1, &error);
895 g_assert_no_error (error);
896 g_socket_bind (sock2, addr, TRUE, &error);
897 g_object_unref (addr);
898 g_assert_no_error (error);
900 g_object_unref (sock1);
901 g_object_unref (sock2);
908 g_test_init (&argc, &argv, NULL);
910 g_test_add_func ("/socket/ipv4_sync", test_ipv4_sync);
911 g_test_add_func ("/socket/ipv4_async", test_ipv4_async);
912 g_test_add_func ("/socket/ipv6_sync", test_ipv6_sync);
913 g_test_add_func ("/socket/ipv6_async", test_ipv6_async);
914 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
915 g_test_add_func ("/socket/ipv6_v4mapped", test_ipv6_v4mapped);
917 g_test_add_func ("/socket/close_graceful", test_close_graceful);
918 g_test_add_func ("/socket/timed_wait", test_timed_wait);
919 g_test_add_func ("/socket/address", test_sockaddr);
921 g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
922 g_test_add_func ("/socket/unix-connection", test_unix_connection);
923 g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data);
925 g_test_add_func ("/socket/reuse/tcp", test_reuse_tcp);
926 g_test_add_func ("/socket/reuse/udp", test_reuse_udp);