gio/tests/socket: add datagram version of test_ip_sync
[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   GCancellable *cancellable; /* to shut down dgram echo server thread */
41 } IPTestData;
42
43 static gpointer
44 echo_server_dgram_thread (gpointer user_data)
45 {
46   IPTestData *data = user_data;
47   GSocketAddress *sa;
48   GCancellable *cancellable = data->cancellable;
49   GSocket *sock;
50   GError *error = NULL;
51   gssize nread, nwrote;
52   gchar buf[128];
53
54   sock = data->server;
55
56   while (TRUE)
57     {
58       nread = g_socket_receive_from (sock, &sa, buf, sizeof (buf), cancellable, &error);
59       if (error && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
60         break;
61       g_assert_no_error (error);
62       g_assert_cmpint (nread, >=, 0);
63
64       nwrote = g_socket_send_to (sock, sa, buf, nread, cancellable, &error);
65       if (error && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
66         break;
67       g_assert_no_error (error);
68       g_assert_cmpint (nwrote, ==, nread);
69
70       g_object_unref (sa);
71     }
72
73   g_clear_error (&error);
74
75   return NULL;
76 }
77
78 static gpointer
79 echo_server_thread (gpointer user_data)
80 {
81   IPTestData *data = user_data;
82   GSocket *sock;
83   GError *error = NULL;
84   gssize nread, nwrote;
85   gchar buf[128];
86
87   sock = g_socket_accept (data->server, NULL, &error);
88   g_assert_no_error (error);
89
90   while (TRUE)
91     {
92       nread = g_socket_receive (sock, buf, sizeof (buf), NULL, &error);
93       g_assert_no_error (error);
94       g_assert_cmpint (nread, >=, 0);
95
96       if (nread == 0)
97         break;
98
99       nwrote = g_socket_send (sock, buf, nread, NULL, &error);
100       g_assert_no_error (error);
101       g_assert_cmpint (nwrote, ==, nread);
102     }
103
104   g_socket_close (sock, &error);
105   g_assert_no_error (error);
106   g_object_unref (sock);
107   return NULL;
108 }
109
110 static IPTestData *
111 create_server_full (GSocketFamily family,
112                     GSocketType   socket_type,
113                     GThreadFunc   server_thread,
114                     gboolean      v4mapped)
115 {
116   IPTestData *data;
117   GSocket *server;
118   GError *error = NULL;
119   GSocketAddress *addr;
120   GInetAddress *iaddr;
121
122   data = g_slice_new (IPTestData);
123   data->family = family;
124
125   data->server = server = g_socket_new (family,
126                                         socket_type,
127                                         G_SOCKET_PROTOCOL_DEFAULT,
128                                         &error);
129   g_assert_no_error (error);
130
131   g_assert_cmpint (g_socket_get_family (server), ==, family);
132   g_assert_cmpint (g_socket_get_socket_type (server), ==, socket_type);
133   g_assert_cmpint (g_socket_get_protocol (server), ==, G_SOCKET_PROTOCOL_DEFAULT);
134
135   g_socket_set_blocking (server, TRUE);
136
137 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
138   if (v4mapped)
139     {
140       g_socket_set_option (data->server, IPPROTO_IPV6, IPV6_V6ONLY, FALSE, NULL);
141       if (! g_socket_speaks_ipv4 (data->server))
142         {
143           g_object_unref (data->server);
144           g_slice_free (IPTestData, data);
145           return NULL;
146         }
147     }
148 #endif
149
150   if (v4mapped)
151     iaddr = g_inet_address_new_any (family);
152   else
153     iaddr = g_inet_address_new_loopback (family);
154   addr = g_inet_socket_address_new (iaddr, 0);
155   g_object_unref (iaddr);
156
157   g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), ==, 0);
158   g_socket_bind (server, addr, TRUE, &error);
159   g_assert_no_error (error);
160   g_object_unref (addr);
161
162   addr = g_socket_get_local_address (server, &error);
163   g_assert_no_error (error);
164   g_assert_cmpint (g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)), !=, 0);
165   g_object_unref (addr);
166
167   if (socket_type == G_SOCKET_TYPE_STREAM)
168     {
169       g_socket_listen (server, &error);
170       g_assert_no_error (error);
171     }
172   else
173     {
174       data->cancellable = g_cancellable_new ();
175     }
176
177   data->thread = g_thread_new ("server", server_thread, data);
178
179   return data;
180 }
181
182 static IPTestData *
183 create_server (GSocketFamily family,
184                GThreadFunc   server_thread,
185                gboolean      v4mapped)
186 {
187   return create_server_full (family, G_SOCKET_TYPE_STREAM, server_thread, v4mapped);
188 }
189
190 static const gchar *testbuf = "0123456789abcdef";
191
192 static gboolean
193 test_ip_async_read_ready (GSocket      *client,
194                           GIOCondition  cond,
195                           gpointer      user_data)
196 {
197   IPTestData *data = user_data;
198   GError *error = NULL;
199   gssize len;
200   gchar buf[128];
201
202   g_assert_cmpint (cond, ==, G_IO_IN);
203
204   len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
205   g_assert_no_error (error);
206   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
207
208   g_assert_cmpstr (testbuf, ==, buf);
209
210   g_main_loop_quit (data->loop);
211
212   return FALSE;
213 }
214
215 static gboolean
216 test_ip_async_write_ready (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
225   g_assert_cmpint (cond, ==, G_IO_OUT);
226
227   len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
228   g_assert_no_error (error);
229   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
230
231   source = g_socket_create_source (client, G_IO_IN, NULL);
232   g_source_set_callback (source, (GSourceFunc)test_ip_async_read_ready,
233                          data, NULL);
234   g_source_attach (source, NULL);
235   g_source_unref (source);
236
237   return FALSE;
238 }
239
240 static gboolean
241 test_ip_async_timed_out (GSocket      *client,
242                          GIOCondition  cond,
243                          gpointer      user_data)
244 {
245   IPTestData *data = user_data;
246   GError *error = NULL;
247   GSource *source;
248   gssize len;
249   gchar buf[128];
250
251   if (data->family == G_SOCKET_FAMILY_IPV4)
252     {
253       g_assert_cmpint (cond, ==, G_IO_IN);
254       len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
255       g_assert_cmpint (len, ==, -1);
256       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
257       g_clear_error (&error);
258     }
259
260   source = g_socket_create_source (client, G_IO_OUT, NULL);
261   g_source_set_callback (source, (GSourceFunc)test_ip_async_write_ready,
262                          data, NULL);
263   g_source_attach (source, NULL);
264   g_source_unref (source);
265
266   return FALSE;
267 }
268
269 static gboolean
270 test_ip_async_connected (GSocket      *client,
271                          GIOCondition  cond,
272                          gpointer      user_data)
273 {
274   IPTestData *data = user_data;
275   GError *error = NULL;
276   GSource *source;
277   gssize len;
278   gchar buf[128];
279
280   g_socket_check_connect_result (client, &error);
281   g_assert_no_error (error);
282   /* We do this after the check_connect_result, since that will give a
283    * more useful assertion in case of error.
284    */
285   g_assert_cmpint (cond, ==, G_IO_OUT);
286
287   g_assert (g_socket_is_connected (client));
288
289   /* This adds 1 second to "make check", so let's just only do it once. */
290   if (data->family == G_SOCKET_FAMILY_IPV4)
291     {
292       len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
293       g_assert_cmpint (len, ==, -1);
294       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
295       g_clear_error (&error);
296
297       source = g_socket_create_source (client, G_IO_IN, NULL);
298       g_source_set_callback (source, (GSourceFunc)test_ip_async_timed_out,
299                              data, NULL);
300       g_source_attach (source, NULL);
301       g_source_unref (source);
302     }
303   else
304     test_ip_async_timed_out (client, 0, data);
305
306   return FALSE;
307 }
308
309 static gboolean
310 idle_test_ip_async_connected (gpointer user_data)
311 {
312   IPTestData *data = user_data;
313
314   return test_ip_async_connected (data->client, G_IO_OUT, data);
315 }
316
317 static void
318 test_ip_async (GSocketFamily family)
319 {
320   IPTestData *data;
321   GError *error = NULL;
322   GSocket *client;
323   GSocketAddress *addr;
324   GSource *source;
325   gssize len;
326   gchar buf[128];
327
328   data = create_server (family, echo_server_thread, FALSE);
329   addr = g_socket_get_local_address (data->server, &error);
330   g_assert_no_error (error);
331
332   client = g_socket_new (family,
333                          G_SOCKET_TYPE_STREAM,
334                          G_SOCKET_PROTOCOL_DEFAULT,
335                          &error);
336   g_assert_no_error (error);
337   data->client = client;
338
339   g_assert_cmpint (g_socket_get_family (client), ==, family);
340   g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
341   g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
342
343   g_socket_set_blocking (client, FALSE);
344   g_socket_set_timeout (client, 1);
345
346   if (g_socket_connect (client, addr, NULL, &error))
347     {
348       g_assert_no_error (error);
349       g_idle_add (idle_test_ip_async_connected, data);
350     }
351   else
352     {
353       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
354       g_clear_error (&error);
355       source = g_socket_create_source (client, G_IO_OUT, NULL);
356       g_source_set_callback (source, (GSourceFunc)test_ip_async_connected,
357                              data, NULL);
358       g_source_attach (source, NULL);
359       g_source_unref (source);
360     }
361   g_object_unref (addr);
362
363   data->loop = g_main_loop_new (NULL, TRUE);
364   g_main_loop_run (data->loop);
365   g_main_loop_unref (data->loop);
366
367   g_socket_shutdown (client, FALSE, TRUE, &error);
368   g_assert_no_error (error);
369
370   g_thread_join (data->thread);
371
372   if (family == G_SOCKET_FAMILY_IPV4)
373     {
374       /* Test that reading on a remote-closed socket gets back 0 bytes. */
375       len = g_socket_receive_with_blocking (client, buf, sizeof (buf),
376                                             TRUE, NULL, &error);
377       g_assert_no_error (error);
378       g_assert_cmpint (len, ==, 0);
379     }
380   else
381     {
382       /* Test that writing to a remote-closed socket gets back CONNECTION_CLOSED. */
383       len = g_socket_send_with_blocking (client, testbuf, strlen (testbuf) + 1,
384                                          TRUE, NULL, &error);
385       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED);
386       g_assert_cmpint (len, ==, -1);
387       g_clear_error (&error);
388     }
389
390   g_socket_close (client, &error);
391   g_assert_no_error (error);
392   g_socket_close (data->server, &error);
393   g_assert_no_error (error);
394
395   g_object_unref (data->server);
396   g_object_unref (client);
397
398   g_slice_free (IPTestData, data);
399 }
400
401 static void
402 test_ipv4_async (void)
403 {
404   test_ip_async (G_SOCKET_FAMILY_IPV4);
405 }
406
407 static void
408 test_ipv6_async (void)
409 {
410   if (!ipv6_supported)
411     {
412       g_test_skip ("No support for IPv6");
413       return;
414     }
415
416   test_ip_async (G_SOCKET_FAMILY_IPV6);
417 }
418
419 static const gchar testbuf2[] = "0123456789abcdefghijklmnopqrstuvwxyz";
420
421 static void
422 test_ip_sync (GSocketFamily family)
423 {
424   IPTestData *data;
425   GError *error = NULL;
426   GSocket *client;
427   GSocketAddress *addr;
428   gssize len;
429   gchar buf[128];
430
431   data = create_server (family, echo_server_thread, FALSE);
432   addr = g_socket_get_local_address (data->server, &error);
433   g_assert_no_error (error);
434
435   client = g_socket_new (family,
436                          G_SOCKET_TYPE_STREAM,
437                          G_SOCKET_PROTOCOL_DEFAULT,
438                          &error);
439   g_assert_no_error (error);
440
441   g_assert_cmpint (g_socket_get_family (client), ==, family);
442   g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
443   g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
444
445   g_socket_set_blocking (client, TRUE);
446   g_socket_set_timeout (client, 1);
447
448   g_socket_connect (client, addr, NULL, &error);
449   g_assert_no_error (error);
450   g_assert (g_socket_is_connected (client));
451   g_object_unref (addr);
452
453   /* This adds 1 second to "make check", so let's just only do it once. */
454   if (family == G_SOCKET_FAMILY_IPV4)
455     {
456       len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
457       g_assert_cmpint (len, ==, -1);
458       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
459       g_clear_error (&error);
460     }
461
462   len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
463   g_assert_no_error (error);
464   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
465   
466   len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
467   g_assert_no_error (error);
468   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
469
470   g_assert_cmpstr (testbuf, ==, buf);
471
472   {
473     GOutputVector v[7] = { { NULL, }, };
474
475     v[0].buffer = testbuf2 + 0;
476     v[0].size = 3;
477     v[1].buffer = testbuf2 + 3;
478     v[1].size = 5;
479     v[2].buffer = testbuf2 + 3 + 5;
480     v[2].size = 0;
481     v[3].buffer = testbuf2 + 3 + 5;
482     v[3].size = 6;
483     v[4].buffer = testbuf2 + 3 + 5 + 6;
484     v[4].size = 2;
485     v[5].buffer = testbuf2 + 3 + 5 + 6 + 2;
486     v[5].size = 1;
487     v[6].buffer = testbuf2 + 3 + 5 + 6 + 2 + 1;
488     v[6].size = strlen (testbuf2) - (3 + 5 + 6 + 2 + 1);
489
490     len = g_socket_send_message (client, NULL, v, G_N_ELEMENTS (v), NULL, 0, 0, NULL, &error);
491     g_assert_no_error (error);
492     g_assert_cmpint (len, ==, strlen (testbuf2));
493
494     len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
495     g_assert_no_error (error);
496     g_assert_cmpint (len, ==, strlen (testbuf2));
497     g_assert_cmpstr (testbuf2, ==, buf);
498   }
499
500   g_socket_shutdown (client, FALSE, TRUE, &error);
501   g_assert_no_error (error);
502
503   g_thread_join (data->thread);
504
505   if (family == G_SOCKET_FAMILY_IPV4)
506     {
507       /* Test that reading on a remote-closed socket gets back 0 bytes. */
508       len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
509       g_assert_no_error (error);
510       g_assert_cmpint (len, ==, 0);
511     }
512   else
513     {
514       /* Test that writing to a remote-closed socket gets back CONNECTION_CLOSED. */
515       len = g_socket_send (client, testbuf, strlen (testbuf) + 1, NULL, &error);
516       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CONNECTION_CLOSED);
517       g_assert_cmpint (len, ==, -1);
518       g_clear_error (&error);
519     }
520
521   g_socket_close (client, &error);
522   g_assert_no_error (error);
523   g_socket_close (data->server, &error);
524   g_assert_no_error (error);
525
526   g_object_unref (data->server);
527   g_object_unref (client);
528
529   g_slice_free (IPTestData, data);
530 }
531
532 static void
533 test_ipv4_sync (void)
534 {
535   test_ip_sync (G_SOCKET_FAMILY_IPV4);
536 }
537
538 static void
539 test_ipv6_sync (void)
540 {
541   if (!ipv6_supported)
542     {
543       g_test_skip ("No support for IPv6");
544       return;
545     }
546
547   test_ip_sync (G_SOCKET_FAMILY_IPV6);
548 }
549
550 static void
551 test_ip_sync_dgram (GSocketFamily family)
552 {
553   IPTestData *data;
554   GError *error = NULL;
555   GSocket *client;
556   GSocketAddress *dest_addr;
557   gssize len;
558   gchar buf[128];
559
560   data = create_server_full (family, G_SOCKET_TYPE_DATAGRAM,
561                              echo_server_dgram_thread, FALSE);
562
563   dest_addr = g_socket_get_local_address (data->server, &error);
564
565   client = g_socket_new (family,
566                          G_SOCKET_TYPE_DATAGRAM,
567                          G_SOCKET_PROTOCOL_DEFAULT,
568                          &error);
569   g_assert_no_error (error);
570
571   g_assert_cmpint (g_socket_get_family (client), ==, family);
572   g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_DATAGRAM);
573   g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
574
575   g_socket_set_blocking (client, TRUE);
576   g_socket_set_timeout (client, 1);
577
578   len = g_socket_send_to (client, dest_addr, testbuf, strlen (testbuf) + 1, NULL, &error);
579   g_assert_no_error (error);
580   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
581
582   len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
583   g_assert_no_error (error);
584   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
585
586   g_assert_cmpstr (testbuf, ==, buf);
587
588   {
589     GOutputVector v[7] = { { NULL, }, };
590
591     v[0].buffer = testbuf2 + 0;
592     v[0].size = 3;
593     v[1].buffer = testbuf2 + 3;
594     v[1].size = 5;
595     v[2].buffer = testbuf2 + 3 + 5;
596     v[2].size = 0;
597     v[3].buffer = testbuf2 + 3 + 5;
598     v[3].size = 6;
599     v[4].buffer = testbuf2 + 3 + 5 + 6;
600     v[4].size = 2;
601     v[5].buffer = testbuf2 + 3 + 5 + 6 + 2;
602     v[5].size = 1;
603     v[6].buffer = testbuf2 + 3 + 5 + 6 + 2 + 1;
604     v[6].size = strlen (testbuf2) - (3 + 5 + 6 + 2 + 1);
605
606     len = g_socket_send_message (client, dest_addr, v, G_N_ELEMENTS (v), NULL, 0, 0, NULL, &error);
607     g_assert_no_error (error);
608     g_assert_cmpint (len, ==, strlen (testbuf2));
609
610     memset (buf, 0, sizeof (buf));
611     len = g_socket_receive_from (client, NULL, buf, sizeof (buf), NULL, &error);
612     g_assert_no_error (error);
613     g_assert_cmpint (len, ==, strlen (testbuf2));
614     g_assert_cmpstr (testbuf2, ==, buf);
615   }
616
617   g_cancellable_cancel (data->cancellable);
618
619   g_thread_join (data->thread);
620
621   g_socket_close (client, &error);
622   g_assert_no_error (error);
623   g_socket_close (data->server, &error);
624   g_assert_no_error (error);
625
626   g_object_unref (data->server);
627   g_object_unref (data->cancellable);
628   g_object_unref (client);
629   g_object_unref (dest_addr);
630
631   g_slice_free (IPTestData, data);
632 }
633
634 static void
635 test_ipv4_sync_dgram (void)
636 {
637   test_ip_sync_dgram (G_SOCKET_FAMILY_IPV4);
638 }
639
640 static void
641 test_ipv6_sync_dgram (void)
642 {
643   if (!ipv6_supported)
644     {
645       g_test_skip ("No support for IPv6");
646       return;
647     }
648
649   test_ip_sync_dgram (G_SOCKET_FAMILY_IPV6);
650 }
651
652 static gpointer
653 graceful_server_thread (gpointer user_data)
654 {
655   IPTestData *data = user_data;
656   GSocket *sock;
657   GError *error = NULL;
658   gssize len;
659
660   sock = g_socket_accept (data->server, NULL, &error);
661   g_assert_no_error (error);
662
663   len = g_socket_send (sock, testbuf, strlen (testbuf) + 1, NULL, &error);
664   g_assert_no_error (error);
665   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
666
667   return sock;
668 }
669
670 static void
671 test_close_graceful (void)
672 {
673   GSocketFamily family = G_SOCKET_FAMILY_IPV4;
674   IPTestData *data;
675   GError *error = NULL;
676   GSocket *client, *server;
677   GSocketAddress *addr;
678   gssize len;
679   gchar buf[128];
680
681   data = create_server (family, graceful_server_thread, FALSE);
682   addr = g_socket_get_local_address (data->server, &error);
683   g_assert_no_error (error);
684
685   client = g_socket_new (family,
686                          G_SOCKET_TYPE_STREAM,
687                          G_SOCKET_PROTOCOL_DEFAULT,
688                          &error);
689   g_assert_no_error (error);
690
691   g_assert_cmpint (g_socket_get_family (client), ==, family);
692   g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_STREAM);
693   g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT);
694
695   g_socket_set_blocking (client, TRUE);
696   g_socket_set_timeout (client, 1);
697
698   g_socket_connect (client, addr, NULL, &error);
699   g_assert_no_error (error);
700   g_assert (g_socket_is_connected (client));
701   g_object_unref (addr);
702
703   server = g_thread_join (data->thread);
704
705   /* similar to g_tcp_connection_set_graceful_disconnect(), but explicit */
706   g_socket_shutdown (server, FALSE, TRUE, &error);
707   g_assert_no_error (error);
708
709   /* we must timeout */
710   g_socket_condition_wait (client, G_IO_HUP, NULL, &error);
711   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
712   g_clear_error (&error);
713
714   /* check that the remaining data is received */
715   len = g_socket_receive (client, buf, strlen (testbuf) + 1, NULL, &error);
716   g_assert_no_error (error);
717   g_assert_cmpint (len, ==, strlen (testbuf) + 1);
718
719   /* and only then the connection is closed */
720   len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
721   g_assert_no_error (error);
722   g_assert_cmpint (len, ==, 0);
723
724   g_socket_close (server, &error);
725   g_assert_no_error (error);
726
727   g_socket_close (client, &error);
728   g_assert_no_error (error);
729
730   g_object_unref (server);
731   g_object_unref (data->server);
732   g_object_unref (client);
733
734   g_slice_free (IPTestData, data);
735 }
736
737 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
738 static gpointer
739 v4mapped_server_thread (gpointer user_data)
740 {
741   IPTestData *data = user_data;
742   GSocket *sock;
743   GError *error = NULL;
744   GSocketAddress *addr;
745
746   sock = g_socket_accept (data->server, NULL, &error);
747   g_assert_no_error (error);
748
749   g_assert_cmpint (g_socket_get_family (sock), ==, G_SOCKET_FAMILY_IPV6);
750
751   addr = g_socket_get_local_address (sock, &error);
752   g_assert_no_error (error);
753   g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
754   g_object_unref (addr);
755
756   addr = g_socket_get_remote_address (sock, &error);
757   g_assert_no_error (error);
758   g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
759   g_object_unref (addr);
760
761   g_socket_close (sock, &error);
762   g_assert_no_error (error);
763   g_object_unref (sock);
764   return NULL;
765 }
766
767 static void
768 test_ipv6_v4mapped (void)
769 {
770   IPTestData *data;
771   GError *error = NULL;
772   GSocket *client;
773   GSocketAddress *addr, *v4addr;
774   GInetAddress *iaddr;
775
776   if (!ipv6_supported)
777     {
778       g_test_skip ("No support for IPv6");
779       return;
780     }
781
782   data = create_server (G_SOCKET_FAMILY_IPV6, v4mapped_server_thread, TRUE);
783
784   if (data == NULL)
785     {
786       g_test_message ("Test not run: not supported by the OS");
787       return;
788     }
789
790   client = g_socket_new (G_SOCKET_FAMILY_IPV4,
791                          G_SOCKET_TYPE_STREAM,
792                          G_SOCKET_PROTOCOL_DEFAULT,
793                          &error);
794   g_assert_no_error (error);
795
796   g_socket_set_blocking (client, TRUE);
797   g_socket_set_timeout (client, 1);
798
799   addr = g_socket_get_local_address (data->server, &error);
800   g_assert_no_error (error);
801   iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
802   v4addr = g_inet_socket_address_new (iaddr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
803   g_object_unref (iaddr);
804   g_object_unref (addr);
805
806   g_socket_connect (client, v4addr, NULL, &error);
807   g_assert_no_error (error);
808   g_assert (g_socket_is_connected (client));
809
810   g_thread_join (data->thread);
811
812   g_socket_close (client, &error);
813   g_assert_no_error (error);
814   g_socket_close (data->server, &error);
815   g_assert_no_error (error);
816
817   g_object_unref (data->server);
818   g_object_unref (client);
819   g_object_unref (v4addr);
820
821   g_slice_free (IPTestData, data);
822 }
823 #endif
824
825 static void
826 test_timed_wait (void)
827 {
828   IPTestData *data;
829   GError *error = NULL;
830   GSocket *client;
831   GSocketAddress *addr;
832   gint64 start_time;
833   gint poll_duration;
834
835   data = create_server (G_SOCKET_FAMILY_IPV4, echo_server_thread, FALSE);
836   addr = g_socket_get_local_address (data->server, &error);
837   g_assert_no_error (error);
838
839   client = g_socket_new (G_SOCKET_FAMILY_IPV4,
840                          G_SOCKET_TYPE_STREAM,
841                          G_SOCKET_PROTOCOL_DEFAULT,
842                          &error);
843   g_assert_no_error (error);
844
845   g_socket_set_blocking (client, TRUE);
846   g_socket_set_timeout (client, 1);
847
848   g_socket_connect (client, addr, NULL, &error);
849   g_assert_no_error (error);
850   g_object_unref (addr);
851
852   start_time = g_get_monotonic_time ();
853   g_socket_condition_timed_wait (client, G_IO_IN, 100000 /* 100 ms */,
854                                  NULL, &error);
855   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
856   g_clear_error (&error);
857   poll_duration = g_get_monotonic_time () - start_time;
858
859   g_assert_cmpint (poll_duration, >=, 98000);
860   g_assert_cmpint (poll_duration, <, 112000);
861
862   g_socket_close (client, &error);
863   g_assert_no_error (error);
864
865   g_thread_join (data->thread);
866
867   g_socket_close (data->server, &error);
868   g_assert_no_error (error);
869
870   g_object_unref (data->server);
871   g_object_unref (client);
872
873   g_slice_free (IPTestData, data);
874 }
875
876 static void
877 test_sockaddr (void)
878 {
879   struct sockaddr_in6 sin6, gsin6;
880   GSocketAddress *saddr;
881   GInetSocketAddress *isaddr;
882   GInetAddress *iaddr;
883   GError *error = NULL;
884
885   memset (&sin6, 0, sizeof (sin6));
886   sin6.sin6_family = AF_INET6;
887   sin6.sin6_addr = in6addr_loopback;
888   sin6.sin6_port = g_htons (42);
889   sin6.sin6_scope_id = 17;
890   sin6.sin6_flowinfo = 1729;
891
892   saddr = g_socket_address_new_from_native (&sin6, sizeof (sin6));
893   g_assert (G_IS_INET_SOCKET_ADDRESS (saddr));
894
895   isaddr = G_INET_SOCKET_ADDRESS (saddr);
896   iaddr = g_inet_socket_address_get_address (isaddr);
897   g_assert_cmpint (g_inet_address_get_family (iaddr), ==, G_SOCKET_FAMILY_IPV6);
898   g_assert (g_inet_address_get_is_loopback (iaddr));
899
900   g_assert_cmpint (g_inet_socket_address_get_port (isaddr), ==, 42);
901   g_assert_cmpint (g_inet_socket_address_get_scope_id (isaddr), ==, 17);
902   g_assert_cmpint (g_inet_socket_address_get_flowinfo (isaddr), ==, 1729);
903
904   g_socket_address_to_native (saddr, &gsin6, sizeof (gsin6), &error);
905   g_assert_no_error (error);
906
907   g_assert (memcmp (&sin6.sin6_addr, &gsin6.sin6_addr, sizeof (struct in6_addr)) == 0);
908   g_assert_cmpint (sin6.sin6_port, ==, gsin6.sin6_port);
909   g_assert_cmpint (sin6.sin6_scope_id, ==, gsin6.sin6_scope_id);
910   g_assert_cmpint (sin6.sin6_flowinfo, ==, gsin6.sin6_flowinfo);
911
912   g_object_unref (saddr);
913 }
914
915 #ifdef G_OS_UNIX
916 static void
917 test_unix_from_fd (void)
918 {
919   gint fd;
920   GError *error;
921   GSocket *s;
922
923   fd = socket (AF_UNIX, SOCK_STREAM, 0);
924   g_assert_cmpint (fd, !=, -1);
925
926   error = NULL;
927   s = g_socket_new_from_fd (fd, &error);
928   g_assert_no_error (error);
929   g_assert_cmpint (g_socket_get_family (s), ==, G_SOCKET_FAMILY_UNIX);
930   g_assert_cmpint (g_socket_get_socket_type (s), ==, G_SOCKET_TYPE_STREAM);
931   g_assert_cmpint (g_socket_get_protocol (s), ==, G_SOCKET_PROTOCOL_DEFAULT);
932   g_object_unref (s);
933 }
934
935 static void
936 test_unix_connection (void)
937 {
938   gint fd;
939   GError *error;
940   GSocket *s;
941   GSocketConnection *c;
942
943   fd = socket (AF_UNIX, SOCK_STREAM, 0);
944   g_assert_cmpint (fd, !=, -1);
945
946   error = NULL;
947   s = g_socket_new_from_fd (fd, &error);
948   g_assert_no_error (error);
949   c = g_socket_connection_factory_create_connection (s);
950   g_assert (G_IS_UNIX_CONNECTION (c));
951   g_object_unref (c);
952   g_object_unref (s);
953 }
954
955 static GSocketConnection *
956 create_connection_for_fd (int fd)
957 {
958   GError *err = NULL;
959   GSocket *socket;
960   GSocketConnection *connection;
961
962   socket = g_socket_new_from_fd (fd, &err);
963   g_assert_no_error (err);
964   g_assert (G_IS_SOCKET (socket));
965   connection = g_socket_connection_factory_create_connection (socket);
966   g_assert (G_IS_UNIX_CONNECTION (connection));
967   g_object_unref (socket);
968   return connection;
969 }
970
971 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
972
973 static void
974 test_unix_connection_ancillary_data (void)
975 {
976   GError *err = NULL;
977   gint pv[2], sv[3];
978   gint status, fd, len;
979   char buffer[1024];
980   pid_t pid;
981
982   status = pipe (pv);
983   g_assert_cmpint (status, ==, 0);
984
985   status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
986   g_assert_cmpint (status, ==, 0);
987
988   pid = fork ();
989   g_assert_cmpint (pid, >=, 0);
990
991   /* Child: close its copy of the write end of the pipe, receive it
992    * again from the parent over the socket, and write some text to it.
993    *
994    * Parent: send the write end of the pipe (still open for the
995    * parent) over the socket, close it, and read some text from the
996    * read end of the pipe.
997    */
998   if (pid == 0)
999     {
1000       GSocketConnection *connection;
1001
1002       close (sv[1]);
1003       connection = create_connection_for_fd (sv[0]);
1004
1005       status = close (pv[1]);
1006       g_assert_cmpint (status, ==, 0);
1007
1008       err = NULL;
1009       fd = g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection), NULL,
1010                                          &err);
1011       g_assert_no_error (err);
1012       g_assert_cmpint (fd, >, -1);
1013       g_object_unref (connection);
1014
1015       do
1016         len = write (fd, TEST_DATA, sizeof (TEST_DATA));
1017       while (len == -1 && errno == EINTR);
1018       g_assert_cmpint (len, ==, sizeof (TEST_DATA));
1019       exit (0);
1020     }
1021   else
1022     {
1023       GSocketConnection *connection;
1024
1025       close (sv[0]);
1026       connection = create_connection_for_fd (sv[1]);
1027
1028       err = NULL;
1029       g_unix_connection_send_fd (G_UNIX_CONNECTION (connection), pv[1], NULL,
1030                                  &err);
1031       g_assert_no_error (err);
1032       g_object_unref (connection);
1033
1034       status = close (pv[1]);
1035       g_assert_cmpint (status, ==, 0);
1036
1037       memset (buffer, 0xff, sizeof buffer);
1038       do
1039         len = read (pv[0], buffer, sizeof buffer);
1040       while (len == -1 && errno == EINTR);
1041
1042       g_assert_cmpint (len, ==, sizeof (TEST_DATA));
1043       g_assert_cmpstr (buffer, ==, TEST_DATA);
1044
1045       waitpid (pid, &status, 0);
1046       g_assert (WIFEXITED (status));
1047       g_assert_cmpint (WEXITSTATUS (status), ==, 0);
1048     }
1049
1050   /* TODO: add test for g_unix_connection_send_credentials() and
1051    * g_unix_connection_receive_credentials().
1052    */
1053 }
1054 #endif /* G_OS_UNIX */
1055
1056 static void
1057 test_reuse_tcp (void)
1058 {
1059   GSocket *sock1, *sock2;
1060   GError *error = NULL;
1061   GInetAddress *iaddr;
1062   GSocketAddress *addr;
1063
1064   sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1065                         G_SOCKET_TYPE_STREAM,
1066                         G_SOCKET_PROTOCOL_DEFAULT,
1067                         &error);
1068   g_assert_no_error (error);
1069
1070   iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1071   addr = g_inet_socket_address_new (iaddr, 0);
1072   g_object_unref (iaddr);
1073   g_socket_bind (sock1, addr, TRUE, &error);
1074   g_object_unref (addr);
1075   g_assert_no_error (error);
1076
1077   g_socket_listen (sock1, &error);
1078   g_assert_no_error (error);
1079
1080   sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1081                         G_SOCKET_TYPE_STREAM,
1082                         G_SOCKET_PROTOCOL_DEFAULT,
1083                         &error);
1084   g_assert_no_error (error);
1085
1086   addr = g_socket_get_local_address (sock1, &error);
1087   g_assert_no_error (error);
1088   g_socket_bind (sock2, addr, TRUE, &error);
1089   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_ADDRESS_IN_USE);
1090   g_clear_error (&error);
1091   g_object_unref (addr);
1092
1093   g_object_unref (sock1);
1094   g_object_unref (sock2);
1095 }
1096
1097 static void
1098 test_reuse_udp (void)
1099 {
1100   GSocket *sock1, *sock2;
1101   GError *error = NULL;
1102   GInetAddress *iaddr;
1103   GSocketAddress *addr;
1104
1105   sock1 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1106                         G_SOCKET_TYPE_DATAGRAM,
1107                         G_SOCKET_PROTOCOL_DEFAULT,
1108                         &error);
1109   g_assert_no_error (error);
1110
1111   iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
1112   addr = g_inet_socket_address_new (iaddr, 0);
1113   g_object_unref (iaddr);
1114   g_socket_bind (sock1, addr, TRUE, &error);
1115   g_object_unref (addr);
1116   g_assert_no_error (error);
1117
1118   sock2 = g_socket_new (G_SOCKET_FAMILY_IPV4,
1119                         G_SOCKET_TYPE_DATAGRAM,
1120                         G_SOCKET_PROTOCOL_DEFAULT,
1121                         &error);
1122   g_assert_no_error (error);
1123
1124   addr = g_socket_get_local_address (sock1, &error);
1125   g_assert_no_error (error);
1126   g_socket_bind (sock2, addr, TRUE, &error);
1127   g_object_unref (addr);
1128   g_assert_no_error (error);
1129
1130   g_object_unref (sock1);
1131   g_object_unref (sock2);
1132 }
1133
1134 static void
1135 test_get_available (gconstpointer user_data)
1136 {
1137   GSocketType socket_type = GPOINTER_TO_UINT (user_data);
1138   GError *err = NULL;
1139   GSocket *listener, *server, *client;
1140   GInetAddress *addr;
1141   GSocketAddress *saddr;
1142   gchar data[] = "0123456789abcdef";
1143   gchar buf[34];
1144   gssize nread;
1145
1146   listener = g_socket_new (G_SOCKET_FAMILY_IPV4,
1147                            socket_type,
1148                            G_SOCKET_PROTOCOL_DEFAULT,
1149                            &err);
1150   g_assert_no_error (err);
1151   g_assert (G_IS_SOCKET (listener));
1152
1153   client = g_socket_new (G_SOCKET_FAMILY_IPV4,
1154                          socket_type,
1155                          G_SOCKET_PROTOCOL_DEFAULT,
1156                          &err);
1157   g_assert_no_error (err);
1158   g_assert (G_IS_SOCKET (client));
1159
1160   if (socket_type == G_SOCKET_TYPE_STREAM)
1161     {
1162       g_socket_set_option (client, IPPROTO_TCP, TCP_NODELAY, TRUE, &err);
1163       g_assert_no_error (err);
1164     }
1165
1166   addr = g_inet_address_new_any (G_SOCKET_FAMILY_IPV4);
1167   saddr = g_inet_socket_address_new (addr, 0);
1168
1169   g_socket_bind (listener, saddr, TRUE, &err);
1170   g_assert_no_error (err);
1171   g_object_unref (saddr);
1172   g_object_unref (addr);
1173
1174   saddr = g_socket_get_local_address (listener, &err);
1175   g_assert_no_error (err);
1176
1177   if (socket_type == G_SOCKET_TYPE_STREAM)
1178     {
1179       g_socket_listen (listener, &err);
1180       g_assert_no_error (err);
1181       g_socket_connect (client, saddr, NULL, &err);
1182       g_assert_no_error (err);
1183
1184       server = g_socket_accept (listener, NULL, &err);
1185       g_assert_no_error (err);
1186       g_socket_set_blocking (server, FALSE);
1187       g_object_unref (listener);
1188     }
1189   else
1190     server = listener;
1191
1192   g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
1193   g_assert_no_error (err);
1194
1195   while (!g_socket_condition_wait (server, G_IO_IN, NULL, NULL))
1196     ;
1197   g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1198
1199   g_socket_send_to (client, saddr, data, sizeof (data), NULL, &err);
1200   g_assert_no_error (err);
1201
1202   /* We need to wait until the data has actually been copied into the
1203    * server socket's buffers, but g_socket_condition_wait() won't help
1204    * here since the socket is definitely already readable. So there's
1205    * a race condition in checking its available bytes. In the TCP
1206    * case, we poll for a bit until the new data shows up. In the UDP
1207    * case, there's not much we can do, but at least the failure mode
1208    * is passes-when-it-shouldn't, not fails-when-it-shouldn't.
1209    */
1210   if (socket_type == G_SOCKET_TYPE_STREAM)
1211     {
1212       int tries;
1213
1214       for (tries = 0; tries < 100; tries++)
1215         {
1216           if (g_socket_get_available_bytes (server) > sizeof (data))
1217             break;
1218           g_usleep (100000);
1219         }
1220
1221       g_assert_cmpint (g_socket_get_available_bytes (server), ==, 2 * sizeof (data));
1222     }
1223   else
1224     {
1225       g_usleep (100000);
1226       g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1227     }
1228
1229   g_assert_cmpint (sizeof (buf), >=, 2 * sizeof (data));
1230   nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
1231   g_assert_no_error (err);
1232
1233   if (socket_type == G_SOCKET_TYPE_STREAM)
1234     {
1235       g_assert_cmpint (nread, ==, 2 * sizeof (data));
1236       g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1237     }
1238   else
1239     {
1240       g_assert_cmpint (nread, ==, sizeof (data));
1241       g_assert_cmpint (g_socket_get_available_bytes (server), ==, sizeof (data));
1242     }
1243
1244   nread = g_socket_receive (server, buf, sizeof (buf), NULL, &err);
1245   if (socket_type == G_SOCKET_TYPE_STREAM)
1246     {
1247       g_assert_cmpint (nread, ==, -1);
1248       g_assert_error (err, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
1249       g_clear_error (&err);
1250     }
1251   else
1252     {
1253       g_assert_cmpint (nread, ==, sizeof (data));
1254       g_assert_no_error (err);
1255     }
1256
1257   g_assert_cmpint (g_socket_get_available_bytes (server), ==, 0);
1258
1259   g_socket_close (server, &err);
1260   g_assert_no_error (err);
1261
1262   g_object_unref (saddr);
1263   g_object_unref (server);
1264   g_object_unref (client);
1265 }
1266
1267 int
1268 main (int   argc,
1269       char *argv[])
1270 {
1271   GSocket *sock;
1272   GError *error = NULL;
1273
1274   g_test_init (&argc, &argv, NULL);
1275
1276   sock = g_socket_new (G_SOCKET_FAMILY_IPV6,
1277                        G_SOCKET_TYPE_STREAM,
1278                        G_SOCKET_PROTOCOL_DEFAULT,
1279                        &error);
1280   if (sock != NULL)
1281     {
1282       ipv6_supported = TRUE;
1283       g_object_unref (sock);
1284     }
1285   else
1286     {
1287       g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
1288       g_clear_error (&error);
1289     }
1290
1291   g_test_add_func ("/socket/ipv4_sync", test_ipv4_sync);
1292   g_test_add_func ("/socket/ipv4_async", test_ipv4_async);
1293   g_test_add_func ("/socket/ipv6_sync", test_ipv6_sync);
1294   g_test_add_func ("/socket/ipv6_async", test_ipv6_async);
1295   g_test_add_func ("/socket/ipv4_sync/datagram", test_ipv4_sync_dgram);
1296   g_test_add_func ("/socket/ipv6_sync/datagram", test_ipv6_sync_dgram);
1297 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
1298   g_test_add_func ("/socket/ipv6_v4mapped", test_ipv6_v4mapped);
1299 #endif
1300   g_test_add_func ("/socket/close_graceful", test_close_graceful);
1301   g_test_add_func ("/socket/timed_wait", test_timed_wait);
1302   g_test_add_func ("/socket/address", test_sockaddr);
1303 #ifdef G_OS_UNIX
1304   g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
1305   g_test_add_func ("/socket/unix-connection", test_unix_connection);
1306   g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data);
1307 #endif
1308   g_test_add_func ("/socket/reuse/tcp", test_reuse_tcp);
1309   g_test_add_func ("/socket/reuse/udp", test_reuse_udp);
1310   g_test_add_data_func ("/socket/get_available/datagram", GUINT_TO_POINTER (G_SOCKET_TYPE_DATAGRAM),
1311                         test_get_available);
1312   g_test_add_data_func ("/socket/get_available/stream", GUINT_TO_POINTER (G_SOCKET_TYPE_STREAM),
1313                         test_get_available);
1314
1315   return g_test_run();
1316 }
1317