1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-object-tree.c DBusObjectTree (internals of DBusConnection)
4 * Copyright (C) 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "dbus-object-tree.h"
24 #include "dbus-connection-internal.h"
25 #include "dbus-internals.h"
26 #include "dbus-hash.h"
27 #include "dbus-protocol.h"
32 * @defgroup DBusObjectTree A hierarchy of objects with container-contained relationship
33 * @ingroup DBusInternals
34 * @brief DBusObjectTree is used by DBusConnection to track the object tree
36 * Types and functions related to DBusObjectTree. These
39 * @todo this is totally broken, because of the following case:
40 * /foo, /foo/bar, /foo/baz
41 * if we then receive a message to /foo/baz we need to hand it
42 * to /foo/baz and /foo but not /foo/bar. So we should be
43 * using a real tree structure as with GConfListeners.
48 typedef struct DBusObjectSubtree DBusObjectSubtree;
50 static DBusObjectSubtree* _dbus_object_subtree_new (const char *name,
51 const DBusObjectPathVTable *vtable,
53 static void _dbus_object_subtree_ref (DBusObjectSubtree *subtree);
54 static void _dbus_object_subtree_unref (DBusObjectSubtree *subtree);
59 DBusConnection *connection;
61 DBusObjectSubtree *root;
64 struct DBusObjectSubtree
67 DBusObjectSubtree *parent;
68 DBusObjectPathUnregisterFunction unregister_function;
69 DBusObjectPathMessageFunction message_function;
71 DBusObjectSubtree **subtrees;
73 unsigned int subtrees_sorted : 1;
74 unsigned int invoke_as_fallback : 1;
75 char name[1]; /**< Allocated as large as necessary */
79 _dbus_object_tree_new (DBusConnection *connection)
83 /* the connection passed in here isn't fully constructed,
84 * so don't do anything more than store a pointer to
88 tree = dbus_new0 (DBusObjectTree, 1);
93 tree->connection = connection;
94 tree->root = _dbus_object_subtree_new ("/", NULL, NULL);
95 if (tree->root == NULL)
97 tree->root->invoke_as_fallback = TRUE;
111 _dbus_object_tree_ref (DBusObjectTree *tree)
113 _dbus_assert (tree->refcount > 0);
119 _dbus_object_tree_unref (DBusObjectTree *tree)
121 _dbus_assert (tree->refcount > 0);
125 if (tree->refcount == 0)
127 _dbus_object_tree_free_all_unlocked (tree);
134 subtree_cmp (DBusObjectSubtree *subtree_a,
135 DBusObjectSubtree *subtree_b)
137 return strcmp (subtree_a->name, subtree_b->name);
141 subtree_qsort_cmp (const void *a,
144 DBusObjectSubtree **subtree_a_p = (void*) a;
145 DBusObjectSubtree **subtree_b_p = (void*) b;
147 return subtree_cmp (*subtree_a_p, *subtree_b_p);
151 ensure_sorted (DBusObjectSubtree *subtree)
153 if (subtree->subtrees && !subtree->subtrees_sorted)
155 qsort (subtree->subtrees,
157 sizeof (DBusObjectSubtree*),
159 subtree->subtrees_sorted = TRUE;
163 #define VERBOSE_FIND 0
165 static DBusObjectSubtree*
166 find_subtree_recurse (DBusObjectSubtree *subtree,
168 dbus_bool_t return_deepest_match,
169 dbus_bool_t create_if_not_found,
170 int *index_in_parent)
174 _dbus_assert (!(return_deepest_match && create_if_not_found));
179 _dbus_verbose (" path exhausted, returning %s\n",
186 _dbus_verbose (" searching children of %s for %s\n",
187 subtree->name, path[0]);
190 ensure_sorted (subtree);
192 /* FIXME we should do a binary search here instead
197 while (i < subtree->n_subtrees)
201 v = strcmp (path[0], subtree->subtrees[i]->name);
204 _dbus_verbose (" %s cmp %s = %d\n",
205 path[0], subtree->subtrees[i]->name,
214 _dbus_verbose (" storing parent index %d\n", i);
216 *index_in_parent = i;
219 if (return_deepest_match)
221 DBusObjectSubtree *next;
223 next = find_subtree_recurse (subtree->subtrees[i],
224 &path[1], return_deepest_match,
225 create_if_not_found, index_in_parent);
227 subtree->invoke_as_fallback)
230 _dbus_verbose (" no deeper match found, returning %s\n",
239 return find_subtree_recurse (subtree->subtrees[i],
240 &path[1], return_deepest_match,
241 create_if_not_found, index_in_parent);
253 _dbus_verbose (" no match found, current tree %s, create_if_not_found = %d\n",
254 subtree->name, create_if_not_found);
257 if (create_if_not_found)
259 DBusObjectSubtree* child;
260 DBusObjectSubtree **new_subtrees;
264 _dbus_verbose (" creating subtree %s\n",
268 child = _dbus_object_subtree_new (path[0],
273 /* FIXME we should do the "double alloc each time" standard thing */
274 new_n_subtrees = subtree->n_subtrees + 1;
275 new_subtrees = dbus_realloc (subtree->subtrees,
276 new_n_subtrees * sizeof (DBusObjectSubtree*));
277 if (new_subtrees == NULL)
279 child->unregister_function = NULL;
280 child->message_function = NULL;
281 _dbus_object_subtree_unref (child);
285 new_subtrees[subtree->n_subtrees] = child;
287 *index_in_parent = subtree->n_subtrees;
288 subtree->subtrees_sorted = FALSE;
289 subtree->n_subtrees = new_n_subtrees;
290 subtree->subtrees = new_subtrees;
292 child->parent = subtree;
294 return find_subtree_recurse (child,
295 &path[1], return_deepest_match,
296 create_if_not_found, index_in_parent);
299 return (return_deepest_match && subtree->invoke_as_fallback) ? subtree : NULL;
302 static DBusObjectSubtree*
303 find_subtree (DBusObjectTree *tree,
305 int *index_in_parent)
307 DBusObjectSubtree *subtree;
310 _dbus_verbose ("Looking for exact registered subtree\n");
313 subtree = find_subtree_recurse (tree->root, path, FALSE, FALSE, index_in_parent);
315 if (subtree && subtree->message_function == NULL)
321 static DBusObjectSubtree*
322 find_handler (DBusObjectTree *tree,
326 _dbus_verbose ("Looking for deepest handler\n");
328 return find_subtree_recurse (tree->root, path, TRUE, FALSE, NULL);
331 static DBusObjectSubtree*
332 ensure_subtree (DBusObjectTree *tree,
336 _dbus_verbose ("Ensuring subtree\n");
338 return find_subtree_recurse (tree->root, path, FALSE, TRUE, NULL);
342 * Registers a new subtree in the global object tree.
344 * @param tree the global object tree
345 * @param fallback #TRUE to handle messages to children of this path
346 * @param path NULL-terminated array of path elements giving path to subtree
347 * @param vtable the vtable used to traverse this subtree
348 * @param user_data user data to pass to methods in the vtable
349 * @returns #FALSE if not enough memory
352 _dbus_object_tree_register (DBusObjectTree *tree,
353 dbus_bool_t fallback,
355 const DBusObjectPathVTable *vtable,
358 DBusObjectSubtree *subtree;
360 _dbus_assert (tree != NULL);
361 _dbus_assert (vtable->message_function != NULL);
362 _dbus_assert (path != NULL);
364 subtree = ensure_subtree (tree, path);
368 #ifndef DBUS_DISABLE_CHECKS
369 if (subtree->message_function != NULL)
371 _dbus_warn ("A handler is already registered for the path starting with path[0] = \"%s\"\n",
372 path[0] ? path[0] : "null");
376 _dbus_assert (subtree->message_function == NULL);
379 subtree->message_function = vtable->message_function;
380 subtree->unregister_function = vtable->unregister_function;
381 subtree->user_data = user_data;
382 subtree->invoke_as_fallback = fallback != FALSE;
388 * Unregisters an object subtree that was registered with the
391 * @param tree the global object tree
392 * @param path path to the subtree (same as the one passed to _dbus_object_tree_register())
395 _dbus_object_tree_unregister_and_unlock (DBusObjectTree *tree,
399 DBusObjectSubtree *subtree;
400 DBusObjectPathUnregisterFunction unregister_function;
402 DBusConnection *connection;
404 _dbus_assert (path != NULL);
406 subtree = find_subtree (tree, path, &i);
408 #ifndef DBUS_DISABLE_CHECKS
411 _dbus_warn ("Attempted to unregister path (path[0] = %s path[1] = %s) which isn't registered\n",
412 path[0] ? path[0] : "null",
413 path[1] ? path[1] : "null");
417 _dbus_assert (subtree != NULL);
420 _dbus_assert (subtree->parent == NULL ||
421 (i >= 0 && subtree->parent->subtrees[i] == subtree));
423 subtree->message_function = NULL;
425 unregister_function = subtree->unregister_function;
426 user_data = subtree->user_data;
428 subtree->unregister_function = NULL;
429 subtree->user_data = NULL;
431 /* If we have no subtrees of our own, remove from
432 * our parent (FIXME could also be more aggressive
433 * and remove our parent if it becomes empty)
435 if (subtree->parent && subtree->n_subtrees == 0)
437 /* assumes a 0-byte memmove is OK */
438 memmove (&subtree->parent->subtrees[i],
439 &subtree->parent->subtrees[i+1],
440 (subtree->parent->n_subtrees - i - 1) *
441 sizeof (subtree->parent->subtrees[0]));
442 subtree->parent->n_subtrees -= 1;
444 subtree->parent = NULL;
446 _dbus_object_subtree_unref (subtree);
450 connection = tree->connection;
452 /* Unlock and call application code */
453 #ifdef DBUS_BUILD_TESTS
457 _dbus_connection_ref_unlocked (connection);
458 _dbus_connection_unlock (connection);
461 if (unregister_function)
462 (* unregister_function) (connection, user_data);
464 #ifdef DBUS_BUILD_TESTS
467 dbus_connection_unref (connection);
471 free_subtree_recurse (DBusConnection *connection,
472 DBusObjectSubtree *subtree)
474 /* Delete them from the end, for slightly
475 * more robustness against odd reentrancy.
477 while (subtree->n_subtrees > 0)
479 DBusObjectSubtree *child;
481 child = subtree->subtrees[subtree->n_subtrees - 1];
482 subtree->subtrees[subtree->n_subtrees - 1] = NULL;
483 subtree->n_subtrees -= 1;
484 child->parent = NULL;
486 free_subtree_recurse (connection, child);
489 /* Call application code */
490 if (subtree->unregister_function)
492 (* subtree->unregister_function) (connection,
494 subtree->message_function = NULL;
495 subtree->unregister_function = NULL;
496 subtree->user_data = NULL;
499 /* Now free ourselves */
500 _dbus_object_subtree_unref (subtree);
504 * Free all the handlers in the tree. Lock on tree's connection
507 * @param tree the object tree
510 _dbus_object_tree_free_all_unlocked (DBusObjectTree *tree)
513 free_subtree_recurse (tree->connection,
519 * Tries to dispatch a message by directing it to handler for the
520 * object path listed in the message header, if any. Messages are
521 * dispatched first to the registered handler that matches the largest
522 * number of path elements; that is, message to /foo/bar/baz would go
523 * to the handler for /foo/bar before the one for /foo.
525 * @todo thread problems
527 * @param tree the global object tree
528 * @param message the message to dispatch
529 * @returns whether message was handled successfully
532 _dbus_object_tree_dispatch_and_unlock (DBusObjectTree *tree,
533 DBusMessage *message)
538 DBusHandlerResult result;
539 DBusObjectSubtree *subtree;
542 _dbus_verbose ("Dispatch of message by object path\n");
546 if (!dbus_message_get_path_decomposed (message, &path))
548 _dbus_verbose ("No memory to get decomposed path\n");
549 return DBUS_HANDLER_RESULT_NEED_MEMORY;
554 _dbus_verbose ("No path field in message\n");
555 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
558 /* Find the deepest path that covers the path in the message */
559 subtree = find_handler (tree, (const char**) path);
561 /* Build a list of all paths that cover the path in the message */
565 while (subtree != NULL)
567 if (subtree->message_function != NULL)
569 _dbus_object_subtree_ref (subtree);
571 /* run deepest paths first */
572 if (!_dbus_list_append (&list, subtree))
574 result = DBUS_HANDLER_RESULT_NEED_MEMORY;
575 _dbus_object_subtree_unref (subtree);
576 goto free_and_return;
580 subtree = subtree->parent;
583 _dbus_verbose ("%d handlers in the path tree for this message\n",
584 _dbus_list_get_length (&list));
586 /* Invoke each handler in the list */
588 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
590 link = _dbus_list_get_first_link (&list);
593 DBusList *next = _dbus_list_get_next_link (&list, link);
594 subtree = link->data;
596 /* message_function is NULL if we're unregistered
599 if (subtree->message_function)
601 DBusObjectPathMessageFunction message_function;
604 message_function = subtree->message_function;
605 user_data = subtree->user_data;
608 _dbus_verbose (" (invoking a handler)\n");
611 #ifdef DBUS_BUILD_TESTS
612 if (tree->connection)
614 _dbus_connection_unlock (tree->connection);
616 /* FIXME you could unregister the subtree in another thread
617 * before we invoke the callback, and I can't figure out a
618 * good way to solve this.
621 result = (* message_function) (tree->connection,
625 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
626 goto free_and_return;
628 #ifdef DBUS_BUILD_TESTS
629 if (tree->connection)
631 _dbus_connection_lock (tree->connection);
637 #ifdef DBUS_BUILD_TESTS
638 if (tree->connection)
640 _dbus_connection_unlock (tree->connection);
645 link = _dbus_list_get_first_link (&list);
646 _dbus_object_subtree_unref (link->data);
647 _dbus_list_remove_link (&list, link);
649 dbus_free_string_array (path);
655 * Allocates a subtree object.
657 * @param name name to duplicate.
658 * @returns newly-allocated subtree
660 static DBusObjectSubtree*
661 allocate_subtree_object (const char *name)
664 DBusObjectSubtree *subtree;
665 const size_t front_padding = _DBUS_STRUCT_OFFSET (DBusObjectSubtree, name);
667 _dbus_assert (name != NULL);
671 subtree = dbus_malloc (front_padding + (len + 1));
676 memcpy (subtree->name, name, len + 1);
681 static DBusObjectSubtree*
682 _dbus_object_subtree_new (const char *name,
683 const DBusObjectPathVTable *vtable,
686 DBusObjectSubtree *subtree;
688 subtree = allocate_subtree_object (name);
692 _dbus_assert (name != NULL);
694 subtree->parent = NULL;
698 subtree->message_function = vtable->message_function;
699 subtree->unregister_function = vtable->unregister_function;
703 subtree->message_function = NULL;
704 subtree->unregister_function = NULL;
707 subtree->user_data = user_data;
708 subtree->refcount.value = 1;
709 subtree->subtrees = NULL;
710 subtree->n_subtrees = 0;
711 subtree->subtrees_sorted = TRUE;
725 _dbus_object_subtree_ref (DBusObjectSubtree *subtree)
727 _dbus_assert (subtree->refcount.value > 0);
728 _dbus_atomic_inc (&subtree->refcount);
732 _dbus_object_subtree_unref (DBusObjectSubtree *subtree)
734 _dbus_assert (subtree->refcount.value > 0);
736 if (_dbus_atomic_dec (&subtree->refcount) == 1)
738 _dbus_assert (subtree->unregister_function == NULL);
739 _dbus_assert (subtree->message_function == NULL);
741 dbus_free (subtree->subtrees);
748 #ifdef DBUS_BUILD_TESTS
749 #include "dbus-test.h"
753 flatten_path (const char **path)
759 if (!_dbus_string_init (&str))
765 if (!_dbus_string_append_byte (&str, '/'))
768 if (!_dbus_string_append (&str, path[i]))
774 if (!_dbus_string_steal_data (&str, &s))
777 _dbus_string_free (&str);
782 _dbus_string_free (&str);
786 /* Returns TRUE if container is a parent of child
789 path_contains (const char **container,
795 while (child[i] != NULL)
799 if (container[i] == NULL)
800 return TRUE; /* container ran out, child continues;
801 * thus the container is a parent of the
805 _dbus_assert (container[i] != NULL);
806 _dbus_assert (child[i] != NULL);
808 v = strcmp (container[i], child[i]);
811 return FALSE; /* they overlap until here and then are different,
818 /* Child ran out; if container also did, they are equal;
819 * otherwise, the child is a parent of the container.
821 if (container[i] == NULL)
822 return TRUE; /* equal is counted as containing */
828 spew_subtree_recurse (DBusObjectSubtree *subtree,
840 _dbus_verbose ("%s (%d children)\n",
841 subtree->name, subtree->n_subtrees);
844 while (i < subtree->n_subtrees)
846 spew_subtree_recurse (subtree->subtrees[i], indent + 2);
853 spew_tree (DBusObjectTree *tree)
855 spew_subtree_recurse (tree->root, 0);
861 dbus_bool_t message_handled;
862 dbus_bool_t handler_unregistered;
868 test_unregister_function (DBusConnection *connection,
871 TreeTestData *ttd = user_data;
873 ttd->handler_unregistered = TRUE;
876 static DBusHandlerResult
877 test_message_function (DBusConnection *connection,
878 DBusMessage *message,
881 TreeTestData *ttd = user_data;
883 ttd->message_handled = TRUE;
885 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
889 do_register (DBusObjectTree *tree,
892 TreeTestData *tree_test_data)
894 DBusObjectPathVTable vtable = { test_unregister_function,
895 test_message_function, NULL };
897 tree_test_data[i].message_handled = FALSE;
898 tree_test_data[i].handler_unregistered = FALSE;
899 tree_test_data[i].path = path;
901 if (!_dbus_object_tree_register (tree, TRUE, path,
910 do_test_dispatch (DBusObjectTree *tree,
913 TreeTestData *tree_test_data,
916 DBusMessage *message;
918 DBusHandlerResult result;
923 flat = flatten_path (path);
927 message = dbus_message_new_method_call (NULL,
929 "org.freedesktop.TestInterface",
936 while (j < n_test_data)
938 tree_test_data[j].message_handled = FALSE;
942 result = _dbus_object_tree_dispatch_and_unlock (tree, message);
943 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
946 _dbus_assert (tree_test_data[i].message_handled);
949 while (j < n_test_data)
951 if (tree_test_data[j].message_handled)
952 _dbus_assert (path_contains (tree_test_data[j].path,
955 _dbus_assert (!path_contains (tree_test_data[j].path,
961 dbus_message_unref (message);
967 dbus_message_unref (message);
972 object_tree_test_iteration (void *data)
974 const char *path1[] = { "foo", NULL };
975 const char *path2[] = { "foo", "bar", NULL };
976 const char *path3[] = { "foo", "bar", "baz", NULL };
977 const char *path4[] = { "foo", "bar", "boo", NULL };
978 const char *path5[] = { "blah", NULL };
979 const char *path6[] = { "blah", "boof", NULL };
980 const char *path7[] = { "blah", "boof", "this", "is", "really", "long", NULL };
981 const char *path8[] = { "childless", NULL };
982 DBusObjectTree *tree;
983 TreeTestData tree_test_data[8];
988 tree = _dbus_object_tree_new (NULL);
992 if (!do_register (tree, path1, 0, tree_test_data))
995 _dbus_assert (find_subtree (tree, path1, NULL));
996 _dbus_assert (!find_subtree (tree, path2, NULL));
997 _dbus_assert (!find_subtree (tree, path3, NULL));
998 _dbus_assert (!find_subtree (tree, path4, NULL));
999 _dbus_assert (!find_subtree (tree, path5, NULL));
1000 _dbus_assert (!find_subtree (tree, path6, NULL));
1001 _dbus_assert (!find_subtree (tree, path7, NULL));
1002 _dbus_assert (!find_subtree (tree, path8, NULL));
1004 _dbus_assert (find_handler (tree, path1));
1005 _dbus_assert (find_handler (tree, path2));
1006 _dbus_assert (find_handler (tree, path3));
1007 _dbus_assert (find_handler (tree, path4));
1008 _dbus_assert (find_handler (tree, path5) == tree->root);
1009 _dbus_assert (find_handler (tree, path6) == tree->root);
1010 _dbus_assert (find_handler (tree, path7) == tree->root);
1011 _dbus_assert (find_handler (tree, path8) == tree->root);
1013 if (!do_register (tree, path2, 1, tree_test_data))
1016 _dbus_assert (find_subtree (tree, path1, NULL));
1017 _dbus_assert (find_subtree (tree, path2, NULL));
1018 _dbus_assert (!find_subtree (tree, path3, NULL));
1019 _dbus_assert (!find_subtree (tree, path4, NULL));
1020 _dbus_assert (!find_subtree (tree, path5, NULL));
1021 _dbus_assert (!find_subtree (tree, path6, NULL));
1022 _dbus_assert (!find_subtree (tree, path7, NULL));
1023 _dbus_assert (!find_subtree (tree, path8, NULL));
1025 if (!do_register (tree, path3, 2, tree_test_data))
1028 _dbus_assert (find_subtree (tree, path1, NULL));
1029 _dbus_assert (find_subtree (tree, path2, NULL));
1030 _dbus_assert (find_subtree (tree, path3, NULL));
1031 _dbus_assert (!find_subtree (tree, path4, NULL));
1032 _dbus_assert (!find_subtree (tree, path5, NULL));
1033 _dbus_assert (!find_subtree (tree, path6, NULL));
1034 _dbus_assert (!find_subtree (tree, path7, NULL));
1035 _dbus_assert (!find_subtree (tree, path8, NULL));
1037 if (!do_register (tree, path4, 3, tree_test_data))
1040 _dbus_assert (find_subtree (tree, path1, NULL));
1041 _dbus_assert (find_subtree (tree, path2, NULL));
1042 _dbus_assert (find_subtree (tree, path3, NULL));
1043 _dbus_assert (find_subtree (tree, path4, NULL));
1044 _dbus_assert (!find_subtree (tree, path5, NULL));
1045 _dbus_assert (!find_subtree (tree, path6, NULL));
1046 _dbus_assert (!find_subtree (tree, path7, NULL));
1047 _dbus_assert (!find_subtree (tree, path8, NULL));
1049 if (!do_register (tree, path5, 4, tree_test_data))
1052 _dbus_assert (find_subtree (tree, path1, NULL));
1053 _dbus_assert (find_subtree (tree, path2, NULL));
1054 _dbus_assert (find_subtree (tree, path3, NULL));
1055 _dbus_assert (find_subtree (tree, path4, NULL));
1056 _dbus_assert (find_subtree (tree, path5, NULL));
1057 _dbus_assert (!find_subtree (tree, path6, NULL));
1058 _dbus_assert (!find_subtree (tree, path7, NULL));
1059 _dbus_assert (!find_subtree (tree, path8, NULL));
1061 _dbus_assert (find_handler (tree, path1) != tree->root);
1062 _dbus_assert (find_handler (tree, path2) != tree->root);
1063 _dbus_assert (find_handler (tree, path3) != tree->root);
1064 _dbus_assert (find_handler (tree, path4) != tree->root);
1065 _dbus_assert (find_handler (tree, path5) != tree->root);
1066 _dbus_assert (find_handler (tree, path6) != tree->root);
1067 _dbus_assert (find_handler (tree, path7) != tree->root);
1068 _dbus_assert (find_handler (tree, path8) == tree->root);
1070 if (!do_register (tree, path6, 5, tree_test_data))
1073 _dbus_assert (find_subtree (tree, path1, NULL));
1074 _dbus_assert (find_subtree (tree, path2, NULL));
1075 _dbus_assert (find_subtree (tree, path3, NULL));
1076 _dbus_assert (find_subtree (tree, path4, NULL));
1077 _dbus_assert (find_subtree (tree, path5, NULL));
1078 _dbus_assert (find_subtree (tree, path6, NULL));
1079 _dbus_assert (!find_subtree (tree, path7, NULL));
1080 _dbus_assert (!find_subtree (tree, path8, NULL));
1082 if (!do_register (tree, path7, 6, tree_test_data))
1085 _dbus_assert (find_subtree (tree, path1, NULL));
1086 _dbus_assert (find_subtree (tree, path2, NULL));
1087 _dbus_assert (find_subtree (tree, path3, NULL));
1088 _dbus_assert (find_subtree (tree, path4, NULL));
1089 _dbus_assert (find_subtree (tree, path5, NULL));
1090 _dbus_assert (find_subtree (tree, path6, NULL));
1091 _dbus_assert (find_subtree (tree, path7, NULL));
1092 _dbus_assert (!find_subtree (tree, path8, NULL));
1094 if (!do_register (tree, path8, 7, tree_test_data))
1097 _dbus_assert (find_subtree (tree, path1, NULL));
1098 _dbus_assert (find_subtree (tree, path2, NULL));
1099 _dbus_assert (find_subtree (tree, path3, NULL));
1100 _dbus_assert (find_subtree (tree, path4, NULL));
1101 _dbus_assert (find_subtree (tree, path5, NULL));
1102 _dbus_assert (find_subtree (tree, path6, NULL));
1103 _dbus_assert (find_subtree (tree, path7, NULL));
1104 _dbus_assert (find_subtree (tree, path8, NULL));
1106 _dbus_assert (find_handler (tree, path1) != tree->root);
1107 _dbus_assert (find_handler (tree, path2) != tree->root);
1108 _dbus_assert (find_handler (tree, path3) != tree->root);
1109 _dbus_assert (find_handler (tree, path4) != tree->root);
1110 _dbus_assert (find_handler (tree, path5) != tree->root);
1111 _dbus_assert (find_handler (tree, path6) != tree->root);
1112 _dbus_assert (find_handler (tree, path7) != tree->root);
1113 _dbus_assert (find_handler (tree, path8) != tree->root);
1115 /* Check that destroying tree calls unregister funcs */
1116 _dbus_object_tree_unref (tree);
1119 while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1121 _dbus_assert (tree_test_data[i].handler_unregistered);
1122 _dbus_assert (!tree_test_data[i].message_handled);
1126 /* Now start again and try the individual unregister function */
1127 tree = _dbus_object_tree_new (NULL);
1131 if (!do_register (tree, path1, 0, tree_test_data))
1133 if (!do_register (tree, path2, 1, tree_test_data))
1135 if (!do_register (tree, path3, 2, tree_test_data))
1137 if (!do_register (tree, path4, 3, tree_test_data))
1139 if (!do_register (tree, path5, 4, tree_test_data))
1141 if (!do_register (tree, path6, 5, tree_test_data))
1143 if (!do_register (tree, path7, 6, tree_test_data))
1145 if (!do_register (tree, path8, 7, tree_test_data))
1148 _dbus_object_tree_unregister_and_unlock (tree, path1);
1150 _dbus_assert (!find_subtree (tree, path1, NULL));
1151 _dbus_assert (find_subtree (tree, path2, NULL));
1152 _dbus_assert (find_subtree (tree, path3, NULL));
1153 _dbus_assert (find_subtree (tree, path4, NULL));
1154 _dbus_assert (find_subtree (tree, path5, NULL));
1155 _dbus_assert (find_subtree (tree, path6, NULL));
1156 _dbus_assert (find_subtree (tree, path7, NULL));
1157 _dbus_assert (find_subtree (tree, path8, NULL));
1159 _dbus_object_tree_unregister_and_unlock (tree, path2);
1161 _dbus_assert (!find_subtree (tree, path1, NULL));
1162 _dbus_assert (!find_subtree (tree, path2, NULL));
1163 _dbus_assert (find_subtree (tree, path3, NULL));
1164 _dbus_assert (find_subtree (tree, path4, NULL));
1165 _dbus_assert (find_subtree (tree, path5, NULL));
1166 _dbus_assert (find_subtree (tree, path6, NULL));
1167 _dbus_assert (find_subtree (tree, path7, NULL));
1168 _dbus_assert (find_subtree (tree, path8, NULL));
1170 _dbus_object_tree_unregister_and_unlock (tree, path3);
1172 _dbus_assert (!find_subtree (tree, path1, NULL));
1173 _dbus_assert (!find_subtree (tree, path2, NULL));
1174 _dbus_assert (!find_subtree (tree, path3, NULL));
1175 _dbus_assert (find_subtree (tree, path4, NULL));
1176 _dbus_assert (find_subtree (tree, path5, NULL));
1177 _dbus_assert (find_subtree (tree, path6, NULL));
1178 _dbus_assert (find_subtree (tree, path7, NULL));
1179 _dbus_assert (find_subtree (tree, path8, NULL));
1181 _dbus_object_tree_unregister_and_unlock (tree, path4);
1183 _dbus_assert (!find_subtree (tree, path1, NULL));
1184 _dbus_assert (!find_subtree (tree, path2, NULL));
1185 _dbus_assert (!find_subtree (tree, path3, NULL));
1186 _dbus_assert (!find_subtree (tree, path4, NULL));
1187 _dbus_assert (find_subtree (tree, path5, NULL));
1188 _dbus_assert (find_subtree (tree, path6, NULL));
1189 _dbus_assert (find_subtree (tree, path7, NULL));
1190 _dbus_assert (find_subtree (tree, path8, NULL));
1192 _dbus_object_tree_unregister_and_unlock (tree, path5);
1194 _dbus_assert (!find_subtree (tree, path1, NULL));
1195 _dbus_assert (!find_subtree (tree, path2, NULL));
1196 _dbus_assert (!find_subtree (tree, path3, NULL));
1197 _dbus_assert (!find_subtree (tree, path4, NULL));
1198 _dbus_assert (!find_subtree (tree, path5, NULL));
1199 _dbus_assert (find_subtree (tree, path6, NULL));
1200 _dbus_assert (find_subtree (tree, path7, NULL));
1201 _dbus_assert (find_subtree (tree, path8, NULL));
1203 _dbus_object_tree_unregister_and_unlock (tree, path6);
1205 _dbus_assert (!find_subtree (tree, path1, NULL));
1206 _dbus_assert (!find_subtree (tree, path2, NULL));
1207 _dbus_assert (!find_subtree (tree, path3, NULL));
1208 _dbus_assert (!find_subtree (tree, path4, NULL));
1209 _dbus_assert (!find_subtree (tree, path5, NULL));
1210 _dbus_assert (!find_subtree (tree, path6, NULL));
1211 _dbus_assert (find_subtree (tree, path7, NULL));
1212 _dbus_assert (find_subtree (tree, path8, NULL));
1214 _dbus_object_tree_unregister_and_unlock (tree, path7);
1216 _dbus_assert (!find_subtree (tree, path1, NULL));
1217 _dbus_assert (!find_subtree (tree, path2, NULL));
1218 _dbus_assert (!find_subtree (tree, path3, NULL));
1219 _dbus_assert (!find_subtree (tree, path4, NULL));
1220 _dbus_assert (!find_subtree (tree, path5, NULL));
1221 _dbus_assert (!find_subtree (tree, path6, NULL));
1222 _dbus_assert (!find_subtree (tree, path7, NULL));
1223 _dbus_assert (find_subtree (tree, path8, NULL));
1225 _dbus_object_tree_unregister_and_unlock (tree, path8);
1227 _dbus_assert (!find_subtree (tree, path1, NULL));
1228 _dbus_assert (!find_subtree (tree, path2, NULL));
1229 _dbus_assert (!find_subtree (tree, path3, NULL));
1230 _dbus_assert (!find_subtree (tree, path4, NULL));
1231 _dbus_assert (!find_subtree (tree, path5, NULL));
1232 _dbus_assert (!find_subtree (tree, path6, NULL));
1233 _dbus_assert (!find_subtree (tree, path7, NULL));
1234 _dbus_assert (!find_subtree (tree, path8, NULL));
1237 while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1239 _dbus_assert (tree_test_data[i].handler_unregistered);
1240 _dbus_assert (!tree_test_data[i].message_handled);
1244 /* Register it all again, and test dispatch */
1246 if (!do_register (tree, path1, 0, tree_test_data))
1248 if (!do_register (tree, path2, 1, tree_test_data))
1250 if (!do_register (tree, path3, 2, tree_test_data))
1252 if (!do_register (tree, path4, 3, tree_test_data))
1254 if (!do_register (tree, path5, 4, tree_test_data))
1256 if (!do_register (tree, path6, 5, tree_test_data))
1258 if (!do_register (tree, path7, 6, tree_test_data))
1260 if (!do_register (tree, path8, 7, tree_test_data))
1267 if (!do_test_dispatch (tree, path1, 0, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1269 if (!do_test_dispatch (tree, path2, 1, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1271 if (!do_test_dispatch (tree, path3, 2, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1273 if (!do_test_dispatch (tree, path4, 3, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1275 if (!do_test_dispatch (tree, path5, 4, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1277 if (!do_test_dispatch (tree, path6, 5, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1279 if (!do_test_dispatch (tree, path7, 6, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1281 if (!do_test_dispatch (tree, path8, 7, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1288 _dbus_object_tree_ref (tree);
1289 _dbus_object_tree_unref (tree);
1290 _dbus_object_tree_unref (tree);
1297 * @ingroup DBusObjectTree
1298 * Unit test for DBusObjectTree
1299 * @returns #TRUE on success.
1302 _dbus_object_tree_test (void)
1304 _dbus_test_oom_handling ("object tree",
1305 object_tree_test_iteration,
1311 #endif /* DBUS_BUILD_TESTS */