Imported Upstream version 2.53.3
[platform/upstream/glib.git] / gio / tests / socket.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2008-2011 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include <gio/gio.h>
20
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
29
30 #include "gnetworkingprivate.h"
31
32 static gboolean ipv6_supported;
33
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;
42
43 static gpointer
44 echo_server_dgram_thread (gpointer user_data)
45 {
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];
53
54   sock = data->server;
55
56   while (TRUE)
57     {
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);
63
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);
69
70       g_object_unref (sa);
71     }
72
73   g_clear_error (&error);
74
75   return NULL;
76 }
77
78 static gpointer
79 echo_server_thread (gpointer user_data)
80 {
81   IPTestData *data = user_data;
82   GSocket *sock;
83   GError *error = NULL;
84   gssize nread, nwrote;
85   gchar buf[128];
86
87   sock = g_socket_accept (data->server, NULL, &error);
88   g_assert_no_error (error);
89
90   while (TRUE)
91     {
92       nread = g_socket_receive (sock, buf, sizeof (buf), NULL, &error);
93       g_assert_no_error (error);
94       g_assert_cmpint (nread, >=, 0);
95
96       if (nread == 0)
97         break;
98
99       nwrote = g_socket_send (sock, buf, nread, NULL, &error);
100       g_assert_no_error (error);
101       g_assert_cmpint (nwrote, ==, nread);
102     }
103
104   g_socket_close (sock, &error);
105   g_assert_no_error (error);
106   g_object_unref (sock);
107   return NULL;
108 }
109
110 static IPTestData *
111 create_server_full (GSocketFamily family,
112                     GSocketType   socket_type,
113                     GThreadFunc   server_thread,
114                     gboolean      v4mapped)
115 {
116   IPTestData *data;
117   GSocket *server;
118   GError *error = NULL;
119   GSocketAddress *addr;
120   GInetAddress *iaddr;
121
122   data = g_slice_new (IPTestData);
123   data->family = family;
124
125   data->server = server = g_socket_new (family,
126                                         socket_type,
127                                         G_SOCKET_PROTOCOL_DEFAULT,
128                                         &error);
129   g_assert_no_error (error);
130
131   g_assert_cmpint (g_socket_get_family (server), ==, family);
132   g_assert_cmpint (g_socket_get_socket_type (server), ==, socket_type);
133   g_assert_cmpint (g_socket_get_protocol (server), ==, G_SOCKET_PROTOCOL_DEFAULT);
134
135   g_socket_set_blocking (server, TRUE);
136
137 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
138   if (v4mapped)
139     {
140       g_socket_set_option (data->server, IPPROTO_IPV6, IPV6_V6ONLY, FALSE, NULL);
141       if (! g_socket_speaks_ipv4 (data->server))
142         {
143           g_object_unref (data->server);
144           g_slice_free (IPTestData, data);
145           return NULL;
146         }
147     }
148 #endif
149
150   if (v4mapped)
151     iaddr = g_inet_address_new_any (family);
152   else
153     iaddr = g_inet_address_new_loopback (family);
154   addr = g_inet_socket_address_new (iaddr, 0);
155   g_object_unref (iaddr);
156
157   g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), ==, 0);
158   g_socket_bind (server, addr, TRUE, &error);
159   g_assert_no_error (error);
160   g_object_unref (addr);
161
162   addr = g_socket_get_local_address (server, &error);
163   g_assert_no_error (error);
164   g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), !=, 0);
165   g_object_unref (addr);
166
167   if (socket_type == G_SOCKET_TYPE_STREAM)
168     {
169       g_socket_listen (server, &error);
170       g_assert_no_error (error);
171     }
172   else
173     {
174       data->cancellable = g_cancellable_new ();
175     }
176
177   data->thread = g_thread_new ("server", server_thread, data);
178
179   return data;
180 }
181
182 static IPTestData *
183 create_server (GSocketFamily family,
184                GThreadFunc   server_thread,
185                gboolean      v4mapped)
186 {
187   return create_server_full (family, G_SOCKET_TYPE_STREAM, server_thread, v4mapped);
188 }
189
190 static const gchar *testbuf = "0123456789abcdef";
191
192 static gboolean
193 test_ip_async_read_ready (GSocket      *client,
194                           GIOCondition  cond,
195                           gpointer      user_data)
196 {
197   IPTestData *data = user_data;
198   GError *error = NULL;
199   gssize len;
200   gchar buf[128];
201
202   g_assert_cmpint (cond, ==, G_IO_IN);
203
204   len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
205   g_assert_no_error (error);
206   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
207
208   g_assert_cmpstr (testbuf, ==, buf);
209
210   g_main_loop_quit (data->loop);
211
212   return FALSE;
213 }
214
215 static gboolean
216 test_ip_async_write_ready (GSocket      *client,
217                            GIOCondition  cond,
218                            gpointer      user_data)
219 {
220   IPTestData *data = user_data;
221   GError *error = NULL;
222   GSource *source;
223   gssize len;
224
225   g_assert_cmpint (cond, ==, G_IO_OUT);
226
227   len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
228   g_assert_no_error (error);
229   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
230
231   source = g_socket_create_source (client, G_IO_IN, NULL);
232   g_source_set_callback (source, (GSourceFunc)test_ip_async_read_ready,
233                          data, NULL);
234   g_source_attach (source, NULL);
235   g_source_unref (source);
236
237   return FALSE;
238 }
239
240 static gboolean
241 test_ip_async_timed_out (GSocket      *client,
242                          GIOCondition  cond,
243                          gpointer      user_data)
244 {
245   IPTestData *data = user_data;
246   GError *error = NULL;
247   GSource *source;
248   gssize len;
249   gchar buf[128];
250
251   if (data->family == G_SOCKET_FAMILY_IPV4)
252     {
253       g_assert_cmpint (cond, ==, G_IO_IN);
254       len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
255       g_assert_cmpint (len, ==, -1);
256       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
257       g_clear_error (&error);
258     }
259
260   source = g_socket_create_source (client, G_IO_OUT, NULL);
261   g_source_set_callback (source, (GSourceFunc)test_ip_async_write_ready,
262                          data, NULL);
263   g_source_attach (source, NULL);
264   g_source_unref (source);
265
266   return FALSE;
267 }
268
269 static gboolean
270 test_ip_async_connected (GSocket      *client,
271                          GIOCondition  cond,
272                          gpointer      user_data)
273 {
274   IPTestData *data = user_data;
275   GError *error = NULL;
276   GSource *source;
277   gssize len;
278   gchar buf[128];
279
280   g_socket_check_connect_result (client, &error);
281   g_assert_no_error (error);
282   /* We do this after the check_connect_result, since that will give a
283    * more useful assertion in case of error.
284    */
285   g_assert_cmpint (cond, ==, G_IO_OUT);
286
287   g_assert (g_socket_is_connected (client));
288
289   /* This adds 1 second to "make check", so let's just only do it once. */
290   if (data->family == G_SOCKET_FAMILY_IPV4)
291     {
292       len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
293       g_assert_cmpint (len, ==, -1);
294       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
295       g_clear_error (&error);
296
297       source = g_socket_create_source (client, G_IO_IN, NULL);
298       g_source_set_callback (source, (GSourceFunc)test_ip_async_timed_out,
299                              data, NULL);
300       g_source_attach (source, NULL);
301       g_source_unref (source);
302     }
303   else
304     test_ip_async_timed_out (client, 0, data);
305
306   return FALSE;
307 }
308
309 static gboolean
310 idle_test_ip_async_connected (gpointer user_data)
311 {
312   IPTestData *data = user_data;
313
314   return test_ip_async_connected (data->client, G_IO_OUT, data);
315 }
316
317 static void
318 test_ip_async (GSocketFamily family)
319 {
320   IPTestData *data;
321   GError *error = NULL;
322   GSocket *client;
323   GSocketAddress *addr;
324   GSource *source;
325   gssize len;
326   gchar buf[128];
327
328   data = create_server (family, echo_server_thread, FALSE);
329   addr = g_socket_get_local_address (data->server, &error);
330   g_assert_no_error (error);
331
332   client = g_socket_new (family,
333                          G_SOCKET_TYPE_STREAM,
334                          G_SOCKET_PROTOCOL_DEFAULT,
335                          &error);
336   g_assert_no_error (error);
337   data->client = client;
338
339   g_assert_cmpint (g_socket_get_family (client), ==, family);
340   g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
341   g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
342
343   g_socket_set_blocking (client, FALSE);
344   g_socket_set_timeout (client, 1);
345
346   if (g_socket_connect (client, addr, NULL, &error))
347     {
348       g_assert_no_error (error);
349       g_idle_add (idle_test_ip_async_connected, data);
350     }
351   else
352     {
353       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
354       g_clear_error (&error);
355       source = g_socket_create_source (client, G_IO_OUT, NULL);
356       g_source_set_callback (source, (GSourceFunc)test_ip_async_connected,
357                              data, NULL);
358       g_source_attach (source, NULL);
359       g_source_unref (source);
360     }
361   g_object_unref (addr);
362
363   data->loop = g_main_loop_new (NULL, TRUE);
364   g_main_loop_run (data->loop);
365   g_main_loop_unref (data->loop);
366
367   g_socket_shutdown (client, FALSE, TRUE, &error);
368   g_assert_no_error (error);
369
370   g_thread_join (data->thread);
371
372   if (family == G_SOCKET_FAMILY_IPV4)
373     {
374       /* Test that reading on a remote-closed socket gets back 0 bytes. */
375       len = g_socket_receive_with_blocking (client, buf, sizeof (buf),
376                                             TRUE, NULL, &error);
377       g_assert_no_error (error);
378       g_assert_cmpint (len, ==, 0);
379     }
380   else
381     {
382       /* Test that writing to a remote-closed socket gets back CONNECTION_CLOSED. */
383       len = g_socket_send_with_blocking (client, testbuf, strlen (testbuf) + 1,
384                                          TRUE, NULL, &error);
385       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED);
386       g_assert_cmpint (len, ==, -1);
387       g_clear_error (&error);
388     }
389
390   g_socket_close (client, &error);
391   g_assert_no_error (error);
392   g_socket_close (data->server, &error);
393   g_assert_no_error (error);
394
395   g_object_unref (data->server);
396   g_object_unref (client);
397
398   g_slice_free (IPTestData, data);
399 }
400
401 static void
402 test_ipv4_async (void)
403 {
404   test_ip_async (G_SOCKET_FAMILY_IPV4);
405 }
406
407 static void
408 test_ipv6_async (void)
409 {
410   if (!ipv6_supported)
411     {
412       g_test_skip ("No support for IPv6");
413       return;
414     }
415
416   test_ip_async (G_SOCKET_FAMILY_IPV6);
417 }
418
419 static const gchar testbuf2[] = "0123456789abcdefghijklmnopqrstuvwxyz";
420
421 static void
422 test_ip_sync (GSocketFamily family)
423 {
424   IPTestData *data;
425   GError *error = NULL;
426   GSocket *client;
427   GSocketAddress *addr;
428   gssize len;
429   gchar buf[128];
430
431   data = create_server (family, echo_server_thread, FALSE);
432   addr = g_socket_get_local_address (data->server, &error);
433   g_assert_no_error (error);
434
435   client = g_socket_new (family,
436                          G_SOCKET_TYPE_STREAM,
437                          G_SOCKET_PROTOCOL_DEFAULT,
438                          &error);
439   g_assert_no_error (error);
440
441   g_assert_cmpint (g_socket_get_family (client), ==, family);
442   g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
443   g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
444
445   g_socket_set_blocking (client, TRUE);
446   g_socket_set_timeout (client, 1);
447
448   g_socket_connect (client, addr, NULL, &error);
449   g_assert_no_error (error);
450   g_assert (g_socket_is_connected (client));
451   g_object_unref (addr);
452
453   /* This adds 1 second to "make check", so let's just only do it once. */
454   if (family == G_SOCKET_FAMILY_IPV4)
455     {
456       len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
457       g_assert_cmpint (len, ==, -1);
458       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
459       g_clear_error (&error);
460     }
461
462   len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
463   g_assert_no_error (error);
464   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
465   
466   len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
467   g_assert_no_error (error);
468   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
469
470   g_assert_cmpstr (testbuf, ==, buf);
471
472   {
473     GOutputVector v[7] = { { NULL, }, };
474
475     v[0].buffer = testbuf2 + 0;
476     v[0].size = 3;
477     v[1].buffer = testbuf2 + 3;
478     v[1].size = 5;
479     v[2].buffer = testbuf2 + 3 + 5;
480     v[2].size = 0;
481     v[3].buffer = testbuf2 + 3 + 5;
482     v[3].size = 6;
483     v[4].buffer = testbuf2 + 3 + 5 + 6;
484     v[4].size = 2;
485     v[5].buffer = testbuf2 + 3 + 5 + 6 + 2;
486     v[5].size = 1;
487     v[6].buffer = testbuf2 + 3 + 5 + 6 + 2 + 1;
488     v[6].size = strlen (testbuf2) - (3 + 5 + 6 + 2 + 1);
489
490     len = g_socket_send_message (client, NULL, v, G_N_ELEMENTS (v), NULL, 0, 0, NULL, &error);
491     g_assert_no_error (error);
492     g_assert_cmpint (len, ==, strlen (testbuf2));
493
494     memset (buf, 0, sizeof (buf));
495     len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
496     g_assert_no_error (error);
497     g_assert_cmpint (len, ==, strlen (testbuf2));
498     g_assert_cmpstr (testbuf2, ==, buf);
499   }
500
501   g_socket_shutdown (client, FALSE, TRUE, &error);
502   g_assert_no_error (error);
503
504   g_thread_join (data->thread);
505
506   if (family == G_SOCKET_FAMILY_IPV4)
507     {
508       /* Test that reading on a remote-closed socket gets back 0 bytes. */
509       len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
510       g_assert_no_error (error);
511       g_assert_cmpint (len, ==, 0);
512     }
513   else
514     {
515       /* Test that writing to a remote-closed socket gets back CONNECTION_CLOSED. */
516       len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
517       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED);
518       g_assert_cmpint (len, ==, -1);
519       g_clear_error (&error);
520     }
521
522   g_socket_close (client, &error);
523   g_assert_no_error (error);
524   g_socket_close (data->server, &error);
525   g_assert_no_error (error);
526
527   g_object_unref (data->server);
528   g_object_unref (client);
529
530   g_slice_free (IPTestData, data);
531 }
532
533 static void
534 test_ipv4_sync (void)
535 {
536   test_ip_sync (G_SOCKET_FAMILY_IPV4);
537 }
538
539 static void
540 test_ipv6_sync (void)
541 {
542   if (!ipv6_supported)
543     {
544       g_test_skip ("No support for IPv6");
545       return;
546     }
547
548   test_ip_sync (G_SOCKET_FAMILY_IPV6);
549 }
550
551 static void
552 test_ip_sync_dgram (GSocketFamily family)
553 {
554   IPTestData *data;
555   GError *error = NULL;
556   GSocket *client;
557   GSocketAddress *dest_addr;
558   gssize len;
559   gchar buf[128];
560
561   data = create_server_full (family, G_SOCKET_TYPE_DATAGRAM,
562                              echo_server_dgram_thread, FALSE);
563
564   dest_addr = g_socket_get_local_address (data->server, &error);
565
566   client = g_socket_new (family,
567                          G_SOCKET_TYPE_DATAGRAM,
568                          G_SOCKET_PROTOCOL_DEFAULT,
569                          &error);
570   g_assert_no_error (error);
571
572   g_assert_cmpint (g_socket_get_family (client), ==, family);
573   g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_DATAGRAM);
574   g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
575
576   g_socket_set_blocking (client, TRUE);
577   g_socket_set_timeout (client, 1);
578
579   len = g_socket_send_to (client, dest_addr, testbuf, strlen (testbuf) + 1, NULL, &error);
580   g_assert_no_error (error);
581   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
582
583   len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
584   g_assert_no_error (error);
585   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
586
587   g_assert_cmpstr (testbuf, ==, buf);
588
589   {
590     GOutputMessage m[3] = { { NULL, }, };
591     GInputMessage im[3] = { { NULL, }, };
592     GOutputVector v[7] = { { NULL, }, };
593     GInputVector iv[7] = { { NULL, }, };
594
595     v[0].buffer = testbuf2 + 0;
596     v[0].size = 3;
597     v[1].buffer = testbuf2 + 3;
598     v[1].size = 5;
599     v[2].buffer = testbuf2 + 3 + 5;
600     v[2].size = 0;
601     v[3].buffer = testbuf2 + 3 + 5;
602     v[3].size = 6;
603     v[4].buffer = testbuf2 + 3 + 5 + 6;
604     v[4].size = 2;
605     v[5].buffer = testbuf2 + 3 + 5 + 6 + 2;
606     v[5].size = 1;
607     v[6].buffer = testbuf2 + 3 + 5 + 6 + 2 + 1;
608     v[6].size = strlen (testbuf2) - (3 + 5 + 6 + 2 + 1);
609
610     iv[0].buffer = buf + 0;
611     iv[0].size = 3;
612     iv[1].buffer = buf + 3;
613     iv[1].size = 5;
614     iv[2].buffer = buf + 3 + 5;
615     iv[2].size = 0;
616     iv[3].buffer = buf + 3 + 5;
617     iv[3].size = 6;
618     iv[4].buffer = buf + 3 + 5 + 6;
619     iv[4].size = 2;
620     iv[5].buffer = buf + 3 + 5 + 6 + 2;
621     iv[5].size = 1;
622     iv[6].buffer = buf + 3 + 5 + 6 + 2 + 1;
623     iv[6].size = sizeof (buf) - (3 + 5 + 6 + 2 + 1);
624
625     len = g_socket_send_message (client, dest_addr, v, G_N_ELEMENTS (v), NULL, 0, 0, NULL, &error);
626     g_assert_no_error (error);
627     g_assert_cmpint (len, ==, strlen (testbuf2));
628
629     memset (buf, 0, sizeof (buf));
630     len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
631     g_assert_no_error (error);
632     g_assert_cmpint (len, ==, strlen (testbuf2));
633     g_assert_cmpstr (testbuf2, ==, buf);
634
635     m[0].vectors = &v[0];
636     m[0].num_vectors = 1;
637     m[0].address = dest_addr;
638     m[1].vectors = &v[0];
639     m[1].num_vectors = 6;
640     m[1].address = dest_addr;
641     m[2].vectors = &v[6];
642     m[2].num_vectors = 1;
643     m[2].address = dest_addr;
644
645     len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
646     g_assert_no_error (error);
647     g_assert_cmpint (len, ==, G_N_ELEMENTS (m));
648     g_assert_cmpint (m[0].bytes_sent, ==, 3);
649     g_assert_cmpint (m[1].bytes_sent, ==, 17);
650     g_assert_cmpint (m[2].bytes_sent, ==, v[6].size);
651
652     memset (buf, 0, sizeof (buf));
653     len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
654     g_assert_no_error (error);
655     g_assert_cmpint (len, ==, 3);
656
657     memset (buf, 0, sizeof (buf));
658     len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
659     g_assert_no_error (error);
660     /* v[0].size + v[1].size + v[2].size + v[3].size + v[4].size + v[5].size */
661     g_assert_cmpint (len, ==, 17);
662     g_assert (memcmp (testbuf2, buf, 17) == 0);
663
664     memset (buf, 0, sizeof (buf));
665     len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
666     g_assert_no_error (error);
667     g_assert_cmpint (len, ==, v[6].size);
668     g_assert_cmpstr (buf, ==, v[6].buffer);
669
670     /* reset since we're re-using the message structs */
671     m[0].bytes_sent = 0;
672     m[1].bytes_sent = 0;
673     m[2].bytes_sent = 0;
674
675     /* now try receiving multiple messages */
676     len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
677     g_assert_no_error (error);
678     g_assert_cmpint (len, ==, G_N_ELEMENTS (m));
679     g_assert_cmpint (m[0].bytes_sent, ==, 3);
680     g_assert_cmpint (m[1].bytes_sent, ==, 17);
681     g_assert_cmpint (m[2].bytes_sent, ==, v[6].size);
682
683     im[0].vectors = &iv[0];
684     im[0].num_vectors = 1;
685     im[1].vectors = &iv[0];
686     im[1].num_vectors = 6;
687     im[2].vectors = &iv[6];
688     im[2].num_vectors = 1;
689
690     memset (buf, 0, sizeof (buf));
691     len = g_socket_receive_messages (client, im, G_N_ELEMENTS (im), 0,
692                                      NULL, &error);
693     g_assert_no_error (error);
694     g_assert_cmpint (len, ==, G_N_ELEMENTS (im));
695
696     g_assert_cmpuint (im[0].bytes_received, ==, 3);
697     /* v[0].size + v[1].size + v[2].size + v[3].size + v[4].size + v[5].size */
698     g_assert_cmpuint (im[1].bytes_received, ==, 17);
699     g_assert_cmpuint (im[2].bytes_received, ==, v[6].size);
700
701     /* reset since we're re-using the message structs */
702     m[0].bytes_sent = 0;
703     m[1].bytes_sent = 0;
704     m[2].bytes_sent = 0;
705
706     /* now try to generate an early return by omitting the destination address on [1] */
707     m[1].address = NULL;
708     len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
709     g_assert_no_error (error);
710     g_assert_cmpint (len, ==, 1);
711
712     g_assert_cmpint (m[0].bytes_sent, ==, 3);
713     g_assert_cmpint (m[1].bytes_sent, ==, 0);
714     g_assert_cmpint (m[2].bytes_sent, ==, 0);
715
716     /* reset since we're re-using the message structs */
717     m[0].bytes_sent = 0;
718     m[1].bytes_sent = 0;
719     m[2].bytes_sent = 0;
720
721     /* now try to generate an error by omitting all destination addresses */
722     m[0].address = NULL;
723     m[1].address = NULL;
724     m[2].address = NULL;
725     len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
726     g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
727     g_clear_error (&error);
728     g_assert_cmpint (len, ==, -1);
729
730     g_assert_cmpint (m[0].bytes_sent, ==, 0);
731     g_assert_cmpint (m[1].bytes_sent, ==, 0);
732     g_assert_cmpint (m[2].bytes_sent, ==, 0);
733
734     len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
735     g_assert_cmpint (len, ==, 3);
736   }
737
738   g_cancellable_cancel (data->cancellable);
739
740   g_thread_join (data->thread);
741
742   g_socket_close (client, &error);
743   g_assert_no_error (error);
744   g_socket_close (data->server, &error);
745   g_assert_no_error (error);
746
747   g_object_unref (data->server);
748   g_object_unref (data->cancellable);
749   g_object_unref (client);
750   g_object_unref (dest_addr);
751
752   g_slice_free (IPTestData, data);
753 }
754
755 static void
756 test_ipv4_sync_dgram (void)
757 {
758   test_ip_sync_dgram (G_SOCKET_FAMILY_IPV4);
759 }
760
761 static void
762 test_ipv6_sync_dgram (void)
763 {
764   if (!ipv6_supported)
765     {
766       g_test_skip ("No support for IPv6");
767       return;
768     }
769
770   test_ip_sync_dgram (G_SOCKET_FAMILY_IPV6);
771 }
772
773 static gpointer
774 cancellable_thread_cb (gpointer data)
775 {
776   GCancellable *cancellable = data;
777
778   g_usleep (0.1 * G_USEC_PER_SEC);
779   g_cancellable_cancel (cancellable);
780   g_object_unref (cancellable);
781
782   return NULL;
783 }
784
785 static void
786 test_ip_sync_dgram_timeouts (GSocketFamily family)
787 {
788   GError *error = NULL;
789   GSocket *client = NULL;
790   GCancellable *cancellable = NULL;
791   GThread *cancellable_thread = NULL;
792   gssize len;
793
794   client = g_socket_new (family,
795                          G_SOCKET_TYPE_DATAGRAM,
796                          G_SOCKET_PROTOCOL_DEFAULT,
797                          &error);
798   g_assert_no_error (error);
799
800   g_assert_cmpint (g_socket_get_family (client), ==, family);
801   g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_DATAGRAM);
802   g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
803
804   /* No overall timeout: test the per-operation timeouts instead. */
805   g_socket_set_timeout (client, 0);
806
807   cancellable = g_cancellable_new ();
808
809   /* Check for timeouts when no server is running. */
810   {
811     gint64 start_time;
812     GInputMessage im = { NULL, };
813     GInputVector iv = { NULL, };
814     guint8 buf[128];
815
816     iv.buffer = buf;
817     iv.size = sizeof (buf);
818
819     im.vectors = &iv;
820     im.num_vectors = 1;
821
822     memset (buf, 0, sizeof (buf));
823
824     /* Try a non-blocking read. */
825     g_socket_set_blocking (client, FALSE);
826     len = g_socket_receive_messages (client, &im, 1, 0  /* flags */,
827                                      NULL, &error);
828     g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
829     g_assert_cmpint (len, ==, -1);
830     g_clear_error (&error);
831
832     /* Try a timeout read. Can’t really validate the time taken more than
833      * checking it’s positive. */
834     g_socket_set_timeout (client, 1);
835     g_socket_set_blocking (client, TRUE);
836     start_time = g_get_monotonic_time ();
837     len = g_socket_receive_messages (client, &im, 1, 0  /* flags */,
838                                      NULL, &error);
839     g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
840     g_assert_cmpint (len, ==, -1);
841     g_assert_cmpint (g_get_monotonic_time () - start_time, >, 0);
842     g_clear_error (&error);
843
844     /* Try a blocking read, cancelled from another thread. */
845     g_socket_set_timeout (client, 0);
846     cancellable_thread = g_thread_new ("cancellable",
847                                        cancellable_thread_cb,
848                                        g_object_ref (cancellable));
849
850     start_time = g_get_monotonic_time ();
851     len = g_socket_receive_messages (client, &im, 1, 0  /* flags */,
852                                      cancellable, &error);
853     g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
854     g_assert_cmpint (len, ==, -1);
855     g_assert_cmpint (g_get_monotonic_time () - start_time, >, 0);
856     g_clear_error (&error);
857
858     g_thread_join (cancellable_thread);
859   }
860
861   g_socket_close (client, &error);
862   g_assert_no_error (error);
863
864   g_object_unref (client);
865   g_object_unref (cancellable);
866 }
867
868 static void
869 test_ipv4_sync_dgram_timeouts (void)
870 {
871   test_ip_sync_dgram_timeouts (G_SOCKET_FAMILY_IPV4);
872 }
873
874 static void
875 test_ipv6_sync_dgram_timeouts (void)
876 {
877   if (!ipv6_supported)
878     {
879       g_test_skip ("No support for IPv6");
880       return;
881     }
882
883   test_ip_sync_dgram_timeouts (G_SOCKET_FAMILY_IPV6);
884 }
885
886 static gpointer
887 graceful_server_thread (gpointer user_data)
888 {
889   IPTestData *data = user_data;
890   GSocket *sock;
891   GError *error = NULL;
892   gssize len;
893
894   sock = g_socket_accept (data->server, NULL, &error);
895   g_assert_no_error (error);
896
897   len = g_socket_send (sock, testbuf, strlen (testbuf) + 1, NULL, &error);
898   g_assert_no_error (error);
899   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
900
901   return sock;
902 }
903
904 static void
905 test_close_graceful (void)
906 {
907   GSocketFamily family = G_SOCKET_FAMILY_IPV4;
908   IPTestData *data;
909   GError *error = NULL;
910   GSocket *client, *server;
911   GSocketAddress *addr;
912   gssize len;
913   gchar buf[128];
914
915   data = create_server (family, graceful_server_thread, FALSE);
916   addr = g_socket_get_local_address (data->server, &error);
917   g_assert_no_error (error);
918
919   client = g_socket_new (family,
920                          G_SOCKET_TYPE_STREAM,
921                          G_SOCKET_PROTOCOL_DEFAULT,
922                          &error);
923   g_assert_no_error (error);
924
925   g_assert_cmpint (g_socket_get_family (client), ==, family);
926   g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
927   g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
928
929   g_socket_set_blocking (client, TRUE);
930   g_socket_set_timeout (client, 1);
931
932   g_socket_connect (client, addr, NULL, &error);
933   g_assert_no_error (error);
934   g_assert (g_socket_is_connected (client));
935   g_object_unref (addr);
936
937   server = g_thread_join (data->thread);
938
939   /* similar to g_tcp_connection_set_graceful_disconnect(), but explicit */
940   g_socket_shutdown (server, FALSE, TRUE, &error);
941   g_assert_no_error (error);
942
943   /* we must timeout */
944   g_socket_condition_wait (client, G_IO_HUP, NULL, &error);
945   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
946   g_clear_error (&error);
947
948   /* check that the remaining data is received */
949   len = g_socket_receive (client, buf, strlen (testbuf) + 1, NULL, &error);
950   g_assert_no_error (error);
951   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
952
953   /* and only then the connection is closed */
954   len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
955   g_assert_no_error (error);
956   g_assert_cmpint (len, ==, 0);
957
958   g_socket_close (server, &error);
959   g_assert_no_error (error);
960
961   g_socket_close (client, &error);
962   g_assert_no_error (error);
963
964   g_object_unref (server);
965   g_object_unref (data->server);
966   g_object_unref (client);
967
968   g_slice_free (IPTestData, data);
969 }
970
971 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
972 static gpointer
973 v4mapped_server_thread (gpointer user_data)
974 {
975   IPTestData *data = user_data;
976   GSocket *sock;
977   GError *error = NULL;
978   GSocketAddress *addr;
979
980   sock = g_socket_accept (data->server, NULL, &error);
981   g_assert_no_error (error);
982
983   g_assert_cmpint (g_socket_get_family (sock), ==, G_SOCKET_FAMILY_IPV6);
984
985   addr = g_socket_get_local_address (sock, &error);
986   g_assert_no_error (error);
987   g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
988   g_object_unref (addr);
989
990   addr = g_socket_get_remote_address (sock, &error);
991   g_assert_no_error (error);
992   g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
993   g_object_unref (addr);
994
995   g_socket_close (sock, &error);
996   g_assert_no_error (error);
997   g_object_unref (sock);
998   return NULL;
999 }
1000
1001 static void
1002 test_ipv6_v4mapped (void)
1003 {
1004   IPTestData *data;
1005   GError *error = NULL;
1006   GSocket *client;
1007   GSocketAddress *addr, *v4addr;
1008   GInetAddress *iaddr;
1009
1010   if (!ipv6_supported)
1011     {
1012       g_test_skip ("No support for IPv6");
1013       return;
1014     }
1015
1016   data = create_server (G_SOCKET_FAMILY_IPV6, v4mapped_server_thread, TRUE);
1017
1018   if (data == NULL)
1019     {
1020       g_test_message ("Test not run: not supported by the OS");
1021       return;
1022     }
1023
1024   client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1025                          G_SOCKET_TYPE_STREAM,
1026                          G_SOCKET_PROTOCOL_DEFAULT,
1027                          &error);
1028   g_assert_no_error (error);
1029
1030   g_socket_set_blocking (client, TRUE);
1031   g_socket_set_timeout (client, 1);
1032
1033   addr = g_socket_get_local_address (data->server, &error);
1034   g_assert_no_error (error);
1035   iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1036   v4addr = g_inet_socket_address_new (iaddr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
1037   g_object_unref (iaddr);
1038   g_object_unref (addr);
1039
1040   g_socket_connect (client, v4addr, NULL, &error);
1041   g_assert_no_error (error);
1042   g_assert (g_socket_is_connected (client));
1043
1044   g_thread_join (data->thread);
1045
1046   g_socket_close (client, &error);
1047   g_assert_no_error (error);
1048   g_socket_close (data->server, &error);
1049   g_assert_no_error (error);
1050
1051   g_object_unref (data->server);
1052   g_object_unref (client);
1053   g_object_unref (v4addr);
1054
1055   g_slice_free (IPTestData, data);
1056 }
1057 #endif
1058
1059 static void
1060 test_timed_wait (void)
1061 {
1062   IPTestData *data;
1063   GError *error = NULL;
1064   GSocket *client;
1065   GSocketAddress *addr;
1066   gint64 start_time;
1067   gint poll_duration;
1068
1069   data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE);
1070   addr = g_socket_get_local_address (data->server, &error);
1071   g_assert_no_error (error);
1072
1073   client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1074                          G_SOCKET_TYPE_STREAM,
1075                          G_SOCKET_PROTOCOL_DEFAULT,
1076                          &error);
1077   g_assert_no_error (error);
1078
1079   g_socket_set_blocking (client, TRUE);
1080   g_socket_set_timeout (client, 1);
1081
1082   g_socket_connect (client, addr, NULL, &error);
1083   g_assert_no_error (error);
1084   g_object_unref (addr);
1085
1086   start_time = g_get_monotonic_time ();
1087   g_socket_condition_timed_wait (client, G_IO_IN, 100000 /* 100 ms */,
1088                                  NULL, &error);
1089   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
1090   g_clear_error (&error);
1091   poll_duration = g_get_monotonic_time () - start_time;
1092
1093   g_assert_cmpint (poll_duration, >=, 98000);
1094   g_assert_cmpint (poll_duration, <, 112000);
1095
1096   g_socket_close (client, &error);
1097   g_assert_no_error (error);
1098
1099   g_thread_join (data->thread);
1100
1101   g_socket_close (data->server, &error);
1102   g_assert_no_error (error);
1103
1104   g_object_unref (data->server);
1105   g_object_unref (client);
1106
1107   g_slice_free (IPTestData, data);
1108 }
1109
1110 static int
1111 duplicate_fd (int fd)
1112 {
1113 #ifdef G_OS_WIN32
1114   HANDLE newfd;
1115
1116   if (!DuplicateHandle (GetCurrentProcess (),
1117                         (HANDLE)fd,
1118                         GetCurrentProcess (),
1119                         &newfd,
1120                         0,
1121                         FALSE,
1122                         DUPLICATE_SAME_ACCESS))
1123     {
1124       return -1;
1125     }
1126
1127   return (int)newfd;
1128 #else
1129   return dup (fd);
1130 #endif
1131 }
1132
1133 static void
1134 test_fd_reuse (void)
1135 {
1136   IPTestData *data;
1137   GError *error = NULL;
1138   GSocket *client;
1139   GSocket *client2;
1140   GSocketAddress *addr;
1141   int fd;
1142   gssize len;
1143   gchar buf[128];
1144
1145   g_test_bug ("741707");
1146
1147   data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE);
1148   addr = g_socket_get_local_address (data->server, &error);
1149   g_assert_no_error (error);
1150
1151   client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1152                          G_SOCKET_TYPE_STREAM,
1153                          G_SOCKET_PROTOCOL_DEFAULT,
1154                          &error);
1155   g_assert_no_error (error);
1156
1157   g_socket_set_blocking (client, TRUE);
1158   g_socket_set_timeout (client, 1);
1159
1160   g_socket_connect (client, addr, NULL, &error);
1161   g_assert_no_error (error);
1162   g_assert (g_socket_is_connected (client));
1163   g_object_unref (addr);
1164
1165   /* we have to dup otherwise the fd gets closed twice on unref */
1166   fd = duplicate_fd (g_socket_get_fd (client));
1167   client2 = g_socket_new_from_fd (fd, &error);
1168   g_assert_no_error (error);
1169
1170   g_assert_cmpint (g_socket_get_family (client2), ==, g_socket_get_family (client));
1171   g_assert_cmpint (g_socket_get_socket_type (client2), ==, g_socket_get_socket_type (client));
1172   g_assert_cmpint (g_socket_get_protocol (client2), ==, G_SOCKET_PROTOCOL_TCP);
1173
1174   len = g_socket_send (client2, testbuf, strlen (testbuf) + 1, NULL, &error);
1175   g_assert_no_error (error);
1176   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
1177
1178   len = g_socket_receive (client2, buf, sizeof (buf), NULL, &error);
1179   g_assert_no_error (error);
1180   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
1181
1182   g_assert_cmpstr (testbuf, ==, buf);
1183
1184   g_socket_shutdown (client, FALSE, TRUE, &error);
1185   g_assert_no_error (error);
1186   /* The semantics of dup()+shutdown() are ambiguous; this call will succeed
1187    * on Linux, but return ENOTCONN on OS X.
1188    */
1189   g_socket_shutdown (client2, FALSE, TRUE, NULL);
1190
1191   g_thread_join (data->thread);
1192
1193   g_socket_close (client, &error);
1194   g_assert_no_error (error);
1195   g_socket_close (client2, &error);
1196   g_assert_no_error (error);
1197   g_socket_close (data->server, &error);
1198   g_assert_no_error (error);
1199
1200   g_assert_cmpint (g_socket_get_fd (client), ==, -1);
1201   g_assert_cmpint (g_socket_get_fd (client2), ==, -1);
1202   g_assert_cmpint (g_socket_get_fd (data->server), ==, -1);
1203
1204   g_object_unref (data->server);
1205   g_object_unref (client);
1206   g_object_unref (client2);
1207
1208   g_slice_free (IPTestData, data);
1209 }
1210
1211 static void
1212 test_sockaddr (void)
1213 {
1214   struct sockaddr_in6 sin6, gsin6;
1215   GSocketAddress *saddr;
1216   GInetSocketAddress *isaddr;
1217   GInetAddress *iaddr;
1218   GError *error = NULL;
1219
1220   memset (&sin6, 0, sizeof (sin6));
1221   sin6.sin6_family = AF_INET6;
1222   sin6.sin6_addr = in6addr_loopback;
1223   sin6.sin6_port = g_htons (42);
1224   sin6.sin6_scope_id = 17;
1225   sin6.sin6_flowinfo = 1729;
1226
1227   saddr = g_socket_address_new_from_native (&sin6, sizeof (sin6));
1228   g_assert (G_IS_INET_SOCKET_ADDRESS (saddr));
1229
1230   isaddr = G_INET_SOCKET_ADDRESS (saddr);
1231   iaddr = g_inet_socket_address_get_address (isaddr);
1232   g_assert_cmpint (g_inet_address_get_family (iaddr), ==, G_SOCKET_FAMILY_IPV6);
1233   g_assert (g_inet_address_get_is_loopback (iaddr));
1234
1235   g_assert_cmpint (g_inet_socket_address_get_port (isaddr), ==, 42);
1236   g_assert_cmpint (g_inet_socket_address_get_scope_id (isaddr), ==, 17);
1237   g_assert_cmpint (g_inet_socket_address_get_flowinfo (isaddr), ==, 1729);
1238
1239   g_socket_address_to_native (saddr, &gsin6, sizeof (gsin6), &error);
1240   g_assert_no_error (error);
1241
1242   g_assert (memcmp (&sin6.sin6_addr, &gsin6.sin6_addr, sizeof (struct in6_addr)) == 0);
1243   g_assert_cmpint (sin6.sin6_port, ==, gsin6.sin6_port);
1244   g_assert_cmpint (sin6.sin6_scope_id, ==, gsin6.sin6_scope_id);
1245   g_assert_cmpint (sin6.sin6_flowinfo, ==, gsin6.sin6_flowinfo);
1246
1247   g_object_unref (saddr);
1248 }
1249
1250 #ifdef G_OS_UNIX
1251 static void
1252 test_unix_from_fd (void)
1253 {
1254   gint fd;
1255   GError *error;
1256   GSocket *s;
1257
1258   fd = socket (AF_UNIX, SOCK_STREAM, 0);
1259   g_assert_cmpint (fd, !=, -1);
1260
1261   error = NULL;
1262   s = g_socket_new_from_fd (fd, &error);
1263   g_assert_no_error (error);
1264   g_assert_cmpint (g_socket_get_family (s), ==, G_SOCKET_FAMILY_UNIX);
1265   g_assert_cmpint (g_socket_get_socket_type (s), ==, G_SOCKET_TYPE_STREAM);
1266   g_assert_cmpint (g_socket_get_protocol (s), ==, G_SOCKET_PROTOCOL_DEFAULT);
1267   g_object_unref (s);
1268 }
1269
1270 static void
1271 test_unix_connection (void)
1272 {
1273   gint fd;
1274   GError *error;
1275   GSocket *s;
1276   GSocketConnection *c;
1277
1278   fd = socket (AF_UNIX, SOCK_STREAM, 0);
1279   g_assert_cmpint (fd, !=, -1);
1280
1281   error = NULL;
1282   s = g_socket_new_from_fd (fd, &error);
1283   g_assert_no_error (error);
1284   c = g_socket_connection_factory_create_connection (s);
1285   g_assert (G_IS_UNIX_CONNECTION (c));
1286   g_object_unref (c);
1287   g_object_unref (s);
1288 }
1289
1290 static GSocketConnection *
1291 create_connection_for_fd (int fd)
1292 {
1293   GError *err = NULL;
1294   GSocket *socket;
1295   GSocketConnection *connection;
1296
1297   socket = g_socket_new_from_fd (fd, &err);
1298   g_assert_no_error (err);
1299   g_assert (G_IS_SOCKET (socket));
1300   connection = g_socket_connection_factory_create_connection (socket);
1301   g_assert (G_IS_UNIX_CONNECTION (connection));
1302   g_object_unref (socket);
1303   return connection;
1304 }
1305
1306 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
1307
1308 static void
1309 test_unix_connection_ancillary_data (void)
1310 {
1311   GError *err = NULL;
1312   gint pv[2], sv[3];
1313   gint status, fd, len;
1314   char buffer[1024];
1315   pid_t pid;
1316
1317   status = pipe (pv);
1318   g_assert_cmpint (status, ==, 0);
1319
1320   status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
1321   g_assert_cmpint (status, ==, 0);
1322
1323   pid = fork ();
1324   g_assert_cmpint (pid, >=, 0);
1325
1326   /* Child: close its copy of the write end of the pipe, receive it
1327    * again from the parent over the socket, and write some text to it.
1328    *
1329    * Parent: send the write end of the pipe (still open for the
1330    * parent) over the socket, close it, and read some text from the
1331    * read end of the pipe.
1332    */
1333   if (pid == 0)
1334     {
1335       GSocketConnection *connection;
1336
1337       close (sv[1]);
1338       connection = create_connection_for_fd (sv[0]);
1339
1340       status = close (pv[1]);
1341       g_assert_cmpint (status, ==, 0);
1342
1343       err = NULL;
1344       fd = g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection), NULL,
1345                                          &err);
1346       g_assert_no_error (err);
1347       g_assert_cmpint (fd, >, -1);
1348       g_object_unref (connection);
1349
1350       do
1351         len = write (fd, TEST_DATA, sizeof (TEST_DATA));
1352       while (len == -1 && errno == EINTR);
1353       g_assert_cmpint (len, ==, sizeof (TEST_DATA));
1354       exit (0);
1355     }
1356   else
1357     {
1358       GSocketConnection *connection;
1359
1360       close (sv[0]);
1361       connection = create_connection_for_fd (sv[1]);
1362
1363       err = NULL;
1364       g_unix_connection_send_fd (G_UNIX_CONNECTION (connection), pv[1], NULL,
1365                                  &err);
1366       g_assert_no_error (err);
1367       g_object_unref (connection);
1368
1369       status = close (pv[1]);
1370       g_assert_cmpint (status, ==, 0);
1371
1372       memset (buffer, 0xff, sizeof buffer);
1373       do
1374         len = read (pv[0], buffer, sizeof buffer);
1375       while (len == -1 && errno == EINTR);
1376
1377       g_assert_cmpint (len, ==, sizeof (TEST_DATA));
1378       g_assert_cmpstr (buffer, ==, TEST_DATA);
1379
1380       waitpid (pid, &status, 0);
1381       g_assert (WIFEXITED (status));
1382       g_assert_cmpint (WEXITSTATUS (status), ==, 0);
1383     }
1384
1385   /* TODO: add test for g_unix_connection_send_credentials() and
1386    * g_unix_connection_receive_credentials().
1387    */
1388 }
1389 #endif /* G_OS_UNIX */
1390
1391 static void
1392 test_reuse_tcp (void)
1393 {
1394   GSocket *sock1, *sock2;
1395   GError *error = NULL;
1396   GInetAddress *iaddr;
1397   GSocketAddress *addr;
1398
1399   sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1400                         G_SOCKET_TYPE_STREAM,
1401                         G_SOCKET_PROTOCOL_DEFAULT,
1402                         &error);
1403   g_assert_no_error (error);
1404
1405   iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1406   addr = g_inet_socket_address_new (iaddr, 0);
1407   g_object_unref (iaddr);
1408   g_socket_bind (sock1, addr, TRUE, &error);
1409   g_object_unref (addr);
1410   g_assert_no_error (error);
1411
1412   g_socket_listen (sock1, &error);
1413   g_assert_no_error (error);
1414
1415   sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1416                         G_SOCKET_TYPE_STREAM,
1417                         G_SOCKET_PROTOCOL_DEFAULT,
1418                         &error);
1419   g_assert_no_error (error);
1420
1421   addr = g_socket_get_local_address (sock1, &error);
1422   g_assert_no_error (error);
1423   g_socket_bind (sock2, addr, TRUE, &error);
1424   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_ADDRESS_IN_USE);
1425   g_clear_error (&error);
1426   g_object_unref (addr);
1427
1428   g_object_unref (sock1);
1429   g_object_unref (sock2);
1430 }
1431
1432 static void
1433 test_reuse_udp (void)
1434 {
1435   GSocket *sock1, *sock2;
1436   GError *error = NULL;
1437   GInetAddress *iaddr;
1438   GSocketAddress *addr;
1439
1440   sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1441                         G_SOCKET_TYPE_DATAGRAM,
1442                         G_SOCKET_PROTOCOL_DEFAULT,
1443                         &error);
1444   g_assert_no_error (error);
1445
1446   iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1447   addr = g_inet_socket_address_new (iaddr, 0);
1448   g_object_unref (iaddr);
1449   g_socket_bind (sock1, addr, TRUE, &error);
1450   g_object_unref (addr);
1451   g_assert_no_error (error);
1452
1453   sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1454                         G_SOCKET_TYPE_DATAGRAM,
1455                         G_SOCKET_PROTOCOL_DEFAULT,
1456                         &error);
1457   g_assert_no_error (error);
1458
1459   addr = g_socket_get_local_address (sock1, &error);
1460   g_assert_no_error (error);
1461   g_socket_bind (sock2, addr, TRUE, &error);
1462   g_object_unref (addr);
1463   g_assert_no_error (error);
1464
1465   g_object_unref (sock1);
1466   g_object_unref (sock2);
1467 }
1468
1469 static void
1470 test_get_available (gconstpointer user_data)
1471 {
1472   GSocketType socket_type = GPOINTER_TO_UINT (user_data);
1473   GError *err = NULL;
1474   GSocket *listener, *server, *client;
1475   GInetAddress *addr;
1476   GSocketAddress *saddr;
1477   gchar data[] = "0123456789abcdef";
1478   gchar buf[34];
1479   gssize nread;
1480
1481   listener = g_socket_new (G_SOCKET_FAMILY_IPV4,
1482                            socket_type,
1483                            G_SOCKET_PROTOCOL_DEFAULT,
1484                            &err);
1485   g_assert_no_error (err);
1486   g_assert (G_IS_SOCKET (listener));
1487
1488   client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1489                          socket_type,
1490                          G_SOCKET_PROTOCOL_DEFAULT,
1491                          &err);
1492   g_assert_no_error (err);
1493   g_assert (G_IS_SOCKET (client));
1494
1495   if (socket_type == G_SOCKET_TYPE_STREAM)
1496     {
1497       g_socket_set_option (client, IPPROTO_TCP, TCP_NODELAY, TRUE, &err);
1498       g_assert_no_error (err);
1499     }
1500
1501   addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
1502   saddr = g_inet_socket_address_new (addr, 0);
1503
1504   g_socket_bind (listener, saddr, TRUE, &err);
1505   g_assert_no_error (err);
1506   g_object_unref (saddr);
1507   g_object_unref (addr);
1508
1509   saddr = g_socket_get_local_address (listener, &err);
1510   g_assert_no_error (err);
1511
1512   if (socket_type == G_SOCKET_TYPE_STREAM)
1513     {
1514       g_socket_listen (listener, &err);
1515       g_assert_no_error (err);
1516       g_socket_connect (client, saddr, NULL, &err);
1517       g_assert_no_error (err);
1518
1519       server = g_socket_accept (listener, NULL, &err);
1520       g_assert_no_error (err);
1521       g_socket_set_blocking (server, FALSE);
1522       g_object_unref (listener);
1523     }
1524   else
1525     server = listener;
1526
1527   g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
1528   g_assert_no_error (err);
1529
1530   while (!g_socket_condition_wait (server, G_IO_IN, NULL, NULL))
1531     ;
1532   g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1533
1534   g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
1535   g_assert_no_error (err);
1536
1537   /* We need to wait until the data has actually been copied into the
1538    * server socket's buffers, but g_socket_condition_wait() won't help
1539    * here since the socket is definitely already readable. So there's
1540    * a race condition in checking its available bytes. In the TCP
1541    * case, we poll for a bit until the new data shows up. In the UDP
1542    * case, there's not much we can do, but at least the failure mode
1543    * is passes-when-it-shouldn't, not fails-when-it-shouldn't.
1544    */
1545   if (socket_type == G_SOCKET_TYPE_STREAM)
1546     {
1547       int tries;
1548
1549       for (tries = 0; tries < 100; tries++)
1550         {
1551           if (g_socket_get_available_bytes (server) > sizeof (data))
1552             break;
1553           g_usleep (100000);
1554         }
1555
1556       g_assert_cmpint (g_socket_get_available_bytes (server), ==, 2 * sizeof (data));
1557     }
1558   else
1559     {
1560       g_usleep (100000);
1561       g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1562     }
1563
1564   g_assert_cmpint (sizeof (buf), >=, 2 * sizeof (data));
1565   nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
1566   g_assert_no_error (err);
1567
1568   if (socket_type == G_SOCKET_TYPE_STREAM)
1569     {
1570       g_assert_cmpint (nread, ==, 2 * sizeof (data));
1571       g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1572     }
1573   else
1574     {
1575       g_assert_cmpint (nread, ==, sizeof (data));
1576       g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1577     }
1578
1579   nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
1580   if (socket_type == G_SOCKET_TYPE_STREAM)
1581     {
1582       g_assert_cmpint (nread, ==, -1);
1583       g_assert_error (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
1584       g_clear_error (&err);
1585     }
1586   else
1587     {
1588       g_assert_cmpint (nread, ==, sizeof (data));
1589       g_assert_no_error (err);
1590     }
1591
1592   g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1593
1594   g_socket_close (server, &err);
1595   g_assert_no_error (err);
1596
1597   g_object_unref (saddr);
1598   g_object_unref (server);
1599   g_object_unref (client);
1600 }
1601
1602 int
1603 main (int   argc,
1604       char *argv[])
1605 {
1606   GSocket *sock;
1607   GError *error = NULL;
1608
1609   g_test_init (&argc, &argv, NULL);
1610   g_test_bug_base ("https://bugzilla.gnome.org/");
1611
1612   sock = g_socket_new (G_SOCKET_FAMILY_IPV6,
1613                        G_SOCKET_TYPE_STREAM,
1614                        G_SOCKET_PROTOCOL_DEFAULT,
1615                        &error);
1616   if (sock != NULL)
1617     {
1618       ipv6_supported = TRUE;
1619       g_object_unref (sock);
1620     }
1621   else
1622     {
1623       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
1624       g_clear_error (&error);
1625     }
1626
1627   g_test_add_func ("/socket/ipv4_sync", test_ipv4_sync);
1628   g_test_add_func ("/socket/ipv4_async", test_ipv4_async);
1629   g_test_add_func ("/socket/ipv6_sync", test_ipv6_sync);
1630   g_test_add_func ("/socket/ipv6_async", test_ipv6_async);
1631   g_test_add_func ("/socket/ipv4_sync/datagram", test_ipv4_sync_dgram);
1632   g_test_add_func ("/socket/ipv4_sync/datagram/timeouts", test_ipv4_sync_dgram_timeouts);
1633   g_test_add_func ("/socket/ipv6_sync/datagram", test_ipv6_sync_dgram);
1634   g_test_add_func ("/socket/ipv6_sync/datagram/timeouts", test_ipv6_sync_dgram_timeouts);
1635 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
1636   g_test_add_func ("/socket/ipv6_v4mapped", test_ipv6_v4mapped);
1637 #endif
1638   g_test_add_func ("/socket/close_graceful", test_close_graceful);
1639   g_test_add_func ("/socket/timed_wait", test_timed_wait);
1640   g_test_add_func ("/socket/fd_reuse", test_fd_reuse);
1641   g_test_add_func ("/socket/address", test_sockaddr);
1642 #ifdef G_OS_UNIX
1643   g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
1644   g_test_add_func ("/socket/unix-connection", test_unix_connection);
1645   g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data);
1646 #endif
1647   g_test_add_func ("/socket/reuse/tcp", test_reuse_tcp);
1648   g_test_add_func ("/socket/reuse/udp", test_reuse_udp);
1649   g_test_add_data_func ("/socket/get_available/datagram", GUINT_TO_POINTER (G_SOCKET_TYPE_DATAGRAM),
1650                         test_get_available);
1651   g_test_add_data_func ("/socket/get_available/stream", GUINT_TO_POINTER (G_SOCKET_TYPE_STREAM),
1652                         test_get_available);
1653
1654   return g_test_run();
1655 }
1656