GDBus: Fix bug in child enumeration
[platform/upstream/glib.git] / gio / tests / gdbus-export.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include <gio/gio.h>
24 #include <unistd.h>
25 #include <string.h>
26
27 #include "gdbus-tests.h"
28
29 /* all tests rely on a shared mainloop */
30 static GMainLoop *loop = NULL;
31
32 static GDBusConnection *c = NULL;
33
34 /* ---------------------------------------------------------------------------------------------------- */
35 /* Test that we can export objects, the hierarchy is correct and the right handlers are invoked */
36 /* ---------------------------------------------------------------------------------------------------- */
37
38 static const GDBusArgInfo foo_method1_in_args =
39 {
40   -1,
41   "an_input_string",
42   "s",
43   NULL
44 };
45 static const GDBusArgInfo * const foo_method1_in_arg_pointers[] = {&foo_method1_in_args, NULL};
46
47 static const GDBusArgInfo foo_method1_out_args =
48 {
49   -1,
50   "an_output_string",
51   "s",
52   NULL
53 };
54 static const GDBusArgInfo * const foo_method1_out_arg_pointers[] = {&foo_method1_out_args, NULL};
55
56 static const GDBusMethodInfo foo_method_info_method1 =
57 {
58   -1,
59   "Method1",
60   (GDBusArgInfo **) &foo_method1_in_arg_pointers,
61   (GDBusArgInfo **) &foo_method1_out_arg_pointers,
62   NULL
63 };
64 static const GDBusMethodInfo foo_method_info_method2 =
65 {
66   -1,
67   "Method2",
68   NULL,
69   NULL,
70   NULL
71 };
72 static const GDBusMethodInfo * const foo_method_info_pointers[] = {&foo_method_info_method1, &foo_method_info_method2, NULL};
73
74 static const GDBusSignalInfo foo_signal_info =
75 {
76   -1,
77   "SignalAlpha",
78   NULL,
79   NULL
80 };
81 static const GDBusSignalInfo * const foo_signal_info_pointers[] = {&foo_signal_info, NULL};
82
83 static const GDBusPropertyInfo foo_property_info[] =
84 {
85   {
86     -1,
87     "PropertyUno",
88     "s",
89     G_DBUS_PROPERTY_INFO_FLAGS_READABLE | G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE,
90     NULL
91   },
92   {
93     -1,
94     "NotWritable",
95     "s",
96     G_DBUS_PROPERTY_INFO_FLAGS_READABLE,
97     NULL
98   },
99   {
100     -1,
101     "NotReadable",
102     "s",
103     G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE,
104     NULL
105   }
106 };
107 static const GDBusPropertyInfo * const foo_property_info_pointers[] =
108 {
109   &foo_property_info[0],
110   &foo_property_info[1],
111   &foo_property_info[2],
112   NULL
113 };
114
115 static const GDBusInterfaceInfo foo_interface_info =
116 {
117   -1,
118   "org.example.Foo",
119   (GDBusMethodInfo **) &foo_method_info_pointers,
120   (GDBusSignalInfo **) &foo_signal_info_pointers,
121   (GDBusPropertyInfo **)&foo_property_info_pointers,
122   NULL,
123 };
124
125 static void
126 foo_method_call (GDBusConnection       *connection,
127                  const gchar           *sender,
128                  const gchar           *object_path,
129                  const gchar           *interface_name,
130                  const gchar           *method_name,
131                  GVariant              *parameters,
132                  GDBusMethodInvocation *invocation,
133                  gpointer               user_data)
134 {
135   if (g_strcmp0 (method_name, "Method1") == 0)
136     {
137       const gchar *input;
138       gchar *output;
139       g_variant_get (parameters, "(&s)", &input);
140       output = g_strdup_printf ("You passed the string `%s'. Jolly good!", input);
141       g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", output));
142       g_free (output);
143     }
144   else
145     {
146       g_dbus_method_invocation_return_dbus_error (invocation,
147                                                   "org.example.SomeError",
148                                                   "How do you like them apples, buddy!");
149     }
150 }
151
152 static GVariant *
153 foo_get_property (GDBusConnection       *connection,
154                   const gchar           *sender,
155                   const gchar           *object_path,
156                   const gchar           *interface_name,
157                   const gchar           *property_name,
158                   GError               **error,
159                   gpointer               user_data)
160 {
161   GVariant *ret;
162   gchar *s;
163   s = g_strdup_printf ("Property `%s' Is What It Is!", property_name);
164   ret = g_variant_new_string (s);
165   g_free (s);
166   return ret;
167 }
168
169 static gboolean
170 foo_set_property (GDBusConnection       *connection,
171                   const gchar           *sender,
172                   const gchar           *object_path,
173                   const gchar           *interface_name,
174                   const gchar           *property_name,
175                   GVariant              *value,
176                   GError               **error,
177                   gpointer               user_data)
178 {
179   gchar *s;
180   s = g_variant_print (value, TRUE);
181   g_set_error (error,
182                G_DBUS_ERROR,
183                G_DBUS_ERROR_SPAWN_FILE_INVALID,
184                "Returning some error instead of writing the value `%s' to the property `%s'",
185                property_name, s);
186   g_free (s);
187   return FALSE;
188 }
189
190 static const GDBusInterfaceVTable foo_vtable =
191 {
192   foo_method_call,
193   foo_get_property,
194   foo_set_property
195 };
196
197 /* -------------------- */
198
199 static const GDBusMethodInfo bar_method_info[] =
200 {
201   {
202     -1,
203     "MethodA",
204     NULL,
205     NULL,
206     NULL
207   },
208   {
209     -1,
210     "MethodB",
211     NULL,
212     NULL,
213     NULL
214   }
215 };
216 static const GDBusMethodInfo * const bar_method_info_pointers[] = {&bar_method_info[0], &bar_method_info[1], NULL};
217
218 static const GDBusSignalInfo bar_signal_info[] =
219 {
220   {
221     -1,
222     "SignalMars",
223     NULL,
224     NULL
225   }
226 };
227 static const GDBusSignalInfo * const bar_signal_info_pointers[] = {&bar_signal_info[0], NULL};
228
229 static const GDBusPropertyInfo bar_property_info[] =
230 {
231   {
232     -1,
233     "PropertyDuo",
234     "s",
235     G_DBUS_PROPERTY_INFO_FLAGS_READABLE,
236     NULL
237   }
238 };
239 static const GDBusPropertyInfo * const bar_property_info_pointers[] = {&bar_property_info[0], NULL};
240
241 static const GDBusInterfaceInfo bar_interface_info =
242 {
243   -1,
244   "org.example.Bar",
245   (GDBusMethodInfo **) bar_method_info_pointers,
246   (GDBusSignalInfo **) bar_signal_info_pointers,
247   (GDBusPropertyInfo **) bar_property_info_pointers,
248   NULL,
249 };
250
251 /* -------------------- */
252
253 static const GDBusMethodInfo dyna_method_info[] =
254 {
255   {
256     -1,
257     "DynaCyber",
258     NULL,
259     NULL,
260     NULL
261   }
262 };
263 static const GDBusMethodInfo * const dyna_method_info_pointers[] = {&dyna_method_info[0], NULL};
264
265 static const GDBusInterfaceInfo dyna_interface_info =
266 {
267   -1,
268   "org.example.Dyna",
269   (GDBusMethodInfo **) dyna_method_info_pointers,
270   NULL, /* no signals */
271   NULL, /* no properties */
272   NULL,
273 };
274
275 static void
276 dyna_cyber (GDBusConnection *connection,
277             const gchar *sender,
278             const gchar *object_path,
279             const gchar *interface_name,
280             const gchar *method_name,
281             GVariant *parameters,
282             GDBusMethodInvocation *invocation,
283             gpointer user_data)
284 {
285   GPtrArray *data = user_data;
286   gchar *node_name;
287   guint n;
288
289   node_name = strrchr (object_path, '/') + 1;
290
291   /* Add new node if it is not already known */
292   for (n = 0; n < data->len ; n++)
293     {
294       if (g_strcmp0 (g_ptr_array_index (data, n), node_name) == 0)
295         goto out;
296     }
297   g_ptr_array_add (data, g_strdup(node_name));
298
299   out:
300     g_dbus_method_invocation_return_value (invocation, NULL);
301 }
302
303 static const GDBusInterfaceVTable dyna_interface_vtable =
304 {
305   dyna_cyber,
306   NULL,
307   NULL
308 };
309
310 /* ---------------------------------------------------------------------------------------------------- */
311
312 static void
313 introspect_callback (GDBusProxy   *proxy,
314                      GAsyncResult *res,
315                      gpointer      user_data)
316 {
317   gchar **xml_data = user_data;
318   GVariant *result;
319   GError *error;
320
321   error = NULL;
322   result = g_dbus_proxy_call_finish (proxy,
323                                               res,
324                                               &error);
325   g_assert_no_error (error);
326   g_assert (result != NULL);
327   g_variant_get (result, "(s)", xml_data);
328   g_variant_unref (result);
329
330   g_main_loop_quit (loop);
331 }
332
333 static gchar **
334 get_nodes_at (GDBusConnection  *c,
335               const gchar      *object_path)
336 {
337   GError *error;
338   GDBusProxy *proxy;
339   gchar *xml_data;
340   GPtrArray *p;
341   GDBusNodeInfo *node_info;
342   guint n;
343
344   error = NULL;
345   proxy = g_dbus_proxy_new_sync (c,
346                                  G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
347                                  G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
348                                  NULL,
349                                  g_dbus_connection_get_unique_name (c),
350                                  object_path,
351                                  "org.freedesktop.DBus.Introspectable",
352                                  NULL,
353                                  &error);
354   g_assert_no_error (error);
355   g_assert (proxy != NULL);
356
357   /* do this async to avoid libdbus-1 deadlocks */
358   xml_data = NULL;
359   g_dbus_proxy_call (proxy,
360                      "Introspect",
361                      NULL,
362                      G_DBUS_CALL_FLAGS_NONE,
363                      -1,
364                      NULL,
365                      (GAsyncReadyCallback) introspect_callback,
366                      &xml_data);
367   g_main_loop_run (loop);
368   g_assert (xml_data != NULL);
369
370   node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
371   g_assert_no_error (error);
372   g_assert (node_info != NULL);
373
374   p = g_ptr_array_new ();
375   for (n = 0; node_info->nodes != NULL && node_info->nodes[n] != NULL; n++)
376     {
377       const GDBusNodeInfo *sub_node_info = node_info->nodes[n];
378       g_ptr_array_add (p, g_strdup (sub_node_info->path));
379     }
380   g_ptr_array_add (p, NULL);
381
382   g_object_unref (proxy);
383   g_free (xml_data);
384   g_dbus_node_info_unref (node_info);
385
386   return (gchar **) g_ptr_array_free (p, FALSE);
387 }
388
389 static gboolean
390 has_interface (GDBusConnection *c,
391                const gchar     *object_path,
392                const gchar     *interface_name)
393 {
394   GError *error;
395   GDBusProxy *proxy;
396   gchar *xml_data;
397   GDBusNodeInfo *node_info;
398   gboolean ret;
399
400   error = NULL;
401   proxy = g_dbus_proxy_new_sync (c,
402                                  G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
403                                  G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
404                                  NULL,
405                                  g_dbus_connection_get_unique_name (c),
406                                  object_path,
407                                  "org.freedesktop.DBus.Introspectable",
408                                  NULL,
409                                  &error);
410   g_assert_no_error (error);
411   g_assert (proxy != NULL);
412
413   /* do this async to avoid libdbus-1 deadlocks */
414   xml_data = NULL;
415   g_dbus_proxy_call (proxy,
416                      "Introspect",
417                      NULL,
418                      G_DBUS_CALL_FLAGS_NONE,
419                      -1,
420                      NULL,
421                      (GAsyncReadyCallback) introspect_callback,
422                      &xml_data);
423   g_main_loop_run (loop);
424   g_assert (xml_data != NULL);
425
426   node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
427   g_assert_no_error (error);
428   g_assert (node_info != NULL);
429
430   ret = (g_dbus_node_info_lookup_interface (node_info, interface_name) != NULL);
431
432   g_object_unref (proxy);
433   g_free (xml_data);
434   g_dbus_node_info_unref (node_info);
435
436   return ret;
437 }
438
439 static guint
440 count_interfaces (GDBusConnection *c,
441                   const gchar     *object_path)
442 {
443   GError *error;
444   GDBusProxy *proxy;
445   gchar *xml_data;
446   GDBusNodeInfo *node_info;
447   guint ret;
448
449   error = NULL;
450   proxy = g_dbus_proxy_new_sync (c,
451                                  G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
452                                  G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
453                                  NULL,
454                                  g_dbus_connection_get_unique_name (c),
455                                  object_path,
456                                  "org.freedesktop.DBus.Introspectable",
457                                  NULL,
458                                  &error);
459   g_assert_no_error (error);
460   g_assert (proxy != NULL);
461
462   /* do this async to avoid libdbus-1 deadlocks */
463   xml_data = NULL;
464   g_dbus_proxy_call (proxy,
465                      "Introspect",
466                      NULL,
467                      G_DBUS_CALL_FLAGS_NONE,
468                      -1,
469                      NULL,
470                      (GAsyncReadyCallback) introspect_callback,
471                      &xml_data);
472   g_main_loop_run (loop);
473   g_assert (xml_data != NULL);
474
475   node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
476   g_assert_no_error (error);
477   g_assert (node_info != NULL);
478
479   ret = 0;
480   while (node_info->interfaces != NULL && node_info->interfaces[ret] != NULL)
481     ret++;
482
483   g_object_unref (proxy);
484   g_free (xml_data);
485   g_dbus_node_info_unref (node_info);
486
487   return ret;
488 }
489
490 static void
491 dyna_create_callback (GDBusProxy   *proxy,
492                       GAsyncResult  *res,
493                       gpointer      user_data)
494 {
495   GVariant *result;
496   GError *error;
497
498   error = NULL;
499   result = g_dbus_proxy_call_finish (proxy,
500                                      res,
501                                      &error);
502   g_assert_no_error (error);
503   g_assert (result != NULL);
504   g_variant_unref (result);
505
506   g_main_loop_quit (loop);
507 }
508
509 /* Dynamically create @object_name under /foo/dyna */
510 static void
511 dyna_create (GDBusConnection *c,
512              const gchar     *object_name)
513 {
514   GError *error;
515   GDBusProxy *proxy;
516   gchar *object_path;
517
518   object_path = g_strconcat ("/foo/dyna/", object_name, NULL);
519
520   error = NULL;
521   proxy = g_dbus_proxy_new_sync (c,
522                                  G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
523                                  G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
524                                  NULL,
525                                  g_dbus_connection_get_unique_name (c),
526                                  object_path,
527                                  "org.example.Dyna",
528                                  NULL,
529                                  &error);
530   g_assert_no_error (error);
531   g_assert (proxy != NULL);
532
533   /* do this async to avoid libdbus-1 deadlocks */
534   g_dbus_proxy_call (proxy,
535                      "DynaCyber",
536                      g_variant_new ("()"),
537                      G_DBUS_CALL_FLAGS_NONE,
538                      -1,
539                      NULL,
540                      (GAsyncReadyCallback) dyna_create_callback,
541                      NULL);
542   g_main_loop_run (loop);
543
544   g_assert_no_error (error);
545
546   g_object_unref (proxy);
547   g_free (object_path);
548
549   return;
550 }
551
552 typedef struct
553 {
554   guint num_unregistered_calls;
555   guint num_unregistered_subtree_calls;
556   guint num_subtree_nodes;
557 } ObjectRegistrationData;
558
559 static void
560 on_object_unregistered (gpointer user_data)
561 {
562   ObjectRegistrationData *data = user_data;
563
564   data->num_unregistered_calls++;
565 }
566
567 static void
568 on_subtree_unregistered (gpointer user_data)
569 {
570   ObjectRegistrationData *data = user_data;
571
572   data->num_unregistered_subtree_calls++;
573 }
574
575 static gboolean
576 _g_strv_has_string (const gchar* const * haystack,
577                     const gchar *needle)
578 {
579   guint n;
580
581   for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
582     {
583       if (g_strcmp0 (haystack[n], needle) == 0)
584         return TRUE;
585     }
586   return FALSE;
587 }
588
589 /* -------------------- */
590
591 static gchar **
592 subtree_enumerate (GDBusConnection       *connection,
593                    const gchar           *sender,
594                    const gchar           *object_path,
595                    gpointer               user_data)
596 {
597   ObjectRegistrationData *data = user_data;
598   GPtrArray *p;
599   gchar **nodes;
600   guint n;
601
602   p = g_ptr_array_new ();
603
604   for (n = 0; n < data->num_subtree_nodes; n++)
605     {
606       g_ptr_array_add (p, g_strdup_printf ("vp%d", n));
607       g_ptr_array_add (p, g_strdup_printf ("evp%d", n));
608     }
609   g_ptr_array_add (p, NULL);
610
611   nodes = (gchar **) g_ptr_array_free (p, FALSE);
612
613   return nodes;
614 }
615
616 /* Only allows certain objects, and aborts on unknowns */
617 static GPtrArray *
618 subtree_introspect (GDBusConnection       *connection,
619                     const gchar           *sender,
620                     const gchar           *object_path,
621                     const gchar           *node,
622                     gpointer               user_data)
623 {
624   GPtrArray *interfaces;
625
626   /* VPs implement the Foo interface, EVPs implement the Bar interface. The root
627    * does not implement any interfaces
628    */
629   interfaces = g_ptr_array_new ();
630   if (g_str_has_prefix (node, "vp"))
631     {
632       g_ptr_array_add (interfaces, (gpointer) &foo_interface_info);
633     }
634   else if (g_str_has_prefix (node, "evp"))
635     {
636       g_ptr_array_add (interfaces, (gpointer) &bar_interface_info);
637     }
638   else if (g_strcmp0 (node, "/") == 0)
639     {
640       /* do nothing */
641     }
642   else
643     {
644       g_assert_not_reached ();
645     }
646
647   return interfaces;
648 }
649
650 static const GDBusInterfaceVTable *
651 subtree_dispatch (GDBusConnection             *connection,
652                   const gchar                 *sender,
653                   const gchar                 *object_path,
654                   const gchar                 *interface_name,
655                   const gchar                 *node,
656                   gpointer                    *out_user_data,
657                   gpointer                     user_data)
658 {
659   if (g_strcmp0 (interface_name, "org.example.Foo") == 0)
660     return &foo_vtable;
661   else
662     return NULL;
663 }
664
665 static const GDBusSubtreeVTable subtree_vtable =
666 {
667   subtree_enumerate,
668   subtree_introspect,
669   subtree_dispatch
670 };
671
672 /* -------------------- */
673
674 static gchar **
675 dynamic_subtree_enumerate (GDBusConnection       *connection,
676                            const gchar           *sender,
677                            const gchar           *object_path,
678                            gpointer               user_data)
679 {
680   GPtrArray *data = user_data;
681   gchar **nodes = g_new (gchar*, data->len + 1);
682   guint n;
683
684   for (n = 0; n < data->len; n++)
685     {
686       nodes[n] = g_strdup (g_ptr_array_index (data, n));
687     }
688   nodes[data->len] = NULL;
689
690   return nodes;
691 }
692
693 /* Allow all objects to be introspected */
694 static GPtrArray *
695 dynamic_subtree_introspect (GDBusConnection       *connection,
696                             const gchar           *sender,
697                             const gchar           *object_path,
698                             const gchar           *node,
699                             gpointer               user_data)
700 {
701   GPtrArray *interfaces;
702
703   /* All nodes (including the root node) implements the Dyna interface */
704   interfaces = g_ptr_array_new ();
705   g_ptr_array_add (interfaces, (gpointer) &dyna_interface_info);
706
707   return interfaces;
708 }
709
710 static const GDBusInterfaceVTable *
711 dynamic_subtree_dispatch (GDBusConnection             *connection,
712                           const gchar                 *sender,
713                           const gchar                 *object_path,
714                           const gchar                 *interface_name,
715                           const gchar                 *node,
716                           gpointer                    *out_user_data,
717                           gpointer                     user_data)
718 {
719   *out_user_data = user_data;
720   return &dyna_interface_vtable;
721 }
722
723 static const GDBusSubtreeVTable dynamic_subtree_vtable =
724 {
725   dynamic_subtree_enumerate,
726   dynamic_subtree_introspect,
727   dynamic_subtree_dispatch
728 };
729
730 /* -------------------- */
731
732 static gpointer
733 test_dispatch_thread_func (gpointer user_data)
734 {
735   const gchar *object_path = user_data;
736   GDBusProxy *foo_proxy;
737   GVariant *value;
738   GVariant *inner;
739   GError *error;
740   gchar *s;
741   const gchar *value_str;
742
743   foo_proxy = g_dbus_proxy_new_sync (c,
744                                      G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS |
745                                      G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
746                                      NULL,
747                                      g_dbus_connection_get_unique_name (c),
748                                      object_path,
749                                      "org.example.Foo",
750                                      NULL,
751                                      &error);
752   g_assert (foo_proxy != NULL);
753
754   /* generic interfaces */
755   error = NULL;
756   value = g_dbus_proxy_call_sync (foo_proxy,
757                                   "org.freedesktop.DBus.Peer.Ping",
758                                   NULL,
759                                   G_DBUS_CALL_FLAGS_NONE,
760                                   -1,
761                                   NULL,
762                                   &error);
763   g_assert_no_error (error);
764   g_assert (value != NULL);
765   g_variant_unref (value);
766
767   /* user methods */
768   error = NULL;
769   value = g_dbus_proxy_call_sync (foo_proxy,
770                                   "Method1",
771                                   g_variant_new ("(s)", "winwinwin"),
772                                   G_DBUS_CALL_FLAGS_NONE,
773                                   -1,
774                                   NULL,
775                                   &error);
776   g_assert_no_error (error);
777   g_assert (value != NULL);
778   g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("(s)")));
779   g_variant_get (value, "(&s)", &value_str);
780   g_assert_cmpstr (value_str, ==, "You passed the string `winwinwin'. Jolly good!");
781   g_variant_unref (value);
782
783   error = NULL;
784   value = g_dbus_proxy_call_sync (foo_proxy,
785                                   "Method2",
786                                   NULL,
787                                   G_DBUS_CALL_FLAGS_NONE,
788                                   -1,
789                                   NULL,
790                                   &error);
791   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
792   g_assert_cmpstr (error->message, ==, "GDBus.Error:org.example.SomeError: How do you like them apples, buddy!");
793   g_error_free (error);
794   g_assert (value == NULL);
795
796   error = NULL;
797   value = g_dbus_proxy_call_sync (foo_proxy,
798                                   "Method2",
799                                   g_variant_new ("(s)", "failfailfail"),
800                                   G_DBUS_CALL_FLAGS_NONE,
801                                   -1,
802                                   NULL,
803                                   &error);
804   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
805   g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Type of message, `(s)', does not match expected type `()'");
806   g_error_free (error);
807   g_assert (value == NULL);
808
809   error = NULL;
810   value = g_dbus_proxy_call_sync (foo_proxy,
811                                   "NonExistantMethod",
812                                   NULL,
813                                   G_DBUS_CALL_FLAGS_NONE,
814                                   -1,
815                                   NULL,
816                                   &error);
817   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
818   g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod: No such method `NonExistantMethod'");
819   g_error_free (error);
820   g_assert (value == NULL);
821
822   error = NULL;
823   value = g_dbus_proxy_call_sync (foo_proxy,
824                                   "org.example.FooXYZ.NonExistant",
825                                   NULL,
826                                   G_DBUS_CALL_FLAGS_NONE,
827                                   -1,
828                                   NULL,
829                                   &error);
830   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
831   g_error_free (error);
832   g_assert (value == NULL);
833
834   /* user properties */
835   error = NULL;
836   value = g_dbus_proxy_call_sync (foo_proxy,
837                                   "org.freedesktop.DBus.Properties.Get",
838                                   g_variant_new ("(ss)",
839                                                  "org.example.Foo",
840                                                  "PropertyUno"),
841                                   G_DBUS_CALL_FLAGS_NONE,
842                                   -1,
843                                   NULL,
844                                   &error);
845   g_assert_no_error (error);
846   g_assert (value != NULL);
847   g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("(v)")));
848   g_variant_get (value, "(v)", &inner);
849   g_assert (g_variant_is_of_type (inner, G_VARIANT_TYPE_STRING));
850   g_assert_cmpstr (g_variant_get_string (inner, NULL), ==, "Property `PropertyUno' Is What It Is!");
851   g_variant_unref (value);
852
853   error = NULL;
854   value = g_dbus_proxy_call_sync (foo_proxy,
855                                   "org.freedesktop.DBus.Properties.Get",
856                                   g_variant_new ("(ss)",
857                                                  "org.example.Foo",
858                                                  "ThisDoesntExist"),
859                                   G_DBUS_CALL_FLAGS_NONE,
860                                   -1,
861                                   NULL,
862                                   &error);
863   g_assert (value == NULL);
864   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
865   g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: No such property `ThisDoesntExist'");
866   g_error_free (error);
867
868   error = NULL;
869   value = g_dbus_proxy_call_sync (foo_proxy,
870                                   "org.freedesktop.DBus.Properties.Get",
871                                   g_variant_new ("(ss)",
872                                                  "org.example.Foo",
873                                                  "NotReadable"),
874                                   G_DBUS_CALL_FLAGS_NONE,
875                                   -1,
876                                   NULL,
877                                   &error);
878   g_assert (value == NULL);
879   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
880   g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Property `NotReadable' is not readable");
881   g_error_free (error);
882
883   error = NULL;
884   value = g_dbus_proxy_call_sync (foo_proxy,
885                                   "org.freedesktop.DBus.Properties.Set",
886                                   g_variant_new ("(ssv)",
887                                                  "org.example.Foo",
888                                                  "NotReadable",
889                                                  g_variant_new_string ("But Writable you are!")),
890                                   G_DBUS_CALL_FLAGS_NONE,
891                                   -1,
892                                   NULL,
893                                   &error);
894   g_assert (value == NULL);
895   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_SPAWN_FILE_INVALID);
896   g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.Spawn.FileInvalid: Returning some error instead of writing the value `NotReadable' to the property `'But Writable you are!''");
897   g_error_free (error);
898
899   error = NULL;
900   value = g_dbus_proxy_call_sync (foo_proxy,
901                                   "org.freedesktop.DBus.Properties.Set",
902                                   g_variant_new ("(ssv)",
903                                                  "org.example.Foo",
904                                                  "NotWritable",
905                                                  g_variant_new_uint32 (42)),
906                                   G_DBUS_CALL_FLAGS_NONE,
907                                   -1,
908                                   NULL,
909                                   &error);
910   g_assert (value == NULL);
911   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
912   g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Property `NotWritable' is not writable");
913   g_error_free (error);
914
915   error = NULL;
916   value = g_dbus_proxy_call_sync (foo_proxy,
917                                   "org.freedesktop.DBus.Properties.GetAll",
918                                   g_variant_new ("(s)",
919                                                  "org.example.Foo"),
920                                   G_DBUS_CALL_FLAGS_NONE,
921                                   -1,
922                                   NULL,
923                                   &error);
924   g_assert_no_error (error);
925   g_assert (value != NULL);
926   g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("(a{sv})")));
927   s = g_variant_print (value, TRUE);
928   g_assert_cmpstr (s, ==, "({'PropertyUno': <\"Property `PropertyUno' Is What It Is!\">, 'NotWritable': <\"Property `NotWritable' Is What It Is!\">},)");
929   g_free (s);
930   g_variant_unref (value);
931
932   g_object_unref (foo_proxy);
933
934   g_main_loop_quit (loop);
935   return NULL;
936 }
937
938 static void
939 test_dispatch (const gchar *object_path)
940 {
941   GThread *thread;
942   GError *error;
943
944   /* run this in a thread to avoid deadlocks */
945   error = NULL;
946   thread = g_thread_create (test_dispatch_thread_func,
947                             (gpointer) object_path,
948                             TRUE,
949                             &error);
950   g_assert_no_error (error);
951   g_assert (thread != NULL);
952   g_main_loop_run (loop);
953   g_thread_join (thread);
954 }
955
956 static void
957 test_object_registration (void)
958 {
959   GError *error;
960   ObjectRegistrationData data;
961   GPtrArray *dyna_data;
962   gchar **nodes;
963   guint boss_foo_reg_id;
964   guint boss_bar_reg_id;
965   guint worker1_foo_reg_id;
966   guint worker1p1_foo_reg_id;
967   guint worker2_bar_reg_id;
968   guint intern1_foo_reg_id;
969   guint intern2_bar_reg_id;
970   guint intern2_foo_reg_id;
971   guint intern3_bar_reg_id;
972   guint registration_id;
973   guint subtree_registration_id;
974   guint non_subtree_object_path_foo_reg_id;
975   guint non_subtree_object_path_bar_reg_id;
976   guint dyna_subtree_registration_id;
977   guint num_successful_registrations;
978
979   data.num_unregistered_calls = 0;
980   data.num_unregistered_subtree_calls = 0;
981   data.num_subtree_nodes = 0;
982
983   num_successful_registrations = 0;
984
985   error = NULL;
986   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
987   g_assert_no_error (error);
988   g_assert (c != NULL);
989
990   registration_id = g_dbus_connection_register_object (c,
991                                                        "/foo/boss",
992                                                        &foo_interface_info,
993                                                        &foo_vtable,
994                                                        &data,
995                                                        on_object_unregistered,
996                                                        &error);
997   g_assert_no_error (error);
998   g_assert (registration_id > 0);
999   boss_foo_reg_id = registration_id;
1000   num_successful_registrations++;
1001
1002   registration_id = g_dbus_connection_register_object (c,
1003                                                        "/foo/boss",
1004                                                        &bar_interface_info,
1005                                                        NULL,
1006                                                        &data,
1007                                                        on_object_unregistered,
1008                                                        &error);
1009   g_assert_no_error (error);
1010   g_assert (registration_id > 0);
1011   boss_bar_reg_id = registration_id;
1012   num_successful_registrations++;
1013
1014   registration_id = g_dbus_connection_register_object (c,
1015                                                        "/foo/boss/worker1",
1016                                                        &foo_interface_info,
1017                                                        NULL,
1018                                                        &data,
1019                                                        on_object_unregistered,
1020                                                        &error);
1021   g_assert_no_error (error);
1022   g_assert (registration_id > 0);
1023   worker1_foo_reg_id = registration_id;
1024   num_successful_registrations++;
1025
1026   registration_id = g_dbus_connection_register_object (c,
1027                                                        "/foo/boss/worker1p1",
1028                                                        &foo_interface_info,
1029                                                        NULL,
1030                                                        &data,
1031                                                        on_object_unregistered,
1032                                                        &error);
1033   g_assert_no_error (error);
1034   g_assert (registration_id > 0);
1035   worker1p1_foo_reg_id = registration_id;
1036   num_successful_registrations++;
1037
1038   registration_id = g_dbus_connection_register_object (c,
1039                                                        "/foo/boss/worker2",
1040                                                        &bar_interface_info,
1041                                                        NULL,
1042                                                        &data,
1043                                                        on_object_unregistered,
1044                                                        &error);
1045   g_assert_no_error (error);
1046   g_assert (registration_id > 0);
1047   worker2_bar_reg_id = registration_id;
1048   num_successful_registrations++;
1049
1050   registration_id = g_dbus_connection_register_object (c,
1051                                                        "/foo/boss/interns/intern1",
1052                                                        &foo_interface_info,
1053                                                        NULL,
1054                                                        &data,
1055                                                        on_object_unregistered,
1056                                                        &error);
1057   g_assert_no_error (error);
1058   g_assert (registration_id > 0);
1059   intern1_foo_reg_id = registration_id;
1060   num_successful_registrations++;
1061
1062   /* ... and try again at another path */
1063   registration_id = g_dbus_connection_register_object (c,
1064                                                        "/foo/boss/interns/intern2",
1065                                                        &bar_interface_info,
1066                                                        NULL,
1067                                                        &data,
1068                                                        on_object_unregistered,
1069                                                        &error);
1070   g_assert_no_error (error);
1071   g_assert (registration_id > 0);
1072   intern2_bar_reg_id = registration_id;
1073   num_successful_registrations++;
1074
1075   /* register at the same path/interface - this should fail */
1076   registration_id = g_dbus_connection_register_object (c,
1077                                                        "/foo/boss/interns/intern2",
1078                                                        &bar_interface_info,
1079                                                        NULL,
1080                                                        &data,
1081                                                        on_object_unregistered,
1082                                                        &error);
1083   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
1084   g_assert (!g_dbus_error_is_remote_error (error));
1085   g_error_free (error);
1086   error = NULL;
1087   g_assert (registration_id == 0);
1088
1089   /* register at different interface - shouldn't fail */
1090   registration_id = g_dbus_connection_register_object (c,
1091                                                        "/foo/boss/interns/intern2",
1092                                                        &foo_interface_info,
1093                                                        NULL,
1094                                                        &data,
1095                                                        on_object_unregistered,
1096                                                        &error);
1097   g_assert_no_error (error);
1098   g_assert (registration_id > 0);
1099   intern2_foo_reg_id = registration_id;
1100   num_successful_registrations++;
1101
1102   /* unregister it via the id */
1103   g_assert (g_dbus_connection_unregister_object (c, registration_id));
1104   g_assert_cmpint (data.num_unregistered_calls, ==, 1);
1105   intern2_foo_reg_id = 0;
1106
1107   /* register it back */
1108   registration_id = g_dbus_connection_register_object (c,
1109                                                        "/foo/boss/interns/intern2",
1110                                                        &foo_interface_info,
1111                                                        NULL,
1112                                                        &data,
1113                                                        on_object_unregistered,
1114                                                        &error);
1115   g_assert_no_error (error);
1116   g_assert (registration_id > 0);
1117   intern2_foo_reg_id = registration_id;
1118   num_successful_registrations++;
1119
1120   registration_id = g_dbus_connection_register_object (c,
1121                                                        "/foo/boss/interns/intern3",
1122                                                        &bar_interface_info,
1123                                                        NULL,
1124                                                        &data,
1125                                                        on_object_unregistered,
1126                                                        &error);
1127   g_assert_no_error (error);
1128   g_assert (registration_id > 0);
1129   intern3_bar_reg_id = registration_id;
1130   num_successful_registrations++;
1131
1132   /* now register a whole subtree at /foo/boss/executives */
1133   subtree_registration_id = g_dbus_connection_register_subtree (c,
1134                                                                 "/foo/boss/executives",
1135                                                                 &subtree_vtable,
1136                                                                 G_DBUS_SUBTREE_FLAGS_NONE,
1137                                                                 &data,
1138                                                                 on_subtree_unregistered,
1139                                                                 &error);
1140   g_assert_no_error (error);
1141   g_assert (subtree_registration_id > 0);
1142   /* try registering it again.. this should fail */
1143   registration_id = g_dbus_connection_register_subtree (c,
1144                                                         "/foo/boss/executives",
1145                                                         &subtree_vtable,
1146                                                         G_DBUS_SUBTREE_FLAGS_NONE,
1147                                                         &data,
1148                                                         on_subtree_unregistered,
1149                                                         &error);
1150   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
1151   g_assert (!g_dbus_error_is_remote_error (error));
1152   g_error_free (error);
1153   error = NULL;
1154   g_assert (registration_id == 0);
1155
1156   /* unregister it, then register it again */
1157   g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 0);
1158   g_assert (g_dbus_connection_unregister_subtree (c, subtree_registration_id));
1159   g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 1);
1160   subtree_registration_id = g_dbus_connection_register_subtree (c,
1161                                                                 "/foo/boss/executives",
1162                                                                 &subtree_vtable,
1163                                                                 G_DBUS_SUBTREE_FLAGS_NONE,
1164                                                                 &data,
1165                                                                 on_subtree_unregistered,
1166                                                                 &error);
1167   g_assert_no_error (error);
1168   g_assert (subtree_registration_id > 0);
1169
1170   /* try to register something under /foo/boss/executives - this should work
1171    * because registered subtrees and registered objects can coexist.
1172    *
1173    * Make the exported object implement *two* interfaces so we can check
1174    * that the right introspection handler is invoked.
1175    */
1176   registration_id = g_dbus_connection_register_object (c,
1177                                                        "/foo/boss/executives/non_subtree_object",
1178                                                        &bar_interface_info,
1179                                                        NULL,
1180                                                        &data,
1181                                                        on_object_unregistered,
1182                                                        &error);
1183   g_assert_no_error (error);
1184   g_assert (registration_id > 0);
1185   non_subtree_object_path_bar_reg_id = registration_id;
1186   num_successful_registrations++;
1187   registration_id = g_dbus_connection_register_object (c,
1188                                                        "/foo/boss/executives/non_subtree_object",
1189                                                        &foo_interface_info,
1190                                                        NULL,
1191                                                        &data,
1192                                                        on_object_unregistered,
1193                                                        &error);
1194   g_assert_no_error (error);
1195   g_assert (registration_id > 0);
1196   non_subtree_object_path_foo_reg_id = registration_id;
1197   num_successful_registrations++;
1198
1199   /* now register a dynamic subtree, spawning objects as they are called */
1200   dyna_data = g_ptr_array_new ();
1201   dyna_subtree_registration_id = g_dbus_connection_register_subtree (c,
1202                                                                      "/foo/dyna",
1203                                                                      &dynamic_subtree_vtable,
1204                                                                      G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES,
1205                                                                      dyna_data,
1206                                                                      (GDestroyNotify)g_ptr_array_unref,
1207                                                                      &error);
1208   g_assert_no_error (error);
1209   g_assert (dyna_subtree_registration_id > 0);
1210
1211   /* First assert that we have no nodes in the dynamic subtree */
1212   nodes = get_nodes_at (c, "/foo/dyna");
1213   g_assert (nodes != NULL);
1214   g_assert_cmpint (g_strv_length (nodes), ==, 0);
1215   g_strfreev (nodes);
1216   g_assert_cmpint (count_interfaces (c, "/foo/dyna"), ==, 4);
1217
1218   /* Install three nodes in the dynamic subtree via the dyna_data backdoor and
1219    * assert that they show up correctly in the introspection data */
1220   g_ptr_array_add (dyna_data, "lol");
1221   g_ptr_array_add (dyna_data, "cat");
1222   g_ptr_array_add (dyna_data, "cheezburger");
1223   nodes = get_nodes_at (c, "/foo/dyna");
1224   g_assert (nodes != NULL);
1225   g_assert_cmpint (g_strv_length (nodes), ==, 3);
1226   g_assert_cmpstr (nodes[0], ==, "lol");
1227   g_assert_cmpstr (nodes[1], ==, "cat");
1228   g_assert_cmpstr (nodes[2], ==, "cheezburger");
1229   g_strfreev (nodes);
1230   g_assert_cmpint (count_interfaces (c, "/foo/dyna/lol"), ==, 4);
1231   g_assert_cmpint (count_interfaces (c, "/foo/dyna/cat"), ==, 4);
1232   g_assert_cmpint (count_interfaces (c, "/foo/dyna/cheezburger"), ==, 4);
1233
1234   /* Call a non-existing object path and assert that it has been created */
1235   dyna_create (c, "dynamicallycreated");
1236   nodes = get_nodes_at (c, "/foo/dyna");
1237   g_assert (nodes != NULL);
1238   g_assert_cmpint (g_strv_length (nodes), ==, 4);
1239   g_assert_cmpstr (nodes[0], ==, "lol");
1240   g_assert_cmpstr (nodes[1], ==, "cat");
1241   g_assert_cmpstr (nodes[2], ==, "cheezburger");
1242   g_assert_cmpstr (nodes[3], ==, "dynamicallycreated");
1243   g_strfreev (nodes);
1244   g_assert_cmpint (count_interfaces (c, "/foo/dyna/dynamicallycreated"), ==, 4);
1245
1246   /* now check that the object hierarachy is properly generated... yes, it's a bit
1247    * perverse that we round-trip to the bus to introspect ourselves ;-)
1248    */
1249   nodes = get_nodes_at (c, "/");
1250   g_assert (nodes != NULL);
1251   g_assert_cmpint (g_strv_length (nodes), ==, 1);
1252   g_assert_cmpstr (nodes[0], ==, "foo");
1253   g_strfreev (nodes);
1254   g_assert_cmpint (count_interfaces (c, "/"), ==, 0);
1255
1256   nodes = get_nodes_at (c, "/foo");
1257   g_assert (nodes != NULL);
1258   g_assert_cmpint (g_strv_length (nodes), ==, 2);
1259   g_assert_cmpstr (nodes[0], ==, "boss");
1260   g_assert_cmpstr (nodes[1], ==, "dyna");
1261   g_strfreev (nodes);
1262   g_assert_cmpint (count_interfaces (c, "/foo"), ==, 0);
1263
1264   nodes = get_nodes_at (c, "/foo/boss");
1265   g_assert (nodes != NULL);
1266   g_assert_cmpint (g_strv_length (nodes), ==, 5);
1267   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker1"));
1268   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker1p1"));
1269   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker2"));
1270   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "interns"));
1271   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "executives"));
1272   g_strfreev (nodes);
1273   /* any registered object always implement org.freedesktop.DBus.[Peer,Introspectable,Properties] */
1274   g_assert_cmpint (count_interfaces (c, "/foo/boss"), ==, 5);
1275   g_assert (has_interface (c, "/foo/boss", foo_interface_info.name));
1276   g_assert (has_interface (c, "/foo/boss", bar_interface_info.name));
1277
1278   /* check subtree nodes - we should have only non_subtree_object in /foo/boss/executives
1279    * because data.num_subtree_nodes is 0
1280    */
1281   nodes = get_nodes_at (c, "/foo/boss/executives");
1282   g_assert (nodes != NULL);
1283   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1284   g_assert_cmpint (g_strv_length (nodes), ==, 1);
1285   g_strfreev (nodes);
1286   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives"), ==, 0);
1287
1288   /* now change data.num_subtree_nodes and check */
1289   data.num_subtree_nodes = 2;
1290   nodes = get_nodes_at (c, "/foo/boss/executives");
1291   g_assert (nodes != NULL);
1292   g_assert_cmpint (g_strv_length (nodes), ==, 5);
1293   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1294   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp0"));
1295   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp1"));
1296   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp0"));
1297   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp1"));
1298   /* check that /foo/boss/executives/non_subtree_object is not handled by the
1299    * subtree handlers - we can do this because objects from subtree handlers
1300    * has exactly one interface and non_subtree_object has two
1301    */
1302   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/non_subtree_object"), ==, 5);
1303   g_assert (has_interface (c, "/foo/boss/executives/non_subtree_object", foo_interface_info.name));
1304   g_assert (has_interface (c, "/foo/boss/executives/non_subtree_object", bar_interface_info.name));
1305   /* check that the vp and evp objects are handled by the subtree handlers */
1306   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/vp0"), ==, 4);
1307   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/vp1"), ==, 4);
1308   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/evp0"), ==, 4);
1309   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/evp1"), ==, 4);
1310   g_assert (has_interface (c, "/foo/boss/executives/vp0", foo_interface_info.name));
1311   g_assert (has_interface (c, "/foo/boss/executives/vp1", foo_interface_info.name));
1312   g_assert (has_interface (c, "/foo/boss/executives/evp0", bar_interface_info.name));
1313   g_assert (has_interface (c, "/foo/boss/executives/evp1", bar_interface_info.name));
1314   g_strfreev (nodes);
1315   data.num_subtree_nodes = 3;
1316   nodes = get_nodes_at (c, "/foo/boss/executives");
1317   g_assert (nodes != NULL);
1318   g_assert_cmpint (g_strv_length (nodes), ==, 7);
1319   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1320   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp0"));
1321   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp1"));
1322   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp2"));
1323   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp0"));
1324   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp1"));
1325   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp2"));
1326   g_strfreev (nodes);
1327
1328   /* This is to check that a bug (rather, class of bugs) in gdbusconnection.c's
1329    *
1330    *  g_dbus_connection_list_registered_unlocked()
1331    *
1332    * where /foo/boss/worker1 reported a child '1', is now fixed.
1333    */
1334   nodes = get_nodes_at (c, "/foo/boss/worker1");
1335   g_assert (nodes != NULL);
1336   g_assert_cmpint (g_strv_length (nodes), ==, 0);
1337   g_strfreev (nodes);
1338
1339   /* check that calls are properly dispatched to the functions in foo_vtable for objects
1340    * implementing the org.example.Foo interface
1341    *
1342    * We do this for both a regular registered object (/foo/boss) and also for an object
1343    * registered through the subtree mechanism.
1344    */
1345   test_dispatch ("/foo/boss");
1346   test_dispatch ("/foo/boss/executives/vp0");
1347
1348   /* To prevent from exiting and attaching a D-Bus tool like D-Feet; uncomment: */
1349 #if 0
1350   g_debug ("Point D-feet or other tool at: %s", session_bus_get_temporary_address());
1351   g_main_loop_run (loop);
1352 #endif
1353
1354   /* check that unregistering the subtree handler works */
1355   g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 1);
1356   g_assert (g_dbus_connection_unregister_subtree (c, subtree_registration_id));
1357   g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 2);
1358   nodes = get_nodes_at (c, "/foo/boss/executives");
1359   g_assert (nodes != NULL);
1360   g_assert_cmpint (g_strv_length (nodes), ==, 1);
1361   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1362   g_strfreev (nodes);
1363
1364   g_assert (g_dbus_connection_unregister_object (c, boss_foo_reg_id));
1365   g_assert (g_dbus_connection_unregister_object (c, boss_bar_reg_id));
1366   g_assert (g_dbus_connection_unregister_object (c, worker1_foo_reg_id));
1367   g_assert (g_dbus_connection_unregister_object (c, worker1p1_foo_reg_id));
1368   g_assert (g_dbus_connection_unregister_object (c, worker2_bar_reg_id));
1369   g_assert (g_dbus_connection_unregister_object (c, intern1_foo_reg_id));
1370   g_assert (g_dbus_connection_unregister_object (c, intern2_bar_reg_id));
1371   g_assert (g_dbus_connection_unregister_object (c, intern2_foo_reg_id));
1372   g_assert (g_dbus_connection_unregister_object (c, intern3_bar_reg_id));
1373   g_assert (g_dbus_connection_unregister_object (c, non_subtree_object_path_bar_reg_id));
1374   g_assert (g_dbus_connection_unregister_object (c, non_subtree_object_path_foo_reg_id));
1375
1376   g_assert_cmpint (data.num_unregistered_calls, ==, num_successful_registrations);
1377
1378   /* check that we no longer export any objects - TODO: it looks like there's a bug in
1379    * libdbus-1 here: libdbus still reports the '/foo' object; so disable the test for now
1380    */
1381 #if 0
1382   nodes = get_nodes_at (c, "/");
1383   g_assert (nodes != NULL);
1384   g_assert_cmpint (g_strv_length (nodes), ==, 0);
1385   g_strfreev (nodes);
1386 #endif
1387
1388   g_object_unref (c);
1389 }
1390
1391
1392 /* ---------------------------------------------------------------------------------------------------- */
1393
1394 int
1395 main (int   argc,
1396       char *argv[])
1397 {
1398   gint ret;
1399
1400   g_type_init ();
1401   g_test_init (&argc, &argv, NULL);
1402
1403   /* all the tests rely on a shared main loop */
1404   loop = g_main_loop_new (NULL, FALSE);
1405
1406   /* all the tests use a session bus with a well-known address that we can bring up and down
1407    * using session_bus_up() and session_bus_down().
1408    */
1409   g_unsetenv ("DISPLAY");
1410   g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
1411
1412   session_bus_up ();
1413
1414   /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
1415    * until one can connect to the bus but that's not how things work right now
1416    */
1417   usleep (500 * 1000);
1418
1419   g_test_add_func ("/gdbus/object-registration", test_object_registration);
1420   /* TODO: check that we spit out correct introspection data */
1421   /* TODO: check that registering a whole subtree works */
1422
1423   ret = g_test_run();
1424
1425   /* tear down bus */
1426   session_bus_down ();
1427
1428   return ret;
1429 }