Add gnetworking.h
[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, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <gio/gio.h>
22
23 #ifdef G_OS_UNIX
24 #include <errno.h>
25 #include <sys/wait.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <gio/gnetworking.h>
29 #include <gio/gunixconnection.h>
30 #endif
31
32 #include "gnetworkingprivate.h"
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       int fd, v6_only;
104
105       fd = g_socket_get_fd (server);
106       v6_only = 0;
107       setsockopt (fd, IPPROTO_IPV6, IPV6_V6ONLY, &v6_only, sizeof (v6_only));
108     }
109 #endif
110
111   if (v4mapped)
112     iaddr = g_inet_address_new_any (family);
113   else
114     iaddr = g_inet_address_new_loopback (family);
115   addr = g_inet_socket_address_new (iaddr, 0);
116   g_object_unref (iaddr);
117
118   g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), ==, 0);
119   g_socket_bind (server, addr, TRUE, &error);
120   g_assert_no_error (error);
121   g_object_unref (addr);
122
123   addr = g_socket_get_local_address (server, &error);
124   g_assert_no_error (error);
125   g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), !=, 0);
126   g_object_unref (addr);
127
128   g_socket_listen (server, &error);
129   g_assert_no_error (error);
130
131   data->thread = g_thread_new ("server", server_thread, data);
132
133   return data;
134 }
135
136 static const gchar *testbuf = "0123456789abcdef";
137
138 static gboolean
139 test_ip_async_read_ready (GSocket      *client,
140                           GIOCondition  cond,
141                           gpointer      user_data)
142 {
143   IPTestData *data = user_data;
144   GError *error = NULL;
145   gssize len;
146   gchar buf[128];
147
148   g_assert_cmpint (cond, ==, G_IO_IN);
149
150   len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
151   g_assert_no_error (error);
152   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
153
154   g_assert_cmpstr (testbuf, ==, buf);
155
156   g_main_loop_quit (data->loop);
157
158   return FALSE;
159 }
160
161 static gboolean
162 test_ip_async_write_ready (GSocket      *client,
163                            GIOCondition  cond,
164                            gpointer      user_data)
165 {
166   IPTestData *data = user_data;
167   GError *error = NULL;
168   GSource *source;
169   gssize len;
170
171   g_assert_cmpint (cond, ==, G_IO_OUT);
172
173   len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
174   g_assert_no_error (error);
175   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
176
177   source = g_socket_create_source (client, G_IO_IN, NULL);
178   g_source_set_callback (source, (GSourceFunc)test_ip_async_read_ready,
179                          data, NULL);
180   g_source_attach (source, NULL);
181   g_source_unref (source);
182
183   return FALSE;
184 }
185
186 static gboolean
187 test_ip_async_timed_out (GSocket      *client,
188                          GIOCondition  cond,
189                          gpointer      user_data)
190 {
191   IPTestData *data = user_data;
192   GError *error = NULL;
193   GSource *source;
194   gssize len;
195   gchar buf[128];
196
197   if (data->family == G_SOCKET_FAMILY_IPV4)
198     {
199       g_assert_cmpint (cond, ==, G_IO_IN);
200       len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
201       g_assert_cmpint (len, ==, -1);
202       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
203       g_clear_error (&error);
204     }
205
206   source = g_socket_create_source (client, G_IO_OUT, NULL);
207   g_source_set_callback (source, (GSourceFunc)test_ip_async_write_ready,
208                          data, NULL);
209   g_source_attach (source, NULL);
210   g_source_unref (source);
211
212   return FALSE;
213 }
214
215 static gboolean
216 test_ip_async_connected (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   gchar buf[128];
225
226   g_socket_check_connect_result (client, &error);
227   g_assert_no_error (error);
228   /* We do this after the check_connect_result, since that will give a
229    * more useful assertion in case of error.
230    */
231   g_assert_cmpint (cond, ==, G_IO_OUT);
232
233   g_assert (g_socket_is_connected (client));
234
235   /* This adds 1 second to "make check", so let's just only do it once. */
236   if (data->family == G_SOCKET_FAMILY_IPV4)
237     {
238       len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
239       g_assert_cmpint (len, ==, -1);
240       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
241       g_clear_error (&error);
242
243       source = g_socket_create_source (client, G_IO_IN, NULL);
244       g_source_set_callback (source, (GSourceFunc)test_ip_async_timed_out,
245                              data, NULL);
246       g_source_attach (source, NULL);
247       g_source_unref (source);
248     }
249   else
250     test_ip_async_timed_out (client, 0, data);
251
252   return FALSE;
253 }
254
255 static gboolean
256 idle_test_ip_async_connected (gpointer user_data)
257 {
258   IPTestData *data = user_data;
259
260   return test_ip_async_connected (data->client, G_IO_OUT, data);
261 }
262
263 static void
264 test_ip_async (GSocketFamily family)
265 {
266   IPTestData *data;
267   GError *error = NULL;
268   GSocket *client;
269   GSocketAddress *addr;
270   GSource *source;
271   gssize len;
272   gchar buf[128];
273
274   data = create_server (family, echo_server_thread, FALSE);
275   addr = g_socket_get_local_address (data->server, &error);
276
277   client = g_socket_new (family,
278                          G_SOCKET_TYPE_STREAM,
279                          G_SOCKET_PROTOCOL_DEFAULT,
280                          &error);
281   g_assert_no_error (error);
282   data->client = client;
283
284   g_assert_cmpint (g_socket_get_family (client), ==, family);
285   g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
286   g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
287
288   g_socket_set_blocking (client, FALSE);
289   g_socket_set_timeout (client, 1);
290
291   if (g_socket_connect (client, addr, NULL, &error))
292     {
293       g_assert_no_error (error);
294       g_idle_add (idle_test_ip_async_connected, data);
295     }
296   else
297     {
298       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
299       g_clear_error (&error);
300       source = g_socket_create_source (client, G_IO_OUT, NULL);
301       g_source_set_callback (source, (GSourceFunc)test_ip_async_connected,
302                              data, NULL);
303       g_source_attach (source, NULL);
304       g_source_unref (source);
305     }
306   g_object_unref (addr);
307
308   data->loop = g_main_loop_new (NULL, TRUE);
309   g_main_loop_run (data->loop);
310   g_main_loop_unref (data->loop);
311
312   g_socket_shutdown (client, FALSE, TRUE, &error);
313   g_assert_no_error (error);
314
315   g_thread_join (data->thread);
316
317   len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
318   g_assert_no_error (error);
319   g_assert_cmpint (len, ==, 0);
320
321   g_socket_close (client, &error);
322   g_assert_no_error (error);
323   g_socket_close (data->server, &error);
324   g_assert_no_error (error);
325
326   g_object_unref (data->server);
327   g_object_unref (client);
328
329   g_slice_free (IPTestData, data);
330 }
331
332 static void
333 test_ipv4_async (void)
334 {
335   test_ip_async (G_SOCKET_FAMILY_IPV4);
336 }
337
338 static void
339 test_ipv6_async (void)
340 {
341   test_ip_async (G_SOCKET_FAMILY_IPV6);
342 }
343
344 static void
345 test_ip_sync (GSocketFamily family)
346 {
347   IPTestData *data;
348   GError *error = NULL;
349   GSocket *client;
350   GSocketAddress *addr;
351   gssize len;
352   gchar buf[128];
353
354   data = create_server (family, echo_server_thread, FALSE);
355   addr = g_socket_get_local_address (data->server, &error);
356
357   client = g_socket_new (family,
358                          G_SOCKET_TYPE_STREAM,
359                          G_SOCKET_PROTOCOL_DEFAULT,
360                          &error);
361   g_assert_no_error (error);
362
363   g_assert_cmpint (g_socket_get_family (client), ==, family);
364   g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
365   g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
366
367   g_socket_set_blocking (client, TRUE);
368   g_socket_set_timeout (client, 1);
369
370   g_socket_connect (client, addr, NULL, &error);
371   g_assert_no_error (error);
372   g_assert (g_socket_is_connected (client));
373   g_object_unref (addr);
374
375   /* This adds 1 second to "make check", so let's just only do it once. */
376   if (family == G_SOCKET_FAMILY_IPV4)
377     {
378       len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
379       g_assert_cmpint (len, ==, -1);
380       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
381       g_clear_error (&error);
382     }
383
384   len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
385   g_assert_no_error (error);
386   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
387   
388   len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
389   g_assert_no_error (error);
390   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
391
392   g_assert_cmpstr (testbuf, ==, buf);
393
394   g_socket_shutdown (client, FALSE, TRUE, &error);
395   g_assert_no_error (error);
396
397   g_thread_join (data->thread);
398
399   len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
400   g_assert_no_error (error);
401   g_assert_cmpint (len, ==, 0);
402
403   g_socket_close (client, &error);
404   g_assert_no_error (error);
405   g_socket_close (data->server, &error);
406   g_assert_no_error (error);
407
408   g_object_unref (data->server);
409   g_object_unref (client);
410
411   g_slice_free (IPTestData, data);
412 }
413
414 static void
415 test_ipv4_sync (void)
416 {
417   test_ip_sync (G_SOCKET_FAMILY_IPV4);
418 }
419
420 static void
421 test_ipv6_sync (void)
422 {
423   test_ip_sync (G_SOCKET_FAMILY_IPV6);
424 }
425
426 static gpointer
427 graceful_server_thread (gpointer user_data)
428 {
429   IPTestData *data = user_data;
430   GSocket *sock;
431   GError *error = NULL;
432   gssize len;
433
434   sock = g_socket_accept (data->server, NULL, &error);
435   g_assert_no_error (error);
436
437   len = g_socket_send (sock, testbuf, strlen (testbuf) + 1, NULL, &error);
438   g_assert_no_error (error);
439   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
440
441   return sock;
442 }
443
444 static void
445 test_close_graceful (void)
446 {
447   GSocketFamily family = G_SOCKET_FAMILY_IPV4;
448   IPTestData *data;
449   GError *error = NULL;
450   GSocket *client, *server;
451   GSocketAddress *addr;
452   gssize len;
453   gchar buf[128];
454
455   data = create_server (family, graceful_server_thread, FALSE);
456   addr = g_socket_get_local_address (data->server, &error);
457
458   client = g_socket_new (family,
459                          G_SOCKET_TYPE_STREAM,
460                          G_SOCKET_PROTOCOL_DEFAULT,
461                          &error);
462   g_assert_no_error (error);
463
464   g_assert_cmpint (g_socket_get_family (client), ==, family);
465   g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
466   g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
467
468   g_socket_set_blocking (client, TRUE);
469   g_socket_set_timeout (client, 1);
470
471   g_socket_connect (client, addr, NULL, &error);
472   g_assert_no_error (error);
473   g_assert (g_socket_is_connected (client));
474   g_object_unref (addr);
475
476   server = g_thread_join (data->thread);
477
478   /* similar to g_tcp_connection_set_graceful_disconnect(), but explicit */
479   g_socket_shutdown (server, FALSE, TRUE, &error);
480   g_assert_no_error (error);
481
482   /* we must timeout */
483   g_socket_condition_wait (client, G_IO_HUP, NULL, &error);
484   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
485   g_clear_error (&error);
486
487   /* check that the remaining data is received */
488   len = g_socket_receive (client, buf, strlen (testbuf) + 1, NULL, &error);
489   g_assert_no_error (error);
490   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
491
492   /* and only then the connection is closed */
493   len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
494   g_assert_no_error (error);
495   g_assert_cmpint (len, ==, 0);
496
497   g_socket_close (server, &error);
498   g_assert_no_error (error);
499
500   g_socket_close (client, &error);
501   g_assert_no_error (error);
502
503   g_object_unref (server);
504   g_object_unref (data->server);
505   g_object_unref (client);
506
507   g_slice_free (IPTestData, data);
508 }
509
510 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
511 static gpointer
512 v4mapped_server_thread (gpointer user_data)
513 {
514   IPTestData *data = user_data;
515   GSocket *sock;
516   GError *error = NULL;
517   GSocketAddress *addr;
518
519   sock = g_socket_accept (data->server, NULL, &error);
520   g_assert_no_error (error);
521
522   g_assert_cmpint (g_socket_get_family (sock), ==, G_SOCKET_FAMILY_IPV6);
523
524   addr = g_socket_get_local_address (sock, &error);
525   g_assert_no_error (error);
526   g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
527   g_object_unref (addr);
528
529   addr = g_socket_get_remote_address (sock, &error);
530   g_assert_no_error (error);
531   g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
532   g_object_unref (addr);
533
534   g_socket_close (sock, &error);
535   g_assert_no_error (error);
536   g_object_unref (sock);
537   return NULL;
538 }
539
540 static void
541 test_ipv6_v4mapped (void)
542 {
543   IPTestData *data;
544   GError *error = NULL;
545   GSocket *client;
546   GSocketAddress *addr, *v4addr;
547   GInetAddress *iaddr;
548
549   data = create_server (G_SOCKET_FAMILY_IPV6, v4mapped_server_thread, TRUE);
550
551   client = g_socket_new (G_SOCKET_FAMILY_IPV4,
552                          G_SOCKET_TYPE_STREAM,
553                          G_SOCKET_PROTOCOL_DEFAULT,
554                          &error);
555   g_assert_no_error (error);
556
557   g_socket_set_blocking (client, TRUE);
558   g_socket_set_timeout (client, 1);
559
560   addr = g_socket_get_local_address (data->server, &error);
561   iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
562   v4addr = g_inet_socket_address_new (iaddr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
563   g_object_unref (iaddr);
564   g_object_unref (addr);
565
566   g_socket_connect (client, v4addr, NULL, &error);
567   g_assert_no_error (error);
568   g_assert (g_socket_is_connected (client));
569
570   g_thread_join (data->thread);
571
572   g_socket_close (client, &error);
573   g_assert_no_error (error);
574   g_socket_close (data->server, &error);
575   g_assert_no_error (error);
576
577   g_object_unref (data->server);
578   g_object_unref (client);
579   g_object_unref (v4addr);
580
581   g_slice_free (IPTestData, data);
582 }
583 #endif
584
585 static void
586 test_timed_wait (void)
587 {
588   IPTestData *data;
589   GError *error = NULL;
590   GSocket *client;
591   GSocketAddress *addr;
592   gint64 start_time;
593   gint poll_duration;
594
595   data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE);
596   addr = g_socket_get_local_address (data->server, &error);
597
598   client = g_socket_new (G_SOCKET_FAMILY_IPV4,
599                          G_SOCKET_TYPE_STREAM,
600                          G_SOCKET_PROTOCOL_DEFAULT,
601                          &error);
602   g_assert_no_error (error);
603
604   g_socket_set_blocking (client, TRUE);
605   g_socket_set_timeout (client, 1);
606
607   g_socket_connect (client, addr, NULL, &error);
608   g_assert_no_error (error);
609   g_object_unref (addr);
610
611   start_time = g_get_monotonic_time ();
612   g_socket_condition_timed_wait (client, G_IO_IN, 100000 /* 100 ms */,
613                                  NULL, &error);
614   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
615   g_clear_error (&error);
616   poll_duration = g_get_monotonic_time () - start_time;
617
618   g_assert_cmpint (poll_duration, >=, 98000);
619   g_assert_cmpint (poll_duration, <, 112000);
620
621   g_socket_close (client, &error);
622   g_assert_no_error (error);
623
624   g_thread_join (data->thread);
625
626   g_socket_close (data->server, &error);
627   g_assert_no_error (error);
628
629   g_object_unref (data->server);
630   g_object_unref (client);
631
632   g_slice_free (IPTestData, data);
633 }
634
635 static void
636 test_sockaddr (void)
637 {
638   struct sockaddr_in6 sin6, gsin6;
639   GSocketAddress *saddr;
640   GInetSocketAddress *isaddr;
641   GInetAddress *iaddr;
642   GError *error = NULL;
643
644   memset (&sin6, 0, sizeof (sin6));
645   sin6.sin6_family = AF_INET6;
646   sin6.sin6_addr = in6addr_loopback;
647   sin6.sin6_port = g_htons (42);
648   sin6.sin6_scope_id = g_htonl (17);
649   sin6.sin6_flowinfo = g_htonl (1729);
650
651   saddr = g_socket_address_new_from_native (&sin6, sizeof (sin6));
652   g_assert (G_IS_INET_SOCKET_ADDRESS (saddr));
653
654   isaddr = G_INET_SOCKET_ADDRESS (saddr);
655   iaddr = g_inet_socket_address_get_address (isaddr);
656   g_assert_cmpint (g_inet_address_get_family (iaddr), ==, G_SOCKET_FAMILY_IPV6);
657   g_assert (g_inet_address_get_is_loopback (iaddr));
658
659   g_assert_cmpint (g_inet_socket_address_get_port (isaddr), ==, 42);
660   g_assert_cmpint (g_inet_socket_address_get_scope_id (isaddr), ==, 17);
661   g_assert_cmpint (g_inet_socket_address_get_flowinfo (isaddr), ==, 1729);
662
663   g_socket_address_to_native (saddr, &gsin6, sizeof (gsin6), &error);
664   g_assert_no_error (error);
665
666   g_assert (memcmp (&sin6.sin6_addr, &gsin6.sin6_addr, sizeof (struct in6_addr)) == 0);
667   g_assert_cmpint (sin6.sin6_port, ==, gsin6.sin6_port);
668   g_assert_cmpint (sin6.sin6_scope_id, ==, gsin6.sin6_scope_id);
669   g_assert_cmpint (sin6.sin6_flowinfo, ==, gsin6.sin6_flowinfo);
670
671   g_object_unref (saddr);
672 }
673
674 #ifdef G_OS_UNIX
675 static void
676 test_unix_from_fd (void)
677 {
678   gint fd;
679   GError *error;
680   GSocket *s;
681
682   fd = socket (AF_UNIX, SOCK_STREAM, 0);
683   g_assert_cmpint (fd, !=, -1);
684
685   error = NULL;
686   s = g_socket_new_from_fd (fd, &error);
687   g_assert_no_error (error);
688   g_assert_cmpint (g_socket_get_family (s), ==, G_SOCKET_FAMILY_UNIX);
689   g_assert_cmpint (g_socket_get_socket_type (s), ==, G_SOCKET_TYPE_STREAM);
690   g_assert_cmpint (g_socket_get_protocol (s), ==, G_SOCKET_PROTOCOL_DEFAULT);
691   g_object_unref (s);
692 }
693
694 static void
695 test_unix_connection (void)
696 {
697   gint fd;
698   GError *error;
699   GSocket *s;
700   GSocketConnection *c;
701
702   fd = socket (AF_UNIX, SOCK_STREAM, 0);
703   g_assert_cmpint (fd, !=, -1);
704
705   error = NULL;
706   s = g_socket_new_from_fd (fd, &error);
707   g_assert_no_error (error);
708   c = g_socket_connection_factory_create_connection (s);
709   g_assert (G_IS_UNIX_CONNECTION (c));
710   g_object_unref (c);
711   g_object_unref (s);
712 }
713
714 static GSocketConnection *
715 create_connection_for_fd (int fd)
716 {
717   GError *err = NULL;
718   GSocket *socket;
719   GSocketConnection *connection;
720
721   socket = g_socket_new_from_fd (fd, &err);
722   g_assert_no_error (err);
723   g_assert (G_IS_SOCKET (socket));
724   connection = g_socket_connection_factory_create_connection (socket);
725   g_assert (G_IS_UNIX_CONNECTION (connection));
726   g_object_unref (socket);
727   return connection;
728 }
729
730 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
731
732 static void
733 test_unix_connection_ancillary_data (void)
734 {
735   GError *err = NULL;
736   gint pv[2], sv[3];
737   gint status, fd, len;
738   char buffer[1024];
739   pid_t pid;
740
741   status = pipe (pv);
742   g_assert_cmpint (status, ==, 0);
743
744   status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
745   g_assert_cmpint (status, ==, 0);
746
747   pid = fork ();
748   g_assert_cmpint (pid, >=, 0);
749
750   /* Child: close its copy of the write end of the pipe, receive it
751    * again from the parent over the socket, and write some text to it.
752    *
753    * Parent: send the write end of the pipe (still open for the
754    * parent) over the socket, close it, and read some text from the
755    * read end of the pipe.
756    */
757   if (pid == 0)
758     {
759       GSocketConnection *connection;
760
761       close (sv[1]);
762       connection = create_connection_for_fd (sv[0]);
763
764       status = close (pv[1]);
765       g_assert_cmpint (status, ==, 0);
766
767       err = NULL;
768       fd = g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection), NULL,
769                                          &err);
770       g_assert_no_error (err);
771       g_assert_cmpint (fd, >, -1);
772       g_object_unref (connection);
773
774       do
775         len = write (fd, TEST_DATA, sizeof (TEST_DATA));
776       while (len == -1 && errno == EINTR);
777       g_assert_cmpint (len, ==, sizeof (TEST_DATA));
778       exit (0);
779     }
780   else
781     {
782       GSocketConnection *connection;
783
784       close (sv[0]);
785       connection = create_connection_for_fd (sv[1]);
786
787       err = NULL;
788       g_unix_connection_send_fd (G_UNIX_CONNECTION (connection), pv[1], NULL,
789                                  &err);
790       g_assert_no_error (err);
791       g_object_unref (connection);
792
793       status = close (pv[1]);
794       g_assert_cmpint (status, ==, 0);
795
796       memset (buffer, 0xff, sizeof buffer);
797       do
798         len = read (pv[0], buffer, sizeof buffer);
799       while (len == -1 && errno == EINTR);
800
801       g_assert_cmpint (len, ==, sizeof (TEST_DATA));
802       g_assert_cmpstr (buffer, ==, TEST_DATA);
803
804       waitpid (pid, &status, 0);
805       g_assert (WIFEXITED (status));
806       g_assert_cmpint (WEXITSTATUS (status), ==, 0);
807     }
808
809   /* TODO: add test for g_unix_connection_send_credentials() and
810    * g_unix_connection_receive_credentials().
811    */
812 }
813 #endif /* G_OS_UNIX */
814
815 int
816 main (int   argc,
817       char *argv[])
818 {
819   g_test_init (&argc, &argv, NULL);
820
821   g_test_add_func ("/socket/ipv4_sync", test_ipv4_sync);
822   g_test_add_func ("/socket/ipv4_async", test_ipv4_async);
823   g_test_add_func ("/socket/ipv6_sync", test_ipv6_sync);
824   g_test_add_func ("/socket/ipv6_async", test_ipv6_async);
825 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
826   g_test_add_func ("/socket/ipv6_v4mapped", test_ipv6_v4mapped);
827 #endif
828   g_test_add_func ("/socket/close_graceful", test_close_graceful);
829   g_test_add_func ("/socket/timed_wait", test_timed_wait);
830   g_test_add_func ("/socket/address", test_sockaddr);
831 #ifdef G_OS_UNIX
832   g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
833   g_test_add_func ("/socket/unix-connection", test_unix_connection);
834   g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data);
835 #endif
836
837   return g_test_run();
838 }
839