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 (GDBusProxy *proxy)
42 gchar *dbus_error_name;
44 /* check that we can invoke a method */
46 result = g_dbus_proxy_call_sync (proxy,
48 g_variant_new ("(s)", "Hey"),
49 G_DBUS_CALL_FLAGS_NONE,
53 g_assert_no_error (error);
54 g_assert (result != NULL);
55 g_assert_cmpstr (g_variant_get_type_string (result), ==, "(s)");
56 g_variant_get (result, "(&s)", &str);
57 g_assert_cmpstr (str, ==, "You greeted me with 'Hey'. Thanks!");
58 g_variant_unref (result);
60 /* Check that we can completely recover the returned error */
61 result = g_dbus_proxy_call_sync (proxy,
63 g_variant_new ("(s)", "Yo"),
64 G_DBUS_CALL_FLAGS_NONE,
68 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
69 g_assert (g_dbus_error_is_remote_error (error));
70 g_assert (g_dbus_error_is_remote_error (error));
71 g_assert (result == NULL);
72 dbus_error_name = g_dbus_error_get_remote_error (error);
73 g_assert_cmpstr (dbus_error_name, ==, "com.example.TestException");
74 g_free (dbus_error_name);
75 g_assert (g_dbus_error_strip_remote_error (error));
76 g_assert_cmpstr (error->message, ==, "Yo is not a proper greeting");
77 g_clear_error (&error);
79 /* Check that we get a timeout if the method handling is taking longer than timeout */
81 result = g_dbus_proxy_call_sync (proxy,
83 g_variant_new ("(i)", 500 /* msec */),
84 G_DBUS_CALL_FLAGS_NONE,
88 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
89 g_assert (!g_dbus_error_is_remote_error (error));
90 g_assert (result == NULL);
91 g_clear_error (&error);
93 /* Check that proxy-default timeouts work. */
94 g_assert_cmpint (g_dbus_proxy_get_default_timeout (proxy), ==, -1);
96 /* the default timeout is 25000 msec so this should work */
97 result = g_dbus_proxy_call_sync (proxy,
99 g_variant_new ("(i)", 500 /* msec */),
100 G_DBUS_CALL_FLAGS_NONE,
101 -1, /* use proxy default (e.g. -1 -> e.g. 25000 msec) */
104 g_assert_no_error (error);
105 g_assert (result != NULL);
106 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
107 g_variant_unref (result);
109 /* now set the proxy-default timeout to 250 msec and try the 500 msec call - this should FAIL */
110 g_dbus_proxy_set_default_timeout (proxy, 250);
111 g_assert_cmpint (g_dbus_proxy_get_default_timeout (proxy), ==, 250);
112 result = g_dbus_proxy_call_sync (proxy,
114 g_variant_new ("(i)", 500 /* msec */),
115 G_DBUS_CALL_FLAGS_NONE,
116 -1, /* use proxy default (e.g. 250 msec) */
119 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
120 g_assert (!g_dbus_error_is_remote_error (error));
121 g_assert (result == NULL);
122 g_clear_error (&error);
124 /* clean up after ourselves */
125 g_dbus_proxy_set_default_timeout (proxy, -1);
128 /* ---------------------------------------------------------------------------------------------------- */
129 /* Test that the property aspects of GDBusProxy works */
130 /* ---------------------------------------------------------------------------------------------------- */
133 test_properties (GDBusProxy *proxy)
143 * Check that we can read cached properties.
145 * No need to test all properties - GVariant has already been tested
147 variant = g_dbus_proxy_get_cached_property (proxy, "y");
148 g_assert (variant != NULL);
149 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
150 g_variant_unref (variant);
151 variant = g_dbus_proxy_get_cached_property (proxy, "o");
152 g_assert (variant != NULL);
153 g_assert_cmpstr (g_variant_get_string (variant, NULL), ==, "/some/path");
154 g_variant_unref (variant);
157 * Now ask the service to change a property and check that #GDBusProxy::g-property-changed
158 * is received. Also check that the cache is updated.
160 variant2 = g_variant_new_byte (42);
161 result = g_dbus_proxy_call_sync (proxy,
163 g_variant_new ("(sv)",
166 G_DBUS_CALL_FLAGS_NONE,
170 g_assert_no_error (error);
171 g_assert (result != NULL);
172 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
173 g_variant_unref (result);
174 _g_assert_signal_received (proxy, "g-properties-changed");
175 variant = g_dbus_proxy_get_cached_property (proxy, "y");
176 g_assert (variant != NULL);
177 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
178 g_variant_unref (variant);
180 g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_byte (142));
181 variant = g_dbus_proxy_get_cached_property (proxy, "y");
182 g_assert (variant != NULL);
183 g_assert_cmpint (g_variant_get_byte (variant), ==, 142);
184 g_variant_unref (variant);
186 g_dbus_proxy_set_cached_property (proxy, "y", NULL);
187 variant = g_dbus_proxy_get_cached_property (proxy, "y");
188 g_assert (variant == NULL);
190 /* Check that the invalidation feature of the PropertiesChanged()
191 * signal works... First, check that we have a cached value of the
192 * property (from the initial GetAll() call)
194 variant = g_dbus_proxy_get_cached_property (proxy, "PropertyThatWillBeInvalidated");
195 g_assert (variant != NULL);
196 g_assert_cmpstr (g_variant_get_string (variant, NULL), ==, "InitialValue");
197 g_variant_unref (variant);
198 /* now ask to invalidate the property - this causes a
200 * PropertiesChanaged("com.example.Frob",
202 * ["PropertyThatWillBeInvalidated")
204 * signal to be emitted. This is received before the method reply
205 * for FrobInvalidateProperty *but* since the proxy was created in
206 * the same thread as we're doing this synchronous call, we'll get
207 * the method reply before...
209 result = g_dbus_proxy_call_sync (proxy,
210 "FrobInvalidateProperty",
212 G_DBUS_CALL_FLAGS_NONE,
216 g_assert_no_error (error);
217 g_assert (result != NULL);
218 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
219 g_variant_unref (result);
220 /* ... hence we wait for the g-properties-changed signal to be delivered */
221 _g_assert_signal_received (proxy, "g-properties-changed");
222 /* ... and now we finally, check that the cached value has been invalidated */
223 variant = g_dbus_proxy_get_cached_property (proxy, "PropertyThatWillBeInvalidated");
224 g_assert (variant == NULL);
227 /* ---------------------------------------------------------------------------------------------------- */
228 /* Test that the signal aspects of GDBusProxy works */
229 /* ---------------------------------------------------------------------------------------------------- */
232 test_proxy_signals_on_signal (GDBusProxy *proxy,
233 const gchar *sender_name,
234 const gchar *signal_name,
235 GVariant *parameters,
238 GString *s = user_data;
240 g_assert_cmpstr (signal_name, ==, "TestSignal");
241 g_assert_cmpstr (g_variant_get_type_string (parameters), ==, "(sov)");
243 g_variant_print_string (parameters, s, TRUE);
248 GMainLoop *internal_loop;
253 test_proxy_signals_on_emit_signal_cb (GDBusProxy *proxy,
257 TestSignalData *data = user_data;
262 result = g_dbus_proxy_call_finish (proxy,
265 g_assert_no_error (error);
266 g_assert (result != NULL);
267 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
268 g_variant_unref (result);
270 /* check that the signal was recieved before we got the method result */
271 g_assert (strlen (data->s->str) > 0);
273 /* break out of the loop */
274 g_main_loop_quit (data->internal_loop);
278 test_signals (GDBusProxy *proxy)
282 gulong signal_handler_id;
289 * Ask the service to emit a signal and check that we receive it.
291 * Note that blocking calls don't block in the mainloop so wait for the signal (which
292 * is dispatched before the method reply)
294 s = g_string_new (NULL);
295 signal_handler_id = g_signal_connect (proxy,
297 G_CALLBACK (test_proxy_signals_on_signal),
300 result = g_dbus_proxy_call_sync (proxy,
302 g_variant_new ("(so)",
303 "Accept the next proposition you hear",
305 G_DBUS_CALL_FLAGS_NONE,
309 g_assert_no_error (error);
310 g_assert (result != NULL);
311 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
312 g_variant_unref (result);
313 /* check that we haven't received the signal just yet */
314 g_assert (strlen (s->str) == 0);
315 /* and now wait for the signal */
316 _g_assert_signal_received (proxy, "g-signal");
317 g_assert_cmpstr (s->str,
319 "('Accept the next proposition you hear .. in bed!', objectpath '/some/path/in/bed', <'a variant'>)");
320 g_signal_handler_disconnect (proxy, signal_handler_id);
321 g_string_free (s, TRUE);
324 * Now do this async to check the signal is received before the method returns.
326 s = g_string_new (NULL);
327 data.internal_loop = g_main_loop_new (NULL, FALSE);
329 signal_handler_id = g_signal_connect (proxy,
331 G_CALLBACK (test_proxy_signals_on_signal),
333 g_dbus_proxy_call (proxy,
335 g_variant_new ("(so)",
336 "You will make a great programmer",
338 G_DBUS_CALL_FLAGS_NONE,
341 (GAsyncReadyCallback) test_proxy_signals_on_emit_signal_cb,
343 g_main_loop_run (data.internal_loop);
344 g_main_loop_unref (data.internal_loop);
345 g_assert_cmpstr (s->str,
347 "('You will make a great programmer .. in bed!', objectpath '/some/other/path/in/bed', <'a variant'>)");
348 g_signal_handler_disconnect (proxy, signal_handler_id);
349 g_string_free (s, TRUE);
353 test_bogus_method_return (GDBusProxy *proxy)
355 GError *error = NULL;
358 result = g_dbus_proxy_call_sync (proxy,
361 G_DBUS_CALL_FLAGS_NONE,
365 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
366 g_assert (result == NULL);
369 /* ---------------------------------------------------------------------------------------------------- */
371 static const gchar *frob_dbus_interface_xml =
373 " <interface name='com.example.Frob'>"
374 /* Deliberately different from gdbus-testserver.py's definition */
375 " <method name='PairReturn'>"
376 " <arg type='u' name='somenumber' direction='in'/>"
377 " <arg type='s' name='somestring' direction='out'/>"
379 " <method name='HelloWorld'>"
380 " <arg type='s' name='somestring' direction='in'/>"
381 " <arg type='s' name='somestring' direction='out'/>"
383 " <method name='Sleep'>"
384 " <arg type='i' name='timeout' direction='in'/>"
386 " <property name='y' type='y' access='readwrite'/>"
389 static GDBusInterfaceInfo *frob_dbus_interface_info;
392 test_expected_interface (GDBusProxy *proxy)
394 /* This is obviously wrong but expected interface is not set so we don't fail... */
395 g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_string ("error_me_out!"));
396 g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_byte (42));
397 g_dbus_proxy_set_cached_property (proxy, "does-not-exist", g_variant_new_string ("something"));
398 g_dbus_proxy_set_cached_property (proxy, "does-not-exist", NULL);
400 /* Now repeat the method tests, with an expected interface set */
401 g_dbus_proxy_set_interface_info (proxy, frob_dbus_interface_info);
402 test_methods (proxy);
404 /* And now one more test where we deliberately set the expected
405 * interface definition incorrectly
407 test_bogus_method_return (proxy);
409 /* Also check that we complain if setting a cached property of the wrong type */
410 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
412 g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_string ("error_me_out!"));
414 g_test_trap_assert_stderr ("*Trying to set property y of type s but according to the expected interface the type is y*");
415 g_test_trap_assert_failed();
417 if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
419 g_dbus_proxy_set_cached_property (proxy, "does-not-exist", g_variant_new_string ("something"));
421 g_test_trap_assert_stderr ("*Trying to lookup property does-not-exist which isn't in expected interface com.example.Frob*");
422 g_test_trap_assert_failed();
424 /* this should work, however (since the type is correct) */
425 g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_byte (42));
432 GDBusConnection *connection;
437 /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
438 * until one can connect to the bus but that's not how things work right now
443 connection = g_bus_get_sync (G_BUS_TYPE_SESSION,
446 g_assert_no_error (error);
448 proxy = g_dbus_proxy_new_sync (connection,
449 G_DBUS_PROXY_FLAGS_NONE,
450 NULL, /* GDBusInterfaceInfo */
451 "com.example.TestService", /* name */
452 "/com/example/TestObject", /* object path */
453 "com.example.Frob", /* interface */
454 NULL, /* GCancellable */
456 g_assert_no_error (error);
458 /* this is safe; testserver will exit once the bus goes away */
459 g_assert (g_spawn_command_line_async (SRCDIR "/gdbus-testserver.py", NULL));
461 _g_assert_property_notify (proxy, "g-name-owner");
463 test_methods (proxy);
464 test_properties (proxy);
465 test_signals (proxy);
466 test_expected_interface (proxy);
468 g_object_unref (proxy);
469 g_object_unref (connection);
472 /* ---------------------------------------------------------------------------------------------------- */
479 GDBusNodeInfo *introspection_data = NULL;
482 g_test_init (&argc, &argv, NULL);
484 introspection_data = g_dbus_node_info_new_for_xml (frob_dbus_interface_xml, NULL);
485 g_assert (introspection_data != NULL);
486 frob_dbus_interface_info = introspection_data->interfaces[0];
488 /* all the tests rely on a shared main loop */
489 loop = g_main_loop_new (NULL, FALSE);
491 /* all the tests use a session bus with a well-known address that we can bring up and down
492 * using session_bus_up() and session_bus_down().
494 g_unsetenv ("DISPLAY");
495 g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
497 g_test_add_func ("/gdbus/proxy", test_proxy);
501 g_dbus_node_info_unref (introspection_data);