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 static GDBusConnection *c = NULL;
34 /* ---------------------------------------------------------------------------------------------------- */
35 /* Test that we can export objects, the hierarchy is correct and the right handlers are invoked */
36 /* ---------------------------------------------------------------------------------------------------- */
38 static const GDBusArgInfo foo_method1_in_args =
45 static const GDBusArgInfo * const foo_method1_in_arg_pointers[] = {&foo_method1_in_args, NULL};
47 static const GDBusArgInfo foo_method1_out_args =
54 static const GDBusArgInfo * const foo_method1_out_arg_pointers[] = {&foo_method1_out_args, NULL};
56 static const GDBusMethodInfo foo_method_info_method1 =
60 (GDBusArgInfo **) &foo_method1_in_arg_pointers,
61 (GDBusArgInfo **) &foo_method1_out_arg_pointers,
64 static const GDBusMethodInfo foo_method_info_method2 =
72 static const GDBusMethodInfo * const foo_method_info_pointers[] = {&foo_method_info_method1, &foo_method_info_method2, NULL};
74 static const GDBusSignalInfo foo_signal_info =
81 static const GDBusSignalInfo * const foo_signal_info_pointers[] = {&foo_signal_info, NULL};
83 static const GDBusPropertyInfo foo_property_info[] =
89 G_DBUS_PROPERTY_INFO_FLAGS_READABLE | G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE,
96 G_DBUS_PROPERTY_INFO_FLAGS_READABLE,
103 G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE,
107 static const GDBusPropertyInfo * const foo_property_info_pointers[] =
109 &foo_property_info[0],
110 &foo_property_info[1],
111 &foo_property_info[2],
115 static const GDBusInterfaceInfo foo_interface_info =
119 (GDBusMethodInfo **) &foo_method_info_pointers,
120 (GDBusSignalInfo **) &foo_signal_info_pointers,
121 (GDBusPropertyInfo **)&foo_property_info_pointers,
125 /* Foo2 is just Foo without the properties */
126 static const GDBusInterfaceInfo foo2_interface_info =
130 (GDBusMethodInfo **) &foo_method_info_pointers,
131 (GDBusSignalInfo **) &foo_signal_info_pointers,
135 foo_method_call (GDBusConnection *connection,
137 const gchar *object_path,
138 const gchar *interface_name,
139 const gchar *method_name,
140 GVariant *parameters,
141 GDBusMethodInvocation *invocation,
144 if (g_strcmp0 (method_name, "Method1") == 0)
148 g_variant_get (parameters, "(&s)", &input);
149 output = g_strdup_printf ("You passed the string '%s'. Jolly good!", input);
150 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", output));
155 g_dbus_method_invocation_return_dbus_error (invocation,
156 "org.example.SomeError",
157 "How do you like them apples, buddy!");
162 foo_get_property (GDBusConnection *connection,
164 const gchar *object_path,
165 const gchar *interface_name,
166 const gchar *property_name,
172 s = g_strdup_printf ("Property '%s' Is What It Is!", property_name);
173 ret = g_variant_new_string (s);
179 foo_set_property (GDBusConnection *connection,
181 const gchar *object_path,
182 const gchar *interface_name,
183 const gchar *property_name,
189 s = g_variant_print (value, TRUE);
192 G_DBUS_ERROR_SPAWN_FILE_INVALID,
193 "Returning some error instead of writing the value '%s' to the property '%s'",
199 static const GDBusInterfaceVTable foo_vtable =
206 /* -------------------- */
208 static const GDBusMethodInfo bar_method_info[] =
225 static const GDBusMethodInfo * const bar_method_info_pointers[] = {&bar_method_info[0], &bar_method_info[1], NULL};
227 static const GDBusSignalInfo bar_signal_info[] =
236 static const GDBusSignalInfo * const bar_signal_info_pointers[] = {&bar_signal_info[0], NULL};
238 static const GDBusPropertyInfo bar_property_info[] =
244 G_DBUS_PROPERTY_INFO_FLAGS_READABLE,
248 static const GDBusPropertyInfo * const bar_property_info_pointers[] = {&bar_property_info[0], NULL};
250 static const GDBusInterfaceInfo bar_interface_info =
254 (GDBusMethodInfo **) bar_method_info_pointers,
255 (GDBusSignalInfo **) bar_signal_info_pointers,
256 (GDBusPropertyInfo **) bar_property_info_pointers,
260 /* -------------------- */
262 static const GDBusMethodInfo dyna_method_info[] =
272 static const GDBusMethodInfo * const dyna_method_info_pointers[] = {&dyna_method_info[0], NULL};
274 static const GDBusInterfaceInfo dyna_interface_info =
278 (GDBusMethodInfo **) dyna_method_info_pointers,
279 NULL, /* no signals */
280 NULL, /* no properties */
285 dyna_cyber (GDBusConnection *connection,
287 const gchar *object_path,
288 const gchar *interface_name,
289 const gchar *method_name,
290 GVariant *parameters,
291 GDBusMethodInvocation *invocation,
294 GPtrArray *data = user_data;
298 node_name = strrchr (object_path, '/') + 1;
300 /* Add new node if it is not already known */
301 for (n = 0; n < data->len ; n++)
303 if (g_strcmp0 (g_ptr_array_index (data, n), node_name) == 0)
306 g_ptr_array_add (data, g_strdup(node_name));
309 g_dbus_method_invocation_return_value (invocation, NULL);
312 static const GDBusInterfaceVTable dyna_interface_vtable =
319 /* ---------------------------------------------------------------------------------------------------- */
322 introspect_callback (GDBusProxy *proxy,
326 gchar **xml_data = user_data;
331 result = g_dbus_proxy_call_finish (proxy,
334 g_assert_no_error (error);
335 g_assert (result != NULL);
336 g_variant_get (result, "(s)", xml_data);
337 g_variant_unref (result);
339 g_main_loop_quit (loop);
343 get_nodes_at (GDBusConnection *c,
344 const gchar *object_path)
350 GDBusNodeInfo *node_info;
354 proxy = g_dbus_proxy_new_sync (c,
355 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
356 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
358 g_dbus_connection_get_unique_name (c),
360 "org.freedesktop.DBus.Introspectable",
363 g_assert_no_error (error);
364 g_assert (proxy != NULL);
366 /* do this async to avoid libdbus-1 deadlocks */
368 g_dbus_proxy_call (proxy,
371 G_DBUS_CALL_FLAGS_NONE,
374 (GAsyncReadyCallback) introspect_callback,
376 g_main_loop_run (loop);
377 g_assert (xml_data != NULL);
379 node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
380 g_assert_no_error (error);
381 g_assert (node_info != NULL);
383 p = g_ptr_array_new ();
384 for (n = 0; node_info->nodes != NULL && node_info->nodes[n] != NULL; n++)
386 const GDBusNodeInfo *sub_node_info = node_info->nodes[n];
387 g_ptr_array_add (p, g_strdup (sub_node_info->path));
389 g_ptr_array_add (p, NULL);
391 g_object_unref (proxy);
393 g_dbus_node_info_unref (node_info);
395 return (gchar **) g_ptr_array_free (p, FALSE);
399 has_interface (GDBusConnection *c,
400 const gchar *object_path,
401 const gchar *interface_name)
406 GDBusNodeInfo *node_info;
410 proxy = g_dbus_proxy_new_sync (c,
411 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
412 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
414 g_dbus_connection_get_unique_name (c),
416 "org.freedesktop.DBus.Introspectable",
419 g_assert_no_error (error);
420 g_assert (proxy != NULL);
422 /* do this async to avoid libdbus-1 deadlocks */
424 g_dbus_proxy_call (proxy,
427 G_DBUS_CALL_FLAGS_NONE,
430 (GAsyncReadyCallback) introspect_callback,
432 g_main_loop_run (loop);
433 g_assert (xml_data != NULL);
435 node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
436 g_assert_no_error (error);
437 g_assert (node_info != NULL);
439 ret = (g_dbus_node_info_lookup_interface (node_info, interface_name) != NULL);
441 g_object_unref (proxy);
443 g_dbus_node_info_unref (node_info);
449 count_interfaces (GDBusConnection *c,
450 const gchar *object_path)
455 GDBusNodeInfo *node_info;
459 proxy = g_dbus_proxy_new_sync (c,
460 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
461 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
463 g_dbus_connection_get_unique_name (c),
465 "org.freedesktop.DBus.Introspectable",
468 g_assert_no_error (error);
469 g_assert (proxy != NULL);
471 /* do this async to avoid libdbus-1 deadlocks */
473 g_dbus_proxy_call (proxy,
476 G_DBUS_CALL_FLAGS_NONE,
479 (GAsyncReadyCallback) introspect_callback,
481 g_main_loop_run (loop);
482 g_assert (xml_data != NULL);
484 node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
485 g_assert_no_error (error);
486 g_assert (node_info != NULL);
489 while (node_info->interfaces != NULL && node_info->interfaces[ret] != NULL)
492 g_object_unref (proxy);
494 g_dbus_node_info_unref (node_info);
500 dyna_create_callback (GDBusProxy *proxy,
508 result = g_dbus_proxy_call_finish (proxy,
511 g_assert_no_error (error);
512 g_assert (result != NULL);
513 g_variant_unref (result);
515 g_main_loop_quit (loop);
518 /* Dynamically create @object_name under /foo/dyna */
520 dyna_create (GDBusConnection *c,
521 const gchar *object_name)
527 object_path = g_strconcat ("/foo/dyna/", object_name, NULL);
530 proxy = g_dbus_proxy_new_sync (c,
531 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
532 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
534 g_dbus_connection_get_unique_name (c),
539 g_assert_no_error (error);
540 g_assert (proxy != NULL);
542 /* do this async to avoid libdbus-1 deadlocks */
543 g_dbus_proxy_call (proxy,
545 g_variant_new ("()"),
546 G_DBUS_CALL_FLAGS_NONE,
549 (GAsyncReadyCallback) dyna_create_callback,
551 g_main_loop_run (loop);
553 g_assert_no_error (error);
555 g_object_unref (proxy);
556 g_free (object_path);
563 guint num_unregistered_calls;
564 guint num_unregistered_subtree_calls;
565 guint num_subtree_nodes;
566 } ObjectRegistrationData;
569 on_object_unregistered (gpointer user_data)
571 ObjectRegistrationData *data = user_data;
573 data->num_unregistered_calls++;
577 on_subtree_unregistered (gpointer user_data)
579 ObjectRegistrationData *data = user_data;
581 data->num_unregistered_subtree_calls++;
585 _g_strv_has_string (const gchar* const * haystack,
590 for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
592 if (g_strcmp0 (haystack[n], needle) == 0)
598 /* -------------------- */
601 subtree_enumerate (GDBusConnection *connection,
603 const gchar *object_path,
606 ObjectRegistrationData *data = user_data;
611 p = g_ptr_array_new ();
613 for (n = 0; n < data->num_subtree_nodes; n++)
615 g_ptr_array_add (p, g_strdup_printf ("vp%d", n));
616 g_ptr_array_add (p, g_strdup_printf ("evp%d", n));
618 g_ptr_array_add (p, NULL);
620 nodes = (gchar **) g_ptr_array_free (p, FALSE);
625 /* Only allows certain objects, and aborts on unknowns */
626 static GDBusInterfaceInfo **
627 subtree_introspect (GDBusConnection *connection,
629 const gchar *object_path,
633 const GDBusInterfaceInfo *interfaces[2] = {
634 NULL /* filled in below */, NULL
637 /* VPs implement the Foo interface, EVPs implement the Bar interface. The root
638 * does not implement any interfaces
644 else if (g_str_has_prefix (node, "vp"))
646 interfaces[0] = &foo_interface_info;
648 else if (g_str_has_prefix (node, "evp"))
650 interfaces[0] = &bar_interface_info;
654 g_assert_not_reached ();
657 return g_memdup (interfaces, 2 * sizeof (void *));
660 static const GDBusInterfaceVTable *
661 subtree_dispatch (GDBusConnection *connection,
663 const gchar *object_path,
664 const gchar *interface_name,
666 gpointer *out_user_data,
669 if (g_strcmp0 (interface_name, "org.example.Foo") == 0)
675 static const GDBusSubtreeVTable subtree_vtable =
682 /* -------------------- */
685 dynamic_subtree_enumerate (GDBusConnection *connection,
687 const gchar *object_path,
690 GPtrArray *data = user_data;
691 gchar **nodes = g_new (gchar*, data->len + 1);
694 for (n = 0; n < data->len; n++)
696 nodes[n] = g_strdup (g_ptr_array_index (data, n));
698 nodes[data->len] = NULL;
703 /* Allow all objects to be introspected */
704 static GDBusInterfaceInfo **
705 dynamic_subtree_introspect (GDBusConnection *connection,
707 const gchar *object_path,
711 const GDBusInterfaceInfo *interfaces[2] = { &dyna_interface_info, NULL };
713 return g_memdup (interfaces, 2 * sizeof (void *));
716 static const GDBusInterfaceVTable *
717 dynamic_subtree_dispatch (GDBusConnection *connection,
719 const gchar *object_path,
720 const gchar *interface_name,
722 gpointer *out_user_data,
725 *out_user_data = user_data;
726 return &dyna_interface_vtable;
729 static const GDBusSubtreeVTable dynamic_subtree_vtable =
731 dynamic_subtree_enumerate,
732 dynamic_subtree_introspect,
733 dynamic_subtree_dispatch
736 /* -------------------- */
739 test_dispatch_thread_func (gpointer user_data)
741 const gchar *object_path = user_data;
742 GDBusProxy *foo_proxy;
747 const gchar *value_str;
749 foo_proxy = g_dbus_proxy_new_sync (c,
750 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS |
751 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
753 g_dbus_connection_get_unique_name (c),
758 g_assert (foo_proxy != NULL);
760 /* generic interfaces */
762 value = g_dbus_proxy_call_sync (foo_proxy,
763 "org.freedesktop.DBus.Peer.Ping",
765 G_DBUS_CALL_FLAGS_NONE,
769 g_assert_no_error (error);
770 g_assert (value != NULL);
771 g_variant_unref (value);
775 value = g_dbus_proxy_call_sync (foo_proxy,
777 g_variant_new ("(s)", "winwinwin"),
778 G_DBUS_CALL_FLAGS_NONE,
782 g_assert_no_error (error);
783 g_assert (value != NULL);
784 g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("(s)")));
785 g_variant_get (value, "(&s)", &value_str);
786 g_assert_cmpstr (value_str, ==, "You passed the string 'winwinwin'. Jolly good!");
787 g_variant_unref (value);
790 value = g_dbus_proxy_call_sync (foo_proxy,
793 G_DBUS_CALL_FLAGS_NONE,
797 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
798 g_assert_cmpstr (error->message, ==, "GDBus.Error:org.example.SomeError: How do you like them apples, buddy!");
799 g_error_free (error);
800 g_assert (value == NULL);
803 value = g_dbus_proxy_call_sync (foo_proxy,
805 g_variant_new ("(s)", "failfailfail"),
806 G_DBUS_CALL_FLAGS_NONE,
810 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
811 g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Type of message, '(s)', does not match expected type '()'");
812 g_error_free (error);
813 g_assert (value == NULL);
816 value = g_dbus_proxy_call_sync (foo_proxy,
819 G_DBUS_CALL_FLAGS_NONE,
823 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
824 g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod: No such method 'NonExistantMethod'");
825 g_error_free (error);
826 g_assert (value == NULL);
829 value = g_dbus_proxy_call_sync (foo_proxy,
830 "org.example.FooXYZ.NonExistant",
832 G_DBUS_CALL_FLAGS_NONE,
836 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
837 g_error_free (error);
838 g_assert (value == NULL);
840 /* user properties */
842 value = g_dbus_proxy_call_sync (foo_proxy,
843 "org.freedesktop.DBus.Properties.Get",
844 g_variant_new ("(ss)",
847 G_DBUS_CALL_FLAGS_NONE,
851 g_assert_no_error (error);
852 g_assert (value != NULL);
853 g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("(v)")));
854 g_variant_get (value, "(v)", &inner);
855 g_assert (g_variant_is_of_type (inner, G_VARIANT_TYPE_STRING));
856 g_assert_cmpstr (g_variant_get_string (inner, NULL), ==, "Property 'PropertyUno' Is What It Is!");
857 g_variant_unref (value);
858 g_variant_unref (inner);
861 value = g_dbus_proxy_call_sync (foo_proxy,
862 "org.freedesktop.DBus.Properties.Get",
863 g_variant_new ("(ss)",
866 G_DBUS_CALL_FLAGS_NONE,
870 g_assert (value == NULL);
871 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
872 g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: No such property 'ThisDoesntExist'");
873 g_error_free (error);
876 value = g_dbus_proxy_call_sync (foo_proxy,
877 "org.freedesktop.DBus.Properties.Get",
878 g_variant_new ("(ss)",
881 G_DBUS_CALL_FLAGS_NONE,
885 g_assert (value == NULL);
886 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
887 g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Property 'NotReadable' is not readable");
888 g_error_free (error);
891 value = g_dbus_proxy_call_sync (foo_proxy,
892 "org.freedesktop.DBus.Properties.Set",
893 g_variant_new ("(ssv)",
896 g_variant_new_string ("But Writable you are!")),
897 G_DBUS_CALL_FLAGS_NONE,
901 g_assert (value == NULL);
902 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_SPAWN_FILE_INVALID);
903 g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.Spawn.FileInvalid: Returning some error instead of writing the value 'NotReadable' to the property ''But Writable you are!''");
904 g_error_free (error);
907 value = g_dbus_proxy_call_sync (foo_proxy,
908 "org.freedesktop.DBus.Properties.Set",
909 g_variant_new ("(ssv)",
912 g_variant_new_uint32 (42)),
913 G_DBUS_CALL_FLAGS_NONE,
917 g_assert (value == NULL);
918 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
919 g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Property 'NotWritable' is not writable");
920 g_error_free (error);
923 value = g_dbus_proxy_call_sync (foo_proxy,
924 "org.freedesktop.DBus.Properties.GetAll",
925 g_variant_new ("(s)",
927 G_DBUS_CALL_FLAGS_NONE,
931 g_assert_no_error (error);
932 g_assert (value != NULL);
933 g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("(a{sv})")));
934 s = g_variant_print (value, TRUE);
935 g_assert_cmpstr (s, ==, "({'PropertyUno': <\"Property 'PropertyUno' Is What It Is!\">, 'NotWritable': <\"Property 'NotWritable' Is What It Is!\">},)");
937 g_variant_unref (value);
939 g_object_unref (foo_proxy);
941 g_main_loop_quit (loop);
946 test_dispatch (const gchar *object_path)
950 /* run this in a thread to avoid deadlocks */
951 thread = g_thread_new ("test_dispatch",
952 test_dispatch_thread_func,
953 (gpointer) object_path);
954 g_main_loop_run (loop);
955 g_thread_join (thread);
959 test_object_registration (void)
962 ObjectRegistrationData data;
963 GPtrArray *dyna_data;
965 guint boss_foo_reg_id;
966 guint boss_bar_reg_id;
967 guint worker1_foo_reg_id;
968 guint worker1p1_foo_reg_id;
969 guint worker2_bar_reg_id;
970 guint intern1_foo_reg_id;
971 guint intern2_bar_reg_id;
972 guint intern2_foo_reg_id;
973 guint intern3_bar_reg_id;
974 guint registration_id;
975 guint subtree_registration_id;
976 guint non_subtree_object_path_foo_reg_id;
977 guint non_subtree_object_path_bar_reg_id;
978 guint dyna_subtree_registration_id;
979 guint num_successful_registrations;
981 data.num_unregistered_calls = 0;
982 data.num_unregistered_subtree_calls = 0;
983 data.num_subtree_nodes = 0;
985 num_successful_registrations = 0;
988 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
989 g_assert_no_error (error);
990 g_assert (c != NULL);
992 registration_id = g_dbus_connection_register_object (c,
994 (GDBusInterfaceInfo *) &foo_interface_info,
997 on_object_unregistered,
999 g_assert_no_error (error);
1000 g_assert (registration_id > 0);
1001 boss_foo_reg_id = registration_id;
1002 num_successful_registrations++;
1004 registration_id = g_dbus_connection_register_object (c,
1006 (GDBusInterfaceInfo *) &bar_interface_info,
1009 on_object_unregistered,
1011 g_assert_no_error (error);
1012 g_assert (registration_id > 0);
1013 boss_bar_reg_id = registration_id;
1014 num_successful_registrations++;
1016 registration_id = g_dbus_connection_register_object (c,
1017 "/foo/boss/worker1",
1018 (GDBusInterfaceInfo *) &foo_interface_info,
1021 on_object_unregistered,
1023 g_assert_no_error (error);
1024 g_assert (registration_id > 0);
1025 worker1_foo_reg_id = registration_id;
1026 num_successful_registrations++;
1028 registration_id = g_dbus_connection_register_object (c,
1029 "/foo/boss/worker1p1",
1030 (GDBusInterfaceInfo *) &foo_interface_info,
1033 on_object_unregistered,
1035 g_assert_no_error (error);
1036 g_assert (registration_id > 0);
1037 worker1p1_foo_reg_id = registration_id;
1038 num_successful_registrations++;
1040 registration_id = g_dbus_connection_register_object (c,
1041 "/foo/boss/worker2",
1042 (GDBusInterfaceInfo *) &bar_interface_info,
1045 on_object_unregistered,
1047 g_assert_no_error (error);
1048 g_assert (registration_id > 0);
1049 worker2_bar_reg_id = registration_id;
1050 num_successful_registrations++;
1052 registration_id = g_dbus_connection_register_object (c,
1053 "/foo/boss/interns/intern1",
1054 (GDBusInterfaceInfo *) &foo_interface_info,
1057 on_object_unregistered,
1059 g_assert_no_error (error);
1060 g_assert (registration_id > 0);
1061 intern1_foo_reg_id = registration_id;
1062 num_successful_registrations++;
1064 /* ... and try again at another path */
1065 registration_id = g_dbus_connection_register_object (c,
1066 "/foo/boss/interns/intern2",
1067 (GDBusInterfaceInfo *) &bar_interface_info,
1070 on_object_unregistered,
1072 g_assert_no_error (error);
1073 g_assert (registration_id > 0);
1074 intern2_bar_reg_id = registration_id;
1075 num_successful_registrations++;
1077 /* register at the same path/interface - this should fail */
1078 registration_id = g_dbus_connection_register_object (c,
1079 "/foo/boss/interns/intern2",
1080 (GDBusInterfaceInfo *) &bar_interface_info,
1083 on_object_unregistered,
1085 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
1086 g_assert (!g_dbus_error_is_remote_error (error));
1087 g_error_free (error);
1089 g_assert (registration_id == 0);
1091 /* register at different interface - shouldn't fail */
1092 registration_id = g_dbus_connection_register_object (c,
1093 "/foo/boss/interns/intern2",
1094 (GDBusInterfaceInfo *) &foo_interface_info,
1097 on_object_unregistered,
1099 g_assert_no_error (error);
1100 g_assert (registration_id > 0);
1101 intern2_foo_reg_id = registration_id;
1102 num_successful_registrations++;
1104 /* unregister it via the id */
1105 g_assert (g_dbus_connection_unregister_object (c, registration_id));
1106 g_main_context_iteration (NULL, FALSE);
1107 g_assert_cmpint (data.num_unregistered_calls, ==, 1);
1108 intern2_foo_reg_id = 0;
1110 /* register it back */
1111 registration_id = g_dbus_connection_register_object (c,
1112 "/foo/boss/interns/intern2",
1113 (GDBusInterfaceInfo *) &foo_interface_info,
1116 on_object_unregistered,
1118 g_assert_no_error (error);
1119 g_assert (registration_id > 0);
1120 intern2_foo_reg_id = registration_id;
1121 num_successful_registrations++;
1123 registration_id = g_dbus_connection_register_object (c,
1124 "/foo/boss/interns/intern3",
1125 (GDBusInterfaceInfo *) &bar_interface_info,
1128 on_object_unregistered,
1130 g_assert_no_error (error);
1131 g_assert (registration_id > 0);
1132 intern3_bar_reg_id = registration_id;
1133 num_successful_registrations++;
1135 /* now register a whole subtree at /foo/boss/executives */
1136 subtree_registration_id = g_dbus_connection_register_subtree (c,
1137 "/foo/boss/executives",
1139 G_DBUS_SUBTREE_FLAGS_NONE,
1141 on_subtree_unregistered,
1143 g_assert_no_error (error);
1144 g_assert (subtree_registration_id > 0);
1145 /* try registering it again.. this should fail */
1146 registration_id = g_dbus_connection_register_subtree (c,
1147 "/foo/boss/executives",
1149 G_DBUS_SUBTREE_FLAGS_NONE,
1151 on_subtree_unregistered,
1153 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
1154 g_assert (!g_dbus_error_is_remote_error (error));
1155 g_error_free (error);
1157 g_assert (registration_id == 0);
1159 /* unregister it, then register it again */
1160 g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 0);
1161 g_assert (g_dbus_connection_unregister_subtree (c, subtree_registration_id));
1162 g_main_context_iteration (NULL, FALSE);
1163 g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 1);
1164 subtree_registration_id = g_dbus_connection_register_subtree (c,
1165 "/foo/boss/executives",
1167 G_DBUS_SUBTREE_FLAGS_NONE,
1169 on_subtree_unregistered,
1171 g_assert_no_error (error);
1172 g_assert (subtree_registration_id > 0);
1174 /* try to register something under /foo/boss/executives - this should work
1175 * because registered subtrees and registered objects can coexist.
1177 * Make the exported object implement *two* interfaces so we can check
1178 * that the right introspection handler is invoked.
1180 registration_id = g_dbus_connection_register_object (c,
1181 "/foo/boss/executives/non_subtree_object",
1182 (GDBusInterfaceInfo *) &bar_interface_info,
1185 on_object_unregistered,
1187 g_assert_no_error (error);
1188 g_assert (registration_id > 0);
1189 non_subtree_object_path_bar_reg_id = registration_id;
1190 num_successful_registrations++;
1191 registration_id = g_dbus_connection_register_object (c,
1192 "/foo/boss/executives/non_subtree_object",
1193 (GDBusInterfaceInfo *) &foo_interface_info,
1196 on_object_unregistered,
1198 g_assert_no_error (error);
1199 g_assert (registration_id > 0);
1200 non_subtree_object_path_foo_reg_id = registration_id;
1201 num_successful_registrations++;
1203 /* now register a dynamic subtree, spawning objects as they are called */
1204 dyna_data = g_ptr_array_new ();
1205 dyna_subtree_registration_id = g_dbus_connection_register_subtree (c,
1207 &dynamic_subtree_vtable,
1208 G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES,
1210 (GDestroyNotify)g_ptr_array_unref,
1212 g_assert_no_error (error);
1213 g_assert (dyna_subtree_registration_id > 0);
1215 /* First assert that we have no nodes in the dynamic subtree */
1216 nodes = get_nodes_at (c, "/foo/dyna");
1217 g_assert (nodes != NULL);
1218 g_assert_cmpint (g_strv_length (nodes), ==, 0);
1220 g_assert_cmpint (count_interfaces (c, "/foo/dyna"), ==, 4);
1222 /* Install three nodes in the dynamic subtree via the dyna_data backdoor and
1223 * assert that they show up correctly in the introspection data */
1224 g_ptr_array_add (dyna_data, "lol");
1225 g_ptr_array_add (dyna_data, "cat");
1226 g_ptr_array_add (dyna_data, "cheezburger");
1227 nodes = get_nodes_at (c, "/foo/dyna");
1228 g_assert (nodes != NULL);
1229 g_assert_cmpint (g_strv_length (nodes), ==, 3);
1230 g_assert_cmpstr (nodes[0], ==, "lol");
1231 g_assert_cmpstr (nodes[1], ==, "cat");
1232 g_assert_cmpstr (nodes[2], ==, "cheezburger");
1234 g_assert_cmpint (count_interfaces (c, "/foo/dyna/lol"), ==, 4);
1235 g_assert_cmpint (count_interfaces (c, "/foo/dyna/cat"), ==, 4);
1236 g_assert_cmpint (count_interfaces (c, "/foo/dyna/cheezburger"), ==, 4);
1238 /* Call a non-existing object path and assert that it has been created */
1239 dyna_create (c, "dynamicallycreated");
1240 nodes = get_nodes_at (c, "/foo/dyna");
1241 g_assert (nodes != NULL);
1242 g_assert_cmpint (g_strv_length (nodes), ==, 4);
1243 g_assert_cmpstr (nodes[0], ==, "lol");
1244 g_assert_cmpstr (nodes[1], ==, "cat");
1245 g_assert_cmpstr (nodes[2], ==, "cheezburger");
1246 g_assert_cmpstr (nodes[3], ==, "dynamicallycreated");
1248 g_assert_cmpint (count_interfaces (c, "/foo/dyna/dynamicallycreated"), ==, 4);
1250 /* now check that the object hierarachy is properly generated... yes, it's a bit
1251 * perverse that we round-trip to the bus to introspect ourselves ;-)
1253 nodes = get_nodes_at (c, "/");
1254 g_assert (nodes != NULL);
1255 g_assert_cmpint (g_strv_length (nodes), ==, 1);
1256 g_assert_cmpstr (nodes[0], ==, "foo");
1258 g_assert_cmpint (count_interfaces (c, "/"), ==, 0);
1260 nodes = get_nodes_at (c, "/foo");
1261 g_assert (nodes != NULL);
1262 g_assert_cmpint (g_strv_length (nodes), ==, 2);
1263 g_assert_cmpstr (nodes[0], ==, "boss");
1264 g_assert_cmpstr (nodes[1], ==, "dyna");
1266 g_assert_cmpint (count_interfaces (c, "/foo"), ==, 0);
1268 nodes = get_nodes_at (c, "/foo/boss");
1269 g_assert (nodes != NULL);
1270 g_assert_cmpint (g_strv_length (nodes), ==, 5);
1271 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker1"));
1272 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker1p1"));
1273 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker2"));
1274 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "interns"));
1275 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "executives"));
1277 /* any registered object always implement org.freedesktop.DBus.[Peer,Introspectable,Properties] */
1278 g_assert_cmpint (count_interfaces (c, "/foo/boss"), ==, 5);
1279 g_assert (has_interface (c, "/foo/boss", foo_interface_info.name));
1280 g_assert (has_interface (c, "/foo/boss", bar_interface_info.name));
1282 /* check subtree nodes - we should have only non_subtree_object in /foo/boss/executives
1283 * because data.num_subtree_nodes is 0
1285 nodes = get_nodes_at (c, "/foo/boss/executives");
1286 g_assert (nodes != NULL);
1287 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1288 g_assert_cmpint (g_strv_length (nodes), ==, 1);
1290 g_assert_cmpint (count_interfaces (c, "/foo/boss/executives"), ==, 0);
1292 /* now change data.num_subtree_nodes and check */
1293 data.num_subtree_nodes = 2;
1294 nodes = get_nodes_at (c, "/foo/boss/executives");
1295 g_assert (nodes != NULL);
1296 g_assert_cmpint (g_strv_length (nodes), ==, 5);
1297 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1298 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp0"));
1299 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp1"));
1300 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp0"));
1301 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp1"));
1302 /* check that /foo/boss/executives/non_subtree_object is not handled by the
1303 * subtree handlers - we can do this because objects from subtree handlers
1304 * has exactly one interface and non_subtree_object has two
1306 g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/non_subtree_object"), ==, 5);
1307 g_assert (has_interface (c, "/foo/boss/executives/non_subtree_object", foo_interface_info.name));
1308 g_assert (has_interface (c, "/foo/boss/executives/non_subtree_object", bar_interface_info.name));
1309 /* check that the vp and evp objects are handled by the subtree handlers */
1310 g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/vp0"), ==, 4);
1311 g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/vp1"), ==, 4);
1312 g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/evp0"), ==, 4);
1313 g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/evp1"), ==, 4);
1314 g_assert (has_interface (c, "/foo/boss/executives/vp0", foo_interface_info.name));
1315 g_assert (has_interface (c, "/foo/boss/executives/vp1", foo_interface_info.name));
1316 g_assert (has_interface (c, "/foo/boss/executives/evp0", bar_interface_info.name));
1317 g_assert (has_interface (c, "/foo/boss/executives/evp1", bar_interface_info.name));
1319 data.num_subtree_nodes = 3;
1320 nodes = get_nodes_at (c, "/foo/boss/executives");
1321 g_assert (nodes != NULL);
1322 g_assert_cmpint (g_strv_length (nodes), ==, 7);
1323 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1324 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp0"));
1325 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp1"));
1326 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp2"));
1327 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp0"));
1328 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp1"));
1329 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp2"));
1332 /* This is to check that a bug (rather, class of bugs) in gdbusconnection.c's
1334 * g_dbus_connection_list_registered_unlocked()
1336 * where /foo/boss/worker1 reported a child '1', is now fixed.
1338 nodes = get_nodes_at (c, "/foo/boss/worker1");
1339 g_assert (nodes != NULL);
1340 g_assert_cmpint (g_strv_length (nodes), ==, 0);
1343 /* check that calls are properly dispatched to the functions in foo_vtable for objects
1344 * implementing the org.example.Foo interface
1346 * We do this for both a regular registered object (/foo/boss) and also for an object
1347 * registered through the subtree mechanism.
1349 test_dispatch ("/foo/boss");
1350 test_dispatch ("/foo/boss/executives/vp0");
1352 /* To prevent from exiting and attaching a D-Bus tool like D-Feet; uncomment: */
1354 g_debug ("Point D-feet or other tool at: %s", g_test_dbus_get_temporary_address());
1355 g_main_loop_run (loop);
1358 /* check that unregistering the subtree handler works */
1359 g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 1);
1360 g_assert (g_dbus_connection_unregister_subtree (c, subtree_registration_id));
1361 g_main_context_iteration (NULL, FALSE);
1362 g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 2);
1363 nodes = get_nodes_at (c, "/foo/boss/executives");
1364 g_assert (nodes != NULL);
1365 g_assert_cmpint (g_strv_length (nodes), ==, 1);
1366 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1369 g_assert (g_dbus_connection_unregister_object (c, boss_foo_reg_id));
1370 g_assert (g_dbus_connection_unregister_object (c, boss_bar_reg_id));
1371 g_assert (g_dbus_connection_unregister_object (c, worker1_foo_reg_id));
1372 g_assert (g_dbus_connection_unregister_object (c, worker1p1_foo_reg_id));
1373 g_assert (g_dbus_connection_unregister_object (c, worker2_bar_reg_id));
1374 g_assert (g_dbus_connection_unregister_object (c, intern1_foo_reg_id));
1375 g_assert (g_dbus_connection_unregister_object (c, intern2_bar_reg_id));
1376 g_assert (g_dbus_connection_unregister_object (c, intern2_foo_reg_id));
1377 g_assert (g_dbus_connection_unregister_object (c, intern3_bar_reg_id));
1378 g_assert (g_dbus_connection_unregister_object (c, non_subtree_object_path_bar_reg_id));
1379 g_assert (g_dbus_connection_unregister_object (c, non_subtree_object_path_foo_reg_id));
1381 g_main_context_iteration (NULL, FALSE);
1382 g_assert_cmpint (data.num_unregistered_calls, ==, num_successful_registrations);
1384 /* check that we no longer export any objects - TODO: it looks like there's a bug in
1385 * libdbus-1 here: libdbus still reports the '/foo' object; so disable the test for now
1388 nodes = get_nodes_at (c, "/");
1389 g_assert (nodes != NULL);
1390 g_assert_cmpint (g_strv_length (nodes), ==, 0);
1397 static const GDBusInterfaceInfo test_interface_info1 =
1401 (GDBusMethodInfo **) NULL,
1402 (GDBusSignalInfo **) NULL,
1403 (GDBusPropertyInfo **) NULL,
1407 static const GDBusInterfaceInfo test_interface_info2 =
1410 "org.freedesktop.DBus.Properties",
1411 (GDBusMethodInfo **) NULL,
1412 (GDBusSignalInfo **) NULL,
1413 (GDBusPropertyInfo **) NULL,
1418 check_interfaces (GDBusConnection *c,
1419 const gchar *object_path,
1420 const gchar **interfaces)
1425 GDBusNodeInfo *node_info;
1429 proxy = g_dbus_proxy_new_sync (c,
1430 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
1431 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
1433 g_dbus_connection_get_unique_name (c),
1435 "org.freedesktop.DBus.Introspectable",
1438 g_assert_no_error (error);
1439 g_assert (proxy != NULL);
1441 /* do this async to avoid libdbus-1 deadlocks */
1443 g_dbus_proxy_call (proxy,
1446 G_DBUS_CALL_FLAGS_NONE,
1449 (GAsyncReadyCallback) introspect_callback,
1451 g_main_loop_run (loop);
1452 g_assert (xml_data != NULL);
1454 node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
1455 g_assert_no_error (error);
1456 g_assert (node_info != NULL);
1458 g_assert (node_info->interfaces != NULL);
1459 for (i = 0; node_info->interfaces[i]; i++) ;
1461 if (g_strv_length ((gchar**)interfaces) != i - 1)
1463 g_print ("expected ");
1464 for (i = 0; interfaces[i]; i++)
1465 g_print ("%s ", interfaces[i]);
1467 for (i = 0; node_info->interfaces[i]; i++)
1468 g_print ("%s ", node_info->interfaces[i]->name);
1472 g_assert_cmpint (g_strv_length ((gchar**)interfaces), ==, i - 1);
1474 for (i = 0; interfaces[i]; i++)
1476 for (j = 0; node_info->interfaces[j]; j++)
1478 if (strcmp (interfaces[i], node_info->interfaces[j]->name) == 0)
1482 g_assert_not_reached ();
1487 g_object_unref (proxy);
1489 g_dbus_node_info_unref (node_info);
1493 test_registered_interfaces (void)
1497 const gchar *interfaces[] = {
1499 "org.freedesktop.DBus.Properties",
1500 "org.freedesktop.DBus.Introspectable",
1505 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
1506 g_assert_no_error (error);
1507 g_assert (c != NULL);
1509 id1 = g_dbus_connection_register_object (c,
1511 (GDBusInterfaceInfo *) &test_interface_info1,
1516 g_assert_no_error (error);
1518 id2 = g_dbus_connection_register_object (c,
1520 (GDBusInterfaceInfo *) &test_interface_info2,
1525 g_assert_no_error (error);
1528 check_interfaces (c, "/test", interfaces);
1530 g_assert (g_dbus_connection_unregister_object (c, id1));
1531 g_assert (g_dbus_connection_unregister_object (c, id2));
1536 /* ---------------------------------------------------------------------------------------------------- */
1539 test_async_method_call (GDBusConnection *connection,
1540 const gchar *sender,
1541 const gchar *object_path,
1542 const gchar *interface_name,
1543 const gchar *method_name,
1544 GVariant *parameters,
1545 GDBusMethodInvocation *invocation,
1548 const GDBusPropertyInfo *property;
1550 /* Strictly speaking, this function should also expect to receive
1551 * method calls not on the org.freedesktop.DBus.Properties interface,
1552 * but we don't do any during this testcase, so assert that.
1554 g_assert_cmpstr (interface_name, ==, "org.freedesktop.DBus.Properties");
1555 g_assert (g_dbus_method_invocation_get_method_info (invocation) == NULL);
1557 property = g_dbus_method_invocation_get_property_info (invocation);
1559 /* We should never be seeing any property calls on the com.example.Bar
1560 * interface because it doesn't export any properties.
1562 * In each case below make sure the interface is org.example.Foo.
1565 /* Do a whole lot of asserts to make sure that invalid calls are still
1566 * getting properly rejected by GDBusConnection and that our
1567 * environment is as we expect it to be.
1569 if (g_str_equal (method_name, "Get"))
1571 const gchar *iface_name, *prop_name;
1573 g_variant_get (parameters, "(&s&s)", &iface_name, &prop_name);
1574 g_assert_cmpstr (iface_name, ==, "org.example.Foo");
1575 g_assert (property != NULL);
1576 g_assert_cmpstr (prop_name, ==, property->name);
1577 g_assert (property->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE);
1578 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(v)", g_variant_new_string (prop_name)));
1581 else if (g_str_equal (method_name, "Set"))
1583 const gchar *iface_name, *prop_name;
1586 g_variant_get (parameters, "(&s&sv)", &iface_name, &prop_name, &value);
1587 g_assert_cmpstr (iface_name, ==, "org.example.Foo");
1588 g_assert (property != NULL);
1589 g_assert_cmpstr (prop_name, ==, property->name);
1590 g_assert (property->flags & G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE);
1591 g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE (property->signature)));
1592 g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1593 g_variant_unref (value);
1596 else if (g_str_equal (method_name, "GetAll"))
1598 const gchar *iface_name;
1600 g_variant_get (parameters, "(&s)", &iface_name);
1601 g_assert_cmpstr (iface_name, ==, "org.example.Foo");
1602 g_assert (property == NULL);
1603 g_dbus_method_invocation_return_value (invocation,
1604 g_variant_new_parsed ("({ 'PropertyUno': < 'uno' >,"
1605 " 'NotWritable': < 'notwrite' > },)"));
1609 g_assert_not_reached ();
1612 static gint outstanding_cases;
1615 ensure_result_cb (GObject *source,
1616 GAsyncResult *result,
1619 GDBusConnection *connection = G_DBUS_CONNECTION (source);
1622 reply = g_dbus_connection_call_finish (connection, result, NULL);
1624 if (user_data == NULL)
1626 /* Expected an error */
1627 g_assert (reply == NULL);
1631 /* Expected a reply of a particular format. */
1634 g_assert (reply != NULL);
1635 str = g_variant_print (reply, TRUE);
1636 g_assert_cmpstr (str, ==, (const gchar *) user_data);
1639 g_variant_unref (reply);
1642 g_assert_cmpint (outstanding_cases, >, 0);
1643 outstanding_cases--;
1647 test_async_case (GDBusConnection *connection,
1648 const gchar *expected_reply,
1649 const gchar *method,
1650 const gchar *format_string,
1655 va_start (ap, format_string);
1657 g_dbus_connection_call (connection, g_dbus_connection_get_unique_name (connection), "/foo",
1658 "org.freedesktop.DBus.Properties", method, g_variant_new_va (format_string, NULL, &ap),
1659 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, ensure_result_cb, (gpointer) expected_reply);
1663 outstanding_cases++;
1667 test_async_properties (void)
1669 GError *error = NULL;
1670 guint registration_id, registration_id2;
1671 static const GDBusInterfaceVTable vtable = {
1672 test_async_method_call, NULL, NULL
1675 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
1676 g_assert_no_error (error);
1677 g_assert (c != NULL);
1679 registration_id = g_dbus_connection_register_object (c,
1681 (GDBusInterfaceInfo *) &foo_interface_info,
1682 &vtable, NULL, NULL, &error);
1683 g_assert_no_error (error);
1684 g_assert (registration_id);
1685 registration_id2 = g_dbus_connection_register_object (c,
1687 (GDBusInterfaceInfo *) &foo2_interface_info,
1688 &vtable, NULL, NULL, &error);
1689 g_assert_no_error (error);
1690 g_assert (registration_id);
1692 test_async_case (c, NULL, "random", "()");
1694 /* Test a variety of error cases */
1695 test_async_case (c, NULL, "Get", "(si)", "wrong signature", 5);
1696 test_async_case (c, NULL, "Get", "(ss)", "org.example.WrongInterface", "zzz");
1697 test_async_case (c, NULL, "Get", "(ss)", "org.example.Foo", "NoSuchProperty");
1698 test_async_case (c, NULL, "Get", "(ss)", "org.example.Foo", "NotReadable");
1700 test_async_case (c, NULL, "Set", "(si)", "wrong signature", 5);
1701 test_async_case (c, NULL, "Set", "(ssv)", "org.example.WrongInterface", "zzz", g_variant_new_string (""));
1702 test_async_case (c, NULL, "Set", "(ssv)", "org.example.Foo", "NoSuchProperty", g_variant_new_string (""));
1703 test_async_case (c, NULL, "Set", "(ssv)", "org.example.Foo", "NotWritable", g_variant_new_string (""));
1704 test_async_case (c, NULL, "Set", "(ssv)", "org.example.Foo", "PropertyUno", g_variant_new_object_path ("/wrong"));
1706 test_async_case (c, NULL, "GetAll", "(si)", "wrong signature", 5);
1707 test_async_case (c, NULL, "GetAll", "(s)", "org.example.WrongInterface");
1709 /* Make sure that we get no unexpected async property calls for com.example.Foo2 */
1710 test_async_case (c, NULL, "Get", "(ss)", "org.example.Foo2", "zzz");
1711 test_async_case (c, NULL, "Set", "(ssv)", "org.example.Foo2", "zzz", g_variant_new_string (""));
1712 test_async_case (c, "(@a{sv} {},)", "GetAll", "(s)", "org.example.Foo2");
1714 /* Now do the proper things */
1715 test_async_case (c, "(<'PropertyUno'>,)", "Get", "(ss)", "org.example.Foo", "PropertyUno");
1716 test_async_case (c, "(<'NotWritable'>,)", "Get", "(ss)", "org.example.Foo", "NotWritable");
1717 test_async_case (c, "()", "Set", "(ssv)", "org.example.Foo", "PropertyUno", g_variant_new_string (""));
1718 test_async_case (c, "()", "Set", "(ssv)", "org.example.Foo", "NotReadable", g_variant_new_string (""));
1719 test_async_case (c, "({'PropertyUno': <'uno'>, 'NotWritable': <'notwrite'>},)", "GetAll", "(s)", "org.example.Foo");
1721 while (outstanding_cases)
1722 g_main_context_iteration (NULL, TRUE);
1724 g_dbus_connection_unregister_object (c, registration_id);
1725 g_dbus_connection_unregister_object (c, registration_id2);
1729 /* ---------------------------------------------------------------------------------------------------- */
1735 g_test_init (&argc, &argv, NULL);
1737 /* all the tests rely on a shared main loop */
1738 loop = g_main_loop_new (NULL, FALSE);
1740 g_test_add_func ("/gdbus/object-registration", test_object_registration);
1741 g_test_add_func ("/gdbus/registered-interfaces", test_registered_interfaces);
1742 g_test_add_func ("/gdbus/async-properties", test_async_properties);
1744 /* TODO: check that we spit out correct introspection data */
1745 /* TODO: check that registering a whole subtree works */
1747 return session_bus_run ();