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