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