1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-object-tree.c DBusObjectTree (internals of DBusConnection)
4 * Copyright (C) 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "dbus-object-tree.h"
24 #include "dbus-connection-internal.h"
25 #include "dbus-internals.h"
26 #include "dbus-hash.h"
27 #include "dbus-protocol.h"
28 #include "dbus-string.h"
33 * @defgroup DBusObjectTree A hierarchy of objects with container-contained relationship
34 * @ingroup DBusInternals
35 * @brief DBusObjectTree is used by DBusConnection to track the object tree
37 * Types and functions related to DBusObjectTree. These
38 * are all library-internal.
43 /** Subnode of the object hierarchy */
44 typedef struct DBusObjectSubtree DBusObjectSubtree;
46 static DBusObjectSubtree* _dbus_object_subtree_new (const char *name,
47 const DBusObjectPathVTable *vtable,
49 static void _dbus_object_subtree_ref (DBusObjectSubtree *subtree);
50 static void _dbus_object_subtree_unref (DBusObjectSubtree *subtree);
53 * Internals of DBusObjectTree
57 int refcount; /**< Reference count */
58 DBusConnection *connection; /**< Connection this tree belongs to */
60 DBusObjectSubtree *root; /**< Root of the tree ("/" node) */
64 * Struct representing a single registered subtree handler, or node
65 * that's a parent of a registered subtree handler. If
66 * message_function != NULL there's actually a handler at this node.
68 struct DBusObjectSubtree
70 DBusAtomic refcount; /**< Reference count */
71 DBusObjectSubtree *parent; /**< Parent node */
72 DBusObjectPathUnregisterFunction unregister_function; /**< Function to call on unregister */
73 DBusObjectPathMessageFunction message_function; /**< Function to handle messages */
74 void *user_data; /**< Data for functions */
75 DBusObjectSubtree **subtrees; /**< Child nodes */
76 int n_subtrees; /**< Number of child nodes */
77 unsigned int subtrees_sorted : 1; /**< Whether children are sorted */
78 unsigned int invoke_as_fallback : 1; /**< Whether to invoke message_function when child nodes don't handle the message */
79 char name[1]; /**< Allocated as large as necessary */
83 * Creates a new object tree, representing a mapping from paths
86 * @param connection the connection this tree belongs to
87 * @returns the new tree or #NULL if no memory
90 _dbus_object_tree_new (DBusConnection *connection)
94 /* the connection passed in here isn't fully constructed,
95 * so don't do anything more than store a pointer to
99 tree = dbus_new0 (DBusObjectTree, 1);
104 tree->connection = connection;
105 tree->root = _dbus_object_subtree_new ("/", NULL, NULL);
106 if (tree->root == NULL)
108 tree->root->invoke_as_fallback = TRUE;
122 * Increment the reference count
123 * @param tree the object tree
126 _dbus_object_tree_ref (DBusObjectTree *tree)
128 _dbus_assert (tree->refcount > 0);
134 * Decrement the reference count
135 * @param tree the object tree
138 _dbus_object_tree_unref (DBusObjectTree *tree)
140 _dbus_assert (tree->refcount > 0);
144 if (tree->refcount == 0)
146 _dbus_object_tree_free_all_unlocked (tree);
153 subtree_cmp (DBusObjectSubtree *subtree_a,
154 DBusObjectSubtree *subtree_b)
156 return strcmp (subtree_a->name, subtree_b->name);
160 subtree_qsort_cmp (const void *a,
163 DBusObjectSubtree **subtree_a_p = (void*) a;
164 DBusObjectSubtree **subtree_b_p = (void*) b;
166 return subtree_cmp (*subtree_a_p, *subtree_b_p);
170 ensure_sorted (DBusObjectSubtree *subtree)
172 if (subtree->subtrees && !subtree->subtrees_sorted)
174 qsort (subtree->subtrees,
176 sizeof (DBusObjectSubtree*),
178 subtree->subtrees_sorted = TRUE;
182 /** Set to 1 to get a bunch of debug spew about finding the
185 #define VERBOSE_FIND 0
187 static DBusObjectSubtree*
188 find_subtree_recurse (DBusObjectSubtree *subtree,
190 dbus_bool_t return_deepest_match,
191 dbus_bool_t create_if_not_found,
192 int *index_in_parent)
196 _dbus_assert (!(return_deepest_match && create_if_not_found));
201 _dbus_verbose (" path exhausted, returning %s\n",
208 _dbus_verbose (" searching children of %s for %s\n",
209 subtree->name, path[0]);
212 ensure_sorted (subtree);
214 /* FIXME we should do a binary search here instead
219 while (i < subtree->n_subtrees)
223 v = strcmp (path[0], subtree->subtrees[i]->name);
226 _dbus_verbose (" %s cmp %s = %d\n",
227 path[0], subtree->subtrees[i]->name,
236 _dbus_verbose (" storing parent index %d\n", i);
238 *index_in_parent = i;
241 if (return_deepest_match)
243 DBusObjectSubtree *next;
245 next = find_subtree_recurse (subtree->subtrees[i],
246 &path[1], return_deepest_match,
247 create_if_not_found, index_in_parent);
249 subtree->invoke_as_fallback)
252 _dbus_verbose (" no deeper match found, returning %s\n",
261 return find_subtree_recurse (subtree->subtrees[i],
262 &path[1], return_deepest_match,
263 create_if_not_found, index_in_parent);
275 _dbus_verbose (" no match found, current tree %s, create_if_not_found = %d\n",
276 subtree->name, create_if_not_found);
279 if (create_if_not_found)
281 DBusObjectSubtree* child;
282 DBusObjectSubtree **new_subtrees;
286 _dbus_verbose (" creating subtree %s\n",
290 child = _dbus_object_subtree_new (path[0],
295 /* FIXME we should do the "double alloc each time" standard thing */
296 new_n_subtrees = subtree->n_subtrees + 1;
297 new_subtrees = dbus_realloc (subtree->subtrees,
298 new_n_subtrees * sizeof (DBusObjectSubtree*));
299 if (new_subtrees == NULL)
301 child->unregister_function = NULL;
302 child->message_function = NULL;
303 _dbus_object_subtree_unref (child);
307 new_subtrees[subtree->n_subtrees] = child;
309 *index_in_parent = subtree->n_subtrees;
310 subtree->subtrees_sorted = FALSE;
311 subtree->n_subtrees = new_n_subtrees;
312 subtree->subtrees = new_subtrees;
314 child->parent = subtree;
316 return find_subtree_recurse (child,
317 &path[1], return_deepest_match,
318 create_if_not_found, index_in_parent);
321 return (return_deepest_match && subtree->invoke_as_fallback) ? subtree : NULL;
324 static DBusObjectSubtree*
325 find_subtree (DBusObjectTree *tree,
327 int *index_in_parent)
329 DBusObjectSubtree *subtree;
332 _dbus_verbose ("Looking for exact registered subtree\n");
335 subtree = find_subtree_recurse (tree->root, path, FALSE, FALSE, index_in_parent);
337 if (subtree && subtree->message_function == NULL)
343 static DBusObjectSubtree*
344 find_handler (DBusObjectTree *tree,
348 _dbus_verbose ("Looking for deepest handler\n");
350 return find_subtree_recurse (tree->root, path, TRUE, FALSE, NULL);
353 static DBusObjectSubtree*
354 ensure_subtree (DBusObjectTree *tree,
358 _dbus_verbose ("Ensuring subtree\n");
360 return find_subtree_recurse (tree->root, path, FALSE, TRUE, NULL);
364 * Registers a new subtree in the global object tree.
366 * @param tree the global object tree
367 * @param fallback #TRUE to handle messages to children of this path
368 * @param path NULL-terminated array of path elements giving path to subtree
369 * @param vtable the vtable used to traverse this subtree
370 * @param user_data user data to pass to methods in the vtable
371 * @returns #FALSE if not enough memory
374 _dbus_object_tree_register (DBusObjectTree *tree,
375 dbus_bool_t fallback,
377 const DBusObjectPathVTable *vtable,
380 DBusObjectSubtree *subtree;
382 _dbus_assert (tree != NULL);
383 _dbus_assert (vtable->message_function != NULL);
384 _dbus_assert (path != NULL);
386 subtree = ensure_subtree (tree, path);
390 #ifndef DBUS_DISABLE_CHECKS
391 if (subtree->message_function != NULL)
393 _dbus_warn ("A handler is already registered for the path starting with path[0] = \"%s\"\n",
394 path[0] ? path[0] : "null");
398 _dbus_assert (subtree->message_function == NULL);
401 subtree->message_function = vtable->message_function;
402 subtree->unregister_function = vtable->unregister_function;
403 subtree->user_data = user_data;
404 subtree->invoke_as_fallback = fallback != FALSE;
410 * Unregisters an object subtree that was registered with the
413 * @param tree the global object tree
414 * @param path path to the subtree (same as the one passed to _dbus_object_tree_register())
417 _dbus_object_tree_unregister_and_unlock (DBusObjectTree *tree,
421 DBusObjectSubtree *subtree;
422 DBusObjectPathUnregisterFunction unregister_function;
424 DBusConnection *connection;
426 _dbus_assert (path != NULL);
428 subtree = find_subtree (tree, path, &i);
430 #ifndef DBUS_DISABLE_CHECKS
433 _dbus_warn ("Attempted to unregister path (path[0] = %s path[1] = %s) which isn't registered\n",
434 path[0] ? path[0] : "null",
435 path[1] ? path[1] : "null");
439 _dbus_assert (subtree != NULL);
442 _dbus_assert (subtree->parent == NULL ||
443 (i >= 0 && subtree->parent->subtrees[i] == subtree));
445 subtree->message_function = NULL;
447 unregister_function = subtree->unregister_function;
448 user_data = subtree->user_data;
450 subtree->unregister_function = NULL;
451 subtree->user_data = NULL;
453 /* If we have no subtrees of our own, remove from
454 * our parent (FIXME could also be more aggressive
455 * and remove our parent if it becomes empty)
457 if (subtree->parent && subtree->n_subtrees == 0)
459 /* assumes a 0-byte memmove is OK */
460 memmove (&subtree->parent->subtrees[i],
461 &subtree->parent->subtrees[i+1],
462 (subtree->parent->n_subtrees - i - 1) *
463 sizeof (subtree->parent->subtrees[0]));
464 subtree->parent->n_subtrees -= 1;
466 subtree->parent = NULL;
468 _dbus_object_subtree_unref (subtree);
472 connection = tree->connection;
474 /* Unlock and call application code */
475 #ifdef DBUS_BUILD_TESTS
479 _dbus_connection_ref_unlocked (connection);
480 _dbus_connection_unlock (connection);
483 if (unregister_function)
484 (* unregister_function) (connection, user_data);
486 #ifdef DBUS_BUILD_TESTS
489 dbus_connection_unref (connection);
493 free_subtree_recurse (DBusConnection *connection,
494 DBusObjectSubtree *subtree)
496 /* Delete them from the end, for slightly
497 * more robustness against odd reentrancy.
499 while (subtree->n_subtrees > 0)
501 DBusObjectSubtree *child;
503 child = subtree->subtrees[subtree->n_subtrees - 1];
504 subtree->subtrees[subtree->n_subtrees - 1] = NULL;
505 subtree->n_subtrees -= 1;
506 child->parent = NULL;
508 free_subtree_recurse (connection, child);
511 /* Call application code */
512 if (subtree->unregister_function)
514 (* subtree->unregister_function) (connection,
516 subtree->message_function = NULL;
517 subtree->unregister_function = NULL;
518 subtree->user_data = NULL;
521 /* Now free ourselves */
522 _dbus_object_subtree_unref (subtree);
526 * Free all the handlers in the tree. Lock on tree's connection
529 * @param tree the object tree
532 _dbus_object_tree_free_all_unlocked (DBusObjectTree *tree)
535 free_subtree_recurse (tree->connection,
541 _dbus_object_tree_list_registered_unlocked (DBusObjectTree *tree,
542 const char **parent_path,
543 char ***child_entries)
545 DBusObjectSubtree *subtree;
548 _dbus_assert (parent_path != NULL);
549 _dbus_assert (child_entries != NULL);
551 *child_entries = NULL;
553 subtree = find_subtree (tree, parent_path, NULL);
556 retval = dbus_new0 (char *, 1);
563 retval = dbus_new0 (char*, subtree->n_subtrees + 1);
567 while (i < subtree->n_subtrees)
569 retval[i] = _dbus_strdup (subtree->subtrees[i]->name);
570 if (retval[i] == NULL)
572 dbus_free_string_array (retval);
582 *child_entries = retval;
583 return retval != NULL;
586 static DBusHandlerResult
587 handle_default_introspect_unlocked (DBusObjectTree *tree,
588 DBusMessage *message,
592 DBusHandlerResult result;
596 if (!dbus_message_is_method_call (message,
597 DBUS_INTERFACE_ORG_FREEDESKTOP_INTROSPECTABLE,
599 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
601 if (!_dbus_string_init (&xml))
602 return DBUS_HANDLER_RESULT_NEED_MEMORY;
604 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
607 if (!_dbus_object_tree_list_registered_unlocked (tree, path, &children))
610 if (!_dbus_string_append (&xml, "<node>\n"))
614 while (children[i] != NULL)
616 if (!_dbus_string_append_printf (&xml, " <node name=\"%s\"/>\n",
623 if (!_dbus_string_append (&xml, "</node>\n"))
626 result = DBUS_HANDLER_RESULT_HANDLED;
629 _dbus_string_free (&xml);
630 dbus_free_string_array (children);
636 * Tries to dispatch a message by directing it to handler for the
637 * object path listed in the message header, if any. Messages are
638 * dispatched first to the registered handler that matches the largest
639 * number of path elements; that is, message to /foo/bar/baz would go
640 * to the handler for /foo/bar before the one for /foo.
642 * @todo thread problems
644 * @param tree the global object tree
645 * @param message the message to dispatch
646 * @returns whether message was handled successfully
649 _dbus_object_tree_dispatch_and_unlock (DBusObjectTree *tree,
650 DBusMessage *message)
655 DBusHandlerResult result;
656 DBusObjectSubtree *subtree;
659 _dbus_verbose ("Dispatch of message by object path\n");
663 if (!dbus_message_get_path_decomposed (message, &path))
665 #ifdef DBUS_BUILD_TESTS
666 if (tree->connection)
668 _dbus_connection_unlock (tree->connection);
670 _dbus_verbose ("No memory to get decomposed path\n");
672 return DBUS_HANDLER_RESULT_NEED_MEMORY;
677 #ifdef DBUS_BUILD_TESTS
678 if (tree->connection)
680 _dbus_connection_unlock (tree->connection);
682 _dbus_verbose ("No path field in message\n");
683 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
686 /* Find the deepest path that covers the path in the message */
687 subtree = find_handler (tree, (const char**) path);
689 /* Build a list of all paths that cover the path in the message */
693 while (subtree != NULL)
695 if (subtree->message_function != NULL)
697 _dbus_object_subtree_ref (subtree);
699 /* run deepest paths first */
700 if (!_dbus_list_append (&list, subtree))
702 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
703 _dbus_object_subtree_unref (subtree);
704 goto free_and_return;
708 subtree = subtree->parent;
711 _dbus_verbose ("%d handlers in the path tree for this message\n",
712 _dbus_list_get_length (&list));
714 /* Invoke each handler in the list */
716 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
718 link = _dbus_list_get_first_link (&list);
721 DBusList *next = _dbus_list_get_next_link (&list, link);
722 subtree = link->data;
724 /* message_function is NULL if we're unregistered
727 if (subtree->message_function)
729 DBusObjectPathMessageFunction message_function;
732 message_function = subtree->message_function;
733 user_data = subtree->user_data;
736 _dbus_verbose (" (invoking a handler)\n");
739 #ifdef DBUS_BUILD_TESTS
740 if (tree->connection)
742 _dbus_connection_unlock (tree->connection);
744 /* FIXME you could unregister the subtree in another thread
745 * before we invoke the callback, and I can't figure out a
746 * good way to solve this.
749 result = (* message_function) (tree->connection,
753 #ifdef DBUS_BUILD_TESTS
754 if (tree->connection)
756 _dbus_connection_lock (tree->connection);
758 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
759 goto free_and_return;
767 if (result == DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
769 /* This hardcoded default handler does a minimal Introspect()
771 result = handle_default_introspect_unlocked (tree, message,
772 (const char**) path);
775 #ifdef DBUS_BUILD_TESTS
776 if (tree->connection)
778 _dbus_connection_unlock (tree->connection);
782 link = _dbus_list_get_first_link (&list);
783 _dbus_object_subtree_unref (link->data);
784 _dbus_list_remove_link (&list, link);
787 dbus_free_string_array (path);
793 * Allocates a subtree object.
795 * @param name name to duplicate.
796 * @returns newly-allocated subtree
798 static DBusObjectSubtree*
799 allocate_subtree_object (const char *name)
802 DBusObjectSubtree *subtree;
803 const size_t front_padding = _DBUS_STRUCT_OFFSET (DBusObjectSubtree, name);
805 _dbus_assert (name != NULL);
809 subtree = dbus_malloc (front_padding + (len + 1));
814 memcpy (subtree->name, name, len + 1);
819 static DBusObjectSubtree*
820 _dbus_object_subtree_new (const char *name,
821 const DBusObjectPathVTable *vtable,
824 DBusObjectSubtree *subtree;
826 subtree = allocate_subtree_object (name);
830 _dbus_assert (name != NULL);
832 subtree->parent = NULL;
836 subtree->message_function = vtable->message_function;
837 subtree->unregister_function = vtable->unregister_function;
841 subtree->message_function = NULL;
842 subtree->unregister_function = NULL;
845 subtree->user_data = user_data;
846 subtree->refcount.value = 1;
847 subtree->subtrees = NULL;
848 subtree->n_subtrees = 0;
849 subtree->subtrees_sorted = TRUE;
863 _dbus_object_subtree_ref (DBusObjectSubtree *subtree)
865 _dbus_assert (subtree->refcount.value > 0);
866 _dbus_atomic_inc (&subtree->refcount);
870 _dbus_object_subtree_unref (DBusObjectSubtree *subtree)
872 _dbus_assert (subtree->refcount.value > 0);
874 if (_dbus_atomic_dec (&subtree->refcount) == 1)
876 _dbus_assert (subtree->unregister_function == NULL);
877 _dbus_assert (subtree->message_function == NULL);
879 dbus_free (subtree->subtrees);
885 * Lists the registered fallback handlers and object path handlers at
886 * the given parent_path. The returned array should be freed with
887 * dbus_free_string_array().
889 * @param connection the connection
890 * @param parent_path the path to list the child handlers of
891 * @param child_entries returns #NULL-terminated array of children
892 * @returns #FALSE if no memory to allocate the child entries
895 _dbus_object_tree_list_registered_and_unlock (DBusObjectTree *tree,
896 const char **parent_path,
897 char ***child_entries)
901 result = _dbus_object_tree_list_registered_unlocked (tree,
905 #ifdef DBUS_BUILD_TESTS
906 if (tree->connection)
908 _dbus_connection_unlock (tree->connection);
915 #ifdef DBUS_BUILD_TESTS
916 #include "dbus-test.h"
920 flatten_path (const char **path)
926 if (!_dbus_string_init (&str))
932 if (!_dbus_string_append_byte (&str, '/'))
935 if (!_dbus_string_append (&str, path[i]))
941 if (!_dbus_string_steal_data (&str, &s))
944 _dbus_string_free (&str);
949 _dbus_string_free (&str);
953 /* Returns TRUE if container is a parent of child
956 path_contains (const char **container,
962 while (child[i] != NULL)
966 if (container[i] == NULL)
967 return TRUE; /* container ran out, child continues;
968 * thus the container is a parent of the
972 _dbus_assert (container[i] != NULL);
973 _dbus_assert (child[i] != NULL);
975 v = strcmp (container[i], child[i]);
978 return FALSE; /* they overlap until here and then are different,
985 /* Child ran out; if container also did, they are equal;
986 * otherwise, the child is a parent of the container.
988 if (container[i] == NULL)
989 return TRUE; /* equal is counted as containing */
995 spew_subtree_recurse (DBusObjectSubtree *subtree,
1003 _dbus_verbose (" ");
1007 _dbus_verbose ("%s (%d children)\n",
1008 subtree->name, subtree->n_subtrees);
1011 while (i < subtree->n_subtrees)
1013 spew_subtree_recurse (subtree->subtrees[i], indent + 2);
1020 spew_tree (DBusObjectTree *tree)
1022 spew_subtree_recurse (tree->root, 0);
1026 * Callback data used in tests
1030 const char **path; /**< Path */
1031 dbus_bool_t message_handled; /**< Gets set to true if message handler called */
1032 dbus_bool_t handler_unregistered; /**< gets set to true if handler is unregistered */
1038 test_unregister_function (DBusConnection *connection,
1041 TreeTestData *ttd = user_data;
1043 ttd->handler_unregistered = TRUE;
1046 static DBusHandlerResult
1047 test_message_function (DBusConnection *connection,
1048 DBusMessage *message,
1051 TreeTestData *ttd = user_data;
1053 ttd->message_handled = TRUE;
1055 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1059 do_register (DBusObjectTree *tree,
1062 TreeTestData *tree_test_data)
1064 DBusObjectPathVTable vtable = { test_unregister_function,
1065 test_message_function, NULL };
1067 tree_test_data[i].message_handled = FALSE;
1068 tree_test_data[i].handler_unregistered = FALSE;
1069 tree_test_data[i].path = path;
1071 if (!_dbus_object_tree_register (tree, TRUE, path,
1073 &tree_test_data[i]))
1080 do_test_dispatch (DBusObjectTree *tree,
1083 TreeTestData *tree_test_data,
1086 DBusMessage *message;
1088 DBusHandlerResult result;
1093 flat = flatten_path (path);
1097 message = dbus_message_new_method_call (NULL,
1099 "org.freedesktop.TestInterface",
1102 if (message == NULL)
1106 while (j < n_test_data)
1108 tree_test_data[j].message_handled = FALSE;
1112 result = _dbus_object_tree_dispatch_and_unlock (tree, message);
1113 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
1116 _dbus_assert (tree_test_data[i].message_handled);
1119 while (j < n_test_data)
1121 if (tree_test_data[j].message_handled)
1122 _dbus_assert (path_contains (tree_test_data[j].path,
1125 _dbus_assert (!path_contains (tree_test_data[j].path,
1131 dbus_message_unref (message);
1137 dbus_message_unref (message);
1142 object_tree_test_iteration (void *data)
1144 const char *path1[] = { "foo", NULL };
1145 const char *path2[] = { "foo", "bar", NULL };
1146 const char *path3[] = { "foo", "bar", "baz", NULL };
1147 const char *path4[] = { "foo", "bar", "boo", NULL };
1148 const char *path5[] = { "blah", NULL };
1149 const char *path6[] = { "blah", "boof", NULL };
1150 const char *path7[] = { "blah", "boof", "this", "is", "really", "long", NULL };
1151 const char *path8[] = { "childless", NULL };
1152 DBusObjectTree *tree;
1153 TreeTestData tree_test_data[8];
1158 tree = _dbus_object_tree_new (NULL);
1162 if (!do_register (tree, path1, 0, tree_test_data))
1165 _dbus_assert (find_subtree (tree, path1, NULL));
1166 _dbus_assert (!find_subtree (tree, path2, NULL));
1167 _dbus_assert (!find_subtree (tree, path3, NULL));
1168 _dbus_assert (!find_subtree (tree, path4, NULL));
1169 _dbus_assert (!find_subtree (tree, path5, NULL));
1170 _dbus_assert (!find_subtree (tree, path6, NULL));
1171 _dbus_assert (!find_subtree (tree, path7, NULL));
1172 _dbus_assert (!find_subtree (tree, path8, NULL));
1174 _dbus_assert (find_handler (tree, path1));
1175 _dbus_assert (find_handler (tree, path2));
1176 _dbus_assert (find_handler (tree, path3));
1177 _dbus_assert (find_handler (tree, path4));
1178 _dbus_assert (find_handler (tree, path5) == tree->root);
1179 _dbus_assert (find_handler (tree, path6) == tree->root);
1180 _dbus_assert (find_handler (tree, path7) == tree->root);
1181 _dbus_assert (find_handler (tree, path8) == tree->root);
1183 if (!do_register (tree, path2, 1, tree_test_data))
1186 _dbus_assert (find_subtree (tree, path1, NULL));
1187 _dbus_assert (find_subtree (tree, path2, NULL));
1188 _dbus_assert (!find_subtree (tree, path3, NULL));
1189 _dbus_assert (!find_subtree (tree, path4, NULL));
1190 _dbus_assert (!find_subtree (tree, path5, NULL));
1191 _dbus_assert (!find_subtree (tree, path6, NULL));
1192 _dbus_assert (!find_subtree (tree, path7, NULL));
1193 _dbus_assert (!find_subtree (tree, path8, NULL));
1195 if (!do_register (tree, path3, 2, tree_test_data))
1198 _dbus_assert (find_subtree (tree, path1, NULL));
1199 _dbus_assert (find_subtree (tree, path2, NULL));
1200 _dbus_assert (find_subtree (tree, path3, NULL));
1201 _dbus_assert (!find_subtree (tree, path4, NULL));
1202 _dbus_assert (!find_subtree (tree, path5, NULL));
1203 _dbus_assert (!find_subtree (tree, path6, NULL));
1204 _dbus_assert (!find_subtree (tree, path7, NULL));
1205 _dbus_assert (!find_subtree (tree, path8, NULL));
1207 if (!do_register (tree, path4, 3, tree_test_data))
1210 _dbus_assert (find_subtree (tree, path1, NULL));
1211 _dbus_assert (find_subtree (tree, path2, NULL));
1212 _dbus_assert (find_subtree (tree, path3, NULL));
1213 _dbus_assert (find_subtree (tree, path4, NULL));
1214 _dbus_assert (!find_subtree (tree, path5, NULL));
1215 _dbus_assert (!find_subtree (tree, path6, NULL));
1216 _dbus_assert (!find_subtree (tree, path7, NULL));
1217 _dbus_assert (!find_subtree (tree, path8, NULL));
1219 if (!do_register (tree, path5, 4, tree_test_data))
1222 _dbus_assert (find_subtree (tree, path1, NULL));
1223 _dbus_assert (find_subtree (tree, path2, NULL));
1224 _dbus_assert (find_subtree (tree, path3, NULL));
1225 _dbus_assert (find_subtree (tree, path4, NULL));
1226 _dbus_assert (find_subtree (tree, path5, NULL));
1227 _dbus_assert (!find_subtree (tree, path6, NULL));
1228 _dbus_assert (!find_subtree (tree, path7, NULL));
1229 _dbus_assert (!find_subtree (tree, path8, NULL));
1231 _dbus_assert (find_handler (tree, path1) != tree->root);
1232 _dbus_assert (find_handler (tree, path2) != tree->root);
1233 _dbus_assert (find_handler (tree, path3) != tree->root);
1234 _dbus_assert (find_handler (tree, path4) != tree->root);
1235 _dbus_assert (find_handler (tree, path5) != tree->root);
1236 _dbus_assert (find_handler (tree, path6) != tree->root);
1237 _dbus_assert (find_handler (tree, path7) != tree->root);
1238 _dbus_assert (find_handler (tree, path8) == tree->root);
1240 if (!do_register (tree, path6, 5, tree_test_data))
1243 _dbus_assert (find_subtree (tree, path1, NULL));
1244 _dbus_assert (find_subtree (tree, path2, NULL));
1245 _dbus_assert (find_subtree (tree, path3, NULL));
1246 _dbus_assert (find_subtree (tree, path4, NULL));
1247 _dbus_assert (find_subtree (tree, path5, NULL));
1248 _dbus_assert (find_subtree (tree, path6, NULL));
1249 _dbus_assert (!find_subtree (tree, path7, NULL));
1250 _dbus_assert (!find_subtree (tree, path8, NULL));
1252 if (!do_register (tree, path7, 6, tree_test_data))
1255 _dbus_assert (find_subtree (tree, path1, NULL));
1256 _dbus_assert (find_subtree (tree, path2, NULL));
1257 _dbus_assert (find_subtree (tree, path3, NULL));
1258 _dbus_assert (find_subtree (tree, path4, NULL));
1259 _dbus_assert (find_subtree (tree, path5, NULL));
1260 _dbus_assert (find_subtree (tree, path6, NULL));
1261 _dbus_assert (find_subtree (tree, path7, NULL));
1262 _dbus_assert (!find_subtree (tree, path8, NULL));
1264 if (!do_register (tree, path8, 7, tree_test_data))
1267 _dbus_assert (find_subtree (tree, path1, NULL));
1268 _dbus_assert (find_subtree (tree, path2, NULL));
1269 _dbus_assert (find_subtree (tree, path3, NULL));
1270 _dbus_assert (find_subtree (tree, path4, NULL));
1271 _dbus_assert (find_subtree (tree, path5, NULL));
1272 _dbus_assert (find_subtree (tree, path6, NULL));
1273 _dbus_assert (find_subtree (tree, path7, NULL));
1274 _dbus_assert (find_subtree (tree, path8, NULL));
1276 _dbus_assert (find_handler (tree, path1) != tree->root);
1277 _dbus_assert (find_handler (tree, path2) != tree->root);
1278 _dbus_assert (find_handler (tree, path3) != tree->root);
1279 _dbus_assert (find_handler (tree, path4) != tree->root);
1280 _dbus_assert (find_handler (tree, path5) != tree->root);
1281 _dbus_assert (find_handler (tree, path6) != tree->root);
1282 _dbus_assert (find_handler (tree, path7) != tree->root);
1283 _dbus_assert (find_handler (tree, path8) != tree->root);
1285 /* Check that destroying tree calls unregister funcs */
1286 _dbus_object_tree_unref (tree);
1289 while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1291 _dbus_assert (tree_test_data[i].handler_unregistered);
1292 _dbus_assert (!tree_test_data[i].message_handled);
1296 /* Now start again and try the individual unregister function */
1297 tree = _dbus_object_tree_new (NULL);
1301 if (!do_register (tree, path1, 0, tree_test_data))
1303 if (!do_register (tree, path2, 1, tree_test_data))
1305 if (!do_register (tree, path3, 2, tree_test_data))
1307 if (!do_register (tree, path4, 3, tree_test_data))
1309 if (!do_register (tree, path5, 4, tree_test_data))
1311 if (!do_register (tree, path6, 5, tree_test_data))
1313 if (!do_register (tree, path7, 6, tree_test_data))
1315 if (!do_register (tree, path8, 7, tree_test_data))
1318 _dbus_object_tree_unregister_and_unlock (tree, path1);
1320 _dbus_assert (!find_subtree (tree, path1, NULL));
1321 _dbus_assert (find_subtree (tree, path2, NULL));
1322 _dbus_assert (find_subtree (tree, path3, NULL));
1323 _dbus_assert (find_subtree (tree, path4, NULL));
1324 _dbus_assert (find_subtree (tree, path5, NULL));
1325 _dbus_assert (find_subtree (tree, path6, NULL));
1326 _dbus_assert (find_subtree (tree, path7, NULL));
1327 _dbus_assert (find_subtree (tree, path8, NULL));
1329 _dbus_object_tree_unregister_and_unlock (tree, path2);
1331 _dbus_assert (!find_subtree (tree, path1, NULL));
1332 _dbus_assert (!find_subtree (tree, path2, NULL));
1333 _dbus_assert (find_subtree (tree, path3, NULL));
1334 _dbus_assert (find_subtree (tree, path4, NULL));
1335 _dbus_assert (find_subtree (tree, path5, NULL));
1336 _dbus_assert (find_subtree (tree, path6, NULL));
1337 _dbus_assert (find_subtree (tree, path7, NULL));
1338 _dbus_assert (find_subtree (tree, path8, NULL));
1340 _dbus_object_tree_unregister_and_unlock (tree, path3);
1342 _dbus_assert (!find_subtree (tree, path1, NULL));
1343 _dbus_assert (!find_subtree (tree, path2, NULL));
1344 _dbus_assert (!find_subtree (tree, path3, NULL));
1345 _dbus_assert (find_subtree (tree, path4, NULL));
1346 _dbus_assert (find_subtree (tree, path5, NULL));
1347 _dbus_assert (find_subtree (tree, path6, NULL));
1348 _dbus_assert (find_subtree (tree, path7, NULL));
1349 _dbus_assert (find_subtree (tree, path8, NULL));
1351 _dbus_object_tree_unregister_and_unlock (tree, path4);
1353 _dbus_assert (!find_subtree (tree, path1, NULL));
1354 _dbus_assert (!find_subtree (tree, path2, NULL));
1355 _dbus_assert (!find_subtree (tree, path3, NULL));
1356 _dbus_assert (!find_subtree (tree, path4, NULL));
1357 _dbus_assert (find_subtree (tree, path5, NULL));
1358 _dbus_assert (find_subtree (tree, path6, NULL));
1359 _dbus_assert (find_subtree (tree, path7, NULL));
1360 _dbus_assert (find_subtree (tree, path8, NULL));
1362 _dbus_object_tree_unregister_and_unlock (tree, path5);
1364 _dbus_assert (!find_subtree (tree, path1, NULL));
1365 _dbus_assert (!find_subtree (tree, path2, NULL));
1366 _dbus_assert (!find_subtree (tree, path3, NULL));
1367 _dbus_assert (!find_subtree (tree, path4, NULL));
1368 _dbus_assert (!find_subtree (tree, path5, NULL));
1369 _dbus_assert (find_subtree (tree, path6, NULL));
1370 _dbus_assert (find_subtree (tree, path7, NULL));
1371 _dbus_assert (find_subtree (tree, path8, NULL));
1373 _dbus_object_tree_unregister_and_unlock (tree, path6);
1375 _dbus_assert (!find_subtree (tree, path1, NULL));
1376 _dbus_assert (!find_subtree (tree, path2, NULL));
1377 _dbus_assert (!find_subtree (tree, path3, NULL));
1378 _dbus_assert (!find_subtree (tree, path4, NULL));
1379 _dbus_assert (!find_subtree (tree, path5, NULL));
1380 _dbus_assert (!find_subtree (tree, path6, NULL));
1381 _dbus_assert (find_subtree (tree, path7, NULL));
1382 _dbus_assert (find_subtree (tree, path8, NULL));
1384 _dbus_object_tree_unregister_and_unlock (tree, path7);
1386 _dbus_assert (!find_subtree (tree, path1, NULL));
1387 _dbus_assert (!find_subtree (tree, path2, NULL));
1388 _dbus_assert (!find_subtree (tree, path3, NULL));
1389 _dbus_assert (!find_subtree (tree, path4, NULL));
1390 _dbus_assert (!find_subtree (tree, path5, NULL));
1391 _dbus_assert (!find_subtree (tree, path6, NULL));
1392 _dbus_assert (!find_subtree (tree, path7, NULL));
1393 _dbus_assert (find_subtree (tree, path8, NULL));
1395 _dbus_object_tree_unregister_and_unlock (tree, path8);
1397 _dbus_assert (!find_subtree (tree, path1, NULL));
1398 _dbus_assert (!find_subtree (tree, path2, NULL));
1399 _dbus_assert (!find_subtree (tree, path3, NULL));
1400 _dbus_assert (!find_subtree (tree, path4, NULL));
1401 _dbus_assert (!find_subtree (tree, path5, NULL));
1402 _dbus_assert (!find_subtree (tree, path6, NULL));
1403 _dbus_assert (!find_subtree (tree, path7, NULL));
1404 _dbus_assert (!find_subtree (tree, path8, NULL));
1407 while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1409 _dbus_assert (tree_test_data[i].handler_unregistered);
1410 _dbus_assert (!tree_test_data[i].message_handled);
1414 /* Register it all again, and test dispatch */
1416 if (!do_register (tree, path1, 0, tree_test_data))
1418 if (!do_register (tree, path2, 1, tree_test_data))
1420 if (!do_register (tree, path3, 2, tree_test_data))
1422 if (!do_register (tree, path4, 3, tree_test_data))
1424 if (!do_register (tree, path5, 4, tree_test_data))
1426 if (!do_register (tree, path6, 5, tree_test_data))
1428 if (!do_register (tree, path7, 6, tree_test_data))
1430 if (!do_register (tree, path8, 7, tree_test_data))
1437 if (!do_test_dispatch (tree, path1, 0, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1439 if (!do_test_dispatch (tree, path2, 1, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1441 if (!do_test_dispatch (tree, path3, 2, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1443 if (!do_test_dispatch (tree, path4, 3, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1445 if (!do_test_dispatch (tree, path5, 4, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1447 if (!do_test_dispatch (tree, path6, 5, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1449 if (!do_test_dispatch (tree, path7, 6, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1451 if (!do_test_dispatch (tree, path8, 7, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1458 _dbus_object_tree_ref (tree);
1459 _dbus_object_tree_unref (tree);
1460 _dbus_object_tree_unref (tree);
1467 * @ingroup DBusObjectTree
1468 * Unit test for DBusObjectTree
1469 * @returns #TRUE on success.
1472 _dbus_object_tree_test (void)
1474 _dbus_test_oom_handling ("object tree",
1475 object_tree_test_iteration,
1481 #endif /* DBUS_BUILD_TESTS */