Merge branch '976-disable-assert-checks' into 'master'
[glib.git] / gio / tests / socket.c
blob9b3adb4e4ea72b33e2408917fc605bd780300d90
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/>.
19 #include <gio/gio.h>
21 #ifdef G_OS_UNIX
22 #include <errno.h>
23 #include <sys/wait.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <gio/gnetworking.h>
27 #include <gio/gunixconnection.h>
28 #endif
30 #include "gnetworkingprivate.h"
32 static gboolean ipv6_supported;
34 typedef struct {
35 GSocket *server;
36 GSocket *client;
37 GSocketFamily family;
38 GThread *thread;
39 GMainLoop *loop;
40 GCancellable *cancellable; /* to shut down dgram echo server thread */
41 } IPTestData;
43 static gpointer
44 echo_server_dgram_thread (gpointer user_data)
46 IPTestData *data = user_data;
47 GSocketAddress *sa;
48 GCancellable *cancellable = data->cancellable;
49 GSocket *sock;
50 GError *error = NULL;
51 gssize nread, nwrote;
52 gchar buf[128];
54 sock = data->server;
56 while (TRUE)
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))
60 break;
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))
66 break;
67 g_assert_no_error (error);
68 g_assert_cmpint (nwrote, ==, nread);
70 g_object_unref (sa);
73 g_clear_error (&error);
75 return NULL;
78 static gpointer
79 echo_server_thread (gpointer user_data)
81 IPTestData *data = user_data;
82 GSocket *sock;
83 GError *error = NULL;
84 gssize nread, nwrote;
85 gchar buf[128];
87 sock = g_socket_accept (data->server, NULL, &error);
88 g_assert_no_error (error);
90 while (TRUE)
92 nread = g_socket_receive (sock, buf, sizeof (buf), NULL, &error);
93 g_assert_no_error (error);
94 g_assert_cmpint (nread, >=, 0);
96 if (nread == 0)
97 break;
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);
107 return NULL;
110 static IPTestData *
111 create_server_full (GSocketFamily family,
112 GSocketType socket_type,
113 GThreadFunc server_thread,
114 gboolean v4mapped,
115 GError **error)
117 IPTestData *data;
118 GSocket *server;
119 GSocketAddress *addr;
120 GInetAddress *iaddr;
122 data = g_slice_new (IPTestData);
123 data->family = family;
125 data->server = server = g_socket_new (family,
126 socket_type,
127 G_SOCKET_PROTOCOL_DEFAULT,
128 error);
129 if (server == NULL)
130 goto error;
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)
139 if (v4mapped)
141 g_socket_set_option (data->server, IPPROTO_IPV6, IPV6_V6ONLY, FALSE, NULL);
142 if (!g_socket_speaks_ipv4 (data->server))
143 goto error;
145 #endif
147 if (v4mapped)
148 iaddr = g_inet_address_new_any (family);
149 else
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);
158 goto error;
160 g_object_unref (addr);
162 addr = g_socket_get_local_address (server, error);
163 if (addr == NULL)
164 goto 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))
171 goto error;
173 else
175 data->cancellable = g_cancellable_new ();
178 data->thread = g_thread_new ("server", server_thread, data);
180 return data;
182 error:
183 g_clear_object (&data->server);
184 g_slice_free (IPTestData, data);
186 return NULL;
189 static IPTestData *
190 create_server (GSocketFamily family,
191 GThreadFunc server_thread,
192 gboolean v4mapped,
193 GError **error)
195 return create_server_full (family, G_SOCKET_TYPE_STREAM, server_thread, v4mapped, error);
198 static const gchar *testbuf = "0123456789abcdef";
200 static gboolean
201 test_ip_async_read_ready (GSocket *client,
202 GIOCondition cond,
203 gpointer user_data)
205 IPTestData *data = user_data;
206 GError *error = NULL;
207 gssize len;
208 gchar buf[128];
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);
220 return FALSE;
223 static gboolean
224 test_ip_async_write_ready (GSocket *client,
225 GIOCondition cond,
226 gpointer user_data)
228 IPTestData *data = user_data;
229 GError *error = NULL;
230 GSource *source;
231 gssize len;
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,
241 data, NULL);
242 g_source_attach (source, NULL);
243 g_source_unref (source);
245 return FALSE;
248 static gboolean
249 test_ip_async_timed_out (GSocket *client,
250 GIOCondition cond,
251 gpointer user_data)
253 IPTestData *data = user_data;
254 GError *error = NULL;
255 GSource *source;
256 gssize len;
257 gchar buf[128];
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,
270 data, NULL);
271 g_source_attach (source, NULL);
272 g_source_unref (source);
274 return FALSE;
277 static gboolean
278 test_ip_async_connected (GSocket *client,
279 GIOCondition cond,
280 gpointer user_data)
282 IPTestData *data = user_data;
283 GError *error = NULL;
284 GSource *source;
285 gssize len;
286 gchar buf[128];
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,
307 data, NULL);
308 g_source_attach (source, NULL);
309 g_source_unref (source);
311 else
312 test_ip_async_timed_out (client, 0, data);
314 return FALSE;
317 static gboolean
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);
325 static void
326 test_ip_async (GSocketFamily family)
328 IPTestData *data;
329 GError *error = NULL;
330 GSocket *client;
331 GSocketAddress *addr;
332 GSource *source;
333 gssize len;
334 gchar buf[128];
336 data = create_server (family, echo_server_thread, FALSE, &error);
337 if (error != NULL)
339 gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
340 g_test_skip (message);
341 g_free (message);
342 g_clear_error (&error);
343 return;
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,
352 &error);
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);
368 else
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,
374 data, NULL);
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),
393 TRUE, NULL, &error);
394 g_assert_no_error (error);
395 g_assert_cmpint (len, ==, 0);
397 else
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,
401 TRUE, NULL, &error);
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);
418 static void
419 test_ipv4_async (void)
421 test_ip_async (G_SOCKET_FAMILY_IPV4);
424 static void
425 test_ipv6_async (void)
427 if (!ipv6_supported)
429 g_test_skip ("No support for IPv6");
430 return;
433 test_ip_async (G_SOCKET_FAMILY_IPV6);
436 static const gchar testbuf2[] = "0123456789abcdefghijklmnopqrstuvwxyz";
438 static void
439 test_ip_sync (GSocketFamily family)
441 IPTestData *data;
442 GError *error = NULL;
443 GSocket *client;
444 GSocketAddress *addr;
445 gssize len;
446 gchar buf[128];
448 data = create_server (family, echo_server_thread, FALSE, &error);
449 if (error != NULL)
451 gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
452 g_test_skip (message);
453 g_free (message);
454 g_clear_error (&error);
455 return;
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,
464 &error);
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;
502 v[0].size = 3;
503 v[1].buffer = testbuf2 + 3;
504 v[1].size = 5;
505 v[2].buffer = testbuf2 + 3 + 5;
506 v[2].size = 0;
507 v[3].buffer = testbuf2 + 3 + 5;
508 v[3].size = 6;
509 v[4].buffer = testbuf2 + 3 + 5 + 6;
510 v[4].size = 2;
511 v[5].buffer = testbuf2 + 3 + 5 + 6 + 2;
512 v[5].size = 1;
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);
539 else
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);
559 static void
560 test_ipv4_sync (void)
562 test_ip_sync (G_SOCKET_FAMILY_IPV4);
565 static void
566 test_ipv6_sync (void)
568 if (!ipv6_supported)
570 g_test_skip ("No support for IPv6");
571 return;
574 test_ip_sync (G_SOCKET_FAMILY_IPV6);
577 static void
578 test_ip_sync_dgram (GSocketFamily family)
580 IPTestData *data;
581 GError *error = NULL;
582 GSocket *client;
583 GSocketAddress *dest_addr;
584 gssize len;
585 gchar buf[128];
587 data = create_server_full (family, G_SOCKET_TYPE_DATAGRAM,
588 echo_server_dgram_thread, FALSE, &error);
589 if (error != NULL)
591 gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
592 g_test_skip (message);
593 g_free (message);
594 g_clear_error (&error);
595 return;
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,
603 &error);
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;
630 v[0].size = 3;
631 v[1].buffer = testbuf2 + 3;
632 v[1].size = 5;
633 v[2].buffer = testbuf2 + 3 + 5;
634 v[2].size = 0;
635 v[3].buffer = testbuf2 + 3 + 5;
636 v[3].size = 6;
637 v[4].buffer = testbuf2 + 3 + 5 + 6;
638 v[4].size = 2;
639 v[5].buffer = testbuf2 + 3 + 5 + 6 + 2;
640 v[5].size = 1;
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;
645 iv[0].size = 3;
646 iv[1].buffer = buf + 3;
647 iv[1].size = 5;
648 iv[2].buffer = buf + 3 + 5;
649 iv[2].size = 0;
650 iv[3].buffer = buf + 3 + 5;
651 iv[3].size = 6;
652 iv[4].buffer = buf + 3 + 5 + 6;
653 iv[4].size = 2;
654 iv[5].buffer = buf + 3 + 5 + 6 + 2;
655 iv[5].size = 1;
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 */
705 m[0].bytes_sent = 0;
706 m[1].bytes_sent = 0;
707 m[2].bytes_sent = 0;
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,
726 NULL, &error);
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 */
736 m[0].bytes_sent = 0;
737 m[1].bytes_sent = 0;
738 m[2].bytes_sent = 0;
740 /* now try to generate an early return by omitting the destination address on [1] */
741 m[1].address = NULL;
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 */
751 m[0].bytes_sent = 0;
752 m[1].bytes_sent = 0;
753 m[2].bytes_sent = 0;
755 /* now try to generate an error by omitting all destination addresses */
756 m[0].address = NULL;
757 m[1].address = NULL;
758 m[2].address = NULL;
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);
789 static void
790 test_ipv4_sync_dgram (void)
792 test_ip_sync_dgram (G_SOCKET_FAMILY_IPV4);
795 static void
796 test_ipv6_sync_dgram (void)
798 if (!ipv6_supported)
800 g_test_skip ("No support for IPv6");
801 return;
804 test_ip_sync_dgram (G_SOCKET_FAMILY_IPV6);
807 static gpointer
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);
816 return NULL;
819 static void
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;
826 gssize len;
828 client = g_socket_new (family,
829 G_SOCKET_TYPE_DATAGRAM,
830 G_SOCKET_PROTOCOL_DEFAULT,
831 &error);
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. */
845 gint64 start_time;
846 GInputMessage im = { NULL, };
847 GInputVector iv = { NULL, };
848 guint8 buf[128];
850 iv.buffer = buf;
851 iv.size = sizeof (buf);
853 im.vectors = &iv;
854 im.num_vectors = 1;
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 */,
861 NULL, &error);
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 */,
872 NULL, &error);
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);
902 static void
903 test_ipv4_sync_dgram_timeouts (void)
905 test_ip_sync_dgram_timeouts (G_SOCKET_FAMILY_IPV4);
908 static void
909 test_ipv6_sync_dgram_timeouts (void)
911 if (!ipv6_supported)
913 g_test_skip ("No support for IPv6");
914 return;
917 test_ip_sync_dgram_timeouts (G_SOCKET_FAMILY_IPV6);
920 static gpointer
921 graceful_server_thread (gpointer user_data)
923 IPTestData *data = user_data;
924 GSocket *sock;
925 GError *error = NULL;
926 gssize len;
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);
935 return sock;
938 static void
939 test_close_graceful (void)
941 GSocketFamily family = G_SOCKET_FAMILY_IPV4;
942 IPTestData *data;
943 GError *error = NULL;
944 GSocket *client, *server;
945 GSocketAddress *addr;
946 gssize len;
947 gchar buf[128];
949 data = create_server (family, graceful_server_thread, FALSE, &error);
950 if (error != NULL)
952 gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
953 g_test_skip (message);
954 g_free (message);
955 g_clear_error (&error);
956 return;
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,
965 &error);
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)
1015 static gpointer
1016 v4mapped_server_thread (gpointer user_data)
1018 IPTestData *data = user_data;
1019 GSocket *sock;
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);
1041 return NULL;
1044 static void
1045 test_ipv6_v4mapped (void)
1047 IPTestData *data;
1048 GError *error = NULL;
1049 GSocket *client;
1050 GSocketAddress *addr, *v4addr;
1051 GInetAddress *iaddr;
1053 if (!ipv6_supported)
1055 g_test_skip ("No support for IPv6");
1056 return;
1059 data = create_server (G_SOCKET_FAMILY_IPV6, v4mapped_server_thread, TRUE, &error);
1060 if (error != NULL)
1062 gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
1063 g_test_skip (message);
1064 g_free (message);
1065 g_clear_error (&error);
1066 return;
1069 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1070 G_SOCKET_TYPE_STREAM,
1071 G_SOCKET_PROTOCOL_DEFAULT,
1072 &error);
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);
1102 #endif
1104 static void
1105 test_timed_wait (void)
1107 IPTestData *data;
1108 GError *error = NULL;
1109 GSocket *client;
1110 GSocketAddress *addr;
1111 gint64 start_time;
1112 gint poll_duration;
1114 data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE, &error);
1115 if (error != NULL)
1117 gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
1118 g_test_skip (message);
1119 g_free (message);
1120 g_clear_error (&error);
1121 return;
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,
1130 &error);
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 */,
1142 NULL, &error);
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);
1164 static int
1165 duplicate_fd (int fd)
1167 #ifdef G_OS_WIN32
1168 HANDLE newfd;
1170 if (!DuplicateHandle (GetCurrentProcess (),
1171 (HANDLE)fd,
1172 GetCurrentProcess (),
1173 &newfd,
1175 FALSE,
1176 DUPLICATE_SAME_ACCESS))
1178 return -1;
1181 return (int)newfd;
1182 #else
1183 return dup (fd);
1184 #endif
1187 static void
1188 test_fd_reuse (void)
1190 IPTestData *data;
1191 GError *error = NULL;
1192 GSocket *client;
1193 GSocket *client2;
1194 GSocketAddress *addr;
1195 int fd;
1196 gssize len;
1197 gchar buf[128];
1199 g_test_bug ("741707");
1201 data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE, &error);
1202 if (error != NULL)
1204 gchar *message = g_strdup_printf ("Failed to create server: %s", error->message);
1205 g_test_skip (message);
1206 g_free (message);
1207 g_clear_error (&error);
1208 return;
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,
1217 &error);
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);
1274 static void
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);
1313 #ifdef G_OS_UNIX
1314 static void
1315 test_unix_from_fd (void)
1317 gint fd;
1318 GError *error;
1319 GSocket *s;
1321 fd = socket (AF_UNIX, SOCK_STREAM, 0);
1322 g_assert_cmpint (fd, !=, -1);
1324 error = NULL;
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);
1330 g_object_unref (s);
1333 static void
1334 test_unix_connection (void)
1336 gint fd;
1337 GError *error;
1338 GSocket *s;
1339 GSocketConnection *c;
1341 fd = socket (AF_UNIX, SOCK_STREAM, 0);
1342 g_assert_cmpint (fd, !=, -1);
1344 error = NULL;
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));
1349 g_object_unref (c);
1350 g_object_unref (s);
1353 static GSocketConnection *
1354 create_connection_for_fd (int fd)
1356 GError *err = NULL;
1357 GSocket *socket;
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);
1366 return connection;
1369 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
1371 static void
1372 test_unix_connection_ancillary_data (void)
1374 GError *err = NULL;
1375 gint pv[2], sv[3];
1376 gint status, fd, len;
1377 char buffer[1024];
1378 pid_t pid;
1380 status = pipe (pv);
1381 g_assert_cmpint (status, ==, 0);
1383 status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
1384 g_assert_cmpint (status, ==, 0);
1386 pid = fork ();
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.
1396 if (pid == 0)
1398 GSocketConnection *connection;
1400 close (sv[1]);
1401 connection = create_connection_for_fd (sv[0]);
1403 status = close (pv[1]);
1404 g_assert_cmpint (status, ==, 0);
1406 err = NULL;
1407 fd = g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection), NULL,
1408 &err);
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));
1417 exit (0);
1419 else
1421 GSocketConnection *connection;
1423 close (sv[0]);
1424 connection = create_connection_for_fd (sv[1]);
1426 err = NULL;
1427 g_unix_connection_send_fd (G_UNIX_CONNECTION (connection), pv[1], NULL,
1428 &err);
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().
1453 static gboolean
1454 postmortem_source_cb (GSocket *socket,
1455 GIOCondition condition,
1456 gpointer user_data)
1458 gboolean *been_here = user_data;
1460 g_assert_cmpint (condition, ==, G_IO_NVAL);
1462 *been_here = TRUE;
1463 return FALSE;
1466 static void
1467 test_source_postmortem (void)
1469 GMainContext *context;
1470 GSocket *socket;
1471 GSource *source;
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
1491 * exactly once. */
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 */
1501 static void
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,
1512 &error);
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,
1528 &error);
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);
1542 static void
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,
1553 &error);
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,
1566 &error);
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);
1579 static void
1580 test_get_available (gconstpointer user_data)
1582 GSocketType socket_type = GPOINTER_TO_UINT (user_data);
1583 GError *err = NULL;
1584 GSocket *listener, *server, *client;
1585 GInetAddress *addr;
1586 GSocketAddress *saddr;
1587 gchar data[] = "0123456789abcdef";
1588 gchar buf[34];
1589 gssize nread;
1591 listener = g_socket_new (G_SOCKET_FAMILY_IPV4,
1592 socket_type,
1593 G_SOCKET_PROTOCOL_DEFAULT,
1594 &err);
1595 g_assert_no_error (err);
1596 g_assert (G_IS_SOCKET (listener));
1598 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1599 socket_type,
1600 G_SOCKET_PROTOCOL_DEFAULT,
1601 &err);
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);
1634 else
1635 server = 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)
1657 int tries;
1659 for (tries = 0; tries < 100; tries++)
1661 if (g_socket_get_available_bytes (server) > sizeof (data))
1662 break;
1663 g_usleep (100000);
1666 g_assert_cmpint (g_socket_get_available_bytes (server), ==, 2 * sizeof (data));
1668 else
1670 g_usleep (100000);
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);
1683 else
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);
1696 else
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);
1713 main (int argc,
1714 char *argv[])
1716 GSocket *sock;
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,
1725 &error);
1726 if (sock != NULL)
1728 ipv6_supported = TRUE;
1729 g_object_unref (sock);
1731 else
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);
1747 #endif
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);
1752 #ifdef G_OS_UNIX
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);
1757 #endif
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();