1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-object-tree.c DBusObjectTree (internals of DBusConnection)
4 * Copyright (C) 2003, 2005 Red Hat Inc.
6 * Licensed under the Academic Free License version 2.1
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "dbus-object-tree.h"
26 #include "dbus-connection-internal.h"
27 #include "dbus-internals.h"
28 #include "dbus-hash.h"
29 #include "dbus-protocol.h"
30 #include "dbus-string.h"
35 * @defgroup DBusObjectTree A hierarchy of objects with container-contained relationship
36 * @ingroup DBusInternals
37 * @brief DBusObjectTree is used by DBusConnection to track the object tree
39 * Types and functions related to DBusObjectTree. These
40 * are all library-internal.
45 /** Subnode of the object hierarchy */
46 typedef struct DBusObjectSubtree DBusObjectSubtree;
48 static DBusObjectSubtree* _dbus_object_subtree_new (const char *name,
49 const DBusObjectPathVTable *vtable,
51 static DBusObjectSubtree* _dbus_object_subtree_ref (DBusObjectSubtree *subtree);
52 static void _dbus_object_subtree_unref (DBusObjectSubtree *subtree);
55 * Internals of DBusObjectTree
59 int refcount; /**< Reference count */
60 DBusConnection *connection; /**< Connection this tree belongs to */
62 DBusObjectSubtree *root; /**< Root of the tree ("/" node) */
66 * Struct representing a single registered subtree handler, or node
67 * that's a parent of a registered subtree handler. If
68 * message_function != NULL there's actually a handler at this node.
70 struct DBusObjectSubtree
72 DBusAtomic refcount; /**< Reference count */
73 DBusObjectSubtree *parent; /**< Parent node */
74 DBusObjectPathUnregisterFunction unregister_function; /**< Function to call on unregister */
75 DBusObjectPathMessageFunction message_function; /**< Function to handle messages */
76 void *user_data; /**< Data for functions */
77 DBusObjectSubtree **subtrees; /**< Child nodes */
78 int n_subtrees; /**< Number of child nodes */
79 int max_subtrees; /**< Number of allocated entries in subtrees */
80 unsigned int invoke_as_fallback : 1; /**< Whether to invoke message_function when child nodes don't handle the message */
81 char name[1]; /**< Allocated as large as necessary */
85 * Creates a new object tree, representing a mapping from paths
88 * @param connection the connection this tree belongs to
89 * @returns the new tree or #NULL if no memory
92 _dbus_object_tree_new (DBusConnection *connection)
96 /* the connection passed in here isn't fully constructed,
97 * so don't do anything more than store a pointer to
101 tree = dbus_new0 (DBusObjectTree, 1);
106 tree->connection = connection;
107 tree->root = _dbus_object_subtree_new ("/", NULL, NULL);
108 if (tree->root == NULL)
110 tree->root->invoke_as_fallback = TRUE;
124 * Increment the reference count
125 * @param tree the object tree
126 * @returns the object tree
129 _dbus_object_tree_ref (DBusObjectTree *tree)
131 _dbus_assert (tree->refcount > 0);
139 * Decrement the reference count
140 * @param tree the object tree
143 _dbus_object_tree_unref (DBusObjectTree *tree)
145 _dbus_assert (tree->refcount > 0);
149 if (tree->refcount == 0)
151 _dbus_object_tree_free_all_unlocked (tree);
157 /** Set to 1 to get a bunch of debug spew about finding the
160 #define VERBOSE_FIND 0
162 static DBusObjectSubtree*
163 find_subtree_recurse (DBusObjectSubtree *subtree,
165 dbus_bool_t create_if_not_found,
166 int *index_in_parent,
167 dbus_bool_t *exact_match)
170 dbus_bool_t return_deepest_match;
172 return_deepest_match = exact_match != NULL;
174 _dbus_assert (!(return_deepest_match && create_if_not_found));
179 _dbus_verbose (" path exhausted, returning %s\n",
182 if (exact_match != NULL)
188 _dbus_verbose (" searching children of %s for %s\n",
189 subtree->name, path[0]);
193 j = subtree->n_subtrees;
199 v = strcmp (path[0], subtree->subtrees[k]->name);
202 _dbus_verbose (" %s cmp %s = %d\n",
203 path[0], subtree->subtrees[k]->name,
212 _dbus_verbose (" storing parent index %d\n", k);
214 *index_in_parent = k;
217 if (return_deepest_match)
219 DBusObjectSubtree *next;
221 next = find_subtree_recurse (subtree->subtrees[k],
222 &path[1], create_if_not_found,
223 index_in_parent, exact_match);
225 subtree->invoke_as_fallback)
228 _dbus_verbose (" no deeper match found, returning %s\n",
231 if (exact_match != NULL)
232 *exact_match = FALSE;
239 return find_subtree_recurse (subtree->subtrees[k],
240 &path[1], create_if_not_found,
241 index_in_parent, exact_match);
254 _dbus_verbose (" no match found, current tree %s, create_if_not_found = %d\n",
255 subtree->name, create_if_not_found);
258 if (create_if_not_found)
260 DBusObjectSubtree* child;
261 int child_pos, new_n_subtrees;
264 _dbus_verbose (" creating subtree %s\n",
268 child = _dbus_object_subtree_new (path[0],
273 new_n_subtrees = subtree->n_subtrees + 1;
274 if (new_n_subtrees > subtree->max_subtrees)
276 int new_max_subtrees;
277 DBusObjectSubtree **new_subtrees;
279 new_max_subtrees = subtree->max_subtrees == 0 ? 1 : 2 * subtree->max_subtrees;
280 new_subtrees = dbus_realloc (subtree->subtrees,
281 new_max_subtrees * sizeof (DBusObjectSubtree*));
282 if (new_subtrees == NULL)
284 _dbus_object_subtree_unref (child);
287 subtree->subtrees = new_subtrees;
288 subtree->max_subtrees = new_max_subtrees;
291 /* The binary search failed, so i == j points to the
292 place the child should be inserted. */
294 _dbus_assert (child_pos < new_n_subtrees &&
295 new_n_subtrees <= subtree->max_subtrees);
296 if (child_pos + 1 < new_n_subtrees)
298 memmove (&subtree->subtrees[child_pos+1],
299 &subtree->subtrees[child_pos],
300 (new_n_subtrees - child_pos - 1) *
301 sizeof subtree->subtrees[0]);
303 subtree->subtrees[child_pos] = child;
306 *index_in_parent = child_pos;
307 subtree->n_subtrees = new_n_subtrees;
308 child->parent = subtree;
310 return find_subtree_recurse (child,
311 &path[1], create_if_not_found,
312 index_in_parent, exact_match);
316 if (exact_match != NULL)
317 *exact_match = FALSE;
318 return (return_deepest_match && subtree->invoke_as_fallback) ? subtree : NULL;
322 static DBusObjectSubtree*
323 find_subtree (DBusObjectTree *tree,
325 int *index_in_parent)
327 DBusObjectSubtree *subtree;
330 _dbus_verbose ("Looking for exact registered subtree\n");
333 subtree = find_subtree_recurse (tree->root, path, FALSE, index_in_parent, NULL);
335 if (subtree && subtree->message_function == NULL)
341 static DBusObjectSubtree*
342 lookup_subtree (DBusObjectTree *tree,
346 _dbus_verbose ("Looking for subtree\n");
348 return find_subtree_recurse (tree->root, path, FALSE, NULL, NULL);
351 static DBusObjectSubtree*
352 find_handler (DBusObjectTree *tree,
354 dbus_bool_t *exact_match)
357 _dbus_verbose ("Looking for deepest handler\n");
359 _dbus_assert (exact_match != NULL);
361 *exact_match = FALSE; /* ensure always initialized */
363 return find_subtree_recurse (tree->root, path, FALSE, NULL, exact_match);
366 static DBusObjectSubtree*
367 ensure_subtree (DBusObjectTree *tree,
371 _dbus_verbose ("Ensuring subtree\n");
373 return find_subtree_recurse (tree->root, path, TRUE, NULL, NULL);
376 static char *flatten_path (const char **path);
379 * Registers a new subtree in the global object tree.
381 * @param tree the global object tree
382 * @param fallback #TRUE to handle messages to children of this path
383 * @param path NULL-terminated array of path elements giving path to subtree
384 * @param vtable the vtable used to traverse this subtree
385 * @param user_data user data to pass to methods in the vtable
386 * @param error address where an error can be returned
387 * @returns #FALSE if an error (#DBUS_ERROR_NO_MEMORY or
388 * #DBUS_ERROR_OBJECT_PATH_IN_USE) is reported
391 _dbus_object_tree_register (DBusObjectTree *tree,
392 dbus_bool_t fallback,
394 const DBusObjectPathVTable *vtable,
398 DBusObjectSubtree *subtree;
400 _dbus_assert (tree != NULL);
401 _dbus_assert (vtable->message_function != NULL);
402 _dbus_assert (path != NULL);
404 subtree = ensure_subtree (tree, path);
407 _DBUS_SET_OOM (error);
411 if (subtree->message_function != NULL)
415 char *complete_path = flatten_path (path);
417 dbus_set_error (error, DBUS_ERROR_OBJECT_PATH_IN_USE,
418 "A handler is already registered for %s",
419 complete_path ? complete_path
420 : "(cannot represent path: out of memory!)");
422 dbus_free (complete_path);
428 subtree->message_function = vtable->message_function;
429 subtree->unregister_function = vtable->unregister_function;
430 subtree->user_data = user_data;
431 subtree->invoke_as_fallback = fallback != FALSE;
437 * Unregisters an object subtree that was registered with the
440 * @param tree the global object tree
441 * @param path path to the subtree (same as the one passed to _dbus_object_tree_register())
444 _dbus_object_tree_unregister_and_unlock (DBusObjectTree *tree,
448 DBusObjectSubtree *subtree;
449 DBusObjectPathUnregisterFunction unregister_function;
451 DBusConnection *connection;
453 _dbus_assert (path != NULL);
455 unregister_function = NULL;
458 subtree = find_subtree (tree, path, &i);
460 #ifndef DBUS_DISABLE_CHECKS
463 _dbus_warn ("Attempted to unregister path (path[0] = %s path[1] = %s) which isn't registered\n",
464 path[0] ? path[0] : "null",
465 path[1] ? path[1] : "null");
469 _dbus_assert (subtree != NULL);
472 _dbus_assert (subtree->parent == NULL ||
473 (i >= 0 && subtree->parent->subtrees[i] == subtree));
475 subtree->message_function = NULL;
477 unregister_function = subtree->unregister_function;
478 user_data = subtree->user_data;
480 subtree->unregister_function = NULL;
481 subtree->user_data = NULL;
483 /* If we have no subtrees of our own, remove from
484 * our parent (FIXME could also be more aggressive
485 * and remove our parent if it becomes empty)
487 if (subtree->parent && subtree->n_subtrees == 0)
489 /* assumes a 0-byte memmove is OK */
490 memmove (&subtree->parent->subtrees[i],
491 &subtree->parent->subtrees[i+1],
492 (subtree->parent->n_subtrees - i - 1) *
493 sizeof (subtree->parent->subtrees[0]));
494 subtree->parent->n_subtrees -= 1;
496 subtree->parent = NULL;
498 _dbus_object_subtree_unref (subtree);
503 connection = tree->connection;
505 /* Unlock and call application code */
506 #ifdef DBUS_BUILD_TESTS
510 _dbus_connection_ref_unlocked (connection);
511 _dbus_verbose ("unlock\n");
512 _dbus_connection_unlock (connection);
515 if (unregister_function)
516 (* unregister_function) (connection, user_data);
518 #ifdef DBUS_BUILD_TESTS
521 dbus_connection_unref (connection);
525 free_subtree_recurse (DBusConnection *connection,
526 DBusObjectSubtree *subtree)
528 /* Delete them from the end, for slightly
529 * more robustness against odd reentrancy.
531 while (subtree->n_subtrees > 0)
533 DBusObjectSubtree *child;
535 child = subtree->subtrees[subtree->n_subtrees - 1];
536 subtree->subtrees[subtree->n_subtrees - 1] = NULL;
537 subtree->n_subtrees -= 1;
538 child->parent = NULL;
540 free_subtree_recurse (connection, child);
543 /* Call application code */
544 if (subtree->unregister_function)
545 (* subtree->unregister_function) (connection,
548 subtree->message_function = NULL;
549 subtree->unregister_function = NULL;
550 subtree->user_data = NULL;
552 /* Now free ourselves */
553 _dbus_object_subtree_unref (subtree);
557 * Free all the handlers in the tree. Lock on tree's connection
560 * @param tree the object tree
563 _dbus_object_tree_free_all_unlocked (DBusObjectTree *tree)
566 free_subtree_recurse (tree->connection,
572 _dbus_object_tree_list_registered_unlocked (DBusObjectTree *tree,
573 const char **parent_path,
574 char ***child_entries)
576 DBusObjectSubtree *subtree;
579 _dbus_assert (parent_path != NULL);
580 _dbus_assert (child_entries != NULL);
582 *child_entries = NULL;
584 subtree = lookup_subtree (tree, parent_path);
587 retval = dbus_new0 (char *, 1);
592 retval = dbus_new0 (char*, subtree->n_subtrees + 1);
596 while (i < subtree->n_subtrees)
598 retval[i] = _dbus_strdup (subtree->subtrees[i]->name);
599 if (retval[i] == NULL)
601 dbus_free_string_array (retval);
611 *child_entries = retval;
612 return retval != NULL;
615 static DBusHandlerResult
616 handle_default_introspect_and_unlock (DBusObjectTree *tree,
617 DBusMessage *message,
621 DBusHandlerResult result;
625 DBusMessageIter iter;
626 const char *v_STRING;
627 dbus_bool_t already_unlocked;
629 /* We have the connection lock here */
631 already_unlocked = FALSE;
633 _dbus_verbose (" considering default Introspect() handler...\n");
637 if (!dbus_message_is_method_call (message,
638 DBUS_INTERFACE_INTROSPECTABLE,
641 #ifdef DBUS_BUILD_TESTS
642 if (tree->connection)
645 _dbus_verbose ("unlock\n");
646 _dbus_connection_unlock (tree->connection);
649 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
652 _dbus_verbose (" using default Introspect() handler!\n");
654 if (!_dbus_string_init (&xml))
656 #ifdef DBUS_BUILD_TESTS
657 if (tree->connection)
660 _dbus_verbose ("unlock\n");
661 _dbus_connection_unlock (tree->connection);
664 return DBUS_HANDLER_RESULT_NEED_MEMORY;
667 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
670 if (!_dbus_object_tree_list_registered_unlocked (tree, path, &children))
673 if (!_dbus_string_append (&xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
676 if (!_dbus_string_append (&xml, "<node>\n"))
680 while (children[i] != NULL)
682 if (!_dbus_string_append_printf (&xml, " <node name=\"%s\"/>\n",
689 if (!_dbus_string_append (&xml, "</node>\n"))
692 reply = dbus_message_new_method_return (message);
696 dbus_message_iter_init_append (reply, &iter);
697 v_STRING = _dbus_string_get_const_data (&xml);
698 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &v_STRING))
701 #ifdef DBUS_BUILD_TESTS
702 if (tree->connection)
705 already_unlocked = TRUE;
707 if (!_dbus_connection_send_and_unlock (tree->connection, reply, NULL))
711 result = DBUS_HANDLER_RESULT_HANDLED;
714 #ifdef DBUS_BUILD_TESTS
715 if (tree->connection)
718 if (!already_unlocked)
720 _dbus_verbose ("unlock\n");
721 _dbus_connection_unlock (tree->connection);
725 _dbus_string_free (&xml);
726 dbus_free_string_array (children);
728 dbus_message_unref (reply);
734 * Tries to dispatch a message by directing it to handler for the
735 * object path listed in the message header, if any. Messages are
736 * dispatched first to the registered handler that matches the largest
737 * number of path elements; that is, message to /foo/bar/baz would go
738 * to the handler for /foo/bar before the one for /foo.
740 * @todo thread problems
742 * @param tree the global object tree
743 * @param message the message to dispatch
744 * @returns whether message was handled successfully
747 _dbus_object_tree_dispatch_and_unlock (DBusObjectTree *tree,
748 DBusMessage *message,
749 dbus_bool_t *found_object)
752 dbus_bool_t exact_match;
755 DBusHandlerResult result;
756 DBusObjectSubtree *subtree;
759 _dbus_verbose ("Dispatch of message by object path\n");
763 if (!dbus_message_get_path_decomposed (message, &path))
765 #ifdef DBUS_BUILD_TESTS
766 if (tree->connection)
769 _dbus_verbose ("unlock\n");
770 _dbus_connection_unlock (tree->connection);
773 _dbus_verbose ("No memory to get decomposed path\n");
775 return DBUS_HANDLER_RESULT_NEED_MEMORY;
780 #ifdef DBUS_BUILD_TESTS
781 if (tree->connection)
784 _dbus_verbose ("unlock\n");
785 _dbus_connection_unlock (tree->connection);
788 _dbus_verbose ("No path field in message\n");
789 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
792 /* Find the deepest path that covers the path in the message */
793 subtree = find_handler (tree, (const char**) path, &exact_match);
796 *found_object = !!subtree;
798 /* Build a list of all paths that cover the path in the message */
802 while (subtree != NULL)
804 if (subtree->message_function != NULL && (exact_match || subtree->invoke_as_fallback))
806 _dbus_object_subtree_ref (subtree);
808 /* run deepest paths first */
809 if (!_dbus_list_append (&list, subtree))
811 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
812 _dbus_object_subtree_unref (subtree);
813 goto free_and_return;
818 subtree = subtree->parent;
821 _dbus_verbose ("%d handlers in the path tree for this message\n",
822 _dbus_list_get_length (&list));
824 /* Invoke each handler in the list */
826 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
828 link = _dbus_list_get_first_link (&list);
831 DBusList *next = _dbus_list_get_next_link (&list, link);
832 subtree = link->data;
834 /* message_function is NULL if we're unregistered
837 if (subtree->message_function)
839 DBusObjectPathMessageFunction message_function;
842 message_function = subtree->message_function;
843 user_data = subtree->user_data;
846 _dbus_verbose (" (invoking a handler)\n");
849 #ifdef DBUS_BUILD_TESTS
850 if (tree->connection)
853 _dbus_verbose ("unlock\n");
854 _dbus_connection_unlock (tree->connection);
857 /* FIXME you could unregister the subtree in another thread
858 * before we invoke the callback, and I can't figure out a
859 * good way to solve this.
862 result = (* message_function) (tree->connection,
866 #ifdef DBUS_BUILD_TESTS
867 if (tree->connection)
869 _dbus_connection_lock (tree->connection);
871 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
872 goto free_and_return;
880 if (result == DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
882 /* This hardcoded default handler does a minimal Introspect()
884 result = handle_default_introspect_and_unlock (tree, message,
885 (const char**) path);
889 #ifdef DBUS_BUILD_TESTS
890 if (tree->connection)
893 _dbus_verbose ("unlock\n");
894 _dbus_connection_unlock (tree->connection);
900 link = _dbus_list_get_first_link (&list);
901 _dbus_object_subtree_unref (link->data);
902 _dbus_list_remove_link (&list, link);
905 dbus_free_string_array (path);
911 * Looks up the data passed to _dbus_object_tree_register() for a
912 * handler at the given path.
914 * @param tree the global object tree
915 * @param path NULL-terminated array of path elements giving path to subtree
916 * @returns the object's user_data or #NULL if none found
919 _dbus_object_tree_get_user_data_unlocked (DBusObjectTree *tree,
922 dbus_bool_t exact_match;
923 DBusObjectSubtree *subtree;
925 _dbus_assert (tree != NULL);
926 _dbus_assert (path != NULL);
928 /* Find the deepest path that covers the path in the message */
929 subtree = find_handler (tree, (const char**) path, &exact_match);
931 if ((subtree == NULL) || !exact_match)
933 _dbus_verbose ("No object at specified path found\n");
937 return subtree->user_data;
941 * Allocates a subtree object.
943 * @param name name to duplicate.
944 * @returns newly-allocated subtree
946 static DBusObjectSubtree*
947 allocate_subtree_object (const char *name)
950 DBusObjectSubtree *subtree;
951 const size_t front_padding = _DBUS_STRUCT_OFFSET (DBusObjectSubtree, name);
953 _dbus_assert (name != NULL);
957 subtree = dbus_malloc (MAX (front_padding + (len + 1), sizeof (DBusObjectSubtree)));
962 memcpy (subtree->name, name, len + 1);
967 static DBusObjectSubtree*
968 _dbus_object_subtree_new (const char *name,
969 const DBusObjectPathVTable *vtable,
972 DBusObjectSubtree *subtree;
974 subtree = allocate_subtree_object (name);
978 _dbus_assert (name != NULL);
980 subtree->parent = NULL;
984 subtree->message_function = vtable->message_function;
985 subtree->unregister_function = vtable->unregister_function;
989 subtree->message_function = NULL;
990 subtree->unregister_function = NULL;
993 subtree->user_data = user_data;
994 subtree->refcount.value = 1;
995 subtree->subtrees = NULL;
996 subtree->n_subtrees = 0;
997 subtree->max_subtrees = 0;
998 subtree->invoke_as_fallback = FALSE;
1006 static DBusObjectSubtree *
1007 _dbus_object_subtree_ref (DBusObjectSubtree *subtree)
1009 _dbus_assert (subtree->refcount.value > 0);
1010 _dbus_atomic_inc (&subtree->refcount);
1016 _dbus_object_subtree_unref (DBusObjectSubtree *subtree)
1018 _dbus_assert (subtree->refcount.value > 0);
1020 if (_dbus_atomic_dec (&subtree->refcount) == 1)
1022 _dbus_assert (subtree->unregister_function == NULL);
1023 _dbus_assert (subtree->message_function == NULL);
1025 dbus_free (subtree->subtrees);
1026 dbus_free (subtree);
1031 * Lists the registered fallback handlers and object path handlers at
1032 * the given parent_path. The returned array should be freed with
1033 * dbus_free_string_array().
1035 * @param tree the object tree
1036 * @param parent_path the path to list the child handlers of
1037 * @param child_entries returns #NULL-terminated array of children
1038 * @returns #FALSE if no memory to allocate the child entries
1041 _dbus_object_tree_list_registered_and_unlock (DBusObjectTree *tree,
1042 const char **parent_path,
1043 char ***child_entries)
1047 result = _dbus_object_tree_list_registered_unlocked (tree,
1051 #ifdef DBUS_BUILD_TESTS
1052 if (tree->connection)
1055 _dbus_verbose ("unlock\n");
1056 _dbus_connection_unlock (tree->connection);
1063 /** Set to 1 to get a bunch of spew about disassembling the path string */
1064 #define VERBOSE_DECOMPOSE 0
1067 * Decompose an object path. A path of just "/" is
1068 * represented as an empty vector of strings.
1069 * The path need not be nul terminated.
1071 * @param data the path data
1072 * @param len the length of the path string
1073 * @param path address to store new object path
1074 * @param path_len length of stored path
1077 _dbus_decompose_path (const char* data,
1086 _dbus_assert (data != NULL);
1087 _dbus_assert (path != NULL);
1089 #if VERBOSE_DECOMPOSE
1090 _dbus_verbose ("Decomposing path \"%s\"\n",
1095 if (len > 1) /* if path is not just "/" */
1100 _dbus_assert (data[i] != '\0');
1107 retval = dbus_new0 (char*, n_components + 1);
1113 if (n_components == 0)
1117 while (comp < n_components)
1119 _dbus_assert (i < len);
1125 while (j < len && data[j] != '/')
1128 /* Now [i, j) is the path component */
1129 _dbus_assert (i < j);
1130 _dbus_assert (data[i] != '/');
1131 _dbus_assert (j == len || data[j] == '/');
1133 #if VERBOSE_DECOMPOSE
1134 _dbus_verbose (" (component in [%d,%d))\n",
1138 retval[comp] = _dbus_memdup (&data[i], j - i + 1);
1139 if (retval[comp] == NULL)
1141 dbus_free_string_array (retval);
1144 retval[comp][j-i] = '\0';
1145 #if VERBOSE_DECOMPOSE
1146 _dbus_verbose (" (component %d = \"%s\")\n",
1147 comp, retval[comp]);
1153 _dbus_assert (i == len);
1157 *path_len = n_components;
1165 flatten_path (const char **path)
1170 if (!_dbus_string_init (&str))
1173 if (path[0] == NULL)
1175 if (!_dbus_string_append_byte (&str, '/'))
1185 if (!_dbus_string_append_byte (&str, '/'))
1188 if (!_dbus_string_append (&str, path[i]))
1195 if (!_dbus_string_steal_data (&str, &s))
1198 _dbus_string_free (&str);
1203 _dbus_string_free (&str);
1208 #ifdef DBUS_BUILD_TESTS
1210 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1212 #include "dbus-test.h"
1222 /* Returns TRUE if container is a parent of child
1224 static StrComparison
1225 path_contains (const char **container,
1231 while (child[i] != NULL)
1235 if (container[i] == NULL)
1236 return STR_PREFIX; /* container ran out, child continues;
1237 * thus the container is a parent of the
1241 _dbus_assert (container[i] != NULL);
1242 _dbus_assert (child[i] != NULL);
1244 v = strcmp (container[i], child[i]);
1247 return STR_DIFFERENT; /* they overlap until here and then are different,
1254 /* Child ran out; if container also did, they are equal;
1255 * otherwise, the child is a parent of the container.
1257 if (container[i] == NULL)
1260 return STR_DIFFERENT;
1265 spew_subtree_recurse (DBusObjectSubtree *subtree,
1273 _dbus_verbose (" ");
1277 _dbus_verbose ("%s (%d children)\n",
1278 subtree->name, subtree->n_subtrees);
1281 while (i < subtree->n_subtrees)
1283 spew_subtree_recurse (subtree->subtrees[i], indent + 2);
1290 spew_tree (DBusObjectTree *tree)
1292 spew_subtree_recurse (tree->root, 0);
1297 * Callback data used in tests
1301 const char **path; /**< Path */
1302 dbus_bool_t handler_fallback; /**< true if the handler may be called as fallback */
1303 dbus_bool_t message_handled; /**< Gets set to true if message handler called */
1304 dbus_bool_t handler_unregistered; /**< gets set to true if handler is unregistered */
1309 test_unregister_function (DBusConnection *connection,
1312 TreeTestData *ttd = user_data;
1314 ttd->handler_unregistered = TRUE;
1317 static DBusHandlerResult
1318 test_message_function (DBusConnection *connection,
1319 DBusMessage *message,
1322 TreeTestData *ttd = user_data;
1324 ttd->message_handled = TRUE;
1326 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1330 do_register (DBusObjectTree *tree,
1332 dbus_bool_t fallback,
1334 TreeTestData *tree_test_data)
1336 DBusObjectPathVTable vtable = { test_unregister_function,
1337 test_message_function, NULL };
1339 tree_test_data[i].message_handled = FALSE;
1340 tree_test_data[i].handler_unregistered = FALSE;
1341 tree_test_data[i].handler_fallback = fallback;
1342 tree_test_data[i].path = path;
1344 if (!_dbus_object_tree_register (tree, fallback, path,
1350 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path) ==
1351 &tree_test_data[i]);
1357 do_test_dispatch (DBusObjectTree *tree,
1360 TreeTestData *tree_test_data,
1363 DBusMessage *message;
1365 DBusHandlerResult result;
1370 flat = flatten_path (path);
1374 message = dbus_message_new_method_call (NULL,
1376 "org.freedesktop.TestInterface",
1379 if (message == NULL)
1383 while (j < n_test_data)
1385 tree_test_data[j].message_handled = FALSE;
1389 result = _dbus_object_tree_dispatch_and_unlock (tree, message, NULL);
1390 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
1393 _dbus_assert (tree_test_data[i].message_handled);
1396 while (j < n_test_data)
1398 if (tree_test_data[j].message_handled)
1400 if (tree_test_data[j].handler_fallback)
1401 _dbus_assert (path_contains (tree_test_data[j].path,
1402 path) != STR_DIFFERENT);
1404 _dbus_assert (path_contains (tree_test_data[j].path, path) == STR_EQUAL);
1408 if (tree_test_data[j].handler_fallback)
1409 _dbus_assert (path_contains (tree_test_data[j].path,
1410 path) == STR_DIFFERENT);
1412 _dbus_assert (path_contains (tree_test_data[j].path, path) != STR_EQUAL);
1418 dbus_message_unref (message);
1424 dbus_message_unref (message);
1429 string_array_length (const char **array)
1432 for (i = 0; array[i]; i++) ;
1439 const char *result[20];
1440 } DecomposePathTest;
1442 static DecomposePathTest decompose_tests[] = {
1443 { "/foo", { "foo", NULL } },
1444 { "/foo/bar", { "foo", "bar", NULL } },
1446 { "/a/b", { "a", "b", NULL } },
1447 { "/a/b/c", { "a", "b", "c", NULL } },
1448 { "/a/b/c/d", { "a", "b", "c", "d", NULL } },
1449 { "/foo/bar/q", { "foo", "bar", "q", NULL } },
1450 { "/foo/bar/this/is/longer", { "foo", "bar", "this", "is", "longer", NULL } }
1454 run_decompose_tests (void)
1459 while (i < _DBUS_N_ELEMENTS (decompose_tests))
1465 if (!_dbus_decompose_path (decompose_tests[i].path,
1466 strlen (decompose_tests[i].path),
1467 &result, &result_len))
1470 expected_len = string_array_length (decompose_tests[i].result);
1472 if (result_len != (int) string_array_length ((const char**)result) ||
1473 expected_len != result_len ||
1474 path_contains (decompose_tests[i].result,
1475 (const char**) result) != STR_EQUAL)
1477 int real_len = string_array_length ((const char**)result);
1478 _dbus_warn ("Expected decompose of %s to have len %d, returned %d, appears to have %d\n",
1479 decompose_tests[i].path, expected_len, result_len,
1481 _dbus_warn ("Decompose resulted in elements: { ");
1483 while (i < real_len)
1485 _dbus_warn ("\"%s\"%s", result[i],
1486 (i + 1) == real_len ? "" : ", ");
1490 _dbus_assert_not_reached ("path decompose failed\n");
1493 dbus_free_string_array (result);
1502 object_tree_test_iteration (void *data)
1504 const char *path0[] = { NULL };
1505 const char *path1[] = { "foo", NULL };
1506 const char *path2[] = { "foo", "bar", NULL };
1507 const char *path3[] = { "foo", "bar", "baz", NULL };
1508 const char *path4[] = { "foo", "bar", "boo", NULL };
1509 const char *path5[] = { "blah", NULL };
1510 const char *path6[] = { "blah", "boof", NULL };
1511 const char *path7[] = { "blah", "boof", "this", "is", "really", "long", NULL };
1512 const char *path8[] = { "childless", NULL };
1513 DBusObjectTree *tree;
1514 TreeTestData tree_test_data[9];
1516 dbus_bool_t exact_match;
1518 if (!run_decompose_tests ())
1523 tree = _dbus_object_tree_new (NULL);
1527 if (!do_register (tree, path0, TRUE, 0, tree_test_data))
1530 _dbus_assert (find_subtree (tree, path0, NULL));
1531 _dbus_assert (!find_subtree (tree, path1, NULL));
1532 _dbus_assert (!find_subtree (tree, path2, NULL));
1533 _dbus_assert (!find_subtree (tree, path3, NULL));
1534 _dbus_assert (!find_subtree (tree, path4, NULL));
1535 _dbus_assert (!find_subtree (tree, path5, NULL));
1536 _dbus_assert (!find_subtree (tree, path6, NULL));
1537 _dbus_assert (!find_subtree (tree, path7, NULL));
1538 _dbus_assert (!find_subtree (tree, path8, NULL));
1540 _dbus_assert (find_handler (tree, path0, &exact_match) && exact_match);
1541 _dbus_assert (find_handler (tree, path1, &exact_match) == tree->root && !exact_match);
1542 _dbus_assert (find_handler (tree, path2, &exact_match) == tree->root && !exact_match);
1543 _dbus_assert (find_handler (tree, path3, &exact_match) == tree->root && !exact_match);
1544 _dbus_assert (find_handler (tree, path4, &exact_match) == tree->root && !exact_match);
1545 _dbus_assert (find_handler (tree, path5, &exact_match) == tree->root && !exact_match);
1546 _dbus_assert (find_handler (tree, path6, &exact_match) == tree->root && !exact_match);
1547 _dbus_assert (find_handler (tree, path7, &exact_match) == tree->root && !exact_match);
1548 _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
1550 if (!do_register (tree, path1, TRUE, 1, tree_test_data))
1553 _dbus_assert (find_subtree (tree, path0, NULL));
1554 _dbus_assert (find_subtree (tree, path1, NULL));
1555 _dbus_assert (!find_subtree (tree, path2, NULL));
1556 _dbus_assert (!find_subtree (tree, path3, NULL));
1557 _dbus_assert (!find_subtree (tree, path4, NULL));
1558 _dbus_assert (!find_subtree (tree, path5, NULL));
1559 _dbus_assert (!find_subtree (tree, path6, NULL));
1560 _dbus_assert (!find_subtree (tree, path7, NULL));
1561 _dbus_assert (!find_subtree (tree, path8, NULL));
1563 _dbus_assert (find_handler (tree, path0, &exact_match) && exact_match);
1564 _dbus_assert (find_handler (tree, path1, &exact_match) && exact_match);
1565 _dbus_assert (find_handler (tree, path2, &exact_match) && !exact_match);
1566 _dbus_assert (find_handler (tree, path3, &exact_match) && !exact_match);
1567 _dbus_assert (find_handler (tree, path4, &exact_match) && !exact_match);
1568 _dbus_assert (find_handler (tree, path5, &exact_match) == tree->root && !exact_match);
1569 _dbus_assert (find_handler (tree, path6, &exact_match) == tree->root && !exact_match);
1570 _dbus_assert (find_handler (tree, path7, &exact_match) == tree->root && !exact_match);
1571 _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
1573 if (!do_register (tree, path2, TRUE, 2, tree_test_data))
1576 _dbus_assert (find_subtree (tree, path1, NULL));
1577 _dbus_assert (find_subtree (tree, path2, NULL));
1578 _dbus_assert (!find_subtree (tree, path3, NULL));
1579 _dbus_assert (!find_subtree (tree, path4, NULL));
1580 _dbus_assert (!find_subtree (tree, path5, NULL));
1581 _dbus_assert (!find_subtree (tree, path6, NULL));
1582 _dbus_assert (!find_subtree (tree, path7, NULL));
1583 _dbus_assert (!find_subtree (tree, path8, NULL));
1585 if (!do_register (tree, path3, TRUE, 3, tree_test_data))
1588 _dbus_assert (find_subtree (tree, path0, NULL));
1589 _dbus_assert (find_subtree (tree, path1, NULL));
1590 _dbus_assert (find_subtree (tree, path2, NULL));
1591 _dbus_assert (find_subtree (tree, path3, NULL));
1592 _dbus_assert (!find_subtree (tree, path4, NULL));
1593 _dbus_assert (!find_subtree (tree, path5, NULL));
1594 _dbus_assert (!find_subtree (tree, path6, NULL));
1595 _dbus_assert (!find_subtree (tree, path7, NULL));
1596 _dbus_assert (!find_subtree (tree, path8, NULL));
1598 if (!do_register (tree, path4, TRUE, 4, tree_test_data))
1601 _dbus_assert (find_subtree (tree, path0, NULL));
1602 _dbus_assert (find_subtree (tree, path1, NULL));
1603 _dbus_assert (find_subtree (tree, path2, NULL));
1604 _dbus_assert (find_subtree (tree, path3, NULL));
1605 _dbus_assert (find_subtree (tree, path4, NULL));
1606 _dbus_assert (!find_subtree (tree, path5, NULL));
1607 _dbus_assert (!find_subtree (tree, path6, NULL));
1608 _dbus_assert (!find_subtree (tree, path7, NULL));
1609 _dbus_assert (!find_subtree (tree, path8, NULL));
1611 if (!do_register (tree, path5, TRUE, 5, tree_test_data))
1614 _dbus_assert (find_subtree (tree, path0, NULL));
1615 _dbus_assert (find_subtree (tree, path1, NULL));
1616 _dbus_assert (find_subtree (tree, path2, NULL));
1617 _dbus_assert (find_subtree (tree, path3, NULL));
1618 _dbus_assert (find_subtree (tree, path4, NULL));
1619 _dbus_assert (find_subtree (tree, path5, NULL));
1620 _dbus_assert (!find_subtree (tree, path6, NULL));
1621 _dbus_assert (!find_subtree (tree, path7, NULL));
1622 _dbus_assert (!find_subtree (tree, path8, NULL));
1624 _dbus_assert (find_handler (tree, path0, &exact_match) == tree->root && exact_match);
1625 _dbus_assert (find_handler (tree, path1, &exact_match) != tree->root && exact_match);
1626 _dbus_assert (find_handler (tree, path2, &exact_match) != tree->root && exact_match);
1627 _dbus_assert (find_handler (tree, path3, &exact_match) != tree->root && exact_match);
1628 _dbus_assert (find_handler (tree, path4, &exact_match) != tree->root && exact_match);
1629 _dbus_assert (find_handler (tree, path5, &exact_match) != tree->root && exact_match);
1630 _dbus_assert (find_handler (tree, path6, &exact_match) != tree->root && !exact_match);
1631 _dbus_assert (find_handler (tree, path7, &exact_match) != tree->root && !exact_match);
1632 _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
1634 if (!do_register (tree, path6, TRUE, 6, tree_test_data))
1637 _dbus_assert (find_subtree (tree, path0, NULL));
1638 _dbus_assert (find_subtree (tree, path1, NULL));
1639 _dbus_assert (find_subtree (tree, path2, NULL));
1640 _dbus_assert (find_subtree (tree, path3, NULL));
1641 _dbus_assert (find_subtree (tree, path4, NULL));
1642 _dbus_assert (find_subtree (tree, path5, NULL));
1643 _dbus_assert (find_subtree (tree, path6, NULL));
1644 _dbus_assert (!find_subtree (tree, path7, NULL));
1645 _dbus_assert (!find_subtree (tree, path8, NULL));
1647 if (!do_register (tree, path7, TRUE, 7, tree_test_data))
1650 _dbus_assert (find_subtree (tree, path0, NULL));
1651 _dbus_assert (find_subtree (tree, path1, NULL));
1652 _dbus_assert (find_subtree (tree, path2, NULL));
1653 _dbus_assert (find_subtree (tree, path3, NULL));
1654 _dbus_assert (find_subtree (tree, path4, NULL));
1655 _dbus_assert (find_subtree (tree, path5, NULL));
1656 _dbus_assert (find_subtree (tree, path6, NULL));
1657 _dbus_assert (find_subtree (tree, path7, NULL));
1658 _dbus_assert (!find_subtree (tree, path8, NULL));
1660 if (!do_register (tree, path8, TRUE, 8, tree_test_data))
1663 _dbus_assert (find_subtree (tree, path0, NULL));
1664 _dbus_assert (find_subtree (tree, path1, NULL));
1665 _dbus_assert (find_subtree (tree, path2, NULL));
1666 _dbus_assert (find_subtree (tree, path3, NULL));
1667 _dbus_assert (find_subtree (tree, path4, NULL));
1668 _dbus_assert (find_subtree (tree, path5, NULL));
1669 _dbus_assert (find_subtree (tree, path6, NULL));
1670 _dbus_assert (find_subtree (tree, path7, NULL));
1671 _dbus_assert (find_subtree (tree, path8, NULL));
1673 _dbus_assert (find_handler (tree, path0, &exact_match) == tree->root && exact_match);
1674 _dbus_assert (find_handler (tree, path1, &exact_match) != tree->root && exact_match);
1675 _dbus_assert (find_handler (tree, path2, &exact_match) != tree->root && exact_match);
1676 _dbus_assert (find_handler (tree, path3, &exact_match) != tree->root && exact_match);
1677 _dbus_assert (find_handler (tree, path4, &exact_match) != tree->root && exact_match);
1678 _dbus_assert (find_handler (tree, path5, &exact_match) != tree->root && exact_match);
1679 _dbus_assert (find_handler (tree, path6, &exact_match) != tree->root && exact_match);
1680 _dbus_assert (find_handler (tree, path7, &exact_match) != tree->root && exact_match);
1681 _dbus_assert (find_handler (tree, path8, &exact_match) != tree->root && exact_match);
1683 /* test the list_registered function */
1686 const char *root[] = { NULL };
1687 char **child_entries;
1690 _dbus_object_tree_list_registered_unlocked (tree, path1, &child_entries);
1691 if (child_entries != NULL)
1693 nb = string_array_length ((const char**)child_entries);
1694 _dbus_assert (nb == 1);
1695 dbus_free_string_array (child_entries);
1698 _dbus_object_tree_list_registered_unlocked (tree, path2, &child_entries);
1699 if (child_entries != NULL)
1701 nb = string_array_length ((const char**)child_entries);
1702 _dbus_assert (nb == 2);
1703 dbus_free_string_array (child_entries);
1706 _dbus_object_tree_list_registered_unlocked (tree, path8, &child_entries);
1707 if (child_entries != NULL)
1709 nb = string_array_length ((const char**)child_entries);
1710 _dbus_assert (nb == 0);
1711 dbus_free_string_array (child_entries);
1714 _dbus_object_tree_list_registered_unlocked (tree, root, &child_entries);
1715 if (child_entries != NULL)
1717 nb = string_array_length ((const char**)child_entries);
1718 _dbus_assert (nb == 3);
1719 dbus_free_string_array (child_entries);
1723 /* Check that destroying tree calls unregister funcs */
1724 _dbus_object_tree_unref (tree);
1727 while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1729 _dbus_assert (tree_test_data[i].handler_unregistered);
1730 _dbus_assert (!tree_test_data[i].message_handled);
1734 /* Now start again and try the individual unregister function */
1735 tree = _dbus_object_tree_new (NULL);
1739 if (!do_register (tree, path0, TRUE, 0, tree_test_data))
1741 if (!do_register (tree, path1, TRUE, 1, tree_test_data))
1743 if (!do_register (tree, path2, TRUE, 2, tree_test_data))
1745 if (!do_register (tree, path3, TRUE, 3, tree_test_data))
1747 if (!do_register (tree, path4, TRUE, 4, tree_test_data))
1749 if (!do_register (tree, path5, TRUE, 5, tree_test_data))
1751 if (!do_register (tree, path6, TRUE, 6, tree_test_data))
1753 if (!do_register (tree, path7, TRUE, 7, tree_test_data))
1755 if (!do_register (tree, path8, TRUE, 8, tree_test_data))
1758 _dbus_object_tree_unregister_and_unlock (tree, path0);
1759 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path0) == NULL);
1761 _dbus_assert (!find_subtree (tree, path0, NULL));
1762 _dbus_assert (find_subtree (tree, path1, NULL));
1763 _dbus_assert (find_subtree (tree, path2, NULL));
1764 _dbus_assert (find_subtree (tree, path3, NULL));
1765 _dbus_assert (find_subtree (tree, path4, NULL));
1766 _dbus_assert (find_subtree (tree, path5, NULL));
1767 _dbus_assert (find_subtree (tree, path6, NULL));
1768 _dbus_assert (find_subtree (tree, path7, NULL));
1769 _dbus_assert (find_subtree (tree, path8, NULL));
1771 _dbus_object_tree_unregister_and_unlock (tree, path1);
1772 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path1) == NULL);
1774 _dbus_assert (!find_subtree (tree, path0, NULL));
1775 _dbus_assert (!find_subtree (tree, path1, NULL));
1776 _dbus_assert (find_subtree (tree, path2, NULL));
1777 _dbus_assert (find_subtree (tree, path3, NULL));
1778 _dbus_assert (find_subtree (tree, path4, NULL));
1779 _dbus_assert (find_subtree (tree, path5, NULL));
1780 _dbus_assert (find_subtree (tree, path6, NULL));
1781 _dbus_assert (find_subtree (tree, path7, NULL));
1782 _dbus_assert (find_subtree (tree, path8, NULL));
1784 _dbus_object_tree_unregister_and_unlock (tree, path2);
1785 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path2) == NULL);
1787 _dbus_assert (!find_subtree (tree, path0, NULL));
1788 _dbus_assert (!find_subtree (tree, path1, NULL));
1789 _dbus_assert (!find_subtree (tree, path2, NULL));
1790 _dbus_assert (find_subtree (tree, path3, NULL));
1791 _dbus_assert (find_subtree (tree, path4, NULL));
1792 _dbus_assert (find_subtree (tree, path5, NULL));
1793 _dbus_assert (find_subtree (tree, path6, NULL));
1794 _dbus_assert (find_subtree (tree, path7, NULL));
1795 _dbus_assert (find_subtree (tree, path8, NULL));
1797 _dbus_object_tree_unregister_and_unlock (tree, path3);
1798 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path3) == NULL);
1800 _dbus_assert (!find_subtree (tree, path0, NULL));
1801 _dbus_assert (!find_subtree (tree, path1, NULL));
1802 _dbus_assert (!find_subtree (tree, path2, NULL));
1803 _dbus_assert (!find_subtree (tree, path3, NULL));
1804 _dbus_assert (find_subtree (tree, path4, NULL));
1805 _dbus_assert (find_subtree (tree, path5, NULL));
1806 _dbus_assert (find_subtree (tree, path6, NULL));
1807 _dbus_assert (find_subtree (tree, path7, NULL));
1808 _dbus_assert (find_subtree (tree, path8, NULL));
1810 _dbus_object_tree_unregister_and_unlock (tree, path4);
1811 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path4) == NULL);
1813 _dbus_assert (!find_subtree (tree, path0, NULL));
1814 _dbus_assert (!find_subtree (tree, path1, NULL));
1815 _dbus_assert (!find_subtree (tree, path2, NULL));
1816 _dbus_assert (!find_subtree (tree, path3, NULL));
1817 _dbus_assert (!find_subtree (tree, path4, NULL));
1818 _dbus_assert (find_subtree (tree, path5, NULL));
1819 _dbus_assert (find_subtree (tree, path6, NULL));
1820 _dbus_assert (find_subtree (tree, path7, NULL));
1821 _dbus_assert (find_subtree (tree, path8, NULL));
1823 _dbus_object_tree_unregister_and_unlock (tree, path5);
1824 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path5) == NULL);
1826 _dbus_assert (!find_subtree (tree, path0, NULL));
1827 _dbus_assert (!find_subtree (tree, path1, NULL));
1828 _dbus_assert (!find_subtree (tree, path2, NULL));
1829 _dbus_assert (!find_subtree (tree, path3, NULL));
1830 _dbus_assert (!find_subtree (tree, path4, NULL));
1831 _dbus_assert (!find_subtree (tree, path5, NULL));
1832 _dbus_assert (find_subtree (tree, path6, NULL));
1833 _dbus_assert (find_subtree (tree, path7, NULL));
1834 _dbus_assert (find_subtree (tree, path8, NULL));
1836 _dbus_object_tree_unregister_and_unlock (tree, path6);
1837 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path6) == NULL);
1839 _dbus_assert (!find_subtree (tree, path0, NULL));
1840 _dbus_assert (!find_subtree (tree, path1, NULL));
1841 _dbus_assert (!find_subtree (tree, path2, NULL));
1842 _dbus_assert (!find_subtree (tree, path3, NULL));
1843 _dbus_assert (!find_subtree (tree, path4, NULL));
1844 _dbus_assert (!find_subtree (tree, path5, NULL));
1845 _dbus_assert (!find_subtree (tree, path6, NULL));
1846 _dbus_assert (find_subtree (tree, path7, NULL));
1847 _dbus_assert (find_subtree (tree, path8, NULL));
1849 _dbus_object_tree_unregister_and_unlock (tree, path7);
1850 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path7) == NULL);
1852 _dbus_assert (!find_subtree (tree, path0, NULL));
1853 _dbus_assert (!find_subtree (tree, path1, NULL));
1854 _dbus_assert (!find_subtree (tree, path2, NULL));
1855 _dbus_assert (!find_subtree (tree, path3, NULL));
1856 _dbus_assert (!find_subtree (tree, path4, NULL));
1857 _dbus_assert (!find_subtree (tree, path5, NULL));
1858 _dbus_assert (!find_subtree (tree, path6, NULL));
1859 _dbus_assert (!find_subtree (tree, path7, NULL));
1860 _dbus_assert (find_subtree (tree, path8, NULL));
1862 _dbus_object_tree_unregister_and_unlock (tree, path8);
1863 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path8) == NULL);
1865 _dbus_assert (!find_subtree (tree, path0, NULL));
1866 _dbus_assert (!find_subtree (tree, path1, NULL));
1867 _dbus_assert (!find_subtree (tree, path2, NULL));
1868 _dbus_assert (!find_subtree (tree, path3, NULL));
1869 _dbus_assert (!find_subtree (tree, path4, NULL));
1870 _dbus_assert (!find_subtree (tree, path5, NULL));
1871 _dbus_assert (!find_subtree (tree, path6, NULL));
1872 _dbus_assert (!find_subtree (tree, path7, NULL));
1873 _dbus_assert (!find_subtree (tree, path8, NULL));
1876 while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1878 _dbus_assert (tree_test_data[i].handler_unregistered);
1879 _dbus_assert (!tree_test_data[i].message_handled);
1883 /* Register it all again, and test dispatch */
1885 if (!do_register (tree, path0, TRUE, 0, tree_test_data))
1887 if (!do_register (tree, path1, FALSE, 1, tree_test_data))
1889 if (!do_register (tree, path2, TRUE, 2, tree_test_data))
1891 if (!do_register (tree, path3, TRUE, 3, tree_test_data))
1893 if (!do_register (tree, path4, TRUE, 4, tree_test_data))
1895 if (!do_register (tree, path5, TRUE, 5, tree_test_data))
1897 if (!do_register (tree, path6, FALSE, 6, tree_test_data))
1899 if (!do_register (tree, path7, TRUE, 7, tree_test_data))
1901 if (!do_register (tree, path8, TRUE, 8, tree_test_data))
1908 if (!do_test_dispatch (tree, path0, 0, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1910 if (!do_test_dispatch (tree, path1, 1, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1912 if (!do_test_dispatch (tree, path2, 2, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1914 if (!do_test_dispatch (tree, path3, 3, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1916 if (!do_test_dispatch (tree, path4, 4, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1918 if (!do_test_dispatch (tree, path5, 5, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1920 if (!do_test_dispatch (tree, path6, 6, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1922 if (!do_test_dispatch (tree, path7, 7, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1924 if (!do_test_dispatch (tree, path8, 8, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1931 _dbus_object_tree_ref (tree);
1932 _dbus_object_tree_unref (tree);
1933 _dbus_object_tree_unref (tree);
1940 * @ingroup DBusObjectTree
1941 * Unit test for DBusObjectTree
1942 * @returns #TRUE on success.
1945 _dbus_object_tree_test (void)
1947 _dbus_test_oom_handling ("object tree",
1948 object_tree_test_iteration,
1954 #endif /* !DOXYGEN_SHOULD_SKIP_THIS */
1956 #endif /* DBUS_BUILD_TESTS */