Imported Upstream version 2.56.2
[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                     GError        **error)
116 {
117   IPTestData *data;
118   GSocket *server;
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   if (server == NULL)
130     goto error;
131
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);
135
136   g_socket_set_blocking (server, TRUE);
137
138 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
139   if (v4mapped)
140     {
141       g_socket_set_option (data->server, IPPROTO_IPV6, IPV6_V6ONLY, FALSE, NULL);
142       if (!g_socket_speaks_ipv4 (data->server))
143         goto error;
144     }
145 #endif
146
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);
153
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))
156     {
157       g_object_unref (addr);
158       goto error;
159     }
160   g_object_unref (addr);
161
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);
167
168   if (socket_type == G_SOCKET_TYPE_STREAM)
169     {
170       if (!g_socket_listen (server, error))
171         goto error;
172     }
173   else
174     {
175       data->cancellable = g_cancellable_new ();
176     }
177
178   data->thread = g_thread_new ("server", server_thread, data);
179
180   return data;
181
182 error:
183   g_clear_object (&data->server);
184   g_slice_free (IPTestData, data);
185
186   return NULL;
187 }
188
189 static IPTestData *
190 create_server (GSocketFamily   family,
191                GThreadFunc     server_thread,
192                gboolean        v4mapped,
193                GError        **error)
194 {
195   return create_server_full (family, G_SOCKET_TYPE_STREAM, server_thread, v4mapped, error);
196 }
197
198 static const gchar *testbuf = "0123456789abcdef";
199
200 static gboolean
201 test_ip_async_read_ready (GSocket      *client,
202                           GIOCondition  cond,
203                           gpointer      user_data)
204 {
205   IPTestData *data = user_data;
206   GError *error = NULL;
207   gssize len;
208   gchar buf[128];
209
210   g_assert_cmpint (cond, ==, G_IO_IN);
211
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);
215
216   g_assert_cmpstr (testbuf, ==, buf);
217
218   g_main_loop_quit (data->loop);
219
220   return FALSE;
221 }
222
223 static gboolean
224 test_ip_async_write_ready (GSocket      *client,
225                            GIOCondition  cond,
226                            gpointer      user_data)
227 {
228   IPTestData *data = user_data;
229   GError *error = NULL;
230   GSource *source;
231   gssize len;
232
233   g_assert_cmpint (cond, ==, G_IO_OUT);
234
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);
238
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);
244
245   return FALSE;
246 }
247
248 static gboolean
249 test_ip_async_timed_out (GSocket      *client,
250                          GIOCondition  cond,
251                          gpointer      user_data)
252 {
253   IPTestData *data = user_data;
254   GError *error = NULL;
255   GSource *source;
256   gssize len;
257   gchar buf[128];
258
259   if (data->family == G_SOCKET_FAMILY_IPV4)
260     {
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);
266     }
267
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);
273
274   return FALSE;
275 }
276
277 static gboolean
278 test_ip_async_connected (GSocket      *client,
279                          GIOCondition  cond,
280                          gpointer      user_data)
281 {
282   IPTestData *data = user_data;
283   GError *error = NULL;
284   GSource *source;
285   gssize len;
286   gchar buf[128];
287
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.
292    */
293   g_assert_cmpint (cond, ==, G_IO_OUT);
294
295   g_assert (g_socket_is_connected (client));
296
297   /* This adds 1 second to "make check", so let's just only do it once. */
298   if (data->family == G_SOCKET_FAMILY_IPV4)
299     {
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);
304
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);
310     }
311   else
312     test_ip_async_timed_out (client, 0, data);
313
314   return FALSE;
315 }
316
317 static gboolean
318 idle_test_ip_async_connected (gpointer user_data)
319 {
320   IPTestData *data = user_data;
321
322   return test_ip_async_connected (data->client, G_IO_OUT, data);
323 }
324
325 static void
326 test_ip_async (GSocketFamily family)
327 {
328   IPTestData *data;
329   GError *error = NULL;
330   GSocket *client;
331   GSocketAddress *addr;
332   GSource *source;
333   gssize len;
334   gchar buf[128];
335
336   data = create_server (family, echo_server_thread, FALSE, &error);
337   if (error != NULL)
338     {
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;
344     }
345
346   addr = g_socket_get_local_address (data->server, &error);
347   g_assert_no_error (error);
348
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;
355
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);
359
360   g_socket_set_blocking (client, FALSE);
361   g_socket_set_timeout (client, 1);
362
363   if (g_socket_connect (client, addr, NULL, &error))
364     {
365       g_assert_no_error (error);
366       g_idle_add (idle_test_ip_async_connected, data);
367     }
368   else
369     {
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);
377     }
378   g_object_unref (addr);
379
380   data->loop = g_main_loop_new (NULL, TRUE);
381   g_main_loop_run (data->loop);
382   g_main_loop_unref (data->loop);
383
384   g_socket_shutdown (client, FALSE, TRUE, &error);
385   g_assert_no_error (error);
386
387   g_thread_join (data->thread);
388
389   if (family == G_SOCKET_FAMILY_IPV4)
390     {
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);
396     }
397   else
398     {
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);
405     }
406
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);
411
412   g_object_unref (data->server);
413   g_object_unref (client);
414
415   g_slice_free (IPTestData, data);
416 }
417
418 static void
419 test_ipv4_async (void)
420 {
421   test_ip_async (G_SOCKET_FAMILY_IPV4);
422 }
423
424 static void
425 test_ipv6_async (void)
426 {
427   if (!ipv6_supported)
428     {
429       g_test_skip ("No support for IPv6");
430       return;
431     }
432
433   test_ip_async (G_SOCKET_FAMILY_IPV6);
434 }
435
436 static const gchar testbuf2[] = "0123456789abcdefghijklmnopqrstuvwxyz";
437
438 static void
439 test_ip_sync (GSocketFamily family)
440 {
441   IPTestData *data;
442   GError *error = NULL;
443   GSocket *client;
444   GSocketAddress *addr;
445   gssize len;
446   gchar buf[128];
447
448   data = create_server (family, echo_server_thread, FALSE, &error);
449   if (error != NULL)
450     {
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;
456     }
457
458   addr = g_socket_get_local_address (data->server, &error);
459   g_assert_no_error (error);
460
461   client = g_socket_new (family,
462                          G_SOCKET_TYPE_STREAM,
463                          G_SOCKET_PROTOCOL_DEFAULT,
464                          &error);
465   g_assert_no_error (error);
466
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);
470
471   g_socket_set_blocking (client, TRUE);
472   g_socket_set_timeout (client, 1);
473
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);
478
479   /* This adds 1 second to "make check", so let's just only do it once. */
480   if (family == G_SOCKET_FAMILY_IPV4)
481     {
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);
486     }
487
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);
491   
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);
495
496   g_assert_cmpstr (testbuf, ==, buf);
497
498   {
499     GOutputVector v[7] = { { NULL, }, };
500
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);
515
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));
519
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);
525   }
526
527   g_socket_shutdown (client, FALSE, TRUE, &error);
528   g_assert_no_error (error);
529
530   g_thread_join (data->thread);
531
532   if (family == G_SOCKET_FAMILY_IPV4)
533     {
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);
538     }
539   else
540     {
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);
546     }
547
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);
552
553   g_object_unref (data->server);
554   g_object_unref (client);
555
556   g_slice_free (IPTestData, data);
557 }
558
559 static void
560 test_ipv4_sync (void)
561 {
562   test_ip_sync (G_SOCKET_FAMILY_IPV4);
563 }
564
565 static void
566 test_ipv6_sync (void)
567 {
568   if (!ipv6_supported)
569     {
570       g_test_skip ("No support for IPv6");
571       return;
572     }
573
574   test_ip_sync (G_SOCKET_FAMILY_IPV6);
575 }
576
577 static void
578 test_ip_sync_dgram (GSocketFamily family)
579 {
580   IPTestData *data;
581   GError *error = NULL;
582   GSocket *client;
583   GSocketAddress *dest_addr;
584   gssize len;
585   gchar buf[128];
586
587   data = create_server_full (family, G_SOCKET_TYPE_DATAGRAM,
588                              echo_server_dgram_thread, FALSE, &error);
589   if (error != NULL)
590     {
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;
596     }
597
598   dest_addr = g_socket_get_local_address (data->server, &error);
599
600   client = g_socket_new (family,
601                          G_SOCKET_TYPE_DATAGRAM,
602                          G_SOCKET_PROTOCOL_DEFAULT,
603                          &error);
604   g_assert_no_error (error);
605
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);
609
610   g_socket_set_blocking (client, TRUE);
611   g_socket_set_timeout (client, 1);
612
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);
616
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);
620
621   g_assert_cmpstr (testbuf, ==, buf);
622
623   {
624     GOutputMessage m[3] = { { NULL, }, };
625     GInputMessage im[3] = { { NULL, }, };
626     GOutputVector v[7] = { { NULL, }, };
627     GInputVector iv[7] = { { NULL, }, };
628
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);
643
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);
658
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));
662
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);
668
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;
678
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);
685
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);
690
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);
697
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);
703
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;
708
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);
716
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;
723
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));
729
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);
734
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;
739
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);
745
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);
749
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;
754
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);
763
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);
767
768     len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
769     g_assert_cmpint (len, ==, 3);
770   }
771
772   g_cancellable_cancel (data->cancellable);
773
774   g_thread_join (data->thread);
775
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);
780
781   g_object_unref (data->server);
782   g_object_unref (data->cancellable);
783   g_object_unref (client);
784   g_object_unref (dest_addr);
785
786   g_slice_free (IPTestData, data);
787 }
788
789 static void
790 test_ipv4_sync_dgram (void)
791 {
792   test_ip_sync_dgram (G_SOCKET_FAMILY_IPV4);
793 }
794
795 static void
796 test_ipv6_sync_dgram (void)
797 {
798   if (!ipv6_supported)
799     {
800       g_test_skip ("No support for IPv6");
801       return;
802     }
803
804   test_ip_sync_dgram (G_SOCKET_FAMILY_IPV6);
805 }
806
807 static gpointer
808 cancellable_thread_cb (gpointer data)
809 {
810   GCancellable *cancellable = data;
811
812   g_usleep (0.1 * G_USEC_PER_SEC);
813   g_cancellable_cancel (cancellable);
814   g_object_unref (cancellable);
815
816   return NULL;
817 }
818
819 static void
820 test_ip_sync_dgram_timeouts (GSocketFamily family)
821 {
822   GError *error = NULL;
823   GSocket *client = NULL;
824   GCancellable *cancellable = NULL;
825   GThread *cancellable_thread = NULL;
826   gssize len;
827
828   client = g_socket_new (family,
829                          G_SOCKET_TYPE_DATAGRAM,
830                          G_SOCKET_PROTOCOL_DEFAULT,
831                          &error);
832   g_assert_no_error (error);
833
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);
837
838   /* No overall timeout: test the per-operation timeouts instead. */
839   g_socket_set_timeout (client, 0);
840
841   cancellable = g_cancellable_new ();
842
843   /* Check for timeouts when no server is running. */
844   {
845     gint64 start_time;
846     GInputMessage im = { NULL, };
847     GInputVector iv = { NULL, };
848     guint8 buf[128];
849
850     iv.buffer = buf;
851     iv.size = sizeof (buf);
852
853     im.vectors = &iv;
854     im.num_vectors = 1;
855
856     memset (buf, 0, sizeof (buf));
857
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);
865
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);
877
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));
883
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);
891
892     g_thread_join (cancellable_thread);
893   }
894
895   g_socket_close (client, &error);
896   g_assert_no_error (error);
897
898   g_object_unref (client);
899   g_object_unref (cancellable);
900 }
901
902 static void
903 test_ipv4_sync_dgram_timeouts (void)
904 {
905   test_ip_sync_dgram_timeouts (G_SOCKET_FAMILY_IPV4);
906 }
907
908 static void
909 test_ipv6_sync_dgram_timeouts (void)
910 {
911   if (!ipv6_supported)
912     {
913       g_test_skip ("No support for IPv6");
914       return;
915     }
916
917   test_ip_sync_dgram_timeouts (G_SOCKET_FAMILY_IPV6);
918 }
919
920 static gpointer
921 graceful_server_thread (gpointer user_data)
922 {
923   IPTestData *data = user_data;
924   GSocket *sock;
925   GError *error = NULL;
926   gssize len;
927
928   sock = g_socket_accept (data->server, NULL, &error);
929   g_assert_no_error (error);
930
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);
934
935   return sock;
936 }
937
938 static void
939 test_close_graceful (void)
940 {
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];
948
949   data = create_server (family, graceful_server_thread, FALSE, &error);
950   if (error != NULL)
951     {
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;
957     }
958
959   addr = g_socket_get_local_address (data->server, &error);
960   g_assert_no_error (error);
961
962   client = g_socket_new (family,
963                          G_SOCKET_TYPE_STREAM,
964                          G_SOCKET_PROTOCOL_DEFAULT,
965                          &error);
966   g_assert_no_error (error);
967
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);
971
972   g_socket_set_blocking (client, TRUE);
973   g_socket_set_timeout (client, 1);
974
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);
979
980   server = g_thread_join (data->thread);
981
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);
985
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);
990
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);
995
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);
1000
1001   g_socket_close (server, &error);
1002   g_assert_no_error (error);
1003
1004   g_socket_close (client, &error);
1005   g_assert_no_error (error);
1006
1007   g_object_unref (server);
1008   g_object_unref (data->server);
1009   g_object_unref (client);
1010
1011   g_slice_free (IPTestData, data);
1012 }
1013
1014 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
1015 static gpointer
1016 v4mapped_server_thread (gpointer user_data)
1017 {
1018   IPTestData *data = user_data;
1019   GSocket *sock;
1020   GError *error = NULL;
1021   GSocketAddress *addr;
1022
1023   sock = g_socket_accept (data->server, NULL, &error);
1024   g_assert_no_error (error);
1025
1026   g_assert_cmpint (g_socket_get_family (sock), ==, G_SOCKET_FAMILY_IPV6);
1027
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);
1032
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);
1037
1038   g_socket_close (sock, &error);
1039   g_assert_no_error (error);
1040   g_object_unref (sock);
1041   return NULL;
1042 }
1043
1044 static void
1045 test_ipv6_v4mapped (void)
1046 {
1047   IPTestData *data;
1048   GError *error = NULL;
1049   GSocket *client;
1050   GSocketAddress *addr, *v4addr;
1051   GInetAddress *iaddr;
1052
1053   if (!ipv6_supported)
1054     {
1055       g_test_skip ("No support for IPv6");
1056       return;
1057     }
1058
1059   data = create_server (G_SOCKET_FAMILY_IPV6, v4mapped_server_thread, TRUE, &error);
1060   if (error != NULL)
1061     {
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;
1067     }
1068
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);
1074
1075   g_socket_set_blocking (client, TRUE);
1076   g_socket_set_timeout (client, 1);
1077
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);
1084
1085   g_socket_connect (client, v4addr, NULL, &error);
1086   g_assert_no_error (error);
1087   g_assert (g_socket_is_connected (client));
1088
1089   g_thread_join (data->thread);
1090
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);
1095
1096   g_object_unref (data->server);
1097   g_object_unref (client);
1098   g_object_unref (v4addr);
1099
1100   g_slice_free (IPTestData, data);
1101 }
1102 #endif
1103
1104 static void
1105 test_timed_wait (void)
1106 {
1107   IPTestData *data;
1108   GError *error = NULL;
1109   GSocket *client;
1110   GSocketAddress *addr;
1111   gint64 start_time;
1112   gint poll_duration;
1113
1114   data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE, &error);
1115   if (error != NULL)
1116     {
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;
1122     }
1123
1124   addr = g_socket_get_local_address (data->server, &error);
1125   g_assert_no_error (error);
1126
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);
1132
1133   g_socket_set_blocking (client, TRUE);
1134   g_socket_set_timeout (client, 1);
1135
1136   g_socket_connect (client, addr, NULL, &error);
1137   g_assert_no_error (error);
1138   g_object_unref (addr);
1139
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;
1146
1147   g_assert_cmpint (poll_duration, >=, 98000);
1148   g_assert_cmpint (poll_duration, <, 112000);
1149
1150   g_socket_close (client, &error);
1151   g_assert_no_error (error);
1152
1153   g_thread_join (data->thread);
1154
1155   g_socket_close (data->server, &error);
1156   g_assert_no_error (error);
1157
1158   g_object_unref (data->server);
1159   g_object_unref (client);
1160
1161   g_slice_free (IPTestData, data);
1162 }
1163
1164 static int
1165 duplicate_fd (int fd)
1166 {
1167 #ifdef G_OS_WIN32
1168   HANDLE newfd;
1169
1170   if (!DuplicateHandle (GetCurrentProcess (),
1171                         (HANDLE)fd,
1172                         GetCurrentProcess (),
1173                         &newfd,
1174                         0,
1175                         FALSE,
1176                         DUPLICATE_SAME_ACCESS))
1177     {
1178       return -1;
1179     }
1180
1181   return (int)newfd;
1182 #else
1183   return dup (fd);
1184 #endif
1185 }
1186
1187 static void
1188 test_fd_reuse (void)
1189 {
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];
1198
1199   g_test_bug ("741707");
1200
1201   data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE, &error);
1202   if (error != NULL)
1203     {
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;
1209     }
1210
1211   addr = g_socket_get_local_address (data->server, &error);
1212   g_assert_no_error (error);
1213
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);
1219
1220   g_socket_set_blocking (client, TRUE);
1221   g_socket_set_timeout (client, 1);
1222
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);
1227
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);
1232
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);
1236
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);
1240
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);
1244
1245   g_assert_cmpstr (testbuf, ==, buf);
1246
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.
1251    */
1252   g_socket_shutdown (client2, FALSE, TRUE, NULL);
1253
1254   g_thread_join (data->thread);
1255
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);
1262
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);
1266
1267   g_object_unref (data->server);
1268   g_object_unref (client);
1269   g_object_unref (client2);
1270
1271   g_slice_free (IPTestData, data);
1272 }
1273
1274 static void
1275 test_sockaddr (void)
1276 {
1277   struct sockaddr_in6 sin6, gsin6;
1278   GSocketAddress *saddr;
1279   GInetSocketAddress *isaddr;
1280   GInetAddress *iaddr;
1281   GError *error = NULL;
1282
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;
1289
1290   saddr = g_socket_address_new_from_native (&sin6, sizeof (sin6));
1291   g_assert (G_IS_INET_SOCKET_ADDRESS (saddr));
1292
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));
1297
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);
1301
1302   g_socket_address_to_native (saddr, &gsin6, sizeof (gsin6), &error);
1303   g_assert_no_error (error);
1304
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);
1309
1310   g_object_unref (saddr);
1311 }
1312
1313 #ifdef G_OS_UNIX
1314 static void
1315 test_unix_from_fd (void)
1316 {
1317   gint fd;
1318   GError *error;
1319   GSocket *s;
1320
1321   fd = socket (AF_UNIX, SOCK_STREAM, 0);
1322   g_assert_cmpint (fd, !=, -1);
1323
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);
1331 }
1332
1333 static void
1334 test_unix_connection (void)
1335 {
1336   gint fd;
1337   GError *error;
1338   GSocket *s;
1339   GSocketConnection *c;
1340
1341   fd = socket (AF_UNIX, SOCK_STREAM, 0);
1342   g_assert_cmpint (fd, !=, -1);
1343
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);
1351 }
1352
1353 static GSocketConnection *
1354 create_connection_for_fd (int fd)
1355 {
1356   GError *err = NULL;
1357   GSocket *socket;
1358   GSocketConnection *connection;
1359
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;
1367 }
1368
1369 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
1370
1371 static void
1372 test_unix_connection_ancillary_data (void)
1373 {
1374   GError *err = NULL;
1375   gint pv[2], sv[3];
1376   gint status, fd, len;
1377   char buffer[1024];
1378   pid_t pid;
1379
1380   status = pipe (pv);
1381   g_assert_cmpint (status, ==, 0);
1382
1383   status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
1384   g_assert_cmpint (status, ==, 0);
1385
1386   pid = fork ();
1387   g_assert_cmpint (pid, >=, 0);
1388
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.
1391    *
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.
1395    */
1396   if (pid == 0)
1397     {
1398       GSocketConnection *connection;
1399
1400       close (sv[1]);
1401       connection = create_connection_for_fd (sv[0]);
1402
1403       status = close (pv[1]);
1404       g_assert_cmpint (status, ==, 0);
1405
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);
1412
1413       do
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);
1418     }
1419   else
1420     {
1421       GSocketConnection *connection;
1422
1423       close (sv[0]);
1424       connection = create_connection_for_fd (sv[1]);
1425
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);
1431
1432       status = close (pv[1]);
1433       g_assert_cmpint (status, ==, 0);
1434
1435       memset (buffer, 0xff, sizeof buffer);
1436       do
1437         len = read (pv[0], buffer, sizeof buffer);
1438       while (len == -1 && errno == EINTR);
1439
1440       g_assert_cmpint (len, ==, sizeof (TEST_DATA));
1441       g_assert_cmpstr (buffer, ==, TEST_DATA);
1442
1443       waitpid (pid, &status, 0);
1444       g_assert (WIFEXITED (status));
1445       g_assert_cmpint (WEXITSTATUS (status), ==, 0);
1446     }
1447
1448   /* TODO: add test for g_unix_connection_send_credentials() and
1449    * g_unix_connection_receive_credentials().
1450    */
1451 }
1452
1453 static gboolean
1454 postmortem_source_cb (GSocket      *socket,
1455                       GIOCondition  condition,
1456                       gpointer      user_data)
1457 {
1458   gboolean *been_here = user_data;
1459
1460   g_assert_cmpint (condition, ==, G_IO_NVAL);
1461
1462   *been_here = TRUE;
1463   return FALSE;
1464 }
1465
1466 static void
1467 test_source_postmortem (void)
1468 {
1469   GMainContext *context;
1470   GSocket *socket;
1471   GSource *source;
1472   GError *error = NULL;
1473   gboolean callback_visited = FALSE;
1474
1475   socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error);
1476   g_assert_no_error (error);
1477
1478   context = g_main_context_new ();
1479
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);
1485
1486   g_socket_close (socket, &error);
1487   g_assert_no_error (error);
1488   g_object_unref (socket);
1489
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));
1495
1496   g_main_context_unref (context);
1497 }
1498
1499 #endif /* G_OS_UNIX */
1500
1501 static void
1502 test_reuse_tcp (void)
1503 {
1504   GSocket *sock1, *sock2;
1505   GError *error = NULL;
1506   GInetAddress *iaddr;
1507   GSocketAddress *addr;
1508
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);
1514
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);
1521
1522   g_socket_listen (sock1, &error);
1523   g_assert_no_error (error);
1524
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);
1530
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);
1537
1538   g_object_unref (sock1);
1539   g_object_unref (sock2);
1540 }
1541
1542 static void
1543 test_reuse_udp (void)
1544 {
1545   GSocket *sock1, *sock2;
1546   GError *error = NULL;
1547   GInetAddress *iaddr;
1548   GSocketAddress *addr;
1549
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);
1555
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);
1562
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);
1568
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);
1574
1575   g_object_unref (sock1);
1576   g_object_unref (sock2);
1577 }
1578
1579 static void
1580 test_get_available (gconstpointer user_data)
1581 {
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;
1590
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));
1597
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));
1604
1605   if (socket_type == G_SOCKET_TYPE_STREAM)
1606     {
1607       g_socket_set_option (client, IPPROTO_TCP, TCP_NODELAY, TRUE, &err);
1608       g_assert_no_error (err);
1609     }
1610
1611   addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
1612   saddr = g_inet_socket_address_new (addr, 0);
1613
1614   g_socket_bind (listener, saddr, TRUE, &err);
1615   g_assert_no_error (err);
1616   g_object_unref (saddr);
1617   g_object_unref (addr);
1618
1619   saddr = g_socket_get_local_address (listener, &err);
1620   g_assert_no_error (err);
1621
1622   if (socket_type == G_SOCKET_TYPE_STREAM)
1623     {
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);
1628
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);
1633     }
1634   else
1635     server = listener;
1636
1637   g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
1638   g_assert_no_error (err);
1639
1640   while (!g_socket_condition_wait (server, G_IO_IN, NULL, NULL))
1641     ;
1642   g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1643
1644   g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
1645   g_assert_no_error (err);
1646
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.
1654    */
1655   if (socket_type == G_SOCKET_TYPE_STREAM)
1656     {
1657       int tries;
1658
1659       for (tries = 0; tries < 100; tries++)
1660         {
1661           if (g_socket_get_available_bytes (server) > sizeof (data))
1662             break;
1663           g_usleep (100000);
1664         }
1665
1666       g_assert_cmpint (g_socket_get_available_bytes (server), ==, 2 * sizeof (data));
1667     }
1668   else
1669     {
1670       g_usleep (100000);
1671       g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1672     }
1673
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);
1677
1678   if (socket_type == G_SOCKET_TYPE_STREAM)
1679     {
1680       g_assert_cmpint (nread, ==, 2 * sizeof (data));
1681       g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1682     }
1683   else
1684     {
1685       g_assert_cmpint (nread, ==, sizeof (data));
1686       g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1687     }
1688
1689   nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
1690   if (socket_type == G_SOCKET_TYPE_STREAM)
1691     {
1692       g_assert_cmpint (nread, ==, -1);
1693       g_assert_error (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
1694       g_clear_error (&err);
1695     }
1696   else
1697     {
1698       g_assert_cmpint (nread, ==, sizeof (data));
1699       g_assert_no_error (err);
1700     }
1701
1702   g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1703
1704   g_socket_close (server, &err);
1705   g_assert_no_error (err);
1706
1707   g_object_unref (saddr);
1708   g_object_unref (server);
1709   g_object_unref (client);
1710 }
1711
1712 int
1713 main (int   argc,
1714       char *argv[])
1715 {
1716   GSocket *sock;
1717   GError *error = NULL;
1718
1719   g_test_init (&argc, &argv, NULL);
1720   g_test_bug_base ("https://bugzilla.gnome.org/");
1721
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)
1727     {
1728       ipv6_supported = TRUE;
1729       g_object_unref (sock);
1730     }
1731   else
1732     {
1733       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
1734       g_clear_error (&error);
1735     }
1736
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);
1764
1765   return g_test_run();
1766 }
1767