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,
126 foo_method_call (GDBusConnection *connection,
128 const gchar *object_path,
129 const gchar *interface_name,
130 const gchar *method_name,
131 GVariant *parameters,
132 GDBusMethodInvocation *invocation,
135 if (g_strcmp0 (method_name, "Method1") == 0)
139 g_variant_get (parameters, "(&s)", &input);
140 output = g_strdup_printf ("You passed the string `%s'. Jolly good!", input);
141 g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", output));
146 g_dbus_method_invocation_return_dbus_error (invocation,
147 "org.example.SomeError",
148 "How do you like them apples, buddy!");
153 foo_get_property (GDBusConnection *connection,
155 const gchar *object_path,
156 const gchar *interface_name,
157 const gchar *property_name,
163 s = g_strdup_printf ("Property `%s' Is What It Is!", property_name);
164 ret = g_variant_new_string (s);
170 foo_set_property (GDBusConnection *connection,
172 const gchar *object_path,
173 const gchar *interface_name,
174 const gchar *property_name,
180 s = g_variant_print (value, TRUE);
183 G_DBUS_ERROR_SPAWN_FILE_INVALID,
184 "Returning some error instead of writing the value `%s' to the property `%s'",
190 static const GDBusInterfaceVTable foo_vtable =
197 /* -------------------- */
199 static const GDBusMethodInfo bar_method_info[] =
216 static const GDBusMethodInfo * const bar_method_info_pointers[] = {&bar_method_info[0], &bar_method_info[1], NULL};
218 static const GDBusSignalInfo bar_signal_info[] =
227 static const GDBusSignalInfo * const bar_signal_info_pointers[] = {&bar_signal_info[0], NULL};
229 static const GDBusPropertyInfo bar_property_info[] =
235 G_DBUS_PROPERTY_INFO_FLAGS_READABLE,
239 static const GDBusPropertyInfo * const bar_property_info_pointers[] = {&bar_property_info[0], NULL};
241 static const GDBusInterfaceInfo bar_interface_info =
245 (GDBusMethodInfo **) bar_method_info_pointers,
246 (GDBusSignalInfo **) bar_signal_info_pointers,
247 (GDBusPropertyInfo **) bar_property_info_pointers,
251 /* -------------------- */
253 static const GDBusMethodInfo dyna_method_info[] =
263 static const GDBusMethodInfo * const dyna_method_info_pointers[] = {&dyna_method_info[0], NULL};
265 static const GDBusInterfaceInfo dyna_interface_info =
269 (GDBusMethodInfo **) dyna_method_info_pointers,
270 NULL, /* no signals */
271 NULL, /* no properties */
276 dyna_cyber (GDBusConnection *connection,
278 const gchar *object_path,
279 const gchar *interface_name,
280 const gchar *method_name,
281 GVariant *parameters,
282 GDBusMethodInvocation *invocation,
285 GPtrArray *data = user_data;
289 node_name = strrchr (object_path, '/') + 1;
291 /* Add new node if it is not already known */
292 for (n = 0; n < data->len ; n++)
294 if (g_strcmp0 (g_ptr_array_index (data, n), node_name) == 0)
297 g_ptr_array_add (data, g_strdup(node_name));
300 g_dbus_method_invocation_return_value (invocation, NULL);
303 static const GDBusInterfaceVTable dyna_interface_vtable =
310 /* ---------------------------------------------------------------------------------------------------- */
313 introspect_callback (GDBusProxy *proxy,
317 gchar **xml_data = user_data;
322 result = g_dbus_proxy_call_finish (proxy,
325 g_assert_no_error (error);
326 g_assert (result != NULL);
327 g_variant_get (result, "(s)", xml_data);
328 g_variant_unref (result);
330 g_main_loop_quit (loop);
334 get_nodes_at (GDBusConnection *c,
335 const gchar *object_path)
341 GDBusNodeInfo *node_info;
345 proxy = g_dbus_proxy_new_sync (c,
346 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
347 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
349 g_dbus_connection_get_unique_name (c),
351 "org.freedesktop.DBus.Introspectable",
354 g_assert_no_error (error);
355 g_assert (proxy != NULL);
357 /* do this async to avoid libdbus-1 deadlocks */
359 g_dbus_proxy_call (proxy,
362 G_DBUS_CALL_FLAGS_NONE,
365 (GAsyncReadyCallback) introspect_callback,
367 g_main_loop_run (loop);
368 g_assert (xml_data != NULL);
370 node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
371 g_assert_no_error (error);
372 g_assert (node_info != NULL);
374 p = g_ptr_array_new ();
375 for (n = 0; node_info->nodes != NULL && node_info->nodes[n] != NULL; n++)
377 const GDBusNodeInfo *sub_node_info = node_info->nodes[n];
378 g_ptr_array_add (p, g_strdup (sub_node_info->path));
380 g_ptr_array_add (p, NULL);
382 g_object_unref (proxy);
384 g_dbus_node_info_unref (node_info);
386 return (gchar **) g_ptr_array_free (p, FALSE);
390 has_interface (GDBusConnection *c,
391 const gchar *object_path,
392 const gchar *interface_name)
397 GDBusNodeInfo *node_info;
401 proxy = g_dbus_proxy_new_sync (c,
402 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
403 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
405 g_dbus_connection_get_unique_name (c),
407 "org.freedesktop.DBus.Introspectable",
410 g_assert_no_error (error);
411 g_assert (proxy != NULL);
413 /* do this async to avoid libdbus-1 deadlocks */
415 g_dbus_proxy_call (proxy,
418 G_DBUS_CALL_FLAGS_NONE,
421 (GAsyncReadyCallback) introspect_callback,
423 g_main_loop_run (loop);
424 g_assert (xml_data != NULL);
426 node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
427 g_assert_no_error (error);
428 g_assert (node_info != NULL);
430 ret = (g_dbus_node_info_lookup_interface (node_info, interface_name) != NULL);
432 g_object_unref (proxy);
434 g_dbus_node_info_unref (node_info);
440 count_interfaces (GDBusConnection *c,
441 const gchar *object_path)
446 GDBusNodeInfo *node_info;
450 proxy = g_dbus_proxy_new_sync (c,
451 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
452 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
454 g_dbus_connection_get_unique_name (c),
456 "org.freedesktop.DBus.Introspectable",
459 g_assert_no_error (error);
460 g_assert (proxy != NULL);
462 /* do this async to avoid libdbus-1 deadlocks */
464 g_dbus_proxy_call (proxy,
467 G_DBUS_CALL_FLAGS_NONE,
470 (GAsyncReadyCallback) introspect_callback,
472 g_main_loop_run (loop);
473 g_assert (xml_data != NULL);
475 node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
476 g_assert_no_error (error);
477 g_assert (node_info != NULL);
480 while (node_info->interfaces != NULL && node_info->interfaces[ret] != NULL)
483 g_object_unref (proxy);
485 g_dbus_node_info_unref (node_info);
491 dyna_create_callback (GDBusProxy *proxy,
499 result = g_dbus_proxy_call_finish (proxy,
502 g_assert_no_error (error);
503 g_assert (result != NULL);
504 g_variant_unref (result);
506 g_main_loop_quit (loop);
509 /* Dynamically create @object_name under /foo/dyna */
511 dyna_create (GDBusConnection *c,
512 const gchar *object_name)
518 object_path = g_strconcat ("/foo/dyna/", object_name, NULL);
521 proxy = g_dbus_proxy_new_sync (c,
522 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
523 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
525 g_dbus_connection_get_unique_name (c),
530 g_assert_no_error (error);
531 g_assert (proxy != NULL);
533 /* do this async to avoid libdbus-1 deadlocks */
534 g_dbus_proxy_call (proxy,
536 g_variant_new ("()"),
537 G_DBUS_CALL_FLAGS_NONE,
540 (GAsyncReadyCallback) dyna_create_callback,
542 g_main_loop_run (loop);
544 g_assert_no_error (error);
546 g_object_unref (proxy);
547 g_free (object_path);
554 guint num_unregistered_calls;
555 guint num_unregistered_subtree_calls;
556 guint num_subtree_nodes;
557 } ObjectRegistrationData;
560 on_object_unregistered (gpointer user_data)
562 ObjectRegistrationData *data = user_data;
564 data->num_unregistered_calls++;
568 on_subtree_unregistered (gpointer user_data)
570 ObjectRegistrationData *data = user_data;
572 data->num_unregistered_subtree_calls++;
576 _g_strv_has_string (const gchar* const * haystack,
581 for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
583 if (g_strcmp0 (haystack[n], needle) == 0)
589 /* -------------------- */
592 subtree_enumerate (GDBusConnection *connection,
594 const gchar *object_path,
597 ObjectRegistrationData *data = user_data;
602 p = g_ptr_array_new ();
604 for (n = 0; n < data->num_subtree_nodes; n++)
606 g_ptr_array_add (p, g_strdup_printf ("vp%d", n));
607 g_ptr_array_add (p, g_strdup_printf ("evp%d", n));
609 g_ptr_array_add (p, NULL);
611 nodes = (gchar **) g_ptr_array_free (p, FALSE);
616 /* Only allows certain objects, and aborts on unknowns */
618 subtree_introspect (GDBusConnection *connection,
620 const gchar *object_path,
624 GPtrArray *interfaces;
626 /* VPs implement the Foo interface, EVPs implement the Bar interface. The root
627 * does not implement any interfaces
629 interfaces = g_ptr_array_new ();
630 if (g_str_has_prefix (node, "vp"))
632 g_ptr_array_add (interfaces, (gpointer) &foo_interface_info);
634 else if (g_str_has_prefix (node, "evp"))
636 g_ptr_array_add (interfaces, (gpointer) &bar_interface_info);
638 else if (g_strcmp0 (node, "/") == 0)
644 g_assert_not_reached ();
650 static const GDBusInterfaceVTable *
651 subtree_dispatch (GDBusConnection *connection,
653 const gchar *object_path,
654 const gchar *interface_name,
656 gpointer *out_user_data,
659 if (g_strcmp0 (interface_name, "org.example.Foo") == 0)
665 static const GDBusSubtreeVTable subtree_vtable =
672 /* -------------------- */
675 dynamic_subtree_enumerate (GDBusConnection *connection,
677 const gchar *object_path,
680 GPtrArray *data = user_data;
681 gchar **nodes = g_new (gchar*, data->len + 1);
684 for (n = 0; n < data->len; n++)
686 nodes[n] = g_strdup (g_ptr_array_index (data, n));
688 nodes[data->len] = NULL;
693 /* Allow all objects to be introspected */
695 dynamic_subtree_introspect (GDBusConnection *connection,
697 const gchar *object_path,
701 GPtrArray *interfaces;
703 /* All nodes (including the root node) implements the Dyna interface */
704 interfaces = g_ptr_array_new ();
705 g_ptr_array_add (interfaces, (gpointer) &dyna_interface_info);
710 static const GDBusInterfaceVTable *
711 dynamic_subtree_dispatch (GDBusConnection *connection,
713 const gchar *object_path,
714 const gchar *interface_name,
716 gpointer *out_user_data,
719 *out_user_data = user_data;
720 return &dyna_interface_vtable;
723 static const GDBusSubtreeVTable dynamic_subtree_vtable =
725 dynamic_subtree_enumerate,
726 dynamic_subtree_introspect,
727 dynamic_subtree_dispatch
730 /* -------------------- */
733 test_dispatch_thread_func (gpointer user_data)
735 const gchar *object_path = user_data;
736 GDBusProxy *foo_proxy;
741 const gchar *value_str;
743 foo_proxy = g_dbus_proxy_new_sync (c,
744 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS |
745 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
747 g_dbus_connection_get_unique_name (c),
752 g_assert (foo_proxy != NULL);
754 /* generic interfaces */
756 value = g_dbus_proxy_call_sync (foo_proxy,
757 "org.freedesktop.DBus.Peer.Ping",
759 G_DBUS_CALL_FLAGS_NONE,
763 g_assert_no_error (error);
764 g_assert (value != NULL);
765 g_variant_unref (value);
769 value = g_dbus_proxy_call_sync (foo_proxy,
771 g_variant_new ("(s)", "winwinwin"),
772 G_DBUS_CALL_FLAGS_NONE,
776 g_assert_no_error (error);
777 g_assert (value != NULL);
778 g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("(s)")));
779 g_variant_get (value, "(&s)", &value_str);
780 g_assert_cmpstr (value_str, ==, "You passed the string `winwinwin'. Jolly good!");
781 g_variant_unref (value);
784 value = g_dbus_proxy_call_sync (foo_proxy,
787 G_DBUS_CALL_FLAGS_NONE,
791 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
792 g_assert_cmpstr (error->message, ==, "GDBus.Error:org.example.SomeError: How do you like them apples, buddy!");
793 g_error_free (error);
794 g_assert (value == NULL);
797 value = g_dbus_proxy_call_sync (foo_proxy,
799 g_variant_new ("(s)", "failfailfail"),
800 G_DBUS_CALL_FLAGS_NONE,
804 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
805 g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Type of message, `(s)', does not match expected type `()'");
806 g_error_free (error);
807 g_assert (value == NULL);
810 value = g_dbus_proxy_call_sync (foo_proxy,
813 G_DBUS_CALL_FLAGS_NONE,
817 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
818 g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod: No such method `NonExistantMethod'");
819 g_error_free (error);
820 g_assert (value == NULL);
823 value = g_dbus_proxy_call_sync (foo_proxy,
824 "org.example.FooXYZ.NonExistant",
826 G_DBUS_CALL_FLAGS_NONE,
830 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
831 g_error_free (error);
832 g_assert (value == NULL);
834 /* user properties */
836 value = g_dbus_proxy_call_sync (foo_proxy,
837 "org.freedesktop.DBus.Properties.Get",
838 g_variant_new ("(ss)",
841 G_DBUS_CALL_FLAGS_NONE,
845 g_assert_no_error (error);
846 g_assert (value != NULL);
847 g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("(v)")));
848 g_variant_get (value, "(v)", &inner);
849 g_assert (g_variant_is_of_type (inner, G_VARIANT_TYPE_STRING));
850 g_assert_cmpstr (g_variant_get_string (inner, NULL), ==, "Property `PropertyUno' Is What It Is!");
851 g_variant_unref (value);
854 value = g_dbus_proxy_call_sync (foo_proxy,
855 "org.freedesktop.DBus.Properties.Get",
856 g_variant_new ("(ss)",
859 G_DBUS_CALL_FLAGS_NONE,
863 g_assert (value == NULL);
864 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
865 g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: No such property `ThisDoesntExist'");
866 g_error_free (error);
869 value = g_dbus_proxy_call_sync (foo_proxy,
870 "org.freedesktop.DBus.Properties.Get",
871 g_variant_new ("(ss)",
874 G_DBUS_CALL_FLAGS_NONE,
878 g_assert (value == NULL);
879 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
880 g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Property `NotReadable' is not readable");
881 g_error_free (error);
884 value = g_dbus_proxy_call_sync (foo_proxy,
885 "org.freedesktop.DBus.Properties.Set",
886 g_variant_new ("(ssv)",
889 g_variant_new_string ("But Writable you are!")),
890 G_DBUS_CALL_FLAGS_NONE,
894 g_assert (value == NULL);
895 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_SPAWN_FILE_INVALID);
896 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!''");
897 g_error_free (error);
900 value = g_dbus_proxy_call_sync (foo_proxy,
901 "org.freedesktop.DBus.Properties.Set",
902 g_variant_new ("(ssv)",
905 g_variant_new_uint32 (42)),
906 G_DBUS_CALL_FLAGS_NONE,
910 g_assert (value == NULL);
911 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
912 g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Property `NotWritable' is not writable");
913 g_error_free (error);
916 value = g_dbus_proxy_call_sync (foo_proxy,
917 "org.freedesktop.DBus.Properties.GetAll",
918 g_variant_new ("(s)",
920 G_DBUS_CALL_FLAGS_NONE,
924 g_assert_no_error (error);
925 g_assert (value != NULL);
926 g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("(a{sv})")));
927 s = g_variant_print (value, TRUE);
928 g_assert_cmpstr (s, ==, "({'PropertyUno': <\"Property `PropertyUno' Is What It Is!\">, 'NotWritable': <\"Property `NotWritable' Is What It Is!\">},)");
930 g_variant_unref (value);
932 g_object_unref (foo_proxy);
934 g_main_loop_quit (loop);
939 test_dispatch (const gchar *object_path)
944 /* run this in a thread to avoid deadlocks */
946 thread = g_thread_create (test_dispatch_thread_func,
947 (gpointer) object_path,
950 g_assert_no_error (error);
951 g_assert (thread != NULL);
952 g_main_loop_run (loop);
953 g_thread_join (thread);
957 test_object_registration (void)
960 ObjectRegistrationData data;
961 GPtrArray *dyna_data;
963 guint boss_foo_reg_id;
964 guint boss_bar_reg_id;
965 guint worker1_foo_reg_id;
966 guint worker2_bar_reg_id;
967 guint intern1_foo_reg_id;
968 guint intern2_bar_reg_id;
969 guint intern2_foo_reg_id;
970 guint intern3_bar_reg_id;
971 guint registration_id;
972 guint subtree_registration_id;
973 guint non_subtree_object_path_foo_reg_id;
974 guint non_subtree_object_path_bar_reg_id;
975 guint dyna_subtree_registration_id;
976 guint num_successful_registrations;
978 data.num_unregistered_calls = 0;
979 data.num_unregistered_subtree_calls = 0;
980 data.num_subtree_nodes = 0;
982 num_successful_registrations = 0;
985 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
986 g_assert_no_error (error);
987 g_assert (c != NULL);
989 registration_id = g_dbus_connection_register_object (c,
994 on_object_unregistered,
996 g_assert_no_error (error);
997 g_assert (registration_id > 0);
998 boss_foo_reg_id = registration_id;
999 num_successful_registrations++;
1001 registration_id = g_dbus_connection_register_object (c,
1003 &bar_interface_info,
1006 on_object_unregistered,
1008 g_assert_no_error (error);
1009 g_assert (registration_id > 0);
1010 boss_bar_reg_id = registration_id;
1011 num_successful_registrations++;
1013 registration_id = g_dbus_connection_register_object (c,
1014 "/foo/boss/worker1",
1015 &foo_interface_info,
1018 on_object_unregistered,
1020 g_assert_no_error (error);
1021 g_assert (registration_id > 0);
1022 worker1_foo_reg_id = registration_id;
1023 num_successful_registrations++;
1025 registration_id = g_dbus_connection_register_object (c,
1026 "/foo/boss/worker2",
1027 &bar_interface_info,
1030 on_object_unregistered,
1032 g_assert_no_error (error);
1033 g_assert (registration_id > 0);
1034 worker2_bar_reg_id = registration_id;
1035 num_successful_registrations++;
1037 registration_id = g_dbus_connection_register_object (c,
1038 "/foo/boss/interns/intern1",
1039 &foo_interface_info,
1042 on_object_unregistered,
1044 g_assert_no_error (error);
1045 g_assert (registration_id > 0);
1046 intern1_foo_reg_id = registration_id;
1047 num_successful_registrations++;
1049 /* ... and try again at another path */
1050 registration_id = g_dbus_connection_register_object (c,
1051 "/foo/boss/interns/intern2",
1052 &bar_interface_info,
1055 on_object_unregistered,
1057 g_assert_no_error (error);
1058 g_assert (registration_id > 0);
1059 intern2_bar_reg_id = registration_id;
1060 num_successful_registrations++;
1062 /* register at the same path/interface - this should fail */
1063 registration_id = g_dbus_connection_register_object (c,
1064 "/foo/boss/interns/intern2",
1065 &bar_interface_info,
1068 on_object_unregistered,
1070 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
1071 g_assert (!g_dbus_error_is_remote_error (error));
1072 g_error_free (error);
1074 g_assert (registration_id == 0);
1076 /* register at different interface - shouldn't fail */
1077 registration_id = g_dbus_connection_register_object (c,
1078 "/foo/boss/interns/intern2",
1079 &foo_interface_info,
1082 on_object_unregistered,
1084 g_assert_no_error (error);
1085 g_assert (registration_id > 0);
1086 intern2_foo_reg_id = registration_id;
1087 num_successful_registrations++;
1089 /* unregister it via the id */
1090 g_assert (g_dbus_connection_unregister_object (c, registration_id));
1091 g_assert_cmpint (data.num_unregistered_calls, ==, 1);
1092 intern2_foo_reg_id = 0;
1094 /* register it back */
1095 registration_id = g_dbus_connection_register_object (c,
1096 "/foo/boss/interns/intern2",
1097 &foo_interface_info,
1100 on_object_unregistered,
1102 g_assert_no_error (error);
1103 g_assert (registration_id > 0);
1104 intern2_foo_reg_id = registration_id;
1105 num_successful_registrations++;
1107 registration_id = g_dbus_connection_register_object (c,
1108 "/foo/boss/interns/intern3",
1109 &bar_interface_info,
1112 on_object_unregistered,
1114 g_assert_no_error (error);
1115 g_assert (registration_id > 0);
1116 intern3_bar_reg_id = registration_id;
1117 num_successful_registrations++;
1119 /* now register a whole subtree at /foo/boss/executives */
1120 subtree_registration_id = g_dbus_connection_register_subtree (c,
1121 "/foo/boss/executives",
1123 G_DBUS_SUBTREE_FLAGS_NONE,
1125 on_subtree_unregistered,
1127 g_assert_no_error (error);
1128 g_assert (subtree_registration_id > 0);
1129 /* try registering it again.. this should fail */
1130 registration_id = g_dbus_connection_register_subtree (c,
1131 "/foo/boss/executives",
1133 G_DBUS_SUBTREE_FLAGS_NONE,
1135 on_subtree_unregistered,
1137 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
1138 g_assert (!g_dbus_error_is_remote_error (error));
1139 g_error_free (error);
1141 g_assert (registration_id == 0);
1143 /* unregister it, then register it again */
1144 g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 0);
1145 g_assert (g_dbus_connection_unregister_subtree (c, subtree_registration_id));
1146 g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 1);
1147 subtree_registration_id = g_dbus_connection_register_subtree (c,
1148 "/foo/boss/executives",
1150 G_DBUS_SUBTREE_FLAGS_NONE,
1152 on_subtree_unregistered,
1154 g_assert_no_error (error);
1155 g_assert (subtree_registration_id > 0);
1157 /* try to register something under /foo/boss/executives - this should work
1158 * because registered subtrees and registered objects can coexist.
1160 * Make the exported object implement *two* interfaces so we can check
1161 * that the right introspection handler is invoked.
1163 registration_id = g_dbus_connection_register_object (c,
1164 "/foo/boss/executives/non_subtree_object",
1165 &bar_interface_info,
1168 on_object_unregistered,
1170 g_assert_no_error (error);
1171 g_assert (registration_id > 0);
1172 non_subtree_object_path_bar_reg_id = registration_id;
1173 num_successful_registrations++;
1174 registration_id = g_dbus_connection_register_object (c,
1175 "/foo/boss/executives/non_subtree_object",
1176 &foo_interface_info,
1179 on_object_unregistered,
1181 g_assert_no_error (error);
1182 g_assert (registration_id > 0);
1183 non_subtree_object_path_foo_reg_id = registration_id;
1184 num_successful_registrations++;
1186 /* now register a dynamic subtree, spawning objects as they are called */
1187 dyna_data = g_ptr_array_new ();
1188 dyna_subtree_registration_id = g_dbus_connection_register_subtree (c,
1190 &dynamic_subtree_vtable,
1191 G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES,
1193 (GDestroyNotify)g_ptr_array_unref,
1195 g_assert_no_error (error);
1196 g_assert (dyna_subtree_registration_id > 0);
1198 /* First assert that we have no nodes in the dynamic subtree */
1199 nodes = get_nodes_at (c, "/foo/dyna");
1200 g_assert (nodes != NULL);
1201 g_assert_cmpint (g_strv_length (nodes), ==, 0);
1203 g_assert_cmpint (count_interfaces (c, "/foo/dyna"), ==, 4);
1205 /* Install three nodes in the dynamic subtree via the dyna_data backdoor and
1206 * assert that they show up correctly in the introspection data */
1207 g_ptr_array_add (dyna_data, "lol");
1208 g_ptr_array_add (dyna_data, "cat");
1209 g_ptr_array_add (dyna_data, "cheezburger");
1210 nodes = get_nodes_at (c, "/foo/dyna");
1211 g_assert (nodes != NULL);
1212 g_assert_cmpint (g_strv_length (nodes), ==, 3);
1213 g_assert_cmpstr (nodes[0], ==, "lol");
1214 g_assert_cmpstr (nodes[1], ==, "cat");
1215 g_assert_cmpstr (nodes[2], ==, "cheezburger");
1217 g_assert_cmpint (count_interfaces (c, "/foo/dyna/lol"), ==, 4);
1218 g_assert_cmpint (count_interfaces (c, "/foo/dyna/cat"), ==, 4);
1219 g_assert_cmpint (count_interfaces (c, "/foo/dyna/cheezburger"), ==, 4);
1221 /* Call a non-existing object path and assert that it has been created */
1222 dyna_create (c, "dynamicallycreated");
1223 nodes = get_nodes_at (c, "/foo/dyna");
1224 g_assert (nodes != NULL);
1225 g_assert_cmpint (g_strv_length (nodes), ==, 4);
1226 g_assert_cmpstr (nodes[0], ==, "lol");
1227 g_assert_cmpstr (nodes[1], ==, "cat");
1228 g_assert_cmpstr (nodes[2], ==, "cheezburger");
1229 g_assert_cmpstr (nodes[3], ==, "dynamicallycreated");
1231 g_assert_cmpint (count_interfaces (c, "/foo/dyna/dynamicallycreated"), ==, 4);
1233 /* now check that the object hierarachy is properly generated... yes, it's a bit
1234 * perverse that we round-trip to the bus to introspect ourselves ;-)
1236 nodes = get_nodes_at (c, "/");
1237 g_assert (nodes != NULL);
1238 g_assert_cmpint (g_strv_length (nodes), ==, 1);
1239 g_assert_cmpstr (nodes[0], ==, "foo");
1241 g_assert_cmpint (count_interfaces (c, "/"), ==, 0);
1243 nodes = get_nodes_at (c, "/foo");
1244 g_assert (nodes != NULL);
1245 g_assert_cmpint (g_strv_length (nodes), ==, 2);
1246 g_assert_cmpstr (nodes[0], ==, "boss");
1247 g_assert_cmpstr (nodes[1], ==, "dyna");
1249 g_assert_cmpint (count_interfaces (c, "/foo"), ==, 0);
1251 nodes = get_nodes_at (c, "/foo/boss");
1252 g_assert (nodes != NULL);
1253 g_assert_cmpint (g_strv_length (nodes), ==, 4);
1254 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker1"));
1255 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker2"));
1256 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "interns"));
1257 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "executives"));
1259 /* any registered object always implement org.freedesktop.DBus.[Peer,Introspectable,Properties] */
1260 g_assert_cmpint (count_interfaces (c, "/foo/boss"), ==, 5);
1261 g_assert (has_interface (c, "/foo/boss", foo_interface_info.name));
1262 g_assert (has_interface (c, "/foo/boss", bar_interface_info.name));
1264 /* check subtree nodes - we should have only non_subtree_object in /foo/boss/executives
1265 * because data.num_subtree_nodes is 0
1267 nodes = get_nodes_at (c, "/foo/boss/executives");
1268 g_assert (nodes != NULL);
1269 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1270 g_assert_cmpint (g_strv_length (nodes), ==, 1);
1272 g_assert_cmpint (count_interfaces (c, "/foo/boss/executives"), ==, 0);
1274 /* now change data.num_subtree_nodes and check */
1275 data.num_subtree_nodes = 2;
1276 nodes = get_nodes_at (c, "/foo/boss/executives");
1277 g_assert (nodes != NULL);
1278 g_assert_cmpint (g_strv_length (nodes), ==, 5);
1279 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1280 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp0"));
1281 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp1"));
1282 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp0"));
1283 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp1"));
1284 /* check that /foo/boss/executives/non_subtree_object is not handled by the
1285 * subtree handlers - we can do this because objects from subtree handlers
1286 * has exactly one interface and non_subtree_object has two
1288 g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/non_subtree_object"), ==, 5);
1289 g_assert (has_interface (c, "/foo/boss/executives/non_subtree_object", foo_interface_info.name));
1290 g_assert (has_interface (c, "/foo/boss/executives/non_subtree_object", bar_interface_info.name));
1291 /* check that the vp and evp objects are handled by the subtree handlers */
1292 g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/vp0"), ==, 4);
1293 g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/vp1"), ==, 4);
1294 g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/evp0"), ==, 4);
1295 g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/evp1"), ==, 4);
1296 g_assert (has_interface (c, "/foo/boss/executives/vp0", foo_interface_info.name));
1297 g_assert (has_interface (c, "/foo/boss/executives/vp1", foo_interface_info.name));
1298 g_assert (has_interface (c, "/foo/boss/executives/evp0", bar_interface_info.name));
1299 g_assert (has_interface (c, "/foo/boss/executives/evp1", bar_interface_info.name));
1301 data.num_subtree_nodes = 3;
1302 nodes = get_nodes_at (c, "/foo/boss/executives");
1303 g_assert (nodes != NULL);
1304 g_assert_cmpint (g_strv_length (nodes), ==, 7);
1305 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1306 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp0"));
1307 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp1"));
1308 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp2"));
1309 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp0"));
1310 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp1"));
1311 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp2"));
1314 /* check that calls are properly dispatched to the functions in foo_vtable for objects
1315 * implementing the org.example.Foo interface
1317 * We do this for both a regular registered object (/foo/boss) and also for an object
1318 * registered through the subtree mechanism.
1320 test_dispatch ("/foo/boss");
1321 test_dispatch ("/foo/boss/executives/vp0");
1323 /* To prevent from exiting and attaching a D-Bus tool like D-Feet; uncomment: */
1325 g_debug ("Point D-feet or other tool at: %s", session_bus_get_temporary_address());
1326 g_main_loop_run (loop);
1329 /* check that unregistering the subtree handler works */
1330 g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 1);
1331 g_assert (g_dbus_connection_unregister_subtree (c, subtree_registration_id));
1332 g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 2);
1333 nodes = get_nodes_at (c, "/foo/boss/executives");
1334 g_assert (nodes != NULL);
1335 g_assert_cmpint (g_strv_length (nodes), ==, 1);
1336 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1339 g_assert (g_dbus_connection_unregister_object (c, boss_foo_reg_id));
1340 g_assert (g_dbus_connection_unregister_object (c, boss_bar_reg_id));
1341 g_assert (g_dbus_connection_unregister_object (c, worker1_foo_reg_id));
1342 g_assert (g_dbus_connection_unregister_object (c, worker2_bar_reg_id));
1343 g_assert (g_dbus_connection_unregister_object (c, intern1_foo_reg_id));
1344 g_assert (g_dbus_connection_unregister_object (c, intern2_bar_reg_id));
1345 g_assert (g_dbus_connection_unregister_object (c, intern2_foo_reg_id));
1346 g_assert (g_dbus_connection_unregister_object (c, intern3_bar_reg_id));
1347 g_assert (g_dbus_connection_unregister_object (c, non_subtree_object_path_bar_reg_id));
1348 g_assert (g_dbus_connection_unregister_object (c, non_subtree_object_path_foo_reg_id));
1350 g_assert_cmpint (data.num_unregistered_calls, ==, num_successful_registrations);
1352 /* check that we no longer export any objects - TODO: it looks like there's a bug in
1353 * libdbus-1 here: libdbus still reports the '/foo' object; so disable the test for now
1356 nodes = get_nodes_at (c, "/");
1357 g_assert (nodes != NULL);
1358 g_assert_cmpint (g_strv_length (nodes), ==, 0);
1366 /* ---------------------------------------------------------------------------------------------------- */
1375 g_test_init (&argc, &argv, NULL);
1377 /* all the tests rely on a shared main loop */
1378 loop = g_main_loop_new (NULL, FALSE);
1380 /* all the tests use a session bus with a well-known address that we can bring up and down
1381 * using session_bus_up() and session_bus_down().
1383 g_unsetenv ("DISPLAY");
1384 g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
1388 /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
1389 * until one can connect to the bus but that's not how things work right now
1391 usleep (500 * 1000);
1393 g_test_add_func ("/gdbus/object-registration", test_object_registration);
1394 /* TODO: check that we spit out correct introspection data */
1395 /* TODO: check that registering a whole subtree works */
1400 session_bus_down ();