gsocket: add a testcase that shows a hang on win32
[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 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     GOutputVector v[7] = { { NULL, }, };
592
593     v[0].buffer = testbuf2 + 0;
594     v[0].size = 3;
595     v[1].buffer = testbuf2 + 3;
596     v[1].size = 5;
597     v[2].buffer = testbuf2 + 3 + 5;
598     v[2].size = 0;
599     v[3].buffer = testbuf2 + 3 + 5;
600     v[3].size = 6;
601     v[4].buffer = testbuf2 + 3 + 5 + 6;
602     v[4].size = 2;
603     v[5].buffer = testbuf2 + 3 + 5 + 6 + 2;
604     v[5].size = 1;
605     v[6].buffer = testbuf2 + 3 + 5 + 6 + 2 + 1;
606     v[6].size = strlen (testbuf2) - (3 + 5 + 6 + 2 + 1);
607
608     len = g_socket_send_message (client, dest_addr, v, G_N_ELEMENTS (v), NULL, 0, 0, NULL, &error);
609     g_assert_no_error (error);
610     g_assert_cmpint (len, ==, strlen (testbuf2));
611
612     memset (buf, 0, sizeof (buf));
613     len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
614     g_assert_no_error (error);
615     g_assert_cmpint (len, ==, strlen (testbuf2));
616     g_assert_cmpstr (testbuf2, ==, buf);
617
618     m[0].vectors = &v[0];
619     m[0].num_vectors = 1;
620     m[0].address = dest_addr;
621     m[1].vectors = &v[0];
622     m[1].num_vectors = 6;
623     m[1].address = dest_addr;
624     m[2].vectors = &v[6];
625     m[2].num_vectors = 1;
626     m[2].address = dest_addr;
627
628     len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
629     g_assert_no_error (error);
630     g_assert_cmpint (len, ==, G_N_ELEMENTS (m));
631     g_assert_cmpint (m[0].bytes_sent, ==, 3);
632     g_assert_cmpint (m[1].bytes_sent, ==, 17);
633     g_assert_cmpint (m[2].bytes_sent, ==, v[6].size);
634
635     memset (buf, 0, sizeof (buf));
636     len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
637     g_assert_no_error (error);
638     g_assert_cmpint (len, ==, 3);
639
640     memset (buf, 0, sizeof (buf));
641     len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
642     g_assert_no_error (error);
643     /* v[0].size + v[1].size + v[2].size + v[3].size + v[4].size + v[5].size */
644     g_assert_cmpint (len, ==, 17);
645     g_assert (memcmp (testbuf2, buf, 17) == 0);
646
647     memset (buf, 0, sizeof (buf));
648     len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
649     g_assert_no_error (error);
650     g_assert_cmpint (len, ==, v[6].size);
651     g_assert_cmpstr (buf, ==, v[6].buffer);
652
653     /* reset since we're re-using the message structs */
654     m[0].bytes_sent = 0;
655     m[1].bytes_sent = 0;
656     m[2].bytes_sent = 0;
657
658     /* now try to generate an error by omitting the destination address on [1] */
659     m[1].address = NULL;
660     len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error);
661     g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
662     g_clear_error (&error);
663     g_assert_cmpint (len, ==, -1);
664
665     g_assert_cmpint (m[0].bytes_sent, ==, 3);
666     g_assert_cmpint (m[1].bytes_sent, ==, 0);
667     g_assert_cmpint (m[2].bytes_sent, ==, 0);
668
669     len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
670     g_assert_cmpint (len, ==, 3);
671   }
672
673   g_cancellable_cancel (data->cancellable);
674
675   g_thread_join (data->thread);
676
677   g_socket_close (client, &error);
678   g_assert_no_error (error);
679   g_socket_close (data->server, &error);
680   g_assert_no_error (error);
681
682   g_object_unref (data->server);
683   g_object_unref (data->cancellable);
684   g_object_unref (client);
685   g_object_unref (dest_addr);
686
687   g_slice_free (IPTestData, data);
688 }
689
690 static void
691 test_ipv4_sync_dgram (void)
692 {
693   test_ip_sync_dgram (G_SOCKET_FAMILY_IPV4);
694 }
695
696 static void
697 test_ipv6_sync_dgram (void)
698 {
699   if (!ipv6_supported)
700     {
701       g_test_skip ("No support for IPv6");
702       return;
703     }
704
705   test_ip_sync_dgram (G_SOCKET_FAMILY_IPV6);
706 }
707
708 static gpointer
709 graceful_server_thread (gpointer user_data)
710 {
711   IPTestData *data = user_data;
712   GSocket *sock;
713   GError *error = NULL;
714   gssize len;
715
716   sock = g_socket_accept (data->server, NULL, &error);
717   g_assert_no_error (error);
718
719   len = g_socket_send (sock, testbuf, strlen (testbuf) + 1, NULL, &error);
720   g_assert_no_error (error);
721   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
722
723   return sock;
724 }
725
726 static void
727 test_close_graceful (void)
728 {
729   GSocketFamily family = G_SOCKET_FAMILY_IPV4;
730   IPTestData *data;
731   GError *error = NULL;
732   GSocket *client, *server;
733   GSocketAddress *addr;
734   gssize len;
735   gchar buf[128];
736
737   data = create_server (family, graceful_server_thread, FALSE);
738   addr = g_socket_get_local_address (data->server, &error);
739   g_assert_no_error (error);
740
741   client = g_socket_new (family,
742                          G_SOCKET_TYPE_STREAM,
743                          G_SOCKET_PROTOCOL_DEFAULT,
744                          &error);
745   g_assert_no_error (error);
746
747   g_assert_cmpint (g_socket_get_family (client), ==, family);
748   g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
749   g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
750
751   g_socket_set_blocking (client, TRUE);
752   g_socket_set_timeout (client, 1);
753
754   g_socket_connect (client, addr, NULL, &error);
755   g_assert_no_error (error);
756   g_assert (g_socket_is_connected (client));
757   g_object_unref (addr);
758
759   server = g_thread_join (data->thread);
760
761   /* similar to g_tcp_connection_set_graceful_disconnect(), but explicit */
762   g_socket_shutdown (server, FALSE, TRUE, &error);
763   g_assert_no_error (error);
764
765   /* we must timeout */
766   g_socket_condition_wait (client, G_IO_HUP, NULL, &error);
767   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
768   g_clear_error (&error);
769
770   /* check that the remaining data is received */
771   len = g_socket_receive (client, buf, strlen (testbuf) + 1, NULL, &error);
772   g_assert_no_error (error);
773   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
774
775   /* and only then the connection is closed */
776   len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
777   g_assert_no_error (error);
778   g_assert_cmpint (len, ==, 0);
779
780   g_socket_close (server, &error);
781   g_assert_no_error (error);
782
783   g_socket_close (client, &error);
784   g_assert_no_error (error);
785
786   g_object_unref (server);
787   g_object_unref (data->server);
788   g_object_unref (client);
789
790   g_slice_free (IPTestData, data);
791 }
792
793 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
794 static gpointer
795 v4mapped_server_thread (gpointer user_data)
796 {
797   IPTestData *data = user_data;
798   GSocket *sock;
799   GError *error = NULL;
800   GSocketAddress *addr;
801
802   sock = g_socket_accept (data->server, NULL, &error);
803   g_assert_no_error (error);
804
805   g_assert_cmpint (g_socket_get_family (sock), ==, G_SOCKET_FAMILY_IPV6);
806
807   addr = g_socket_get_local_address (sock, &error);
808   g_assert_no_error (error);
809   g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
810   g_object_unref (addr);
811
812   addr = g_socket_get_remote_address (sock, &error);
813   g_assert_no_error (error);
814   g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
815   g_object_unref (addr);
816
817   g_socket_close (sock, &error);
818   g_assert_no_error (error);
819   g_object_unref (sock);
820   return NULL;
821 }
822
823 static void
824 test_ipv6_v4mapped (void)
825 {
826   IPTestData *data;
827   GError *error = NULL;
828   GSocket *client;
829   GSocketAddress *addr, *v4addr;
830   GInetAddress *iaddr;
831
832   if (!ipv6_supported)
833     {
834       g_test_skip ("No support for IPv6");
835       return;
836     }
837
838   data = create_server (G_SOCKET_FAMILY_IPV6, v4mapped_server_thread, TRUE);
839
840   if (data == NULL)
841     {
842       g_test_message ("Test not run: not supported by the OS");
843       return;
844     }
845
846   client = g_socket_new (G_SOCKET_FAMILY_IPV4,
847                          G_SOCKET_TYPE_STREAM,
848                          G_SOCKET_PROTOCOL_DEFAULT,
849                          &error);
850   g_assert_no_error (error);
851
852   g_socket_set_blocking (client, TRUE);
853   g_socket_set_timeout (client, 1);
854
855   addr = g_socket_get_local_address (data->server, &error);
856   g_assert_no_error (error);
857   iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
858   v4addr = g_inet_socket_address_new (iaddr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
859   g_object_unref (iaddr);
860   g_object_unref (addr);
861
862   g_socket_connect (client, v4addr, NULL, &error);
863   g_assert_no_error (error);
864   g_assert (g_socket_is_connected (client));
865
866   g_thread_join (data->thread);
867
868   g_socket_close (client, &error);
869   g_assert_no_error (error);
870   g_socket_close (data->server, &error);
871   g_assert_no_error (error);
872
873   g_object_unref (data->server);
874   g_object_unref (client);
875   g_object_unref (v4addr);
876
877   g_slice_free (IPTestData, data);
878 }
879 #endif
880
881 static void
882 test_timed_wait (void)
883 {
884   IPTestData *data;
885   GError *error = NULL;
886   GSocket *client;
887   GSocketAddress *addr;
888   gint64 start_time;
889   gint poll_duration;
890
891   data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE);
892   addr = g_socket_get_local_address (data->server, &error);
893   g_assert_no_error (error);
894
895   client = g_socket_new (G_SOCKET_FAMILY_IPV4,
896                          G_SOCKET_TYPE_STREAM,
897                          G_SOCKET_PROTOCOL_DEFAULT,
898                          &error);
899   g_assert_no_error (error);
900
901   g_socket_set_blocking (client, TRUE);
902   g_socket_set_timeout (client, 1);
903
904   g_socket_connect (client, addr, NULL, &error);
905   g_assert_no_error (error);
906   g_object_unref (addr);
907
908   start_time = g_get_monotonic_time ();
909   g_socket_condition_timed_wait (client, G_IO_IN, 100000 /* 100 ms */,
910                                  NULL, &error);
911   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
912   g_clear_error (&error);
913   poll_duration = g_get_monotonic_time () - start_time;
914
915   g_assert_cmpint (poll_duration, >=, 98000);
916   g_assert_cmpint (poll_duration, <, 112000);
917
918   g_socket_close (client, &error);
919   g_assert_no_error (error);
920
921   g_thread_join (data->thread);
922
923   g_socket_close (data->server, &error);
924   g_assert_no_error (error);
925
926   g_object_unref (data->server);
927   g_object_unref (client);
928
929   g_slice_free (IPTestData, data);
930 }
931
932 static int
933 duplicate_fd(int fd)
934 {
935 #ifdef G_OS_WIN32
936     HANDLE newfd;
937
938     if (!DuplicateHandle (GetCurrentProcess (),
939                           (HANDLE)fd,
940                           GetCurrentProcess (),
941                           &newfd,
942                           0,
943                           FALSE,
944                            DUPLICATE_SAME_ACCESS))
945     {
946         return -1;
947     }
948
949     return (int)newfd;
950 #else
951     return dup(fd);
952 #endif
953 }
954
955 static void
956 test_fd_roundtrip (void)
957 {
958   IPTestData *data;
959   GError *error = NULL;
960   GSocket *client;
961   GSocket *client2;
962   GSocketAddress *addr;
963   int fd;
964   gssize len;
965   gchar buf[128];
966
967   data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE);
968   addr = g_socket_get_local_address (data->server, &error);
969   g_assert_no_error (error);
970
971   client = g_socket_new (G_SOCKET_FAMILY_IPV4,
972                          G_SOCKET_TYPE_STREAM,
973                          G_SOCKET_PROTOCOL_DEFAULT,
974                          &error);
975   g_assert_no_error (error);
976
977   g_socket_set_blocking (client, TRUE);
978   g_socket_set_timeout (client, 1);
979
980   g_socket_connect (client, addr, NULL, &error);
981   g_assert_no_error (error);
982   g_assert (g_socket_is_connected (client));
983   g_object_unref (addr);
984
985   /* we have to dup otherwise the fd gets closed twice on unref */
986   fd = duplicate_fd (g_socket_get_fd (client));
987   client2 = g_socket_new_from_fd (fd, &error);
988   g_assert_no_error (error);
989
990   g_assert_cmpint (g_socket_get_family (client2), ==, g_socket_get_family (client));
991   g_assert_cmpint (g_socket_get_socket_type (client2), ==, g_socket_get_socket_type (client));
992   g_assert_cmpint (g_socket_get_protocol (client2), ==, G_SOCKET_PROTOCOL_TCP);
993
994   len = g_socket_send (client2, testbuf, strlen (testbuf) + 1, NULL, &error);
995   g_assert_no_error (error);
996   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
997
998   len = g_socket_receive (client2, buf, sizeof (buf), NULL, &error);
999   g_assert_no_error (error);
1000   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
1001
1002   g_assert_cmpstr (testbuf, ==, buf);
1003
1004   g_socket_shutdown (client, FALSE, TRUE, &error);
1005   g_assert_no_error (error);
1006   g_socket_shutdown (client2, FALSE, TRUE, &error);
1007   g_assert_no_error (error);
1008
1009   g_thread_join (data->thread);
1010
1011   g_socket_close (client, &error);
1012   g_assert_no_error (error);
1013   g_socket_close (client2, &error);
1014   g_assert_no_error (error);
1015   g_socket_close (data->server, &error);
1016   g_assert_no_error (error);
1017
1018   g_object_unref (data->server);
1019   g_object_unref (client);
1020   g_object_unref (client2);
1021
1022   g_slice_free (IPTestData, data);
1023 }
1024
1025 static void
1026 test_sockaddr (void)
1027 {
1028   struct sockaddr_in6 sin6, gsin6;
1029   GSocketAddress *saddr;
1030   GInetSocketAddress *isaddr;
1031   GInetAddress *iaddr;
1032   GError *error = NULL;
1033
1034   memset (&sin6, 0, sizeof (sin6));
1035   sin6.sin6_family = AF_INET6;
1036   sin6.sin6_addr = in6addr_loopback;
1037   sin6.sin6_port = g_htons (42);
1038   sin6.sin6_scope_id = 17;
1039   sin6.sin6_flowinfo = 1729;
1040
1041   saddr = g_socket_address_new_from_native (&sin6, sizeof (sin6));
1042   g_assert (G_IS_INET_SOCKET_ADDRESS (saddr));
1043
1044   isaddr = G_INET_SOCKET_ADDRESS (saddr);
1045   iaddr = g_inet_socket_address_get_address (isaddr);
1046   g_assert_cmpint (g_inet_address_get_family (iaddr), ==, G_SOCKET_FAMILY_IPV6);
1047   g_assert (g_inet_address_get_is_loopback (iaddr));
1048
1049   g_assert_cmpint (g_inet_socket_address_get_port (isaddr), ==, 42);
1050   g_assert_cmpint (g_inet_socket_address_get_scope_id (isaddr), ==, 17);
1051   g_assert_cmpint (g_inet_socket_address_get_flowinfo (isaddr), ==, 1729);
1052
1053   g_socket_address_to_native (saddr, &gsin6, sizeof (gsin6), &error);
1054   g_assert_no_error (error);
1055
1056   g_assert (memcmp (&sin6.sin6_addr, &gsin6.sin6_addr, sizeof (struct in6_addr)) == 0);
1057   g_assert_cmpint (sin6.sin6_port, ==, gsin6.sin6_port);
1058   g_assert_cmpint (sin6.sin6_scope_id, ==, gsin6.sin6_scope_id);
1059   g_assert_cmpint (sin6.sin6_flowinfo, ==, gsin6.sin6_flowinfo);
1060
1061   g_object_unref (saddr);
1062 }
1063
1064 #ifdef G_OS_UNIX
1065 static void
1066 test_unix_from_fd (void)
1067 {
1068   gint fd;
1069   GError *error;
1070   GSocket *s;
1071
1072   fd = socket (AF_UNIX, SOCK_STREAM, 0);
1073   g_assert_cmpint (fd, !=, -1);
1074
1075   error = NULL;
1076   s = g_socket_new_from_fd (fd, &error);
1077   g_assert_no_error (error);
1078   g_assert_cmpint (g_socket_get_family (s), ==, G_SOCKET_FAMILY_UNIX);
1079   g_assert_cmpint (g_socket_get_socket_type (s), ==, G_SOCKET_TYPE_STREAM);
1080   g_assert_cmpint (g_socket_get_protocol (s), ==, G_SOCKET_PROTOCOL_DEFAULT);
1081   g_object_unref (s);
1082 }
1083
1084 static void
1085 test_unix_connection (void)
1086 {
1087   gint fd;
1088   GError *error;
1089   GSocket *s;
1090   GSocketConnection *c;
1091
1092   fd = socket (AF_UNIX, SOCK_STREAM, 0);
1093   g_assert_cmpint (fd, !=, -1);
1094
1095   error = NULL;
1096   s = g_socket_new_from_fd (fd, &error);
1097   g_assert_no_error (error);
1098   c = g_socket_connection_factory_create_connection (s);
1099   g_assert (G_IS_UNIX_CONNECTION (c));
1100   g_object_unref (c);
1101   g_object_unref (s);
1102 }
1103
1104 static GSocketConnection *
1105 create_connection_for_fd (int fd)
1106 {
1107   GError *err = NULL;
1108   GSocket *socket;
1109   GSocketConnection *connection;
1110
1111   socket = g_socket_new_from_fd (fd, &err);
1112   g_assert_no_error (err);
1113   g_assert (G_IS_SOCKET (socket));
1114   connection = g_socket_connection_factory_create_connection (socket);
1115   g_assert (G_IS_UNIX_CONNECTION (connection));
1116   g_object_unref (socket);
1117   return connection;
1118 }
1119
1120 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
1121
1122 static void
1123 test_unix_connection_ancillary_data (void)
1124 {
1125   GError *err = NULL;
1126   gint pv[2], sv[3];
1127   gint status, fd, len;
1128   char buffer[1024];
1129   pid_t pid;
1130
1131   status = pipe (pv);
1132   g_assert_cmpint (status, ==, 0);
1133
1134   status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
1135   g_assert_cmpint (status, ==, 0);
1136
1137   pid = fork ();
1138   g_assert_cmpint (pid, >=, 0);
1139
1140   /* Child: close its copy of the write end of the pipe, receive it
1141    * again from the parent over the socket, and write some text to it.
1142    *
1143    * Parent: send the write end of the pipe (still open for the
1144    * parent) over the socket, close it, and read some text from the
1145    * read end of the pipe.
1146    */
1147   if (pid == 0)
1148     {
1149       GSocketConnection *connection;
1150
1151       close (sv[1]);
1152       connection = create_connection_for_fd (sv[0]);
1153
1154       status = close (pv[1]);
1155       g_assert_cmpint (status, ==, 0);
1156
1157       err = NULL;
1158       fd = g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection), NULL,
1159                                          &err);
1160       g_assert_no_error (err);
1161       g_assert_cmpint (fd, >, -1);
1162       g_object_unref (connection);
1163
1164       do
1165         len = write (fd, TEST_DATA, sizeof (TEST_DATA));
1166       while (len == -1 && errno == EINTR);
1167       g_assert_cmpint (len, ==, sizeof (TEST_DATA));
1168       exit (0);
1169     }
1170   else
1171     {
1172       GSocketConnection *connection;
1173
1174       close (sv[0]);
1175       connection = create_connection_for_fd (sv[1]);
1176
1177       err = NULL;
1178       g_unix_connection_send_fd (G_UNIX_CONNECTION (connection), pv[1], NULL,
1179                                  &err);
1180       g_assert_no_error (err);
1181       g_object_unref (connection);
1182
1183       status = close (pv[1]);
1184       g_assert_cmpint (status, ==, 0);
1185
1186       memset (buffer, 0xff, sizeof buffer);
1187       do
1188         len = read (pv[0], buffer, sizeof buffer);
1189       while (len == -1 && errno == EINTR);
1190
1191       g_assert_cmpint (len, ==, sizeof (TEST_DATA));
1192       g_assert_cmpstr (buffer, ==, TEST_DATA);
1193
1194       waitpid (pid, &status, 0);
1195       g_assert (WIFEXITED (status));
1196       g_assert_cmpint (WEXITSTATUS (status), ==, 0);
1197     }
1198
1199   /* TODO: add test for g_unix_connection_send_credentials() and
1200    * g_unix_connection_receive_credentials().
1201    */
1202 }
1203 #endif /* G_OS_UNIX */
1204
1205 static void
1206 test_reuse_tcp (void)
1207 {
1208   GSocket *sock1, *sock2;
1209   GError *error = NULL;
1210   GInetAddress *iaddr;
1211   GSocketAddress *addr;
1212
1213   sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1214                         G_SOCKET_TYPE_STREAM,
1215                         G_SOCKET_PROTOCOL_DEFAULT,
1216                         &error);
1217   g_assert_no_error (error);
1218
1219   iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1220   addr = g_inet_socket_address_new (iaddr, 0);
1221   g_object_unref (iaddr);
1222   g_socket_bind (sock1, addr, TRUE, &error);
1223   g_object_unref (addr);
1224   g_assert_no_error (error);
1225
1226   g_socket_listen (sock1, &error);
1227   g_assert_no_error (error);
1228
1229   sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1230                         G_SOCKET_TYPE_STREAM,
1231                         G_SOCKET_PROTOCOL_DEFAULT,
1232                         &error);
1233   g_assert_no_error (error);
1234
1235   addr = g_socket_get_local_address (sock1, &error);
1236   g_assert_no_error (error);
1237   g_socket_bind (sock2, addr, TRUE, &error);
1238   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_ADDRESS_IN_USE);
1239   g_clear_error (&error);
1240   g_object_unref (addr);
1241
1242   g_object_unref (sock1);
1243   g_object_unref (sock2);
1244 }
1245
1246 static void
1247 test_reuse_udp (void)
1248 {
1249   GSocket *sock1, *sock2;
1250   GError *error = NULL;
1251   GInetAddress *iaddr;
1252   GSocketAddress *addr;
1253
1254   sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1255                         G_SOCKET_TYPE_DATAGRAM,
1256                         G_SOCKET_PROTOCOL_DEFAULT,
1257                         &error);
1258   g_assert_no_error (error);
1259
1260   iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1261   addr = g_inet_socket_address_new (iaddr, 0);
1262   g_object_unref (iaddr);
1263   g_socket_bind (sock1, addr, TRUE, &error);
1264   g_object_unref (addr);
1265   g_assert_no_error (error);
1266
1267   sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1268                         G_SOCKET_TYPE_DATAGRAM,
1269                         G_SOCKET_PROTOCOL_DEFAULT,
1270                         &error);
1271   g_assert_no_error (error);
1272
1273   addr = g_socket_get_local_address (sock1, &error);
1274   g_assert_no_error (error);
1275   g_socket_bind (sock2, addr, TRUE, &error);
1276   g_object_unref (addr);
1277   g_assert_no_error (error);
1278
1279   g_object_unref (sock1);
1280   g_object_unref (sock2);
1281 }
1282
1283 static void
1284 test_get_available (gconstpointer user_data)
1285 {
1286   GSocketType socket_type = GPOINTER_TO_UINT (user_data);
1287   GError *err = NULL;
1288   GSocket *listener, *server, *client;
1289   GInetAddress *addr;
1290   GSocketAddress *saddr;
1291   gchar data[] = "0123456789abcdef";
1292   gchar buf[34];
1293   gssize nread;
1294
1295   listener = g_socket_new (G_SOCKET_FAMILY_IPV4,
1296                            socket_type,
1297                            G_SOCKET_PROTOCOL_DEFAULT,
1298                            &err);
1299   g_assert_no_error (err);
1300   g_assert (G_IS_SOCKET (listener));
1301
1302   client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1303                          socket_type,
1304                          G_SOCKET_PROTOCOL_DEFAULT,
1305                          &err);
1306   g_assert_no_error (err);
1307   g_assert (G_IS_SOCKET (client));
1308
1309   if (socket_type == G_SOCKET_TYPE_STREAM)
1310     {
1311       g_socket_set_option (client, IPPROTO_TCP, TCP_NODELAY, TRUE, &err);
1312       g_assert_no_error (err);
1313     }
1314
1315   addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
1316   saddr = g_inet_socket_address_new (addr, 0);
1317
1318   g_socket_bind (listener, saddr, TRUE, &err);
1319   g_assert_no_error (err);
1320   g_object_unref (saddr);
1321   g_object_unref (addr);
1322
1323   saddr = g_socket_get_local_address (listener, &err);
1324   g_assert_no_error (err);
1325
1326   if (socket_type == G_SOCKET_TYPE_STREAM)
1327     {
1328       g_socket_listen (listener, &err);
1329       g_assert_no_error (err);
1330       g_socket_connect (client, saddr, NULL, &err);
1331       g_assert_no_error (err);
1332
1333       server = g_socket_accept (listener, NULL, &err);
1334       g_assert_no_error (err);
1335       g_socket_set_blocking (server, FALSE);
1336       g_object_unref (listener);
1337     }
1338   else
1339     server = listener;
1340
1341   g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
1342   g_assert_no_error (err);
1343
1344   while (!g_socket_condition_wait (server, G_IO_IN, NULL, NULL))
1345     ;
1346   g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1347
1348   g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
1349   g_assert_no_error (err);
1350
1351   /* We need to wait until the data has actually been copied into the
1352    * server socket's buffers, but g_socket_condition_wait() won't help
1353    * here since the socket is definitely already readable. So there's
1354    * a race condition in checking its available bytes. In the TCP
1355    * case, we poll for a bit until the new data shows up. In the UDP
1356    * case, there's not much we can do, but at least the failure mode
1357    * is passes-when-it-shouldn't, not fails-when-it-shouldn't.
1358    */
1359   if (socket_type == G_SOCKET_TYPE_STREAM)
1360     {
1361       int tries;
1362
1363       for (tries = 0; tries < 100; tries++)
1364         {
1365           if (g_socket_get_available_bytes (server) > sizeof (data))
1366             break;
1367           g_usleep (100000);
1368         }
1369
1370       g_assert_cmpint (g_socket_get_available_bytes (server), ==, 2 * sizeof (data));
1371     }
1372   else
1373     {
1374       g_usleep (100000);
1375       g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1376     }
1377
1378   g_assert_cmpint (sizeof (buf), >=, 2 * sizeof (data));
1379   nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
1380   g_assert_no_error (err);
1381
1382   if (socket_type == G_SOCKET_TYPE_STREAM)
1383     {
1384       g_assert_cmpint (nread, ==, 2 * sizeof (data));
1385       g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1386     }
1387   else
1388     {
1389       g_assert_cmpint (nread, ==, sizeof (data));
1390       g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1391     }
1392
1393   nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
1394   if (socket_type == G_SOCKET_TYPE_STREAM)
1395     {
1396       g_assert_cmpint (nread, ==, -1);
1397       g_assert_error (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
1398       g_clear_error (&err);
1399     }
1400   else
1401     {
1402       g_assert_cmpint (nread, ==, sizeof (data));
1403       g_assert_no_error (err);
1404     }
1405
1406   g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1407
1408   g_socket_close (server, &err);
1409   g_assert_no_error (err);
1410
1411   g_object_unref (saddr);
1412   g_object_unref (server);
1413   g_object_unref (client);
1414 }
1415
1416 int
1417 main (int   argc,
1418       char *argv[])
1419 {
1420   GSocket *sock;
1421   GError *error = NULL;
1422
1423   g_test_init (&argc, &argv, NULL);
1424
1425   sock = g_socket_new (G_SOCKET_FAMILY_IPV6,
1426                        G_SOCKET_TYPE_STREAM,
1427                        G_SOCKET_PROTOCOL_DEFAULT,
1428                        &error);
1429   if (sock != NULL)
1430     {
1431       ipv6_supported = TRUE;
1432       g_object_unref (sock);
1433     }
1434   else
1435     {
1436       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
1437       g_clear_error (&error);
1438     }
1439
1440   g_test_add_func ("/socket/ipv4_sync", test_ipv4_sync);
1441   g_test_add_func ("/socket/ipv4_async", test_ipv4_async);
1442   g_test_add_func ("/socket/ipv6_sync", test_ipv6_sync);
1443   g_test_add_func ("/socket/ipv6_async", test_ipv6_async);
1444   g_test_add_func ("/socket/ipv4_sync/datagram", test_ipv4_sync_dgram);
1445   g_test_add_func ("/socket/ipv6_sync/datagram", test_ipv6_sync_dgram);
1446 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
1447   g_test_add_func ("/socket/ipv6_v4mapped", test_ipv6_v4mapped);
1448 #endif
1449   g_test_add_func ("/socket/close_graceful", test_close_graceful);
1450   g_test_add_func ("/socket/timed_wait", test_timed_wait);
1451   g_test_add_func ("/socket/fd_roundtrip", test_fd_roundtrip);
1452   g_test_add_func ("/socket/address", test_sockaddr);
1453 #ifdef G_OS_UNIX
1454   g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
1455   g_test_add_func ("/socket/unix-connection", test_unix_connection);
1456   g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data);
1457 #endif
1458   g_test_add_func ("/socket/reuse/tcp", test_reuse_tcp);
1459   g_test_add_func ("/socket/reuse/udp", test_reuse_udp);
1460   g_test_add_data_func ("/socket/get_available/datagram", GUINT_TO_POINTER (G_SOCKET_TYPE_DATAGRAM),
1461                         test_get_available);
1462   g_test_add_data_func ("/socket/get_available/stream", GUINT_TO_POINTER (G_SOCKET_TYPE_STREAM),
1463                         test_get_available);
1464
1465   return g_test_run();
1466 }
1467