More test coverage for settings backends
[platform/upstream/glib.git] / gio / tests / gdbus-overflow.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include <gio/gio.h>
26 #include <unistd.h>
27 #include <string.h>
28
29 /* for open(2) */
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <string.h>
34
35 /* for g_unlink() */
36 #include <glib/gstdio.h>
37
38 #include <gio/gnetworking.h>
39 #include <gio/gunixsocketaddress.h>
40 #include <gio/gunixfdlist.h>
41
42 /* used in test_overflow */
43 #ifdef G_OS_UNIX
44 #include <gio/gunixconnection.h>
45 #include <errno.h>
46 #endif
47
48 #ifdef G_OS_UNIX
49 static gboolean is_unix = TRUE;
50 #else
51 static gboolean is_unix = FALSE;
52 #endif
53
54 static gchar *tmp_address = NULL;
55 static gchar *test_guid = NULL;
56 static GMainLoop *loop = NULL;
57
58 static const gchar *test_interface_introspection_xml =
59   "<node>"
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'/>"
64   "    </method>"
65   "    <method name='EmitSignal'/>"
66   "    <method name='EmitSignalWithNameSet'/>"
67   "    <method name='OpenFile'>"
68   "      <arg type='s' name='path' direction='in'/>"
69   "    </method>"
70   "    <signal name='PeerSignal'>"
71   "      <arg type='s' name='a_string'/>"
72   "    </signal>"
73   "    <property type='s' name='PeerProperty' access='read'/>"
74   "  </interface>"
75   "</node>";
76 static GDBusInterfaceInfo *test_interface_introspection_data = NULL;
77
78
79 #ifdef G_OS_UNIX
80
81 /* Chosen to be big enough to overflow the socket buffer */
82 #define OVERFLOW_NUM_SIGNALS 5000
83 #define OVERFLOW_TIMEOUT_SEC 10
84
85 static GDBusMessage *
86 overflow_filter_func (GDBusConnection *connection,
87                       GDBusMessage    *message,
88                       gboolean         incoming,
89                       gpointer         user_data)
90 {
91   volatile gint *counter = user_data;
92   *counter += 1;
93   return message;
94 }
95
96 static gboolean
97 overflow_on_500ms_later_func (gpointer user_data)
98 {
99   g_main_loop_quit (loop);
100   return FALSE; /* don't keep the idle */
101 }
102
103 static void
104 test_overflow (void)
105 {
106   gint sv[2];
107   gint n;
108   GSocket *socket;
109   GSocketConnection *socket_connection;
110   GDBusConnection *producer, *consumer;
111   GError *error;
112   GTimer *timer;
113   volatile gint n_messages_received;
114   volatile gint n_messages_sent;
115
116   g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
117
118   error = NULL;
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),
125                                          NULL, /* guid */
126                                          G_DBUS_CONNECTION_FLAGS_NONE,
127                                          NULL, /* GDBusAuthObserver */
128                                          NULL, /* GCancellable */
129
130                                          &error);
131   g_dbus_connection_set_exit_on_close (producer, TRUE);
132   g_assert_no_error (error);
133   g_object_unref (socket_connection);
134   n_messages_sent = 0;
135   g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
136
137   /* send enough data that we get an EAGAIN */
138   for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
139     {
140       error = NULL;
141       g_dbus_connection_emit_signal (producer,
142                                      NULL, /* destination */
143                                      "/org/foo/Object",
144                                      "org.foo.Interface",
145                                      "Member",
146                                      g_variant_new ("(s)", "a string"),
147                                      &error);
148       g_assert_no_error (error);
149     }
150
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
154    *
155    * This is to verify that not all the submitted messages have been
156    * sent to the underlying transport.
157    */
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);
161
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),
169                                          NULL, /* guid */
170                                          G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
171                                          NULL, /* GDBusAuthObserver */
172                                          NULL, /* GCancellable */
173                                          &error);
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);
179
180   timer = g_timer_new ();
181   g_timer_start (timer);
182
183   while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
184       g_main_context_iteration (NULL, FALSE);
185
186   g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
187   g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
188
189   g_timer_destroy (timer);
190   g_object_unref (consumer);
191   g_object_unref (producer);
192 }
193 #else
194 static void
195 test_overflow (void)
196 {
197   /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
198 }
199 #endif
200
201 /* ---------------------------------------------------------------------------------------------------- */
202
203
204 int
205 main (int   argc,
206       char *argv[])
207 {
208   gint ret;
209   GDBusNodeInfo *introspection_data = NULL;
210   gchar *tmpdir = NULL;
211
212   g_test_init (&argc, &argv, NULL);
213
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];
217
218   test_guid = g_dbus_generate_guid ();
219
220   if (is_unix)
221     {
222       if (g_unix_socket_address_abstract_names_supported ())
223         tmp_address = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
224       else
225         {
226           tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
227           tmp_address = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
228         }
229     }
230   else
231     tmp_address = g_strdup ("nonce-tcp:");
232
233   /* all the tests rely on a shared main loop */
234   loop = g_main_loop_new (NULL, FALSE);
235
236   g_test_add_func ("/gdbus/overflow", test_overflow);
237
238   ret = g_test_run();
239
240   g_main_loop_unref (loop);
241   g_free (test_guid);
242   g_dbus_node_info_unref (introspection_data);
243   if (is_unix)
244     g_free (tmp_address);
245   if (tmpdir)
246     {
247       g_rmdir (tmpdir);
248       g_free (tmpdir);
249     }
250
251   return ret;
252 }