2003-09-06 Havoc Pennington <hp@pobox.com>
[platform/upstream/dbus.git] / dbus / dbus-object-tree.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-object-tree.c  DBusObjectTree (internals of DBusConnection)
3  *
4  * Copyright (C) 2003  Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
7  *
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.
12  *
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.
17  *
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
21  *
22  */
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 <string.h>
29 #include <stdlib.h>
30
31 /**
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
35  *
36  * Types and functions related to DBusObjectTree. These
37  * are all internal.
38  *
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.
44  *
45  * @{
46  */
47
48 typedef struct DBusObjectSubtree DBusObjectSubtree;
49
50 static DBusObjectSubtree* _dbus_object_subtree_new   (const char                  *name,
51                                                       const DBusObjectPathVTable  *vtable,
52                                                       void                        *user_data);
53 static void               _dbus_object_subtree_ref   (DBusObjectSubtree           *subtree);
54 static void               _dbus_object_subtree_unref (DBusObjectSubtree           *subtree);
55
56 struct DBusObjectTree
57 {
58   int                 refcount;
59   DBusConnection     *connection;
60
61   DBusObjectSubtree  *root;
62 };
63
64 struct DBusObjectSubtree
65 {
66   DBusAtomic                         refcount;
67   DBusObjectSubtree                 *parent;
68   DBusObjectPathUnregisterFunction   unregister_function;
69   DBusObjectPathMessageFunction      message_function;
70   void                              *user_data;
71   DBusObjectSubtree                **subtrees;
72   int                                n_subtrees;
73   unsigned int                       subtrees_sorted : 1;
74   unsigned int                       invoke_as_fallback : 1;
75   char                               name[1]; /**< Allocated as large as necessary */
76 };
77
78 DBusObjectTree*
79 _dbus_object_tree_new (DBusConnection *connection)
80 {
81   DBusObjectTree *tree;
82
83   /* the connection passed in here isn't fully constructed,
84    * so don't do anything more than store a pointer to
85    * it
86    */
87
88   tree = dbus_new0 (DBusObjectTree, 1);
89   if (tree == NULL)
90     goto oom;
91
92   tree->refcount = 1;
93   tree->connection = connection;
94   tree->root = _dbus_object_subtree_new ("/", NULL, NULL);
95   if (tree->root == NULL)
96     goto oom;
97   tree->root->invoke_as_fallback = TRUE;
98   
99   return tree;
100
101  oom:
102   if (tree)
103     {
104       dbus_free (tree);
105     }
106
107   return NULL;
108 }
109
110 void
111 _dbus_object_tree_ref (DBusObjectTree *tree)
112 {
113   _dbus_assert (tree->refcount > 0);
114
115   tree->refcount += 1;
116 }
117
118 void
119 _dbus_object_tree_unref (DBusObjectTree *tree)
120 {
121   _dbus_assert (tree->refcount > 0);
122
123   tree->refcount -= 1;
124
125   if (tree->refcount == 0)
126     {
127       _dbus_object_tree_free_all_unlocked (tree);
128
129       dbus_free (tree);
130     }
131 }
132
133 static int
134 subtree_cmp (DBusObjectSubtree *subtree_a,
135              DBusObjectSubtree *subtree_b)
136 {
137   return strcmp (subtree_a->name, subtree_b->name);
138 }
139
140 static int
141 subtree_qsort_cmp (const void *a,
142                    const void *b)
143 {
144   DBusObjectSubtree **subtree_a_p = (void*) a;
145   DBusObjectSubtree **subtree_b_p = (void*) b;
146
147   return subtree_cmp (*subtree_a_p, *subtree_b_p);
148 }
149
150 static void
151 ensure_sorted (DBusObjectSubtree *subtree)
152 {
153   if (subtree->subtrees && !subtree->subtrees_sorted)
154     {
155       qsort (subtree->subtrees,
156              subtree->n_subtrees,
157              sizeof (DBusObjectSubtree*),
158              subtree_qsort_cmp);
159       subtree->subtrees_sorted = TRUE;
160     }
161 }
162
163 #define VERBOSE_FIND 0
164
165 static DBusObjectSubtree*
166 find_subtree_recurse (DBusObjectSubtree  *subtree,
167                       const char        **path,
168                       dbus_bool_t         return_deepest_match,
169                       dbus_bool_t         create_if_not_found,
170                       int                *index_in_parent)
171 {
172   int i;
173
174   _dbus_assert (!(return_deepest_match && create_if_not_found));
175
176   if (path[0] == NULL)
177     {
178 #if VERBOSE_FIND
179       _dbus_verbose ("  path exhausted, returning %s\n",
180                      subtree->name);
181 #endif
182       return subtree;
183     }
184
185 #if VERBOSE_FIND
186   _dbus_verbose ("  searching children of %s for %s\n",
187                  subtree->name, path[0]);
188 #endif
189   
190   ensure_sorted (subtree);
191
192   /* FIXME we should do a binary search here instead
193    * of O(n)
194    */
195
196   i = 0;
197   while (i < subtree->n_subtrees)
198     {
199       int v;
200
201       v = strcmp (path[0], subtree->subtrees[i]->name);
202
203 #if VERBOSE_FIND
204       _dbus_verbose ("  %s cmp %s = %d\n",
205                      path[0], subtree->subtrees[i]->name,
206                      v);
207 #endif
208       
209       if (v == 0)
210         {
211           if (index_in_parent)
212             {
213 #if VERBOSE_FIND
214               _dbus_verbose ("  storing parent index %d\n", i);
215 #endif
216               *index_in_parent = i;
217             }
218
219           if (return_deepest_match)
220             {
221               DBusObjectSubtree *next;
222
223               next = find_subtree_recurse (subtree->subtrees[i],
224                                            &path[1], return_deepest_match,
225                                            create_if_not_found, index_in_parent);
226               if (next == NULL &&
227                   subtree->invoke_as_fallback)
228                 {
229 #if VERBOSE_FIND
230                   _dbus_verbose ("  no deeper match found, returning %s\n",
231                                  subtree->name);
232 #endif
233                   return subtree;
234                 }
235               else
236                 return next;
237             }
238           else
239             return find_subtree_recurse (subtree->subtrees[i],
240                                          &path[1], return_deepest_match,
241                                          create_if_not_found, index_in_parent);
242         }
243       else if (v < 0)
244         {
245           goto not_found;
246         }
247
248       ++i;
249     }
250
251  not_found:
252 #if VERBOSE_FIND
253   _dbus_verbose ("  no match found, current tree %s, create_if_not_found = %d\n",
254                  subtree->name, create_if_not_found);
255 #endif
256   
257   if (create_if_not_found)
258     {
259       DBusObjectSubtree* child;
260       DBusObjectSubtree **new_subtrees;
261       int new_n_subtrees;
262
263 #if VERBOSE_FIND
264       _dbus_verbose ("  creating subtree %s\n",
265                      path[0]);
266 #endif
267       
268       child = _dbus_object_subtree_new (path[0],
269                                         NULL, NULL);
270       if (child == NULL)
271         return NULL;
272
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)
278         {
279           child->unregister_function = NULL;
280           child->message_function = NULL;
281           _dbus_object_subtree_unref (child);
282           return FALSE;
283         }
284
285       new_subtrees[subtree->n_subtrees] = child;
286       if (index_in_parent)
287         *index_in_parent = subtree->n_subtrees;
288       subtree->subtrees_sorted = FALSE;
289       subtree->n_subtrees = new_n_subtrees;
290       subtree->subtrees = new_subtrees;
291
292       child->parent = subtree;
293
294       return find_subtree_recurse (child,
295                                    &path[1], return_deepest_match,
296                                    create_if_not_found, index_in_parent);
297     }
298   else
299     return (return_deepest_match && subtree->invoke_as_fallback) ? subtree : NULL;
300 }
301
302 static DBusObjectSubtree*
303 find_subtree (DBusObjectTree *tree,
304               const char    **path,
305               int            *index_in_parent)
306 {
307   DBusObjectSubtree *subtree;
308
309 #if VERBOSE_FIND
310   _dbus_verbose ("Looking for exact registered subtree\n");
311 #endif
312   
313   subtree = find_subtree_recurse (tree->root, path, FALSE, FALSE, index_in_parent);
314
315   if (subtree && subtree->message_function == NULL)
316     return NULL;
317   else
318     return subtree;
319 }
320
321 static DBusObjectSubtree*
322 find_handler (DBusObjectTree *tree,
323               const char    **path)
324 {
325 #if VERBOSE_FIND
326   _dbus_verbose ("Looking for deepest handler\n");
327 #endif
328   return find_subtree_recurse (tree->root, path, TRUE, FALSE, NULL);
329 }
330
331 static DBusObjectSubtree*
332 ensure_subtree (DBusObjectTree *tree,
333                 const char    **path)
334 {
335 #if VERBOSE_FIND
336   _dbus_verbose ("Ensuring subtree\n");
337 #endif
338   return find_subtree_recurse (tree->root, path, FALSE, TRUE, NULL);
339 }
340
341 /**
342  * Registers a new subtree in the global object tree.
343  *
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
350  */
351 dbus_bool_t
352 _dbus_object_tree_register (DBusObjectTree              *tree,
353                             dbus_bool_t                  fallback,
354                             const char                 **path,
355                             const DBusObjectPathVTable  *vtable,
356                             void                        *user_data)
357 {
358   DBusObjectSubtree  *subtree;
359
360   _dbus_assert (tree != NULL);
361   _dbus_assert (vtable->message_function != NULL);
362   _dbus_assert (path != NULL);
363
364   subtree = ensure_subtree (tree, path);
365   if (subtree == NULL)
366     return FALSE;
367
368 #ifndef DBUS_DISABLE_CHECKS
369   if (subtree->message_function != NULL)
370     {
371       _dbus_warn ("A handler is already registered for the path starting with path[0] = \"%s\"\n",
372                   path[0] ? path[0] : "null");
373       return FALSE;
374     }
375 #else
376   _dbus_assert (subtree->message_function == NULL);
377 #endif
378
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;
383   
384   return TRUE;
385 }
386
387 /**
388  * Unregisters an object subtree that was registered with the
389  * same path.
390  *
391  * @param tree the global object tree
392  * @param path path to the subtree (same as the one passed to _dbus_object_tree_register())
393  */
394 void
395 _dbus_object_tree_unregister_and_unlock (DBusObjectTree          *tree,
396                                          const char             **path)
397 {
398   int i;
399   DBusObjectSubtree *subtree;
400   DBusObjectPathUnregisterFunction unregister_function;
401   void *user_data;
402   DBusConnection *connection;
403
404   _dbus_assert (path != NULL);
405
406   subtree = find_subtree (tree, path, &i);
407
408 #ifndef DBUS_DISABLE_CHECKS
409   if (subtree == NULL)
410     {
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");
414       return;
415     }
416 #else
417   _dbus_assert (subtree != NULL);
418 #endif
419
420   _dbus_assert (subtree->parent == NULL ||
421                 (i >= 0 && subtree->parent->subtrees[i] == subtree));
422
423   subtree->message_function = NULL;
424
425   unregister_function = subtree->unregister_function;
426   user_data = subtree->user_data;
427
428   subtree->unregister_function = NULL;
429   subtree->user_data = NULL;
430
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)
434    */
435   if (subtree->parent && subtree->n_subtrees == 0)
436     {
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;
443
444       subtree->parent = NULL;
445
446       _dbus_object_subtree_unref (subtree);
447     }
448   subtree = NULL;
449
450   connection = tree->connection;
451
452   /* Unlock and call application code */
453 #ifdef DBUS_BUILD_TESTS
454   if (connection)
455 #endif
456     {
457       _dbus_connection_ref_unlocked (connection);
458       _dbus_connection_unlock (connection);
459     }
460
461   if (unregister_function)
462     (* unregister_function) (connection, user_data);
463
464 #ifdef DBUS_BUILD_TESTS
465   if (connection)
466 #endif
467     dbus_connection_unref (connection);
468 }
469
470 static void
471 free_subtree_recurse (DBusConnection    *connection,
472                       DBusObjectSubtree *subtree)
473 {
474   /* Delete them from the end, for slightly
475    * more robustness against odd reentrancy.
476    */
477   while (subtree->n_subtrees > 0)
478     {
479       DBusObjectSubtree *child;
480
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;
485
486       free_subtree_recurse (connection, child);
487     }
488
489   /* Call application code */
490   if (subtree->unregister_function)
491     {
492       (* subtree->unregister_function) (connection,
493                                         subtree->user_data);
494       subtree->message_function = NULL;
495       subtree->unregister_function = NULL;
496       subtree->user_data = NULL;
497     }
498
499   /* Now free ourselves */
500   _dbus_object_subtree_unref (subtree);
501 }
502
503 /**
504  * Free all the handlers in the tree. Lock on tree's connection
505  * must not be held.
506  *
507  * @param tree the object tree
508  */
509 void
510 _dbus_object_tree_free_all_unlocked (DBusObjectTree *tree)
511 {
512   if (tree->root)
513     free_subtree_recurse (tree->connection,
514                           tree->root);
515   tree->root = NULL;
516 }
517
518 /**
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.
524  *
525  * @todo thread problems
526  *
527  * @param tree the global object tree
528  * @param message the message to dispatch
529  * @returns whether message was handled successfully
530  */
531 DBusHandlerResult
532 _dbus_object_tree_dispatch_and_unlock (DBusObjectTree          *tree,
533                                        DBusMessage             *message)
534 {
535   char **path;
536   DBusList *list;
537   DBusList *link;
538   DBusHandlerResult result;
539   DBusObjectSubtree *subtree;
540   
541 #if 0
542   _dbus_verbose ("Dispatch of message by object path\n");
543 #endif
544   
545   path = NULL;
546   if (!dbus_message_get_path_decomposed (message, &path))
547     {
548       _dbus_verbose ("No memory to get decomposed path\n");
549       return DBUS_HANDLER_RESULT_NEED_MEMORY;
550     }
551
552   if (path == NULL)
553     {
554       _dbus_verbose ("No path field in message\n");
555       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
556     }
557   
558   /* Find the deepest path that covers the path in the message */
559   subtree = find_handler (tree, (const char**) path);
560   
561   /* Build a list of all paths that cover the path in the message */
562
563   list = NULL;
564
565   while (subtree != NULL)
566     {
567       if (subtree->message_function != NULL)
568         {
569           _dbus_object_subtree_ref (subtree);
570
571           /* run deepest paths first */
572           if (!_dbus_list_append (&list, subtree))
573             {
574               result = DBUS_HANDLER_RESULT_NEED_MEMORY;
575               _dbus_object_subtree_unref (subtree);
576               goto free_and_return;
577             }
578         }
579
580       subtree = subtree->parent;
581     }
582
583   _dbus_verbose ("%d handlers in the path tree for this message\n",
584                  _dbus_list_get_length (&list));
585
586   /* Invoke each handler in the list */
587
588   result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
589
590   link = _dbus_list_get_first_link (&list);
591   while (link != NULL)
592     {
593       DBusList *next = _dbus_list_get_next_link (&list, link);
594       subtree = link->data;
595
596       /* message_function is NULL if we're unregistered
597        * due to reentrancy
598        */
599       if (subtree->message_function)
600         {
601           DBusObjectPathMessageFunction message_function;
602           void *user_data;
603
604           message_function = subtree->message_function;
605           user_data = subtree->user_data;
606
607 #if 0
608           _dbus_verbose ("  (invoking a handler)\n");
609 #endif
610           
611 #ifdef DBUS_BUILD_TESTS
612           if (tree->connection)
613 #endif
614             _dbus_connection_unlock (tree->connection);
615
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.
619            */
620
621           result = (* message_function) (tree->connection,
622                                          message,
623                                          user_data);
624
625           if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
626             goto free_and_return;
627
628 #ifdef DBUS_BUILD_TESTS
629           if (tree->connection)
630 #endif
631             _dbus_connection_lock (tree->connection);
632         }
633
634       link = next;
635     }
636
637 #ifdef DBUS_BUILD_TESTS
638   if (tree->connection)
639 #endif
640     _dbus_connection_unlock (tree->connection);
641
642  free_and_return:
643   while (list != NULL)
644     {
645       link = _dbus_list_get_first_link (&list);
646       _dbus_object_subtree_unref (link->data);
647       _dbus_list_remove_link (&list, link);
648     }
649   dbus_free_string_array (path);
650
651   return result;
652 }
653
654 /**
655  * Allocates a subtree object.
656  *
657  * @param name name to duplicate.
658  * @returns newly-allocated subtree
659  */
660 static DBusObjectSubtree*
661 allocate_subtree_object (const char *name)
662 {
663   int len;
664   DBusObjectSubtree *subtree;
665   const size_t front_padding = _DBUS_STRUCT_OFFSET (DBusObjectSubtree, name);
666
667   _dbus_assert (name != NULL);
668
669   len = strlen (name);
670
671   subtree = dbus_malloc (front_padding + (len + 1));
672
673   if (subtree == NULL)
674     return NULL;
675
676   memcpy (subtree->name, name, len + 1);
677
678   return subtree;
679 }
680
681 static DBusObjectSubtree*
682 _dbus_object_subtree_new (const char                  *name,
683                           const DBusObjectPathVTable  *vtable,
684                           void                        *user_data)
685 {
686   DBusObjectSubtree *subtree;
687
688   subtree = allocate_subtree_object (name);
689   if (subtree == NULL)
690     goto oom;
691
692   _dbus_assert (name != NULL);
693
694   subtree->parent = NULL;
695
696   if (vtable)
697     {
698       subtree->message_function = vtable->message_function;
699       subtree->unregister_function = vtable->unregister_function;
700     }
701   else
702     {
703       subtree->message_function = NULL;
704       subtree->unregister_function = NULL;
705     }
706
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;
712   
713   return subtree;
714
715  oom:
716   if (subtree)
717     {
718       dbus_free (subtree);
719     }
720
721   return NULL;
722 }
723
724 static void
725 _dbus_object_subtree_ref (DBusObjectSubtree *subtree)
726 {
727   _dbus_assert (subtree->refcount.value > 0);
728   _dbus_atomic_inc (&subtree->refcount);
729 }
730
731 static void
732 _dbus_object_subtree_unref (DBusObjectSubtree *subtree)
733 {
734   _dbus_assert (subtree->refcount.value > 0);
735
736   if (_dbus_atomic_dec (&subtree->refcount) == 1)
737     {
738       _dbus_assert (subtree->unregister_function == NULL);
739       _dbus_assert (subtree->message_function == NULL);
740
741       dbus_free (subtree->subtrees);
742       dbus_free (subtree);
743     }
744 }
745
746 /** @} */
747
748 #ifdef DBUS_BUILD_TESTS
749 #include "dbus-test.h"
750 #include <stdio.h>
751
752 static char*
753 flatten_path (const char **path)
754 {
755   DBusString str;
756   int i;
757   char *s;
758
759   if (!_dbus_string_init (&str))
760     return NULL;
761
762   i = 0;
763   while (path[i])
764     {
765       if (!_dbus_string_append_byte (&str, '/'))
766         goto nomem;
767
768       if (!_dbus_string_append (&str, path[i]))
769         goto nomem;
770
771       ++i;
772     }
773
774   if (!_dbus_string_steal_data (&str, &s))
775     goto nomem;
776
777   _dbus_string_free (&str);
778
779   return s;
780
781  nomem:
782   _dbus_string_free (&str);
783   return NULL;
784 }
785
786 /* Returns TRUE if container is a parent of child
787  */
788 static dbus_bool_t
789 path_contains (const char **container,
790                const char **child)
791 {
792   int i;
793
794   i = 0;
795   while (child[i] != NULL)
796     {
797       int v;
798
799       if (container[i] == NULL)
800         return TRUE; /* container ran out, child continues;
801                       * thus the container is a parent of the
802                       * child.
803                       */
804
805       _dbus_assert (container[i] != NULL);
806       _dbus_assert (child[i] != NULL);
807
808       v = strcmp (container[i], child[i]);
809
810       if (v != 0)
811         return FALSE; /* they overlap until here and then are different,
812                        * not overlapping
813                        */
814
815       ++i;
816     }
817
818   /* Child ran out; if container also did, they are equal;
819    * otherwise, the child is a parent of the container.
820    */
821   if (container[i] == NULL)
822     return TRUE; /* equal is counted as containing */
823   else
824     return FALSE;
825 }
826
827 static void
828 spew_subtree_recurse (DBusObjectSubtree *subtree,
829                       int                indent)
830 {
831   int i;
832
833   i = 0;
834   while (i < indent)
835     {
836       _dbus_verbose (" ");
837       ++i;
838     }
839
840   _dbus_verbose ("%s (%d children)\n",
841                  subtree->name, subtree->n_subtrees);
842
843   i = 0;
844   while (i < subtree->n_subtrees)
845     {
846       spew_subtree_recurse (subtree->subtrees[i], indent + 2);
847
848       ++i;
849     }
850 }
851
852 static void
853 spew_tree (DBusObjectTree *tree)
854 {
855   spew_subtree_recurse (tree->root, 0);
856 }
857
858 typedef struct
859 {
860   const char **path;
861   dbus_bool_t message_handled;
862   dbus_bool_t handler_unregistered;
863
864 } TreeTestData;
865
866
867 static void
868 test_unregister_function (DBusConnection  *connection,
869                           void            *user_data)
870 {
871   TreeTestData *ttd = user_data;
872
873   ttd->handler_unregistered = TRUE;
874 }
875
876 static DBusHandlerResult
877 test_message_function (DBusConnection  *connection,
878                        DBusMessage     *message,
879                        void            *user_data)
880 {
881   TreeTestData *ttd = user_data;
882
883   ttd->message_handled = TRUE;
884
885   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
886 }
887
888 static dbus_bool_t
889 do_register (DBusObjectTree *tree,
890              const char    **path,
891              int             i,
892              TreeTestData   *tree_test_data)
893 {
894   DBusObjectPathVTable vtable = { test_unregister_function,
895                                   test_message_function, NULL };
896
897   tree_test_data[i].message_handled = FALSE;
898   tree_test_data[i].handler_unregistered = FALSE;
899   tree_test_data[i].path = path;
900
901   if (!_dbus_object_tree_register (tree, TRUE, path,
902                                    &vtable,
903                                    &tree_test_data[i]))
904     return FALSE;
905
906   return TRUE;
907 }
908
909 static dbus_bool_t
910 do_test_dispatch (DBusObjectTree *tree,
911                   const char    **path,
912                   int             i,
913                   TreeTestData   *tree_test_data,
914                   int             n_test_data)
915 {
916   DBusMessage *message;
917   int j;
918   DBusHandlerResult result;
919   char *flat;
920
921   message = NULL;
922   
923   flat = flatten_path (path);
924   if (flat == NULL)
925     goto oom;
926
927   message = dbus_message_new_method_call (NULL,
928                                           flat,
929                                           "org.freedesktop.TestInterface",
930                                           "Foo");
931   dbus_free (flat);
932   if (message == NULL)
933     goto oom;
934
935   j = 0;
936   while (j < n_test_data)
937     {
938       tree_test_data[j].message_handled = FALSE;
939       ++j;
940     }
941
942   result = _dbus_object_tree_dispatch_and_unlock (tree, message);
943   if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
944     goto oom;
945
946   _dbus_assert (tree_test_data[i].message_handled);
947
948   j = 0;
949   while (j < n_test_data)
950     {
951       if (tree_test_data[j].message_handled)
952         _dbus_assert (path_contains (tree_test_data[j].path,
953                                      path));
954       else
955         _dbus_assert (!path_contains (tree_test_data[j].path,
956                                       path));
957
958       ++j;
959     }
960
961   dbus_message_unref (message);
962
963   return TRUE;
964
965  oom:
966   if (message)
967     dbus_message_unref (message);
968   return FALSE;
969 }
970
971 static dbus_bool_t
972 object_tree_test_iteration (void *data)
973 {
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];
984   int i;
985
986   tree = NULL;
987
988   tree = _dbus_object_tree_new (NULL);
989   if (tree == NULL)
990     goto out;
991
992   if (!do_register (tree, path1, 0, tree_test_data))
993     goto out;
994
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));
1003
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);
1012
1013   if (!do_register (tree, path2, 1, tree_test_data))
1014     goto out;
1015
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));
1024
1025   if (!do_register (tree, path3, 2, tree_test_data))
1026     goto out;
1027
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));
1036   
1037   if (!do_register (tree, path4, 3, tree_test_data))
1038     goto out;
1039
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));
1048   
1049   if (!do_register (tree, path5, 4, tree_test_data))
1050     goto out;
1051
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));
1060   
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);
1069
1070   if (!do_register (tree, path6, 5, tree_test_data))
1071     goto out;
1072
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));
1081
1082   if (!do_register (tree, path7, 6, tree_test_data))
1083     goto out;
1084
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));
1093
1094   if (!do_register (tree, path8, 7, tree_test_data))
1095     goto out;
1096
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));
1105   
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);
1114   
1115   /* Check that destroying tree calls unregister funcs */
1116   _dbus_object_tree_unref (tree);
1117
1118   i = 0;
1119   while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1120     {
1121       _dbus_assert (tree_test_data[i].handler_unregistered);
1122       _dbus_assert (!tree_test_data[i].message_handled);
1123       ++i;
1124     }
1125
1126   /* Now start again and try the individual unregister function */
1127   tree = _dbus_object_tree_new (NULL);
1128   if (tree == NULL)
1129     goto out;
1130
1131   if (!do_register (tree, path1, 0, tree_test_data))
1132     goto out;
1133   if (!do_register (tree, path2, 1, tree_test_data))
1134     goto out;
1135   if (!do_register (tree, path3, 2, tree_test_data))
1136     goto out;
1137   if (!do_register (tree, path4, 3, tree_test_data))
1138     goto out;
1139   if (!do_register (tree, path5, 4, tree_test_data))
1140     goto out;
1141   if (!do_register (tree, path6, 5, tree_test_data))
1142     goto out;
1143   if (!do_register (tree, path7, 6, tree_test_data))
1144     goto out;
1145   if (!do_register (tree, path8, 7, tree_test_data))
1146     goto out;
1147   
1148   _dbus_object_tree_unregister_and_unlock (tree, path1);
1149
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));
1158
1159   _dbus_object_tree_unregister_and_unlock (tree, path2);
1160
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));
1169   
1170   _dbus_object_tree_unregister_and_unlock (tree, path3);
1171
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));
1180   
1181   _dbus_object_tree_unregister_and_unlock (tree, path4);
1182
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));
1191   
1192   _dbus_object_tree_unregister_and_unlock (tree, path5);
1193
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));
1202   
1203   _dbus_object_tree_unregister_and_unlock (tree, path6);
1204
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));
1213
1214   _dbus_object_tree_unregister_and_unlock (tree, path7);
1215
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));
1224
1225   _dbus_object_tree_unregister_and_unlock (tree, path8);
1226
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));
1235   
1236   i = 0;
1237   while (i < (int) _DBUS_N_ELEMENTS (tree_test_data))
1238     {
1239       _dbus_assert (tree_test_data[i].handler_unregistered);
1240       _dbus_assert (!tree_test_data[i].message_handled);
1241       ++i;
1242     }
1243
1244   /* Register it all again, and test dispatch */
1245
1246   if (!do_register (tree, path1, 0, tree_test_data))
1247     goto out;
1248   if (!do_register (tree, path2, 1, tree_test_data))
1249     goto out;
1250   if (!do_register (tree, path3, 2, tree_test_data))
1251     goto out;
1252   if (!do_register (tree, path4, 3, tree_test_data))
1253     goto out;
1254   if (!do_register (tree, path5, 4, tree_test_data))
1255     goto out;
1256   if (!do_register (tree, path6, 5, tree_test_data))
1257     goto out;
1258   if (!do_register (tree, path7, 6, tree_test_data))
1259     goto out;
1260   if (!do_register (tree, path8, 7, tree_test_data))
1261     goto out;
1262
1263 #if 0
1264   spew_tree (tree);
1265 #endif
1266   
1267   if (!do_test_dispatch (tree, path1, 0, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1268     goto out;
1269   if (!do_test_dispatch (tree, path2, 1, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1270     goto out;
1271   if (!do_test_dispatch (tree, path3, 2, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1272     goto out;
1273   if (!do_test_dispatch (tree, path4, 3, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1274     goto out;
1275   if (!do_test_dispatch (tree, path5, 4, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1276     goto out;
1277   if (!do_test_dispatch (tree, path6, 5, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1278     goto out;
1279   if (!do_test_dispatch (tree, path7, 6, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1280     goto out;
1281   if (!do_test_dispatch (tree, path8, 7, tree_test_data, _DBUS_N_ELEMENTS (tree_test_data)))
1282     goto out;
1283   
1284  out:
1285   if (tree)
1286     {
1287       /* test ref */
1288       _dbus_object_tree_ref (tree);
1289       _dbus_object_tree_unref (tree);
1290       _dbus_object_tree_unref (tree);
1291     }
1292
1293   return TRUE;
1294 }
1295
1296 /**
1297  * @ingroup DBusObjectTree
1298  * Unit test for DBusObjectTree
1299  * @returns #TRUE on success.
1300  */
1301 dbus_bool_t
1302 _dbus_object_tree_test (void)
1303 {
1304   _dbus_test_oom_handling ("object tree",
1305                            object_tree_test_iteration,
1306                            NULL);
1307
1308   return TRUE;
1309 }
1310
1311 #endif /* DBUS_BUILD_TESTS */