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>
28 #include <sys/types.h>
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 */
42 #include <gio/gunixconnection.h>
47 static gboolean is_unix
= TRUE
;
49 static gboolean is_unix
= FALSE
;
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
=
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'/>"
63 " <method name='EmitSignal'/>"
64 " <method name='EmitSignalWithNameSet'/>"
65 " <method name='OpenFile'>"
66 " <arg type='s' name='path' direction='in'/>"
68 " <signal name='PeerSignal'>"
69 " <arg type='s' name='a_string'/>"
71 " <property type='s' name='PeerProperty' access='read'/>"
74 static GDBusInterfaceInfo
*test_interface_introspection_data
= NULL
;
79 /* Chosen to be big enough to overflow the socket buffer */
80 #define OVERFLOW_NUM_SIGNALS 5000
81 #define OVERFLOW_TIMEOUT_SEC 10
84 overflow_filter_func (GDBusConnection
*connection
,
85 GDBusMessage
*message
,
89 volatile gint
*counter
= user_data
;
95 overflow_on_500ms_later_func (gpointer user_data
)
97 g_main_loop_quit (loop
);
98 return FALSE
; /* don't keep the idle */
107 GSocketConnection
*socket_connection
;
108 GDBusConnection
*producer
, *consumer
;
111 volatile gint n_messages_received
;
112 volatile gint n_messages_sent
;
114 g_assert_cmpint (socketpair (AF_UNIX
, SOCK_STREAM
, 0, sv
), ==, 0);
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
),
124 G_DBUS_CONNECTION_FLAGS_NONE
,
125 NULL
, /* GDBusAuthObserver */
126 NULL
, /* GCancellable */
129 g_dbus_connection_set_exit_on_close (producer
, TRUE
);
130 g_assert_no_error (error
);
131 g_object_unref (socket_connection
);
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
++)
139 g_dbus_connection_emit_signal (producer
,
140 NULL
, /* destination */
144 g_variant_new ("(s)", "a string"),
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
),
168 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING
,
169 NULL
, /* GDBusAuthObserver */
170 NULL
, /* GCancellable */
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
);
195 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
199 /* ---------------------------------------------------------------------------------------------------- */
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 ();
220 if (g_unix_socket_address_abstract_names_supported ())
221 tmp_address
= g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
224 tmpdir
= g_dir_make_tmp ("gdbus-test-XXXXXX", NULL
);
225 tmp_address
= g_strdup_printf ("unix:tmpdir=%s", tmpdir
);
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
);
238 g_main_loop_unref (loop
);
240 g_dbus_node_info_unref (introspection_data
);
242 g_free (tmp_address
);