-/* -*- mode: C; c-file-style: "gnu" -*- */
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-object-tree.c DBusObjectTree (internals of DBusConnection)
*
- * Copyright (C) 2003 Red Hat Inc.
+ * Copyright (C) 2003, 2005 Red Hat Inc.
*
* Licensed under the Academic Free License version 2.1
*
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
+
+#include <config.h>
#include "dbus-object-tree.h"
#include "dbus-connection-internal.h"
#include "dbus-internals.h"
void *user_data; /**< Data for functions */
DBusObjectSubtree **subtrees; /**< Child nodes */
int n_subtrees; /**< Number of child nodes */
- unsigned int subtrees_sorted : 1; /**< Whether children are sorted */
+ int max_subtrees; /**< Number of allocated entries in subtrees */
unsigned int invoke_as_fallback : 1; /**< Whether to invoke message_function when child nodes don't handle the message */
char name[1]; /**< Allocated as large as necessary */
};
}
}
-static int
-subtree_cmp (DBusObjectSubtree *subtree_a,
- DBusObjectSubtree *subtree_b)
-{
- return strcmp (subtree_a->name, subtree_b->name);
-}
-
-static int
-subtree_qsort_cmp (const void *a,
- const void *b)
-{
- DBusObjectSubtree **subtree_a_p = (void*) a;
- DBusObjectSubtree **subtree_b_p = (void*) b;
-
- return subtree_cmp (*subtree_a_p, *subtree_b_p);
-}
-
-static void
-ensure_sorted (DBusObjectSubtree *subtree)
-{
- if (subtree->subtrees && !subtree->subtrees_sorted)
- {
- qsort (subtree->subtrees,
- subtree->n_subtrees,
- sizeof (DBusObjectSubtree*),
- subtree_qsort_cmp);
- subtree->subtrees_sorted = TRUE;
- }
-}
-
/** Set to 1 to get a bunch of debug spew about finding the
* subtree nodes
*/
int *index_in_parent,
dbus_bool_t *exact_match)
{
- int i;
+ int i, j;
dbus_bool_t return_deepest_match;
return_deepest_match = exact_match != NULL;
subtree->name, path[0]);
#endif
- ensure_sorted (subtree);
-
- /* FIXME we should do a binary search here instead
- * of O(n)
- */
-
i = 0;
- while (i < subtree->n_subtrees)
+ j = subtree->n_subtrees;
+ while (i < j)
{
- int v;
+ int k, v;
- v = strcmp (path[0], subtree->subtrees[i]->name);
+ k = (i + j) / 2;
+ v = strcmp (path[0], subtree->subtrees[k]->name);
#if VERBOSE_FIND
_dbus_verbose (" %s cmp %s = %d\n",
- path[0], subtree->subtrees[i]->name,
+ path[0], subtree->subtrees[k]->name,
v);
#endif
if (index_in_parent)
{
#if VERBOSE_FIND
- _dbus_verbose (" storing parent index %d\n", i);
+ _dbus_verbose (" storing parent index %d\n", k);
#endif
- *index_in_parent = i;
+ *index_in_parent = k;
}
if (return_deepest_match)
{
DBusObjectSubtree *next;
- next = find_subtree_recurse (subtree->subtrees[i],
+ next = find_subtree_recurse (subtree->subtrees[k],
&path[1], create_if_not_found,
index_in_parent, exact_match);
if (next == NULL &&
return next;
}
else
- return find_subtree_recurse (subtree->subtrees[i],
+ return find_subtree_recurse (subtree->subtrees[k],
&path[1], create_if_not_found,
index_in_parent, exact_match);
}
else if (v < 0)
{
- goto not_found;
+ j = k;
+ }
+ else
+ {
+ i = k + 1;
}
-
- ++i;
}
- not_found:
#if VERBOSE_FIND
_dbus_verbose (" no match found, current tree %s, create_if_not_found = %d\n",
subtree->name, create_if_not_found);
if (create_if_not_found)
{
DBusObjectSubtree* child;
- DBusObjectSubtree **new_subtrees;
- int new_n_subtrees;
+ int child_pos, new_n_subtrees;
#if VERBOSE_FIND
_dbus_verbose (" creating subtree %s\n",
if (child == NULL)
return NULL;
- /* FIXME we should do the "double alloc each time" standard thing */
new_n_subtrees = subtree->n_subtrees + 1;
- new_subtrees = dbus_realloc (subtree->subtrees,
- new_n_subtrees * sizeof (DBusObjectSubtree*));
- if (new_subtrees == NULL)
+ if (new_n_subtrees > subtree->max_subtrees)
{
- child->unregister_function = NULL;
- child->message_function = NULL;
- _dbus_object_subtree_unref (child);
- return NULL;
+ int new_max_subtrees;
+ DBusObjectSubtree **new_subtrees;
+
+ new_max_subtrees = subtree->max_subtrees == 0 ? 1 : 2 * subtree->max_subtrees;
+ new_subtrees = dbus_realloc (subtree->subtrees,
+ new_max_subtrees * sizeof (DBusObjectSubtree*));
+ if (new_subtrees == NULL)
+ {
+ _dbus_object_subtree_unref (child);
+ return NULL;
+ }
+ subtree->subtrees = new_subtrees;
+ subtree->max_subtrees = new_max_subtrees;
}
- new_subtrees[subtree->n_subtrees] = child;
+ /* The binary search failed, so i == j points to the
+ place the child should be inserted. */
+ child_pos = i;
+ _dbus_assert (child_pos < new_n_subtrees &&
+ new_n_subtrees <= subtree->max_subtrees);
+ if (child_pos + 1 < new_n_subtrees)
+ {
+ memmove (&subtree->subtrees[child_pos+1],
+ &subtree->subtrees[child_pos],
+ (new_n_subtrees - child_pos - 1) *
+ sizeof subtree->subtrees[0]);
+ }
+ subtree->subtrees[child_pos] = child;
+
if (index_in_parent)
- *index_in_parent = subtree->n_subtrees;
- subtree->subtrees_sorted = FALSE;
+ *index_in_parent = child_pos;
subtree->n_subtrees = new_n_subtrees;
- subtree->subtrees = new_subtrees;
-
child->parent = subtree;
return find_subtree_recurse (child,
_dbus_verbose ("Looking for deepest handler\n");
#endif
_dbus_assert (exact_match != NULL);
+
+ *exact_match = FALSE; /* ensure always initialized */
+
return find_subtree_recurse (tree->root, path, FALSE, NULL, exact_match);
}
return find_subtree_recurse (tree->root, path, TRUE, NULL, NULL);
}
+static char *flatten_path (const char **path);
+
/**
* Registers a new subtree in the global object tree.
*
* @param path NULL-terminated array of path elements giving path to subtree
* @param vtable the vtable used to traverse this subtree
* @param user_data user data to pass to methods in the vtable
- * @returns #FALSE if not enough memory
+ * @param error address where an error can be returned
+ * @returns #FALSE if an error (#DBUS_ERROR_NO_MEMORY or
+ * #DBUS_ERROR_OBJECT_PATH_IN_USE) is reported
*/
dbus_bool_t
_dbus_object_tree_register (DBusObjectTree *tree,
dbus_bool_t fallback,
const char **path,
const DBusObjectPathVTable *vtable,
- void *user_data)
+ void *user_data,
+ DBusError *error)
{
DBusObjectSubtree *subtree;
subtree = ensure_subtree (tree, path);
if (subtree == NULL)
- return FALSE;
+ {
+ _DBUS_SET_OOM (error);
+ return FALSE;
+ }
-#ifndef DBUS_DISABLE_CHECKS
if (subtree->message_function != NULL)
{
- _dbus_warn ("A handler is already registered for the path starting with path[0] = \"%s\"\n",
- path[0] ? path[0] : "null");
+ if (error != NULL)
+ {
+ char *complete_path = flatten_path (path);
+
+ dbus_set_error (error, DBUS_ERROR_OBJECT_PATH_IN_USE,
+ "A handler is already registered for %s",
+ complete_path ? complete_path
+ : "(cannot represent path: out of memory!)");
+
+ dbus_free (complete_path);
+ }
+
return FALSE;
}
-#else
- _dbus_assert (subtree->message_function == NULL);
-#endif
subtree->message_function = vtable->message_function;
subtree->unregister_function = vtable->unregister_function;
subtree->user_data = user_data;
subtree->invoke_as_fallback = fallback != FALSE;
-
+
return TRUE;
}
_dbus_assert (path != NULL);
+ unregister_function = NULL;
+ user_data = NULL;
+
subtree = find_subtree (tree, path, &i);
#ifndef DBUS_DISABLE_CHECKS
_dbus_warn ("Attempted to unregister path (path[0] = %s path[1] = %s) which isn't registered\n",
path[0] ? path[0] : "null",
path[1] ? path[1] : "null");
- return;
+ goto unlock;
}
#else
_dbus_assert (subtree != NULL);
}
subtree = NULL;
+unlock:
connection = tree->connection;
/* Unlock and call application code */
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
if (connection)
#endif
{
_dbus_connection_ref_unlocked (connection);
+ _dbus_verbose ("unlock\n");
_dbus_connection_unlock (connection);
}
if (unregister_function)
(* unregister_function) (connection, user_data);
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
if (connection)
#endif
dbus_connection_unref (connection);
}
static DBusHandlerResult
-handle_default_introspect_unlocked (DBusObjectTree *tree,
- DBusMessage *message,
- const char **path)
+handle_default_introspect_and_unlock (DBusObjectTree *tree,
+ DBusMessage *message,
+ const char **path)
{
DBusString xml;
DBusHandlerResult result;
char **children;
int i;
+ DBusMessage *reply;
+ DBusMessageIter iter;
+ const char *v_STRING;
+ dbus_bool_t already_unlocked;
+ /* We have the connection lock here */
+
+ already_unlocked = FALSE;
+
+ _dbus_verbose (" considering default Introspect() handler...\n");
+
+ reply = NULL;
+
if (!dbus_message_is_method_call (message,
- DBUS_INTERFACE_ORG_FREEDESKTOP_INTROSPECTABLE,
+ DBUS_INTERFACE_INTROSPECTABLE,
"Introspect"))
- return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+ {
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
+ if (tree->connection)
+#endif
+ {
+ _dbus_verbose ("unlock\n");
+ _dbus_connection_unlock (tree->connection);
+ }
+
+ return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
+ }
+
+ _dbus_verbose (" using default Introspect() handler!\n");
if (!_dbus_string_init (&xml))
- return DBUS_HANDLER_RESULT_NEED_MEMORY;
+ {
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
+ if (tree->connection)
+#endif
+ {
+ _dbus_verbose ("unlock\n");
+ _dbus_connection_unlock (tree->connection);
+ }
+
+ return DBUS_HANDLER_RESULT_NEED_MEMORY;
+ }
result = DBUS_HANDLER_RESULT_NEED_MEMORY;
if (!_dbus_object_tree_list_registered_unlocked (tree, path, &children))
goto out;
+ if (!_dbus_string_append (&xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
+ goto out;
+
if (!_dbus_string_append (&xml, "<node>\n"))
goto out;
if (!_dbus_string_append (&xml, "</node>\n"))
goto out;
+
+ reply = dbus_message_new_method_return (message);
+ if (reply == NULL)
+ goto out;
+
+ dbus_message_iter_init_append (reply, &iter);
+ v_STRING = _dbus_string_get_const_data (&xml);
+ if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &v_STRING))
+ goto out;
+
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
+ if (tree->connection)
+#endif
+ {
+ already_unlocked = TRUE;
+
+ if (!_dbus_connection_send_and_unlock (tree->connection, reply, NULL))
+ goto out;
+ }
result = DBUS_HANDLER_RESULT_HANDLED;
out:
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
+ if (tree->connection)
+#endif
+ {
+ if (!already_unlocked)
+ {
+ _dbus_verbose ("unlock\n");
+ _dbus_connection_unlock (tree->connection);
+ }
+ }
+
_dbus_string_free (&xml);
dbus_free_string_array (children);
+ if (reply)
+ dbus_message_unref (reply);
return result;
}
*/
DBusHandlerResult
_dbus_object_tree_dispatch_and_unlock (DBusObjectTree *tree,
- DBusMessage *message)
+ DBusMessage *message,
+ dbus_bool_t *found_object)
{
char **path;
dbus_bool_t exact_match;
path = NULL;
if (!dbus_message_get_path_decomposed (message, &path))
{
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
if (tree->connection)
#endif
- _dbus_connection_unlock (tree->connection);
+ {
+ _dbus_verbose ("unlock\n");
+ _dbus_connection_unlock (tree->connection);
+ }
_dbus_verbose ("No memory to get decomposed path\n");
if (path == NULL)
{
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
if (tree->connection)
#endif
- _dbus_connection_unlock (tree->connection);
+ {
+ _dbus_verbose ("unlock\n");
+ _dbus_connection_unlock (tree->connection);
+ }
_dbus_verbose ("No path field in message\n");
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
/* Find the deepest path that covers the path in the message */
subtree = find_handler (tree, (const char**) path, &exact_match);
+ if (found_object)
+ *found_object = !!subtree;
+
/* Build a list of all paths that cover the path in the message */
list = NULL;
_dbus_verbose (" (invoking a handler)\n");
#endif
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
if (tree->connection)
#endif
- _dbus_connection_unlock (tree->connection);
+ {
+ _dbus_verbose ("unlock\n");
+ _dbus_connection_unlock (tree->connection);
+ }
/* FIXME you could unregister the subtree in another thread
* before we invoke the callback, and I can't figure out a
message,
user_data);
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
if (tree->connection)
#endif
_dbus_connection_lock (tree->connection);
{
/* This hardcoded default handler does a minimal Introspect()
*/
- result = handle_default_introspect_unlocked (tree, message,
- (const char**) path);
+ result = handle_default_introspect_and_unlock (tree, message,
+ (const char**) path);
}
-
-#ifdef DBUS_BUILD_TESTS
- if (tree->connection)
+ else
+ {
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
+ if (tree->connection)
#endif
- _dbus_connection_unlock (tree->connection);
+ {
+ _dbus_verbose ("unlock\n");
+ _dbus_connection_unlock (tree->connection);
+ }
+ }
while (list != NULL)
{
}
/**
+ * Looks up the data passed to _dbus_object_tree_register() for a
+ * handler at the given path.
+ *
+ * @param tree the global object tree
+ * @param path NULL-terminated array of path elements giving path to subtree
+ * @returns the object's user_data or #NULL if none found
+ */
+void*
+_dbus_object_tree_get_user_data_unlocked (DBusObjectTree *tree,
+ const char **path)
+{
+ dbus_bool_t exact_match;
+ DBusObjectSubtree *subtree;
+
+ _dbus_assert (tree != NULL);
+ _dbus_assert (path != NULL);
+
+ /* Find the deepest path that covers the path in the message */
+ subtree = find_handler (tree, (const char**) path, &exact_match);
+
+ if ((subtree == NULL) || !exact_match)
+ {
+ _dbus_verbose ("No object at specified path found\n");
+ return NULL;
+ }
+
+ return subtree->user_data;
+}
+
+/**
* Allocates a subtree object.
*
* @param name name to duplicate.
len = strlen (name);
- subtree = dbus_malloc (front_padding + (len + 1));
+ subtree = dbus_malloc0 (MAX (front_padding + (len + 1), sizeof (DBusObjectSubtree)));
if (subtree == NULL)
return NULL;
}
subtree->user_data = user_data;
- subtree->refcount.value = 1;
+ _dbus_atomic_inc (&subtree->refcount);
subtree->subtrees = NULL;
subtree->n_subtrees = 0;
- subtree->subtrees_sorted = TRUE;
+ subtree->max_subtrees = 0;
subtree->invoke_as_fallback = FALSE;
return subtree;
oom:
- if (subtree)
- {
- dbus_free (subtree);
- }
-
return NULL;
}
static DBusObjectSubtree *
_dbus_object_subtree_ref (DBusObjectSubtree *subtree)
{
- _dbus_assert (subtree->refcount.value > 0);
+#ifdef DBUS_DISABLE_ASSERT
_dbus_atomic_inc (&subtree->refcount);
+#else
+ dbus_int32_t old_value;
+
+ old_value = _dbus_atomic_inc (&subtree->refcount);
+ _dbus_assert (old_value > 0);
+#endif
return subtree;
}
static void
_dbus_object_subtree_unref (DBusObjectSubtree *subtree)
{
- _dbus_assert (subtree->refcount.value > 0);
+ dbus_int32_t old_value;
+
+ old_value = _dbus_atomic_dec (&subtree->refcount);
+ _dbus_assert (old_value > 0);
- if (_dbus_atomic_dec (&subtree->refcount) == 1)
+ if (old_value == 1)
{
_dbus_assert (subtree->unregister_function == NULL);
_dbus_assert (subtree->message_function == NULL);
parent_path,
child_entries);
-#ifdef DBUS_BUILD_TESTS
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
if (tree->connection)
#endif
- _dbus_connection_unlock (tree->connection);
+ {
+ _dbus_verbose ("unlock\n");
+ _dbus_connection_unlock (tree->connection);
+ }
return result;
}
/**
* Decompose an object path. A path of just "/" is
* represented as an empty vector of strings.
+ * The path need not be nul terminated.
*
* @param data the path data
* @param len the length of the path string
int i, j, comp;
_dbus_assert (data != NULL);
-
+ _dbus_assert (path != NULL);
+
#if VERBOSE_DECOMPOSE
_dbus_verbose ("Decomposing path \"%s\"\n",
data);
#endif
n_components = 0;
- i = 0;
- while (i < len)
+ if (len > 1) /* if path is not just "/" */
{
- if (data[i] == '/')
- n_components += 1;
- ++i;
+ i = 0;
+ while (i < len)
+ {
+ _dbus_assert (data[i] != '\0');
+ if (data[i] == '/')
+ n_components += 1;
+ ++i;
+ }
}
retval = dbus_new0 (char*, n_components + 1);
return FALSE;
comp = 0;
- i = 0;
- while (i < len)
+ if (n_components == 0)
+ i = 1;
+ else
+ i = 0;
+ while (comp < n_components)
{
+ _dbus_assert (i < len);
+
if (data[i] == '/')
++i;
j = i;
/** @} */
-#ifdef DBUS_BUILD_TESTS
-#include "dbus-test.h"
-#include <stdio.h>
-
static char*
flatten_path (const char **path)
{
DBusString str;
- int i;
char *s;
if (!_dbus_string_init (&str))
return NULL;
- i = 0;
- while (path[i])
+ if (path[0] == NULL)
{
if (!_dbus_string_append_byte (&str, '/'))
goto nomem;
-
- if (!_dbus_string_append (&str, path[i]))
- goto nomem;
-
- ++i;
+ }
+ else
+ {
+ int i;
+
+ i = 0;
+ while (path[i])
+ {
+ if (!_dbus_string_append_byte (&str, '/'))
+ goto nomem;
+
+ if (!_dbus_string_append (&str, path[i]))
+ goto nomem;
+
+ ++i;
+ }
}
if (!_dbus_string_steal_data (&str, &s))
}
+#ifdef DBUS_ENABLE_EMBEDDED_TESTS
+
+#ifndef DOXYGEN_SHOULD_SKIP_THIS
+
+#include "dbus-test.h"
+#include <stdio.h>
+
typedef enum
{
STR_EQUAL,
if (container[i] == NULL)
return STR_PREFIX; /* container ran out, child continues;
- * thus the container is a parent of the
- * child.
- */
+ * thus the container is a parent of the
+ * child.
+ */
_dbus_assert (container[i] != NULL);
_dbus_assert (child[i] != NULL);
if (v != 0)
return STR_DIFFERENT; /* they overlap until here and then are different,
- * not overlapping
- */
+ * not overlapping
+ */
++i;
}
if (!_dbus_object_tree_register (tree, fallback, path,
&vtable,
- &tree_test_data[i]))
+ &tree_test_data[i],
+ NULL))
return FALSE;
+ _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path) ==
+ &tree_test_data[i]);
+
return TRUE;
}
++j;
}
- result = _dbus_object_tree_dispatch_and_unlock (tree, message);
+ result = _dbus_object_tree_dispatch_and_unlock (tree, message, NULL);
if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
goto oom;
}
static size_t
-string_array_length (char **array)
+string_array_length (const char **array)
{
size_t i;
for (i = 0; array[i]; i++) ;
return i;
}
+typedef struct
+{
+ const char *path;
+ const char *result[20];
+} DecomposePathTest;
+
+static DecomposePathTest decompose_tests[] = {
+ { "/foo", { "foo", NULL } },
+ { "/foo/bar", { "foo", "bar", NULL } },
+ { "/", { NULL } },
+ { "/a/b", { "a", "b", NULL } },
+ { "/a/b/c", { "a", "b", "c", NULL } },
+ { "/a/b/c/d", { "a", "b", "c", "d", NULL } },
+ { "/foo/bar/q", { "foo", "bar", "q", NULL } },
+ { "/foo/bar/this/is/longer", { "foo", "bar", "this", "is", "longer", NULL } }
+};
+
+static dbus_bool_t
+run_decompose_tests (void)
+{
+ int i;
+
+ i = 0;
+ while (i < _DBUS_N_ELEMENTS (decompose_tests))
+ {
+ char **result;
+ int result_len;
+ int expected_len;
+
+ if (!_dbus_decompose_path (decompose_tests[i].path,
+ strlen (decompose_tests[i].path),
+ &result, &result_len))
+ return FALSE;
+
+ expected_len = string_array_length (decompose_tests[i].result);
+
+ if (result_len != (int) string_array_length ((const char**)result) ||
+ expected_len != result_len ||
+ path_contains (decompose_tests[i].result,
+ (const char**) result) != STR_EQUAL)
+ {
+ int real_len = string_array_length ((const char**)result);
+ _dbus_warn ("Expected decompose of %s to have len %d, returned %d, appears to have %d\n",
+ decompose_tests[i].path, expected_len, result_len,
+ real_len);
+ _dbus_warn ("Decompose resulted in elements: { ");
+ i = 0;
+ while (i < real_len)
+ {
+ _dbus_warn ("\"%s\"%s", result[i],
+ (i + 1) == real_len ? "" : ", ");
+ ++i;
+ }
+ _dbus_warn ("}\n");
+ _dbus_assert_not_reached ("path decompose failed\n");
+ }
+
+ dbus_free_string_array (result);
+
+ ++i;
+ }
+
+ return TRUE;
+}
static dbus_bool_t
object_tree_test_iteration (void *data)
{
+ const char *path0[] = { NULL };
const char *path1[] = { "foo", NULL };
const char *path2[] = { "foo", "bar", NULL };
const char *path3[] = { "foo", "bar", "baz", NULL };
const char *path7[] = { "blah", "boof", "this", "is", "really", "long", NULL };
const char *path8[] = { "childless", NULL };
DBusObjectTree *tree;
- TreeTestData tree_test_data[8];
+ TreeTestData tree_test_data[9];
int i;
dbus_bool_t exact_match;
+ if (!run_decompose_tests ())
+ return FALSE;
+
tree = NULL;
tree = _dbus_object_tree_new (NULL);
if (tree == NULL)
goto out;
- if (!do_register (tree, path1, TRUE, 0, tree_test_data))
+ if (!do_register (tree, path0, TRUE, 0, tree_test_data))
+ goto out;
+
+ _dbus_assert (find_subtree (tree, path0, NULL));
+ _dbus_assert (!find_subtree (tree, path1, NULL));
+ _dbus_assert (!find_subtree (tree, path2, NULL));
+ _dbus_assert (!find_subtree (tree, path3, NULL));
+ _dbus_assert (!find_subtree (tree, path4, NULL));
+ _dbus_assert (!find_subtree (tree, path5, NULL));
+ _dbus_assert (!find_subtree (tree, path6, NULL));
+ _dbus_assert (!find_subtree (tree, path7, NULL));
+ _dbus_assert (!find_subtree (tree, path8, NULL));
+
+ _dbus_assert (find_handler (tree, path0, &exact_match) && exact_match);
+ _dbus_assert (find_handler (tree, path1, &exact_match) == tree->root && !exact_match);
+ _dbus_assert (find_handler (tree, path2, &exact_match) == tree->root && !exact_match);
+ _dbus_assert (find_handler (tree, path3, &exact_match) == tree->root && !exact_match);
+ _dbus_assert (find_handler (tree, path4, &exact_match) == tree->root && !exact_match);
+ _dbus_assert (find_handler (tree, path5, &exact_match) == tree->root && !exact_match);
+ _dbus_assert (find_handler (tree, path6, &exact_match) == tree->root && !exact_match);
+ _dbus_assert (find_handler (tree, path7, &exact_match) == tree->root && !exact_match);
+ _dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
+
+ if (!do_register (tree, path1, TRUE, 1, tree_test_data))
goto out;
+ _dbus_assert (find_subtree (tree, path0, NULL));
_dbus_assert (find_subtree (tree, path1, NULL));
_dbus_assert (!find_subtree (tree, path2, NULL));
_dbus_assert (!find_subtree (tree, path3, NULL));
_dbus_assert (!find_subtree (tree, path7, NULL));
_dbus_assert (!find_subtree (tree, path8, NULL));
+ _dbus_assert (find_handler (tree, path0, &exact_match) && exact_match);
_dbus_assert (find_handler (tree, path1, &exact_match) && exact_match);
_dbus_assert (find_handler (tree, path2, &exact_match) && !exact_match);
_dbus_assert (find_handler (tree, path3, &exact_match) && !exact_match);
_dbus_assert (find_handler (tree, path7, &exact_match) == tree->root && !exact_match);
_dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
- if (!do_register (tree, path2, TRUE, 1, tree_test_data))
+ if (!do_register (tree, path2, TRUE, 2, tree_test_data))
goto out;
_dbus_assert (find_subtree (tree, path1, NULL));
_dbus_assert (!find_subtree (tree, path7, NULL));
_dbus_assert (!find_subtree (tree, path8, NULL));
- if (!do_register (tree, path3, TRUE, 2, tree_test_data))
+ if (!do_register (tree, path3, TRUE, 3, tree_test_data))
goto out;
+ _dbus_assert (find_subtree (tree, path0, NULL));
_dbus_assert (find_subtree (tree, path1, NULL));
_dbus_assert (find_subtree (tree, path2, NULL));
_dbus_assert (find_subtree (tree, path3, NULL));
_dbus_assert (!find_subtree (tree, path7, NULL));
_dbus_assert (!find_subtree (tree, path8, NULL));
- if (!do_register (tree, path4, TRUE, 3, tree_test_data))
+ if (!do_register (tree, path4, TRUE, 4, tree_test_data))
goto out;
+ _dbus_assert (find_subtree (tree, path0, NULL));
_dbus_assert (find_subtree (tree, path1, NULL));
_dbus_assert (find_subtree (tree, path2, NULL));
_dbus_assert (find_subtree (tree, path3, NULL));
_dbus_assert (!find_subtree (tree, path7, NULL));
_dbus_assert (!find_subtree (tree, path8, NULL));
- if (!do_register (tree, path5, TRUE, 4, tree_test_data))
+ if (!do_register (tree, path5, TRUE, 5, tree_test_data))
goto out;
+ _dbus_assert (find_subtree (tree, path0, NULL));
_dbus_assert (find_subtree (tree, path1, NULL));
_dbus_assert (find_subtree (tree, path2, NULL));
_dbus_assert (find_subtree (tree, path3, NULL));
_dbus_assert (!find_subtree (tree, path6, NULL));
_dbus_assert (!find_subtree (tree, path7, NULL));
_dbus_assert (!find_subtree (tree, path8, NULL));
-
+
+ _dbus_assert (find_handler (tree, path0, &exact_match) == tree->root && exact_match);
_dbus_assert (find_handler (tree, path1, &exact_match) != tree->root && exact_match);
_dbus_assert (find_handler (tree, path2, &exact_match) != tree->root && exact_match);
_dbus_assert (find_handler (tree, path3, &exact_match) != tree->root && exact_match);
_dbus_assert (find_handler (tree, path7, &exact_match) != tree->root && !exact_match);
_dbus_assert (find_handler (tree, path8, &exact_match) == tree->root && !exact_match);
- if (!do_register (tree, path6, TRUE, 5, tree_test_data))
+ if (!do_register (tree, path6, TRUE, 6, tree_test_data))
goto out;
+ _dbus_assert (find_subtree (tree, path0, NULL));
_dbus_assert (find_subtree (tree, path1, NULL));
_dbus_assert (find_subtree (tree, path2, NULL));
_dbus_assert (find_subtree (tree, path3, NULL));
_dbus_assert (!find_subtree (tree, path7, NULL));
_dbus_assert (!find_subtree (tree, path8, NULL));
- if (!do_register (tree, path7, TRUE, 6, tree_test_data))
+ if (!do_register (tree, path7, TRUE, 7, tree_test_data))
goto out;
+ _dbus_assert (find_subtree (tree, path0, NULL));
_dbus_assert (find_subtree (tree, path1, NULL));
_dbus_assert (find_subtree (tree, path2, NULL));
_dbus_assert (find_subtree (tree, path3, NULL));
_dbus_assert (find_subtree (tree, path7, NULL));
_dbus_assert (!find_subtree (tree, path8, NULL));
- if (!do_register (tree, path8, TRUE, 7, tree_test_data))
+ if (!do_register (tree, path8, TRUE, 8, tree_test_data))
goto out;
+ _dbus_assert (find_subtree (tree, path0, NULL));
_dbus_assert (find_subtree (tree, path1, NULL));
_dbus_assert (find_subtree (tree, path2, NULL));
_dbus_assert (find_subtree (tree, path3, NULL));
_dbus_assert (find_subtree (tree, path6, NULL));
_dbus_assert (find_subtree (tree, path7, NULL));
_dbus_assert (find_subtree (tree, path8, NULL));
-
+
+ _dbus_assert (find_handler (tree, path0, &exact_match) == tree->root && exact_match);
_dbus_assert (find_handler (tree, path1, &exact_match) != tree->root && exact_match);
_dbus_assert (find_handler (tree, path2, &exact_match) != tree->root && exact_match);
_dbus_assert (find_handler (tree, path3, &exact_match) != tree->root && exact_match);
_dbus_object_tree_list_registered_unlocked (tree, path1, &child_entries);
if (child_entries != NULL)
{
- nb = string_array_length (child_entries);
+ nb = string_array_length ((const char**)child_entries);
_dbus_assert (nb == 1);
dbus_free_string_array (child_entries);
}
_dbus_object_tree_list_registered_unlocked (tree, path2, &child_entries);
if (child_entries != NULL)
{
- nb = string_array_length (child_entries);
+ nb = string_array_length ((const char**)child_entries);
_dbus_assert (nb == 2);
dbus_free_string_array (child_entries);
}
_dbus_object_tree_list_registered_unlocked (tree, path8, &child_entries);
if (child_entries != NULL)
{
- nb = string_array_length (child_entries);
+ nb = string_array_length ((const char**)child_entries);
_dbus_assert (nb == 0);
dbus_free_string_array (child_entries);
}
_dbus_object_tree_list_registered_unlocked (tree, root, &child_entries);
if (child_entries != NULL)
{
- nb = string_array_length (child_entries);
+ nb = string_array_length ((const char**)child_entries);
_dbus_assert (nb == 3);
dbus_free_string_array (child_entries);
}
if (tree == NULL)
goto out;
- if (!do_register (tree, path1, TRUE, 0, tree_test_data))
+ if (!do_register (tree, path0, TRUE, 0, tree_test_data))
goto out;
- if (!do_register (tree, path2, TRUE, 1, tree_test_data))
+ if (!do_register (tree, path1, TRUE, 1, tree_test_data))
goto out;
- if (!do_register (tree, path3, TRUE, 2, tree_test_data))
+ if (!do_register (tree, path2, TRUE, 2, tree_test_data))
goto out;
- if (!do_register (tree, path4, TRUE, 3, tree_test_data))
+ if (!do_register (tree, path3, TRUE, 3, tree_test_data))
goto out;
- if (!do_register (tree, path5, TRUE, 4, tree_test_data))
+ if (!do_register (tree, path4, TRUE, 4, tree_test_data))
goto out;
- if (!do_register (tree, path6, TRUE, 5, tree_test_data))
+ if (!do_register (tree, path5, TRUE, 5, tree_test_data))
goto out;
- if (!do_register (tree, path7, TRUE, 6, tree_test_data))
+ if (!do_register (tree, path6, TRUE, 6, tree_test_data))
goto out;
- if (!do_register (tree, path8, TRUE, 7, tree_test_data))
+ if (!do_register (tree, path7, TRUE, 7, tree_test_data))
goto out;
+ if (!do_register (tree, path8, TRUE, 8, tree_test_data))
+ goto out;
+
+ _dbus_object_tree_unregister_and_unlock (tree, path0);
+ _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path0) == NULL);
+
+ _dbus_assert (!find_subtree (tree, path0, NULL));
+ _dbus_assert (find_subtree (tree, path1, NULL));
+ _dbus_assert (find_subtree (tree, path2, NULL));
+ _dbus_assert (find_subtree (tree, path3, NULL));
+ _dbus_assert (find_subtree (tree, path4, NULL));
+ _dbus_assert (find_subtree (tree, path5, NULL));
+ _dbus_assert (find_subtree (tree, path6, NULL));
+ _dbus_assert (find_subtree (tree, path7, NULL));
+ _dbus_assert (find_subtree (tree, path8, NULL));
_dbus_object_tree_unregister_and_unlock (tree, path1);
+ _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path1) == NULL);
+ _dbus_assert (!find_subtree (tree, path0, NULL));
_dbus_assert (!find_subtree (tree, path1, NULL));
_dbus_assert (find_subtree (tree, path2, NULL));
_dbus_assert (find_subtree (tree, path3, NULL));
_dbus_assert (find_subtree (tree, path8, NULL));
_dbus_object_tree_unregister_and_unlock (tree, path2);
+ _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path2) == NULL);
+ _dbus_assert (!find_subtree (tree, path0, NULL));
_dbus_assert (!find_subtree (tree, path1, NULL));
_dbus_assert (!find_subtree (tree, path2, NULL));
_dbus_assert (find_subtree (tree, path3, NULL));
_dbus_assert (find_subtree (tree, path8, NULL));
_dbus_object_tree_unregister_and_unlock (tree, path3);
+ _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path3) == NULL);
+ _dbus_assert (!find_subtree (tree, path0, NULL));
_dbus_assert (!find_subtree (tree, path1, NULL));
_dbus_assert (!find_subtree (tree, path2, NULL));
_dbus_assert (!find_subtree (tree, path3, NULL));
_dbus_assert (find_subtree (tree, path8, NULL));
_dbus_object_tree_unregister_and_unlock (tree, path4);
+ _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path4) == NULL);
+ _dbus_assert (!find_subtree (tree, path0, NULL));
_dbus_assert (!find_subtree (tree, path1, NULL));
_dbus_assert (!find_subtree (tree, path2, NULL));
_dbus_assert (!find_subtree (tree, path3, NULL));
_dbus_assert (find_subtree (tree, path8, NULL));
_dbus_object_tree_unregister_and_unlock (tree, path5);
+ _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path5) == NULL);
+ _dbus_assert (!find_subtree (tree, path0, NULL));
_dbus_assert (!find_subtree (tree, path1, NULL));
_dbus_assert (!find_subtree (tree, path2, NULL));
_dbus_assert (!find_subtree (tree, path3, NULL));
_dbus_assert (find_subtree (tree, path8, NULL));
_dbus_object_tree_unregister_and_unlock (tree, path6);
+ _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path6) == NULL);
+ _dbus_assert (!find_subtree (tree, path0, NULL));
_dbus_assert (!find_subtree (tree, path1, NULL));
_dbus_assert (!find_subtree (tree, path2, NULL));
_dbus_assert (!find_subtree (tree, path3, NULL));
_dbus_assert (find_subtree (tree, path8, NULL));
_dbus_object_tree_unregister_and_unlock (tree, path7);
+ _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path7) == NULL);
+ _dbus_assert (!find_subtree (tree, path0, NULL));
_dbus_assert (!find_subtree (tree, path1, NULL));
_dbus_assert (!find_subtree (tree, path2, NULL));
_dbus_assert (!find_subtree (tree, path3, NULL));
_dbus_assert (find_subtree (tree, path8, NULL));
_dbus_object_tree_unregister_and_unlock (tree, path8);
+ _dbus_assert (_dbus_object_tree_get_user_data_unlocked (tree, path8) == NULL);
+ _dbus_assert (!find_subtree (tree, path0, NULL));
_dbus_assert (!find_subtree (tree, path1, NULL));
_dbus_assert (!find_subtree (tree, path2, NULL));
_dbus_assert (!find_subtree (tree, path3, NULL));
}
/* Register it all again, and test dispatch */
-
- if (!do_register (tree, path1, FALSE, 0, tree_test_data))
+
+ if (!do_register (tree, path0, TRUE, 0, tree_test_data))
goto out;
- if (!do_register (tree, path2, TRUE, 1, tree_test_data))
+ if (!do_register (tree, path1, FALSE, 1, tree_test_data))
goto out;
- if (!do_register (tree, path3, TRUE, 2, tree_test_data))
+ if (!do_register (tree, path2, TRUE, 2, tree_test_data))
goto out;
- if (!do_register (tree, path4, TRUE, 3, tree_test_data))
+ if (!do_register (tree, path3, TRUE, 3, tree_test_data))
goto out;
- if (!do_register (tree, path5, TRUE, 4, tree_test_data))
+ if (!do_register (tree, path4, TRUE, 4, tree_test_data))
goto out;
- if (!do_register (tree, path6, FALSE, 5, tree_test_data))
+ if (!do_register (tree, path5, TRUE, 5, tree_test_data))
goto out;
- if (!do_register (tree, path7, TRUE, 6, tree_test_data))
+ if (!do_register (tree, path6, FALSE, 6, tree_test_data))
goto out;
- if (!do_register (tree, path8, TRUE, 7, tree_test_data))
+ if (!do_register (tree, path7, TRUE, 7, tree_test_data))
+ goto out;
+ if (!do_register (tree, path8, TRUE, 8, tree_test_data))
goto out;
#if 0
spew_tree (tree);
#endif
-
- if (!do_test_dispatch (tree, path1, 0, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
+
+ if (!do_test_dispatch (tree, path0, 0, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
+ goto out;
+ if (!do_test_dispatch (tree, path1, 1, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
goto out;
- if (!do_test_dispatch (tree, path2, 1, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
+ if (!do_test_dispatch (tree, path2, 2, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
goto out;
- if (!do_test_dispatch (tree, path3, 2, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
+ if (!do_test_dispatch (tree, path3, 3, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
goto out;
- if (!do_test_dispatch (tree, path4, 3, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
+ if (!do_test_dispatch (tree, path4, 4, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
goto out;
- if (!do_test_dispatch (tree, path5, 4, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
+ if (!do_test_dispatch (tree, path5, 5, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
goto out;
- if (!do_test_dispatch (tree, path6, 5, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
+ if (!do_test_dispatch (tree, path6, 6, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
goto out;
- if (!do_test_dispatch (tree, path7, 6, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
+ if (!do_test_dispatch (tree, path7, 7, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
goto out;
- if (!do_test_dispatch (tree, path8, 7, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
+ if (!do_test_dispatch (tree, path8, 8, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
goto out;
out:
return TRUE;
}
-#endif /* DBUS_BUILD_TESTS */
+#endif /* !DOXYGEN_SHOULD_SKIP_THIS */
+
+#endif /* DBUS_ENABLE_EMBEDDED_TESTS */