1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2010 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.
20 * Author: David Zeuthen <davidz@redhat.com>
27 #include <sys/types.h>
28 #include <sys/socket.h>
32 #include <gio/gunixconnection.h>
37 test_unix_from_fd (void)
43 fd = socket (AF_UNIX, SOCK_STREAM, 0);
44 g_assert_cmpint (fd, !=, -1);
47 s = g_socket_new_from_fd (fd, &error);
48 g_assert_no_error (error);
49 g_assert_cmpint (g_socket_get_family (s), ==, G_SOCKET_FAMILY_UNIX);
50 g_assert_cmpint (g_socket_get_socket_type (s), ==, G_SOCKET_TYPE_STREAM);
51 g_assert_cmpint (g_socket_get_protocol (s), ==, G_SOCKET_PROTOCOL_DEFAULT);
56 test_unix_connection (void)
63 fd = socket (AF_UNIX, SOCK_STREAM, 0);
64 g_assert_cmpint (fd, !=, -1);
67 s = g_socket_new_from_fd (fd, &error);
68 g_assert_no_error (error);
69 c = g_socket_connection_factory_create_connection (s);
70 g_assert (G_IS_UNIX_CONNECTION (c));
75 static GSocketConnection *
76 create_connection_for_fd (int fd)
80 GSocketConnection *connection;
82 socket = g_socket_new_from_fd (fd, &err);
83 g_assert_no_error (err);
84 g_assert (G_IS_SOCKET (socket));
85 connection = g_socket_connection_factory_create_connection (socket);
86 g_assert (G_IS_UNIX_CONNECTION (connection));
87 g_object_unref (socket);
91 #define TEST_DATA "failure to say failure to say 'i love gnome-panel!'."
94 test_unix_connection_ancillary_data (void)
103 g_assert_cmpint (status, ==, 0);
105 status = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
106 g_assert_cmpint (status, ==, 0);
109 g_assert_cmpint (pid, >=, 0);
111 /* Child: close its copy of the write end of the pipe, receive it
112 * again from the parent over the socket, and write some text to it.
114 * Parent: send the write end of the pipe (still open for the
115 * parent) over the socket, close it, and read some text from the
116 * read end of the pipe.
120 GSocketConnection *connection;
123 connection = create_connection_for_fd (sv[0]);
125 status = close (pv[1]);
126 g_assert_cmpint (status, ==, 0);
129 fd = g_unix_connection_receive_fd (G_UNIX_CONNECTION (connection), NULL,
131 g_assert_no_error (err);
132 g_assert_cmpint (fd, >, -1);
133 g_object_unref (connection);
136 len = write (fd, TEST_DATA, sizeof (TEST_DATA));
137 while (len == -1 && errno == EINTR);
138 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
143 GSocketConnection *connection;
146 connection = create_connection_for_fd (sv[1]);
149 g_unix_connection_send_fd (G_UNIX_CONNECTION (connection), pv[1], NULL,
151 g_assert_no_error (err);
152 g_object_unref (connection);
154 status = close (pv[1]);
155 g_assert_cmpint (status, ==, 0);
157 memset (buffer, 0xff, sizeof buffer);
159 len = read (pv[0], buffer, sizeof buffer);
160 while (len == -1 && errno == EINTR);
162 g_assert_cmpint (len, ==, sizeof (TEST_DATA));
163 g_assert_cmpstr (buffer, ==, TEST_DATA);
165 waitpid (pid, &status, 0);
166 g_assert (WIFEXITED (status));
167 g_assert_cmpint (WEXITSTATUS (status), ==, 0);
170 /* TODO: add test for g_unix_connection_send_credentials() and
171 * g_unix_connection_receive_credentials().
174 #endif /* G_OS_UNIX */
181 g_test_init (&argc, &argv, NULL);
184 g_test_add_func ("/socket/unix-from-fd", test_unix_from_fd);
185 g_test_add_func ("/socket/unix-connection", test_unix_connection);
186 g_test_add_func ("/socket/unix-connection-ancillary-data", test_unix_connection_ancillary_data);