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_call_sync (proxy,
51 g_variant_new ("(s)", "Hey"),
52 G_DBUS_CALL_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_call_sync (proxy,
66 g_variant_new ("(s)", "Yo"),
67 G_DBUS_CALL_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_call_sync (proxy,
86 g_variant_new ("(i)", 500 /* msec */),
87 G_DBUS_CALL_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_call_sync (proxy,
102 g_variant_new ("(i)", 500 /* msec */),
103 G_DBUS_CALL_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_call_sync (proxy,
117 g_variant_new ("(i)", 500 /* msec */),
118 G_DBUS_CALL_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");
154 g_assert (variant != NULL);
155 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
156 g_variant_unref (variant);
157 variant = g_dbus_proxy_get_cached_property (proxy, "o");
158 g_assert (variant != NULL);
159 g_assert_cmpstr (g_variant_get_string (variant, NULL), ==, "/some/path");
160 g_variant_unref (variant);
163 * Now ask the service to change a property and check that #GDBusProxy::g-property-changed
164 * is received. Also check that the cache is updated.
166 variant2 = g_variant_new_byte (42);
167 result = g_dbus_proxy_call_sync (proxy,
169 g_variant_new ("(sv)",
172 G_DBUS_CALL_FLAGS_NONE,
176 g_assert_no_error (error);
177 g_assert (result != NULL);
178 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
179 g_variant_unref (result);
180 _g_assert_signal_received (proxy, "g-properties-changed");
181 variant = g_dbus_proxy_get_cached_property (proxy, "y");
182 g_assert (variant != NULL);
183 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
184 g_variant_unref (variant);
186 g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_byte (142));
187 variant = g_dbus_proxy_get_cached_property (proxy, "y");
188 g_assert (variant != NULL);
189 g_assert_cmpint (g_variant_get_byte (variant), ==, 142);
190 g_variant_unref (variant);
192 g_dbus_proxy_set_cached_property (proxy, "y", NULL);
193 variant = g_dbus_proxy_get_cached_property (proxy, "y");
194 g_assert (variant == NULL);
197 /* ---------------------------------------------------------------------------------------------------- */
198 /* Test that the signal aspects of GDBusProxy works */
199 /* ---------------------------------------------------------------------------------------------------- */
202 test_proxy_signals_on_signal (GDBusProxy *proxy,
203 const gchar *sender_name,
204 const gchar *signal_name,
205 GVariant *parameters,
208 GString *s = user_data;
210 g_assert_cmpstr (signal_name, ==, "TestSignal");
211 g_assert_cmpstr (g_variant_get_type_string (parameters), ==, "(sov)");
213 g_variant_print_string (parameters, s, TRUE);
218 GMainLoop *internal_loop;
223 test_proxy_signals_on_emit_signal_cb (GDBusProxy *proxy,
227 TestSignalData *data = user_data;
232 result = g_dbus_proxy_call_finish (proxy,
235 g_assert_no_error (error);
236 g_assert (result != NULL);
237 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
238 g_variant_unref (result);
240 /* check that the signal was recieved before we got the method result */
241 g_assert (strlen (data->s->str) > 0);
243 /* break out of the loop */
244 g_main_loop_quit (data->internal_loop);
248 test_signals (GDBusConnection *connection,
250 const gchar *name_owner,
255 gulong signal_handler_id;
262 * Ask the service to emit a signal and check that we receive it.
264 * Note that blocking calls don't block in the mainloop so wait for the signal (which
265 * is dispatched before the method reply)
267 s = g_string_new (NULL);
268 signal_handler_id = g_signal_connect (proxy,
270 G_CALLBACK (test_proxy_signals_on_signal),
273 result = g_dbus_proxy_call_sync (proxy,
275 g_variant_new ("(so)",
276 "Accept the next proposition you hear",
278 G_DBUS_CALL_FLAGS_NONE,
282 g_assert_no_error (error);
283 g_assert (result != NULL);
284 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
285 g_variant_unref (result);
286 /* check that we haven't received the signal just yet */
287 g_assert (strlen (s->str) == 0);
288 /* and now wait for the signal */
289 _g_assert_signal_received (proxy, "g-signal");
290 g_assert_cmpstr (s->str,
292 "('Accept the next proposition you hear .. in bed!', objectpath '/some/path/in/bed', <'a variant'>)");
293 g_signal_handler_disconnect (proxy, signal_handler_id);
294 g_string_free (s, TRUE);
297 * Now do this async to check the signal is received before the method returns.
299 s = g_string_new (NULL);
300 data.internal_loop = g_main_loop_new (NULL, FALSE);
302 signal_handler_id = g_signal_connect (proxy,
304 G_CALLBACK (test_proxy_signals_on_signal),
306 g_dbus_proxy_call (proxy,
308 g_variant_new ("(so)",
309 "You will make a great programmer",
311 G_DBUS_CALL_FLAGS_NONE,
314 (GAsyncReadyCallback) test_proxy_signals_on_emit_signal_cb,
316 g_main_loop_run (data.internal_loop);
317 g_main_loop_unref (data.internal_loop);
318 g_assert_cmpstr (s->str,
320 "('You will make a great programmer .. in bed!', objectpath '/some/other/path/in/bed', <'a variant'>)");
321 g_signal_handler_disconnect (proxy, signal_handler_id);
322 g_string_free (s, TRUE);
326 test_bogus_method_return (GDBusConnection *connection,
328 const gchar *name_owner,
331 GError *error = NULL;
334 result = g_dbus_proxy_call_sync (proxy,
337 G_DBUS_CALL_FLAGS_NONE,
341 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
342 g_assert (result == NULL);
345 /* ---------------------------------------------------------------------------------------------------- */
347 static const gchar *frob_dbus_interface_xml =
349 " <interface name='com.example.Frob'>"
350 /* Deliberately different from gdbus-testserver.py's definition */
351 " <method name='PairReturn'>"
352 " <arg type='u' name='somenumber' direction='in'/>"
353 " <arg type='s' name='somestring' direction='out'/>"
355 " <method name='HelloWorld'>"
356 " <arg type='s' name='somestring' direction='in'/>"
357 " <arg type='s' name='somestring' direction='out'/>"
359 " <method name='Sleep'>"
360 " <arg type='i' name='timeout' direction='in'/>"
362 " <property name='y' type='y' access='readwrite'/>"
365 static GDBusInterfaceInfo *frob_dbus_interface_info;
368 on_proxy_appeared (GDBusConnection *connection,
370 const gchar *name_owner,
374 test_methods (connection, name, name_owner, proxy);
375 test_properties (connection, name, name_owner, proxy);
376 test_signals (connection, name, name_owner, proxy);
378 /* This is obviously wrong but expected interface is not set so we don't fail... */
379 g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_string ("error_me_out!"));
380 g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_byte (42));
381 g_dbus_proxy_set_cached_property (proxy, "does-not-exist", g_variant_new_string ("something"));
382 g_dbus_proxy_set_cached_property (proxy, "does-not-exist", NULL);
384 /* Now repeat the method tests, with an expected interface set */
385 g_dbus_proxy_set_interface_info (proxy, frob_dbus_interface_info);
386 test_methods (connection, name, name_owner, proxy);
388 /* And now one more test where we deliberately set the expected
389 * interface definition incorrectly
391 test_bogus_method_return (connection, name, name_owner, proxy);
393 /* Also check that we complain if setting a cached property of the wrong type */
394 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
396 g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_string ("error_me_out!"));
398 g_test_trap_assert_stderr ("*Trying to set property y of type s but according to the expected interface the type is y*");
399 g_test_trap_assert_failed();
401 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
403 g_dbus_proxy_set_cached_property (proxy, "does-not-exist", g_variant_new_string ("something"));
405 g_test_trap_assert_stderr ("*Trying to lookup property does-not-exist which isn't in expected interface com.example.Frob*");
406 g_test_trap_assert_failed();
408 /* this should work, however (since the type is correct) */
409 g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_byte (42));
411 g_main_loop_quit (loop);
415 on_proxy_vanished (GDBusConnection *connection,
428 /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
429 * until one can connect to the bus but that's not how things work right now
433 watcher_id = g_bus_watch_proxy (G_BUS_TYPE_SESSION,
434 "com.example.TestService",
435 G_BUS_NAME_WATCHER_FLAGS_NONE,
436 "/com/example/TestObject",
439 G_DBUS_PROXY_FLAGS_NONE,
445 /* this is safe; testserver will exit once the bus goes away */
446 g_assert (g_spawn_command_line_async (SRCDIR "/gdbus-testserver.py", NULL));
448 g_main_loop_run (loop);
450 g_bus_unwatch_proxy (watcher_id);
456 /* ---------------------------------------------------------------------------------------------------- */
463 GDBusNodeInfo *introspection_data = NULL;
466 g_test_init (&argc, &argv, NULL);
468 introspection_data = g_dbus_node_info_new_for_xml (frob_dbus_interface_xml, NULL);
469 g_assert (introspection_data != NULL);
470 frob_dbus_interface_info = introspection_data->interfaces[0];
472 /* all the tests rely on a shared main loop */
473 loop = g_main_loop_new (NULL, FALSE);
475 /* all the tests use a session bus with a well-known address that we can bring up and down
476 * using session_bus_up() and session_bus_down().
478 g_unsetenv ("DISPLAY");
479 g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
481 g_test_add_func ("/gdbus/proxy", test_proxy);
485 g_dbus_node_info_unref (introspection_data);