1 /* -*- mode: C; c-file-style: "gnu" -*- */
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., 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 DBusObjectSubtree* _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
124 * @returns the object tree
127 _dbus_object_tree_ref (DBusObjectTree *tree)
129 _dbus_assert (tree->refcount > 0);
137 * Decrement the reference count
138 * @param tree the object tree
141 _dbus_object_tree_unref (DBusObjectTree *tree)
143 _dbus_assert (tree->refcount > 0);
147 if (tree->refcount == 0)
149 _dbus_object_tree_free_all_unlocked (tree);
156 subtree_cmp (DBusObjectSubtree *subtree_a,
157 DBusObjectSubtree *subtree_b)
159 return strcmp (subtree_a->name, subtree_b->name);
163 subtree_qsort_cmp (const void *a,
166 DBusObjectSubtree **subtree_a_p = (void*) a;
167 DBusObjectSubtree **subtree_b_p = (void*) b;
169 return subtree_cmp (*subtree_a_p, *subtree_b_p);
173 ensure_sorted (DBusObjectSubtree *subtree)
175 if (subtree->subtrees && !subtree->subtrees_sorted)
177 qsort (subtree->subtrees,
179 sizeof (DBusObjectSubtree*),
181 subtree->subtrees_sorted = TRUE;
185 /** Set to 1 to get a bunch of debug spew about finding the
188 #define VERBOSE_FIND 0
190 static DBusObjectSubtree*
191 find_subtree_recurse (DBusObjectSubtree *subtree,
193 dbus_bool_t create_if_not_found,
194 int *index_in_parent,
195 dbus_bool_t *exact_match)
198 dbus_bool_t return_deepest_match;
200 return_deepest_match = exact_match != NULL;
202 _dbus_assert (!(return_deepest_match && create_if_not_found));
207 _dbus_verbose (" path exhausted, returning %s\n",
210 if (exact_match != NULL)
216 _dbus_verbose (" searching children of %s for %s\n",
217 subtree->name, path[0]);
220 ensure_sorted (subtree);
222 /* FIXME we should do a binary search here instead
227 while (i < subtree->n_subtrees)
231 v = strcmp (path[0], subtree->subtrees[i]->name);
234 _dbus_verbose (" %s cmp %s = %d\n",
235 path[0], subtree->subtrees[i]->name,
244 _dbus_verbose (" storing parent index %d\n", i);
246 *index_in_parent = i;
249 if (return_deepest_match)
251 DBusObjectSubtree *next;
253 next = find_subtree_recurse (subtree->subtrees[i],
254 &path[1], create_if_not_found,
255 index_in_parent, exact_match);
257 subtree->invoke_as_fallback)
260 _dbus_verbose (" no deeper match found, returning %s\n",
263 if (exact_match != NULL)
264 *exact_match = FALSE;
271 return find_subtree_recurse (subtree->subtrees[i],
272 &path[1], create_if_not_found,
273 index_in_parent, exact_match);
285 _dbus_verbose (" no match found, current tree %s, create_if_not_found = %d\n",
286 subtree->name, create_if_not_found);
289 if (create_if_not_found)
291 DBusObjectSubtree* child;
292 DBusObjectSubtree **new_subtrees;
296 _dbus_verbose (" creating subtree %s\n",
300 child = _dbus_object_subtree_new (path[0],
305 /* FIXME we should do the "double alloc each time" standard thing */
306 new_n_subtrees = subtree->n_subtrees + 1;
307 new_subtrees = dbus_realloc (subtree->subtrees,
308 new_n_subtrees * sizeof (DBusObjectSubtree*));
309 if (new_subtrees == NULL)
311 child->unregister_function = NULL;
312 child->message_function = NULL;
313 _dbus_object_subtree_unref (child);
317 new_subtrees[subtree->n_subtrees] = child;
319 *index_in_parent = subtree->n_subtrees;
320 subtree->subtrees_sorted = FALSE;
321 subtree->n_subtrees = new_n_subtrees;
322 subtree->subtrees = new_subtrees;
324 child->parent = subtree;
326 return find_subtree_recurse (child,
327 &path[1], create_if_not_found,
328 index_in_parent, exact_match);
332 if (exact_match != NULL)
333 *exact_match = FALSE;
334 return (return_deepest_match && subtree->invoke_as_fallback) ? subtree : NULL;
338 static DBusObjectSubtree*
339 find_subtree (DBusObjectTree *tree,
341 int *index_in_parent)
343 DBusObjectSubtree *subtree;
346 _dbus_verbose ("Looking for exact registered subtree\n");
349 subtree = find_subtree_recurse (tree->root, path, FALSE, index_in_parent, NULL);
351 if (subtree && subtree->message_function == NULL)
357 static DBusObjectSubtree*
358 lookup_subtree (DBusObjectTree *tree,
362 _dbus_verbose ("Looking for subtree\n");
364 return find_subtree_recurse (tree->root, path, FALSE, NULL, NULL);
367 static DBusObjectSubtree*
368 find_handler (DBusObjectTree *tree,
370 dbus_bool_t *exact_match)
373 _dbus_verbose ("Looking for deepest handler\n");
375 _dbus_assert (exact_match != NULL);
376 return find_subtree_recurse (tree->root, path, FALSE, NULL, exact_match);
379 static DBusObjectSubtree*
380 ensure_subtree (DBusObjectTree *tree,
384 _dbus_verbose ("Ensuring subtree\n");
386 return find_subtree_recurse (tree->root, path, TRUE, NULL, NULL);
390 * Registers a new subtree in the global object tree.
392 * @param tree the global object tree
393 * @param fallback #TRUE to handle messages to children of this path
394 * @param path NULL-terminated array of path elements giving path to subtree
395 * @param vtable the vtable used to traverse this subtree
396 * @param user_data user data to pass to methods in the vtable
397 * @returns #FALSE if not enough memory
400 _dbus_object_tree_register (DBusObjectTree *tree,
401 dbus_bool_t fallback,
403 const DBusObjectPathVTable *vtable,
406 DBusObjectSubtree *subtree;
408 _dbus_assert (tree != NULL);
409 _dbus_assert (vtable->message_function != NULL);
410 _dbus_assert (path != NULL);
412 subtree = ensure_subtree (tree, path);
416 #ifndef DBUS_DISABLE_CHECKS
417 if (subtree->message_function != NULL)
419 _dbus_warn ("A handler is already registered for the path starting with path[0] = \"%s\"\n",
420 path[0] ? path[0] : "null");
424 _dbus_assert (subtree->message_function == NULL);
427 subtree->message_function = vtable->message_function;
428 subtree->unregister_function = vtable->unregister_function;
429 subtree->user_data = user_data;
430 subtree->invoke_as_fallback = fallback != FALSE;
436 * Unregisters an object subtree that was registered with the
439 * @param tree the global object tree
440 * @param path path to the subtree (same as the one passed to _dbus_object_tree_register())
443 _dbus_object_tree_unregister_and_unlock (DBusObjectTree *tree,
447 DBusObjectSubtree *subtree;
448 DBusObjectPathUnregisterFunction unregister_function;
450 DBusConnection *connection;
452 _dbus_assert (path != NULL);
454 subtree = find_subtree (tree, path, &i);
456 #ifndef DBUS_DISABLE_CHECKS
459 _dbus_warn ("Attempted to unregister path (path[0] = %s path[1] = %s) which isn't registered\n",
460 path[0] ? path[0] : "null",
461 path[1] ? path[1] : "null");
465 _dbus_assert (subtree != NULL);
468 _dbus_assert (subtree->parent == NULL ||
469 (i >= 0 && subtree->parent->subtrees[i] == subtree));
471 subtree->message_function = NULL;
473 unregister_function = subtree->unregister_function;
474 user_data = subtree->user_data;
476 subtree->unregister_function = NULL;
477 subtree->user_data = NULL;
479 /* If we have no subtrees of our own, remove from
480 * our parent (FIXME could also be more aggressive
481 * and remove our parent if it becomes empty)
483 if (subtree->parent && subtree->n_subtrees == 0)
485 /* assumes a 0-byte memmove is OK */
486 memmove (&subtree->parent->subtrees[i],
487 &subtree->parent->subtrees[i+1],
488 (subtree->parent->n_subtrees - i - 1) *
489 sizeof (subtree->parent->subtrees[0]));
490 subtree->parent->n_subtrees -= 1;
492 subtree->parent = NULL;
494 _dbus_object_subtree_unref (subtree);
498 connection = tree->connection;
500 /* Unlock and call application code */
501 #ifdef DBUS_BUILD_TESTS
505 _dbus_connection_ref_unlocked (connection);
506 _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
507 _dbus_connection_unlock (connection);
510 if (unregister_function)
511 (* unregister_function) (connection, user_data);
513 #ifdef DBUS_BUILD_TESTS
516 dbus_connection_unref (connection);
520 free_subtree_recurse (DBusConnection *connection,
521 DBusObjectSubtree *subtree)
523 /* Delete them from the end, for slightly
524 * more robustness against odd reentrancy.
526 while (subtree->n_subtrees > 0)
528 DBusObjectSubtree *child;
530 child = subtree->subtrees[subtree->n_subtrees - 1];
531 subtree->subtrees[subtree->n_subtrees - 1] = NULL;
532 subtree->n_subtrees -= 1;
533 child->parent = NULL;
535 free_subtree_recurse (connection, child);
538 /* Call application code */
539 if (subtree->unregister_function)
540 (* subtree->unregister_function) (connection,
543 subtree->message_function = NULL;
544 subtree->unregister_function = NULL;
545 subtree->user_data = NULL;
547 /* Now free ourselves */
548 _dbus_object_subtree_unref (subtree);
552 * Free all the handlers in the tree. Lock on tree's connection
555 * @param tree the object tree
558 _dbus_object_tree_free_all_unlocked (DBusObjectTree *tree)
561 free_subtree_recurse (tree->connection,
567 _dbus_object_tree_list_registered_unlocked (DBusObjectTree *tree,
568 const char **parent_path,
569 char ***child_entries)
571 DBusObjectSubtree *subtree;
574 _dbus_assert (parent_path != NULL);
575 _dbus_assert (child_entries != NULL);
577 *child_entries = NULL;
579 subtree = lookup_subtree (tree, parent_path);
582 retval = dbus_new0 (char *, 1);
587 retval = dbus_new0 (char*, subtree->n_subtrees + 1);
591 while (i < subtree->n_subtrees)
593 retval[i] = _dbus_strdup (subtree->subtrees[i]->name);
594 if (retval[i] == NULL)
596 dbus_free_string_array (retval);
606 *child_entries = retval;
607 return retval != NULL;
610 static DBusHandlerResult
611 handle_default_introspect_and_unlock (DBusObjectTree *tree,
612 DBusMessage *message,
616 DBusHandlerResult result;
620 DBusMessageIter iter;
621 const char *v_STRING;
622 dbus_bool_t already_unlocked;
624 /* We have the connection lock here */
626 already_unlocked = FALSE;
628 _dbus_verbose (" considering default Introspect() handler...\n");
632 if (!dbus_message_is_method_call (message,
633 DBUS_INTERFACE_INTROSPECTABLE,
636 #ifdef DBUS_BUILD_TESTS
637 if (tree->connection)
640 _dbus_verbose ("unlock %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
641 _dbus_connection_unlock (tree->connection);
644 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
647 _dbus_verbose (" using default Introspect() handler!\n");
649 if (!_dbus_string_init (&xml))
651 #ifdef DBUS_BUILD_TESTS
652 if (tree->connection)
655 _dbus_verbose ("unlock %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
656 _dbus_connection_unlock (tree->connection);
659 return DBUS_HANDLER_RESULT_NEED_MEMORY;
662 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
665 if (!_dbus_object_tree_list_registered_unlocked (tree, path, &children))
668 if (!_dbus_string_append (&xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
671 if (!_dbus_string_append (&xml, "<node>\n"))
675 while (children[i] != NULL)
677 if (!_dbus_string_append_printf (&xml, " <node name=\"%s\"/>\n",
684 if (!_dbus_string_append (&xml, "</node>\n"))
687 reply = dbus_message_new_method_return (message);
691 dbus_message_iter_init_append (reply, &iter);
692 v_STRING = _dbus_string_get_const_data (&xml);
693 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &v_STRING))
696 #ifdef DBUS_BUILD_TESTS
697 if (tree->connection)
700 already_unlocked = TRUE;
702 if (!_dbus_connection_send_and_unlock (tree->connection, reply, NULL))
706 result = DBUS_HANDLER_RESULT_HANDLED;
709 #ifdef DBUS_BUILD_TESTS
710 if (tree->connection)
713 if (!already_unlocked)
715 _dbus_verbose ("unlock %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
716 _dbus_connection_unlock (tree->connection);
720 _dbus_string_free (&xml);
721 dbus_free_string_array (children);
723 dbus_message_unref (reply);
729 * Tries to dispatch a message by directing it to handler for the
730 * object path listed in the message header, if any. Messages are
731 * dispatched first to the registered handler that matches the largest
732 * number of path elements; that is, message to /foo/bar/baz would go
733 * to the handler for /foo/bar before the one for /foo.
735 * @todo thread problems
737 * @param tree the global object tree
738 * @param message the message to dispatch
739 * @returns whether message was handled successfully
742 _dbus_object_tree_dispatch_and_unlock (DBusObjectTree *tree,
743 DBusMessage *message)
746 dbus_bool_t exact_match;
749 DBusHandlerResult result;
750 DBusObjectSubtree *subtree;
753 _dbus_verbose ("Dispatch of message by object path\n");
757 if (!dbus_message_get_path_decomposed (message, &path))
759 #ifdef DBUS_BUILD_TESTS
760 if (tree->connection)
763 _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
764 _dbus_connection_unlock (tree->connection);
767 _dbus_verbose ("No memory to get decomposed path\n");
769 return DBUS_HANDLER_RESULT_NEED_MEMORY;
774 #ifdef DBUS_BUILD_TESTS
775 if (tree->connection)
778 _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
779 _dbus_connection_unlock (tree->connection);
782 _dbus_verbose ("No path field in message\n");
783 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
786 /* Find the deepest path that covers the path in the message */
787 subtree = find_handler (tree, (const char**) path, &exact_match);
789 /* Build a list of all paths that cover the path in the message */
793 while (subtree != NULL)
795 if (subtree->message_function != NULL && (exact_match || subtree->invoke_as_fallback))
797 _dbus_object_subtree_ref (subtree);
799 /* run deepest paths first */
800 if (!_dbus_list_append (&list, subtree))
802 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
803 _dbus_object_subtree_unref (subtree);
804 goto free_and_return;
809 subtree = subtree->parent;
812 _dbus_verbose ("%d handlers in the path tree for this message\n",
813 _dbus_list_get_length (&list));
815 /* Invoke each handler in the list */
817 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
819 link = _dbus_list_get_first_link (&list);
822 DBusList *next = _dbus_list_get_next_link (&list, link);
823 subtree = link->data;
825 /* message_function is NULL if we're unregistered
828 if (subtree->message_function)
830 DBusObjectPathMessageFunction message_function;
833 message_function = subtree->message_function;
834 user_data = subtree->user_data;
837 _dbus_verbose (" (invoking a handler)\n");
840 #ifdef DBUS_BUILD_TESTS
841 if (tree->connection)
844 _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
845 _dbus_connection_unlock (tree->connection);
848 /* FIXME you could unregister the subtree in another thread
849 * before we invoke the callback, and I can't figure out a
850 * good way to solve this.
853 result = (* message_function) (tree->connection,
857 #ifdef DBUS_BUILD_TESTS
858 if (tree->connection)
860 _dbus_connection_lock (tree->connection);
862 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
863 goto free_and_return;
871 if (result == DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
873 /* This hardcoded default handler does a minimal Introspect()
875 result = handle_default_introspect_and_unlock (tree, message,
876 (const char**) path);
880 #ifdef DBUS_BUILD_TESTS
881 if (tree->connection)
884 _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
885 _dbus_connection_unlock (tree->connection);
891 link = _dbus_list_get_first_link (&list);
892 _dbus_object_subtree_unref (link->data);
893 _dbus_list_remove_link (&list, link);
896 dbus_free_string_array (path);
902 * Allocates a subtree object.
904 * @param name name to duplicate.
905 * @returns newly-allocated subtree
907 static DBusObjectSubtree*
908 allocate_subtree_object (const char *name)
911 DBusObjectSubtree *subtree;
912 const size_t front_padding = _DBUS_STRUCT_OFFSET (DBusObjectSubtree, name);
914 _dbus_assert (name != NULL);
918 subtree = dbus_malloc (front_padding + (len + 1));
923 memcpy (subtree->name, name, len + 1);
928 static DBusObjectSubtree*
929 _dbus_object_subtree_new (const char *name,
930 const DBusObjectPathVTable *vtable,
933 DBusObjectSubtree *subtree;
935 subtree = allocate_subtree_object (name);
939 _dbus_assert (name != NULL);
941 subtree->parent = NULL;
945 subtree->message_function = vtable->message_function;
946 subtree->unregister_function = vtable->unregister_function;
950 subtree->message_function = NULL;
951 subtree->unregister_function = NULL;
954 subtree->user_data = user_data;
955 subtree->refcount.value = 1;
956 subtree->subtrees = NULL;
957 subtree->n_subtrees = 0;
958 subtree->subtrees_sorted = TRUE;
959 subtree->invoke_as_fallback = FALSE;
972 static DBusObjectSubtree *
973 _dbus_object_subtree_ref (DBusObjectSubtree *subtree)
975 _dbus_assert (subtree->refcount.value > 0);
976 _dbus_atomic_inc (&subtree->refcount);
982 _dbus_object_subtree_unref (DBusObjectSubtree *subtree)
984 _dbus_assert (subtree->refcount.value > 0);
986 if (_dbus_atomic_dec (&subtree->refcount) == 1)
988 _dbus_assert (subtree->unregister_function == NULL);
989 _dbus_assert (subtree->message_function == NULL);
991 dbus_free (subtree->subtrees);
997 * Lists the registered fallback handlers and object path handlers at
998 * the given parent_path. The returned array should be freed with
999 * dbus_free_string_array().
1001 * @param tree the object tree
1002 * @param parent_path the path to list the child handlers of
1003 * @param child_entries returns #NULL-terminated array of children
1004 * @returns #FALSE if no memory to allocate the child entries
1007 _dbus_object_tree_list_registered_and_unlock (DBusObjectTree *tree,
1008 const char **parent_path,
1009 char ***child_entries)
1013 result = _dbus_object_tree_list_registered_unlocked (tree,
1017 #ifdef DBUS_BUILD_TESTS
1018 if (tree->connection)
1021 _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
1022 _dbus_connection_unlock (tree->connection);
1029 /** Set to 1 to get a bunch of spew about disassembling the path string */
1030 #define VERBOSE_DECOMPOSE 0
1033 * Decompose an object path. A path of just "/" is
1034 * represented as an empty vector of strings.
1035 * The path need not be nul terminated.
1037 * @param data the path data
1038 * @param len the length of the path string
1039 * @param path address to store new object path
1040 * @param path_len length of stored path
1043 _dbus_decompose_path (const char* data,
1052 _dbus_assert (data != NULL);
1054 #if VERBOSE_DECOMPOSE
1055 _dbus_verbose ("Decomposing path \"%s\"\n",
1060 if (len > 1) /* if path is not just "/" */
1071 retval = dbus_new0 (char*, n_components + 1);
1077 if (n_components == 0)
1081 while (comp < n_components)
1083 _dbus_assert (i < len);
1089 while (j < len && data[j] != '/')
1092 /* Now [i, j) is the path component */
1093 _dbus_assert (i < j);
1094 _dbus_assert (data[i] != '/');
1095 _dbus_assert (j == len || data[j] == '/');
1097 #if VERBOSE_DECOMPOSE
1098 _dbus_verbose (" (component in [%d,%d))\n",
1102 retval[comp] = _dbus_memdup (&data[i], j - i + 1);
1103 if (retval[comp] == NULL)
1105 dbus_free_string_array (retval);
1108 retval[comp][j-i] = '\0';
1109 #if VERBOSE_DECOMPOSE
1110 _dbus_verbose (" (component %d = \"%s\")\n",
1111 comp, retval[comp]);
1117 _dbus_assert (i == len);
1121 *path_len = n_components;
1128 #ifdef DBUS_BUILD_TESTS
1129 #include "dbus-test.h"
1133 flatten_path (const char **path)
1138 if (!_dbus_string_init (&str))
1141 if (path[0] == NULL)
1143 if (!_dbus_string_append_byte (&str, '/'))
1153 if (!_dbus_string_append_byte (&str, '/'))
1156 if (!_dbus_string_append (&str, path[i]))
1163 if (!_dbus_string_steal_data (&str, &s))
1166 _dbus_string_free (&str);
1171 _dbus_string_free (&str);
1183 /* Returns TRUE if container is a parent of child
1185 static StrComparison
1186 path_contains (const char **container,
1192 while (child[i] != NULL)
1196 if (container[i] == NULL)
1197 return STR_PREFIX; /* container ran out, child continues;
1198 * thus the container is a parent of the
1202 _dbus_assert (container[i] != NULL);
1203 _dbus_assert (child[i] != NULL);
1205 v = strcmp (container[i], child[i]);
1208 return STR_DIFFERENT; /* they overlap until here and then are different,
1215 /* Child ran out; if container also did, they are equal;
1216 * otherwise, the child is a parent of the container.
1218 if (container[i] == NULL)
1221 return STR_DIFFERENT;
1226 spew_subtree_recurse (DBusObjectSubtree *subtree,
1234 _dbus_verbose (" ");
1238 _dbus_verbose ("%s (%d children)\n",
1239 subtree->name, subtree->n_subtrees);
1242 while (i < subtree->n_subtrees)
1244 spew_subtree_recurse (subtree->subtrees[i], indent + 2);
1251 spew_tree (DBusObjectTree *tree)
1253 spew_subtree_recurse (tree->root, 0);
1258 * Callback data used in tests
1262 const char **path; /**< Path */
1263 dbus_bool_t handler_fallback; /**< true if the handler may be called as fallback */
1264 dbus_bool_t message_handled; /**< Gets set to true if message handler called */
1265 dbus_bool_t handler_unregistered; /**< gets set to true if handler is unregistered */
1270 test_unregister_function (DBusConnection *connection,
1273 TreeTestData *ttd = user_data;
1275 ttd->handler_unregistered = TRUE;
1278 static DBusHandlerResult
1279 test_message_function (DBusConnection *connection,
1280 DBusMessage *message,
1283 TreeTestData *ttd = user_data;
1285 ttd->message_handled = TRUE;
1287 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1291 do_register (DBusObjectTree *tree,
1293 dbus_bool_t fallback,
1295 TreeTestData *tree_test_data)
1297 DBusObjectPathVTable vtable = { test_unregister_function,
1298 test_message_function, NULL };
1300 tree_test_data[i].message_handled = FALSE;
1301 tree_test_data[i].handler_unregistered = FALSE;
1302 tree_test_data[i].handler_fallback = fallback;
1303 tree_test_data[i].path = path;
1305 if (!_dbus_object_tree_register (tree, fallback, path,
1307 &tree_test_data[i]))
1314 do_test_dispatch (DBusObjectTree *tree,
1317 TreeTestData *tree_test_data,
1320 DBusMessage *message;
1322 DBusHandlerResult result;
1327 flat = flatten_path (path);
1331 message = dbus_message_new_method_call (NULL,
1333 "org.freedesktop.TestInterface",
1336 if (message == NULL)
1340 while (j < n_test_data)
1342 tree_test_data[j].message_handled = FALSE;
1346 result = _dbus_object_tree_dispatch_and_unlock (tree, message);
1347 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
1350 _dbus_assert (tree_test_data[i].message_handled);
1353 while (j < n_test_data)
1355 if (tree_test_data[j].message_handled)
1357 if (tree_test_data[j].handler_fallback)
1358 _dbus_assert (path_contains (tree_test_data[j].path,
1359 path) != STR_DIFFERENT);
1361 _dbus_assert (path_contains (tree_test_data[j].path, path) == STR_EQUAL);
1365 if (tree_test_data[j].handler_fallback)
1366 _dbus_assert (path_contains (tree_test_data[j].path,
1367 path) == STR_DIFFERENT);
1369 _dbus_assert (path_contains (tree_test_data[j].path, path) != STR_EQUAL);
1375 dbus_message_unref (message);
1381 dbus_message_unref (message);
1386 string_array_length (const char **array)
1389 for (i = 0; array[i]; i++) ;
1396 const char *result[20];
1397 } DecomposePathTest;
1399 static DecomposePathTest decompose_tests[] = {
1400 { "/foo", { "foo", NULL } },
1401 { "/foo/bar", { "foo", "bar", NULL } },
1403 { "/a/b", { "a", "b", NULL } },
1404 { "/a/b/c", { "a", "b", "c", NULL } },
1405 { "/a/b/c/d", { "a", "b", "c", "d", NULL } },
1406 { "/foo/bar/q", { "foo", "bar", "q", NULL } },
1407 { "/foo/bar/this/is/longer", { "foo", "bar", "this", "is", "longer", NULL } }
1411 run_decompose_tests (void)
1416 while (i < _DBUS_N_ELEMENTS (decompose_tests))
1422 if (!_dbus_decompose_path (decompose_tests[i].path,
1423 strlen (decompose_tests[i].path),
1424 &result, &result_len))
1427 expected_len = string_array_length (decompose_tests[i].result);
1429 if (result_len != (int) string_array_length ((const char**)result) ||
1430 expected_len != result_len ||
1431 path_contains (decompose_tests[i].result,
1432 (const char**) result) != STR_EQUAL)
1434 int real_len = string_array_length ((const char**)result);
1435 _dbus_warn ("Expected decompose of %s to have len %d, returned %d, appears to have %d\n",
1436 decompose_tests[i].path, expected_len, result_len,
1438 _dbus_warn ("Decompose resulted in elements: { ");
1440 while (i < real_len)
1442 _dbus_warn ("\"%s\"%s", result[i],
1443 (i + 1) == real_len ? "" : ", ");
1447 _dbus_assert_not_reached ("path decompose failed\n");
1450 dbus_free_string_array (result);
1459 object_tree_test_iteration (void *data)
1461 const char *path0[] = { NULL };
1462 const char *path1[] = { "foo", NULL };
1463 const char *path2[] = { "foo", "bar", NULL };
1464 const char *path3[] = { "foo", "bar", "baz", NULL };
1465 const char *path4[] = { "foo", "bar", "boo", NULL };
1466 const char *path5[] = { "blah", NULL };
1467 const char *path6[] = { "blah", "boof", NULL };
1468 const char *path7[] = { "blah", "boof", "this", "is", "really", "long", NULL };
1469 const char *path8[] = { "childless", NULL };
1470 DBusObjectTree *tree;
1471 TreeTestData tree_test_data[9];
1473 dbus_bool_t exact_match;
1475 if (!run_decompose_tests ())
1480 tree = _dbus_object_tree_new (NULL);
1484 if (!do_register (tree, path0, TRUE, 0, tree_test_data))
1487 _dbus_assert (find_subtree (tree, path0, NULL));
1488 _dbus_assert (!find_subtree (tree, path1, NULL));
1489 _dbus_assert (!find_subtree (tree, path2, NULL));
1490 _dbus_assert (!find_subtree (tree, path3, NULL));
1491 _dbus_assert (!find_subtree (tree, path4, NULL));
1492 _dbus_assert (!find_subtree (tree, path5, NULL));
1493 _dbus_assert (!find_subtree (tree, path6, NULL));
1494 _dbus_assert (!find_subtree (tree, path7, NULL));
1495 _dbus_assert (!find_subtree (tree, path8, NULL));
1497 _dbus_assert (find_handler (tree, path0, &exact_match) && exact_match);
1498 _dbus_assert (find_handler (tree, path1, &exact_match) == tree->root && !exact_match);
1499 _dbus_assert (find_handler (tree, path2, &exact_match) == tree->root && !exact_match);
1500 _dbus_assert (find_handler (tree, path3, &exact_match) == tree->root && !exact_match);
1501 _dbus_assert (find_handler (tree, path4, &exact_match) == tree->root && !exact_match);
1502 _dbus_assert (find_handler (tree, path5, &exact_match) == tree->root && !exact_match);
1503 _dbus_assert (find_handler (tree, path6, &exact_match) == tree->root && !exact_match);
1504 _dbus_assert (find_handler (tree, path7, &exact_match) == tree->root && !exact_match);
1505 _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
1507 if (!do_register (tree, path1, TRUE, 1, tree_test_data))
1510 _dbus_assert (find_subtree (tree, path0, NULL));
1511 _dbus_assert (find_subtree (tree, path1, NULL));
1512 _dbus_assert (!find_subtree (tree, path2, NULL));
1513 _dbus_assert (!find_subtree (tree, path3, NULL));
1514 _dbus_assert (!find_subtree (tree, path4, NULL));
1515 _dbus_assert (!find_subtree (tree, path5, NULL));
1516 _dbus_assert (!find_subtree (tree, path6, NULL));
1517 _dbus_assert (!find_subtree (tree, path7, NULL));
1518 _dbus_assert (!find_subtree (tree, path8, NULL));
1520 _dbus_assert (find_handler (tree, path0, &exact_match) && exact_match);
1521 _dbus_assert (find_handler (tree, path1, &exact_match) && exact_match);
1522 _dbus_assert (find_handler (tree, path2, &exact_match) && !exact_match);
1523 _dbus_assert (find_handler (tree, path3, &exact_match) && !exact_match);
1524 _dbus_assert (find_handler (tree, path4, &exact_match) && !exact_match);
1525 _dbus_assert (find_handler (tree, path5, &exact_match) == tree->root && !exact_match);
1526 _dbus_assert (find_handler (tree, path6, &exact_match) == tree->root && !exact_match);
1527 _dbus_assert (find_handler (tree, path7, &exact_match) == tree->root && !exact_match);
1528 _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
1530 if (!do_register (tree, path2, TRUE, 2, tree_test_data))
1533 _dbus_assert (find_subtree (tree, path1, NULL));
1534 _dbus_assert (find_subtree (tree, path2, NULL));
1535 _dbus_assert (!find_subtree (tree, path3, NULL));
1536 _dbus_assert (!find_subtree (tree, path4, NULL));
1537 _dbus_assert (!find_subtree (tree, path5, NULL));
1538 _dbus_assert (!find_subtree (tree, path6, NULL));
1539 _dbus_assert (!find_subtree (tree, path7, NULL));
1540 _dbus_assert (!find_subtree (tree, path8, NULL));
1542 if (!do_register (tree, path3, TRUE, 3, tree_test_data))
1545 _dbus_assert (find_subtree (tree, path0, NULL));
1546 _dbus_assert (find_subtree (tree, path1, NULL));
1547 _dbus_assert (find_subtree (tree, path2, NULL));
1548 _dbus_assert (find_subtree (tree, path3, NULL));
1549 _dbus_assert (!find_subtree (tree, path4, NULL));
1550 _dbus_assert (!find_subtree (tree, path5, NULL));
1551 _dbus_assert (!find_subtree (tree, path6, NULL));
1552 _dbus_assert (!find_subtree (tree, path7, NULL));
1553 _dbus_assert (!find_subtree (tree, path8, NULL));
1555 if (!do_register (tree, path4, TRUE, 4, tree_test_data))
1558 _dbus_assert (find_subtree (tree, path0, NULL));
1559 _dbus_assert (find_subtree (tree, path1, NULL));
1560 _dbus_assert (find_subtree (tree, path2, NULL));
1561 _dbus_assert (find_subtree (tree, path3, NULL));
1562 _dbus_assert (find_subtree (tree, path4, NULL));
1563 _dbus_assert (!find_subtree (tree, path5, NULL));
1564 _dbus_assert (!find_subtree (tree, path6, NULL));
1565 _dbus_assert (!find_subtree (tree, path7, NULL));
1566 _dbus_assert (!find_subtree (tree, path8, NULL));
1568 if (!do_register (tree, path5, TRUE, 5, tree_test_data))
1571 _dbus_assert (find_subtree (tree, path0, NULL));
1572 _dbus_assert (find_subtree (tree, path1, NULL));
1573 _dbus_assert (find_subtree (tree, path2, NULL));
1574 _dbus_assert (find_subtree (tree, path3, NULL));
1575 _dbus_assert (find_subtree (tree, path4, NULL));
1576 _dbus_assert (find_subtree (tree, path5, NULL));
1577 _dbus_assert (!find_subtree (tree, path6, NULL));
1578 _dbus_assert (!find_subtree (tree, path7, NULL));
1579 _dbus_assert (!find_subtree (tree, path8, NULL));
1581 _dbus_assert (find_handler (tree, path0, &exact_match) == tree->root && exact_match);
1582 _dbus_assert (find_handler (tree, path1, &exact_match) != tree->root && exact_match);
1583 _dbus_assert (find_handler (tree, path2, &exact_match) != tree->root && exact_match);
1584 _dbus_assert (find_handler (tree, path3, &exact_match) != tree->root && exact_match);
1585 _dbus_assert (find_handler (tree, path4, &exact_match) != tree->root && exact_match);
1586 _dbus_assert (find_handler (tree, path5, &exact_match) != tree->root && exact_match);
1587 _dbus_assert (find_handler (tree, path6, &exact_match) != tree->root && !exact_match);
1588 _dbus_assert (find_handler (tree, path7, &exact_match) != tree->root && !exact_match);
1589 _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
1591 if (!do_register (tree, path6, TRUE, 6, tree_test_data))
1594 _dbus_assert (find_subtree (tree, path0, NULL));
1595 _dbus_assert (find_subtree (tree, path1, NULL));
1596 _dbus_assert (find_subtree (tree, path2, NULL));
1597 _dbus_assert (find_subtree (tree, path3, NULL));
1598 _dbus_assert (find_subtree (tree, path4, NULL));
1599 _dbus_assert (find_subtree (tree, path5, NULL));
1600 _dbus_assert (find_subtree (tree, path6, NULL));
1601 _dbus_assert (!find_subtree (tree, path7, NULL));
1602 _dbus_assert (!find_subtree (tree, path8, NULL));
1604 if (!do_register (tree, path7, TRUE, 7, tree_test_data))
1607 _dbus_assert (find_subtree (tree, path0, NULL));
1608 _dbus_assert (find_subtree (tree, path1, NULL));
1609 _dbus_assert (find_subtree (tree, path2, NULL));
1610 _dbus_assert (find_subtree (tree, path3, NULL));
1611 _dbus_assert (find_subtree (tree, path4, NULL));
1612 _dbus_assert (find_subtree (tree, path5, NULL));
1613 _dbus_assert (find_subtree (tree, path6, NULL));
1614 _dbus_assert (find_subtree (tree, path7, NULL));
1615 _dbus_assert (!find_subtree (tree, path8, NULL));
1617 if (!do_register (tree, path8, TRUE, 8, tree_test_data))
1620 _dbus_assert (find_subtree (tree, path0, NULL));
1621 _dbus_assert (find_subtree (tree, path1, NULL));
1622 _dbus_assert (find_subtree (tree, path2, NULL));
1623 _dbus_assert (find_subtree (tree, path3, NULL));
1624 _dbus_assert (find_subtree (tree, path4, NULL));
1625 _dbus_assert (find_subtree (tree, path5, NULL));
1626 _dbus_assert (find_subtree (tree, path6, NULL));
1627 _dbus_assert (find_subtree (tree, path7, NULL));
1628 _dbus_assert (find_subtree (tree, path8, NULL));
1630 _dbus_assert (find_handler (tree, path0, &exact_match) == tree->root && exact_match);
1631 _dbus_assert (find_handler (tree, path1, &exact_match) != tree->root && exact_match);
1632 _dbus_assert (find_handler (tree, path2, &exact_match) != tree->root && exact_match);
1633 _dbus_assert (find_handler (tree, path3, &exact_match) != tree->root && exact_match);
1634 _dbus_assert (find_handler (tree, path4, &exact_match) != tree->root && exact_match);
1635 _dbus_assert (find_handler (tree, path5, &exact_match) != tree->root && exact_match);
1636 _dbus_assert (find_handler (tree, path6, &exact_match) != tree->root && exact_match);
1637 _dbus_assert (find_handler (tree, path7, &exact_match) != tree->root && exact_match);
1638 _dbus_assert (find_handler (tree, path8, &exact_match) != tree->root && exact_match);
1640 /* test the list_registered function */
1643 const char *root[] = { NULL };
1644 char **child_entries;
1647 _dbus_object_tree_list_registered_unlocked (tree, path1, &child_entries);
1648 if (child_entries != NULL)
1650 nb = string_array_length ((const char**)child_entries);
1651 _dbus_assert (nb == 1);
1652 dbus_free_string_array (child_entries);
1655 _dbus_object_tree_list_registered_unlocked (tree, path2, &child_entries);
1656 if (child_entries != NULL)
1658 nb = string_array_length ((const char**)child_entries);
1659 _dbus_assert (nb == 2);
1660 dbus_free_string_array (child_entries);
1663 _dbus_object_tree_list_registered_unlocked (tree, path8, &child_entries);
1664 if (child_entries != NULL)
1666 nb = string_array_length ((const char**)child_entries);
1667 _dbus_assert (nb == 0);
1668 dbus_free_string_array (child_entries);
1671 _dbus_object_tree_list_registered_unlocked (tree, root, &child_entries);
1672 if (child_entries != NULL)
1674 nb = string_array_length ((const char**)child_entries);
1675 _dbus_assert (nb == 3);
1676 dbus_free_string_array (child_entries);
1680 /* Check that destroying tree calls unregister funcs */
1681 _dbus_object_tree_unref (tree);
1684 while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1686 _dbus_assert (tree_test_data[i].handler_unregistered);
1687 _dbus_assert (!tree_test_data[i].message_handled);
1691 /* Now start again and try the individual unregister function */
1692 tree = _dbus_object_tree_new (NULL);
1696 if (!do_register (tree, path0, TRUE, 0, tree_test_data))
1698 if (!do_register (tree, path1, TRUE, 1, tree_test_data))
1700 if (!do_register (tree, path2, TRUE, 2, tree_test_data))
1702 if (!do_register (tree, path3, TRUE, 3, tree_test_data))
1704 if (!do_register (tree, path4, TRUE, 4, tree_test_data))
1706 if (!do_register (tree, path5, TRUE, 5, tree_test_data))
1708 if (!do_register (tree, path6, TRUE, 6, tree_test_data))
1710 if (!do_register (tree, path7, TRUE, 7, tree_test_data))
1712 if (!do_register (tree, path8, TRUE, 8, tree_test_data))
1715 _dbus_object_tree_unregister_and_unlock (tree, path0);
1717 _dbus_assert (!find_subtree (tree, path0, NULL));
1718 _dbus_assert (find_subtree (tree, path1, NULL));
1719 _dbus_assert (find_subtree (tree, path2, NULL));
1720 _dbus_assert (find_subtree (tree, path3, NULL));
1721 _dbus_assert (find_subtree (tree, path4, NULL));
1722 _dbus_assert (find_subtree (tree, path5, NULL));
1723 _dbus_assert (find_subtree (tree, path6, NULL));
1724 _dbus_assert (find_subtree (tree, path7, NULL));
1725 _dbus_assert (find_subtree (tree, path8, NULL));
1727 _dbus_object_tree_unregister_and_unlock (tree, path1);
1729 _dbus_assert (!find_subtree (tree, path0, NULL));
1730 _dbus_assert (!find_subtree (tree, path1, NULL));
1731 _dbus_assert (find_subtree (tree, path2, NULL));
1732 _dbus_assert (find_subtree (tree, path3, NULL));
1733 _dbus_assert (find_subtree (tree, path4, NULL));
1734 _dbus_assert (find_subtree (tree, path5, NULL));
1735 _dbus_assert (find_subtree (tree, path6, NULL));
1736 _dbus_assert (find_subtree (tree, path7, NULL));
1737 _dbus_assert (find_subtree (tree, path8, NULL));
1739 _dbus_object_tree_unregister_and_unlock (tree, path2);
1741 _dbus_assert (!find_subtree (tree, path0, NULL));
1742 _dbus_assert (!find_subtree (tree, path1, NULL));
1743 _dbus_assert (!find_subtree (tree, path2, NULL));
1744 _dbus_assert (find_subtree (tree, path3, NULL));
1745 _dbus_assert (find_subtree (tree, path4, NULL));
1746 _dbus_assert (find_subtree (tree, path5, NULL));
1747 _dbus_assert (find_subtree (tree, path6, NULL));
1748 _dbus_assert (find_subtree (tree, path7, NULL));
1749 _dbus_assert (find_subtree (tree, path8, NULL));
1751 _dbus_object_tree_unregister_and_unlock (tree, path3);
1753 _dbus_assert (!find_subtree (tree, path0, NULL));
1754 _dbus_assert (!find_subtree (tree, path1, NULL));
1755 _dbus_assert (!find_subtree (tree, path2, NULL));
1756 _dbus_assert (!find_subtree (tree, path3, NULL));
1757 _dbus_assert (find_subtree (tree, path4, NULL));
1758 _dbus_assert (find_subtree (tree, path5, NULL));
1759 _dbus_assert (find_subtree (tree, path6, NULL));
1760 _dbus_assert (find_subtree (tree, path7, NULL));
1761 _dbus_assert (find_subtree (tree, path8, NULL));
1763 _dbus_object_tree_unregister_and_unlock (tree, path4);
1765 _dbus_assert (!find_subtree (tree, path0, NULL));
1766 _dbus_assert (!find_subtree (tree, path1, NULL));
1767 _dbus_assert (!find_subtree (tree, path2, NULL));
1768 _dbus_assert (!find_subtree (tree, path3, NULL));
1769 _dbus_assert (!find_subtree (tree, path4, NULL));
1770 _dbus_assert (find_subtree (tree, path5, NULL));
1771 _dbus_assert (find_subtree (tree, path6, NULL));
1772 _dbus_assert (find_subtree (tree, path7, NULL));
1773 _dbus_assert (find_subtree (tree, path8, NULL));
1775 _dbus_object_tree_unregister_and_unlock (tree, path5);
1777 _dbus_assert (!find_subtree (tree, path0, NULL));
1778 _dbus_assert (!find_subtree (tree, path1, NULL));
1779 _dbus_assert (!find_subtree (tree, path2, NULL));
1780 _dbus_assert (!find_subtree (tree, path3, NULL));
1781 _dbus_assert (!find_subtree (tree, path4, NULL));
1782 _dbus_assert (!find_subtree (tree, path5, NULL));
1783 _dbus_assert (find_subtree (tree, path6, NULL));
1784 _dbus_assert (find_subtree (tree, path7, NULL));
1785 _dbus_assert (find_subtree (tree, path8, NULL));
1787 _dbus_object_tree_unregister_and_unlock (tree, path6);
1789 _dbus_assert (!find_subtree (tree, path0, NULL));
1790 _dbus_assert (!find_subtree (tree, path1, NULL));
1791 _dbus_assert (!find_subtree (tree, path2, NULL));
1792 _dbus_assert (!find_subtree (tree, path3, NULL));
1793 _dbus_assert (!find_subtree (tree, path4, NULL));
1794 _dbus_assert (!find_subtree (tree, path5, NULL));
1795 _dbus_assert (!find_subtree (tree, path6, NULL));
1796 _dbus_assert (find_subtree (tree, path7, NULL));
1797 _dbus_assert (find_subtree (tree, path8, NULL));
1799 _dbus_object_tree_unregister_and_unlock (tree, path7);
1801 _dbus_assert (!find_subtree (tree, path0, NULL));
1802 _dbus_assert (!find_subtree (tree, path1, NULL));
1803 _dbus_assert (!find_subtree (tree, path2, NULL));
1804 _dbus_assert (!find_subtree (tree, path3, NULL));
1805 _dbus_assert (!find_subtree (tree, path4, NULL));
1806 _dbus_assert (!find_subtree (tree, path5, NULL));
1807 _dbus_assert (!find_subtree (tree, path6, NULL));
1808 _dbus_assert (!find_subtree (tree, path7, NULL));
1809 _dbus_assert (find_subtree (tree, path8, NULL));
1811 _dbus_object_tree_unregister_and_unlock (tree, path8);
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));
1824 while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1826 _dbus_assert (tree_test_data[i].handler_unregistered);
1827 _dbus_assert (!tree_test_data[i].message_handled);
1831 /* Register it all again, and test dispatch */
1833 if (!do_register (tree, path0, TRUE, 0, tree_test_data))
1835 if (!do_register (tree, path1, FALSE, 1, tree_test_data))
1837 if (!do_register (tree, path2, TRUE, 2, tree_test_data))
1839 if (!do_register (tree, path3, TRUE, 3, tree_test_data))
1841 if (!do_register (tree, path4, TRUE, 4, tree_test_data))
1843 if (!do_register (tree, path5, TRUE, 5, tree_test_data))
1845 if (!do_register (tree, path6, FALSE, 6, tree_test_data))
1847 if (!do_register (tree, path7, TRUE, 7, tree_test_data))
1849 if (!do_register (tree, path8, TRUE, 8, tree_test_data))
1856 if (!do_test_dispatch (tree, path0, 0, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1858 if (!do_test_dispatch (tree, path1, 1, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1860 if (!do_test_dispatch (tree, path2, 2, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1862 if (!do_test_dispatch (tree, path3, 3, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1864 if (!do_test_dispatch (tree, path4, 4, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1866 if (!do_test_dispatch (tree, path5, 5, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1868 if (!do_test_dispatch (tree, path6, 6, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1870 if (!do_test_dispatch (tree, path7, 7, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1872 if (!do_test_dispatch (tree, path8, 8, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1879 _dbus_object_tree_ref (tree);
1880 _dbus_object_tree_unref (tree);
1881 _dbus_object_tree_unref (tree);
1888 * @ingroup DBusObjectTree
1889 * Unit test for DBusObjectTree
1890 * @returns #TRUE on success.
1893 _dbus_object_tree_test (void)
1895 _dbus_test_oom_handling ("object tree",
1896 object_tree_test_iteration,
1902 #endif /* DBUS_BUILD_TESTS */