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,
347 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
348 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
350 g_dbus_connection_get_unique_name (c),
352 "org.freedesktop.DBus.Introspectable",
355 g_assert_no_error (error);
356 g_assert (proxy != NULL);
358 /* do this async to avoid libdbus-1 deadlocks */
360 g_dbus_proxy_call (proxy,
363 G_DBUS_CALL_FLAGS_NONE,
366 (GAsyncReadyCallback) introspect_callback,
368 g_main_loop_run (loop);
369 g_assert (xml_data != NULL);
371 node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
372 g_assert_no_error (error);
373 g_assert (node_info != NULL);
375 p = g_ptr_array_new ();
376 for (n = 0; node_info->nodes != NULL && node_info->nodes[n] != NULL; n++)
378 const GDBusNodeInfo *sub_node_info = node_info->nodes[n];
379 g_ptr_array_add (p, g_strdup (sub_node_info->path));
381 g_ptr_array_add (p, NULL);
383 g_object_unref (proxy);
385 g_dbus_node_info_unref (node_info);
387 return (gchar **) g_ptr_array_free (p, FALSE);
391 has_interface (GDBusConnection *c,
392 const gchar *object_path,
393 const gchar *interface_name)
398 GDBusNodeInfo *node_info;
402 proxy = g_dbus_proxy_new_sync (c,
404 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
405 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
407 g_dbus_connection_get_unique_name (c),
409 "org.freedesktop.DBus.Introspectable",
412 g_assert_no_error (error);
413 g_assert (proxy != NULL);
415 /* do this async to avoid libdbus-1 deadlocks */
417 g_dbus_proxy_call (proxy,
420 G_DBUS_CALL_FLAGS_NONE,
423 (GAsyncReadyCallback) introspect_callback,
425 g_main_loop_run (loop);
426 g_assert (xml_data != NULL);
428 node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
429 g_assert_no_error (error);
430 g_assert (node_info != NULL);
432 ret = (g_dbus_node_info_lookup_interface (node_info, interface_name) != NULL);
434 g_object_unref (proxy);
436 g_dbus_node_info_unref (node_info);
442 count_interfaces (GDBusConnection *c,
443 const gchar *object_path)
448 GDBusNodeInfo *node_info;
452 proxy = g_dbus_proxy_new_sync (c,
454 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
455 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
457 g_dbus_connection_get_unique_name (c),
459 "org.freedesktop.DBus.Introspectable",
462 g_assert_no_error (error);
463 g_assert (proxy != NULL);
465 /* do this async to avoid libdbus-1 deadlocks */
467 g_dbus_proxy_call (proxy,
470 G_DBUS_CALL_FLAGS_NONE,
473 (GAsyncReadyCallback) introspect_callback,
475 g_main_loop_run (loop);
476 g_assert (xml_data != NULL);
478 node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
479 g_assert_no_error (error);
480 g_assert (node_info != NULL);
483 while (node_info->interfaces != NULL && node_info->interfaces[ret] != NULL)
486 g_object_unref (proxy);
488 g_dbus_node_info_unref (node_info);
494 dyna_create_callback (GDBusProxy *proxy,
502 result = g_dbus_proxy_call_finish (proxy,
505 g_assert_no_error (error);
506 g_assert (result != NULL);
507 g_variant_unref (result);
509 g_main_loop_quit (loop);
512 /* Dynamically create @object_name under /foo/dyna */
514 dyna_create (GDBusConnection *c,
515 const gchar *object_name)
521 object_path = g_strconcat ("/foo/dyna/", object_name, NULL);
524 proxy = g_dbus_proxy_new_sync (c,
526 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
527 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
529 g_dbus_connection_get_unique_name (c),
534 g_assert_no_error (error);
535 g_assert (proxy != NULL);
537 /* do this async to avoid libdbus-1 deadlocks */
538 g_dbus_proxy_call (proxy,
540 g_variant_new ("()"),
541 G_DBUS_CALL_FLAGS_NONE,
544 (GAsyncReadyCallback) dyna_create_callback,
546 g_main_loop_run (loop);
548 g_assert_no_error (error);
550 g_object_unref (proxy);
551 g_free (object_path);
558 guint num_unregistered_calls;
559 guint num_unregistered_subtree_calls;
560 guint num_subtree_nodes;
561 } ObjectRegistrationData;
564 on_object_unregistered (gpointer user_data)
566 ObjectRegistrationData *data = user_data;
568 data->num_unregistered_calls++;
572 on_subtree_unregistered (gpointer user_data)
574 ObjectRegistrationData *data = user_data;
576 data->num_unregistered_subtree_calls++;
580 _g_strv_has_string (const gchar* const * haystack,
585 for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
587 if (g_strcmp0 (haystack[n], needle) == 0)
593 /* -------------------- */
596 subtree_enumerate (GDBusConnection *connection,
598 const gchar *object_path,
601 ObjectRegistrationData *data = user_data;
606 p = g_ptr_array_new ();
608 for (n = 0; n < data->num_subtree_nodes; n++)
610 g_ptr_array_add (p, g_strdup_printf ("vp%d", n));
611 g_ptr_array_add (p, g_strdup_printf ("evp%d", n));
613 g_ptr_array_add (p, NULL);
615 nodes = (gchar **) g_ptr_array_free (p, FALSE);
620 /* Only allows certain objects, and aborts on unknowns */
622 subtree_introspect (GDBusConnection *connection,
624 const gchar *object_path,
628 GPtrArray *interfaces;
630 /* VPs implement the Foo interface, EVPs implement the Bar interface. The root
631 * does not implement any interfaces
633 interfaces = g_ptr_array_new ();
634 if (g_str_has_prefix (node, "vp"))
636 g_ptr_array_add (interfaces, (gpointer) &foo_interface_info);
638 else if (g_str_has_prefix (node, "evp"))
640 g_ptr_array_add (interfaces, (gpointer) &bar_interface_info);
642 else if (g_strcmp0 (node, "/") == 0)
648 g_assert_not_reached ();
654 static const GDBusInterfaceVTable *
655 subtree_dispatch (GDBusConnection *connection,
657 const gchar *object_path,
658 const gchar *interface_name,
660 gpointer *out_user_data,
663 if (g_strcmp0 (interface_name, "org.example.Foo") == 0)
669 static const GDBusSubtreeVTable subtree_vtable =
676 /* -------------------- */
679 dynamic_subtree_enumerate (GDBusConnection *connection,
681 const gchar *object_path,
684 GPtrArray *data = user_data;
685 gchar **nodes = g_new (gchar*, data->len + 1);
688 for (n = 0; n < data->len; n++)
690 nodes[n] = g_strdup (g_ptr_array_index (data, n));
692 nodes[data->len] = NULL;
697 /* Allow all objects to be introspected */
699 dynamic_subtree_introspect (GDBusConnection *connection,
701 const gchar *object_path,
705 GPtrArray *interfaces;
707 /* All nodes (including the root node) implements the Dyna interface */
708 interfaces = g_ptr_array_new ();
709 g_ptr_array_add (interfaces, (gpointer) &dyna_interface_info);
714 static const GDBusInterfaceVTable *
715 dynamic_subtree_dispatch (GDBusConnection *connection,
717 const gchar *object_path,
718 const gchar *interface_name,
720 gpointer *out_user_data,
723 *out_user_data = user_data;
724 return &dyna_interface_vtable;
727 static const GDBusSubtreeVTable dynamic_subtree_vtable =
729 dynamic_subtree_enumerate,
730 dynamic_subtree_introspect,
731 dynamic_subtree_dispatch
734 /* -------------------- */
737 test_dispatch_thread_func (gpointer user_data)
739 const gchar *object_path = user_data;
740 GDBusProxy *foo_proxy;
745 const gchar *value_str;
747 foo_proxy = g_dbus_proxy_new_sync (c,
749 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS |
750 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
752 g_dbus_connection_get_unique_name (c),
757 g_assert (foo_proxy != NULL);
759 /* generic interfaces */
761 value = g_dbus_proxy_call_sync (foo_proxy,
762 "org.freedesktop.DBus.Peer.Ping",
764 G_DBUS_CALL_FLAGS_NONE,
768 g_assert_no_error (error);
769 g_assert (value != NULL);
770 g_variant_unref (value);
774 value = g_dbus_proxy_call_sync (foo_proxy,
776 g_variant_new ("(s)", "winwinwin"),
777 G_DBUS_CALL_FLAGS_NONE,
781 g_assert_no_error (error);
782 g_assert (value != NULL);
783 g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("(s)")));
784 g_variant_get (value, "(&s)", &value_str);
785 g_assert_cmpstr (value_str, ==, "You passed the string `winwinwin'. Jolly good!");
786 g_variant_unref (value);
789 value = g_dbus_proxy_call_sync (foo_proxy,
792 G_DBUS_CALL_FLAGS_NONE,
796 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
797 g_assert_cmpstr (error->message, ==, "GDBus.Error:org.example.SomeError: How do you like them apples, buddy!");
798 g_error_free (error);
799 g_assert (value == NULL);
802 value = g_dbus_proxy_call_sync (foo_proxy,
804 g_variant_new ("(s)", "failfailfail"),
805 G_DBUS_CALL_FLAGS_NONE,
809 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
810 g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Type of message, `(s)', does not match expected type `()'");
811 g_error_free (error);
812 g_assert (value == NULL);
815 value = g_dbus_proxy_call_sync (foo_proxy,
818 G_DBUS_CALL_FLAGS_NONE,
822 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
823 g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod: No such method `NonExistantMethod'");
824 g_error_free (error);
825 g_assert (value == NULL);
828 value = g_dbus_proxy_call_sync (foo_proxy,
829 "org.example.FooXYZ.NonExistant",
831 G_DBUS_CALL_FLAGS_NONE,
835 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
836 g_error_free (error);
837 g_assert (value == NULL);
839 /* user properties */
841 value = g_dbus_proxy_call_sync (foo_proxy,
842 "org.freedesktop.DBus.Properties.Get",
843 g_variant_new ("(ss)",
846 G_DBUS_CALL_FLAGS_NONE,
850 g_assert_no_error (error);
851 g_assert (value != NULL);
852 g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("(v)")));
853 g_variant_get (value, "(v)", &inner);
854 g_assert (g_variant_is_of_type (inner, G_VARIANT_TYPE_STRING));
855 g_assert_cmpstr (g_variant_get_string (inner, NULL), ==, "Property `PropertyUno' Is What It Is!");
856 g_variant_unref (value);
859 value = g_dbus_proxy_call_sync (foo_proxy,
860 "org.freedesktop.DBus.Properties.Get",
861 g_variant_new ("(ss)",
864 G_DBUS_CALL_FLAGS_NONE,
868 g_assert (value == NULL);
869 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
870 g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: No such property `ThisDoesntExist'");
871 g_error_free (error);
874 value = g_dbus_proxy_call_sync (foo_proxy,
875 "org.freedesktop.DBus.Properties.Get",
876 g_variant_new ("(ss)",
879 G_DBUS_CALL_FLAGS_NONE,
883 g_assert (value == NULL);
884 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
885 g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Property `NotReadable' is not readable");
886 g_error_free (error);
889 value = g_dbus_proxy_call_sync (foo_proxy,
890 "org.freedesktop.DBus.Properties.Set",
891 g_variant_new ("(ssv)",
894 g_variant_new_string ("But Writable you are!")),
895 G_DBUS_CALL_FLAGS_NONE,
899 g_assert (value == NULL);
900 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_SPAWN_FILE_INVALID);
901 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!''");
902 g_error_free (error);
905 value = g_dbus_proxy_call_sync (foo_proxy,
906 "org.freedesktop.DBus.Properties.Set",
907 g_variant_new ("(ssv)",
910 g_variant_new_uint32 (42)),
911 G_DBUS_CALL_FLAGS_NONE,
915 g_assert (value == NULL);
916 g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
917 g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Property `NotWritable' is not writable");
918 g_error_free (error);
921 value = g_dbus_proxy_call_sync (foo_proxy,
922 "org.freedesktop.DBus.Properties.GetAll",
923 g_variant_new ("(s)",
925 G_DBUS_CALL_FLAGS_NONE,
929 g_assert_no_error (error);
930 g_assert (value != NULL);
931 g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("(a{sv})")));
932 s = g_variant_print (value, TRUE);
933 g_assert_cmpstr (s, ==, "({'PropertyUno': <\"Property `PropertyUno' Is What It Is!\">, 'NotWritable': <\"Property `NotWritable' Is What It Is!\">},)");
935 g_variant_unref (value);
937 g_object_unref (foo_proxy);
939 g_main_loop_quit (loop);
944 test_dispatch (const gchar *object_path)
949 /* run this in a thread to avoid deadlocks */
951 thread = g_thread_create (test_dispatch_thread_func,
952 (gpointer) object_path,
955 g_assert_no_error (error);
956 g_assert (thread != NULL);
957 g_main_loop_run (loop);
958 g_thread_join (thread);
962 test_object_registration (void)
965 ObjectRegistrationData data;
966 GPtrArray *dyna_data;
968 guint boss_foo_reg_id;
969 guint boss_bar_reg_id;
970 guint worker1_foo_reg_id;
971 guint worker2_bar_reg_id;
972 guint intern1_foo_reg_id;
973 guint intern2_bar_reg_id;
974 guint intern2_foo_reg_id;
975 guint intern3_bar_reg_id;
976 guint registration_id;
977 guint subtree_registration_id;
978 guint non_subtree_object_path_foo_reg_id;
979 guint non_subtree_object_path_bar_reg_id;
980 guint dyna_subtree_registration_id;
981 guint num_successful_registrations;
983 data.num_unregistered_calls = 0;
984 data.num_unregistered_subtree_calls = 0;
985 data.num_subtree_nodes = 0;
987 num_successful_registrations = 0;
990 c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
991 g_assert_no_error (error);
992 g_assert (c != NULL);
994 registration_id = g_dbus_connection_register_object (c,
999 on_object_unregistered,
1001 g_assert_no_error (error);
1002 g_assert (registration_id > 0);
1003 boss_foo_reg_id = registration_id;
1004 num_successful_registrations++;
1006 registration_id = g_dbus_connection_register_object (c,
1008 &bar_interface_info,
1011 on_object_unregistered,
1013 g_assert_no_error (error);
1014 g_assert (registration_id > 0);
1015 boss_bar_reg_id = registration_id;
1016 num_successful_registrations++;
1018 registration_id = g_dbus_connection_register_object (c,
1019 "/foo/boss/worker1",
1020 &foo_interface_info,
1023 on_object_unregistered,
1025 g_assert_no_error (error);
1026 g_assert (registration_id > 0);
1027 worker1_foo_reg_id = registration_id;
1028 num_successful_registrations++;
1030 registration_id = g_dbus_connection_register_object (c,
1031 "/foo/boss/worker2",
1032 &bar_interface_info,
1035 on_object_unregistered,
1037 g_assert_no_error (error);
1038 g_assert (registration_id > 0);
1039 worker2_bar_reg_id = registration_id;
1040 num_successful_registrations++;
1042 registration_id = g_dbus_connection_register_object (c,
1043 "/foo/boss/interns/intern1",
1044 &foo_interface_info,
1047 on_object_unregistered,
1049 g_assert_no_error (error);
1050 g_assert (registration_id > 0);
1051 intern1_foo_reg_id = registration_id;
1052 num_successful_registrations++;
1054 /* ... and try again at another path */
1055 registration_id = g_dbus_connection_register_object (c,
1056 "/foo/boss/interns/intern2",
1057 &bar_interface_info,
1060 on_object_unregistered,
1062 g_assert_no_error (error);
1063 g_assert (registration_id > 0);
1064 intern2_bar_reg_id = registration_id;
1065 num_successful_registrations++;
1067 /* register at the same path/interface - this should fail */
1068 registration_id = g_dbus_connection_register_object (c,
1069 "/foo/boss/interns/intern2",
1070 &bar_interface_info,
1073 on_object_unregistered,
1075 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
1076 g_assert (!g_dbus_error_is_remote_error (error));
1077 g_error_free (error);
1079 g_assert (registration_id == 0);
1081 /* register at different interface - shouldn't fail */
1082 registration_id = g_dbus_connection_register_object (c,
1083 "/foo/boss/interns/intern2",
1084 &foo_interface_info,
1087 on_object_unregistered,
1089 g_assert_no_error (error);
1090 g_assert (registration_id > 0);
1091 intern2_foo_reg_id = registration_id;
1092 num_successful_registrations++;
1094 /* unregister it via the id */
1095 g_assert (g_dbus_connection_unregister_object (c, registration_id));
1096 g_assert_cmpint (data.num_unregistered_calls, ==, 1);
1097 intern2_foo_reg_id = 0;
1099 /* register it back */
1100 registration_id = g_dbus_connection_register_object (c,
1101 "/foo/boss/interns/intern2",
1102 &foo_interface_info,
1105 on_object_unregistered,
1107 g_assert_no_error (error);
1108 g_assert (registration_id > 0);
1109 intern2_foo_reg_id = registration_id;
1110 num_successful_registrations++;
1112 registration_id = g_dbus_connection_register_object (c,
1113 "/foo/boss/interns/intern3",
1114 &bar_interface_info,
1117 on_object_unregistered,
1119 g_assert_no_error (error);
1120 g_assert (registration_id > 0);
1121 intern3_bar_reg_id = registration_id;
1122 num_successful_registrations++;
1124 /* now register a whole subtree at /foo/boss/executives */
1125 subtree_registration_id = g_dbus_connection_register_subtree (c,
1126 "/foo/boss/executives",
1128 G_DBUS_SUBTREE_FLAGS_NONE,
1130 on_subtree_unregistered,
1132 g_assert_no_error (error);
1133 g_assert (subtree_registration_id > 0);
1134 /* try registering it again.. this should fail */
1135 registration_id = g_dbus_connection_register_subtree (c,
1136 "/foo/boss/executives",
1138 G_DBUS_SUBTREE_FLAGS_NONE,
1140 on_subtree_unregistered,
1142 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
1143 g_assert (!g_dbus_error_is_remote_error (error));
1144 g_error_free (error);
1146 g_assert (registration_id == 0);
1148 /* unregister it, then register it again */
1149 g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 0);
1150 g_assert (g_dbus_connection_unregister_subtree (c, subtree_registration_id));
1151 g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 1);
1152 subtree_registration_id = g_dbus_connection_register_subtree (c,
1153 "/foo/boss/executives",
1155 G_DBUS_SUBTREE_FLAGS_NONE,
1157 on_subtree_unregistered,
1159 g_assert_no_error (error);
1160 g_assert (subtree_registration_id > 0);
1162 /* try to register something under /foo/boss/executives - this should work
1163 * because registered subtrees and registered objects can coexist.
1165 * Make the exported object implement *two* interfaces so we can check
1166 * that the right introspection handler is invoked.
1168 registration_id = g_dbus_connection_register_object (c,
1169 "/foo/boss/executives/non_subtree_object",
1170 &bar_interface_info,
1173 on_object_unregistered,
1175 g_assert_no_error (error);
1176 g_assert (registration_id > 0);
1177 non_subtree_object_path_bar_reg_id = registration_id;
1178 num_successful_registrations++;
1179 registration_id = g_dbus_connection_register_object (c,
1180 "/foo/boss/executives/non_subtree_object",
1181 &foo_interface_info,
1184 on_object_unregistered,
1186 g_assert_no_error (error);
1187 g_assert (registration_id > 0);
1188 non_subtree_object_path_foo_reg_id = registration_id;
1189 num_successful_registrations++;
1191 /* now register a dynamic subtree, spawning objects as they are called */
1192 dyna_data = g_ptr_array_new ();
1193 dyna_subtree_registration_id = g_dbus_connection_register_subtree (c,
1195 &dynamic_subtree_vtable,
1196 G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES,
1198 (GDestroyNotify)g_ptr_array_unref,
1200 g_assert_no_error (error);
1201 g_assert (dyna_subtree_registration_id > 0);
1203 /* First assert that we have no nodes in the dynamic subtree */
1204 nodes = get_nodes_at (c, "/foo/dyna");
1205 g_assert (nodes != NULL);
1206 g_assert_cmpint (g_strv_length (nodes), ==, 0);
1208 g_assert_cmpint (count_interfaces (c, "/foo/dyna"), ==, 4);
1210 /* Install three nodes in the dynamic subtree via the dyna_data backdoor and
1211 * assert that they show up correctly in the introspection data */
1212 g_ptr_array_add (dyna_data, "lol");
1213 g_ptr_array_add (dyna_data, "cat");
1214 g_ptr_array_add (dyna_data, "cheezburger");
1215 nodes = get_nodes_at (c, "/foo/dyna");
1216 g_assert (nodes != NULL);
1217 g_assert_cmpint (g_strv_length (nodes), ==, 3);
1218 g_assert_cmpstr (nodes[0], ==, "lol");
1219 g_assert_cmpstr (nodes[1], ==, "cat");
1220 g_assert_cmpstr (nodes[2], ==, "cheezburger");
1222 g_assert_cmpint (count_interfaces (c, "/foo/dyna/lol"), ==, 4);
1223 g_assert_cmpint (count_interfaces (c, "/foo/dyna/cat"), ==, 4);
1224 g_assert_cmpint (count_interfaces (c, "/foo/dyna/cheezburger"), ==, 4);
1226 /* Call a non-existing object path and assert that it has been created */
1227 dyna_create (c, "dynamicallycreated");
1228 nodes = get_nodes_at (c, "/foo/dyna");
1229 g_assert (nodes != NULL);
1230 g_assert_cmpint (g_strv_length (nodes), ==, 4);
1231 g_assert_cmpstr (nodes[0], ==, "lol");
1232 g_assert_cmpstr (nodes[1], ==, "cat");
1233 g_assert_cmpstr (nodes[2], ==, "cheezburger");
1234 g_assert_cmpstr (nodes[3], ==, "dynamicallycreated");
1236 g_assert_cmpint (count_interfaces (c, "/foo/dyna/dynamicallycreated"), ==, 4);
1238 /* now check that the object hierarachy is properly generated... yes, it's a bit
1239 * perverse that we round-trip to the bus to introspect ourselves ;-)
1241 nodes = get_nodes_at (c, "/");
1242 g_assert (nodes != NULL);
1243 g_assert_cmpint (g_strv_length (nodes), ==, 1);
1244 g_assert_cmpstr (nodes[0], ==, "foo");
1246 g_assert_cmpint (count_interfaces (c, "/"), ==, 0);
1248 nodes = get_nodes_at (c, "/foo");
1249 g_assert (nodes != NULL);
1250 g_assert_cmpint (g_strv_length (nodes), ==, 2);
1251 g_assert_cmpstr (nodes[0], ==, "boss");
1252 g_assert_cmpstr (nodes[1], ==, "dyna");
1254 g_assert_cmpint (count_interfaces (c, "/foo"), ==, 0);
1256 nodes = get_nodes_at (c, "/foo/boss");
1257 g_assert (nodes != NULL);
1258 g_assert_cmpint (g_strv_length (nodes), ==, 4);
1259 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker1"));
1260 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker2"));
1261 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "interns"));
1262 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "executives"));
1264 /* any registered object always implement org.freedesktop.DBus.[Peer,Introspectable,Properties] */
1265 g_assert_cmpint (count_interfaces (c, "/foo/boss"), ==, 5);
1266 g_assert (has_interface (c, "/foo/boss", foo_interface_info.name));
1267 g_assert (has_interface (c, "/foo/boss", bar_interface_info.name));
1269 /* check subtree nodes - we should have only non_subtree_object in /foo/boss/executives
1270 * because data.num_subtree_nodes is 0
1272 nodes = get_nodes_at (c, "/foo/boss/executives");
1273 g_assert (nodes != NULL);
1274 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1275 g_assert_cmpint (g_strv_length (nodes), ==, 1);
1277 g_assert_cmpint (count_interfaces (c, "/foo/boss/executives"), ==, 0);
1279 /* now change data.num_subtree_nodes and check */
1280 data.num_subtree_nodes = 2;
1281 nodes = get_nodes_at (c, "/foo/boss/executives");
1282 g_assert (nodes != NULL);
1283 g_assert_cmpint (g_strv_length (nodes), ==, 5);
1284 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1285 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp0"));
1286 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp1"));
1287 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp0"));
1288 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp1"));
1289 /* check that /foo/boss/executives/non_subtree_object is not handled by the
1290 * subtree handlers - we can do this because objects from subtree handlers
1291 * has exactly one interface and non_subtree_object has two
1293 g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/non_subtree_object"), ==, 5);
1294 g_assert (has_interface (c, "/foo/boss/executives/non_subtree_object", foo_interface_info.name));
1295 g_assert (has_interface (c, "/foo/boss/executives/non_subtree_object", bar_interface_info.name));
1296 /* check that the vp and evp objects are handled by the subtree handlers */
1297 g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/vp0"), ==, 4);
1298 g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/vp1"), ==, 4);
1299 g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/evp0"), ==, 4);
1300 g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/evp1"), ==, 4);
1301 g_assert (has_interface (c, "/foo/boss/executives/vp0", foo_interface_info.name));
1302 g_assert (has_interface (c, "/foo/boss/executives/vp1", foo_interface_info.name));
1303 g_assert (has_interface (c, "/foo/boss/executives/evp0", bar_interface_info.name));
1304 g_assert (has_interface (c, "/foo/boss/executives/evp1", bar_interface_info.name));
1306 data.num_subtree_nodes = 3;
1307 nodes = get_nodes_at (c, "/foo/boss/executives");
1308 g_assert (nodes != NULL);
1309 g_assert_cmpint (g_strv_length (nodes), ==, 7);
1310 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1311 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp0"));
1312 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp1"));
1313 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp2"));
1314 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp0"));
1315 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp1"));
1316 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp2"));
1319 /* check that calls are properly dispatched to the functions in foo_vtable for objects
1320 * implementing the org.example.Foo interface
1322 * We do this for both a regular registered object (/foo/boss) and also for an object
1323 * registered through the subtree mechanism.
1325 test_dispatch ("/foo/boss");
1326 test_dispatch ("/foo/boss/executives/vp0");
1328 /* To prevent from exiting and attaching a D-Bus tool like D-Feet; uncomment: */
1330 g_debug ("Point D-feet or other tool at: %s", session_bus_get_temporary_address());
1331 g_main_loop_run (loop);
1334 /* check that unregistering the subtree handler works */
1335 g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 1);
1336 g_assert (g_dbus_connection_unregister_subtree (c, subtree_registration_id));
1337 g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 2);
1338 nodes = get_nodes_at (c, "/foo/boss/executives");
1339 g_assert (nodes != NULL);
1340 g_assert_cmpint (g_strv_length (nodes), ==, 1);
1341 g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1344 g_assert (g_dbus_connection_unregister_object (c, boss_foo_reg_id));
1345 g_assert (g_dbus_connection_unregister_object (c, boss_bar_reg_id));
1346 g_assert (g_dbus_connection_unregister_object (c, worker1_foo_reg_id));
1347 g_assert (g_dbus_connection_unregister_object (c, worker2_bar_reg_id));
1348 g_assert (g_dbus_connection_unregister_object (c, intern1_foo_reg_id));
1349 g_assert (g_dbus_connection_unregister_object (c, intern2_bar_reg_id));
1350 g_assert (g_dbus_connection_unregister_object (c, intern2_foo_reg_id));
1351 g_assert (g_dbus_connection_unregister_object (c, intern3_bar_reg_id));
1352 g_assert (g_dbus_connection_unregister_object (c, non_subtree_object_path_bar_reg_id));
1353 g_assert (g_dbus_connection_unregister_object (c, non_subtree_object_path_foo_reg_id));
1355 g_assert_cmpint (data.num_unregistered_calls, ==, num_successful_registrations);
1357 /* check that we no longer export any objects - TODO: it looks like there's a bug in
1358 * libdbus-1 here: libdbus still reports the '/foo' object; so disable the test for now
1361 nodes = get_nodes_at (c, "/");
1362 g_assert (nodes != NULL);
1363 g_assert_cmpint (g_strv_length (nodes), ==, 0);
1371 /* ---------------------------------------------------------------------------------------------------- */
1380 g_test_init (&argc, &argv, NULL);
1382 /* all the tests rely on a shared main loop */
1383 loop = g_main_loop_new (NULL, FALSE);
1385 /* all the tests use a session bus with a well-known address that we can bring up and down
1386 * using session_bus_up() and session_bus_down().
1388 g_unsetenv ("DISPLAY");
1389 g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
1393 /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
1394 * until one can connect to the bus but that's not how things work right now
1396 usleep (500 * 1000);
1398 g_test_add_func ("/gdbus/object-registration", test_object_registration);
1399 /* TODO: check that we spit out correct introspection data */
1400 /* TODO: check that registering a whole subtree works */
1405 session_bus_down ();