g_thread_new: never fail
[platform/upstream/glib.git] / gio / tests / socket.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2008-2011 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <gio/gio.h>
22
23 #ifdef G_OS_UNIX
24 #include <errno.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <sys/wait.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <gio/gunixconnection.h>
31 #endif
32
33 #include "gnetworkingprivate.h"
34
35 typedef struct {
36   GSocket *server;
37   GSocket *client;
38   GSocketFamily family;
39   GThread *thread;
40   GMainLoop *loop;
41 } IPTestData;
42
43 static gpointer
44 echo_server_thread (gpointer user_data)
45 {
46   IPTestData *data = user_data;
47   GSocket *sock;
48   GError *error = NULL;
49   gssize nread, nwrote;
50   gchar buf[128];
51
52   sock = g_socket_accept (data->server, NULL, &error);
53   g_assert_no_error (error);
54
55   while (TRUE)
56     {
57       nread = g_socket_receive (sock, buf, sizeof (buf), NULL, &error);
58       g_assert_no_error (error);
59       g_assert_cmpint (nread, >=, 0);
60
61       if (nread == 0)
62         break;
63
64       nwrote = g_socket_send (sock, buf, nread, NULL, &error);
65       g_assert_no_error (error);
66       g_assert_cmpint (nwrote, ==, nread);
67     }
68
69   g_socket_close (sock, &error);
70   g_assert_no_error (error);
71   return NULL;
72 }
73
74 static IPTestData *
75 create_server (GSocketFamily family,
76                GThreadFunc   server_thread,
77                gboolean      v4mapped)
78 {
79   IPTestData *data;
80   GSocket *server;
81   GError *error = NULL;
82   GSocketAddress *addr;
83   GInetAddress *iaddr;
84
85   data = g_slice_new (IPTestData);
86   data->family = family;
87
88   data->server = server = g_socket_new (family,
89                                         G_SOCKET_TYPE_STREAM,
90                                         G_SOCKET_PROTOCOL_DEFAULT,
91                                         &error);
92   g_assert_no_error (error);
93
94   g_assert_cmpint (g_socket_get_family (server), ==, family);
95   g_assert_cmpint (g_socket_get_socket_type (server), ==, G_SOCKET_TYPE_STREAM);
96   g_assert_cmpint (g_socket_get_protocol (server), ==, G_SOCKET_PROTOCOL_DEFAULT);
97
98   g_socket_set_blocking (server, TRUE);
99
100 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
101   if (v4mapped)
102     {
103       int fd, v6_only;
104
105       fd = g_socket_get_fd (server);
106       v6_only = 0;
107       setsockopt (fd, IPPROTO_IPV6, IPV6_V6ONLY, &v6_only, sizeof (v6_only));
108     }
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 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
427 static gpointer
428 v4mapped_server_thread (gpointer user_data)
429 {
430   IPTestData *data = user_data;
431   GSocket *sock;
432   GError *error = NULL;
433   GSocketAddress *addr;
434
435   sock = g_socket_accept (data->server, NULL, &error);
436   g_assert_no_error (error);
437
438   g_assert_cmpint (g_socket_get_family (sock), ==, G_SOCKET_FAMILY_IPV6);
439
440   addr = g_socket_get_local_address (sock, &error);
441   g_assert_no_error (error);
442   g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
443   g_object_unref (addr);
444
445   addr = g_socket_get_remote_address (sock, &error);
446   g_assert_no_error (error);
447   g_assert_cmpint (g_socket_address_get_family (addr), ==, G_SOCKET_FAMILY_IPV4);
448   g_object_unref (addr);
449
450   g_socket_close (sock, &error);
451   g_assert_no_error (error);
452   return NULL;
453 }
454
455 static void
456 test_ipv6_v4mapped (void)
457 {
458   IPTestData *data;
459   GError *error = NULL;
460   GSocket *client;
461   GSocketAddress *addr, *v4addr;
462   GInetAddress *iaddr;
463
464   data = create_server (G_SOCKET_FAMILY_IPV6, v4mapped_server_thread, TRUE);
465
466   client = g_socket_new (G_SOCKET_FAMILY_IPV4,
467                          G_SOCKET_TYPE_STREAM,
468                          G_SOCKET_PROTOCOL_DEFAULT,
469                          &error);
470   g_assert_no_error (error);
471
472   g_socket_set_blocking (client, TRUE);
473   g_socket_set_timeout (client, 1);
474
475   addr = g_socket_get_local_address (data->server, &error);
476   iaddr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4);
477   v4addr = g_inet_socket_address_new (iaddr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (addr)));
478   g_object_unref (iaddr);
479   g_object_unref (addr);
480
481   g_socket_connect (client, v4addr, NULL, &error);
482   g_assert_no_error (error);
483   g_assert (g_socket_is_connected (client));
484
485   g_thread_join (data->thread);
486
487   g_socket_close (client, &error);
488   g_assert_no_error (error);
489   g_socket_close (data->server, &error);
490   g_assert_no_error (error);
491
492   g_object_unref (data->server);
493   g_object_unref (client);
494
495   g_slice_free (IPTestData, data);
496 }
497 #endif
498
499 #ifdef G_OS_UNIX
500 static void
501 test_unix_from_fd (void)
502 {
503   gint fd;
504   GError *error;
505   GSocket *s;
506
507   fd = socket (AF_UNIX, SOCK_STREAM, 0);
508   g_assert_cmpint (fd, !=, -1);
509
510   error = NULL;
511   s = g_socket_new_from_fd (fd, &error);
512   g_assert_no_error (error);
513   g_assert_cmpint (g_socket_get_family (s), ==, G_SOCKET_FAMILY_UNIX);
514   g_assert_cmpint (g_socket_get_socket_type (s), ==, G_SOCKET_TYPE_STREAM);
515   g_assert_cmpint (g_socket_get_protocol (s), ==, G_SOCKET_PROTOCOL_DEFAULT);
516   g_object_unref (s);
517 }
518
519 static void
520 test_unix_connection (void)
521 {
522   gint fd;
523   GError *error;
524   GSocket *s;
525   GSocketConnection *c;
526
527   fd = socket (AF_UNIX, SOCK_STREAM, 0);
528   g_assert_cmpint (fd, !=, -1);
529
530   error = NULL;
531   s = g_socket_new_from_fd (fd, &error);
532   g_assert_no_error (error);
533   c = g_socket_connection_factory_create_connection (s);
534   g_assert (G_IS_UNIX_CONNECTION (c));
535   g_object_unref (c);
536   g_object_unref (s);
537 }
538
539 static GSocketConnection *
540 create_connection_for_fd (int fd)
541 {
542   GError *err = NULL;
543   GSocket *socket;
544   GSocketConnection *connection;
545
546   socket = g_socket_new_from_fd (fd, &err);
547   g_assert_no_error (err);
548   g_assert (G_IS_SOCKET (socket));
549   connection = g_socket_connection_factory_create_connection (socket);
550   g_assert (G_IS_UNIX_CONNECTION (connection));
551   g_object_unref (socket);
552   return connection;
553 }
554
555 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
556
557 static void
558 test_unix_connection_ancillary_data (void)
559 {
560   GError *err = NULL;
561   gint pv[2], sv[3];
562   gint status, fd, len;
563   char buffer[1024];
564   pid_t pid;
565
566   status = pipe (pv);
567   g_assert_cmpint (status, ==, 0);
568
569   status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
570   g_assert_cmpint (status, ==, 0);
571
572   pid = fork ();
573   g_assert_cmpint (pid, >=, 0);
574
575   /* Child: close its copy of the write end of the pipe, receive it
576    * again from the parent over the socket, and write some text to it.
577    *
578    * Parent: send the write end of the pipe (still open for the
579    * parent) over the socket, close it, and read some text from the
580    * read end of the pipe.
581    */
582   if (pid == 0)
583     {
584       GSocketConnection *connection;
585
586       close (sv[1]);
587       connection = create_connection_for_fd (sv[0]);
588
589       status = close (pv[1]);
590       g_assert_cmpint (status, ==, 0);
591
592       err = NULL;
593       fd = g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection), NULL,
594                                          &err);
595       g_assert_no_error (err);
596       g_assert_cmpint (fd, >, -1);
597       g_object_unref (connection);
598
599       do
600         len = write (fd, TEST_DATA, sizeof (TEST_DATA));
601       while (len == -1 && errno == EINTR);
602       g_assert_cmpint (len, ==, sizeof (TEST_DATA));
603       exit (0);
604     }
605   else
606     {
607       GSocketConnection *connection;
608
609       close (sv[0]);
610       connection = create_connection_for_fd (sv[1]);
611
612       err = NULL;
613       g_unix_connection_send_fd (G_UNIX_CONNECTION (connection), pv[1], NULL,
614                                  &err);
615       g_assert_no_error (err);
616       g_object_unref (connection);
617
618       status = close (pv[1]);
619       g_assert_cmpint (status, ==, 0);
620
621       memset (buffer, 0xff, sizeof buffer);
622       do
623         len = read (pv[0], buffer, sizeof buffer);
624       while (len == -1 && errno == EINTR);
625
626       g_assert_cmpint (len, ==, sizeof (TEST_DATA));
627       g_assert_cmpstr (buffer, ==, TEST_DATA);
628
629       waitpid (pid, &status, 0);
630       g_assert (WIFEXITED (status));
631       g_assert_cmpint (WEXITSTATUS (status), ==, 0);
632     }
633
634   /* TODO: add test for g_unix_connection_send_credentials() and
635    * g_unix_connection_receive_credentials().
636    */
637 }
638 #endif /* G_OS_UNIX */
639
640 int
641 main (int   argc,
642       char *argv[])
643 {
644   g_type_init ();
645   g_test_init (&argc, &argv, NULL);
646
647   g_test_add_func ("/socket/ipv4_sync", test_ipv4_sync);
648   g_test_add_func ("/socket/ipv4_async", test_ipv4_async);
649   g_test_add_func ("/socket/ipv6_sync", test_ipv6_sync);
650   g_test_add_func ("/socket/ipv6_async", test_ipv6_async);
651 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
652   g_test_add_func ("/socket/ipv6_v4mapped", test_ipv6_v4mapped);
653 #endif
654 #ifdef G_OS_UNIX
655   g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
656   g_test_add_func ("/socket/unix-connection", test_unix_connection);
657   g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data);
658 #endif
659
660   return g_test_run();
661 }
662