Plug mem leaks in gdbus tests & examples
[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_TYPE_DBUS_PROXY,
347                                  G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
348                                  G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
349                                  NULL,
350                                  g_dbus_connection_get_unique_name (c),
351                                  object_path,
352                                  "org.freedesktop.DBus.Introspectable",
353                                  NULL,
354                                  &error);
355   g_assert_no_error (error);
356   g_assert (proxy != NULL);
357
358   /* do this async to avoid libdbus-1 deadlocks */
359   xml_data = NULL;
360   g_dbus_proxy_call (proxy,
361                      "Introspect",
362                      NULL,
363                      G_DBUS_CALL_FLAGS_NONE,
364                      -1,
365                      NULL,
366                      (GAsyncReadyCallback) introspect_callback,
367                      &xml_data);
368   g_main_loop_run (loop);
369   g_assert (xml_data != NULL);
370
371   node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
372   g_assert_no_error (error);
373   g_assert (node_info != NULL);
374
375   p = g_ptr_array_new ();
376   for (n = 0; node_info->nodes != NULL && node_info->nodes[n] != NULL; n++)
377     {
378       const GDBusNodeInfo *sub_node_info = node_info->nodes[n];
379       g_ptr_array_add (p, g_strdup (sub_node_info->path));
380     }
381   g_ptr_array_add (p, NULL);
382
383   g_object_unref (proxy);
384   g_free (xml_data);
385   g_dbus_node_info_unref (node_info);
386
387   return (gchar **) g_ptr_array_free (p, FALSE);
388 }
389
390 static gboolean
391 has_interface (GDBusConnection *c,
392                const gchar     *object_path,
393                const gchar     *interface_name)
394 {
395   GError *error;
396   GDBusProxy *proxy;
397   gchar *xml_data;
398   GDBusNodeInfo *node_info;
399   gboolean ret;
400
401   error = NULL;
402   proxy = g_dbus_proxy_new_sync (c,
403                                  G_TYPE_DBUS_PROXY,
404                                  G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
405                                  G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
406                                  NULL,
407                                  g_dbus_connection_get_unique_name (c),
408                                  object_path,
409                                  "org.freedesktop.DBus.Introspectable",
410                                  NULL,
411                                  &error);
412   g_assert_no_error (error);
413   g_assert (proxy != NULL);
414
415   /* do this async to avoid libdbus-1 deadlocks */
416   xml_data = NULL;
417   g_dbus_proxy_call (proxy,
418                      "Introspect",
419                      NULL,
420                      G_DBUS_CALL_FLAGS_NONE,
421                      -1,
422                      NULL,
423                      (GAsyncReadyCallback) introspect_callback,
424                      &xml_data);
425   g_main_loop_run (loop);
426   g_assert (xml_data != NULL);
427
428   node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
429   g_assert_no_error (error);
430   g_assert (node_info != NULL);
431
432   ret = (g_dbus_node_info_lookup_interface (node_info, interface_name) != NULL);
433
434   g_object_unref (proxy);
435   g_free (xml_data);
436   g_dbus_node_info_unref (node_info);
437
438   return ret;
439 }
440
441 static guint
442 count_interfaces (GDBusConnection *c,
443                   const gchar     *object_path)
444 {
445   GError *error;
446   GDBusProxy *proxy;
447   gchar *xml_data;
448   GDBusNodeInfo *node_info;
449   guint ret;
450
451   error = NULL;
452   proxy = g_dbus_proxy_new_sync (c,
453                                  G_TYPE_DBUS_PROXY,
454                                  G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
455                                  G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
456                                  NULL,
457                                  g_dbus_connection_get_unique_name (c),
458                                  object_path,
459                                  "org.freedesktop.DBus.Introspectable",
460                                  NULL,
461                                  &error);
462   g_assert_no_error (error);
463   g_assert (proxy != NULL);
464
465   /* do this async to avoid libdbus-1 deadlocks */
466   xml_data = NULL;
467   g_dbus_proxy_call (proxy,
468                      "Introspect",
469                      NULL,
470                      G_DBUS_CALL_FLAGS_NONE,
471                      -1,
472                      NULL,
473                      (GAsyncReadyCallback) introspect_callback,
474                      &xml_data);
475   g_main_loop_run (loop);
476   g_assert (xml_data != NULL);
477
478   node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
479   g_assert_no_error (error);
480   g_assert (node_info != NULL);
481
482   ret = 0;
483   while (node_info->interfaces != NULL && node_info->interfaces[ret] != NULL)
484     ret++;
485
486   g_object_unref (proxy);
487   g_free (xml_data);
488   g_dbus_node_info_unref (node_info);
489
490   return ret;
491 }
492
493 static void
494 dyna_create_callback (GDBusProxy   *proxy,
495                       GAsyncResult  *res,
496                       gpointer      user_data)
497 {
498   GVariant *result;
499   GError *error;
500
501   error = NULL;
502   result = g_dbus_proxy_call_finish (proxy,
503                                      res,
504                                      &error);
505   g_assert_no_error (error);
506   g_assert (result != NULL);
507   g_variant_unref (result);
508
509   g_main_loop_quit (loop);
510 }
511
512 /* Dynamically create @object_name under /foo/dyna */
513 static void
514 dyna_create (GDBusConnection *c,
515              const gchar     *object_name)
516 {
517   GError *error;
518   GDBusProxy *proxy;
519   gchar *object_path;
520
521   object_path = g_strconcat ("/foo/dyna/", object_name, NULL);
522
523   error = NULL;
524   proxy = g_dbus_proxy_new_sync (c,
525                                  G_TYPE_DBUS_PROXY,
526                                  G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
527                                  G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
528                                  NULL,
529                                  g_dbus_connection_get_unique_name (c),
530                                  object_path,
531                                  "org.example.Dyna",
532                                  NULL,
533                                  &error);
534   g_assert_no_error (error);
535   g_assert (proxy != NULL);
536
537   /* do this async to avoid libdbus-1 deadlocks */
538   g_dbus_proxy_call (proxy,
539                      "DynaCyber",
540                      g_variant_new ("()"),
541                      G_DBUS_CALL_FLAGS_NONE,
542                      -1,
543                      NULL,
544                      (GAsyncReadyCallback) dyna_create_callback,
545                      NULL);
546   g_main_loop_run (loop);
547
548   g_assert_no_error (error);
549
550   g_object_unref (proxy);
551   g_free (object_path);
552
553   return;
554 }
555
556 typedef struct
557 {
558   guint num_unregistered_calls;
559   guint num_unregistered_subtree_calls;
560   guint num_subtree_nodes;
561 } ObjectRegistrationData;
562
563 static void
564 on_object_unregistered (gpointer user_data)
565 {
566   ObjectRegistrationData *data = user_data;
567
568   data->num_unregistered_calls++;
569 }
570
571 static void
572 on_subtree_unregistered (gpointer user_data)
573 {
574   ObjectRegistrationData *data = user_data;
575
576   data->num_unregistered_subtree_calls++;
577 }
578
579 static gboolean
580 _g_strv_has_string (const gchar* const * haystack,
581                     const gchar *needle)
582 {
583   guint n;
584
585   for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
586     {
587       if (g_strcmp0 (haystack[n], needle) == 0)
588         return TRUE;
589     }
590   return FALSE;
591 }
592
593 /* -------------------- */
594
595 static gchar **
596 subtree_enumerate (GDBusConnection       *connection,
597                    const gchar           *sender,
598                    const gchar           *object_path,
599                    gpointer               user_data)
600 {
601   ObjectRegistrationData *data = user_data;
602   GPtrArray *p;
603   gchar **nodes;
604   guint n;
605
606   p = g_ptr_array_new ();
607
608   for (n = 0; n < data->num_subtree_nodes; n++)
609     {
610       g_ptr_array_add (p, g_strdup_printf ("vp%d", n));
611       g_ptr_array_add (p, g_strdup_printf ("evp%d", n));
612     }
613   g_ptr_array_add (p, NULL);
614
615   nodes = (gchar **) g_ptr_array_free (p, FALSE);
616
617   return nodes;
618 }
619
620 /* Only allows certain objects, and aborts on unknowns */
621 static GPtrArray *
622 subtree_introspect (GDBusConnection       *connection,
623                     const gchar           *sender,
624                     const gchar           *object_path,
625                     const gchar           *node,
626                     gpointer               user_data)
627 {
628   GPtrArray *interfaces;
629
630   /* VPs implement the Foo interface, EVPs implement the Bar interface. The root
631    * does not implement any interfaces
632    */
633   interfaces = g_ptr_array_new ();
634   if (g_str_has_prefix (node, "vp"))
635     {
636       g_ptr_array_add (interfaces, (gpointer) &foo_interface_info);
637     }
638   else if (g_str_has_prefix (node, "evp"))
639     {
640       g_ptr_array_add (interfaces, (gpointer) &bar_interface_info);
641     }
642   else if (g_strcmp0 (node, "/") == 0)
643     {
644       /* do nothing */
645     }
646   else
647     {
648       g_assert_not_reached ();
649     }
650
651   return interfaces;
652 }
653
654 static const GDBusInterfaceVTable *
655 subtree_dispatch (GDBusConnection             *connection,
656                   const gchar                 *sender,
657                   const gchar                 *object_path,
658                   const gchar                 *interface_name,
659                   const gchar                 *node,
660                   gpointer                    *out_user_data,
661                   gpointer                     user_data)
662 {
663   if (g_strcmp0 (interface_name, "org.example.Foo") == 0)
664     return &foo_vtable;
665   else
666     return NULL;
667 }
668
669 static const GDBusSubtreeVTable subtree_vtable =
670 {
671   subtree_enumerate,
672   subtree_introspect,
673   subtree_dispatch
674 };
675
676 /* -------------------- */
677
678 static gchar **
679 dynamic_subtree_enumerate (GDBusConnection       *connection,
680                            const gchar           *sender,
681                            const gchar           *object_path,
682                            gpointer               user_data)
683 {
684   GPtrArray *data = user_data;
685   gchar **nodes = g_new (gchar*, data->len + 1);
686   guint n;
687
688   for (n = 0; n < data->len; n++)
689     {
690       nodes[n] = g_strdup (g_ptr_array_index (data, n));
691     }
692   nodes[data->len] = NULL;
693
694   return nodes;
695 }
696
697 /* Allow all objects to be introspected */
698 static GPtrArray *
699 dynamic_subtree_introspect (GDBusConnection       *connection,
700                             const gchar           *sender,
701                             const gchar           *object_path,
702                             const gchar           *node,
703                             gpointer               user_data)
704 {
705   GPtrArray *interfaces;
706
707   /* All nodes (including the root node) implements the Dyna interface */
708   interfaces = g_ptr_array_new ();
709   g_ptr_array_add (interfaces, (gpointer) &dyna_interface_info);
710
711   return interfaces;
712 }
713
714 static const GDBusInterfaceVTable *
715 dynamic_subtree_dispatch (GDBusConnection             *connection,
716                           const gchar                 *sender,
717                           const gchar                 *object_path,
718                           const gchar                 *interface_name,
719                           const gchar                 *node,
720                           gpointer                    *out_user_data,
721                           gpointer                     user_data)
722 {
723   *out_user_data = user_data;
724   return &dyna_interface_vtable;
725 }
726
727 static const GDBusSubtreeVTable dynamic_subtree_vtable =
728 {
729   dynamic_subtree_enumerate,
730   dynamic_subtree_introspect,
731   dynamic_subtree_dispatch
732 };
733
734 /* -------------------- */
735
736 static gpointer
737 test_dispatch_thread_func (gpointer user_data)
738 {
739   const gchar *object_path = user_data;
740   GDBusProxy *foo_proxy;
741   GVariant *value;
742   GVariant *inner;
743   GError *error;
744   gchar *s;
745   const gchar *value_str;
746
747   foo_proxy = g_dbus_proxy_new_sync (c,
748                                      G_TYPE_DBUS_PROXY,
749                                      G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS |
750                                      G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
751                                      NULL,
752                                      g_dbus_connection_get_unique_name (c),
753                                      object_path,
754                                      "org.example.Foo",
755                                      NULL,
756                                      &error);
757   g_assert (foo_proxy != NULL);
758
759   /* generic interfaces */
760   error = NULL;
761   value = g_dbus_proxy_call_sync (foo_proxy,
762                                   "org.freedesktop.DBus.Peer.Ping",
763                                   NULL,
764                                   G_DBUS_CALL_FLAGS_NONE,
765                                   -1,
766                                   NULL,
767                                   &error);
768   g_assert_no_error (error);
769   g_assert (value != NULL);
770   g_variant_unref (value);
771
772   /* user methods */
773   error = NULL;
774   value = g_dbus_proxy_call_sync (foo_proxy,
775                                   "Method1",
776                                   g_variant_new ("(s)", "winwinwin"),
777                                   G_DBUS_CALL_FLAGS_NONE,
778                                   -1,
779                                   NULL,
780                                   &error);
781   g_assert_no_error (error);
782   g_assert (value != NULL);
783   g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("(s)")));
784   g_variant_get (value, "(&s)", &value_str);
785   g_assert_cmpstr (value_str, ==, "You passed the string `winwinwin'. Jolly good!");
786   g_variant_unref (value);
787
788   error = NULL;
789   value = g_dbus_proxy_call_sync (foo_proxy,
790                                   "Method2",
791                                   NULL,
792                                   G_DBUS_CALL_FLAGS_NONE,
793                                   -1,
794                                   NULL,
795                                   &error);
796   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
797   g_assert_cmpstr (error->message, ==, "GDBus.Error:org.example.SomeError: How do you like them apples, buddy!");
798   g_error_free (error);
799   g_assert (value == NULL);
800
801   error = NULL;
802   value = g_dbus_proxy_call_sync (foo_proxy,
803                                   "Method2",
804                                   g_variant_new ("(s)", "failfailfail"),
805                                   G_DBUS_CALL_FLAGS_NONE,
806                                   -1,
807                                   NULL,
808                                   &error);
809   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
810   g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Signature of message, `s', does not match expected signature `'");
811   g_error_free (error);
812   g_assert (value == NULL);
813
814   error = NULL;
815   value = g_dbus_proxy_call_sync (foo_proxy,
816                                   "NonExistantMethod",
817                                   NULL,
818                                   G_DBUS_CALL_FLAGS_NONE,
819                                   -1,
820                                   NULL,
821                                   &error);
822   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
823   g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod: No such method `NonExistantMethod'");
824   g_error_free (error);
825   g_assert (value == NULL);
826
827   /* user properties */
828   error = NULL;
829   value = g_dbus_proxy_call_sync (foo_proxy,
830                                   "org.freedesktop.DBus.Properties.Get",
831                                   g_variant_new ("(ss)",
832                                                  "org.example.Foo",
833                                                  "PropertyUno"),
834                                   G_DBUS_CALL_FLAGS_NONE,
835                                   -1,
836                                   NULL,
837                                   &error);
838   g_assert_no_error (error);
839   g_assert (value != NULL);
840   g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("(v)")));
841   g_variant_get (value, "(v)", &inner);
842   g_assert (g_variant_is_of_type (inner, G_VARIANT_TYPE_STRING));
843   g_assert_cmpstr (g_variant_get_string (inner, NULL), ==, "Property `PropertyUno' Is What It Is!");
844   g_variant_unref (value);
845
846   error = NULL;
847   value = g_dbus_proxy_call_sync (foo_proxy,
848                                   "org.freedesktop.DBus.Properties.Get",
849                                   g_variant_new ("(ss)",
850                                                  "org.example.Foo",
851                                                  "ThisDoesntExist"),
852                                   G_DBUS_CALL_FLAGS_NONE,
853                                   -1,
854                                   NULL,
855                                   &error);
856   g_assert (value == NULL);
857   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
858   g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: No such property `ThisDoesntExist'");
859   g_error_free (error);
860
861   error = NULL;
862   value = g_dbus_proxy_call_sync (foo_proxy,
863                                   "org.freedesktop.DBus.Properties.Get",
864                                   g_variant_new ("(ss)",
865                                                  "org.example.Foo",
866                                                  "NotReadable"),
867                                   G_DBUS_CALL_FLAGS_NONE,
868                                   -1,
869                                   NULL,
870                                   &error);
871   g_assert (value == NULL);
872   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
873   g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Property `NotReadable' is not readable");
874   g_error_free (error);
875
876   error = NULL;
877   value = g_dbus_proxy_call_sync (foo_proxy,
878                                   "org.freedesktop.DBus.Properties.Set",
879                                   g_variant_new ("(ssv)",
880                                                  "org.example.Foo",
881                                                  "NotReadable",
882                                                  g_variant_new_string ("But Writable you are!")),
883                                   G_DBUS_CALL_FLAGS_NONE,
884                                   -1,
885                                   NULL,
886                                   &error);
887   g_assert (value == NULL);
888   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_SPAWN_FILE_INVALID);
889   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!''");
890   g_error_free (error);
891
892   error = NULL;
893   value = g_dbus_proxy_call_sync (foo_proxy,
894                                   "org.freedesktop.DBus.Properties.Set",
895                                   g_variant_new ("(ssv)",
896                                                  "org.example.Foo",
897                                                  "NotWritable",
898                                                  g_variant_new_uint32 (42)),
899                                   G_DBUS_CALL_FLAGS_NONE,
900                                   -1,
901                                   NULL,
902                                   &error);
903   g_assert (value == NULL);
904   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
905   g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Property `NotWritable' is not writable");
906   g_error_free (error);
907
908   error = NULL;
909   value = g_dbus_proxy_call_sync (foo_proxy,
910                                   "org.freedesktop.DBus.Properties.GetAll",
911                                   g_variant_new ("(s)",
912                                                  "org.example.Foo"),
913                                   G_DBUS_CALL_FLAGS_NONE,
914                                   -1,
915                                   NULL,
916                                   &error);
917   g_assert_no_error (error);
918   g_assert (value != NULL);
919   g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("(a{sv})")));
920   s = g_variant_print (value, TRUE);
921   g_assert_cmpstr (s, ==, "({'PropertyUno': <\"Property `PropertyUno' Is What It Is!\">, 'NotWritable': <\"Property `NotWritable' Is What It Is!\">},)");
922   g_free (s);
923   g_variant_unref (value);
924
925   g_object_unref (foo_proxy);
926
927   g_main_loop_quit (loop);
928   return NULL;
929 }
930
931 static void
932 test_dispatch (const gchar *object_path)
933 {
934   GThread *thread;
935   GError *error;
936
937   /* run this in a thread to avoid deadlocks */
938   error = NULL;
939   thread = g_thread_create (test_dispatch_thread_func,
940                             (gpointer) object_path,
941                             TRUE,
942                             &error);
943   g_assert_no_error (error);
944   g_assert (thread != NULL);
945   g_main_loop_run (loop);
946   g_thread_join (thread);
947 }
948
949 static void
950 test_object_registration (void)
951 {
952   GError *error;
953   ObjectRegistrationData data;
954   GPtrArray *dyna_data;
955   gchar **nodes;
956   guint boss_foo_reg_id;
957   guint boss_bar_reg_id;
958   guint worker1_foo_reg_id;
959   guint worker2_bar_reg_id;
960   guint intern1_foo_reg_id;
961   guint intern2_bar_reg_id;
962   guint intern2_foo_reg_id;
963   guint intern3_bar_reg_id;
964   guint registration_id;
965   guint subtree_registration_id;
966   guint non_subtree_object_path_foo_reg_id;
967   guint non_subtree_object_path_bar_reg_id;
968   guint dyna_subtree_registration_id;
969   guint num_successful_registrations;
970
971   data.num_unregistered_calls = 0;
972   data.num_unregistered_subtree_calls = 0;
973   data.num_subtree_nodes = 0;
974
975   num_successful_registrations = 0;
976
977   error = NULL;
978   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
979   g_assert_no_error (error);
980   g_assert (c != NULL);
981
982   registration_id = g_dbus_connection_register_object (c,
983                                                        "/foo/boss",
984                                                        &foo_interface_info,
985                                                        &foo_vtable,
986                                                        &data,
987                                                        on_object_unregistered,
988                                                        &error);
989   g_assert_no_error (error);
990   g_assert (registration_id > 0);
991   boss_foo_reg_id = registration_id;
992   num_successful_registrations++;
993
994   registration_id = g_dbus_connection_register_object (c,
995                                                        "/foo/boss",
996                                                        &bar_interface_info,
997                                                        NULL,
998                                                        &data,
999                                                        on_object_unregistered,
1000                                                        &error);
1001   g_assert_no_error (error);
1002   g_assert (registration_id > 0);
1003   boss_bar_reg_id = registration_id;
1004   num_successful_registrations++;
1005
1006   registration_id = g_dbus_connection_register_object (c,
1007                                                        "/foo/boss/worker1",
1008                                                        &foo_interface_info,
1009                                                        NULL,
1010                                                        &data,
1011                                                        on_object_unregistered,
1012                                                        &error);
1013   g_assert_no_error (error);
1014   g_assert (registration_id > 0);
1015   worker1_foo_reg_id = registration_id;
1016   num_successful_registrations++;
1017
1018   registration_id = g_dbus_connection_register_object (c,
1019                                                        "/foo/boss/worker2",
1020                                                        &bar_interface_info,
1021                                                        NULL,
1022                                                        &data,
1023                                                        on_object_unregistered,
1024                                                        &error);
1025   g_assert_no_error (error);
1026   g_assert (registration_id > 0);
1027   worker2_bar_reg_id = registration_id;
1028   num_successful_registrations++;
1029
1030   registration_id = g_dbus_connection_register_object (c,
1031                                                        "/foo/boss/interns/intern1",
1032                                                        &foo_interface_info,
1033                                                        NULL,
1034                                                        &data,
1035                                                        on_object_unregistered,
1036                                                        &error);
1037   g_assert_no_error (error);
1038   g_assert (registration_id > 0);
1039   intern1_foo_reg_id = registration_id;
1040   num_successful_registrations++;
1041
1042   /* ... and try again at another path */
1043   registration_id = g_dbus_connection_register_object (c,
1044                                                        "/foo/boss/interns/intern2",
1045                                                        &bar_interface_info,
1046                                                        NULL,
1047                                                        &data,
1048                                                        on_object_unregistered,
1049                                                        &error);
1050   g_assert_no_error (error);
1051   g_assert (registration_id > 0);
1052   intern2_bar_reg_id = registration_id;
1053   num_successful_registrations++;
1054
1055   /* register at the same path/interface - this should fail */
1056   registration_id = g_dbus_connection_register_object (c,
1057                                                        "/foo/boss/interns/intern2",
1058                                                        &bar_interface_info,
1059                                                        NULL,
1060                                                        &data,
1061                                                        on_object_unregistered,
1062                                                        &error);
1063   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
1064   g_assert (!g_dbus_error_is_remote_error (error));
1065   g_error_free (error);
1066   error = NULL;
1067   g_assert (registration_id == 0);
1068
1069   /* register at different interface - shouldn't fail */
1070   registration_id = g_dbus_connection_register_object (c,
1071                                                        "/foo/boss/interns/intern2",
1072                                                        &foo_interface_info,
1073                                                        NULL,
1074                                                        &data,
1075                                                        on_object_unregistered,
1076                                                        &error);
1077   g_assert_no_error (error);
1078   g_assert (registration_id > 0);
1079   intern2_foo_reg_id = registration_id;
1080   num_successful_registrations++;
1081
1082   /* unregister it via the id */
1083   g_assert (g_dbus_connection_unregister_object (c, registration_id));
1084   g_assert_cmpint (data.num_unregistered_calls, ==, 1);
1085   intern2_foo_reg_id = 0;
1086
1087   /* register it back */
1088   registration_id = g_dbus_connection_register_object (c,
1089                                                        "/foo/boss/interns/intern2",
1090                                                        &foo_interface_info,
1091                                                        NULL,
1092                                                        &data,
1093                                                        on_object_unregistered,
1094                                                        &error);
1095   g_assert_no_error (error);
1096   g_assert (registration_id > 0);
1097   intern2_foo_reg_id = registration_id;
1098   num_successful_registrations++;
1099
1100   registration_id = g_dbus_connection_register_object (c,
1101                                                        "/foo/boss/interns/intern3",
1102                                                        &bar_interface_info,
1103                                                        NULL,
1104                                                        &data,
1105                                                        on_object_unregistered,
1106                                                        &error);
1107   g_assert_no_error (error);
1108   g_assert (registration_id > 0);
1109   intern3_bar_reg_id = registration_id;
1110   num_successful_registrations++;
1111
1112   /* now register a whole subtree at /foo/boss/executives */
1113   subtree_registration_id = g_dbus_connection_register_subtree (c,
1114                                                                 "/foo/boss/executives",
1115                                                                 &subtree_vtable,
1116                                                                 G_DBUS_SUBTREE_FLAGS_NONE,
1117                                                                 &data,
1118                                                                 on_subtree_unregistered,
1119                                                                 &error);
1120   g_assert_no_error (error);
1121   g_assert (subtree_registration_id > 0);
1122   /* try registering it again.. this should fail */
1123   registration_id = g_dbus_connection_register_subtree (c,
1124                                                         "/foo/boss/executives",
1125                                                         &subtree_vtable,
1126                                                         G_DBUS_SUBTREE_FLAGS_NONE,
1127                                                         &data,
1128                                                         on_subtree_unregistered,
1129                                                         &error);
1130   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
1131   g_assert (!g_dbus_error_is_remote_error (error));
1132   g_error_free (error);
1133   error = NULL;
1134   g_assert (registration_id == 0);
1135
1136   /* unregister it, then register it again */
1137   g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 0);
1138   g_assert (g_dbus_connection_unregister_subtree (c, subtree_registration_id));
1139   g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 1);
1140   subtree_registration_id = g_dbus_connection_register_subtree (c,
1141                                                                 "/foo/boss/executives",
1142                                                                 &subtree_vtable,
1143                                                                 G_DBUS_SUBTREE_FLAGS_NONE,
1144                                                                 &data,
1145                                                                 on_subtree_unregistered,
1146                                                                 &error);
1147   g_assert_no_error (error);
1148   g_assert (subtree_registration_id > 0);
1149
1150   /* try to register something under /foo/boss/executives - this should work
1151    * because registered subtrees and registered objects can coexist.
1152    *
1153    * Make the exported object implement *two* interfaces so we can check
1154    * that the right introspection handler is invoked.
1155    */
1156   registration_id = g_dbus_connection_register_object (c,
1157                                                        "/foo/boss/executives/non_subtree_object",
1158                                                        &bar_interface_info,
1159                                                        NULL,
1160                                                        &data,
1161                                                        on_object_unregistered,
1162                                                        &error);
1163   g_assert_no_error (error);
1164   g_assert (registration_id > 0);
1165   non_subtree_object_path_bar_reg_id = registration_id;
1166   num_successful_registrations++;
1167   registration_id = g_dbus_connection_register_object (c,
1168                                                        "/foo/boss/executives/non_subtree_object",
1169                                                        &foo_interface_info,
1170                                                        NULL,
1171                                                        &data,
1172                                                        on_object_unregistered,
1173                                                        &error);
1174   g_assert_no_error (error);
1175   g_assert (registration_id > 0);
1176   non_subtree_object_path_foo_reg_id = registration_id;
1177   num_successful_registrations++;
1178
1179   /* now register a dynamic subtree, spawning objects as they are called */
1180   dyna_data = g_ptr_array_new ();
1181   dyna_subtree_registration_id = g_dbus_connection_register_subtree (c,
1182                                                                      "/foo/dyna",
1183                                                                      &dynamic_subtree_vtable,
1184                                                                      G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES,
1185                                                                      dyna_data,
1186                                                                      (GDestroyNotify)g_ptr_array_unref,
1187                                                                      &error);
1188   g_assert_no_error (error);
1189   g_assert (dyna_subtree_registration_id > 0);
1190
1191   /* First assert that we have no nodes in the dynamic subtree */
1192   nodes = get_nodes_at (c, "/foo/dyna");
1193   g_assert (nodes != NULL);
1194   g_assert_cmpint (g_strv_length (nodes), ==, 0);
1195   g_strfreev (nodes);
1196   g_assert_cmpint (count_interfaces (c, "/foo/dyna"), ==, 4);
1197
1198   /* Install three nodes in the dynamic subtree via the dyna_data backdoor and
1199    * assert that they show up correctly in the introspection data */
1200   g_ptr_array_add (dyna_data, "lol");
1201   g_ptr_array_add (dyna_data, "cat");
1202   g_ptr_array_add (dyna_data, "cheezburger");
1203   nodes = get_nodes_at (c, "/foo/dyna");
1204   g_assert (nodes != NULL);
1205   g_assert_cmpint (g_strv_length (nodes), ==, 3);
1206   g_assert_cmpstr (nodes[0], ==, "lol");
1207   g_assert_cmpstr (nodes[1], ==, "cat");
1208   g_assert_cmpstr (nodes[2], ==, "cheezburger");
1209   g_strfreev (nodes);
1210   g_assert_cmpint (count_interfaces (c, "/foo/dyna/lol"), ==, 4);
1211   g_assert_cmpint (count_interfaces (c, "/foo/dyna/cat"), ==, 4);
1212   g_assert_cmpint (count_interfaces (c, "/foo/dyna/cheezburger"), ==, 4);
1213
1214   /* Call a non-existing object path and assert that it has been created */
1215   dyna_create (c, "dynamicallycreated");
1216   nodes = get_nodes_at (c, "/foo/dyna");
1217   g_assert (nodes != NULL);
1218   g_assert_cmpint (g_strv_length (nodes), ==, 4);
1219   g_assert_cmpstr (nodes[0], ==, "lol");
1220   g_assert_cmpstr (nodes[1], ==, "cat");
1221   g_assert_cmpstr (nodes[2], ==, "cheezburger");
1222   g_assert_cmpstr (nodes[3], ==, "dynamicallycreated");
1223   g_strfreev (nodes);
1224   g_assert_cmpint (count_interfaces (c, "/foo/dyna/dynamicallycreated"), ==, 4);
1225
1226   /* now check that the object hierarachy is properly generated... yes, it's a bit
1227    * perverse that we round-trip to the bus to introspect ourselves ;-)
1228    */
1229   nodes = get_nodes_at (c, "/");
1230   g_assert (nodes != NULL);
1231   g_assert_cmpint (g_strv_length (nodes), ==, 1);
1232   g_assert_cmpstr (nodes[0], ==, "foo");
1233   g_strfreev (nodes);
1234   g_assert_cmpint (count_interfaces (c, "/"), ==, 0);
1235
1236   nodes = get_nodes_at (c, "/foo");
1237   g_assert (nodes != NULL);
1238   g_assert_cmpint (g_strv_length (nodes), ==, 2);
1239   g_assert_cmpstr (nodes[0], ==, "boss");
1240   g_assert_cmpstr (nodes[1], ==, "dyna");
1241   g_strfreev (nodes);
1242   g_assert_cmpint (count_interfaces (c, "/foo"), ==, 0);
1243
1244   nodes = get_nodes_at (c, "/foo/boss");
1245   g_assert (nodes != NULL);
1246   g_assert_cmpint (g_strv_length (nodes), ==, 4);
1247   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker1"));
1248   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker2"));
1249   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "interns"));
1250   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "executives"));
1251   g_strfreev (nodes);
1252   /* any registered object always implement org.freedesktop.DBus.[Peer,Introspectable,Properties] */
1253   g_assert_cmpint (count_interfaces (c, "/foo/boss"), ==, 5);
1254   g_assert (has_interface (c, "/foo/boss", foo_interface_info.name));
1255   g_assert (has_interface (c, "/foo/boss", bar_interface_info.name));
1256
1257   /* check subtree nodes - we should have only non_subtree_object in /foo/boss/executives
1258    * because data.num_subtree_nodes is 0
1259    */
1260   nodes = get_nodes_at (c, "/foo/boss/executives");
1261   g_assert (nodes != NULL);
1262   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1263   g_assert_cmpint (g_strv_length (nodes), ==, 1);
1264   g_strfreev (nodes);
1265   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives"), ==, 0);
1266
1267   /* now change data.num_subtree_nodes and check */
1268   data.num_subtree_nodes = 2;
1269   nodes = get_nodes_at (c, "/foo/boss/executives");
1270   g_assert (nodes != NULL);
1271   g_assert_cmpint (g_strv_length (nodes), ==, 5);
1272   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1273   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp0"));
1274   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp1"));
1275   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp0"));
1276   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp1"));
1277   /* check that /foo/boss/executives/non_subtree_object is not handled by the
1278    * subtree handlers - we can do this because objects from subtree handlers
1279    * has exactly one interface and non_subtree_object has two
1280    */
1281   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/non_subtree_object"), ==, 5);
1282   g_assert (has_interface (c, "/foo/boss/executives/non_subtree_object", foo_interface_info.name));
1283   g_assert (has_interface (c, "/foo/boss/executives/non_subtree_object", bar_interface_info.name));
1284   /* check that the vp and evp objects are handled by the subtree handlers */
1285   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/vp0"), ==, 4);
1286   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/vp1"), ==, 4);
1287   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/evp0"), ==, 4);
1288   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/evp1"), ==, 4);
1289   g_assert (has_interface (c, "/foo/boss/executives/vp0", foo_interface_info.name));
1290   g_assert (has_interface (c, "/foo/boss/executives/vp1", foo_interface_info.name));
1291   g_assert (has_interface (c, "/foo/boss/executives/evp0", bar_interface_info.name));
1292   g_assert (has_interface (c, "/foo/boss/executives/evp1", bar_interface_info.name));
1293   g_strfreev (nodes);
1294   data.num_subtree_nodes = 3;
1295   nodes = get_nodes_at (c, "/foo/boss/executives");
1296   g_assert (nodes != NULL);
1297   g_assert_cmpint (g_strv_length (nodes), ==, 7);
1298   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1299   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp0"));
1300   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp1"));
1301   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp2"));
1302   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp0"));
1303   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp1"));
1304   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp2"));
1305   g_strfreev (nodes);
1306
1307   /* check that calls are properly dispatched to the functions in foo_vtable for objects
1308    * implementing the org.example.Foo interface
1309    *
1310    * We do this for both a regular registered object (/foo/boss) and also for an object
1311    * registered through the subtree mechanism.
1312    */
1313   test_dispatch ("/foo/boss");
1314   test_dispatch ("/foo/boss/executives/vp0");
1315
1316   /* To prevent from exiting and attaching a D-Bus tool like D-Feet; uncomment: */
1317 #if 0
1318   g_debug ("Point D-feet or other tool at: %s", session_bus_get_temporary_address());
1319   g_main_loop_run (loop);
1320 #endif
1321
1322   /* check that unregistering the subtree handler works */
1323   g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 1);
1324   g_assert (g_dbus_connection_unregister_subtree (c, subtree_registration_id));
1325   g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 2);
1326   nodes = get_nodes_at (c, "/foo/boss/executives");
1327   g_assert (nodes != NULL);
1328   g_assert_cmpint (g_strv_length (nodes), ==, 1);
1329   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1330   g_strfreev (nodes);
1331
1332   g_assert (g_dbus_connection_unregister_object (c, boss_foo_reg_id));
1333   g_assert (g_dbus_connection_unregister_object (c, boss_bar_reg_id));
1334   g_assert (g_dbus_connection_unregister_object (c, worker1_foo_reg_id));
1335   g_assert (g_dbus_connection_unregister_object (c, worker2_bar_reg_id));
1336   g_assert (g_dbus_connection_unregister_object (c, intern1_foo_reg_id));
1337   g_assert (g_dbus_connection_unregister_object (c, intern2_bar_reg_id));
1338   g_assert (g_dbus_connection_unregister_object (c, intern2_foo_reg_id));
1339   g_assert (g_dbus_connection_unregister_object (c, intern3_bar_reg_id));
1340   g_assert (g_dbus_connection_unregister_object (c, non_subtree_object_path_bar_reg_id));
1341   g_assert (g_dbus_connection_unregister_object (c, non_subtree_object_path_foo_reg_id));
1342
1343   g_assert_cmpint (data.num_unregistered_calls, ==, num_successful_registrations);
1344
1345   /* check that we no longer export any objects - TODO: it looks like there's a bug in
1346    * libdbus-1 here: libdbus still reports the '/foo' object; so disable the test for now
1347    */
1348 #if 0
1349   nodes = get_nodes_at (c, "/");
1350   g_assert (nodes != NULL);
1351   g_assert_cmpint (g_strv_length (nodes), ==, 0);
1352   g_strfreev (nodes);
1353 #endif
1354
1355   g_object_unref (c);
1356 }
1357
1358
1359 /* ---------------------------------------------------------------------------------------------------- */
1360
1361 int
1362 main (int   argc,
1363       char *argv[])
1364 {
1365   gint ret;
1366
1367   g_type_init ();
1368   g_test_init (&argc, &argv, NULL);
1369
1370   /* all the tests rely on a shared main loop */
1371   loop = g_main_loop_new (NULL, FALSE);
1372
1373   /* all the tests use a session bus with a well-known address that we can bring up and down
1374    * using session_bus_up() and session_bus_down().
1375    */
1376   g_unsetenv ("DISPLAY");
1377   g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
1378
1379   session_bus_up ();
1380
1381   /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
1382    * until one can connect to the bus but that's not how things work right now
1383    */
1384   usleep (500 * 1000);
1385
1386   g_test_add_func ("/gdbus/object-registration", test_object_registration);
1387   /* TODO: check that we spit out correct introspection data */
1388   /* TODO: check that registering a whole subtree works */
1389
1390   ret = g_test_run();
1391
1392   /* tear down bus */
1393   session_bus_down ();
1394
1395   return ret;
1396 }