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>
30 #include <sys/types.h>
36 #include <glib/gstdio.h>
38 #include <gio/gnetworking.h>
39 #include <gio/gunixsocketaddress.h>
40 #include <gio/gunixfdlist.h>
42 /* used in test_overflow */
44 #include <gio/gunixconnection.h>
49 static gboolean is_unix = TRUE;
51 static gboolean is_unix = FALSE;
54 static gchar *tmp_address = NULL;
55 static gchar *test_guid = NULL;
56 static GMainLoop *loop = NULL;
58 static const gchar *test_interface_introspection_xml =
60 " <interface name='org.gtk.GDBus.PeerTestInterface'>"
61 " <method name='HelloPeer'>"
62 " <arg type='s' name='greeting' direction='in'/>"
63 " <arg type='s' name='response' direction='out'/>"
65 " <method name='EmitSignal'/>"
66 " <method name='EmitSignalWithNameSet'/>"
67 " <method name='OpenFile'>"
68 " <arg type='s' name='path' direction='in'/>"
70 " <signal name='PeerSignal'>"
71 " <arg type='s' name='a_string'/>"
73 " <property type='s' name='PeerProperty' access='read'/>"
76 static GDBusInterfaceInfo *test_interface_introspection_data = NULL;
81 /* Chosen to be big enough to overflow the socket buffer */
82 #define OVERFLOW_NUM_SIGNALS 5000
83 #define OVERFLOW_TIMEOUT_SEC 10
86 overflow_filter_func (GDBusConnection *connection,
87 GDBusMessage *message,
91 volatile gint *counter = user_data;
97 overflow_on_500ms_later_func (gpointer user_data)
99 g_main_loop_quit (loop);
100 return FALSE; /* don't keep the idle */
109 GSocketConnection *socket_connection;
110 GDBusConnection *producer, *consumer;
113 volatile gint n_messages_received;
114 volatile gint n_messages_sent;
116 g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
119 socket = g_socket_new_from_fd (sv[0], &error);
120 g_assert_no_error (error);
121 socket_connection = g_socket_connection_factory_create_connection (socket);
122 g_assert (socket_connection != NULL);
123 g_object_unref (socket);
124 producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
126 G_DBUS_CONNECTION_FLAGS_NONE,
127 NULL, /* GDBusAuthObserver */
128 NULL, /* GCancellable */
131 g_dbus_connection_set_exit_on_close (producer, TRUE);
132 g_assert_no_error (error);
133 g_object_unref (socket_connection);
135 g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
137 /* send enough data that we get an EAGAIN */
138 for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
141 g_dbus_connection_emit_signal (producer,
142 NULL, /* destination */
146 g_variant_new ("(s)", "a string"),
148 g_assert_no_error (error);
151 /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
152 * kernel buffers) and verify that n_messages_sent <
153 * OVERFLOW_NUM_SIGNALS
155 * This is to verify that not all the submitted messages have been
156 * sent to the underlying transport.
158 g_timeout_add (500, overflow_on_500ms_later_func, NULL);
159 g_main_loop_run (loop);
160 g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
162 /* now suck it all out as a client, and add it up */
163 socket = g_socket_new_from_fd (sv[1], &error);
164 g_assert_no_error (error);
165 socket_connection = g_socket_connection_factory_create_connection (socket);
166 g_assert (socket_connection != NULL);
167 g_object_unref (socket);
168 consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
170 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
171 NULL, /* GDBusAuthObserver */
172 NULL, /* GCancellable */
174 g_assert_no_error (error);
175 g_object_unref (socket_connection);
176 n_messages_received = 0;
177 g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
178 g_dbus_connection_start_message_processing (consumer);
180 timer = g_timer_new ();
181 g_timer_start (timer);
183 while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
184 g_main_context_iteration (NULL, FALSE);
186 g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
187 g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
189 g_timer_destroy (timer);
190 g_object_unref (consumer);
191 g_object_unref (producer);
197 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
201 /* ---------------------------------------------------------------------------------------------------- */
209 GDBusNodeInfo *introspection_data = NULL;
210 gchar *tmpdir = NULL;
212 g_test_init (&argc, &argv, NULL);
214 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
215 g_assert (introspection_data != NULL);
216 test_interface_introspection_data = introspection_data->interfaces[0];
218 test_guid = g_dbus_generate_guid ();
222 if (g_unix_socket_address_abstract_names_supported ())
223 tmp_address = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
226 tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
227 tmp_address = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
231 tmp_address = g_strdup ("nonce-tcp:");
233 /* all the tests rely on a shared main loop */
234 loop = g_main_loop_new (NULL, FALSE);
236 g_test_add_func ("/gdbus/overflow", test_overflow);
240 g_main_loop_unref (loop);
242 g_dbus_node_info_unref (introspection_data);
244 g_free (tmp_address);