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);
196 /* Check that the invalidation feature of the PropertiesChanged()
197 * signal works... First, check that we have a cached value of the
198 * property (from the initial GetAll() call)
200 variant = g_dbus_proxy_get_cached_property (proxy, "PropertyThatWillBeInvalidated");
201 g_assert (variant != NULL);
202 g_assert_cmpstr (g_variant_get_string (variant, NULL), ==, "InitialValue");
203 g_variant_unref (variant);
204 /* now ask to invalidate the property - this causes a
206 * PropertiesChanaged("com.example.Frob",
208 * ["PropertyThatWillBeInvalidated")
210 * signal to be emitted. This is received before the method reply
211 * for FrobInvalidateProperty *but* since the proxy was created in
212 * the same thread as we're doing this synchronous call, we'll get
213 * the method reply before...
215 result = g_dbus_proxy_call_sync (proxy,
216 "FrobInvalidateProperty",
218 G_DBUS_CALL_FLAGS_NONE,
222 g_assert_no_error (error);
223 g_assert (result != NULL);
224 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
225 g_variant_unref (result);
226 /* ... hence we wait for the g-properties-changed signal to be delivered */
227 _g_assert_signal_received (proxy, "g-properties-changed");
228 /* ... and now we finally, check that the cached value has been invalidated */
229 variant = g_dbus_proxy_get_cached_property (proxy, "PropertyThatWillBeInvalidated");
230 g_assert (variant == NULL);
233 /* ---------------------------------------------------------------------------------------------------- */
234 /* Test that the signal aspects of GDBusProxy works */
235 /* ---------------------------------------------------------------------------------------------------- */
238 test_proxy_signals_on_signal (GDBusProxy *proxy,
239 const gchar *sender_name,
240 const gchar *signal_name,
241 GVariant *parameters,
244 GString *s = user_data;
246 g_assert_cmpstr (signal_name, ==, "TestSignal");
247 g_assert_cmpstr (g_variant_get_type_string (parameters), ==, "(sov)");
249 g_variant_print_string (parameters, s, TRUE);
254 GMainLoop *internal_loop;
259 test_proxy_signals_on_emit_signal_cb (GDBusProxy *proxy,
263 TestSignalData *data = user_data;
268 result = g_dbus_proxy_call_finish (proxy,
271 g_assert_no_error (error);
272 g_assert (result != NULL);
273 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
274 g_variant_unref (result);
276 /* check that the signal was recieved before we got the method result */
277 g_assert (strlen (data->s->str) > 0);
279 /* break out of the loop */
280 g_main_loop_quit (data->internal_loop);
284 test_signals (GDBusConnection *connection,
286 const gchar *name_owner,
291 gulong signal_handler_id;
298 * Ask the service to emit a signal and check that we receive it.
300 * Note that blocking calls don't block in the mainloop so wait for the signal (which
301 * is dispatched before the method reply)
303 s = g_string_new (NULL);
304 signal_handler_id = g_signal_connect (proxy,
306 G_CALLBACK (test_proxy_signals_on_signal),
309 result = g_dbus_proxy_call_sync (proxy,
311 g_variant_new ("(so)",
312 "Accept the next proposition you hear",
314 G_DBUS_CALL_FLAGS_NONE,
318 g_assert_no_error (error);
319 g_assert (result != NULL);
320 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
321 g_variant_unref (result);
322 /* check that we haven't received the signal just yet */
323 g_assert (strlen (s->str) == 0);
324 /* and now wait for the signal */
325 _g_assert_signal_received (proxy, "g-signal");
326 g_assert_cmpstr (s->str,
328 "('Accept the next proposition you hear .. in bed!', objectpath '/some/path/in/bed', <'a variant'>)");
329 g_signal_handler_disconnect (proxy, signal_handler_id);
330 g_string_free (s, TRUE);
333 * Now do this async to check the signal is received before the method returns.
335 s = g_string_new (NULL);
336 data.internal_loop = g_main_loop_new (NULL, FALSE);
338 signal_handler_id = g_signal_connect (proxy,
340 G_CALLBACK (test_proxy_signals_on_signal),
342 g_dbus_proxy_call (proxy,
344 g_variant_new ("(so)",
345 "You will make a great programmer",
347 G_DBUS_CALL_FLAGS_NONE,
350 (GAsyncReadyCallback) test_proxy_signals_on_emit_signal_cb,
352 g_main_loop_run (data.internal_loop);
353 g_main_loop_unref (data.internal_loop);
354 g_assert_cmpstr (s->str,
356 "('You will make a great programmer .. in bed!', objectpath '/some/other/path/in/bed', <'a variant'>)");
357 g_signal_handler_disconnect (proxy, signal_handler_id);
358 g_string_free (s, TRUE);
362 test_bogus_method_return (GDBusConnection *connection,
364 const gchar *name_owner,
367 GError *error = NULL;
370 result = g_dbus_proxy_call_sync (proxy,
373 G_DBUS_CALL_FLAGS_NONE,
377 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
378 g_assert (result == NULL);
381 /* ---------------------------------------------------------------------------------------------------- */
383 static const gchar *frob_dbus_interface_xml =
385 " <interface name='com.example.Frob'>"
386 /* Deliberately different from gdbus-testserver.py's definition */
387 " <method name='PairReturn'>"
388 " <arg type='u' name='somenumber' direction='in'/>"
389 " <arg type='s' name='somestring' direction='out'/>"
391 " <method name='HelloWorld'>"
392 " <arg type='s' name='somestring' direction='in'/>"
393 " <arg type='s' name='somestring' direction='out'/>"
395 " <method name='Sleep'>"
396 " <arg type='i' name='timeout' direction='in'/>"
398 " <property name='y' type='y' access='readwrite'/>"
401 static GDBusInterfaceInfo *frob_dbus_interface_info;
404 on_proxy_appeared (GDBusConnection *connection,
406 const gchar *name_owner,
410 test_methods (connection, name, name_owner, proxy);
411 test_properties (connection, name, name_owner, proxy);
412 test_signals (connection, name, name_owner, proxy);
414 /* This is obviously wrong but expected interface is not set so we don't fail... */
415 g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_string ("error_me_out!"));
416 g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_byte (42));
417 g_dbus_proxy_set_cached_property (proxy, "does-not-exist", g_variant_new_string ("something"));
418 g_dbus_proxy_set_cached_property (proxy, "does-not-exist", NULL);
420 /* Now repeat the method tests, with an expected interface set */
421 g_dbus_proxy_set_interface_info (proxy, frob_dbus_interface_info);
422 test_methods (connection, name, name_owner, proxy);
424 /* And now one more test where we deliberately set the expected
425 * interface definition incorrectly
427 test_bogus_method_return (connection, name, name_owner, proxy);
429 /* Also check that we complain if setting a cached property of the wrong type */
430 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
432 g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_string ("error_me_out!"));
434 g_test_trap_assert_stderr ("*Trying to set property y of type s but according to the expected interface the type is y*");
435 g_test_trap_assert_failed();
437 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
439 g_dbus_proxy_set_cached_property (proxy, "does-not-exist", g_variant_new_string ("something"));
441 g_test_trap_assert_stderr ("*Trying to lookup property does-not-exist which isn't in expected interface com.example.Frob*");
442 g_test_trap_assert_failed();
444 /* this should work, however (since the type is correct) */
445 g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_byte (42));
447 g_main_loop_quit (loop);
451 on_proxy_vanished (GDBusConnection *connection,
464 /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
465 * until one can connect to the bus but that's not how things work right now
469 watcher_id = g_bus_watch_proxy (G_BUS_TYPE_SESSION,
470 "com.example.TestService",
471 G_BUS_NAME_WATCHER_FLAGS_NONE,
472 "/com/example/TestObject",
475 G_DBUS_PROXY_FLAGS_NONE,
481 /* this is safe; testserver will exit once the bus goes away */
482 g_assert (g_spawn_command_line_async (SRCDIR "/gdbus-testserver.py", NULL));
484 g_main_loop_run (loop);
486 g_bus_unwatch_proxy (watcher_id);
492 /* ---------------------------------------------------------------------------------------------------- */
499 GDBusNodeInfo *introspection_data = NULL;
502 g_test_init (&argc, &argv, NULL);
504 introspection_data = g_dbus_node_info_new_for_xml (frob_dbus_interface_xml, NULL);
505 g_assert (introspection_data != NULL);
506 frob_dbus_interface_info = introspection_data->interfaces[0];
508 /* all the tests rely on a shared main loop */
509 loop = g_main_loop_new (NULL, FALSE);
511 /* all the tests use a session bus with a well-known address that we can bring up and down
512 * using session_bus_up() and session_bus_down().
514 g_unsetenv ("DISPLAY");
515 g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
517 g_test_add_func ("/gdbus/proxy", test_proxy);
521 g_dbus_node_info_unref (introspection_data);