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