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., 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 int max_subtrees; /**< Number of allocated entries in subtrees */
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);
155 /** Set to 1 to get a bunch of debug spew about finding the
158 #define VERBOSE_FIND 0
160 static DBusObjectSubtree*
161 find_subtree_recurse (DBusObjectSubtree *subtree,
163 dbus_bool_t create_if_not_found,
164 int *index_in_parent,
165 dbus_bool_t *exact_match)
168 dbus_bool_t return_deepest_match;
170 return_deepest_match = exact_match != NULL;
172 _dbus_assert (!(return_deepest_match && create_if_not_found));
177 _dbus_verbose (" path exhausted, returning %s\n",
180 if (exact_match != NULL)
186 _dbus_verbose (" searching children of %s for %s\n",
187 subtree->name, path[0]);
191 j = subtree->n_subtrees;
197 v = strcmp (path[0], subtree->subtrees[k]->name);
200 _dbus_verbose (" %s cmp %s = %d\n",
201 path[0], subtree->subtrees[k]->name,
210 _dbus_verbose (" storing parent index %d\n", k);
212 *index_in_parent = k;
215 if (return_deepest_match)
217 DBusObjectSubtree *next;
219 next = find_subtree_recurse (subtree->subtrees[k],
220 &path[1], create_if_not_found,
221 index_in_parent, exact_match);
223 subtree->invoke_as_fallback)
226 _dbus_verbose (" no deeper match found, returning %s\n",
229 if (exact_match != NULL)
230 *exact_match = FALSE;
237 return find_subtree_recurse (subtree->subtrees[k],
238 &path[1], create_if_not_found,
239 index_in_parent, exact_match);
252 _dbus_verbose (" no match found, current tree %s, create_if_not_found = %d\n",
253 subtree->name, create_if_not_found);
256 if (create_if_not_found)
258 DBusObjectSubtree* child;
259 int child_pos, new_n_subtrees;
262 _dbus_verbose (" creating subtree %s\n",
266 child = _dbus_object_subtree_new (path[0],
271 new_n_subtrees = subtree->n_subtrees + 1;
272 if (new_n_subtrees > subtree->max_subtrees)
274 int new_max_subtrees;
275 DBusObjectSubtree **new_subtrees;
277 new_max_subtrees = subtree->max_subtrees == 0 ? 1 : 2 * subtree->max_subtrees;
278 new_subtrees = dbus_realloc (subtree->subtrees,
279 new_max_subtrees * sizeof (DBusObjectSubtree*));
280 if (new_subtrees == NULL)
282 _dbus_object_subtree_unref (child);
285 subtree->subtrees = new_subtrees;
286 subtree->max_subtrees = new_max_subtrees;
289 /* The binary search failed, so i == j points to the
290 place the child should be inserted. */
292 _dbus_assert (child_pos < new_n_subtrees &&
293 new_n_subtrees <= subtree->max_subtrees);
294 if (child_pos + 1 < new_n_subtrees)
296 memmove (&subtree->subtrees[child_pos+1],
297 &subtree->subtrees[child_pos],
298 (new_n_subtrees - child_pos - 1) *
299 sizeof subtree->subtrees[0]);
301 subtree->subtrees[child_pos] = child;
304 *index_in_parent = child_pos;
305 subtree->n_subtrees = new_n_subtrees;
306 child->parent = subtree;
308 return find_subtree_recurse (child,
309 &path[1], create_if_not_found,
310 index_in_parent, exact_match);
314 if (exact_match != NULL)
315 *exact_match = FALSE;
316 return (return_deepest_match && subtree->invoke_as_fallback) ? subtree : NULL;
320 static DBusObjectSubtree*
321 find_subtree (DBusObjectTree *tree,
323 int *index_in_parent)
325 DBusObjectSubtree *subtree;
328 _dbus_verbose ("Looking for exact registered subtree\n");
331 subtree = find_subtree_recurse (tree->root, path, FALSE, index_in_parent, NULL);
333 if (subtree && subtree->message_function == NULL)
339 static DBusObjectSubtree*
340 lookup_subtree (DBusObjectTree *tree,
344 _dbus_verbose ("Looking for subtree\n");
346 return find_subtree_recurse (tree->root, path, FALSE, NULL, NULL);
349 static DBusObjectSubtree*
350 find_handler (DBusObjectTree *tree,
352 dbus_bool_t *exact_match)
355 _dbus_verbose ("Looking for deepest handler\n");
357 _dbus_assert (exact_match != NULL);
359 *exact_match = FALSE; /* ensure always initialized */
361 return find_subtree_recurse (tree->root, path, FALSE, NULL, exact_match);
364 static DBusObjectSubtree*
365 ensure_subtree (DBusObjectTree *tree,
369 _dbus_verbose ("Ensuring subtree\n");
371 return find_subtree_recurse (tree->root, path, TRUE, NULL, NULL);
374 static char *flatten_path (const char **path);
377 * Registers a new subtree in the global object tree.
379 * @param tree the global object tree
380 * @param fallback #TRUE to handle messages to children of this path
381 * @param path NULL-terminated array of path elements giving path to subtree
382 * @param vtable the vtable used to traverse this subtree
383 * @param user_data user data to pass to methods in the vtable
384 * @param error address where an error can be returned
385 * @returns #FALSE if an error (#DBUS_ERROR_NO_MEMORY or
386 * #DBUS_ERROR_ADDRESS_IN_USE) is reported
389 _dbus_object_tree_register (DBusObjectTree *tree,
390 dbus_bool_t fallback,
392 const DBusObjectPathVTable *vtable,
396 DBusObjectSubtree *subtree;
398 _dbus_assert (tree != NULL);
399 _dbus_assert (vtable->message_function != NULL);
400 _dbus_assert (path != NULL);
402 subtree = ensure_subtree (tree, path);
406 _DBUS_SET_OOM (error);
411 if (subtree->message_function != NULL)
415 char *complete_path = flatten_path (path);
417 dbus_set_error (error, DBUS_ERROR_ADDRESS_IN_USE, "A handler is already registered for %s",
418 complete_path ? complete_path
419 : "(cannot represent path: out of memory!)");
421 dbus_free (complete_path);
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 unregister_function = NULL;
457 subtree = find_subtree (tree, path, &i);
459 #ifndef DBUS_DISABLE_CHECKS
462 _dbus_warn ("Attempted to unregister path (path[0] = %s path[1] = %s) which isn't registered\n",
463 path[0] ? path[0] : "null",
464 path[1] ? path[1] : "null");
468 _dbus_assert (subtree != NULL);
471 _dbus_assert (subtree->parent == NULL ||
472 (i >= 0 && subtree->parent->subtrees[i] == subtree));
474 subtree->message_function = NULL;
476 unregister_function = subtree->unregister_function;
477 user_data = subtree->user_data;
479 subtree->unregister_function = NULL;
480 subtree->user_data = NULL;
482 /* If we have no subtrees of our own, remove from
483 * our parent (FIXME could also be more aggressive
484 * and remove our parent if it becomes empty)
486 if (subtree->parent && subtree->n_subtrees == 0)
488 /* assumes a 0-byte memmove is OK */
489 memmove (&subtree->parent->subtrees[i],
490 &subtree->parent->subtrees[i+1],
491 (subtree->parent->n_subtrees - i - 1) *
492 sizeof (subtree->parent->subtrees[0]));
493 subtree->parent->n_subtrees -= 1;
495 subtree->parent = NULL;
497 _dbus_object_subtree_unref (subtree);
502 connection = tree->connection;
504 /* Unlock and call application code */
505 #ifdef DBUS_BUILD_TESTS
509 _dbus_connection_ref_unlocked (connection);
510 _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
511 _dbus_connection_unlock (connection);
514 if (unregister_function)
515 (* unregister_function) (connection, user_data);
517 #ifdef DBUS_BUILD_TESTS
520 dbus_connection_unref (connection);
524 free_subtree_recurse (DBusConnection *connection,
525 DBusObjectSubtree *subtree)
527 /* Delete them from the end, for slightly
528 * more robustness against odd reentrancy.
530 while (subtree->n_subtrees > 0)
532 DBusObjectSubtree *child;
534 child = subtree->subtrees[subtree->n_subtrees - 1];
535 subtree->subtrees[subtree->n_subtrees - 1] = NULL;
536 subtree->n_subtrees -= 1;
537 child->parent = NULL;
539 free_subtree_recurse (connection, child);
542 /* Call application code */
543 if (subtree->unregister_function)
544 (* subtree->unregister_function) (connection,
547 subtree->message_function = NULL;
548 subtree->unregister_function = NULL;
549 subtree->user_data = NULL;
551 /* Now free ourselves */
552 _dbus_object_subtree_unref (subtree);
556 * Free all the handlers in the tree. Lock on tree's connection
559 * @param tree the object tree
562 _dbus_object_tree_free_all_unlocked (DBusObjectTree *tree)
565 free_subtree_recurse (tree->connection,
571 _dbus_object_tree_list_registered_unlocked (DBusObjectTree *tree,
572 const char **parent_path,
573 char ***child_entries)
575 DBusObjectSubtree *subtree;
578 _dbus_assert (parent_path != NULL);
579 _dbus_assert (child_entries != NULL);
581 *child_entries = NULL;
583 subtree = lookup_subtree (tree, parent_path);
586 retval = dbus_new0 (char *, 1);
591 retval = dbus_new0 (char*, subtree->n_subtrees + 1);
595 while (i < subtree->n_subtrees)
597 retval[i] = _dbus_strdup (subtree->subtrees[i]->name);
598 if (retval[i] == NULL)
600 dbus_free_string_array (retval);
610 *child_entries = retval;
611 return retval != NULL;
614 static DBusHandlerResult
615 handle_default_introspect_and_unlock (DBusObjectTree *tree,
616 DBusMessage *message,
620 DBusHandlerResult result;
624 DBusMessageIter iter;
625 const char *v_STRING;
626 dbus_bool_t already_unlocked;
628 /* We have the connection lock here */
630 already_unlocked = FALSE;
632 _dbus_verbose (" considering default Introspect() handler...\n");
636 if (!dbus_message_is_method_call (message,
637 DBUS_INTERFACE_INTROSPECTABLE,
640 #ifdef DBUS_BUILD_TESTS
641 if (tree->connection)
644 _dbus_verbose ("unlock %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
645 _dbus_connection_unlock (tree->connection);
648 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
651 _dbus_verbose (" using default Introspect() handler!\n");
653 if (!_dbus_string_init (&xml))
655 #ifdef DBUS_BUILD_TESTS
656 if (tree->connection)
659 _dbus_verbose ("unlock %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
660 _dbus_connection_unlock (tree->connection);
663 return DBUS_HANDLER_RESULT_NEED_MEMORY;
666 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
669 if (!_dbus_object_tree_list_registered_unlocked (tree, path, &children))
672 if (!_dbus_string_append (&xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
675 if (!_dbus_string_append (&xml, "<node>\n"))
679 while (children[i] != NULL)
681 if (!_dbus_string_append_printf (&xml, " <node name=\"%s\"/>\n",
688 if (!_dbus_string_append (&xml, "</node>\n"))
691 reply = dbus_message_new_method_return (message);
695 dbus_message_iter_init_append (reply, &iter);
696 v_STRING = _dbus_string_get_const_data (&xml);
697 if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &v_STRING))
700 #ifdef DBUS_BUILD_TESTS
701 if (tree->connection)
704 already_unlocked = TRUE;
706 if (!_dbus_connection_send_and_unlock (tree->connection, reply, NULL))
710 result = DBUS_HANDLER_RESULT_HANDLED;
713 #ifdef DBUS_BUILD_TESTS
714 if (tree->connection)
717 if (!already_unlocked)
719 _dbus_verbose ("unlock %s %d\n", _DBUS_FUNCTION_NAME, __LINE__);
720 _dbus_connection_unlock (tree->connection);
724 _dbus_string_free (&xml);
725 dbus_free_string_array (children);
727 dbus_message_unref (reply);
733 * Tries to dispatch a message by directing it to handler for the
734 * object path listed in the message header, if any. Messages are
735 * dispatched first to the registered handler that matches the largest
736 * number of path elements; that is, message to /foo/bar/baz would go
737 * to the handler for /foo/bar before the one for /foo.
739 * @todo thread problems
741 * @param tree the global object tree
742 * @param message the message to dispatch
743 * @returns whether message was handled successfully
746 _dbus_object_tree_dispatch_and_unlock (DBusObjectTree *tree,
747 DBusMessage *message)
750 dbus_bool_t exact_match;
753 DBusHandlerResult result;
754 DBusObjectSubtree *subtree;
757 _dbus_verbose ("Dispatch of message by object path\n");
761 if (!dbus_message_get_path_decomposed (message, &path))
763 #ifdef DBUS_BUILD_TESTS
764 if (tree->connection)
767 _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
768 _dbus_connection_unlock (tree->connection);
771 _dbus_verbose ("No memory to get decomposed path\n");
773 return DBUS_HANDLER_RESULT_NEED_MEMORY;
778 #ifdef DBUS_BUILD_TESTS
779 if (tree->connection)
782 _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
783 _dbus_connection_unlock (tree->connection);
786 _dbus_verbose ("No path field in message\n");
787 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
790 /* Find the deepest path that covers the path in the message */
791 subtree = find_handler (tree, (const char**) path, &exact_match);
793 /* Build a list of all paths that cover the path in the message */
797 while (subtree != NULL)
799 if (subtree->message_function != NULL && (exact_match || subtree->invoke_as_fallback))
801 _dbus_object_subtree_ref (subtree);
803 /* run deepest paths first */
804 if (!_dbus_list_append (&list, subtree))
806 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
807 _dbus_object_subtree_unref (subtree);
808 goto free_and_return;
813 subtree = subtree->parent;
816 _dbus_verbose ("%d handlers in the path tree for this message\n",
817 _dbus_list_get_length (&list));
819 /* Invoke each handler in the list */
821 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
823 link = _dbus_list_get_first_link (&list);
826 DBusList *next = _dbus_list_get_next_link (&list, link);
827 subtree = link->data;
829 /* message_function is NULL if we're unregistered
832 if (subtree->message_function)
834 DBusObjectPathMessageFunction message_function;
837 message_function = subtree->message_function;
838 user_data = subtree->user_data;
841 _dbus_verbose (" (invoking a handler)\n");
844 #ifdef DBUS_BUILD_TESTS
845 if (tree->connection)
848 _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
849 _dbus_connection_unlock (tree->connection);
852 /* FIXME you could unregister the subtree in another thread
853 * before we invoke the callback, and I can't figure out a
854 * good way to solve this.
857 result = (* message_function) (tree->connection,
861 #ifdef DBUS_BUILD_TESTS
862 if (tree->connection)
864 _dbus_connection_lock (tree->connection);
866 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
867 goto free_and_return;
875 if (result == DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
877 /* This hardcoded default handler does a minimal Introspect()
879 result = handle_default_introspect_and_unlock (tree, message,
880 (const char**) path);
884 #ifdef DBUS_BUILD_TESTS
885 if (tree->connection)
888 _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
889 _dbus_connection_unlock (tree->connection);
895 link = _dbus_list_get_first_link (&list);
896 _dbus_object_subtree_unref (link->data);
897 _dbus_list_remove_link (&list, link);
900 dbus_free_string_array (path);
906 * Looks up the data passed to _dbus_object_tree_register() for a
907 * handler at the given path.
909 * @param tree the global object tree
910 * @param path NULL-terminated array of path elements giving path to subtree
911 * @returns the object's user_data or #NULL if none found
914 _dbus_object_tree_get_user_data_unlocked (DBusObjectTree *tree,
917 dbus_bool_t exact_match;
918 DBusObjectSubtree *subtree;
920 _dbus_assert (tree != NULL);
921 _dbus_assert (path != NULL);
923 /* Find the deepest path that covers the path in the message */
924 subtree = find_handler (tree, (const char**) path, &exact_match);
926 if ((subtree == NULL) || !exact_match)
928 _dbus_verbose ("%s: No object at specified path found\n",
929 _DBUS_FUNCTION_NAME);
933 return subtree->user_data;
937 * Allocates a subtree object.
939 * @param name name to duplicate.
940 * @returns newly-allocated subtree
942 static DBusObjectSubtree*
943 allocate_subtree_object (const char *name)
946 DBusObjectSubtree *subtree;
947 const size_t front_padding = _DBUS_STRUCT_OFFSET (DBusObjectSubtree, name);
949 _dbus_assert (name != NULL);
953 subtree = dbus_malloc (MAX (front_padding + (len + 1), sizeof (DBusObjectSubtree)));
958 memcpy (subtree->name, name, len + 1);
963 static DBusObjectSubtree*
964 _dbus_object_subtree_new (const char *name,
965 const DBusObjectPathVTable *vtable,
968 DBusObjectSubtree *subtree;
970 subtree = allocate_subtree_object (name);
974 _dbus_assert (name != NULL);
976 subtree->parent = NULL;
980 subtree->message_function = vtable->message_function;
981 subtree->unregister_function = vtable->unregister_function;
985 subtree->message_function = NULL;
986 subtree->unregister_function = NULL;
989 subtree->user_data = user_data;
990 subtree->refcount.value = 1;
991 subtree->subtrees = NULL;
992 subtree->n_subtrees = 0;
993 subtree->max_subtrees = 0;
994 subtree->invoke_as_fallback = FALSE;
1002 static DBusObjectSubtree *
1003 _dbus_object_subtree_ref (DBusObjectSubtree *subtree)
1005 _dbus_assert (subtree->refcount.value > 0);
1006 _dbus_atomic_inc (&subtree->refcount);
1012 _dbus_object_subtree_unref (DBusObjectSubtree *subtree)
1014 _dbus_assert (subtree->refcount.value > 0);
1016 if (_dbus_atomic_dec (&subtree->refcount) == 1)
1018 _dbus_assert (subtree->unregister_function == NULL);
1019 _dbus_assert (subtree->message_function == NULL);
1021 dbus_free (subtree->subtrees);
1022 dbus_free (subtree);
1027 * Lists the registered fallback handlers and object path handlers at
1028 * the given parent_path. The returned array should be freed with
1029 * dbus_free_string_array().
1031 * @param tree the object tree
1032 * @param parent_path the path to list the child handlers of
1033 * @param child_entries returns #NULL-terminated array of children
1034 * @returns #FALSE if no memory to allocate the child entries
1037 _dbus_object_tree_list_registered_and_unlock (DBusObjectTree *tree,
1038 const char **parent_path,
1039 char ***child_entries)
1043 result = _dbus_object_tree_list_registered_unlocked (tree,
1047 #ifdef DBUS_BUILD_TESTS
1048 if (tree->connection)
1051 _dbus_verbose ("unlock %s\n", _DBUS_FUNCTION_NAME);
1052 _dbus_connection_unlock (tree->connection);
1059 /** Set to 1 to get a bunch of spew about disassembling the path string */
1060 #define VERBOSE_DECOMPOSE 0
1063 * Decompose an object path. A path of just "/" is
1064 * represented as an empty vector of strings.
1065 * The path need not be nul terminated.
1067 * @param data the path data
1068 * @param len the length of the path string
1069 * @param path address to store new object path
1070 * @param path_len length of stored path
1073 _dbus_decompose_path (const char* data,
1082 _dbus_assert (data != NULL);
1084 #if VERBOSE_DECOMPOSE
1085 _dbus_verbose ("Decomposing path \"%s\"\n",
1090 if (len > 1) /* if path is not just "/" */
1101 retval = dbus_new0 (char*, n_components + 1);
1107 if (n_components == 0)
1111 while (comp < n_components)
1113 _dbus_assert (i < len);
1119 while (j < len && data[j] != '/')
1122 /* Now [i, j) is the path component */
1123 _dbus_assert (i < j);
1124 _dbus_assert (data[i] != '/');
1125 _dbus_assert (j == len || data[j] == '/');
1127 #if VERBOSE_DECOMPOSE
1128 _dbus_verbose (" (component in [%d,%d))\n",
1132 retval[comp] = _dbus_memdup (&data[i], j - i + 1);
1133 if (retval[comp] == NULL)
1135 dbus_free_string_array (retval);
1138 retval[comp][j-i] = '\0';
1139 #if VERBOSE_DECOMPOSE
1140 _dbus_verbose (" (component %d = \"%s\")\n",
1141 comp, retval[comp]);
1147 _dbus_assert (i == len);
1151 *path_len = n_components;
1159 flatten_path (const char **path)
1164 if (!_dbus_string_init (&str))
1167 if (path[0] == NULL)
1169 if (!_dbus_string_append_byte (&str, '/'))
1179 if (!_dbus_string_append_byte (&str, '/'))
1182 if (!_dbus_string_append (&str, path[i]))
1189 if (!_dbus_string_steal_data (&str, &s))
1192 _dbus_string_free (&str);
1197 _dbus_string_free (&str);
1202 #ifdef DBUS_BUILD_TESTS
1204 #ifndef DOXYGEN_SHOULD_SKIP_THIS
1206 #include "dbus-test.h"
1216 /* Returns TRUE if container is a parent of child
1218 static StrComparison
1219 path_contains (const char **container,
1225 while (child[i] != NULL)
1229 if (container[i] == NULL)
1230 return STR_PREFIX; /* container ran out, child continues;
1231 * thus the container is a parent of the
1235 _dbus_assert (container[i] != NULL);
1236 _dbus_assert (child[i] != NULL);
1238 v = strcmp (container[i], child[i]);
1241 return STR_DIFFERENT; /* they overlap until here and then are different,
1248 /* Child ran out; if container also did, they are equal;
1249 * otherwise, the child is a parent of the container.
1251 if (container[i] == NULL)
1254 return STR_DIFFERENT;
1259 spew_subtree_recurse (DBusObjectSubtree *subtree,
1267 _dbus_verbose (" ");
1271 _dbus_verbose ("%s (%d children)\n",
1272 subtree->name, subtree->n_subtrees);
1275 while (i < subtree->n_subtrees)
1277 spew_subtree_recurse (subtree->subtrees[i], indent + 2);
1284 spew_tree (DBusObjectTree *tree)
1286 spew_subtree_recurse (tree->root, 0);
1291 * Callback data used in tests
1295 const char **path; /**< Path */
1296 dbus_bool_t handler_fallback; /**< true if the handler may be called as fallback */
1297 dbus_bool_t message_handled; /**< Gets set to true if message handler called */
1298 dbus_bool_t handler_unregistered; /**< gets set to true if handler is unregistered */
1303 test_unregister_function (DBusConnection *connection,
1306 TreeTestData *ttd = user_data;
1308 ttd->handler_unregistered = TRUE;
1311 static DBusHandlerResult
1312 test_message_function (DBusConnection *connection,
1313 DBusMessage *message,
1316 TreeTestData *ttd = user_data;
1318 ttd->message_handled = TRUE;
1320 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1324 do_register (DBusObjectTree *tree,
1326 dbus_bool_t fallback,
1328 TreeTestData *tree_test_data)
1330 DBusObjectPathVTable vtable = { test_unregister_function,
1331 test_message_function, NULL };
1333 tree_test_data[i].message_handled = FALSE;
1334 tree_test_data[i].handler_unregistered = FALSE;
1335 tree_test_data[i].handler_fallback = fallback;
1336 tree_test_data[i].path = path;
1338 if (!_dbus_object_tree_register (tree, fallback, path,
1344 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path) ==
1345 &tree_test_data[i]);
1351 do_test_dispatch (DBusObjectTree *tree,
1354 TreeTestData *tree_test_data,
1357 DBusMessage *message;
1359 DBusHandlerResult result;
1364 flat = flatten_path (path);
1368 message = dbus_message_new_method_call (NULL,
1370 "org.freedesktop.TestInterface",
1373 if (message == NULL)
1377 while (j < n_test_data)
1379 tree_test_data[j].message_handled = FALSE;
1383 result = _dbus_object_tree_dispatch_and_unlock (tree, message);
1384 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
1387 _dbus_assert (tree_test_data[i].message_handled);
1390 while (j < n_test_data)
1392 if (tree_test_data[j].message_handled)
1394 if (tree_test_data[j].handler_fallback)
1395 _dbus_assert (path_contains (tree_test_data[j].path,
1396 path) != STR_DIFFERENT);
1398 _dbus_assert (path_contains (tree_test_data[j].path, path) == STR_EQUAL);
1402 if (tree_test_data[j].handler_fallback)
1403 _dbus_assert (path_contains (tree_test_data[j].path,
1404 path) == STR_DIFFERENT);
1406 _dbus_assert (path_contains (tree_test_data[j].path, path) != STR_EQUAL);
1412 dbus_message_unref (message);
1418 dbus_message_unref (message);
1423 string_array_length (const char **array)
1426 for (i = 0; array[i]; i++) ;
1433 const char *result[20];
1434 } DecomposePathTest;
1436 static DecomposePathTest decompose_tests[] = {
1437 { "/foo", { "foo", NULL } },
1438 { "/foo/bar", { "foo", "bar", NULL } },
1440 { "/a/b", { "a", "b", NULL } },
1441 { "/a/b/c", { "a", "b", "c", NULL } },
1442 { "/a/b/c/d", { "a", "b", "c", "d", NULL } },
1443 { "/foo/bar/q", { "foo", "bar", "q", NULL } },
1444 { "/foo/bar/this/is/longer", { "foo", "bar", "this", "is", "longer", NULL } }
1448 run_decompose_tests (void)
1453 while (i < _DBUS_N_ELEMENTS (decompose_tests))
1459 if (!_dbus_decompose_path (decompose_tests[i].path,
1460 strlen (decompose_tests[i].path),
1461 &result, &result_len))
1464 expected_len = string_array_length (decompose_tests[i].result);
1466 if (result_len != (int) string_array_length ((const char**)result) ||
1467 expected_len != result_len ||
1468 path_contains (decompose_tests[i].result,
1469 (const char**) result) != STR_EQUAL)
1471 int real_len = string_array_length ((const char**)result);
1472 _dbus_warn ("Expected decompose of %s to have len %d, returned %d, appears to have %d\n",
1473 decompose_tests[i].path, expected_len, result_len,
1475 _dbus_warn ("Decompose resulted in elements: { ");
1477 while (i < real_len)
1479 _dbus_warn ("\"%s\"%s", result[i],
1480 (i + 1) == real_len ? "" : ", ");
1484 _dbus_assert_not_reached ("path decompose failed\n");
1487 dbus_free_string_array (result);
1496 object_tree_test_iteration (void *data)
1498 const char *path0[] = { NULL };
1499 const char *path1[] = { "foo", NULL };
1500 const char *path2[] = { "foo", "bar", NULL };
1501 const char *path3[] = { "foo", "bar", "baz", NULL };
1502 const char *path4[] = { "foo", "bar", "boo", NULL };
1503 const char *path5[] = { "blah", NULL };
1504 const char *path6[] = { "blah", "boof", NULL };
1505 const char *path7[] = { "blah", "boof", "this", "is", "really", "long", NULL };
1506 const char *path8[] = { "childless", NULL };
1507 DBusObjectTree *tree;
1508 TreeTestData tree_test_data[9];
1510 dbus_bool_t exact_match;
1512 if (!run_decompose_tests ())
1517 tree = _dbus_object_tree_new (NULL);
1521 if (!do_register (tree, path0, TRUE, 0, tree_test_data))
1524 _dbus_assert (find_subtree (tree, path0, NULL));
1525 _dbus_assert (!find_subtree (tree, path1, NULL));
1526 _dbus_assert (!find_subtree (tree, path2, NULL));
1527 _dbus_assert (!find_subtree (tree, path3, NULL));
1528 _dbus_assert (!find_subtree (tree, path4, NULL));
1529 _dbus_assert (!find_subtree (tree, path5, NULL));
1530 _dbus_assert (!find_subtree (tree, path6, NULL));
1531 _dbus_assert (!find_subtree (tree, path7, NULL));
1532 _dbus_assert (!find_subtree (tree, path8, NULL));
1534 _dbus_assert (find_handler (tree, path0, &exact_match) && exact_match);
1535 _dbus_assert (find_handler (tree, path1, &exact_match) == tree->root && !exact_match);
1536 _dbus_assert (find_handler (tree, path2, &exact_match) == tree->root && !exact_match);
1537 _dbus_assert (find_handler (tree, path3, &exact_match) == tree->root && !exact_match);
1538 _dbus_assert (find_handler (tree, path4, &exact_match) == tree->root && !exact_match);
1539 _dbus_assert (find_handler (tree, path5, &exact_match) == tree->root && !exact_match);
1540 _dbus_assert (find_handler (tree, path6, &exact_match) == tree->root && !exact_match);
1541 _dbus_assert (find_handler (tree, path7, &exact_match) == tree->root && !exact_match);
1542 _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
1544 if (!do_register (tree, path1, TRUE, 1, tree_test_data))
1547 _dbus_assert (find_subtree (tree, path0, NULL));
1548 _dbus_assert (find_subtree (tree, path1, NULL));
1549 _dbus_assert (!find_subtree (tree, path2, NULL));
1550 _dbus_assert (!find_subtree (tree, path3, NULL));
1551 _dbus_assert (!find_subtree (tree, path4, NULL));
1552 _dbus_assert (!find_subtree (tree, path5, NULL));
1553 _dbus_assert (!find_subtree (tree, path6, NULL));
1554 _dbus_assert (!find_subtree (tree, path7, NULL));
1555 _dbus_assert (!find_subtree (tree, path8, NULL));
1557 _dbus_assert (find_handler (tree, path0, &exact_match) && exact_match);
1558 _dbus_assert (find_handler (tree, path1, &exact_match) && exact_match);
1559 _dbus_assert (find_handler (tree, path2, &exact_match) && !exact_match);
1560 _dbus_assert (find_handler (tree, path3, &exact_match) && !exact_match);
1561 _dbus_assert (find_handler (tree, path4, &exact_match) && !exact_match);
1562 _dbus_assert (find_handler (tree, path5, &exact_match) == tree->root && !exact_match);
1563 _dbus_assert (find_handler (tree, path6, &exact_match) == tree->root && !exact_match);
1564 _dbus_assert (find_handler (tree, path7, &exact_match) == tree->root && !exact_match);
1565 _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
1567 if (!do_register (tree, path2, TRUE, 2, tree_test_data))
1570 _dbus_assert (find_subtree (tree, path1, NULL));
1571 _dbus_assert (find_subtree (tree, path2, NULL));
1572 _dbus_assert (!find_subtree (tree, path3, NULL));
1573 _dbus_assert (!find_subtree (tree, path4, NULL));
1574 _dbus_assert (!find_subtree (tree, path5, NULL));
1575 _dbus_assert (!find_subtree (tree, path6, NULL));
1576 _dbus_assert (!find_subtree (tree, path7, NULL));
1577 _dbus_assert (!find_subtree (tree, path8, NULL));
1579 if (!do_register (tree, path3, TRUE, 3, tree_test_data))
1582 _dbus_assert (find_subtree (tree, path0, NULL));
1583 _dbus_assert (find_subtree (tree, path1, NULL));
1584 _dbus_assert (find_subtree (tree, path2, NULL));
1585 _dbus_assert (find_subtree (tree, path3, NULL));
1586 _dbus_assert (!find_subtree (tree, path4, NULL));
1587 _dbus_assert (!find_subtree (tree, path5, NULL));
1588 _dbus_assert (!find_subtree (tree, path6, NULL));
1589 _dbus_assert (!find_subtree (tree, path7, NULL));
1590 _dbus_assert (!find_subtree (tree, path8, NULL));
1592 if (!do_register (tree, path4, TRUE, 4, tree_test_data))
1595 _dbus_assert (find_subtree (tree, path0, NULL));
1596 _dbus_assert (find_subtree (tree, path1, NULL));
1597 _dbus_assert (find_subtree (tree, path2, NULL));
1598 _dbus_assert (find_subtree (tree, path3, NULL));
1599 _dbus_assert (find_subtree (tree, path4, NULL));
1600 _dbus_assert (!find_subtree (tree, path5, NULL));
1601 _dbus_assert (!find_subtree (tree, path6, NULL));
1602 _dbus_assert (!find_subtree (tree, path7, NULL));
1603 _dbus_assert (!find_subtree (tree, path8, NULL));
1605 if (!do_register (tree, path5, TRUE, 5, tree_test_data))
1608 _dbus_assert (find_subtree (tree, path0, NULL));
1609 _dbus_assert (find_subtree (tree, path1, NULL));
1610 _dbus_assert (find_subtree (tree, path2, NULL));
1611 _dbus_assert (find_subtree (tree, path3, NULL));
1612 _dbus_assert (find_subtree (tree, path4, NULL));
1613 _dbus_assert (find_subtree (tree, path5, NULL));
1614 _dbus_assert (!find_subtree (tree, path6, NULL));
1615 _dbus_assert (!find_subtree (tree, path7, NULL));
1616 _dbus_assert (!find_subtree (tree, path8, NULL));
1618 _dbus_assert (find_handler (tree, path0, &exact_match) == tree->root && exact_match);
1619 _dbus_assert (find_handler (tree, path1, &exact_match) != tree->root && exact_match);
1620 _dbus_assert (find_handler (tree, path2, &exact_match) != tree->root && exact_match);
1621 _dbus_assert (find_handler (tree, path3, &exact_match) != tree->root && exact_match);
1622 _dbus_assert (find_handler (tree, path4, &exact_match) != tree->root && exact_match);
1623 _dbus_assert (find_handler (tree, path5, &exact_match) != tree->root && exact_match);
1624 _dbus_assert (find_handler (tree, path6, &exact_match) != tree->root && !exact_match);
1625 _dbus_assert (find_handler (tree, path7, &exact_match) != tree->root && !exact_match);
1626 _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
1628 if (!do_register (tree, path6, TRUE, 6, tree_test_data))
1631 _dbus_assert (find_subtree (tree, path0, NULL));
1632 _dbus_assert (find_subtree (tree, path1, NULL));
1633 _dbus_assert (find_subtree (tree, path2, NULL));
1634 _dbus_assert (find_subtree (tree, path3, NULL));
1635 _dbus_assert (find_subtree (tree, path4, NULL));
1636 _dbus_assert (find_subtree (tree, path5, NULL));
1637 _dbus_assert (find_subtree (tree, path6, NULL));
1638 _dbus_assert (!find_subtree (tree, path7, NULL));
1639 _dbus_assert (!find_subtree (tree, path8, NULL));
1641 if (!do_register (tree, path7, TRUE, 7, tree_test_data))
1644 _dbus_assert (find_subtree (tree, path0, NULL));
1645 _dbus_assert (find_subtree (tree, path1, NULL));
1646 _dbus_assert (find_subtree (tree, path2, NULL));
1647 _dbus_assert (find_subtree (tree, path3, NULL));
1648 _dbus_assert (find_subtree (tree, path4, NULL));
1649 _dbus_assert (find_subtree (tree, path5, NULL));
1650 _dbus_assert (find_subtree (tree, path6, NULL));
1651 _dbus_assert (find_subtree (tree, path7, NULL));
1652 _dbus_assert (!find_subtree (tree, path8, NULL));
1654 if (!do_register (tree, path8, TRUE, 8, tree_test_data))
1657 _dbus_assert (find_subtree (tree, path0, NULL));
1658 _dbus_assert (find_subtree (tree, path1, NULL));
1659 _dbus_assert (find_subtree (tree, path2, NULL));
1660 _dbus_assert (find_subtree (tree, path3, NULL));
1661 _dbus_assert (find_subtree (tree, path4, NULL));
1662 _dbus_assert (find_subtree (tree, path5, NULL));
1663 _dbus_assert (find_subtree (tree, path6, NULL));
1664 _dbus_assert (find_subtree (tree, path7, NULL));
1665 _dbus_assert (find_subtree (tree, path8, NULL));
1667 _dbus_assert (find_handler (tree, path0, &exact_match) == tree->root && exact_match);
1668 _dbus_assert (find_handler (tree, path1, &exact_match) != tree->root && exact_match);
1669 _dbus_assert (find_handler (tree, path2, &exact_match) != tree->root && exact_match);
1670 _dbus_assert (find_handler (tree, path3, &exact_match) != tree->root && exact_match);
1671 _dbus_assert (find_handler (tree, path4, &exact_match) != tree->root && exact_match);
1672 _dbus_assert (find_handler (tree, path5, &exact_match) != tree->root && exact_match);
1673 _dbus_assert (find_handler (tree, path6, &exact_match) != tree->root && exact_match);
1674 _dbus_assert (find_handler (tree, path7, &exact_match) != tree->root && exact_match);
1675 _dbus_assert (find_handler (tree, path8, &exact_match) != tree->root && exact_match);
1677 /* test the list_registered function */
1680 const char *root[] = { NULL };
1681 char **child_entries;
1684 _dbus_object_tree_list_registered_unlocked (tree, path1, &child_entries);
1685 if (child_entries != NULL)
1687 nb = string_array_length ((const char**)child_entries);
1688 _dbus_assert (nb == 1);
1689 dbus_free_string_array (child_entries);
1692 _dbus_object_tree_list_registered_unlocked (tree, path2, &child_entries);
1693 if (child_entries != NULL)
1695 nb = string_array_length ((const char**)child_entries);
1696 _dbus_assert (nb == 2);
1697 dbus_free_string_array (child_entries);
1700 _dbus_object_tree_list_registered_unlocked (tree, path8, &child_entries);
1701 if (child_entries != NULL)
1703 nb = string_array_length ((const char**)child_entries);
1704 _dbus_assert (nb == 0);
1705 dbus_free_string_array (child_entries);
1708 _dbus_object_tree_list_registered_unlocked (tree, root, &child_entries);
1709 if (child_entries != NULL)
1711 nb = string_array_length ((const char**)child_entries);
1712 _dbus_assert (nb == 3);
1713 dbus_free_string_array (child_entries);
1717 /* Check that destroying tree calls unregister funcs */
1718 _dbus_object_tree_unref (tree);
1721 while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1723 _dbus_assert (tree_test_data[i].handler_unregistered);
1724 _dbus_assert (!tree_test_data[i].message_handled);
1728 /* Now start again and try the individual unregister function */
1729 tree = _dbus_object_tree_new (NULL);
1733 if (!do_register (tree, path0, TRUE, 0, tree_test_data))
1735 if (!do_register (tree, path1, TRUE, 1, tree_test_data))
1737 if (!do_register (tree, path2, TRUE, 2, tree_test_data))
1739 if (!do_register (tree, path3, TRUE, 3, tree_test_data))
1741 if (!do_register (tree, path4, TRUE, 4, tree_test_data))
1743 if (!do_register (tree, path5, TRUE, 5, tree_test_data))
1745 if (!do_register (tree, path6, TRUE, 6, tree_test_data))
1747 if (!do_register (tree, path7, TRUE, 7, tree_test_data))
1749 if (!do_register (tree, path8, TRUE, 8, tree_test_data))
1752 _dbus_object_tree_unregister_and_unlock (tree, path0);
1753 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path0) == NULL);
1755 _dbus_assert (!find_subtree (tree, path0, NULL));
1756 _dbus_assert (find_subtree (tree, path1, NULL));
1757 _dbus_assert (find_subtree (tree, path2, NULL));
1758 _dbus_assert (find_subtree (tree, path3, NULL));
1759 _dbus_assert (find_subtree (tree, path4, NULL));
1760 _dbus_assert (find_subtree (tree, path5, NULL));
1761 _dbus_assert (find_subtree (tree, path6, NULL));
1762 _dbus_assert (find_subtree (tree, path7, NULL));
1763 _dbus_assert (find_subtree (tree, path8, NULL));
1765 _dbus_object_tree_unregister_and_unlock (tree, path1);
1766 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path1) == NULL);
1768 _dbus_assert (!find_subtree (tree, path0, NULL));
1769 _dbus_assert (!find_subtree (tree, path1, NULL));
1770 _dbus_assert (find_subtree (tree, path2, NULL));
1771 _dbus_assert (find_subtree (tree, path3, NULL));
1772 _dbus_assert (find_subtree (tree, path4, NULL));
1773 _dbus_assert (find_subtree (tree, path5, NULL));
1774 _dbus_assert (find_subtree (tree, path6, NULL));
1775 _dbus_assert (find_subtree (tree, path7, NULL));
1776 _dbus_assert (find_subtree (tree, path8, NULL));
1778 _dbus_object_tree_unregister_and_unlock (tree, path2);
1779 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path2) == NULL);
1781 _dbus_assert (!find_subtree (tree, path0, NULL));
1782 _dbus_assert (!find_subtree (tree, path1, NULL));
1783 _dbus_assert (!find_subtree (tree, path2, NULL));
1784 _dbus_assert (find_subtree (tree, path3, NULL));
1785 _dbus_assert (find_subtree (tree, path4, NULL));
1786 _dbus_assert (find_subtree (tree, path5, NULL));
1787 _dbus_assert (find_subtree (tree, path6, NULL));
1788 _dbus_assert (find_subtree (tree, path7, NULL));
1789 _dbus_assert (find_subtree (tree, path8, NULL));
1791 _dbus_object_tree_unregister_and_unlock (tree, path3);
1792 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path3) == NULL);
1794 _dbus_assert (!find_subtree (tree, path0, NULL));
1795 _dbus_assert (!find_subtree (tree, path1, NULL));
1796 _dbus_assert (!find_subtree (tree, path2, NULL));
1797 _dbus_assert (!find_subtree (tree, path3, NULL));
1798 _dbus_assert (find_subtree (tree, path4, NULL));
1799 _dbus_assert (find_subtree (tree, path5, NULL));
1800 _dbus_assert (find_subtree (tree, path6, NULL));
1801 _dbus_assert (find_subtree (tree, path7, NULL));
1802 _dbus_assert (find_subtree (tree, path8, NULL));
1804 _dbus_object_tree_unregister_and_unlock (tree, path4);
1805 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path4) == NULL);
1807 _dbus_assert (!find_subtree (tree, path0, NULL));
1808 _dbus_assert (!find_subtree (tree, path1, NULL));
1809 _dbus_assert (!find_subtree (tree, path2, NULL));
1810 _dbus_assert (!find_subtree (tree, path3, NULL));
1811 _dbus_assert (!find_subtree (tree, path4, NULL));
1812 _dbus_assert (find_subtree (tree, path5, NULL));
1813 _dbus_assert (find_subtree (tree, path6, NULL));
1814 _dbus_assert (find_subtree (tree, path7, NULL));
1815 _dbus_assert (find_subtree (tree, path8, NULL));
1817 _dbus_object_tree_unregister_and_unlock (tree, path5);
1818 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path5) == NULL);
1820 _dbus_assert (!find_subtree (tree, path0, NULL));
1821 _dbus_assert (!find_subtree (tree, path1, NULL));
1822 _dbus_assert (!find_subtree (tree, path2, NULL));
1823 _dbus_assert (!find_subtree (tree, path3, NULL));
1824 _dbus_assert (!find_subtree (tree, path4, NULL));
1825 _dbus_assert (!find_subtree (tree, path5, NULL));
1826 _dbus_assert (find_subtree (tree, path6, NULL));
1827 _dbus_assert (find_subtree (tree, path7, NULL));
1828 _dbus_assert (find_subtree (tree, path8, NULL));
1830 _dbus_object_tree_unregister_and_unlock (tree, path6);
1831 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path6) == NULL);
1833 _dbus_assert (!find_subtree (tree, path0, NULL));
1834 _dbus_assert (!find_subtree (tree, path1, NULL));
1835 _dbus_assert (!find_subtree (tree, path2, NULL));
1836 _dbus_assert (!find_subtree (tree, path3, NULL));
1837 _dbus_assert (!find_subtree (tree, path4, NULL));
1838 _dbus_assert (!find_subtree (tree, path5, NULL));
1839 _dbus_assert (!find_subtree (tree, path6, NULL));
1840 _dbus_assert (find_subtree (tree, path7, NULL));
1841 _dbus_assert (find_subtree (tree, path8, NULL));
1843 _dbus_object_tree_unregister_and_unlock (tree, path7);
1844 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path7) == NULL);
1846 _dbus_assert (!find_subtree (tree, path0, NULL));
1847 _dbus_assert (!find_subtree (tree, path1, NULL));
1848 _dbus_assert (!find_subtree (tree, path2, NULL));
1849 _dbus_assert (!find_subtree (tree, path3, NULL));
1850 _dbus_assert (!find_subtree (tree, path4, NULL));
1851 _dbus_assert (!find_subtree (tree, path5, NULL));
1852 _dbus_assert (!find_subtree (tree, path6, NULL));
1853 _dbus_assert (!find_subtree (tree, path7, NULL));
1854 _dbus_assert (find_subtree (tree, path8, NULL));
1856 _dbus_object_tree_unregister_and_unlock (tree, path8);
1857 _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path8) == NULL);
1859 _dbus_assert (!find_subtree (tree, path0, NULL));
1860 _dbus_assert (!find_subtree (tree, path1, NULL));
1861 _dbus_assert (!find_subtree (tree, path2, NULL));
1862 _dbus_assert (!find_subtree (tree, path3, NULL));
1863 _dbus_assert (!find_subtree (tree, path4, NULL));
1864 _dbus_assert (!find_subtree (tree, path5, NULL));
1865 _dbus_assert (!find_subtree (tree, path6, NULL));
1866 _dbus_assert (!find_subtree (tree, path7, NULL));
1867 _dbus_assert (!find_subtree (tree, path8, NULL));
1870 while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1872 _dbus_assert (tree_test_data[i].handler_unregistered);
1873 _dbus_assert (!tree_test_data[i].message_handled);
1877 /* Register it all again, and test dispatch */
1879 if (!do_register (tree, path0, TRUE, 0, tree_test_data))
1881 if (!do_register (tree, path1, FALSE, 1, tree_test_data))
1883 if (!do_register (tree, path2, TRUE, 2, tree_test_data))
1885 if (!do_register (tree, path3, TRUE, 3, tree_test_data))
1887 if (!do_register (tree, path4, TRUE, 4, tree_test_data))
1889 if (!do_register (tree, path5, TRUE, 5, tree_test_data))
1891 if (!do_register (tree, path6, FALSE, 6, tree_test_data))
1893 if (!do_register (tree, path7, TRUE, 7, tree_test_data))
1895 if (!do_register (tree, path8, TRUE, 8, tree_test_data))
1902 if (!do_test_dispatch (tree, path0, 0, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1904 if (!do_test_dispatch (tree, path1, 1, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1906 if (!do_test_dispatch (tree, path2, 2, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1908 if (!do_test_dispatch (tree, path3, 3, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1910 if (!do_test_dispatch (tree, path4, 4, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1912 if (!do_test_dispatch (tree, path5, 5, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1914 if (!do_test_dispatch (tree, path6, 6, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1916 if (!do_test_dispatch (tree, path7, 7, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1918 if (!do_test_dispatch (tree, path8, 8, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1925 _dbus_object_tree_ref (tree);
1926 _dbus_object_tree_unref (tree);
1927 _dbus_object_tree_unref (tree);
1934 * @ingroup DBusObjectTree
1935 * Unit test for DBusObjectTree
1936 * @returns #TRUE on success.
1939 _dbus_object_tree_test (void)
1941 _dbus_test_oom_handling ("object tree",
1942 object_tree_test_iteration,
1948 #endif /* !DOXYGEN_SHOULD_SKIP_THIS */
1950 #endif /* DBUS_BUILD_TESTS */