Add some more cases to the app-id unit tests
[glib.git] / gio / tests / gdbus-overflow.c
blob507d565ed3f94d8ef4041ec7c468e579ea8dbaa5
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, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
21 #include "config.h"
23 #include <gio/gio.h>
24 #include <unistd.h>
25 #include <string.h>
27 /* for open(2) */
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <string.h>
33 /* for g_unlink() */
34 #include <glib/gstdio.h>
36 #include <gio/gnetworking.h>
37 #include <gio/gunixsocketaddress.h>
38 #include <gio/gunixfdlist.h>
40 /* used in test_overflow */
41 #ifdef G_OS_UNIX
42 #include <gio/gunixconnection.h>
43 #include <errno.h>
44 #endif
46 #ifdef G_OS_UNIX
47 static gboolean is_unix = TRUE;
48 #else
49 static gboolean is_unix = FALSE;
50 #endif
52 static gchar *tmp_address = NULL;
53 static gchar *test_guid = NULL;
54 static GMainLoop *loop = NULL;
56 static const gchar *test_interface_introspection_xml =
57 "<node>"
58 " <interface name='org.gtk.GDBus.PeerTestInterface'>"
59 " <method name='HelloPeer'>"
60 " <arg type='s' name='greeting' direction='in'/>"
61 " <arg type='s' name='response' direction='out'/>"
62 " </method>"
63 " <method name='EmitSignal'/>"
64 " <method name='EmitSignalWithNameSet'/>"
65 " <method name='OpenFile'>"
66 " <arg type='s' name='path' direction='in'/>"
67 " </method>"
68 " <signal name='PeerSignal'>"
69 " <arg type='s' name='a_string'/>"
70 " </signal>"
71 " <property type='s' name='PeerProperty' access='read'/>"
72 " </interface>"
73 "</node>";
74 static GDBusInterfaceInfo *test_interface_introspection_data = NULL;
77 #ifdef G_OS_UNIX
79 /* Chosen to be big enough to overflow the socket buffer */
80 #define OVERFLOW_NUM_SIGNALS 5000
81 #define OVERFLOW_TIMEOUT_SEC 10
83 static GDBusMessage *
84 overflow_filter_func (GDBusConnection *connection,
85 GDBusMessage *message,
86 gboolean incoming,
87 gpointer user_data)
89 volatile gint *counter = user_data;
90 *counter += 1;
91 return message;
94 static gboolean
95 overflow_on_500ms_later_func (gpointer user_data)
97 g_main_loop_quit (loop);
98 return FALSE; /* don't keep the idle */
101 static void
102 test_overflow (void)
104 gint sv[2];
105 gint n;
106 GSocket *socket;
107 GSocketConnection *socket_connection;
108 GDBusConnection *producer, *consumer;
109 GError *error;
110 GTimer *timer;
111 volatile gint n_messages_received;
112 volatile gint n_messages_sent;
114 g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
116 error = NULL;
117 socket = g_socket_new_from_fd (sv[0], &error);
118 g_assert_no_error (error);
119 socket_connection = g_socket_connection_factory_create_connection (socket);
120 g_assert (socket_connection != NULL);
121 g_object_unref (socket);
122 producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
123 NULL, /* guid */
124 G_DBUS_CONNECTION_FLAGS_NONE,
125 NULL, /* GDBusAuthObserver */
126 NULL, /* GCancellable */
128 &error);
129 g_dbus_connection_set_exit_on_close (producer, TRUE);
130 g_assert_no_error (error);
131 g_object_unref (socket_connection);
132 n_messages_sent = 0;
133 g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
135 /* send enough data that we get an EAGAIN */
136 for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
138 error = NULL;
139 g_dbus_connection_emit_signal (producer,
140 NULL, /* destination */
141 "/org/foo/Object",
142 "org.foo.Interface",
143 "Member",
144 g_variant_new ("(s)", "a string"),
145 &error);
146 g_assert_no_error (error);
149 /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
150 * kernel buffers) and verify that n_messages_sent <
151 * OVERFLOW_NUM_SIGNALS
153 * This is to verify that not all the submitted messages have been
154 * sent to the underlying transport.
156 g_timeout_add (500, overflow_on_500ms_later_func, NULL);
157 g_main_loop_run (loop);
158 g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
160 /* now suck it all out as a client, and add it up */
161 socket = g_socket_new_from_fd (sv[1], &error);
162 g_assert_no_error (error);
163 socket_connection = g_socket_connection_factory_create_connection (socket);
164 g_assert (socket_connection != NULL);
165 g_object_unref (socket);
166 consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
167 NULL, /* guid */
168 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
169 NULL, /* GDBusAuthObserver */
170 NULL, /* GCancellable */
171 &error);
172 g_assert_no_error (error);
173 g_object_unref (socket_connection);
174 n_messages_received = 0;
175 g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
176 g_dbus_connection_start_message_processing (consumer);
178 timer = g_timer_new ();
179 g_timer_start (timer);
181 while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
182 g_main_context_iteration (NULL, FALSE);
184 g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
185 g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
187 g_timer_destroy (timer);
188 g_object_unref (consumer);
189 g_object_unref (producer);
191 #else
192 static void
193 test_overflow (void)
195 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
197 #endif
199 /* ---------------------------------------------------------------------------------------------------- */
203 main (int argc,
204 char *argv[])
206 gint ret;
207 GDBusNodeInfo *introspection_data = NULL;
208 gchar *tmpdir = NULL;
210 g_test_init (&argc, &argv, NULL);
212 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
213 g_assert (introspection_data != NULL);
214 test_interface_introspection_data = introspection_data->interfaces[0];
216 test_guid = g_dbus_generate_guid ();
218 if (is_unix)
220 if (g_unix_socket_address_abstract_names_supported ())
221 tmp_address = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
222 else
224 tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
225 tmp_address = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
228 else
229 tmp_address = g_strdup ("nonce-tcp:");
231 /* all the tests rely on a shared main loop */
232 loop = g_main_loop_new (NULL, FALSE);
234 g_test_add_func ("/gdbus/overflow", test_overflow);
236 ret = g_test_run();
238 g_main_loop_unref (loop);
239 g_free (test_guid);
240 g_dbus_node_info_unref (introspection_data);
241 if (is_unix)
242 g_free (tmp_address);
243 if (tmpdir)
245 g_rmdir (tmpdir);
246 g_free (tmpdir);
249 return ret;