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
))
148 iaddr
= g_inet_address_new_any (family
);
150 iaddr
= g_inet_address_new_loopback (family
);
151 addr
= g_inet_socket_address_new (iaddr
, 0);
152 g_object_unref (iaddr
);
154 g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr
)), ==, 0);
155 if (!g_socket_bind (server
, addr
, TRUE
, error
))
157 g_object_unref (addr
);
160 g_object_unref (addr
);
162 addr
= g_socket_get_local_address (server
, error
);
165 g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr
)), !=, 0);
166 g_object_unref (addr
);
168 if (socket_type
== G_SOCKET_TYPE_STREAM
)
170 if (!g_socket_listen (server
, error
))
175 data
->cancellable
= g_cancellable_new ();
178 data
->thread
= g_thread_new ("server", server_thread
, data
);
183 g_clear_object (&data
->server
);
184 g_slice_free (IPTestData
, data
);
190 create_server (GSocketFamily family
,
191 GThreadFunc server_thread
,
195 return create_server_full (family
, G_SOCKET_TYPE_STREAM
, server_thread
, v4mapped
, error
);
198 static const gchar
*testbuf
= "0123456789abcdef";
201 test_ip_async_read_ready (GSocket
*client
,
205 IPTestData
*data
= user_data
;
206 GError
*error
= NULL
;
210 g_assert_cmpint (cond
, ==, G_IO_IN
);
212 len
= g_socket_receive (client
, buf
, sizeof (buf
), NULL
, &error
);
213 g_assert_no_error (error
);
214 g_assert_cmpint (len
, ==, strlen (testbuf
) + 1);
216 g_assert_cmpstr (testbuf
, ==, buf
);
218 g_main_loop_quit (data
->loop
);
224 test_ip_async_write_ready (GSocket
*client
,
228 IPTestData
*data
= user_data
;
229 GError
*error
= NULL
;
233 g_assert_cmpint (cond
, ==, G_IO_OUT
);
235 len
= g_socket_send (client
, testbuf
, strlen (testbuf
) + 1, NULL
, &error
);
236 g_assert_no_error (error
);
237 g_assert_cmpint (len
, ==, strlen (testbuf
) + 1);
239 source
= g_socket_create_source (client
, G_IO_IN
, NULL
);
240 g_source_set_callback (source
, (GSourceFunc
)test_ip_async_read_ready
,
242 g_source_attach (source
, NULL
);
243 g_source_unref (source
);
249 test_ip_async_timed_out (GSocket
*client
,
253 IPTestData
*data
= user_data
;
254 GError
*error
= NULL
;
259 if (data
->family
== G_SOCKET_FAMILY_IPV4
)
261 g_assert_cmpint (cond
, ==, G_IO_IN
);
262 len
= g_socket_receive (client
, buf
, sizeof (buf
), NULL
, &error
);
263 g_assert_cmpint (len
, ==, -1);
264 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_TIMED_OUT
);
265 g_clear_error (&error
);
268 source
= g_socket_create_source (client
, G_IO_OUT
, NULL
);
269 g_source_set_callback (source
, (GSourceFunc
)test_ip_async_write_ready
,
271 g_source_attach (source
, NULL
);
272 g_source_unref (source
);
278 test_ip_async_connected (GSocket
*client
,
282 IPTestData
*data
= user_data
;
283 GError
*error
= NULL
;
288 g_socket_check_connect_result (client
, &error
);
289 g_assert_no_error (error
);
290 /* We do this after the check_connect_result, since that will give a
291 * more useful assertion in case of error.
293 g_assert_cmpint (cond
, ==, G_IO_OUT
);
295 g_assert (g_socket_is_connected (client
));
297 /* This adds 1 second to "make check", so let's just only do it once. */
298 if (data
->family
== G_SOCKET_FAMILY_IPV4
)
300 len
= g_socket_receive (client
, buf
, sizeof (buf
), NULL
, &error
);
301 g_assert_cmpint (len
, ==, -1);
302 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_WOULD_BLOCK
);
303 g_clear_error (&error
);
305 source
= g_socket_create_source (client
, G_IO_IN
, NULL
);
306 g_source_set_callback (source
, (GSourceFunc
)test_ip_async_timed_out
,
308 g_source_attach (source
, NULL
);
309 g_source_unref (source
);
312 test_ip_async_timed_out (client
, 0, data
);
318 idle_test_ip_async_connected (gpointer user_data
)
320 IPTestData
*data
= user_data
;
322 return test_ip_async_connected (data
->client
, G_IO_OUT
, data
);
326 test_ip_async (GSocketFamily family
)
329 GError
*error
= NULL
;
331 GSocketAddress
*addr
;
336 data
= create_server (family
, echo_server_thread
, FALSE
, &error
);
339 gchar
*message
= g_strdup_printf ("Failed to create server: %s", error
->message
);
340 g_test_skip (message
);
342 g_clear_error (&error
);
346 addr
= g_socket_get_local_address (data
->server
, &error
);
347 g_assert_no_error (error
);
349 client
= g_socket_new (family
,
350 G_SOCKET_TYPE_STREAM
,
351 G_SOCKET_PROTOCOL_DEFAULT
,
353 g_assert_no_error (error
);
354 data
->client
= client
;
356 g_assert_cmpint (g_socket_get_family (client
), ==, family
);
357 g_assert_cmpint (g_socket_get_socket_type (client
), ==, G_SOCKET_TYPE_STREAM
);
358 g_assert_cmpint (g_socket_get_protocol (client
), ==, G_SOCKET_PROTOCOL_DEFAULT
);
360 g_socket_set_blocking (client
, FALSE
);
361 g_socket_set_timeout (client
, 1);
363 if (g_socket_connect (client
, addr
, NULL
, &error
))
365 g_assert_no_error (error
);
366 g_idle_add (idle_test_ip_async_connected
, data
);
370 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_PENDING
);
371 g_clear_error (&error
);
372 source
= g_socket_create_source (client
, G_IO_OUT
, NULL
);
373 g_source_set_callback (source
, (GSourceFunc
)test_ip_async_connected
,
375 g_source_attach (source
, NULL
);
376 g_source_unref (source
);
378 g_object_unref (addr
);
380 data
->loop
= g_main_loop_new (NULL
, TRUE
);
381 g_main_loop_run (data
->loop
);
382 g_main_loop_unref (data
->loop
);
384 g_socket_shutdown (client
, FALSE
, TRUE
, &error
);
385 g_assert_no_error (error
);
387 g_thread_join (data
->thread
);
389 if (family
== G_SOCKET_FAMILY_IPV4
)
391 /* Test that reading on a remote-closed socket gets back 0 bytes. */
392 len
= g_socket_receive_with_blocking (client
, buf
, sizeof (buf
),
394 g_assert_no_error (error
);
395 g_assert_cmpint (len
, ==, 0);
399 /* Test that writing to a remote-closed socket gets back CONNECTION_CLOSED. */
400 len
= g_socket_send_with_blocking (client
, testbuf
, strlen (testbuf
) + 1,
402 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_CONNECTION_CLOSED
);
403 g_assert_cmpint (len
, ==, -1);
404 g_clear_error (&error
);
407 g_socket_close (client
, &error
);
408 g_assert_no_error (error
);
409 g_socket_close (data
->server
, &error
);
410 g_assert_no_error (error
);
412 g_object_unref (data
->server
);
413 g_object_unref (client
);
415 g_slice_free (IPTestData
, data
);
419 test_ipv4_async (void)
421 test_ip_async (G_SOCKET_FAMILY_IPV4
);
425 test_ipv6_async (void)
429 g_test_skip ("No support for IPv6");
433 test_ip_async (G_SOCKET_FAMILY_IPV6
);
436 static const gchar testbuf2
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
439 test_ip_sync (GSocketFamily family
)
442 GError
*error
= NULL
;
444 GSocketAddress
*addr
;
448 data
= create_server (family
, echo_server_thread
, FALSE
, &error
);
451 gchar
*message
= g_strdup_printf ("Failed to create server: %s", error
->message
);
452 g_test_skip (message
);
454 g_clear_error (&error
);
458 addr
= g_socket_get_local_address (data
->server
, &error
);
459 g_assert_no_error (error
);
461 client
= g_socket_new (family
,
462 G_SOCKET_TYPE_STREAM
,
463 G_SOCKET_PROTOCOL_DEFAULT
,
465 g_assert_no_error (error
);
467 g_assert_cmpint (g_socket_get_family (client
), ==, family
);
468 g_assert_cmpint (g_socket_get_socket_type (client
), ==, G_SOCKET_TYPE_STREAM
);
469 g_assert_cmpint (g_socket_get_protocol (client
), ==, G_SOCKET_PROTOCOL_DEFAULT
);
471 g_socket_set_blocking (client
, TRUE
);
472 g_socket_set_timeout (client
, 1);
474 g_socket_connect (client
, addr
, NULL
, &error
);
475 g_assert_no_error (error
);
476 g_assert (g_socket_is_connected (client
));
477 g_object_unref (addr
);
479 /* This adds 1 second to "make check", so let's just only do it once. */
480 if (family
== G_SOCKET_FAMILY_IPV4
)
482 len
= g_socket_receive (client
, buf
, sizeof (buf
), NULL
, &error
);
483 g_assert_cmpint (len
, ==, -1);
484 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_TIMED_OUT
);
485 g_clear_error (&error
);
488 len
= g_socket_send (client
, testbuf
, strlen (testbuf
) + 1, NULL
, &error
);
489 g_assert_no_error (error
);
490 g_assert_cmpint (len
, ==, strlen (testbuf
) + 1);
492 len
= g_socket_receive (client
, buf
, sizeof (buf
), NULL
, &error
);
493 g_assert_no_error (error
);
494 g_assert_cmpint (len
, ==, strlen (testbuf
) + 1);
496 g_assert_cmpstr (testbuf
, ==, buf
);
499 GOutputVector v
[7] = { { NULL
, }, };
501 v
[0].buffer
= testbuf2
+ 0;
503 v
[1].buffer
= testbuf2
+ 3;
505 v
[2].buffer
= testbuf2
+ 3 + 5;
507 v
[3].buffer
= testbuf2
+ 3 + 5;
509 v
[4].buffer
= testbuf2
+ 3 + 5 + 6;
511 v
[5].buffer
= testbuf2
+ 3 + 5 + 6 + 2;
513 v
[6].buffer
= testbuf2
+ 3 + 5 + 6 + 2 + 1;
514 v
[6].size
= strlen (testbuf2
) - (3 + 5 + 6 + 2 + 1);
516 len
= g_socket_send_message (client
, NULL
, v
, G_N_ELEMENTS (v
), NULL
, 0, 0, NULL
, &error
);
517 g_assert_no_error (error
);
518 g_assert_cmpint (len
, ==, strlen (testbuf2
));
520 memset (buf
, 0, sizeof (buf
));
521 len
= g_socket_receive (client
, buf
, sizeof (buf
), NULL
, &error
);
522 g_assert_no_error (error
);
523 g_assert_cmpint (len
, ==, strlen (testbuf2
));
524 g_assert_cmpstr (testbuf2
, ==, buf
);
527 g_socket_shutdown (client
, FALSE
, TRUE
, &error
);
528 g_assert_no_error (error
);
530 g_thread_join (data
->thread
);
532 if (family
== G_SOCKET_FAMILY_IPV4
)
534 /* Test that reading on a remote-closed socket gets back 0 bytes. */
535 len
= g_socket_receive (client
, buf
, sizeof (buf
), NULL
, &error
);
536 g_assert_no_error (error
);
537 g_assert_cmpint (len
, ==, 0);
541 /* Test that writing to a remote-closed socket gets back CONNECTION_CLOSED. */
542 len
= g_socket_send (client
, testbuf
, strlen (testbuf
) + 1, NULL
, &error
);
543 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_CONNECTION_CLOSED
);
544 g_assert_cmpint (len
, ==, -1);
545 g_clear_error (&error
);
548 g_socket_close (client
, &error
);
549 g_assert_no_error (error
);
550 g_socket_close (data
->server
, &error
);
551 g_assert_no_error (error
);
553 g_object_unref (data
->server
);
554 g_object_unref (client
);
556 g_slice_free (IPTestData
, data
);
560 test_ipv4_sync (void)
562 test_ip_sync (G_SOCKET_FAMILY_IPV4
);
566 test_ipv6_sync (void)
570 g_test_skip ("No support for IPv6");
574 test_ip_sync (G_SOCKET_FAMILY_IPV6
);
578 test_ip_sync_dgram (GSocketFamily family
)
581 GError
*error
= NULL
;
583 GSocketAddress
*dest_addr
;
587 data
= create_server_full (family
, G_SOCKET_TYPE_DATAGRAM
,
588 echo_server_dgram_thread
, FALSE
, &error
);
591 gchar
*message
= g_strdup_printf ("Failed to create server: %s", error
->message
);
592 g_test_skip (message
);
594 g_clear_error (&error
);
598 dest_addr
= g_socket_get_local_address (data
->server
, &error
);
600 client
= g_socket_new (family
,
601 G_SOCKET_TYPE_DATAGRAM
,
602 G_SOCKET_PROTOCOL_DEFAULT
,
604 g_assert_no_error (error
);
606 g_assert_cmpint (g_socket_get_family (client
), ==, family
);
607 g_assert_cmpint (g_socket_get_socket_type (client
), ==, G_SOCKET_TYPE_DATAGRAM
);
608 g_assert_cmpint (g_socket_get_protocol (client
), ==, G_SOCKET_PROTOCOL_DEFAULT
);
610 g_socket_set_blocking (client
, TRUE
);
611 g_socket_set_timeout (client
, 1);
613 len
= g_socket_send_to (client
, dest_addr
, testbuf
, strlen (testbuf
) + 1, NULL
, &error
);
614 g_assert_no_error (error
);
615 g_assert_cmpint (len
, ==, strlen (testbuf
) + 1);
617 len
= g_socket_receive_from (client
, NULL
, buf
, sizeof (buf
), NULL
, &error
);
618 g_assert_no_error (error
);
619 g_assert_cmpint (len
, ==, strlen (testbuf
) + 1);
621 g_assert_cmpstr (testbuf
, ==, buf
);
624 GOutputMessage m
[3] = { { NULL
, }, };
625 GInputMessage im
[3] = { { NULL
, }, };
626 GOutputVector v
[7] = { { NULL
, }, };
627 GInputVector iv
[7] = { { NULL
, }, };
629 v
[0].buffer
= testbuf2
+ 0;
631 v
[1].buffer
= testbuf2
+ 3;
633 v
[2].buffer
= testbuf2
+ 3 + 5;
635 v
[3].buffer
= testbuf2
+ 3 + 5;
637 v
[4].buffer
= testbuf2
+ 3 + 5 + 6;
639 v
[5].buffer
= testbuf2
+ 3 + 5 + 6 + 2;
641 v
[6].buffer
= testbuf2
+ 3 + 5 + 6 + 2 + 1;
642 v
[6].size
= strlen (testbuf2
) - (3 + 5 + 6 + 2 + 1);
644 iv
[0].buffer
= buf
+ 0;
646 iv
[1].buffer
= buf
+ 3;
648 iv
[2].buffer
= buf
+ 3 + 5;
650 iv
[3].buffer
= buf
+ 3 + 5;
652 iv
[4].buffer
= buf
+ 3 + 5 + 6;
654 iv
[5].buffer
= buf
+ 3 + 5 + 6 + 2;
656 iv
[6].buffer
= buf
+ 3 + 5 + 6 + 2 + 1;
657 iv
[6].size
= sizeof (buf
) - (3 + 5 + 6 + 2 + 1);
659 len
= g_socket_send_message (client
, dest_addr
, v
, G_N_ELEMENTS (v
), NULL
, 0, 0, NULL
, &error
);
660 g_assert_no_error (error
);
661 g_assert_cmpint (len
, ==, strlen (testbuf2
));
663 memset (buf
, 0, sizeof (buf
));
664 len
= g_socket_receive_from (client
, NULL
, buf
, sizeof (buf
), NULL
, &error
);
665 g_assert_no_error (error
);
666 g_assert_cmpint (len
, ==, strlen (testbuf2
));
667 g_assert_cmpstr (testbuf2
, ==, buf
);
669 m
[0].vectors
= &v
[0];
670 m
[0].num_vectors
= 1;
671 m
[0].address
= dest_addr
;
672 m
[1].vectors
= &v
[0];
673 m
[1].num_vectors
= 6;
674 m
[1].address
= dest_addr
;
675 m
[2].vectors
= &v
[6];
676 m
[2].num_vectors
= 1;
677 m
[2].address
= dest_addr
;
679 len
= g_socket_send_messages (client
, m
, G_N_ELEMENTS (m
), 0, NULL
, &error
);
680 g_assert_no_error (error
);
681 g_assert_cmpint (len
, ==, G_N_ELEMENTS (m
));
682 g_assert_cmpint (m
[0].bytes_sent
, ==, 3);
683 g_assert_cmpint (m
[1].bytes_sent
, ==, 17);
684 g_assert_cmpint (m
[2].bytes_sent
, ==, v
[6].size
);
686 memset (buf
, 0, sizeof (buf
));
687 len
= g_socket_receive_from (client
, NULL
, buf
, sizeof (buf
), NULL
, &error
);
688 g_assert_no_error (error
);
689 g_assert_cmpint (len
, ==, 3);
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 /* v[0].size + v[1].size + v[2].size + v[3].size + v[4].size + v[5].size */
695 g_assert_cmpint (len
, ==, 17);
696 g_assert (memcmp (testbuf2
, buf
, 17) == 0);
698 memset (buf
, 0, sizeof (buf
));
699 len
= g_socket_receive_from (client
, NULL
, buf
, sizeof (buf
), NULL
, &error
);
700 g_assert_no_error (error
);
701 g_assert_cmpint (len
, ==, v
[6].size
);
702 g_assert_cmpstr (buf
, ==, v
[6].buffer
);
704 /* reset since we're re-using the message structs */
709 /* now try receiving multiple messages */
710 len
= g_socket_send_messages (client
, m
, G_N_ELEMENTS (m
), 0, NULL
, &error
);
711 g_assert_no_error (error
);
712 g_assert_cmpint (len
, ==, G_N_ELEMENTS (m
));
713 g_assert_cmpint (m
[0].bytes_sent
, ==, 3);
714 g_assert_cmpint (m
[1].bytes_sent
, ==, 17);
715 g_assert_cmpint (m
[2].bytes_sent
, ==, v
[6].size
);
717 im
[0].vectors
= &iv
[0];
718 im
[0].num_vectors
= 1;
719 im
[1].vectors
= &iv
[0];
720 im
[1].num_vectors
= 6;
721 im
[2].vectors
= &iv
[6];
722 im
[2].num_vectors
= 1;
724 memset (buf
, 0, sizeof (buf
));
725 len
= g_socket_receive_messages (client
, im
, G_N_ELEMENTS (im
), 0,
727 g_assert_no_error (error
);
728 g_assert_cmpint (len
, ==, G_N_ELEMENTS (im
));
730 g_assert_cmpuint (im
[0].bytes_received
, ==, 3);
731 /* v[0].size + v[1].size + v[2].size + v[3].size + v[4].size + v[5].size */
732 g_assert_cmpuint (im
[1].bytes_received
, ==, 17);
733 g_assert_cmpuint (im
[2].bytes_received
, ==, v
[6].size
);
735 /* reset since we're re-using the message structs */
740 /* now try to generate an early return by omitting the destination address on [1] */
742 len
= g_socket_send_messages (client
, m
, G_N_ELEMENTS (m
), 0, NULL
, &error
);
743 g_assert_no_error (error
);
744 g_assert_cmpint (len
, ==, 1);
746 g_assert_cmpint (m
[0].bytes_sent
, ==, 3);
747 g_assert_cmpint (m
[1].bytes_sent
, ==, 0);
748 g_assert_cmpint (m
[2].bytes_sent
, ==, 0);
750 /* reset since we're re-using the message structs */
755 /* now try to generate an error by omitting all destination addresses */
759 len
= g_socket_send_messages (client
, m
, G_N_ELEMENTS (m
), 0, NULL
, &error
);
760 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_FAILED
);
761 g_clear_error (&error
);
762 g_assert_cmpint (len
, ==, -1);
764 g_assert_cmpint (m
[0].bytes_sent
, ==, 0);
765 g_assert_cmpint (m
[1].bytes_sent
, ==, 0);
766 g_assert_cmpint (m
[2].bytes_sent
, ==, 0);
768 len
= g_socket_receive_from (client
, NULL
, buf
, sizeof (buf
), NULL
, &error
);
769 g_assert_cmpint (len
, ==, 3);
772 g_cancellable_cancel (data
->cancellable
);
774 g_thread_join (data
->thread
);
776 g_socket_close (client
, &error
);
777 g_assert_no_error (error
);
778 g_socket_close (data
->server
, &error
);
779 g_assert_no_error (error
);
781 g_object_unref (data
->server
);
782 g_object_unref (data
->cancellable
);
783 g_object_unref (client
);
784 g_object_unref (dest_addr
);
786 g_slice_free (IPTestData
, data
);
790 test_ipv4_sync_dgram (void)
792 test_ip_sync_dgram (G_SOCKET_FAMILY_IPV4
);
796 test_ipv6_sync_dgram (void)
800 g_test_skip ("No support for IPv6");
804 test_ip_sync_dgram (G_SOCKET_FAMILY_IPV6
);
808 cancellable_thread_cb (gpointer data
)
810 GCancellable
*cancellable
= data
;
812 g_usleep (0.1 * G_USEC_PER_SEC
);
813 g_cancellable_cancel (cancellable
);
814 g_object_unref (cancellable
);
820 test_ip_sync_dgram_timeouts (GSocketFamily family
)
822 GError
*error
= NULL
;
823 GSocket
*client
= NULL
;
824 GCancellable
*cancellable
= NULL
;
825 GThread
*cancellable_thread
= NULL
;
828 client
= g_socket_new (family
,
829 G_SOCKET_TYPE_DATAGRAM
,
830 G_SOCKET_PROTOCOL_DEFAULT
,
832 g_assert_no_error (error
);
834 g_assert_cmpint (g_socket_get_family (client
), ==, family
);
835 g_assert_cmpint (g_socket_get_socket_type (client
), ==, G_SOCKET_TYPE_DATAGRAM
);
836 g_assert_cmpint (g_socket_get_protocol (client
), ==, G_SOCKET_PROTOCOL_DEFAULT
);
838 /* No overall timeout: test the per-operation timeouts instead. */
839 g_socket_set_timeout (client
, 0);
841 cancellable
= g_cancellable_new ();
843 /* Check for timeouts when no server is running. */
846 GInputMessage im
= { NULL
, };
847 GInputVector iv
= { NULL
, };
851 iv
.size
= sizeof (buf
);
856 memset (buf
, 0, sizeof (buf
));
858 /* Try a non-blocking read. */
859 g_socket_set_blocking (client
, FALSE
);
860 len
= g_socket_receive_messages (client
, &im
, 1, 0 /* flags */,
862 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_WOULD_BLOCK
);
863 g_assert_cmpint (len
, ==, -1);
864 g_clear_error (&error
);
866 /* Try a timeout read. Can’t really validate the time taken more than
867 * checking it’s positive. */
868 g_socket_set_timeout (client
, 1);
869 g_socket_set_blocking (client
, TRUE
);
870 start_time
= g_get_monotonic_time ();
871 len
= g_socket_receive_messages (client
, &im
, 1, 0 /* flags */,
873 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_TIMED_OUT
);
874 g_assert_cmpint (len
, ==, -1);
875 g_assert_cmpint (g_get_monotonic_time () - start_time
, >, 0);
876 g_clear_error (&error
);
878 /* Try a blocking read, cancelled from another thread. */
879 g_socket_set_timeout (client
, 0);
880 cancellable_thread
= g_thread_new ("cancellable",
881 cancellable_thread_cb
,
882 g_object_ref (cancellable
));
884 start_time
= g_get_monotonic_time ();
885 len
= g_socket_receive_messages (client
, &im
, 1, 0 /* flags */,
886 cancellable
, &error
);
887 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_CANCELLED
);
888 g_assert_cmpint (len
, ==, -1);
889 g_assert_cmpint (g_get_monotonic_time () - start_time
, >, 0);
890 g_clear_error (&error
);
892 g_thread_join (cancellable_thread
);
895 g_socket_close (client
, &error
);
896 g_assert_no_error (error
);
898 g_object_unref (client
);
899 g_object_unref (cancellable
);
903 test_ipv4_sync_dgram_timeouts (void)
905 test_ip_sync_dgram_timeouts (G_SOCKET_FAMILY_IPV4
);
909 test_ipv6_sync_dgram_timeouts (void)
913 g_test_skip ("No support for IPv6");
917 test_ip_sync_dgram_timeouts (G_SOCKET_FAMILY_IPV6
);
921 graceful_server_thread (gpointer user_data
)
923 IPTestData
*data
= user_data
;
925 GError
*error
= NULL
;
928 sock
= g_socket_accept (data
->server
, NULL
, &error
);
929 g_assert_no_error (error
);
931 len
= g_socket_send (sock
, testbuf
, strlen (testbuf
) + 1, NULL
, &error
);
932 g_assert_no_error (error
);
933 g_assert_cmpint (len
, ==, strlen (testbuf
) + 1);
939 test_close_graceful (void)
941 GSocketFamily family
= G_SOCKET_FAMILY_IPV4
;
943 GError
*error
= NULL
;
944 GSocket
*client
, *server
;
945 GSocketAddress
*addr
;
949 data
= create_server (family
, graceful_server_thread
, FALSE
, &error
);
952 gchar
*message
= g_strdup_printf ("Failed to create server: %s", error
->message
);
953 g_test_skip (message
);
955 g_clear_error (&error
);
959 addr
= g_socket_get_local_address (data
->server
, &error
);
960 g_assert_no_error (error
);
962 client
= g_socket_new (family
,
963 G_SOCKET_TYPE_STREAM
,
964 G_SOCKET_PROTOCOL_DEFAULT
,
966 g_assert_no_error (error
);
968 g_assert_cmpint (g_socket_get_family (client
), ==, family
);
969 g_assert_cmpint (g_socket_get_socket_type (client
), ==, G_SOCKET_TYPE_STREAM
);
970 g_assert_cmpint (g_socket_get_protocol (client
), ==, G_SOCKET_PROTOCOL_DEFAULT
);
972 g_socket_set_blocking (client
, TRUE
);
973 g_socket_set_timeout (client
, 1);
975 g_socket_connect (client
, addr
, NULL
, &error
);
976 g_assert_no_error (error
);
977 g_assert (g_socket_is_connected (client
));
978 g_object_unref (addr
);
980 server
= g_thread_join (data
->thread
);
982 /* similar to g_tcp_connection_set_graceful_disconnect(), but explicit */
983 g_socket_shutdown (server
, FALSE
, TRUE
, &error
);
984 g_assert_no_error (error
);
986 /* we must timeout */
987 g_socket_condition_wait (client
, G_IO_HUP
, NULL
, &error
);
988 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_TIMED_OUT
);
989 g_clear_error (&error
);
991 /* check that the remaining data is received */
992 len
= g_socket_receive (client
, buf
, strlen (testbuf
) + 1, NULL
, &error
);
993 g_assert_no_error (error
);
994 g_assert_cmpint (len
, ==, strlen (testbuf
) + 1);
996 /* and only then the connection is closed */
997 len
= g_socket_receive (client
, buf
, sizeof (buf
), NULL
, &error
);
998 g_assert_no_error (error
);
999 g_assert_cmpint (len
, ==, 0);
1001 g_socket_close (server
, &error
);
1002 g_assert_no_error (error
);
1004 g_socket_close (client
, &error
);
1005 g_assert_no_error (error
);
1007 g_object_unref (server
);
1008 g_object_unref (data
->server
);
1009 g_object_unref (client
);
1011 g_slice_free (IPTestData
, data
);
1014 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
1016 v4mapped_server_thread (gpointer user_data
)
1018 IPTestData
*data
= user_data
;
1020 GError
*error
= NULL
;
1021 GSocketAddress
*addr
;
1023 sock
= g_socket_accept (data
->server
, NULL
, &error
);
1024 g_assert_no_error (error
);
1026 g_assert_cmpint (g_socket_get_family (sock
), ==, G_SOCKET_FAMILY_IPV6
);
1028 addr
= g_socket_get_local_address (sock
, &error
);
1029 g_assert_no_error (error
);
1030 g_assert_cmpint (g_socket_address_get_family (addr
), ==, G_SOCKET_FAMILY_IPV4
);
1031 g_object_unref (addr
);
1033 addr
= g_socket_get_remote_address (sock
, &error
);
1034 g_assert_no_error (error
);
1035 g_assert_cmpint (g_socket_address_get_family (addr
), ==, G_SOCKET_FAMILY_IPV4
);
1036 g_object_unref (addr
);
1038 g_socket_close (sock
, &error
);
1039 g_assert_no_error (error
);
1040 g_object_unref (sock
);
1045 test_ipv6_v4mapped (void)
1048 GError
*error
= NULL
;
1050 GSocketAddress
*addr
, *v4addr
;
1051 GInetAddress
*iaddr
;
1053 if (!ipv6_supported
)
1055 g_test_skip ("No support for IPv6");
1059 data
= create_server (G_SOCKET_FAMILY_IPV6
, v4mapped_server_thread
, TRUE
, &error
);
1062 gchar
*message
= g_strdup_printf ("Failed to create server: %s", error
->message
);
1063 g_test_skip (message
);
1065 g_clear_error (&error
);
1069 client
= g_socket_new (G_SOCKET_FAMILY_IPV4
,
1070 G_SOCKET_TYPE_STREAM
,
1071 G_SOCKET_PROTOCOL_DEFAULT
,
1073 g_assert_no_error (error
);
1075 g_socket_set_blocking (client
, TRUE
);
1076 g_socket_set_timeout (client
, 1);
1078 addr
= g_socket_get_local_address (data
->server
, &error
);
1079 g_assert_no_error (error
);
1080 iaddr
= g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4
);
1081 v4addr
= g_inet_socket_address_new (iaddr
, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr
)));
1082 g_object_unref (iaddr
);
1083 g_object_unref (addr
);
1085 g_socket_connect (client
, v4addr
, NULL
, &error
);
1086 g_assert_no_error (error
);
1087 g_assert (g_socket_is_connected (client
));
1089 g_thread_join (data
->thread
);
1091 g_socket_close (client
, &error
);
1092 g_assert_no_error (error
);
1093 g_socket_close (data
->server
, &error
);
1094 g_assert_no_error (error
);
1096 g_object_unref (data
->server
);
1097 g_object_unref (client
);
1098 g_object_unref (v4addr
);
1100 g_slice_free (IPTestData
, data
);
1105 test_timed_wait (void)
1108 GError
*error
= NULL
;
1110 GSocketAddress
*addr
;
1114 data
= create_server (G_SOCKET_FAMILY_IPV4
, echo_server_thread
, FALSE
, &error
);
1117 gchar
*message
= g_strdup_printf ("Failed to create server: %s", error
->message
);
1118 g_test_skip (message
);
1120 g_clear_error (&error
);
1124 addr
= g_socket_get_local_address (data
->server
, &error
);
1125 g_assert_no_error (error
);
1127 client
= g_socket_new (G_SOCKET_FAMILY_IPV4
,
1128 G_SOCKET_TYPE_STREAM
,
1129 G_SOCKET_PROTOCOL_DEFAULT
,
1131 g_assert_no_error (error
);
1133 g_socket_set_blocking (client
, TRUE
);
1134 g_socket_set_timeout (client
, 1);
1136 g_socket_connect (client
, addr
, NULL
, &error
);
1137 g_assert_no_error (error
);
1138 g_object_unref (addr
);
1140 start_time
= g_get_monotonic_time ();
1141 g_socket_condition_timed_wait (client
, G_IO_IN
, 100000 /* 100 ms */,
1143 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_TIMED_OUT
);
1144 g_clear_error (&error
);
1145 poll_duration
= g_get_monotonic_time () - start_time
;
1147 g_assert_cmpint (poll_duration
, >=, 98000);
1148 g_assert_cmpint (poll_duration
, <, 112000);
1150 g_socket_close (client
, &error
);
1151 g_assert_no_error (error
);
1153 g_thread_join (data
->thread
);
1155 g_socket_close (data
->server
, &error
);
1156 g_assert_no_error (error
);
1158 g_object_unref (data
->server
);
1159 g_object_unref (client
);
1161 g_slice_free (IPTestData
, data
);
1165 duplicate_fd (int fd
)
1170 if (!DuplicateHandle (GetCurrentProcess (),
1172 GetCurrentProcess (),
1176 DUPLICATE_SAME_ACCESS
))
1188 test_fd_reuse (void)
1191 GError
*error
= NULL
;
1194 GSocketAddress
*addr
;
1199 g_test_bug ("741707");
1201 data
= create_server (G_SOCKET_FAMILY_IPV4
, echo_server_thread
, FALSE
, &error
);
1204 gchar
*message
= g_strdup_printf ("Failed to create server: %s", error
->message
);
1205 g_test_skip (message
);
1207 g_clear_error (&error
);
1211 addr
= g_socket_get_local_address (data
->server
, &error
);
1212 g_assert_no_error (error
);
1214 client
= g_socket_new (G_SOCKET_FAMILY_IPV4
,
1215 G_SOCKET_TYPE_STREAM
,
1216 G_SOCKET_PROTOCOL_DEFAULT
,
1218 g_assert_no_error (error
);
1220 g_socket_set_blocking (client
, TRUE
);
1221 g_socket_set_timeout (client
, 1);
1223 g_socket_connect (client
, addr
, NULL
, &error
);
1224 g_assert_no_error (error
);
1225 g_assert (g_socket_is_connected (client
));
1226 g_object_unref (addr
);
1228 /* we have to dup otherwise the fd gets closed twice on unref */
1229 fd
= duplicate_fd (g_socket_get_fd (client
));
1230 client2
= g_socket_new_from_fd (fd
, &error
);
1231 g_assert_no_error (error
);
1233 g_assert_cmpint (g_socket_get_family (client2
), ==, g_socket_get_family (client
));
1234 g_assert_cmpint (g_socket_get_socket_type (client2
), ==, g_socket_get_socket_type (client
));
1235 g_assert_cmpint (g_socket_get_protocol (client2
), ==, G_SOCKET_PROTOCOL_TCP
);
1237 len
= g_socket_send (client2
, testbuf
, strlen (testbuf
) + 1, NULL
, &error
);
1238 g_assert_no_error (error
);
1239 g_assert_cmpint (len
, ==, strlen (testbuf
) + 1);
1241 len
= g_socket_receive (client2
, buf
, sizeof (buf
), NULL
, &error
);
1242 g_assert_no_error (error
);
1243 g_assert_cmpint (len
, ==, strlen (testbuf
) + 1);
1245 g_assert_cmpstr (testbuf
, ==, buf
);
1247 g_socket_shutdown (client
, FALSE
, TRUE
, &error
);
1248 g_assert_no_error (error
);
1249 /* The semantics of dup()+shutdown() are ambiguous; this call will succeed
1250 * on Linux, but return ENOTCONN on OS X.
1252 g_socket_shutdown (client2
, FALSE
, TRUE
, NULL
);
1254 g_thread_join (data
->thread
);
1256 g_socket_close (client
, &error
);
1257 g_assert_no_error (error
);
1258 g_socket_close (client2
, &error
);
1259 g_assert_no_error (error
);
1260 g_socket_close (data
->server
, &error
);
1261 g_assert_no_error (error
);
1263 g_assert_cmpint (g_socket_get_fd (client
), ==, -1);
1264 g_assert_cmpint (g_socket_get_fd (client2
), ==, -1);
1265 g_assert_cmpint (g_socket_get_fd (data
->server
), ==, -1);
1267 g_object_unref (data
->server
);
1268 g_object_unref (client
);
1269 g_object_unref (client2
);
1271 g_slice_free (IPTestData
, data
);
1275 test_sockaddr (void)
1277 struct sockaddr_in6 sin6
, gsin6
;
1278 GSocketAddress
*saddr
;
1279 GInetSocketAddress
*isaddr
;
1280 GInetAddress
*iaddr
;
1281 GError
*error
= NULL
;
1283 memset (&sin6
, 0, sizeof (sin6
));
1284 sin6
.sin6_family
= AF_INET6
;
1285 sin6
.sin6_addr
= in6addr_loopback
;
1286 sin6
.sin6_port
= g_htons (42);
1287 sin6
.sin6_scope_id
= 17;
1288 sin6
.sin6_flowinfo
= 1729;
1290 saddr
= g_socket_address_new_from_native (&sin6
, sizeof (sin6
));
1291 g_assert (G_IS_INET_SOCKET_ADDRESS (saddr
));
1293 isaddr
= G_INET_SOCKET_ADDRESS (saddr
);
1294 iaddr
= g_inet_socket_address_get_address (isaddr
);
1295 g_assert_cmpint (g_inet_address_get_family (iaddr
), ==, G_SOCKET_FAMILY_IPV6
);
1296 g_assert (g_inet_address_get_is_loopback (iaddr
));
1298 g_assert_cmpint (g_inet_socket_address_get_port (isaddr
), ==, 42);
1299 g_assert_cmpint (g_inet_socket_address_get_scope_id (isaddr
), ==, 17);
1300 g_assert_cmpint (g_inet_socket_address_get_flowinfo (isaddr
), ==, 1729);
1302 g_socket_address_to_native (saddr
, &gsin6
, sizeof (gsin6
), &error
);
1303 g_assert_no_error (error
);
1305 g_assert (memcmp (&sin6
.sin6_addr
, &gsin6
.sin6_addr
, sizeof (struct in6_addr
)) == 0);
1306 g_assert_cmpint (sin6
.sin6_port
, ==, gsin6
.sin6_port
);
1307 g_assert_cmpint (sin6
.sin6_scope_id
, ==, gsin6
.sin6_scope_id
);
1308 g_assert_cmpint (sin6
.sin6_flowinfo
, ==, gsin6
.sin6_flowinfo
);
1310 g_object_unref (saddr
);
1315 test_unix_from_fd (void)
1321 fd
= socket (AF_UNIX
, SOCK_STREAM
, 0);
1322 g_assert_cmpint (fd
, !=, -1);
1325 s
= g_socket_new_from_fd (fd
, &error
);
1326 g_assert_no_error (error
);
1327 g_assert_cmpint (g_socket_get_family (s
), ==, G_SOCKET_FAMILY_UNIX
);
1328 g_assert_cmpint (g_socket_get_socket_type (s
), ==, G_SOCKET_TYPE_STREAM
);
1329 g_assert_cmpint (g_socket_get_protocol (s
), ==, G_SOCKET_PROTOCOL_DEFAULT
);
1334 test_unix_connection (void)
1339 GSocketConnection
*c
;
1341 fd
= socket (AF_UNIX
, SOCK_STREAM
, 0);
1342 g_assert_cmpint (fd
, !=, -1);
1345 s
= g_socket_new_from_fd (fd
, &error
);
1346 g_assert_no_error (error
);
1347 c
= g_socket_connection_factory_create_connection (s
);
1348 g_assert (G_IS_UNIX_CONNECTION (c
));
1353 static GSocketConnection
*
1354 create_connection_for_fd (int fd
)
1358 GSocketConnection
*connection
;
1360 socket
= g_socket_new_from_fd (fd
, &err
);
1361 g_assert_no_error (err
);
1362 g_assert (G_IS_SOCKET (socket
));
1363 connection
= g_socket_connection_factory_create_connection (socket
);
1364 g_assert (G_IS_UNIX_CONNECTION (connection
));
1365 g_object_unref (socket
);
1369 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
1372 test_unix_connection_ancillary_data (void)
1376 gint status
, fd
, len
;
1381 g_assert_cmpint (status
, ==, 0);
1383 status
= socketpair (PF_UNIX
, SOCK_STREAM
, 0, sv
);
1384 g_assert_cmpint (status
, ==, 0);
1387 g_assert_cmpint (pid
, >=, 0);
1389 /* Child: close its copy of the write end of the pipe, receive it
1390 * again from the parent over the socket, and write some text to it.
1392 * Parent: send the write end of the pipe (still open for the
1393 * parent) over the socket, close it, and read some text from the
1394 * read end of the pipe.
1398 GSocketConnection
*connection
;
1401 connection
= create_connection_for_fd (sv
[0]);
1403 status
= close (pv
[1]);
1404 g_assert_cmpint (status
, ==, 0);
1407 fd
= g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection
), NULL
,
1409 g_assert_no_error (err
);
1410 g_assert_cmpint (fd
, >, -1);
1411 g_object_unref (connection
);
1414 len
= write (fd
, TEST_DATA
, sizeof (TEST_DATA
));
1415 while (len
== -1 && errno
== EINTR
);
1416 g_assert_cmpint (len
, ==, sizeof (TEST_DATA
));
1421 GSocketConnection
*connection
;
1424 connection
= create_connection_for_fd (sv
[1]);
1427 g_unix_connection_send_fd (G_UNIX_CONNECTION (connection
), pv
[1], NULL
,
1429 g_assert_no_error (err
);
1430 g_object_unref (connection
);
1432 status
= close (pv
[1]);
1433 g_assert_cmpint (status
, ==, 0);
1435 memset (buffer
, 0xff, sizeof buffer
);
1437 len
= read (pv
[0], buffer
, sizeof buffer
);
1438 while (len
== -1 && errno
== EINTR
);
1440 g_assert_cmpint (len
, ==, sizeof (TEST_DATA
));
1441 g_assert_cmpstr (buffer
, ==, TEST_DATA
);
1443 waitpid (pid
, &status
, 0);
1444 g_assert (WIFEXITED (status
));
1445 g_assert_cmpint (WEXITSTATUS (status
), ==, 0);
1448 /* TODO: add test for g_unix_connection_send_credentials() and
1449 * g_unix_connection_receive_credentials().
1454 postmortem_source_cb (GSocket
*socket
,
1455 GIOCondition condition
,
1458 gboolean
*been_here
= user_data
;
1460 g_assert_cmpint (condition
, ==, G_IO_NVAL
);
1467 test_source_postmortem (void)
1469 GMainContext
*context
;
1472 GError
*error
= NULL
;
1473 gboolean callback_visited
= FALSE
;
1475 socket
= g_socket_new (G_SOCKET_FAMILY_UNIX
, G_SOCKET_TYPE_STREAM
, G_SOCKET_PROTOCOL_DEFAULT
, &error
);
1476 g_assert_no_error (error
);
1478 context
= g_main_context_new ();
1480 source
= g_socket_create_source (socket
, G_IO_IN
, NULL
);
1481 g_source_set_callback (source
, (GSourceFunc
) postmortem_source_cb
,
1482 &callback_visited
, NULL
);
1483 g_source_attach (source
, context
);
1484 g_source_unref (source
);
1486 g_socket_close (socket
, &error
);
1487 g_assert_no_error (error
);
1488 g_object_unref (socket
);
1490 /* Test that, after a socket is closed, its source callback should be called
1492 g_main_context_iteration (context
, FALSE
);
1493 g_assert (callback_visited
);
1494 g_assert (!g_main_context_pending (context
));
1496 g_main_context_unref (context
);
1499 #endif /* G_OS_UNIX */
1502 test_reuse_tcp (void)
1504 GSocket
*sock1
, *sock2
;
1505 GError
*error
= NULL
;
1506 GInetAddress
*iaddr
;
1507 GSocketAddress
*addr
;
1509 sock1
= g_socket_new (G_SOCKET_FAMILY_IPV4
,
1510 G_SOCKET_TYPE_STREAM
,
1511 G_SOCKET_PROTOCOL_DEFAULT
,
1513 g_assert_no_error (error
);
1515 iaddr
= g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4
);
1516 addr
= g_inet_socket_address_new (iaddr
, 0);
1517 g_object_unref (iaddr
);
1518 g_socket_bind (sock1
, addr
, TRUE
, &error
);
1519 g_object_unref (addr
);
1520 g_assert_no_error (error
);
1522 g_socket_listen (sock1
, &error
);
1523 g_assert_no_error (error
);
1525 sock2
= g_socket_new (G_SOCKET_FAMILY_IPV4
,
1526 G_SOCKET_TYPE_STREAM
,
1527 G_SOCKET_PROTOCOL_DEFAULT
,
1529 g_assert_no_error (error
);
1531 addr
= g_socket_get_local_address (sock1
, &error
);
1532 g_assert_no_error (error
);
1533 g_socket_bind (sock2
, addr
, TRUE
, &error
);
1534 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_ADDRESS_IN_USE
);
1535 g_clear_error (&error
);
1536 g_object_unref (addr
);
1538 g_object_unref (sock1
);
1539 g_object_unref (sock2
);
1543 test_reuse_udp (void)
1545 GSocket
*sock1
, *sock2
;
1546 GError
*error
= NULL
;
1547 GInetAddress
*iaddr
;
1548 GSocketAddress
*addr
;
1550 sock1
= g_socket_new (G_SOCKET_FAMILY_IPV4
,
1551 G_SOCKET_TYPE_DATAGRAM
,
1552 G_SOCKET_PROTOCOL_DEFAULT
,
1554 g_assert_no_error (error
);
1556 iaddr
= g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4
);
1557 addr
= g_inet_socket_address_new (iaddr
, 0);
1558 g_object_unref (iaddr
);
1559 g_socket_bind (sock1
, addr
, TRUE
, &error
);
1560 g_object_unref (addr
);
1561 g_assert_no_error (error
);
1563 sock2
= g_socket_new (G_SOCKET_FAMILY_IPV4
,
1564 G_SOCKET_TYPE_DATAGRAM
,
1565 G_SOCKET_PROTOCOL_DEFAULT
,
1567 g_assert_no_error (error
);
1569 addr
= g_socket_get_local_address (sock1
, &error
);
1570 g_assert_no_error (error
);
1571 g_socket_bind (sock2
, addr
, TRUE
, &error
);
1572 g_object_unref (addr
);
1573 g_assert_no_error (error
);
1575 g_object_unref (sock1
);
1576 g_object_unref (sock2
);
1580 test_get_available (gconstpointer user_data
)
1582 GSocketType socket_type
= GPOINTER_TO_UINT (user_data
);
1584 GSocket
*listener
, *server
, *client
;
1586 GSocketAddress
*saddr
;
1587 gchar data
[] = "0123456789abcdef";
1591 listener
= g_socket_new (G_SOCKET_FAMILY_IPV4
,
1593 G_SOCKET_PROTOCOL_DEFAULT
,
1595 g_assert_no_error (err
);
1596 g_assert (G_IS_SOCKET (listener
));
1598 client
= g_socket_new (G_SOCKET_FAMILY_IPV4
,
1600 G_SOCKET_PROTOCOL_DEFAULT
,
1602 g_assert_no_error (err
);
1603 g_assert (G_IS_SOCKET (client
));
1605 if (socket_type
== G_SOCKET_TYPE_STREAM
)
1607 g_socket_set_option (client
, IPPROTO_TCP
, TCP_NODELAY
, TRUE
, &err
);
1608 g_assert_no_error (err
);
1611 addr
= g_inet_address_new_any (G_SOCKET_FAMILY_IPV4
);
1612 saddr
= g_inet_socket_address_new (addr
, 0);
1614 g_socket_bind (listener
, saddr
, TRUE
, &err
);
1615 g_assert_no_error (err
);
1616 g_object_unref (saddr
);
1617 g_object_unref (addr
);
1619 saddr
= g_socket_get_local_address (listener
, &err
);
1620 g_assert_no_error (err
);
1622 if (socket_type
== G_SOCKET_TYPE_STREAM
)
1624 g_socket_listen (listener
, &err
);
1625 g_assert_no_error (err
);
1626 g_socket_connect (client
, saddr
, NULL
, &err
);
1627 g_assert_no_error (err
);
1629 server
= g_socket_accept (listener
, NULL
, &err
);
1630 g_assert_no_error (err
);
1631 g_socket_set_blocking (server
, FALSE
);
1632 g_object_unref (listener
);
1637 g_socket_send_to (client
, saddr
, data
, sizeof (data
), NULL
, &err
);
1638 g_assert_no_error (err
);
1640 while (!g_socket_condition_wait (server
, G_IO_IN
, NULL
, NULL
))
1642 g_assert_cmpint (g_socket_get_available_bytes (server
), ==, sizeof (data
));
1644 g_socket_send_to (client
, saddr
, data
, sizeof (data
), NULL
, &err
);
1645 g_assert_no_error (err
);
1647 /* We need to wait until the data has actually been copied into the
1648 * server socket's buffers, but g_socket_condition_wait() won't help
1649 * here since the socket is definitely already readable. So there's
1650 * a race condition in checking its available bytes. In the TCP
1651 * case, we poll for a bit until the new data shows up. In the UDP
1652 * case, there's not much we can do, but at least the failure mode
1653 * is passes-when-it-shouldn't, not fails-when-it-shouldn't.
1655 if (socket_type
== G_SOCKET_TYPE_STREAM
)
1659 for (tries
= 0; tries
< 100; tries
++)
1661 if (g_socket_get_available_bytes (server
) > sizeof (data
))
1666 g_assert_cmpint (g_socket_get_available_bytes (server
), ==, 2 * sizeof (data
));
1671 g_assert_cmpint (g_socket_get_available_bytes (server
), ==, sizeof (data
));
1674 g_assert_cmpint (sizeof (buf
), >=, 2 * sizeof (data
));
1675 nread
= g_socket_receive (server
, buf
, sizeof (buf
), NULL
, &err
);
1676 g_assert_no_error (err
);
1678 if (socket_type
== G_SOCKET_TYPE_STREAM
)
1680 g_assert_cmpint (nread
, ==, 2 * sizeof (data
));
1681 g_assert_cmpint (g_socket_get_available_bytes (server
), ==, 0);
1685 g_assert_cmpint (nread
, ==, sizeof (data
));
1686 g_assert_cmpint (g_socket_get_available_bytes (server
), ==, sizeof (data
));
1689 nread
= g_socket_receive (server
, buf
, sizeof (buf
), NULL
, &err
);
1690 if (socket_type
== G_SOCKET_TYPE_STREAM
)
1692 g_assert_cmpint (nread
, ==, -1);
1693 g_assert_error (err
, G_IO_ERROR
, G_IO_ERROR_WOULD_BLOCK
);
1694 g_clear_error (&err
);
1698 g_assert_cmpint (nread
, ==, sizeof (data
));
1699 g_assert_no_error (err
);
1702 g_assert_cmpint (g_socket_get_available_bytes (server
), ==, 0);
1704 g_socket_close (server
, &err
);
1705 g_assert_no_error (err
);
1707 g_object_unref (saddr
);
1708 g_object_unref (server
);
1709 g_object_unref (client
);
1717 GError
*error
= NULL
;
1719 g_test_init (&argc
, &argv
, NULL
);
1720 g_test_bug_base ("https://bugzilla.gnome.org/");
1722 sock
= g_socket_new (G_SOCKET_FAMILY_IPV6
,
1723 G_SOCKET_TYPE_STREAM
,
1724 G_SOCKET_PROTOCOL_DEFAULT
,
1728 ipv6_supported
= TRUE
;
1729 g_object_unref (sock
);
1733 g_assert_error (error
, G_IO_ERROR
, G_IO_ERROR_NOT_SUPPORTED
);
1734 g_clear_error (&error
);
1737 g_test_add_func ("/socket/ipv4_sync", test_ipv4_sync
);
1738 g_test_add_func ("/socket/ipv4_async", test_ipv4_async
);
1739 g_test_add_func ("/socket/ipv6_sync", test_ipv6_sync
);
1740 g_test_add_func ("/socket/ipv6_async", test_ipv6_async
);
1741 g_test_add_func ("/socket/ipv4_sync/datagram", test_ipv4_sync_dgram
);
1742 g_test_add_func ("/socket/ipv4_sync/datagram/timeouts", test_ipv4_sync_dgram_timeouts
);
1743 g_test_add_func ("/socket/ipv6_sync/datagram", test_ipv6_sync_dgram
);
1744 g_test_add_func ("/socket/ipv6_sync/datagram/timeouts", test_ipv6_sync_dgram_timeouts
);
1745 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
1746 g_test_add_func ("/socket/ipv6_v4mapped", test_ipv6_v4mapped
);
1748 g_test_add_func ("/socket/close_graceful", test_close_graceful
);
1749 g_test_add_func ("/socket/timed_wait", test_timed_wait
);
1750 g_test_add_func ("/socket/fd_reuse", test_fd_reuse
);
1751 g_test_add_func ("/socket/address", test_sockaddr
);
1753 g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd
);
1754 g_test_add_func ("/socket/unix-connection", test_unix_connection
);
1755 g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data
);
1756 g_test_add_func ("/socket/source-postmortem", test_source_postmortem
);
1758 g_test_add_func ("/socket/reuse/tcp", test_reuse_tcp
);
1759 g_test_add_func ("/socket/reuse/udp", test_reuse_udp
);
1760 g_test_add_data_func ("/socket/get_available/datagram", GUINT_TO_POINTER (G_SOCKET_TYPE_DATAGRAM
),
1761 test_get_available
);
1762 g_test_add_data_func ("/socket/get_available/stream", GUINT_TO_POINTER (G_SOCKET_TYPE_STREAM
),
1763 test_get_available
);
1765 return g_test_run();