Updated FSF's address
[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 typedef struct {
33   GSocket *server;
34   GSocket *client;
35   GSocketFamily family;
36   GThread *thread;
37   GMainLoop *loop;
38 } IPTestData;
39
40 static gpointer
41 echo_server_thread (gpointer user_data)
42 {
43   IPTestData *data = user_data;
44   GSocket *sock;
45   GError *error = NULL;
46   gssize nread, nwrote;
47   gchar buf[128];
48
49   sock = g_socket_accept (data->server, NULL, &error);
50   g_assert_no_error (error);
51
52   while (TRUE)
53     {
54       nread = g_socket_receive (sock, buf, sizeof (buf), NULL, &error);
55       g_assert_no_error (error);
56       g_assert_cmpint (nread, >=, 0);
57
58       if (nread == 0)
59         break;
60
61       nwrote = g_socket_send (sock, buf, nread, NULL, &error);
62       g_assert_no_error (error);
63       g_assert_cmpint (nwrote, ==, nread);
64     }
65
66   g_socket_close (sock, &error);
67   g_assert_no_error (error);
68   g_object_unref (sock);
69   return NULL;
70 }
71
72 static IPTestData *
73 create_server (GSocketFamily family,
74                GThreadFunc   server_thread,
75                gboolean      v4mapped)
76 {
77   IPTestData *data;
78   GSocket *server;
79   GError *error = NULL;
80   GSocketAddress *addr;
81   GInetAddress *iaddr;
82
83   data = g_slice_new (IPTestData);
84   data->family = family;
85
86   data->server = server = g_socket_new (family,
87                                         G_SOCKET_TYPE_STREAM,
88                                         G_SOCKET_PROTOCOL_DEFAULT,
89                                         &error);
90   g_assert_no_error (error);
91
92   g_assert_cmpint (g_socket_get_family (server), ==, family);
93   g_assert_cmpint (g_socket_get_socket_type (server), ==, G_SOCKET_TYPE_STREAM);
94   g_assert_cmpint (g_socket_get_protocol (server), ==, G_SOCKET_PROTOCOL_DEFAULT);
95
96   g_socket_set_blocking (server, TRUE);
97
98 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
99   if (v4mapped)
100     {
101       g_socket_set_option (data->server, IPPROTO_IPV6, IPV6_V6ONLY, FALSE, NULL);
102       if (! g_socket_speaks_ipv4 (data->server))
103         {
104           g_object_unref (data->server);
105           g_slice_free (IPTestData, data);
106           return NULL;
107         }
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   if (data == NULL)
552     {
553       g_test_message ("Test not run: not supported by the OS");
554       return;
555     }
556
557   client = g_socket_new (G_SOCKET_FAMILY_IPV4,
558                          G_SOCKET_TYPE_STREAM,
559                          G_SOCKET_PROTOCOL_DEFAULT,
560                          &error);
561   g_assert_no_error (error);
562
563   g_socket_set_blocking (client, TRUE);
564   g_socket_set_timeout (client, 1);
565
566   addr = g_socket_get_local_address (data->server, &error);
567   iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
568   v4addr = g_inet_socket_address_new (iaddr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
569   g_object_unref (iaddr);
570   g_object_unref (addr);
571
572   g_socket_connect (client, v4addr, NULL, &error);
573   g_assert_no_error (error);
574   g_assert (g_socket_is_connected (client));
575
576   g_thread_join (data->thread);
577
578   g_socket_close (client, &error);
579   g_assert_no_error (error);
580   g_socket_close (data->server, &error);
581   g_assert_no_error (error);
582
583   g_object_unref (data->server);
584   g_object_unref (client);
585   g_object_unref (v4addr);
586
587   g_slice_free (IPTestData, data);
588 }
589 #endif
590
591 static void
592 test_timed_wait (void)
593 {
594   IPTestData *data;
595   GError *error = NULL;
596   GSocket *client;
597   GSocketAddress *addr;
598   gint64 start_time;
599   gint poll_duration;
600
601   data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE);
602   addr = g_socket_get_local_address (data->server, &error);
603
604   client = g_socket_new (G_SOCKET_FAMILY_IPV4,
605                          G_SOCKET_TYPE_STREAM,
606                          G_SOCKET_PROTOCOL_DEFAULT,
607                          &error);
608   g_assert_no_error (error);
609
610   g_socket_set_blocking (client, TRUE);
611   g_socket_set_timeout (client, 1);
612
613   g_socket_connect (client, addr, NULL, &error);
614   g_assert_no_error (error);
615   g_object_unref (addr);
616
617   start_time = g_get_monotonic_time ();
618   g_socket_condition_timed_wait (client, G_IO_IN, 100000 /* 100 ms */,
619                                  NULL, &error);
620   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
621   g_clear_error (&error);
622   poll_duration = g_get_monotonic_time () - start_time;
623
624   g_assert_cmpint (poll_duration, >=, 98000);
625   g_assert_cmpint (poll_duration, <, 112000);
626
627   g_socket_close (client, &error);
628   g_assert_no_error (error);
629
630   g_thread_join (data->thread);
631
632   g_socket_close (data->server, &error);
633   g_assert_no_error (error);
634
635   g_object_unref (data->server);
636   g_object_unref (client);
637
638   g_slice_free (IPTestData, data);
639 }
640
641 static void
642 test_sockaddr (void)
643 {
644   struct sockaddr_in6 sin6, gsin6;
645   GSocketAddress *saddr;
646   GInetSocketAddress *isaddr;
647   GInetAddress *iaddr;
648   GError *error = NULL;
649
650   memset (&sin6, 0, sizeof (sin6));
651   sin6.sin6_family = AF_INET6;
652   sin6.sin6_addr = in6addr_loopback;
653   sin6.sin6_port = g_htons (42);
654   sin6.sin6_scope_id = 17;
655   sin6.sin6_flowinfo = 1729;
656
657   saddr = g_socket_address_new_from_native (&sin6, sizeof (sin6));
658   g_assert (G_IS_INET_SOCKET_ADDRESS (saddr));
659
660   isaddr = G_INET_SOCKET_ADDRESS (saddr);
661   iaddr = g_inet_socket_address_get_address (isaddr);
662   g_assert_cmpint (g_inet_address_get_family (iaddr), ==, G_SOCKET_FAMILY_IPV6);
663   g_assert (g_inet_address_get_is_loopback (iaddr));
664
665   g_assert_cmpint (g_inet_socket_address_get_port (isaddr), ==, 42);
666   g_assert_cmpint (g_inet_socket_address_get_scope_id (isaddr), ==, 17);
667   g_assert_cmpint (g_inet_socket_address_get_flowinfo (isaddr), ==, 1729);
668
669   g_socket_address_to_native (saddr, &gsin6, sizeof (gsin6), &error);
670   g_assert_no_error (error);
671
672   g_assert (memcmp (&sin6.sin6_addr, &gsin6.sin6_addr, sizeof (struct in6_addr)) == 0);
673   g_assert_cmpint (sin6.sin6_port, ==, gsin6.sin6_port);
674   g_assert_cmpint (sin6.sin6_scope_id, ==, gsin6.sin6_scope_id);
675   g_assert_cmpint (sin6.sin6_flowinfo, ==, gsin6.sin6_flowinfo);
676
677   g_object_unref (saddr);
678 }
679
680 #ifdef G_OS_UNIX
681 static void
682 test_unix_from_fd (void)
683 {
684   gint fd;
685   GError *error;
686   GSocket *s;
687
688   fd = socket (AF_UNIX, SOCK_STREAM, 0);
689   g_assert_cmpint (fd, !=, -1);
690
691   error = NULL;
692   s = g_socket_new_from_fd (fd, &error);
693   g_assert_no_error (error);
694   g_assert_cmpint (g_socket_get_family (s), ==, G_SOCKET_FAMILY_UNIX);
695   g_assert_cmpint (g_socket_get_socket_type (s), ==, G_SOCKET_TYPE_STREAM);
696   g_assert_cmpint (g_socket_get_protocol (s), ==, G_SOCKET_PROTOCOL_DEFAULT);
697   g_object_unref (s);
698 }
699
700 static void
701 test_unix_connection (void)
702 {
703   gint fd;
704   GError *error;
705   GSocket *s;
706   GSocketConnection *c;
707
708   fd = socket (AF_UNIX, SOCK_STREAM, 0);
709   g_assert_cmpint (fd, !=, -1);
710
711   error = NULL;
712   s = g_socket_new_from_fd (fd, &error);
713   g_assert_no_error (error);
714   c = g_socket_connection_factory_create_connection (s);
715   g_assert (G_IS_UNIX_CONNECTION (c));
716   g_object_unref (c);
717   g_object_unref (s);
718 }
719
720 static GSocketConnection *
721 create_connection_for_fd (int fd)
722 {
723   GError *err = NULL;
724   GSocket *socket;
725   GSocketConnection *connection;
726
727   socket = g_socket_new_from_fd (fd, &err);
728   g_assert_no_error (err);
729   g_assert (G_IS_SOCKET (socket));
730   connection = g_socket_connection_factory_create_connection (socket);
731   g_assert (G_IS_UNIX_CONNECTION (connection));
732   g_object_unref (socket);
733   return connection;
734 }
735
736 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
737
738 static void
739 test_unix_connection_ancillary_data (void)
740 {
741   GError *err = NULL;
742   gint pv[2], sv[3];
743   gint status, fd, len;
744   char buffer[1024];
745   pid_t pid;
746
747   status = pipe (pv);
748   g_assert_cmpint (status, ==, 0);
749
750   status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
751   g_assert_cmpint (status, ==, 0);
752
753   pid = fork ();
754   g_assert_cmpint (pid, >=, 0);
755
756   /* Child: close its copy of the write end of the pipe, receive it
757    * again from the parent over the socket, and write some text to it.
758    *
759    * Parent: send the write end of the pipe (still open for the
760    * parent) over the socket, close it, and read some text from the
761    * read end of the pipe.
762    */
763   if (pid == 0)
764     {
765       GSocketConnection *connection;
766
767       close (sv[1]);
768       connection = create_connection_for_fd (sv[0]);
769
770       status = close (pv[1]);
771       g_assert_cmpint (status, ==, 0);
772
773       err = NULL;
774       fd = g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection), NULL,
775                                          &err);
776       g_assert_no_error (err);
777       g_assert_cmpint (fd, >, -1);
778       g_object_unref (connection);
779
780       do
781         len = write (fd, TEST_DATA, sizeof (TEST_DATA));
782       while (len == -1 && errno == EINTR);
783       g_assert_cmpint (len, ==, sizeof (TEST_DATA));
784       exit (0);
785     }
786   else
787     {
788       GSocketConnection *connection;
789
790       close (sv[0]);
791       connection = create_connection_for_fd (sv[1]);
792
793       err = NULL;
794       g_unix_connection_send_fd (G_UNIX_CONNECTION (connection), pv[1], NULL,
795                                  &err);
796       g_assert_no_error (err);
797       g_object_unref (connection);
798
799       status = close (pv[1]);
800       g_assert_cmpint (status, ==, 0);
801
802       memset (buffer, 0xff, sizeof buffer);
803       do
804         len = read (pv[0], buffer, sizeof buffer);
805       while (len == -1 && errno == EINTR);
806
807       g_assert_cmpint (len, ==, sizeof (TEST_DATA));
808       g_assert_cmpstr (buffer, ==, TEST_DATA);
809
810       waitpid (pid, &status, 0);
811       g_assert (WIFEXITED (status));
812       g_assert_cmpint (WEXITSTATUS (status), ==, 0);
813     }
814
815   /* TODO: add test for g_unix_connection_send_credentials() and
816    * g_unix_connection_receive_credentials().
817    */
818 }
819 #endif /* G_OS_UNIX */
820
821 static void
822 test_reuse_tcp (void)
823 {
824   GSocket *sock1, *sock2;
825   GError *error = NULL;
826   GInetAddress *iaddr;
827   GSocketAddress *addr;
828
829   sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
830                         G_SOCKET_TYPE_STREAM,
831                         G_SOCKET_PROTOCOL_DEFAULT,
832                         &error);
833   g_assert_no_error (error);
834
835   iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
836   addr = g_inet_socket_address_new (iaddr, 0);
837   g_object_unref (iaddr);
838   g_socket_bind (sock1, addr, TRUE, &error);
839   g_object_unref (addr);
840   g_assert_no_error (error);
841
842   g_socket_listen (sock1, &error);
843   g_assert_no_error (error);
844
845   sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
846                         G_SOCKET_TYPE_STREAM,
847                         G_SOCKET_PROTOCOL_DEFAULT,
848                         &error);
849   g_assert_no_error (error);
850
851   addr = g_socket_get_local_address (sock1, &error);
852   g_assert_no_error (error);
853   g_socket_bind (sock2, addr, TRUE, &error);
854   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_ADDRESS_IN_USE);
855   g_object_unref (addr);
856
857   g_object_unref (sock1);
858   g_object_unref (sock2);
859 }
860
861 static void
862 test_reuse_udp (void)
863 {
864   GSocket *sock1, *sock2;
865   GError *error = NULL;
866   GInetAddress *iaddr;
867   GSocketAddress *addr;
868
869   sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
870                         G_SOCKET_TYPE_DATAGRAM,
871                         G_SOCKET_PROTOCOL_DEFAULT,
872                         &error);
873   g_assert_no_error (error);
874
875   iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
876   addr = g_inet_socket_address_new (iaddr, 0);
877   g_object_unref (iaddr);
878   g_socket_bind (sock1, addr, TRUE, &error);
879   g_object_unref (addr);
880   g_assert_no_error (error);
881
882   sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
883                         G_SOCKET_TYPE_DATAGRAM,
884                         G_SOCKET_PROTOCOL_DEFAULT,
885                         &error);
886   g_assert_no_error (error);
887
888   addr = g_socket_get_local_address (sock1, &error);
889   g_assert_no_error (error);
890   g_socket_bind (sock2, addr, TRUE, &error);
891   g_object_unref (addr);
892   g_assert_no_error (error);
893
894   g_object_unref (sock1);
895   g_object_unref (sock2);
896 }
897
898 static void
899 test_datagram_get_available (void)
900 {
901   GError *err = NULL;
902   GSocket *server, *client;
903   GInetAddress *addr;
904   GSocketAddress *saddr;
905   gchar data[] = "0123456789abcdef";
906   gchar buf[34];
907   gssize nread;
908
909   server = g_socket_new (G_SOCKET_FAMILY_IPV4,
910                                 G_SOCKET_TYPE_DATAGRAM,
911                                 G_SOCKET_PROTOCOL_DEFAULT,
912                                 &err);
913   g_assert_no_error (err);
914   g_assert (G_IS_SOCKET (server));
915
916   client = g_socket_new (G_SOCKET_FAMILY_IPV4,
917                          G_SOCKET_TYPE_DATAGRAM,
918                          G_SOCKET_PROTOCOL_DEFAULT,
919                          &err);
920   g_assert_no_error (err);
921   g_assert (G_IS_SOCKET (client));
922
923   addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
924   saddr = g_inet_socket_address_new (addr, 0);
925
926   g_socket_bind (server, saddr, TRUE, &err);
927   g_assert_no_error (err);
928   g_object_unref (saddr);
929   g_object_unref (addr);
930
931   saddr = g_socket_get_local_address (server, &err);
932   g_assert_no_error (err);
933
934   g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
935   g_assert_no_error (err);
936
937   while (!g_socket_condition_wait (server, G_IO_IN, NULL, NULL))
938     ;
939   g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
940
941   g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
942   g_assert_no_error (err);
943
944   /* g_socket_condition_wait() won't help here since the socket is
945    * definitely already readable. So there's a race condition here, but
946    * at least the failure mode is passes-when-it-shouldn't, not
947    * fails-when-it-shouldn't.
948    */
949   g_usleep (100000);
950   g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
951
952   g_assert_cmpint (sizeof (buf), >=, 2 * sizeof (data));
953   nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
954   g_assert_cmpint (nread, ==, sizeof (data));
955   g_assert_no_error (err);
956
957   g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
958
959   nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
960   g_assert_cmpint (nread, ==, sizeof (data));
961   g_assert_no_error (err);
962
963   g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
964
965   g_socket_close (server, &err);
966   g_assert_no_error (err);
967
968   g_object_unref (saddr);
969   g_object_unref (server);
970   g_object_unref (client);
971 }
972
973 int
974 main (int   argc,
975       char *argv[])
976 {
977   g_test_init (&argc, &argv, NULL);
978
979   g_test_add_func ("/socket/ipv4_sync", test_ipv4_sync);
980   g_test_add_func ("/socket/ipv4_async", test_ipv4_async);
981   g_test_add_func ("/socket/ipv6_sync", test_ipv6_sync);
982   g_test_add_func ("/socket/ipv6_async", test_ipv6_async);
983 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
984   g_test_add_func ("/socket/ipv6_v4mapped", test_ipv6_v4mapped);
985 #endif
986   g_test_add_func ("/socket/close_graceful", test_close_graceful);
987   g_test_add_func ("/socket/timed_wait", test_timed_wait);
988   g_test_add_func ("/socket/address", test_sockaddr);
989 #ifdef G_OS_UNIX
990   g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
991   g_test_add_func ("/socket/unix-connection", test_unix_connection);
992   g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data);
993 #endif
994   g_test_add_func ("/socket/reuse/tcp", test_reuse_tcp);
995   g_test_add_func ("/socket/reuse/udp", test_reuse_udp);
996   g_test_add_func ("/socket/datagram_get_available", test_datagram_get_available);
997
998   return g_test_run();
999 }
1000