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 "gdbus-tests.h"
29 /* all tests rely on a shared mainloop */
30 static GMainLoop *loop = NULL;
32 /* ---------------------------------------------------------------------------------------------------- */
33 /* Test that the method aspects of GDBusProxy works */
34 /* ---------------------------------------------------------------------------------------------------- */
37 test_methods (GDBusConnection *connection,
39 const gchar *name_owner,
45 gchar *dbus_error_name;
47 /* check that we can invoke a method */
49 result = g_dbus_proxy_invoke_method_sync (proxy,
51 g_variant_new ("(s)", "Hey"),
52 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
56 g_assert_no_error (error);
57 g_assert (result != NULL);
58 g_assert_cmpstr (g_variant_get_type_string (result), ==, "(s)");
59 g_variant_get (result, "(s)", &str);
60 g_assert_cmpstr (str, ==, "You greeted me with 'Hey'. Thanks!");
61 g_variant_unref (result);
63 /* Check that we can completely recover the returned error */
64 result = g_dbus_proxy_invoke_method_sync (proxy,
66 g_variant_new ("(s)", "Yo"),
67 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
71 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
72 g_assert (g_dbus_error_is_remote_error (error));
73 g_assert (g_dbus_error_is_remote_error (error));
74 g_assert (result == NULL);
75 dbus_error_name = g_dbus_error_get_remote_error (error);
76 g_assert_cmpstr (dbus_error_name, ==, "com.example.TestException");
77 g_free (dbus_error_name);
78 g_assert (g_dbus_error_strip_remote_error (error));
79 g_assert_cmpstr (error->message, ==, "Yo is not a proper greeting");
80 g_clear_error (&error);
82 /* Check that we get a timeout if the method handling is taking longer than timeout */
84 result = g_dbus_proxy_invoke_method_sync (proxy,
86 g_variant_new ("(i)", 500 /* msec */),
87 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
91 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
92 g_assert (!g_dbus_error_is_remote_error (error));
93 g_assert (result == NULL);
94 g_clear_error (&error);
96 /* Check that proxy-default timeouts work. */
97 g_assert_cmpint (g_dbus_proxy_get_default_timeout (proxy), ==, -1);
99 /* the default timeout is 25000 msec so this should work */
100 result = g_dbus_proxy_invoke_method_sync (proxy,
102 g_variant_new ("(i)", 500 /* msec */),
103 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
104 -1, /* use proxy default (e.g. -1 -> e.g. 25000 msec) */
107 g_assert_no_error (error);
108 g_assert (result != NULL);
109 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
110 g_variant_unref (result);
112 /* now set the proxy-default timeout to 250 msec and try the 500 msec call - this should FAIL */
113 g_dbus_proxy_set_default_timeout (proxy, 250);
114 g_assert_cmpint (g_dbus_proxy_get_default_timeout (proxy), ==, 250);
115 result = g_dbus_proxy_invoke_method_sync (proxy,
117 g_variant_new ("(i)", 500 /* msec */),
118 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
119 -1, /* use proxy default (e.g. 250 msec) */
122 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
123 g_assert (!g_dbus_error_is_remote_error (error));
124 g_assert (result == NULL);
125 g_clear_error (&error);
127 /* clean up after ourselves */
128 g_dbus_proxy_set_default_timeout (proxy, -1);
131 /* ---------------------------------------------------------------------------------------------------- */
132 /* Test that the property aspects of GDBusProxy works */
133 /* ---------------------------------------------------------------------------------------------------- */
136 test_properties (GDBusConnection *connection,
138 const gchar *name_owner,
149 * Check that we can read cached properties.
151 * No need to test all properties - GVariant has already been tested
153 variant = g_dbus_proxy_get_cached_property (proxy, "y", &error);
154 g_assert_no_error (error);
155 g_assert (variant != NULL);
156 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
157 g_variant_unref (variant);
158 variant = g_dbus_proxy_get_cached_property (proxy, "o", &error);
159 g_assert_no_error (error);
160 g_assert (variant != NULL);
161 g_assert_cmpstr (g_variant_get_string (variant, NULL), ==, "/some/path");
162 g_variant_unref (variant);
165 * Now ask the service to change a property and check that #GDBusProxy::g-property-changed
166 * is received. Also check that the cache is updated.
168 variant2 = g_variant_new_byte (42);
169 result = g_dbus_proxy_invoke_method_sync (proxy,
171 g_variant_new ("(sv)",
174 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
178 g_assert_no_error (error);
179 g_assert (result != NULL);
180 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
181 g_variant_unref (result);
182 _g_assert_signal_received (proxy, "g-properties-changed");
183 variant = g_dbus_proxy_get_cached_property (proxy, "y", &error);
184 g_assert_no_error (error);
185 g_assert (variant != NULL);
186 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
187 g_variant_unref (variant);
190 /* ---------------------------------------------------------------------------------------------------- */
191 /* Test that the signal aspects of GDBusProxy works */
192 /* ---------------------------------------------------------------------------------------------------- */
195 test_proxy_signals_on_signal (GDBusProxy *proxy,
196 const gchar *sender_name,
197 const gchar *signal_name,
198 GVariant *parameters,
201 GString *s = user_data;
203 g_assert_cmpstr (signal_name, ==, "TestSignal");
204 g_assert_cmpstr (g_variant_get_type_string (parameters), ==, "(sov)");
206 g_variant_print_string (parameters, s, TRUE);
211 GMainLoop *internal_loop;
216 test_proxy_signals_on_emit_signal_cb (GDBusProxy *proxy,
220 TestSignalData *data = user_data;
225 result = g_dbus_proxy_invoke_method_finish (proxy,
228 g_assert_no_error (error);
229 g_assert (result != NULL);
230 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
231 g_variant_unref (result);
233 /* check that the signal was recieved before we got the method result */
234 g_assert (strlen (data->s->str) > 0);
236 /* break out of the loop */
237 g_main_loop_quit (data->internal_loop);
241 test_signals (GDBusConnection *connection,
243 const gchar *name_owner,
248 gulong signal_handler_id;
255 * Ask the service to emit a signal and check that we receive it.
257 * Note that blocking calls don't block in the mainloop so wait for the signal (which
258 * is dispatched before the method reply)
260 s = g_string_new (NULL);
261 signal_handler_id = g_signal_connect (proxy,
263 G_CALLBACK (test_proxy_signals_on_signal),
266 result = g_dbus_proxy_invoke_method_sync (proxy,
268 g_variant_new ("(so)",
269 "Accept the next proposition you hear",
271 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
275 g_assert_no_error (error);
276 g_assert (result != NULL);
277 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
278 g_variant_unref (result);
279 /* check that we haven't received the signal just yet */
280 g_assert (strlen (s->str) == 0);
281 /* and now wait for the signal */
282 _g_assert_signal_received (proxy, "g-signal");
283 g_assert_cmpstr (s->str,
285 "('Accept the next proposition you hear .. in bed!', objectpath '/some/path/in/bed', <'a variant'>)");
286 g_signal_handler_disconnect (proxy, signal_handler_id);
287 g_string_free (s, TRUE);
290 * Now do this async to check the signal is received before the method returns.
292 s = g_string_new (NULL);
293 data.internal_loop = g_main_loop_new (NULL, FALSE);
295 signal_handler_id = g_signal_connect (proxy,
297 G_CALLBACK (test_proxy_signals_on_signal),
299 g_dbus_proxy_invoke_method (proxy,
301 g_variant_new ("(so)",
302 "You will make a great programmer",
304 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
307 (GAsyncReadyCallback) test_proxy_signals_on_emit_signal_cb,
309 g_main_loop_run (data.internal_loop);
310 g_main_loop_unref (data.internal_loop);
311 g_assert_cmpstr (s->str,
313 "('You will make a great programmer .. in bed!', objectpath '/some/other/path/in/bed', <'a variant'>)");
314 g_signal_handler_disconnect (proxy, signal_handler_id);
315 g_string_free (s, TRUE);
319 test_bogus_method_return (GDBusConnection *connection,
321 const gchar *name_owner,
324 GError *error = NULL;
327 result = g_dbus_proxy_invoke_method_sync (proxy,
330 G_DBUS_INVOKE_METHOD_FLAGS_NONE,
334 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
335 g_assert (result == NULL);
338 /* ---------------------------------------------------------------------------------------------------- */
340 static const gchar *frob_dbus_interface_xml =
342 " <interface name='com.example.Frob'>"
343 /* Deliberately different from gdbus-testserver.py's definition */
344 " <method name='PairReturn'>"
345 " <arg type='u' name='somenumber' direction='in'/>"
346 " <arg type='s' name='somestring' direction='out'/>"
348 " <method name='HelloWorld'>"
349 " <arg type='s' name='somestring' direction='in'/>"
350 " <arg type='s' name='somestring' direction='out'/>"
352 " <method name='Sleep'>"
353 " <arg type='i' name='timeout' direction='in'/>"
357 static GDBusInterfaceInfo *frob_dbus_interface_info;
360 on_proxy_appeared (GDBusConnection *connection,
362 const gchar *name_owner,
366 test_methods (connection, name, name_owner, proxy);
367 test_properties (connection, name, name_owner, proxy);
368 test_signals (connection, name, name_owner, proxy);
370 /* Now repeat the method tests, with an expected interface set */
371 g_dbus_proxy_set_interface_info (proxy, frob_dbus_interface_info);
372 test_methods (connection, name, name_owner, proxy);
374 /* And now one more test where we deliberately set the expected
375 * interface definition incorrectly
377 test_bogus_method_return (connection, name, name_owner, proxy);
379 g_main_loop_quit (loop);
383 on_proxy_vanished (GDBusConnection *connection,
396 /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
397 * until one can connect to the bus but that's not how things work right now
401 watcher_id = g_bus_watch_proxy (G_BUS_TYPE_SESSION,
402 "com.example.TestService",
403 G_BUS_NAME_WATCHER_FLAGS_NONE,
404 "/com/example/TestObject",
407 G_DBUS_PROXY_FLAGS_NONE,
413 /* this is safe; testserver will exit once the bus goes away */
414 g_assert (g_spawn_command_line_async ("./gdbus-testserver.py", NULL));
416 g_main_loop_run (loop);
418 g_bus_unwatch_proxy (watcher_id);
424 /* ---------------------------------------------------------------------------------------------------- */
431 GDBusNodeInfo *introspection_data = NULL;
434 g_test_init (&argc, &argv, NULL);
436 introspection_data = g_dbus_node_info_new_for_xml (frob_dbus_interface_xml, NULL);
437 g_assert (introspection_data != NULL);
438 frob_dbus_interface_info = introspection_data->interfaces[0];
440 /* all the tests rely on a shared main loop */
441 loop = g_main_loop_new (NULL, FALSE);
443 /* all the tests use a session bus with a well-known address that we can bring up and down
444 * using session_bus_up() and session_bus_down().
446 g_unsetenv ("DISPLAY");
447 g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
449 g_test_add_func ("/gdbus/proxy", test_proxy);
453 g_dbus_node_info_unref (introspection_data);