1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2011 Red Hat, Inc.
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.
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.
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.
25 #include <sys/types.h>
26 #include <sys/socket.h>
30 #include <gio/gunixconnection.h>
33 #include "gnetworkingprivate.h"
44 echo_server_thread (gpointer user_data)
46 IPTestData *data = user_data;
52 sock = g_socket_accept (data->server, NULL, &error);
53 g_assert_no_error (error);
57 nread = g_socket_receive (sock, buf, sizeof (buf), NULL, &error);
58 g_assert_no_error (error);
59 g_assert_cmpint (nread, >=, 0);
64 nwrote = g_socket_send (sock, buf, nread, NULL, &error);
65 g_assert_no_error (error);
66 g_assert_cmpint (nwrote, ==, nread);
69 g_socket_close (sock, &error);
70 g_assert_no_error (error);
75 create_server (GSocketFamily family,
76 GThreadFunc server_thread,
85 data = g_slice_new (IPTestData);
86 data->family = family;
88 data->server = server = g_socket_new (family,
90 G_SOCKET_PROTOCOL_DEFAULT,
92 g_assert_no_error (error);
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);
98 g_socket_set_blocking (server, TRUE);
100 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
105 fd = g_socket_get_fd (server);
107 setsockopt (fd, IPPROTO_IPV6, IPV6_V6ONLY, &v6_only, sizeof (v6_only));
112 iaddr = g_inet_address_new_any (family);
114 iaddr = g_inet_address_new_loopback (family);
115 addr = g_inet_socket_address_new (iaddr, 0);
116 g_object_unref (iaddr);
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);
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);
128 g_socket_listen (server, &error);
129 g_assert_no_error (error);
131 data->thread = g_thread_new ("server", server_thread, data);
136 static const gchar *testbuf = "0123456789abcdef";
139 test_ip_async_read_ready (GSocket *client,
143 IPTestData *data = user_data;
144 GError *error = NULL;
148 g_assert_cmpint (cond, ==, G_IO_IN);
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);
154 g_assert_cmpstr (testbuf, ==, buf);
156 g_main_loop_quit (data->loop);
162 test_ip_async_write_ready (GSocket *client,
166 IPTestData *data = user_data;
167 GError *error = NULL;
171 g_assert_cmpint (cond, ==, G_IO_OUT);
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);
177 source = g_socket_create_source (client, G_IO_IN, NULL);
178 g_source_set_callback (source, (GSourceFunc)test_ip_async_read_ready,
180 g_source_attach (source, NULL);
181 g_source_unref (source);
187 test_ip_async_timed_out (GSocket *client,
191 IPTestData *data = user_data;
192 GError *error = NULL;
197 if (data->family == G_SOCKET_FAMILY_IPV4)
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);
206 source = g_socket_create_source (client, G_IO_OUT, NULL);
207 g_source_set_callback (source, (GSourceFunc)test_ip_async_write_ready,
209 g_source_attach (source, NULL);
210 g_source_unref (source);
216 test_ip_async_connected (GSocket *client,
220 IPTestData *data = user_data;
221 GError *error = NULL;
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.
231 g_assert_cmpint (cond, ==, G_IO_OUT);
233 g_assert (g_socket_is_connected (client));
235 /* This adds 1 second to "make check", so let's just only do it once. */
236 if (data->family == G_SOCKET_FAMILY_IPV4)
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);
243 source = g_socket_create_source (client, G_IO_IN, NULL);
244 g_source_set_callback (source, (GSourceFunc)test_ip_async_timed_out,
246 g_source_attach (source, NULL);
247 g_source_unref (source);
250 test_ip_async_timed_out (client, 0, data);
256 idle_test_ip_async_connected (gpointer user_data)
258 IPTestData *data = user_data;
260 return test_ip_async_connected (data->client, G_IO_OUT, data);
264 test_ip_async (GSocketFamily family)
267 GError *error = NULL;
269 GSocketAddress *addr;
274 data = create_server (family, echo_server_thread, FALSE);
275 addr = g_socket_get_local_address (data->server, &error);
277 client = g_socket_new (family,
278 G_SOCKET_TYPE_STREAM,
279 G_SOCKET_PROTOCOL_DEFAULT,
281 g_assert_no_error (error);
282 data->client = client;
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);
288 g_socket_set_blocking (client, FALSE);
289 g_socket_set_timeout (client, 1);
291 if (g_socket_connect (client, addr, NULL, &error))
293 g_assert_no_error (error);
294 g_idle_add (idle_test_ip_async_connected, data);
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,
303 g_source_attach (source, NULL);
304 g_source_unref (source);
306 g_object_unref (addr);
308 data->loop = g_main_loop_new (NULL, TRUE);
309 g_main_loop_run (data->loop);
310 g_main_loop_unref (data->loop);
312 g_socket_shutdown (client, FALSE, TRUE, &error);
313 g_assert_no_error (error);
315 g_thread_join (data->thread);
317 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
318 g_assert_no_error (error);
319 g_assert_cmpint (len, ==, 0);
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);
326 g_object_unref (data->server);
327 g_object_unref (client);
329 g_slice_free (IPTestData, data);
333 test_ipv4_async (void)
335 test_ip_async (G_SOCKET_FAMILY_IPV4);
339 test_ipv6_async (void)
341 test_ip_async (G_SOCKET_FAMILY_IPV6);
345 test_ip_sync (GSocketFamily family)
348 GError *error = NULL;
350 GSocketAddress *addr;
354 data = create_server (family, echo_server_thread, FALSE);
355 addr = g_socket_get_local_address (data->server, &error);
357 client = g_socket_new (family,
358 G_SOCKET_TYPE_STREAM,
359 G_SOCKET_PROTOCOL_DEFAULT,
361 g_assert_no_error (error);
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);
367 g_socket_set_blocking (client, TRUE);
368 g_socket_set_timeout (client, 1);
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);
375 /* This adds 1 second to "make check", so let's just only do it once. */
376 if (family == G_SOCKET_FAMILY_IPV4)
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);
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);
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);
392 g_assert_cmpstr (testbuf, ==, buf);
394 g_socket_shutdown (client, FALSE, TRUE, &error);
395 g_assert_no_error (error);
397 g_thread_join (data->thread);
399 len = g_socket_receive (client, buf, sizeof (buf), NULL, &error);
400 g_assert_no_error (error);
401 g_assert_cmpint (len, ==, 0);
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);
408 g_object_unref (data->server);
409 g_object_unref (client);
411 g_slice_free (IPTestData, data);
415 test_ipv4_sync (void)
417 test_ip_sync (G_SOCKET_FAMILY_IPV4);
421 test_ipv6_sync (void)
423 test_ip_sync (G_SOCKET_FAMILY_IPV6);
426 #if defined (IPPROTO_IPV6) && defined (IPV6_V6ONLY)
428 v4mapped_server_thread (gpointer user_data)
430 IPTestData *data = user_data;
432 GError *error = NULL;
433 GSocketAddress *addr;
435 sock = g_socket_accept (data->server, NULL, &error);
436 g_assert_no_error (error);
438 g_assert_cmpint (g_socket_get_family (sock), ==, G_SOCKET_FAMILY_IPV6);
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);
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);
450 g_socket_close (sock, &error);
451 g_assert_no_error (error);
456 test_ipv6_v4mapped (void)
459 GError *error = NULL;
461 GSocketAddress *addr, *v4addr;
464 data = create_server (G_SOCKET_FAMILY_IPV6, v4mapped_server_thread, TRUE);
466 client = g_socket_new (G_SOCKET_FAMILY_IPV4,
467 G_SOCKET_TYPE_STREAM,
468 G_SOCKET_PROTOCOL_DEFAULT,
470 g_assert_no_error (error);
472 g_socket_set_blocking (client, TRUE);
473 g_socket_set_timeout (client, 1);
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);
481 g_socket_connect (client, v4addr, NULL, &error);
482 g_assert_no_error (error);
483 g_assert (g_socket_is_connected (client));
485 g_thread_join (data->thread);
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);
492 g_object_unref (data->server);
493 g_object_unref (client);
495 g_slice_free (IPTestData, data);
501 test_unix_from_fd (void)
507 fd = socket (AF_UNIX, SOCK_STREAM, 0);
508 g_assert_cmpint (fd, !=, -1);
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);
520 test_unix_connection (void)
525 GSocketConnection *c;
527 fd = socket (AF_UNIX, SOCK_STREAM, 0);
528 g_assert_cmpint (fd, !=, -1);
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));
539 static GSocketConnection *
540 create_connection_for_fd (int fd)
544 GSocketConnection *connection;
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);
555 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
558 test_unix_connection_ancillary_data (void)
562 gint status, fd, len;
567 g_assert_cmpint (status, ==, 0);
569 status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
570 g_assert_cmpint (status, ==, 0);
573 g_assert_cmpint (pid, >=, 0);
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.
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.
584 GSocketConnection *connection;
587 connection = create_connection_for_fd (sv[0]);
589 status = close (pv[1]);
590 g_assert_cmpint (status, ==, 0);
593 fd = g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection), NULL,
595 g_assert_no_error (err);
596 g_assert_cmpint (fd, >, -1);
597 g_object_unref (connection);
600 len = write (fd, TEST_DATA, sizeof (TEST_DATA));
601 while (len == -1 && errno == EINTR);
602 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
607 GSocketConnection *connection;
610 connection = create_connection_for_fd (sv[1]);
613 g_unix_connection_send_fd (G_UNIX_CONNECTION (connection), pv[1], NULL,
615 g_assert_no_error (err);
616 g_object_unref (connection);
618 status = close (pv[1]);
619 g_assert_cmpint (status, ==, 0);
621 memset (buffer, 0xff, sizeof buffer);
623 len = read (pv[0], buffer, sizeof buffer);
624 while (len == -1 && errno == EINTR);
626 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
627 g_assert_cmpstr (buffer, ==, TEST_DATA);
629 waitpid (pid, &status, 0);
630 g_assert (WIFEXITED (status));
631 g_assert_cmpint (WEXITSTATUS (status), ==, 0);
634 /* TODO: add test for g_unix_connection_send_credentials() and
635 * g_unix_connection_receive_credentials().
638 #endif /* G_OS_UNIX */
645 g_test_init (&argc, &argv, NULL);
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);
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);