Return an error for calls into unknown interfaces or unknown objects
[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   error = NULL;
828   value = g_dbus_proxy_call_sync (foo_proxy,
829                                   "org.example.FooXYZ.NonExistant",
830                                   NULL,
831                                   G_DBUS_CALL_FLAGS_NONE,
832                                   -1,
833                                   NULL,
834                                   &error);
835   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
836   g_error_free (error);
837   g_assert (value == NULL);
838
839   /* user properties */
840   error = NULL;
841   value = g_dbus_proxy_call_sync (foo_proxy,
842                                   "org.freedesktop.DBus.Properties.Get",
843                                   g_variant_new ("(ss)",
844                                                  "org.example.Foo",
845                                                  "PropertyUno"),
846                                   G_DBUS_CALL_FLAGS_NONE,
847                                   -1,
848                                   NULL,
849                                   &error);
850   g_assert_no_error (error);
851   g_assert (value != NULL);
852   g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("(v)")));
853   g_variant_get (value, "(v)", &inner);
854   g_assert (g_variant_is_of_type (inner, G_VARIANT_TYPE_STRING));
855   g_assert_cmpstr (g_variant_get_string (inner, NULL), ==, "Property `PropertyUno' Is What It Is!");
856   g_variant_unref (value);
857
858   error = NULL;
859   value = g_dbus_proxy_call_sync (foo_proxy,
860                                   "org.freedesktop.DBus.Properties.Get",
861                                   g_variant_new ("(ss)",
862                                                  "org.example.Foo",
863                                                  "ThisDoesntExist"),
864                                   G_DBUS_CALL_FLAGS_NONE,
865                                   -1,
866                                   NULL,
867                                   &error);
868   g_assert (value == NULL);
869   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
870   g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: No such property `ThisDoesntExist'");
871   g_error_free (error);
872
873   error = NULL;
874   value = g_dbus_proxy_call_sync (foo_proxy,
875                                   "org.freedesktop.DBus.Properties.Get",
876                                   g_variant_new ("(ss)",
877                                                  "org.example.Foo",
878                                                  "NotReadable"),
879                                   G_DBUS_CALL_FLAGS_NONE,
880                                   -1,
881                                   NULL,
882                                   &error);
883   g_assert (value == NULL);
884   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
885   g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Property `NotReadable' is not readable");
886   g_error_free (error);
887
888   error = NULL;
889   value = g_dbus_proxy_call_sync (foo_proxy,
890                                   "org.freedesktop.DBus.Properties.Set",
891                                   g_variant_new ("(ssv)",
892                                                  "org.example.Foo",
893                                                  "NotReadable",
894                                                  g_variant_new_string ("But Writable you are!")),
895                                   G_DBUS_CALL_FLAGS_NONE,
896                                   -1,
897                                   NULL,
898                                   &error);
899   g_assert (value == NULL);
900   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_SPAWN_FILE_INVALID);
901   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!''");
902   g_error_free (error);
903
904   error = NULL;
905   value = g_dbus_proxy_call_sync (foo_proxy,
906                                   "org.freedesktop.DBus.Properties.Set",
907                                   g_variant_new ("(ssv)",
908                                                  "org.example.Foo",
909                                                  "NotWritable",
910                                                  g_variant_new_uint32 (42)),
911                                   G_DBUS_CALL_FLAGS_NONE,
912                                   -1,
913                                   NULL,
914                                   &error);
915   g_assert (value == NULL);
916   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
917   g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Property `NotWritable' is not writable");
918   g_error_free (error);
919
920   error = NULL;
921   value = g_dbus_proxy_call_sync (foo_proxy,
922                                   "org.freedesktop.DBus.Properties.GetAll",
923                                   g_variant_new ("(s)",
924                                                  "org.example.Foo"),
925                                   G_DBUS_CALL_FLAGS_NONE,
926                                   -1,
927                                   NULL,
928                                   &error);
929   g_assert_no_error (error);
930   g_assert (value != NULL);
931   g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("(a{sv})")));
932   s = g_variant_print (value, TRUE);
933   g_assert_cmpstr (s, ==, "({'PropertyUno': <\"Property `PropertyUno' Is What It Is!\">, 'NotWritable': <\"Property `NotWritable' Is What It Is!\">},)");
934   g_free (s);
935   g_variant_unref (value);
936
937   g_object_unref (foo_proxy);
938
939   g_main_loop_quit (loop);
940   return NULL;
941 }
942
943 static void
944 test_dispatch (const gchar *object_path)
945 {
946   GThread *thread;
947   GError *error;
948
949   /* run this in a thread to avoid deadlocks */
950   error = NULL;
951   thread = g_thread_create (test_dispatch_thread_func,
952                             (gpointer) object_path,
953                             TRUE,
954                             &error);
955   g_assert_no_error (error);
956   g_assert (thread != NULL);
957   g_main_loop_run (loop);
958   g_thread_join (thread);
959 }
960
961 static void
962 test_object_registration (void)
963 {
964   GError *error;
965   ObjectRegistrationData data;
966   GPtrArray *dyna_data;
967   gchar **nodes;
968   guint boss_foo_reg_id;
969   guint boss_bar_reg_id;
970   guint worker1_foo_reg_id;
971   guint worker2_bar_reg_id;
972   guint intern1_foo_reg_id;
973   guint intern2_bar_reg_id;
974   guint intern2_foo_reg_id;
975   guint intern3_bar_reg_id;
976   guint registration_id;
977   guint subtree_registration_id;
978   guint non_subtree_object_path_foo_reg_id;
979   guint non_subtree_object_path_bar_reg_id;
980   guint dyna_subtree_registration_id;
981   guint num_successful_registrations;
982
983   data.num_unregistered_calls = 0;
984   data.num_unregistered_subtree_calls = 0;
985   data.num_subtree_nodes = 0;
986
987   num_successful_registrations = 0;
988
989   error = NULL;
990   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
991   g_assert_no_error (error);
992   g_assert (c != NULL);
993
994   registration_id = g_dbus_connection_register_object (c,
995                                                        "/foo/boss",
996                                                        &foo_interface_info,
997                                                        &foo_vtable,
998                                                        &data,
999                                                        on_object_unregistered,
1000                                                        &error);
1001   g_assert_no_error (error);
1002   g_assert (registration_id > 0);
1003   boss_foo_reg_id = registration_id;
1004   num_successful_registrations++;
1005
1006   registration_id = g_dbus_connection_register_object (c,
1007                                                        "/foo/boss",
1008                                                        &bar_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   boss_bar_reg_id = registration_id;
1016   num_successful_registrations++;
1017
1018   registration_id = g_dbus_connection_register_object (c,
1019                                                        "/foo/boss/worker1",
1020                                                        &foo_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   worker1_foo_reg_id = registration_id;
1028   num_successful_registrations++;
1029
1030   registration_id = g_dbus_connection_register_object (c,
1031                                                        "/foo/boss/worker2",
1032                                                        &bar_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   worker2_bar_reg_id = registration_id;
1040   num_successful_registrations++;
1041
1042   registration_id = g_dbus_connection_register_object (c,
1043                                                        "/foo/boss/interns/intern1",
1044                                                        &foo_interface_info,
1045                                                        NULL,
1046                                                        &data,
1047                                                        on_object_unregistered,
1048                                                        &error);
1049   g_assert_no_error (error);
1050   g_assert (registration_id > 0);
1051   intern1_foo_reg_id = registration_id;
1052   num_successful_registrations++;
1053
1054   /* ... and try again at another path */
1055   registration_id = g_dbus_connection_register_object (c,
1056                                                        "/foo/boss/interns/intern2",
1057                                                        &bar_interface_info,
1058                                                        NULL,
1059                                                        &data,
1060                                                        on_object_unregistered,
1061                                                        &error);
1062   g_assert_no_error (error);
1063   g_assert (registration_id > 0);
1064   intern2_bar_reg_id = registration_id;
1065   num_successful_registrations++;
1066
1067   /* register at the same path/interface - this should fail */
1068   registration_id = g_dbus_connection_register_object (c,
1069                                                        "/foo/boss/interns/intern2",
1070                                                        &bar_interface_info,
1071                                                        NULL,
1072                                                        &data,
1073                                                        on_object_unregistered,
1074                                                        &error);
1075   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
1076   g_assert (!g_dbus_error_is_remote_error (error));
1077   g_error_free (error);
1078   error = NULL;
1079   g_assert (registration_id == 0);
1080
1081   /* register at different interface - shouldn't fail */
1082   registration_id = g_dbus_connection_register_object (c,
1083                                                        "/foo/boss/interns/intern2",
1084                                                        &foo_interface_info,
1085                                                        NULL,
1086                                                        &data,
1087                                                        on_object_unregistered,
1088                                                        &error);
1089   g_assert_no_error (error);
1090   g_assert (registration_id > 0);
1091   intern2_foo_reg_id = registration_id;
1092   num_successful_registrations++;
1093
1094   /* unregister it via the id */
1095   g_assert (g_dbus_connection_unregister_object (c, registration_id));
1096   g_assert_cmpint (data.num_unregistered_calls, ==, 1);
1097   intern2_foo_reg_id = 0;
1098
1099   /* register it back */
1100   registration_id = g_dbus_connection_register_object (c,
1101                                                        "/foo/boss/interns/intern2",
1102                                                        &foo_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   intern2_foo_reg_id = registration_id;
1110   num_successful_registrations++;
1111
1112   registration_id = g_dbus_connection_register_object (c,
1113                                                        "/foo/boss/interns/intern3",
1114                                                        &bar_interface_info,
1115                                                        NULL,
1116                                                        &data,
1117                                                        on_object_unregistered,
1118                                                        &error);
1119   g_assert_no_error (error);
1120   g_assert (registration_id > 0);
1121   intern3_bar_reg_id = registration_id;
1122   num_successful_registrations++;
1123
1124   /* now register a whole subtree at /foo/boss/executives */
1125   subtree_registration_id = g_dbus_connection_register_subtree (c,
1126                                                                 "/foo/boss/executives",
1127                                                                 &subtree_vtable,
1128                                                                 G_DBUS_SUBTREE_FLAGS_NONE,
1129                                                                 &data,
1130                                                                 on_subtree_unregistered,
1131                                                                 &error);
1132   g_assert_no_error (error);
1133   g_assert (subtree_registration_id > 0);
1134   /* try registering it again.. this should fail */
1135   registration_id = g_dbus_connection_register_subtree (c,
1136                                                         "/foo/boss/executives",
1137                                                         &subtree_vtable,
1138                                                         G_DBUS_SUBTREE_FLAGS_NONE,
1139                                                         &data,
1140                                                         on_subtree_unregistered,
1141                                                         &error);
1142   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
1143   g_assert (!g_dbus_error_is_remote_error (error));
1144   g_error_free (error);
1145   error = NULL;
1146   g_assert (registration_id == 0);
1147
1148   /* unregister it, then register it again */
1149   g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 0);
1150   g_assert (g_dbus_connection_unregister_subtree (c, subtree_registration_id));
1151   g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 1);
1152   subtree_registration_id = g_dbus_connection_register_subtree (c,
1153                                                                 "/foo/boss/executives",
1154                                                                 &subtree_vtable,
1155                                                                 G_DBUS_SUBTREE_FLAGS_NONE,
1156                                                                 &data,
1157                                                                 on_subtree_unregistered,
1158                                                                 &error);
1159   g_assert_no_error (error);
1160   g_assert (subtree_registration_id > 0);
1161
1162   /* try to register something under /foo/boss/executives - this should work
1163    * because registered subtrees and registered objects can coexist.
1164    *
1165    * Make the exported object implement *two* interfaces so we can check
1166    * that the right introspection handler is invoked.
1167    */
1168   registration_id = g_dbus_connection_register_object (c,
1169                                                        "/foo/boss/executives/non_subtree_object",
1170                                                        &bar_interface_info,
1171                                                        NULL,
1172                                                        &data,
1173                                                        on_object_unregistered,
1174                                                        &error);
1175   g_assert_no_error (error);
1176   g_assert (registration_id > 0);
1177   non_subtree_object_path_bar_reg_id = registration_id;
1178   num_successful_registrations++;
1179   registration_id = g_dbus_connection_register_object (c,
1180                                                        "/foo/boss/executives/non_subtree_object",
1181                                                        &foo_interface_info,
1182                                                        NULL,
1183                                                        &data,
1184                                                        on_object_unregistered,
1185                                                        &error);
1186   g_assert_no_error (error);
1187   g_assert (registration_id > 0);
1188   non_subtree_object_path_foo_reg_id = registration_id;
1189   num_successful_registrations++;
1190
1191   /* now register a dynamic subtree, spawning objects as they are called */
1192   dyna_data = g_ptr_array_new ();
1193   dyna_subtree_registration_id = g_dbus_connection_register_subtree (c,
1194                                                                      "/foo/dyna",
1195                                                                      &dynamic_subtree_vtable,
1196                                                                      G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES,
1197                                                                      dyna_data,
1198                                                                      (GDestroyNotify)g_ptr_array_unref,
1199                                                                      &error);
1200   g_assert_no_error (error);
1201   g_assert (dyna_subtree_registration_id > 0);
1202
1203   /* First assert that we have no nodes in the dynamic subtree */
1204   nodes = get_nodes_at (c, "/foo/dyna");
1205   g_assert (nodes != NULL);
1206   g_assert_cmpint (g_strv_length (nodes), ==, 0);
1207   g_strfreev (nodes);
1208   g_assert_cmpint (count_interfaces (c, "/foo/dyna"), ==, 4);
1209
1210   /* Install three nodes in the dynamic subtree via the dyna_data backdoor and
1211    * assert that they show up correctly in the introspection data */
1212   g_ptr_array_add (dyna_data, "lol");
1213   g_ptr_array_add (dyna_data, "cat");
1214   g_ptr_array_add (dyna_data, "cheezburger");
1215   nodes = get_nodes_at (c, "/foo/dyna");
1216   g_assert (nodes != NULL);
1217   g_assert_cmpint (g_strv_length (nodes), ==, 3);
1218   g_assert_cmpstr (nodes[0], ==, "lol");
1219   g_assert_cmpstr (nodes[1], ==, "cat");
1220   g_assert_cmpstr (nodes[2], ==, "cheezburger");
1221   g_strfreev (nodes);
1222   g_assert_cmpint (count_interfaces (c, "/foo/dyna/lol"), ==, 4);
1223   g_assert_cmpint (count_interfaces (c, "/foo/dyna/cat"), ==, 4);
1224   g_assert_cmpint (count_interfaces (c, "/foo/dyna/cheezburger"), ==, 4);
1225
1226   /* Call a non-existing object path and assert that it has been created */
1227   dyna_create (c, "dynamicallycreated");
1228   nodes = get_nodes_at (c, "/foo/dyna");
1229   g_assert (nodes != NULL);
1230   g_assert_cmpint (g_strv_length (nodes), ==, 4);
1231   g_assert_cmpstr (nodes[0], ==, "lol");
1232   g_assert_cmpstr (nodes[1], ==, "cat");
1233   g_assert_cmpstr (nodes[2], ==, "cheezburger");
1234   g_assert_cmpstr (nodes[3], ==, "dynamicallycreated");
1235   g_strfreev (nodes);
1236   g_assert_cmpint (count_interfaces (c, "/foo/dyna/dynamicallycreated"), ==, 4);
1237
1238   /* now check that the object hierarachy is properly generated... yes, it's a bit
1239    * perverse that we round-trip to the bus to introspect ourselves ;-)
1240    */
1241   nodes = get_nodes_at (c, "/");
1242   g_assert (nodes != NULL);
1243   g_assert_cmpint (g_strv_length (nodes), ==, 1);
1244   g_assert_cmpstr (nodes[0], ==, "foo");
1245   g_strfreev (nodes);
1246   g_assert_cmpint (count_interfaces (c, "/"), ==, 0);
1247
1248   nodes = get_nodes_at (c, "/foo");
1249   g_assert (nodes != NULL);
1250   g_assert_cmpint (g_strv_length (nodes), ==, 2);
1251   g_assert_cmpstr (nodes[0], ==, "boss");
1252   g_assert_cmpstr (nodes[1], ==, "dyna");
1253   g_strfreev (nodes);
1254   g_assert_cmpint (count_interfaces (c, "/foo"), ==, 0);
1255
1256   nodes = get_nodes_at (c, "/foo/boss");
1257   g_assert (nodes != NULL);
1258   g_assert_cmpint (g_strv_length (nodes), ==, 4);
1259   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker1"));
1260   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker2"));
1261   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "interns"));
1262   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "executives"));
1263   g_strfreev (nodes);
1264   /* any registered object always implement org.freedesktop.DBus.[Peer,Introspectable,Properties] */
1265   g_assert_cmpint (count_interfaces (c, "/foo/boss"), ==, 5);
1266   g_assert (has_interface (c, "/foo/boss", foo_interface_info.name));
1267   g_assert (has_interface (c, "/foo/boss", bar_interface_info.name));
1268
1269   /* check subtree nodes - we should have only non_subtree_object in /foo/boss/executives
1270    * because data.num_subtree_nodes is 0
1271    */
1272   nodes = get_nodes_at (c, "/foo/boss/executives");
1273   g_assert (nodes != NULL);
1274   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1275   g_assert_cmpint (g_strv_length (nodes), ==, 1);
1276   g_strfreev (nodes);
1277   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives"), ==, 0);
1278
1279   /* now change data.num_subtree_nodes and check */
1280   data.num_subtree_nodes = 2;
1281   nodes = get_nodes_at (c, "/foo/boss/executives");
1282   g_assert (nodes != NULL);
1283   g_assert_cmpint (g_strv_length (nodes), ==, 5);
1284   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1285   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp0"));
1286   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp1"));
1287   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp0"));
1288   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp1"));
1289   /* check that /foo/boss/executives/non_subtree_object is not handled by the
1290    * subtree handlers - we can do this because objects from subtree handlers
1291    * has exactly one interface and non_subtree_object has two
1292    */
1293   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/non_subtree_object"), ==, 5);
1294   g_assert (has_interface (c, "/foo/boss/executives/non_subtree_object", foo_interface_info.name));
1295   g_assert (has_interface (c, "/foo/boss/executives/non_subtree_object", bar_interface_info.name));
1296   /* check that the vp and evp objects are handled by the subtree handlers */
1297   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/vp0"), ==, 4);
1298   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/vp1"), ==, 4);
1299   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/evp0"), ==, 4);
1300   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/evp1"), ==, 4);
1301   g_assert (has_interface (c, "/foo/boss/executives/vp0", foo_interface_info.name));
1302   g_assert (has_interface (c, "/foo/boss/executives/vp1", foo_interface_info.name));
1303   g_assert (has_interface (c, "/foo/boss/executives/evp0", bar_interface_info.name));
1304   g_assert (has_interface (c, "/foo/boss/executives/evp1", bar_interface_info.name));
1305   g_strfreev (nodes);
1306   data.num_subtree_nodes = 3;
1307   nodes = get_nodes_at (c, "/foo/boss/executives");
1308   g_assert (nodes != NULL);
1309   g_assert_cmpint (g_strv_length (nodes), ==, 7);
1310   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1311   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp0"));
1312   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp1"));
1313   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp2"));
1314   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp0"));
1315   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp1"));
1316   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp2"));
1317   g_strfreev (nodes);
1318
1319   /* check that calls are properly dispatched to the functions in foo_vtable for objects
1320    * implementing the org.example.Foo interface
1321    *
1322    * We do this for both a regular registered object (/foo/boss) and also for an object
1323    * registered through the subtree mechanism.
1324    */
1325   test_dispatch ("/foo/boss");
1326   test_dispatch ("/foo/boss/executives/vp0");
1327
1328   /* To prevent from exiting and attaching a D-Bus tool like D-Feet; uncomment: */
1329 #if 0
1330   g_debug ("Point D-feet or other tool at: %s", session_bus_get_temporary_address());
1331   g_main_loop_run (loop);
1332 #endif
1333
1334   /* check that unregistering the subtree handler works */
1335   g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 1);
1336   g_assert (g_dbus_connection_unregister_subtree (c, subtree_registration_id));
1337   g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 2);
1338   nodes = get_nodes_at (c, "/foo/boss/executives");
1339   g_assert (nodes != NULL);
1340   g_assert_cmpint (g_strv_length (nodes), ==, 1);
1341   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1342   g_strfreev (nodes);
1343
1344   g_assert (g_dbus_connection_unregister_object (c, boss_foo_reg_id));
1345   g_assert (g_dbus_connection_unregister_object (c, boss_bar_reg_id));
1346   g_assert (g_dbus_connection_unregister_object (c, worker1_foo_reg_id));
1347   g_assert (g_dbus_connection_unregister_object (c, worker2_bar_reg_id));
1348   g_assert (g_dbus_connection_unregister_object (c, intern1_foo_reg_id));
1349   g_assert (g_dbus_connection_unregister_object (c, intern2_bar_reg_id));
1350   g_assert (g_dbus_connection_unregister_object (c, intern2_foo_reg_id));
1351   g_assert (g_dbus_connection_unregister_object (c, intern3_bar_reg_id));
1352   g_assert (g_dbus_connection_unregister_object (c, non_subtree_object_path_bar_reg_id));
1353   g_assert (g_dbus_connection_unregister_object (c, non_subtree_object_path_foo_reg_id));
1354
1355   g_assert_cmpint (data.num_unregistered_calls, ==, num_successful_registrations);
1356
1357   /* check that we no longer export any objects - TODO: it looks like there's a bug in
1358    * libdbus-1 here: libdbus still reports the '/foo' object; so disable the test for now
1359    */
1360 #if 0
1361   nodes = get_nodes_at (c, "/");
1362   g_assert (nodes != NULL);
1363   g_assert_cmpint (g_strv_length (nodes), ==, 0);
1364   g_strfreev (nodes);
1365 #endif
1366
1367   g_object_unref (c);
1368 }
1369
1370
1371 /* ---------------------------------------------------------------------------------------------------- */
1372
1373 int
1374 main (int   argc,
1375       char *argv[])
1376 {
1377   gint ret;
1378
1379   g_type_init ();
1380   g_test_init (&argc, &argv, NULL);
1381
1382   /* all the tests rely on a shared main loop */
1383   loop = g_main_loop_new (NULL, FALSE);
1384
1385   /* all the tests use a session bus with a well-known address that we can bring up and down
1386    * using session_bus_up() and session_bus_down().
1387    */
1388   g_unsetenv ("DISPLAY");
1389   g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
1390
1391   session_bus_up ();
1392
1393   /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
1394    * until one can connect to the bus but that's not how things work right now
1395    */
1396   usleep (500 * 1000);
1397
1398   g_test_add_func ("/gdbus/object-registration", test_object_registration);
1399   /* TODO: check that we spit out correct introspection data */
1400   /* TODO: check that registering a whole subtree works */
1401
1402   ret = g_test_run();
1403
1404   /* tear down bus */
1405   session_bus_down ();
1406
1407   return ret;
1408 }