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