1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-object-tree.c DBusObjectTree (internals of DBusConnection)
4 * Copyright (C) 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 2.0
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 return_deepest_match,
194 dbus_bool_t create_if_not_found,
195 int *index_in_parent)
199 _dbus_assert (!(return_deepest_match && create_if_not_found));
204 _dbus_verbose (" path exhausted, returning %s\n",
211 _dbus_verbose (" searching children of %s for %s\n",
212 subtree->name, path[0]);
215 ensure_sorted (subtree);
217 /* FIXME we should do a binary search here instead
222 while (i < subtree->n_subtrees)
226 v = strcmp (path[0], subtree->subtrees[i]->name);
229 _dbus_verbose (" %s cmp %s = %d\n",
230 path[0], subtree->subtrees[i]->name,
239 _dbus_verbose (" storing parent index %d\n", i);
241 *index_in_parent = i;
244 if (return_deepest_match)
246 DBusObjectSubtree *next;
248 next = find_subtree_recurse (subtree->subtrees[i],
249 &path[1], return_deepest_match,
250 create_if_not_found, index_in_parent);
252 subtree->invoke_as_fallback)
255 _dbus_verbose (" no deeper match found, returning %s\n",
264 return find_subtree_recurse (subtree->subtrees[i],
265 &path[1], return_deepest_match,
266 create_if_not_found, index_in_parent);
278 _dbus_verbose (" no match found, current tree %s, create_if_not_found = %d\n",
279 subtree->name, create_if_not_found);
282 if (create_if_not_found)
284 DBusObjectSubtree* child;
285 DBusObjectSubtree **new_subtrees;
289 _dbus_verbose (" creating subtree %s\n",
293 child = _dbus_object_subtree_new (path[0],
298 /* FIXME we should do the "double alloc each time" standard thing */
299 new_n_subtrees = subtree->n_subtrees + 1;
300 new_subtrees = dbus_realloc (subtree->subtrees,
301 new_n_subtrees * sizeof (DBusObjectSubtree*));
302 if (new_subtrees == NULL)
304 child->unregister_function = NULL;
305 child->message_function = NULL;
306 _dbus_object_subtree_unref (child);
310 new_subtrees[subtree->n_subtrees] = child;
312 *index_in_parent = subtree->n_subtrees;
313 subtree->subtrees_sorted = FALSE;
314 subtree->n_subtrees = new_n_subtrees;
315 subtree->subtrees = new_subtrees;
317 child->parent = subtree;
319 return find_subtree_recurse (child,
320 &path[1], return_deepest_match,
321 create_if_not_found, index_in_parent);
324 return (return_deepest_match && subtree->invoke_as_fallback) ? subtree : NULL;
327 static DBusObjectSubtree*
328 find_subtree (DBusObjectTree *tree,
330 int *index_in_parent)
332 DBusObjectSubtree *subtree;
335 _dbus_verbose ("Looking for exact registered subtree\n");
338 subtree = find_subtree_recurse (tree->root, path, FALSE, FALSE, index_in_parent);
340 if (subtree && subtree->message_function == NULL)
346 static DBusObjectSubtree*
347 lookup_subtree (DBusObjectTree *tree,
351 _dbus_verbose ("Looking for subtree\n");
353 return find_subtree_recurse (tree->root, path, FALSE, FALSE, NULL);
356 static DBusObjectSubtree*
357 find_handler (DBusObjectTree *tree,
361 _dbus_verbose ("Looking for deepest handler\n");
363 return find_subtree_recurse (tree->root, path, TRUE, FALSE, NULL);
366 static DBusObjectSubtree*
367 ensure_subtree (DBusObjectTree *tree,
371 _dbus_verbose ("Ensuring subtree\n");
373 return find_subtree_recurse (tree->root, path, FALSE, TRUE, NULL);
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 * @returns #FALSE if not enough memory
387 _dbus_object_tree_register (DBusObjectTree *tree,
388 dbus_bool_t fallback,
390 const DBusObjectPathVTable *vtable,
393 DBusObjectSubtree *subtree;
395 _dbus_assert (tree != NULL);
396 _dbus_assert (vtable->message_function != NULL);
397 _dbus_assert (path != NULL);
399 subtree = ensure_subtree (tree, path);
403 #ifndef DBUS_DISABLE_CHECKS
404 if (subtree->message_function != NULL)
406 _dbus_warn ("A handler is already registered for the path starting with path[0] = \"%s\"\n",
407 path[0] ? path[0] : "null");
411 _dbus_assert (subtree->message_function == NULL);
414 subtree->message_function = vtable->message_function;
415 subtree->unregister_function = vtable->unregister_function;
416 subtree->user_data = user_data;
417 subtree->invoke_as_fallback = fallback != FALSE;
423 * Unregisters an object subtree that was registered with the
426 * @param tree the global object tree
427 * @param path path to the subtree (same as the one passed to _dbus_object_tree_register())
430 _dbus_object_tree_unregister_and_unlock (DBusObjectTree *tree,
434 DBusObjectSubtree *subtree;
435 DBusObjectPathUnregisterFunction unregister_function;
437 DBusConnection *connection;
439 _dbus_assert (path != NULL);
441 subtree = find_subtree (tree, path, &i);
443 #ifndef DBUS_DISABLE_CHECKS
446 _dbus_warn ("Attempted to unregister path (path[0] = %s path[1] = %s) which isn't registered\n",
447 path[0] ? path[0] : "null",
448 path[1] ? path[1] : "null");
452 _dbus_assert (subtree != NULL);
455 _dbus_assert (subtree->parent == NULL ||
456 (i >= 0 && subtree->parent->subtrees[i] == subtree));
458 subtree->message_function = NULL;
460 unregister_function = subtree->unregister_function;
461 user_data = subtree->user_data;
463 subtree->unregister_function = NULL;
464 subtree->user_data = NULL;
466 /* If we have no subtrees of our own, remove from
467 * our parent (FIXME could also be more aggressive
468 * and remove our parent if it becomes empty)
470 if (subtree->parent && subtree->n_subtrees == 0)
472 /* assumes a 0-byte memmove is OK */
473 memmove (&subtree->parent->subtrees[i],
474 &subtree->parent->subtrees[i+1],
475 (subtree->parent->n_subtrees - i - 1) *
476 sizeof (subtree->parent->subtrees[0]));
477 subtree->parent->n_subtrees -= 1;
479 subtree->parent = NULL;
481 _dbus_object_subtree_unref (subtree);
485 connection = tree->connection;
487 /* Unlock and call application code */
488 #ifdef DBUS_BUILD_TESTS
492 _dbus_connection_ref_unlocked (connection);
493 _dbus_connection_unlock (connection);
496 if (unregister_function)
497 (* unregister_function) (connection, user_data);
499 #ifdef DBUS_BUILD_TESTS
502 dbus_connection_unref (connection);
506 free_subtree_recurse (DBusConnection *connection,
507 DBusObjectSubtree *subtree)
509 /* Delete them from the end, for slightly
510 * more robustness against odd reentrancy.
512 while (subtree->n_subtrees > 0)
514 DBusObjectSubtree *child;
516 child = subtree->subtrees[subtree->n_subtrees - 1];
517 subtree->subtrees[subtree->n_subtrees - 1] = NULL;
518 subtree->n_subtrees -= 1;
519 child->parent = NULL;
521 free_subtree_recurse (connection, child);
524 /* Call application code */
525 if (subtree->unregister_function)
527 (* subtree->unregister_function) (connection,
529 subtree->message_function = NULL;
530 subtree->unregister_function = NULL;
531 subtree->user_data = NULL;
534 /* Now free ourselves */
535 _dbus_object_subtree_unref (subtree);
539 * Free all the handlers in the tree. Lock on tree's connection
542 * @param tree the object tree
545 _dbus_object_tree_free_all_unlocked (DBusObjectTree *tree)
548 free_subtree_recurse (tree->connection,
554 _dbus_object_tree_list_registered_unlocked (DBusObjectTree *tree,
555 const char **parent_path,
556 char ***child_entries)
558 DBusObjectSubtree *subtree;
561 _dbus_assert (parent_path != NULL);
562 _dbus_assert (child_entries != NULL);
564 *child_entries = NULL;
566 subtree = lookup_subtree (tree, parent_path);
569 retval = dbus_new0 (char *, 1);
574 retval = dbus_new0 (char*, subtree->n_subtrees + 1);
578 while (i < subtree->n_subtrees)
580 retval[i] = _dbus_strdup (subtree->subtrees[i]->name);
581 if (retval[i] == NULL)
583 dbus_free_string_array (retval);
593 *child_entries = retval;
594 return retval != NULL;
597 static DBusHandlerResult
598 handle_default_introspect_unlocked (DBusObjectTree *tree,
599 DBusMessage *message,
603 DBusHandlerResult result;
607 if (!dbus_message_is_method_call (message,
608 DBUS_INTERFACE_ORG_FREEDESKTOP_INTROSPECTABLE,
610 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
612 if (!_dbus_string_init (&xml))
613 return DBUS_HANDLER_RESULT_NEED_MEMORY;
615 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
618 if (!_dbus_object_tree_list_registered_unlocked (tree, path, &children))
621 if (!_dbus_string_append (&xml, "<node>\n"))
625 while (children[i] != NULL)
627 if (!_dbus_string_append_printf (&xml, " <node name=\"%s\"/>\n",
634 if (!_dbus_string_append (&xml, "</node>\n"))
637 result = DBUS_HANDLER_RESULT_HANDLED;
640 _dbus_string_free (&xml);
641 dbus_free_string_array (children);
647 * Tries to dispatch a message by directing it to handler for the
648 * object path listed in the message header, if any. Messages are
649 * dispatched first to the registered handler that matches the largest
650 * number of path elements; that is, message to /foo/bar/baz would go
651 * to the handler for /foo/bar before the one for /foo.
653 * @todo thread problems
655 * @param tree the global object tree
656 * @param message the message to dispatch
657 * @returns whether message was handled successfully
660 _dbus_object_tree_dispatch_and_unlock (DBusObjectTree *tree,
661 DBusMessage *message)
666 DBusHandlerResult result;
667 DBusObjectSubtree *subtree;
670 _dbus_verbose ("Dispatch of message by object path\n");
674 if (!dbus_message_get_path_decomposed (message, &path))
676 #ifdef DBUS_BUILD_TESTS
677 if (tree->connection)
679 _dbus_connection_unlock (tree->connection);
681 _dbus_verbose ("No memory to get decomposed path\n");
683 return DBUS_HANDLER_RESULT_NEED_MEMORY;
688 #ifdef DBUS_BUILD_TESTS
689 if (tree->connection)
691 _dbus_connection_unlock (tree->connection);
693 _dbus_verbose ("No path field in message\n");
694 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
697 /* Find the deepest path that covers the path in the message */
698 subtree = find_handler (tree, (const char**) path);
700 /* Build a list of all paths that cover the path in the message */
704 while (subtree != NULL)
706 if (subtree->message_function != NULL)
708 _dbus_object_subtree_ref (subtree);
710 /* run deepest paths first */
711 if (!_dbus_list_append (&list, subtree))
713 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
714 _dbus_object_subtree_unref (subtree);
715 goto free_and_return;
719 subtree = subtree->parent;
722 _dbus_verbose ("%d handlers in the path tree for this message\n",
723 _dbus_list_get_length (&list));
725 /* Invoke each handler in the list */
727 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
729 link = _dbus_list_get_first_link (&list);
732 DBusList *next = _dbus_list_get_next_link (&list, link);
733 subtree = link->data;
735 /* message_function is NULL if we're unregistered
738 if (subtree->message_function)
740 DBusObjectPathMessageFunction message_function;
743 message_function = subtree->message_function;
744 user_data = subtree->user_data;
747 _dbus_verbose (" (invoking a handler)\n");
750 #ifdef DBUS_BUILD_TESTS
751 if (tree->connection)
753 _dbus_connection_unlock (tree->connection);
755 /* FIXME you could unregister the subtree in another thread
756 * before we invoke the callback, and I can't figure out a
757 * good way to solve this.
760 result = (* message_function) (tree->connection,
764 #ifdef DBUS_BUILD_TESTS
765 if (tree->connection)
767 _dbus_connection_lock (tree->connection);
769 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
770 goto free_and_return;
778 if (result == DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
780 /* This hardcoded default handler does a minimal Introspect()
782 result = handle_default_introspect_unlocked (tree, message,
783 (const char**) path);
786 #ifdef DBUS_BUILD_TESTS
787 if (tree->connection)
789 _dbus_connection_unlock (tree->connection);
793 link = _dbus_list_get_first_link (&list);
794 _dbus_object_subtree_unref (link->data);
795 _dbus_list_remove_link (&list, link);
798 dbus_free_string_array (path);
804 * Allocates a subtree object.
806 * @param name name to duplicate.
807 * @returns newly-allocated subtree
809 static DBusObjectSubtree*
810 allocate_subtree_object (const char *name)
813 DBusObjectSubtree *subtree;
814 const size_t front_padding = _DBUS_STRUCT_OFFSET (DBusObjectSubtree, name);
816 _dbus_assert (name != NULL);
820 subtree = dbus_malloc (front_padding + (len + 1));
825 memcpy (subtree->name, name, len + 1);
830 static DBusObjectSubtree*
831 _dbus_object_subtree_new (const char *name,
832 const DBusObjectPathVTable *vtable,
835 DBusObjectSubtree *subtree;
837 subtree = allocate_subtree_object (name);
841 _dbus_assert (name != NULL);
843 subtree->parent = NULL;
847 subtree->message_function = vtable->message_function;
848 subtree->unregister_function = vtable->unregister_function;
852 subtree->message_function = NULL;
853 subtree->unregister_function = NULL;
856 subtree->user_data = user_data;
857 subtree->refcount.value = 1;
858 subtree->subtrees = NULL;
859 subtree->n_subtrees = 0;
860 subtree->subtrees_sorted = TRUE;
873 static DBusObjectSubtree *
874 _dbus_object_subtree_ref (DBusObjectSubtree *subtree)
876 _dbus_assert (subtree->refcount.value > 0);
877 _dbus_atomic_inc (&subtree->refcount);
883 _dbus_object_subtree_unref (DBusObjectSubtree *subtree)
885 _dbus_assert (subtree->refcount.value > 0);
887 if (_dbus_atomic_dec (&subtree->refcount) == 1)
889 _dbus_assert (subtree->unregister_function == NULL);
890 _dbus_assert (subtree->message_function == NULL);
892 dbus_free (subtree->subtrees);
898 * Lists the registered fallback handlers and object path handlers at
899 * the given parent_path. The returned array should be freed with
900 * dbus_free_string_array().
902 * @param connection the connection
903 * @param parent_path the path to list the child handlers of
904 * @param child_entries returns #NULL-terminated array of children
905 * @returns #FALSE if no memory to allocate the child entries
908 _dbus_object_tree_list_registered_and_unlock (DBusObjectTree *tree,
909 const char **parent_path,
910 char ***child_entries)
914 result = _dbus_object_tree_list_registered_unlocked (tree,
918 #ifdef DBUS_BUILD_TESTS
919 if (tree->connection)
921 _dbus_connection_unlock (tree->connection);
928 #ifdef DBUS_BUILD_TESTS
929 #include "dbus-test.h"
933 flatten_path (const char **path)
939 if (!_dbus_string_init (&str))
945 if (!_dbus_string_append_byte (&str, '/'))
948 if (!_dbus_string_append (&str, path[i]))
954 if (!_dbus_string_steal_data (&str, &s))
957 _dbus_string_free (&str);
962 _dbus_string_free (&str);
966 /* Returns TRUE if container is a parent of child
969 path_contains (const char **container,
975 while (child[i] != NULL)
979 if (container[i] == NULL)
980 return TRUE; /* container ran out, child continues;
981 * thus the container is a parent of the
985 _dbus_assert (container[i] != NULL);
986 _dbus_assert (child[i] != NULL);
988 v = strcmp (container[i], child[i]);
991 return FALSE; /* they overlap until here and then are different,
998 /* Child ran out; if container also did, they are equal;
999 * otherwise, the child is a parent of the container.
1001 if (container[i] == NULL)
1002 return TRUE; /* equal is counted as containing */
1008 spew_subtree_recurse (DBusObjectSubtree *subtree,
1016 _dbus_verbose (" ");
1020 _dbus_verbose ("%s (%d children)\n",
1021 subtree->name, subtree->n_subtrees);
1024 while (i < subtree->n_subtrees)
1026 spew_subtree_recurse (subtree->subtrees[i], indent + 2);
1033 spew_tree (DBusObjectTree *tree)
1035 spew_subtree_recurse (tree->root, 0);
1039 * Callback data used in tests
1043 const char **path; /**< Path */
1044 dbus_bool_t message_handled; /**< Gets set to true if message handler called */
1045 dbus_bool_t handler_unregistered; /**< gets set to true if handler is unregistered */
1051 test_unregister_function (DBusConnection *connection,
1054 TreeTestData *ttd = user_data;
1056 ttd->handler_unregistered = TRUE;
1059 static DBusHandlerResult
1060 test_message_function (DBusConnection *connection,
1061 DBusMessage *message,
1064 TreeTestData *ttd = user_data;
1066 ttd->message_handled = TRUE;
1068 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1072 do_register (DBusObjectTree *tree,
1075 TreeTestData *tree_test_data)
1077 DBusObjectPathVTable vtable = { test_unregister_function,
1078 test_message_function, NULL };
1080 tree_test_data[i].message_handled = FALSE;
1081 tree_test_data[i].handler_unregistered = FALSE;
1082 tree_test_data[i].path = path;
1084 if (!_dbus_object_tree_register (tree, TRUE, path,
1086 &tree_test_data[i]))
1093 do_test_dispatch (DBusObjectTree *tree,
1096 TreeTestData *tree_test_data,
1099 DBusMessage *message;
1101 DBusHandlerResult result;
1106 flat = flatten_path (path);
1110 message = dbus_message_new_method_call (NULL,
1112 "org.freedesktop.TestInterface",
1115 if (message == NULL)
1119 while (j < n_test_data)
1121 tree_test_data[j].message_handled = FALSE;
1125 result = _dbus_object_tree_dispatch_and_unlock (tree, message);
1126 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
1129 _dbus_assert (tree_test_data[i].message_handled);
1132 while (j < n_test_data)
1134 if (tree_test_data[j].message_handled)
1135 _dbus_assert (path_contains (tree_test_data[j].path,
1138 _dbus_assert (!path_contains (tree_test_data[j].path,
1144 dbus_message_unref (message);
1150 dbus_message_unref (message);
1155 string_array_length (char **array)
1158 for (i = 0; array[i]; i++) ;
1164 object_tree_test_iteration (void *data)
1166 const char *path1[] = { "foo", NULL };
1167 const char *path2[] = { "foo", "bar", NULL };
1168 const char *path3[] = { "foo", "bar", "baz", NULL };
1169 const char *path4[] = { "foo", "bar", "boo", NULL };
1170 const char *path5[] = { "blah", NULL };
1171 const char *path6[] = { "blah", "boof", NULL };
1172 const char *path7[] = { "blah", "boof", "this", "is", "really", "long", NULL };
1173 const char *path8[] = { "childless", NULL };
1174 DBusObjectTree *tree;
1175 TreeTestData tree_test_data[8];
1180 tree = _dbus_object_tree_new (NULL);
1184 if (!do_register (tree, path1, 0, tree_test_data))
1187 _dbus_assert (find_subtree (tree, path1, NULL));
1188 _dbus_assert (!find_subtree (tree, path2, NULL));
1189 _dbus_assert (!find_subtree (tree, path3, NULL));
1190 _dbus_assert (!find_subtree (tree, path4, NULL));
1191 _dbus_assert (!find_subtree (tree, path5, NULL));
1192 _dbus_assert (!find_subtree (tree, path6, NULL));
1193 _dbus_assert (!find_subtree (tree, path7, NULL));
1194 _dbus_assert (!find_subtree (tree, path8, NULL));
1196 _dbus_assert (find_handler (tree, path1));
1197 _dbus_assert (find_handler (tree, path2));
1198 _dbus_assert (find_handler (tree, path3));
1199 _dbus_assert (find_handler (tree, path4));
1200 _dbus_assert (find_handler (tree, path5) == tree->root);
1201 _dbus_assert (find_handler (tree, path6) == tree->root);
1202 _dbus_assert (find_handler (tree, path7) == tree->root);
1203 _dbus_assert (find_handler (tree, path8) == tree->root);
1205 if (!do_register (tree, path2, 1, tree_test_data))
1208 _dbus_assert (find_subtree (tree, path1, NULL));
1209 _dbus_assert (find_subtree (tree, path2, NULL));
1210 _dbus_assert (!find_subtree (tree, path3, NULL));
1211 _dbus_assert (!find_subtree (tree, path4, NULL));
1212 _dbus_assert (!find_subtree (tree, path5, NULL));
1213 _dbus_assert (!find_subtree (tree, path6, NULL));
1214 _dbus_assert (!find_subtree (tree, path7, NULL));
1215 _dbus_assert (!find_subtree (tree, path8, NULL));
1217 if (!do_register (tree, path3, 2, tree_test_data))
1220 _dbus_assert (find_subtree (tree, path1, NULL));
1221 _dbus_assert (find_subtree (tree, path2, NULL));
1222 _dbus_assert (find_subtree (tree, path3, NULL));
1223 _dbus_assert (!find_subtree (tree, path4, NULL));
1224 _dbus_assert (!find_subtree (tree, path5, NULL));
1225 _dbus_assert (!find_subtree (tree, path6, NULL));
1226 _dbus_assert (!find_subtree (tree, path7, NULL));
1227 _dbus_assert (!find_subtree (tree, path8, NULL));
1229 if (!do_register (tree, path4, 3, tree_test_data))
1232 _dbus_assert (find_subtree (tree, path1, NULL));
1233 _dbus_assert (find_subtree (tree, path2, NULL));
1234 _dbus_assert (find_subtree (tree, path3, NULL));
1235 _dbus_assert (find_subtree (tree, path4, NULL));
1236 _dbus_assert (!find_subtree (tree, path5, NULL));
1237 _dbus_assert (!find_subtree (tree, path6, NULL));
1238 _dbus_assert (!find_subtree (tree, path7, NULL));
1239 _dbus_assert (!find_subtree (tree, path8, NULL));
1241 if (!do_register (tree, path5, 4, tree_test_data))
1244 _dbus_assert (find_subtree (tree, path1, NULL));
1245 _dbus_assert (find_subtree (tree, path2, NULL));
1246 _dbus_assert (find_subtree (tree, path3, NULL));
1247 _dbus_assert (find_subtree (tree, path4, NULL));
1248 _dbus_assert (find_subtree (tree, path5, NULL));
1249 _dbus_assert (!find_subtree (tree, path6, NULL));
1250 _dbus_assert (!find_subtree (tree, path7, NULL));
1251 _dbus_assert (!find_subtree (tree, path8, NULL));
1253 _dbus_assert (find_handler (tree, path1) != tree->root);
1254 _dbus_assert (find_handler (tree, path2) != tree->root);
1255 _dbus_assert (find_handler (tree, path3) != tree->root);
1256 _dbus_assert (find_handler (tree, path4) != tree->root);
1257 _dbus_assert (find_handler (tree, path5) != tree->root);
1258 _dbus_assert (find_handler (tree, path6) != tree->root);
1259 _dbus_assert (find_handler (tree, path7) != tree->root);
1260 _dbus_assert (find_handler (tree, path8) == tree->root);
1262 if (!do_register (tree, path6, 5, tree_test_data))
1265 _dbus_assert (find_subtree (tree, path1, NULL));
1266 _dbus_assert (find_subtree (tree, path2, NULL));
1267 _dbus_assert (find_subtree (tree, path3, NULL));
1268 _dbus_assert (find_subtree (tree, path4, NULL));
1269 _dbus_assert (find_subtree (tree, path5, NULL));
1270 _dbus_assert (find_subtree (tree, path6, NULL));
1271 _dbus_assert (!find_subtree (tree, path7, NULL));
1272 _dbus_assert (!find_subtree (tree, path8, NULL));
1274 if (!do_register (tree, path7, 6, tree_test_data))
1277 _dbus_assert (find_subtree (tree, path1, NULL));
1278 _dbus_assert (find_subtree (tree, path2, NULL));
1279 _dbus_assert (find_subtree (tree, path3, NULL));
1280 _dbus_assert (find_subtree (tree, path4, NULL));
1281 _dbus_assert (find_subtree (tree, path5, NULL));
1282 _dbus_assert (find_subtree (tree, path6, NULL));
1283 _dbus_assert (find_subtree (tree, path7, NULL));
1284 _dbus_assert (!find_subtree (tree, path8, NULL));
1286 if (!do_register (tree, path8, 7, tree_test_data))
1289 _dbus_assert (find_subtree (tree, path1, NULL));
1290 _dbus_assert (find_subtree (tree, path2, NULL));
1291 _dbus_assert (find_subtree (tree, path3, NULL));
1292 _dbus_assert (find_subtree (tree, path4, NULL));
1293 _dbus_assert (find_subtree (tree, path5, NULL));
1294 _dbus_assert (find_subtree (tree, path6, NULL));
1295 _dbus_assert (find_subtree (tree, path7, NULL));
1296 _dbus_assert (find_subtree (tree, path8, NULL));
1298 _dbus_assert (find_handler (tree, path1) != tree->root);
1299 _dbus_assert (find_handler (tree, path2) != tree->root);
1300 _dbus_assert (find_handler (tree, path3) != tree->root);
1301 _dbus_assert (find_handler (tree, path4) != tree->root);
1302 _dbus_assert (find_handler (tree, path5) != tree->root);
1303 _dbus_assert (find_handler (tree, path6) != tree->root);
1304 _dbus_assert (find_handler (tree, path7) != tree->root);
1305 _dbus_assert (find_handler (tree, path8) != tree->root);
1307 /* test the list_registered function */
1310 const char *root[] = { NULL };
1311 char **child_entries;
1314 _dbus_object_tree_list_registered_unlocked (tree, path1, &child_entries);
1315 if (child_entries != NULL)
1317 nb = string_array_length (child_entries);
1318 _dbus_assert (nb == 1);
1319 dbus_free_string_array (child_entries);
1322 _dbus_object_tree_list_registered_unlocked (tree, path2, &child_entries);
1323 if (child_entries != NULL)
1325 nb = string_array_length (child_entries);
1326 _dbus_assert (nb == 2);
1327 dbus_free_string_array (child_entries);
1330 _dbus_object_tree_list_registered_unlocked (tree, path8, &child_entries);
1331 if (child_entries != NULL)
1333 nb = string_array_length (child_entries);
1334 _dbus_assert (nb == 0);
1335 dbus_free_string_array (child_entries);
1338 _dbus_object_tree_list_registered_unlocked (tree, root, &child_entries);
1339 if (child_entries != NULL)
1341 nb = string_array_length (child_entries);
1342 _dbus_assert (nb == 3);
1343 dbus_free_string_array (child_entries);
1347 /* Check that destroying tree calls unregister funcs */
1348 _dbus_object_tree_unref (tree);
1351 while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1353 _dbus_assert (tree_test_data[i].handler_unregistered);
1354 _dbus_assert (!tree_test_data[i].message_handled);
1358 /* Now start again and try the individual unregister function */
1359 tree = _dbus_object_tree_new (NULL);
1363 if (!do_register (tree, path1, 0, tree_test_data))
1365 if (!do_register (tree, path2, 1, tree_test_data))
1367 if (!do_register (tree, path3, 2, tree_test_data))
1369 if (!do_register (tree, path4, 3, tree_test_data))
1371 if (!do_register (tree, path5, 4, tree_test_data))
1373 if (!do_register (tree, path6, 5, tree_test_data))
1375 if (!do_register (tree, path7, 6, tree_test_data))
1377 if (!do_register (tree, path8, 7, tree_test_data))
1380 _dbus_object_tree_unregister_and_unlock (tree, path1);
1382 _dbus_assert (!find_subtree (tree, path1, NULL));
1383 _dbus_assert (find_subtree (tree, path2, NULL));
1384 _dbus_assert (find_subtree (tree, path3, NULL));
1385 _dbus_assert (find_subtree (tree, path4, NULL));
1386 _dbus_assert (find_subtree (tree, path5, NULL));
1387 _dbus_assert (find_subtree (tree, path6, NULL));
1388 _dbus_assert (find_subtree (tree, path7, NULL));
1389 _dbus_assert (find_subtree (tree, path8, NULL));
1391 _dbus_object_tree_unregister_and_unlock (tree, path2);
1393 _dbus_assert (!find_subtree (tree, path1, NULL));
1394 _dbus_assert (!find_subtree (tree, path2, NULL));
1395 _dbus_assert (find_subtree (tree, path3, NULL));
1396 _dbus_assert (find_subtree (tree, path4, NULL));
1397 _dbus_assert (find_subtree (tree, path5, NULL));
1398 _dbus_assert (find_subtree (tree, path6, NULL));
1399 _dbus_assert (find_subtree (tree, path7, NULL));
1400 _dbus_assert (find_subtree (tree, path8, NULL));
1402 _dbus_object_tree_unregister_and_unlock (tree, path3);
1404 _dbus_assert (!find_subtree (tree, path1, NULL));
1405 _dbus_assert (!find_subtree (tree, path2, NULL));
1406 _dbus_assert (!find_subtree (tree, path3, NULL));
1407 _dbus_assert (find_subtree (tree, path4, NULL));
1408 _dbus_assert (find_subtree (tree, path5, NULL));
1409 _dbus_assert (find_subtree (tree, path6, NULL));
1410 _dbus_assert (find_subtree (tree, path7, NULL));
1411 _dbus_assert (find_subtree (tree, path8, NULL));
1413 _dbus_object_tree_unregister_and_unlock (tree, path4);
1415 _dbus_assert (!find_subtree (tree, path1, NULL));
1416 _dbus_assert (!find_subtree (tree, path2, NULL));
1417 _dbus_assert (!find_subtree (tree, path3, NULL));
1418 _dbus_assert (!find_subtree (tree, path4, NULL));
1419 _dbus_assert (find_subtree (tree, path5, NULL));
1420 _dbus_assert (find_subtree (tree, path6, NULL));
1421 _dbus_assert (find_subtree (tree, path7, NULL));
1422 _dbus_assert (find_subtree (tree, path8, NULL));
1424 _dbus_object_tree_unregister_and_unlock (tree, path5);
1426 _dbus_assert (!find_subtree (tree, path1, NULL));
1427 _dbus_assert (!find_subtree (tree, path2, NULL));
1428 _dbus_assert (!find_subtree (tree, path3, NULL));
1429 _dbus_assert (!find_subtree (tree, path4, NULL));
1430 _dbus_assert (!find_subtree (tree, path5, NULL));
1431 _dbus_assert (find_subtree (tree, path6, NULL));
1432 _dbus_assert (find_subtree (tree, path7, NULL));
1433 _dbus_assert (find_subtree (tree, path8, NULL));
1435 _dbus_object_tree_unregister_and_unlock (tree, path6);
1437 _dbus_assert (!find_subtree (tree, path1, NULL));
1438 _dbus_assert (!find_subtree (tree, path2, NULL));
1439 _dbus_assert (!find_subtree (tree, path3, NULL));
1440 _dbus_assert (!find_subtree (tree, path4, NULL));
1441 _dbus_assert (!find_subtree (tree, path5, NULL));
1442 _dbus_assert (!find_subtree (tree, path6, NULL));
1443 _dbus_assert (find_subtree (tree, path7, NULL));
1444 _dbus_assert (find_subtree (tree, path8, NULL));
1446 _dbus_object_tree_unregister_and_unlock (tree, path7);
1448 _dbus_assert (!find_subtree (tree, path1, NULL));
1449 _dbus_assert (!find_subtree (tree, path2, NULL));
1450 _dbus_assert (!find_subtree (tree, path3, NULL));
1451 _dbus_assert (!find_subtree (tree, path4, NULL));
1452 _dbus_assert (!find_subtree (tree, path5, NULL));
1453 _dbus_assert (!find_subtree (tree, path6, NULL));
1454 _dbus_assert (!find_subtree (tree, path7, NULL));
1455 _dbus_assert (find_subtree (tree, path8, NULL));
1457 _dbus_object_tree_unregister_and_unlock (tree, path8);
1459 _dbus_assert (!find_subtree (tree, path1, NULL));
1460 _dbus_assert (!find_subtree (tree, path2, NULL));
1461 _dbus_assert (!find_subtree (tree, path3, NULL));
1462 _dbus_assert (!find_subtree (tree, path4, NULL));
1463 _dbus_assert (!find_subtree (tree, path5, NULL));
1464 _dbus_assert (!find_subtree (tree, path6, NULL));
1465 _dbus_assert (!find_subtree (tree, path7, NULL));
1466 _dbus_assert (!find_subtree (tree, path8, NULL));
1469 while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1471 _dbus_assert (tree_test_data[i].handler_unregistered);
1472 _dbus_assert (!tree_test_data[i].message_handled);
1476 /* Register it all again, and test dispatch */
1478 if (!do_register (tree, path1, 0, tree_test_data))
1480 if (!do_register (tree, path2, 1, tree_test_data))
1482 if (!do_register (tree, path3, 2, tree_test_data))
1484 if (!do_register (tree, path4, 3, tree_test_data))
1486 if (!do_register (tree, path5, 4, tree_test_data))
1488 if (!do_register (tree, path6, 5, tree_test_data))
1490 if (!do_register (tree, path7, 6, tree_test_data))
1492 if (!do_register (tree, path8, 7, tree_test_data))
1499 if (!do_test_dispatch (tree, path1, 0, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1501 if (!do_test_dispatch (tree, path2, 1, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1503 if (!do_test_dispatch (tree, path3, 2, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1505 if (!do_test_dispatch (tree, path4, 3, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1507 if (!do_test_dispatch (tree, path5, 4, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1509 if (!do_test_dispatch (tree, path6, 5, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1511 if (!do_test_dispatch (tree, path7, 6, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1513 if (!do_test_dispatch (tree, path8, 7, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1520 _dbus_object_tree_ref (tree);
1521 _dbus_object_tree_unref (tree);
1522 _dbus_object_tree_unref (tree);
1529 * @ingroup DBusObjectTree
1530 * Unit test for DBusObjectTree
1531 * @returns #TRUE on success.
1534 _dbus_object_tree_test (void)
1536 _dbus_test_oom_handling ("object tree",
1537 object_tree_test_iteration,
1543 #endif /* DBUS_BUILD_TESTS */