[kdbus] sync with kdbus (kdbus.h - commit: b024fb43c66b)
[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 } IPTestData;
41
42 static gpointer
43 echo_server_thread (gpointer user_data)
44 {
45   IPTestData *data = user_data;
46   GSocket *sock;
47   GError *error = NULL;
48   gssize nread, nwrote;
49   gchar buf[128];
50
51   sock = g_socket_accept (data->server, NULL, &error);
52   g_assert_no_error (error);
53
54   while (TRUE)
55     {
56       nread = g_socket_receive (sock, buf, sizeof (buf), NULL, &error);
57       g_assert_no_error (error);
58       g_assert_cmpint (nread, >=, 0);
59
60       if (nread == 0)
61         break;
62
63       nwrote = g_socket_send (sock, buf, nread, NULL, &error);
64       g_assert_no_error (error);
65       g_assert_cmpint (nwrote, ==, nread);
66     }
67
68   g_socket_close (sock, &error);
69   g_assert_no_error (error);
70   g_object_unref (sock);
71   return NULL;
72 }
73
74 static IPTestData *
75 create_server (GSocketFamily family,
76                GThreadFunc   server_thread,
77                gboolean      v4mapped)
78 {
79   IPTestData *data;
80   GSocket *server;
81   GError *error = NULL;
82   GSocketAddress *addr;
83   GInetAddress *iaddr;
84
85   data = g_slice_new (IPTestData);
86   data->family = family;
87
88   data->server = server = g_socket_new (family,
89                                         G_SOCKET_TYPE_STREAM,
90                                         G_SOCKET_PROTOCOL_DEFAULT,
91                                         &error);
92   g_assert_no_error (error);
93
94   g_assert_cmpint (g_socket_get_family (server), ==, family);
95   g_assert_cmpint (g_socket_get_socket_type (server), ==, G_SOCKET_TYPE_STREAM);
96   g_assert_cmpint (g_socket_get_protocol (server), ==, G_SOCKET_PROTOCOL_DEFAULT);
97
98   g_socket_set_blocking (server, TRUE);
99
100 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
101   if (v4mapped)
102     {
103       g_socket_set_option (data->server, IPPROTO_IPV6, IPV6_V6ONLY, FALSE, NULL);
104       if (! g_socket_speaks_ipv4 (data->server))
105         {
106           g_object_unref (data->server);
107           g_slice_free (IPTestData, data);
108           return NULL;
109         }
110     }
111 #endif
112
113   if (v4mapped)
114     iaddr = g_inet_address_new_any (family);
115   else
116     iaddr = g_inet_address_new_loopback (family);
117   addr = g_inet_socket_address_new (iaddr, 0);
118   g_object_unref (iaddr);
119
120   g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), ==, 0);
121   g_socket_bind (server, addr, TRUE, &error);
122   g_assert_no_error (error);
123   g_object_unref (addr);
124
125   addr = g_socket_get_local_address (server, &error);
126   g_assert_no_error (error);
127   g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), !=, 0);
128   g_object_unref (addr);
129
130   g_socket_listen (server, &error);
131   g_assert_no_error (error);
132
133   data->thread = g_thread_new ("server", server_thread, data);
134
135   return data;
136 }
137
138 static const gchar *testbuf = "0123456789abcdef";
139
140 static gboolean
141 test_ip_async_read_ready (GSocket      *client,
142                           GIOCondition  cond,
143                           gpointer      user_data)
144 {
145   IPTestData *data = user_data;
146   GError *error = NULL;
147   gssize len;
148   gchar buf[128];
149
150   g_assert_cmpint (cond, ==, G_IO_IN);
151
152   len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
153   g_assert_no_error (error);
154   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
155
156   g_assert_cmpstr (testbuf, ==, buf);
157
158   g_main_loop_quit (data->loop);
159
160   return FALSE;
161 }
162
163 static gboolean
164 test_ip_async_write_ready (GSocket      *client,
165                            GIOCondition  cond,
166                            gpointer      user_data)
167 {
168   IPTestData *data = user_data;
169   GError *error = NULL;
170   GSource *source;
171   gssize len;
172
173   g_assert_cmpint (cond, ==, G_IO_OUT);
174
175   len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
176   g_assert_no_error (error);
177   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
178
179   source = g_socket_create_source (client, G_IO_IN, NULL);
180   g_source_set_callback (source, (GSourceFunc)test_ip_async_read_ready,
181                          data, NULL);
182   g_source_attach (source, NULL);
183   g_source_unref (source);
184
185   return FALSE;
186 }
187
188 static gboolean
189 test_ip_async_timed_out (GSocket      *client,
190                          GIOCondition  cond,
191                          gpointer      user_data)
192 {
193   IPTestData *data = user_data;
194   GError *error = NULL;
195   GSource *source;
196   gssize len;
197   gchar buf[128];
198
199   if (data->family == G_SOCKET_FAMILY_IPV4)
200     {
201       g_assert_cmpint (cond, ==, G_IO_IN);
202       len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
203       g_assert_cmpint (len, ==, -1);
204       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
205       g_clear_error (&error);
206     }
207
208   source = g_socket_create_source (client, G_IO_OUT, NULL);
209   g_source_set_callback (source, (GSourceFunc)test_ip_async_write_ready,
210                          data, NULL);
211   g_source_attach (source, NULL);
212   g_source_unref (source);
213
214   return FALSE;
215 }
216
217 static gboolean
218 test_ip_async_connected (GSocket      *client,
219                          GIOCondition  cond,
220                          gpointer      user_data)
221 {
222   IPTestData *data = user_data;
223   GError *error = NULL;
224   GSource *source;
225   gssize len;
226   gchar buf[128];
227
228   g_socket_check_connect_result (client, &error);
229   g_assert_no_error (error);
230   /* We do this after the check_connect_result, since that will give a
231    * more useful assertion in case of error.
232    */
233   g_assert_cmpint (cond, ==, G_IO_OUT);
234
235   g_assert (g_socket_is_connected (client));
236
237   /* This adds 1 second to "make check", so let's just only do it once. */
238   if (data->family == G_SOCKET_FAMILY_IPV4)
239     {
240       len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
241       g_assert_cmpint (len, ==, -1);
242       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
243       g_clear_error (&error);
244
245       source = g_socket_create_source (client, G_IO_IN, NULL);
246       g_source_set_callback (source, (GSourceFunc)test_ip_async_timed_out,
247                              data, NULL);
248       g_source_attach (source, NULL);
249       g_source_unref (source);
250     }
251   else
252     test_ip_async_timed_out (client, 0, data);
253
254   return FALSE;
255 }
256
257 static gboolean
258 idle_test_ip_async_connected (gpointer user_data)
259 {
260   IPTestData *data = user_data;
261
262   return test_ip_async_connected (data->client, G_IO_OUT, data);
263 }
264
265 static void
266 test_ip_async (GSocketFamily family)
267 {
268   IPTestData *data;
269   GError *error = NULL;
270   GSocket *client;
271   GSocketAddress *addr;
272   GSource *source;
273   gssize len;
274   gchar buf[128];
275
276   data = create_server (family, echo_server_thread, FALSE);
277   addr = g_socket_get_local_address (data->server, &error);
278   g_assert_no_error (error);
279
280   client = g_socket_new (family,
281                          G_SOCKET_TYPE_STREAM,
282                          G_SOCKET_PROTOCOL_DEFAULT,
283                          &error);
284   g_assert_no_error (error);
285   data->client = client;
286
287   g_assert_cmpint (g_socket_get_family (client), ==, family);
288   g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
289   g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
290
291   g_socket_set_blocking (client, FALSE);
292   g_socket_set_timeout (client, 1);
293
294   if (g_socket_connect (client, addr, NULL, &error))
295     {
296       g_assert_no_error (error);
297       g_idle_add (idle_test_ip_async_connected, data);
298     }
299   else
300     {
301       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
302       g_clear_error (&error);
303       source = g_socket_create_source (client, G_IO_OUT, NULL);
304       g_source_set_callback (source, (GSourceFunc)test_ip_async_connected,
305                              data, NULL);
306       g_source_attach (source, NULL);
307       g_source_unref (source);
308     }
309   g_object_unref (addr);
310
311   data->loop = g_main_loop_new (NULL, TRUE);
312   g_main_loop_run (data->loop);
313   g_main_loop_unref (data->loop);
314
315   g_socket_shutdown (client, FALSE, TRUE, &error);
316   g_assert_no_error (error);
317
318   g_thread_join (data->thread);
319
320   if (family == G_SOCKET_FAMILY_IPV4)
321     {
322       /* Test that reading on a remote-closed socket gets back 0 bytes. */
323       len = g_socket_receive_with_blocking (client, buf, sizeof (buf),
324                                             TRUE, NULL, &error);
325       g_assert_no_error (error);
326       g_assert_cmpint (len, ==, 0);
327     }
328   else
329     {
330       /* Test that writing to a remote-closed socket gets back CONNECTION_CLOSED. */
331       len = g_socket_send_with_blocking (client, testbuf, strlen (testbuf) + 1,
332                                          TRUE, NULL, &error);
333       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED);
334       g_assert_cmpint (len, ==, -1);
335       g_clear_error (&error);
336     }
337
338   g_socket_close (client, &error);
339   g_assert_no_error (error);
340   g_socket_close (data->server, &error);
341   g_assert_no_error (error);
342
343   g_object_unref (data->server);
344   g_object_unref (client);
345
346   g_slice_free (IPTestData, data);
347 }
348
349 static void
350 test_ipv4_async (void)
351 {
352   test_ip_async (G_SOCKET_FAMILY_IPV4);
353 }
354
355 static void
356 test_ipv6_async (void)
357 {
358   if (!ipv6_supported)
359     {
360       g_test_skip ("No support for IPv6");
361       return;
362     }
363
364   test_ip_async (G_SOCKET_FAMILY_IPV6);
365 }
366
367 static void
368 test_ip_sync (GSocketFamily family)
369 {
370   IPTestData *data;
371   GError *error = NULL;
372   GSocket *client;
373   GSocketAddress *addr;
374   gssize len;
375   gchar buf[128];
376
377   data = create_server (family, echo_server_thread, FALSE);
378   addr = g_socket_get_local_address (data->server, &error);
379   g_assert_no_error (error);
380
381   client = g_socket_new (family,
382                          G_SOCKET_TYPE_STREAM,
383                          G_SOCKET_PROTOCOL_DEFAULT,
384                          &error);
385   g_assert_no_error (error);
386
387   g_assert_cmpint (g_socket_get_family (client), ==, family);
388   g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
389   g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
390
391   g_socket_set_blocking (client, TRUE);
392   g_socket_set_timeout (client, 1);
393
394   g_socket_connect (client, addr, NULL, &error);
395   g_assert_no_error (error);
396   g_assert (g_socket_is_connected (client));
397   g_object_unref (addr);
398
399   /* This adds 1 second to "make check", so let's just only do it once. */
400   if (family == G_SOCKET_FAMILY_IPV4)
401     {
402       len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
403       g_assert_cmpint (len, ==, -1);
404       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
405       g_clear_error (&error);
406     }
407
408   len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
409   g_assert_no_error (error);
410   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
411   
412   len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
413   g_assert_no_error (error);
414   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
415
416   g_assert_cmpstr (testbuf, ==, buf);
417
418   g_socket_shutdown (client, FALSE, TRUE, &error);
419   g_assert_no_error (error);
420
421   g_thread_join (data->thread);
422
423   if (family == G_SOCKET_FAMILY_IPV4)
424     {
425       /* Test that reading on a remote-closed socket gets back 0 bytes. */
426       len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
427       g_assert_no_error (error);
428       g_assert_cmpint (len, ==, 0);
429     }
430   else
431     {
432       /* Test that writing to a remote-closed socket gets back CONNECTION_CLOSED. */
433       len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
434       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED);
435       g_assert_cmpint (len, ==, -1);
436       g_clear_error (&error);
437     }
438
439   g_socket_close (client, &error);
440   g_assert_no_error (error);
441   g_socket_close (data->server, &error);
442   g_assert_no_error (error);
443
444   g_object_unref (data->server);
445   g_object_unref (client);
446
447   g_slice_free (IPTestData, data);
448 }
449
450 static void
451 test_ipv4_sync (void)
452 {
453   test_ip_sync (G_SOCKET_FAMILY_IPV4);
454 }
455
456 static void
457 test_ipv6_sync (void)
458 {
459   if (!ipv6_supported)
460     {
461       g_test_skip ("No support for IPv6");
462       return;
463     }
464
465   test_ip_sync (G_SOCKET_FAMILY_IPV6);
466 }
467
468 static gpointer
469 graceful_server_thread (gpointer user_data)
470 {
471   IPTestData *data = user_data;
472   GSocket *sock;
473   GError *error = NULL;
474   gssize len;
475
476   sock = g_socket_accept (data->server, NULL, &error);
477   g_assert_no_error (error);
478
479   len = g_socket_send (sock, testbuf, strlen (testbuf) + 1, NULL, &error);
480   g_assert_no_error (error);
481   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
482
483   return sock;
484 }
485
486 static void
487 test_close_graceful (void)
488 {
489   GSocketFamily family = G_SOCKET_FAMILY_IPV4;
490   IPTestData *data;
491   GError *error = NULL;
492   GSocket *client, *server;
493   GSocketAddress *addr;
494   gssize len;
495   gchar buf[128];
496
497   data = create_server (family, graceful_server_thread, FALSE);
498   addr = g_socket_get_local_address (data->server, &error);
499   g_assert_no_error (error);
500
501   client = g_socket_new (family,
502                          G_SOCKET_TYPE_STREAM,
503                          G_SOCKET_PROTOCOL_DEFAULT,
504                          &error);
505   g_assert_no_error (error);
506
507   g_assert_cmpint (g_socket_get_family (client), ==, family);
508   g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
509   g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
510
511   g_socket_set_blocking (client, TRUE);
512   g_socket_set_timeout (client, 1);
513
514   g_socket_connect (client, addr, NULL, &error);
515   g_assert_no_error (error);
516   g_assert (g_socket_is_connected (client));
517   g_object_unref (addr);
518
519   server = g_thread_join (data->thread);
520
521   /* similar to g_tcp_connection_set_graceful_disconnect(), but explicit */
522   g_socket_shutdown (server, FALSE, TRUE, &error);
523   g_assert_no_error (error);
524
525   /* we must timeout */
526   g_socket_condition_wait (client, G_IO_HUP, NULL, &error);
527   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
528   g_clear_error (&error);
529
530   /* check that the remaining data is received */
531   len = g_socket_receive (client, buf, strlen (testbuf) + 1, NULL, &error);
532   g_assert_no_error (error);
533   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
534
535   /* and only then the connection is closed */
536   len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
537   g_assert_no_error (error);
538   g_assert_cmpint (len, ==, 0);
539
540   g_socket_close (server, &error);
541   g_assert_no_error (error);
542
543   g_socket_close (client, &error);
544   g_assert_no_error (error);
545
546   g_object_unref (server);
547   g_object_unref (data->server);
548   g_object_unref (client);
549
550   g_slice_free (IPTestData, data);
551 }
552
553 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
554 static gpointer
555 v4mapped_server_thread (gpointer user_data)
556 {
557   IPTestData *data = user_data;
558   GSocket *sock;
559   GError *error = NULL;
560   GSocketAddress *addr;
561
562   sock = g_socket_accept (data->server, NULL, &error);
563   g_assert_no_error (error);
564
565   g_assert_cmpint (g_socket_get_family (sock), ==, G_SOCKET_FAMILY_IPV6);
566
567   addr = g_socket_get_local_address (sock, &error);
568   g_assert_no_error (error);
569   g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
570   g_object_unref (addr);
571
572   addr = g_socket_get_remote_address (sock, &error);
573   g_assert_no_error (error);
574   g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
575   g_object_unref (addr);
576
577   g_socket_close (sock, &error);
578   g_assert_no_error (error);
579   g_object_unref (sock);
580   return NULL;
581 }
582
583 static void
584 test_ipv6_v4mapped (void)
585 {
586   IPTestData *data;
587   GError *error = NULL;
588   GSocket *client;
589   GSocketAddress *addr, *v4addr;
590   GInetAddress *iaddr;
591
592   if (!ipv6_supported)
593     {
594       g_test_skip ("No support for IPv6");
595       return;
596     }
597
598   data = create_server (G_SOCKET_FAMILY_IPV6, v4mapped_server_thread, TRUE);
599
600   if (data == NULL)
601     {
602       g_test_message ("Test not run: not supported by the OS");
603       return;
604     }
605
606   client = g_socket_new (G_SOCKET_FAMILY_IPV4,
607                          G_SOCKET_TYPE_STREAM,
608                          G_SOCKET_PROTOCOL_DEFAULT,
609                          &error);
610   g_assert_no_error (error);
611
612   g_socket_set_blocking (client, TRUE);
613   g_socket_set_timeout (client, 1);
614
615   addr = g_socket_get_local_address (data->server, &error);
616   g_assert_no_error (error);
617   iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
618   v4addr = g_inet_socket_address_new (iaddr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
619   g_object_unref (iaddr);
620   g_object_unref (addr);
621
622   g_socket_connect (client, v4addr, NULL, &error);
623   g_assert_no_error (error);
624   g_assert (g_socket_is_connected (client));
625
626   g_thread_join (data->thread);
627
628   g_socket_close (client, &error);
629   g_assert_no_error (error);
630   g_socket_close (data->server, &error);
631   g_assert_no_error (error);
632
633   g_object_unref (data->server);
634   g_object_unref (client);
635   g_object_unref (v4addr);
636
637   g_slice_free (IPTestData, data);
638 }
639 #endif
640
641 static void
642 test_timed_wait (void)
643 {
644   IPTestData *data;
645   GError *error = NULL;
646   GSocket *client;
647   GSocketAddress *addr;
648   gint64 start_time;
649   gint poll_duration;
650
651   data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE);
652   addr = g_socket_get_local_address (data->server, &error);
653   g_assert_no_error (error);
654
655   client = g_socket_new (G_SOCKET_FAMILY_IPV4,
656                          G_SOCKET_TYPE_STREAM,
657                          G_SOCKET_PROTOCOL_DEFAULT,
658                          &error);
659   g_assert_no_error (error);
660
661   g_socket_set_blocking (client, TRUE);
662   g_socket_set_timeout (client, 1);
663
664   g_socket_connect (client, addr, NULL, &error);
665   g_assert_no_error (error);
666   g_object_unref (addr);
667
668   start_time = g_get_monotonic_time ();
669   g_socket_condition_timed_wait (client, G_IO_IN, 100000 /* 100 ms */,
670                                  NULL, &error);
671   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
672   g_clear_error (&error);
673   poll_duration = g_get_monotonic_time () - start_time;
674
675   g_assert_cmpint (poll_duration, >=, 98000);
676   g_assert_cmpint (poll_duration, <, 112000);
677
678   g_socket_close (client, &error);
679   g_assert_no_error (error);
680
681   g_thread_join (data->thread);
682
683   g_socket_close (data->server, &error);
684   g_assert_no_error (error);
685
686   g_object_unref (data->server);
687   g_object_unref (client);
688
689   g_slice_free (IPTestData, data);
690 }
691
692 static void
693 test_sockaddr (void)
694 {
695   struct sockaddr_in6 sin6, gsin6;
696   GSocketAddress *saddr;
697   GInetSocketAddress *isaddr;
698   GInetAddress *iaddr;
699   GError *error = NULL;
700
701   memset (&sin6, 0, sizeof (sin6));
702   sin6.sin6_family = AF_INET6;
703   sin6.sin6_addr = in6addr_loopback;
704   sin6.sin6_port = g_htons (42);
705   sin6.sin6_scope_id = 17;
706   sin6.sin6_flowinfo = 1729;
707
708   saddr = g_socket_address_new_from_native (&sin6, sizeof (sin6));
709   g_assert (G_IS_INET_SOCKET_ADDRESS (saddr));
710
711   isaddr = G_INET_SOCKET_ADDRESS (saddr);
712   iaddr = g_inet_socket_address_get_address (isaddr);
713   g_assert_cmpint (g_inet_address_get_family (iaddr), ==, G_SOCKET_FAMILY_IPV6);
714   g_assert (g_inet_address_get_is_loopback (iaddr));
715
716   g_assert_cmpint (g_inet_socket_address_get_port (isaddr), ==, 42);
717   g_assert_cmpint (g_inet_socket_address_get_scope_id (isaddr), ==, 17);
718   g_assert_cmpint (g_inet_socket_address_get_flowinfo (isaddr), ==, 1729);
719
720   g_socket_address_to_native (saddr, &gsin6, sizeof (gsin6), &error);
721   g_assert_no_error (error);
722
723   g_assert (memcmp (&sin6.sin6_addr, &gsin6.sin6_addr, sizeof (struct in6_addr)) == 0);
724   g_assert_cmpint (sin6.sin6_port, ==, gsin6.sin6_port);
725   g_assert_cmpint (sin6.sin6_scope_id, ==, gsin6.sin6_scope_id);
726   g_assert_cmpint (sin6.sin6_flowinfo, ==, gsin6.sin6_flowinfo);
727
728   g_object_unref (saddr);
729 }
730
731 #ifdef G_OS_UNIX
732 static void
733 test_unix_from_fd (void)
734 {
735   gint fd;
736   GError *error;
737   GSocket *s;
738
739   fd = socket (AF_UNIX, SOCK_STREAM, 0);
740   g_assert_cmpint (fd, !=, -1);
741
742   error = NULL;
743   s = g_socket_new_from_fd (fd, &error);
744   g_assert_no_error (error);
745   g_assert_cmpint (g_socket_get_family (s), ==, G_SOCKET_FAMILY_UNIX);
746   g_assert_cmpint (g_socket_get_socket_type (s), ==, G_SOCKET_TYPE_STREAM);
747   g_assert_cmpint (g_socket_get_protocol (s), ==, G_SOCKET_PROTOCOL_DEFAULT);
748   g_object_unref (s);
749 }
750
751 static void
752 test_unix_connection (void)
753 {
754   gint fd;
755   GError *error;
756   GSocket *s;
757   GSocketConnection *c;
758
759   fd = socket (AF_UNIX, SOCK_STREAM, 0);
760   g_assert_cmpint (fd, !=, -1);
761
762   error = NULL;
763   s = g_socket_new_from_fd (fd, &error);
764   g_assert_no_error (error);
765   c = g_socket_connection_factory_create_connection (s);
766   g_assert (G_IS_UNIX_CONNECTION (c));
767   g_object_unref (c);
768   g_object_unref (s);
769 }
770
771 static GSocketConnection *
772 create_connection_for_fd (int fd)
773 {
774   GError *err = NULL;
775   GSocket *socket;
776   GSocketConnection *connection;
777
778   socket = g_socket_new_from_fd (fd, &err);
779   g_assert_no_error (err);
780   g_assert (G_IS_SOCKET (socket));
781   connection = g_socket_connection_factory_create_connection (socket);
782   g_assert (G_IS_UNIX_CONNECTION (connection));
783   g_object_unref (socket);
784   return connection;
785 }
786
787 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
788
789 static void
790 test_unix_connection_ancillary_data (void)
791 {
792   GError *err = NULL;
793   gint pv[2], sv[3];
794   gint status, fd, len;
795   char buffer[1024];
796   pid_t pid;
797
798   status = pipe (pv);
799   g_assert_cmpint (status, ==, 0);
800
801   status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
802   g_assert_cmpint (status, ==, 0);
803
804   pid = fork ();
805   g_assert_cmpint (pid, >=, 0);
806
807   /* Child: close its copy of the write end of the pipe, receive it
808    * again from the parent over the socket, and write some text to it.
809    *
810    * Parent: send the write end of the pipe (still open for the
811    * parent) over the socket, close it, and read some text from the
812    * read end of the pipe.
813    */
814   if (pid == 0)
815     {
816       GSocketConnection *connection;
817
818       close (sv[1]);
819       connection = create_connection_for_fd (sv[0]);
820
821       status = close (pv[1]);
822       g_assert_cmpint (status, ==, 0);
823
824       err = NULL;
825       fd = g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection), NULL,
826                                          &err);
827       g_assert_no_error (err);
828       g_assert_cmpint (fd, >, -1);
829       g_object_unref (connection);
830
831       do
832         len = write (fd, TEST_DATA, sizeof (TEST_DATA));
833       while (len == -1 && errno == EINTR);
834       g_assert_cmpint (len, ==, sizeof (TEST_DATA));
835       exit (0);
836     }
837   else
838     {
839       GSocketConnection *connection;
840
841       close (sv[0]);
842       connection = create_connection_for_fd (sv[1]);
843
844       err = NULL;
845       g_unix_connection_send_fd (G_UNIX_CONNECTION (connection), pv[1], NULL,
846                                  &err);
847       g_assert_no_error (err);
848       g_object_unref (connection);
849
850       status = close (pv[1]);
851       g_assert_cmpint (status, ==, 0);
852
853       memset (buffer, 0xff, sizeof buffer);
854       do
855         len = read (pv[0], buffer, sizeof buffer);
856       while (len == -1 && errno == EINTR);
857
858       g_assert_cmpint (len, ==, sizeof (TEST_DATA));
859       g_assert_cmpstr (buffer, ==, TEST_DATA);
860
861       waitpid (pid, &status, 0);
862       g_assert (WIFEXITED (status));
863       g_assert_cmpint (WEXITSTATUS (status), ==, 0);
864     }
865
866   /* TODO: add test for g_unix_connection_send_credentials() and
867    * g_unix_connection_receive_credentials().
868    */
869 }
870 #endif /* G_OS_UNIX */
871
872 static void
873 test_reuse_tcp (void)
874 {
875   GSocket *sock1, *sock2;
876   GError *error = NULL;
877   GInetAddress *iaddr;
878   GSocketAddress *addr;
879
880   sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
881                         G_SOCKET_TYPE_STREAM,
882                         G_SOCKET_PROTOCOL_DEFAULT,
883                         &error);
884   g_assert_no_error (error);
885
886   iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
887   addr = g_inet_socket_address_new (iaddr, 0);
888   g_object_unref (iaddr);
889   g_socket_bind (sock1, addr, TRUE, &error);
890   g_object_unref (addr);
891   g_assert_no_error (error);
892
893   g_socket_listen (sock1, &error);
894   g_assert_no_error (error);
895
896   sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
897                         G_SOCKET_TYPE_STREAM,
898                         G_SOCKET_PROTOCOL_DEFAULT,
899                         &error);
900   g_assert_no_error (error);
901
902   addr = g_socket_get_local_address (sock1, &error);
903   g_assert_no_error (error);
904   g_socket_bind (sock2, addr, TRUE, &error);
905   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_ADDRESS_IN_USE);
906   g_clear_error (&error);
907   g_object_unref (addr);
908
909   g_object_unref (sock1);
910   g_object_unref (sock2);
911 }
912
913 static void
914 test_reuse_udp (void)
915 {
916   GSocket *sock1, *sock2;
917   GError *error = NULL;
918   GInetAddress *iaddr;
919   GSocketAddress *addr;
920
921   sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
922                         G_SOCKET_TYPE_DATAGRAM,
923                         G_SOCKET_PROTOCOL_DEFAULT,
924                         &error);
925   g_assert_no_error (error);
926
927   iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
928   addr = g_inet_socket_address_new (iaddr, 0);
929   g_object_unref (iaddr);
930   g_socket_bind (sock1, addr, TRUE, &error);
931   g_object_unref (addr);
932   g_assert_no_error (error);
933
934   sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
935                         G_SOCKET_TYPE_DATAGRAM,
936                         G_SOCKET_PROTOCOL_DEFAULT,
937                         &error);
938   g_assert_no_error (error);
939
940   addr = g_socket_get_local_address (sock1, &error);
941   g_assert_no_error (error);
942   g_socket_bind (sock2, addr, TRUE, &error);
943   g_object_unref (addr);
944   g_assert_no_error (error);
945
946   g_object_unref (sock1);
947   g_object_unref (sock2);
948 }
949
950 static void
951 test_get_available (gconstpointer user_data)
952 {
953   GSocketType socket_type = GPOINTER_TO_UINT (user_data);
954   GError *err = NULL;
955   GSocket *listener, *server, *client;
956   GInetAddress *addr;
957   GSocketAddress *saddr;
958   gchar data[] = "0123456789abcdef";
959   gchar buf[34];
960   gssize nread;
961
962   listener = g_socket_new (G_SOCKET_FAMILY_IPV4,
963                            socket_type,
964                            G_SOCKET_PROTOCOL_DEFAULT,
965                            &err);
966   g_assert_no_error (err);
967   g_assert (G_IS_SOCKET (listener));
968
969   client = g_socket_new (G_SOCKET_FAMILY_IPV4,
970                          socket_type,
971                          G_SOCKET_PROTOCOL_DEFAULT,
972                          &err);
973   g_assert_no_error (err);
974   g_assert (G_IS_SOCKET (client));
975
976   if (socket_type == G_SOCKET_TYPE_STREAM)
977     {
978       g_socket_set_option (client, IPPROTO_TCP, TCP_NODELAY, TRUE, &err);
979       g_assert_no_error (err);
980     }
981
982   addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
983   saddr = g_inet_socket_address_new (addr, 0);
984
985   g_socket_bind (listener, saddr, TRUE, &err);
986   g_assert_no_error (err);
987   g_object_unref (saddr);
988   g_object_unref (addr);
989
990   saddr = g_socket_get_local_address (listener, &err);
991   g_assert_no_error (err);
992
993   if (socket_type == G_SOCKET_TYPE_STREAM)
994     {
995       g_socket_listen (listener, &err);
996       g_assert_no_error (err);
997       g_socket_connect (client, saddr, NULL, &err);
998       g_assert_no_error (err);
999
1000       server = g_socket_accept (listener, NULL, &err);
1001       g_assert_no_error (err);
1002       g_socket_set_blocking (server, FALSE);
1003       g_object_unref (listener);
1004     }
1005   else
1006     server = listener;
1007
1008   g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
1009   g_assert_no_error (err);
1010
1011   while (!g_socket_condition_wait (server, G_IO_IN, NULL, NULL))
1012     ;
1013   g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1014
1015   g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
1016   g_assert_no_error (err);
1017
1018   /* We need to wait until the data has actually been copied into the
1019    * server socket's buffers, but g_socket_condition_wait() won't help
1020    * here since the socket is definitely already readable. So there's
1021    * a race condition in checking its available bytes. In the TCP
1022    * case, we poll for a bit until the new data shows up. In the UDP
1023    * case, there's not much we can do, but at least the failure mode
1024    * is passes-when-it-shouldn't, not fails-when-it-shouldn't.
1025    */
1026   if (socket_type == G_SOCKET_TYPE_STREAM)
1027     {
1028       int tries;
1029
1030       for (tries = 0; tries < 100; tries++)
1031         {
1032           if (g_socket_get_available_bytes (server) > sizeof (data))
1033             break;
1034           g_usleep (100000);
1035         }
1036
1037       g_assert_cmpint (g_socket_get_available_bytes (server), ==, 2 * sizeof (data));
1038     }
1039   else
1040     {
1041       g_usleep (100000);
1042       g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1043     }
1044
1045   g_assert_cmpint (sizeof (buf), >=, 2 * sizeof (data));
1046   nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
1047   g_assert_no_error (err);
1048
1049   if (socket_type == G_SOCKET_TYPE_STREAM)
1050     {
1051       g_assert_cmpint (nread, ==, 2 * sizeof (data));
1052       g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1053     }
1054   else
1055     {
1056       g_assert_cmpint (nread, ==, sizeof (data));
1057       g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1058     }
1059
1060   nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
1061   if (socket_type == G_SOCKET_TYPE_STREAM)
1062     {
1063       g_assert_cmpint (nread, ==, -1);
1064       g_assert_error (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
1065       g_clear_error (&err);
1066     }
1067   else
1068     {
1069       g_assert_cmpint (nread, ==, sizeof (data));
1070       g_assert_no_error (err);
1071     }
1072
1073   g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1074
1075   g_socket_close (server, &err);
1076   g_assert_no_error (err);
1077
1078   g_object_unref (saddr);
1079   g_object_unref (server);
1080   g_object_unref (client);
1081 }
1082
1083 int
1084 main (int   argc,
1085       char *argv[])
1086 {
1087   GSocket *sock;
1088   GError *error = NULL;
1089
1090   g_test_init (&argc, &argv, NULL);
1091
1092   sock = g_socket_new (G_SOCKET_FAMILY_IPV6,
1093                        G_SOCKET_TYPE_STREAM,
1094                        G_SOCKET_PROTOCOL_DEFAULT,
1095                        &error);
1096   if (sock != NULL)
1097     {
1098       ipv6_supported = TRUE;
1099       g_object_unref (sock);
1100     }
1101   else
1102     {
1103       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
1104       g_clear_error (&error);
1105     }
1106
1107   g_test_add_func ("/socket/ipv4_sync", test_ipv4_sync);
1108   g_test_add_func ("/socket/ipv4_async", test_ipv4_async);
1109   g_test_add_func ("/socket/ipv6_sync", test_ipv6_sync);
1110   g_test_add_func ("/socket/ipv6_async", test_ipv6_async);
1111 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
1112   g_test_add_func ("/socket/ipv6_v4mapped", test_ipv6_v4mapped);
1113 #endif
1114   g_test_add_func ("/socket/close_graceful", test_close_graceful);
1115   g_test_add_func ("/socket/timed_wait", test_timed_wait);
1116   g_test_add_func ("/socket/address", test_sockaddr);
1117 #ifdef G_OS_UNIX
1118   g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
1119   g_test_add_func ("/socket/unix-connection", test_unix_connection);
1120   g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data);
1121 #endif
1122   g_test_add_func ("/socket/reuse/tcp", test_reuse_tcp);
1123   g_test_add_func ("/socket/reuse/udp", test_reuse_udp);
1124   g_test_add_data_func ("/socket/get_available/datagram", GUINT_TO_POINTER (G_SOCKET_TYPE_DATAGRAM),
1125                         test_get_available);
1126   g_test_add_data_func ("/socket/get_available/stream", GUINT_TO_POINTER (G_SOCKET_TYPE_STREAM),
1127                         test_get_available);
1128
1129   return g_test_run();
1130 }
1131