[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[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, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: David Zeuthen <davidz@redhat.com>
19  */
20
21 #include <gio/gio.h>
22 #include <unistd.h>
23 #include <string.h>
24
25 #include "gdbus-tests.h"
26
27 /* all tests rely on a shared mainloop */
28 static GMainLoop *loop = NULL;
29
30 static GDBusConnection *c = NULL;
31
32 /* ---------------------------------------------------------------------------------------------------- */
33 /* Test that we can export objects, the hierarchy is correct and the right handlers are invoked */
34 /* ---------------------------------------------------------------------------------------------------- */
35
36 static const GDBusArgInfo foo_method1_in_args =
37 {
38   -1,
39   "an_input_string",
40   "s",
41   NULL
42 };
43 static const GDBusArgInfo * const foo_method1_in_arg_pointers[] = {&foo_method1_in_args, NULL};
44
45 static const GDBusArgInfo foo_method1_out_args =
46 {
47   -1,
48   "an_output_string",
49   "s",
50   NULL
51 };
52 static const GDBusArgInfo * const foo_method1_out_arg_pointers[] = {&foo_method1_out_args, NULL};
53
54 static const GDBusMethodInfo foo_method_info_method1 =
55 {
56   -1,
57   "Method1",
58   (GDBusArgInfo **) &foo_method1_in_arg_pointers,
59   (GDBusArgInfo **) &foo_method1_out_arg_pointers,
60   NULL
61 };
62 static const GDBusMethodInfo foo_method_info_method2 =
63 {
64   -1,
65   "Method2",
66   NULL,
67   NULL,
68   NULL
69 };
70 static const GDBusMethodInfo * const foo_method_info_pointers[] = {&foo_method_info_method1, &foo_method_info_method2, NULL};
71
72 static const GDBusSignalInfo foo_signal_info =
73 {
74   -1,
75   "SignalAlpha",
76   NULL,
77   NULL
78 };
79 static const GDBusSignalInfo * const foo_signal_info_pointers[] = {&foo_signal_info, NULL};
80
81 static const GDBusPropertyInfo foo_property_info[] =
82 {
83   {
84     -1,
85     "PropertyUno",
86     "s",
87     G_DBUS_PROPERTY_INFO_FLAGS_READABLE | G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE,
88     NULL
89   },
90   {
91     -1,
92     "NotWritable",
93     "s",
94     G_DBUS_PROPERTY_INFO_FLAGS_READABLE,
95     NULL
96   },
97   {
98     -1,
99     "NotReadable",
100     "s",
101     G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE,
102     NULL
103   }
104 };
105 static const GDBusPropertyInfo * const foo_property_info_pointers[] =
106 {
107   &foo_property_info[0],
108   &foo_property_info[1],
109   &foo_property_info[2],
110   NULL
111 };
112
113 static const GDBusInterfaceInfo foo_interface_info =
114 {
115   -1,
116   "org.example.Foo",
117   (GDBusMethodInfo **) &foo_method_info_pointers,
118   (GDBusSignalInfo **) &foo_signal_info_pointers,
119   (GDBusPropertyInfo **)&foo_property_info_pointers,
120   NULL,
121 };
122
123 /* Foo2 is just Foo without the properties */
124 static const GDBusInterfaceInfo foo2_interface_info =
125 {
126   -1,
127   "org.example.Foo2",
128   (GDBusMethodInfo **) &foo_method_info_pointers,
129   (GDBusSignalInfo **) &foo_signal_info_pointers,
130 };
131
132 static void
133 foo_method_call (GDBusConnection       *connection,
134                  const gchar           *sender,
135                  const gchar           *object_path,
136                  const gchar           *interface_name,
137                  const gchar           *method_name,
138                  GVariant              *parameters,
139                  GDBusMethodInvocation *invocation,
140                  gpointer               user_data)
141 {
142   if (g_strcmp0 (method_name, "Method1") == 0)
143     {
144       const gchar *input;
145       gchar *output;
146       g_variant_get (parameters, "(&s)", &input);
147       output = g_strdup_printf ("You passed the string '%s'. Jolly good!", input);
148       g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", output));
149       g_free (output);
150     }
151   else
152     {
153       g_dbus_method_invocation_return_dbus_error (invocation,
154                                                   "org.example.SomeError",
155                                                   "How do you like them apples, buddy!");
156     }
157 }
158
159 static GVariant *
160 foo_get_property (GDBusConnection       *connection,
161                   const gchar           *sender,
162                   const gchar           *object_path,
163                   const gchar           *interface_name,
164                   const gchar           *property_name,
165                   GError               **error,
166                   gpointer               user_data)
167 {
168   GVariant *ret;
169   gchar *s;
170   s = g_strdup_printf ("Property '%s' Is What It Is!", property_name);
171   ret = g_variant_new_string (s);
172   g_free (s);
173   return ret;
174 }
175
176 static gboolean
177 foo_set_property (GDBusConnection       *connection,
178                   const gchar           *sender,
179                   const gchar           *object_path,
180                   const gchar           *interface_name,
181                   const gchar           *property_name,
182                   GVariant              *value,
183                   GError               **error,
184                   gpointer               user_data)
185 {
186   gchar *s;
187   s = g_variant_print (value, TRUE);
188   g_set_error (error,
189                G_DBUS_ERROR,
190                G_DBUS_ERROR_SPAWN_FILE_INVALID,
191                "Returning some error instead of writing the value '%s' to the property '%s'",
192                property_name, s);
193   g_free (s);
194   return FALSE;
195 }
196
197 static const GDBusInterfaceVTable foo_vtable =
198 {
199   foo_method_call,
200   foo_get_property,
201   foo_set_property
202 };
203
204 /* -------------------- */
205
206 static const GDBusMethodInfo bar_method_info[] =
207 {
208   {
209     -1,
210     "MethodA",
211     NULL,
212     NULL,
213     NULL
214   },
215   {
216     -1,
217     "MethodB",
218     NULL,
219     NULL,
220     NULL
221   }
222 };
223 static const GDBusMethodInfo * const bar_method_info_pointers[] = {&bar_method_info[0], &bar_method_info[1], NULL};
224
225 static const GDBusSignalInfo bar_signal_info[] =
226 {
227   {
228     -1,
229     "SignalMars",
230     NULL,
231     NULL
232   }
233 };
234 static const GDBusSignalInfo * const bar_signal_info_pointers[] = {&bar_signal_info[0], NULL};
235
236 static const GDBusPropertyInfo bar_property_info[] =
237 {
238   {
239     -1,
240     "PropertyDuo",
241     "s",
242     G_DBUS_PROPERTY_INFO_FLAGS_READABLE,
243     NULL
244   }
245 };
246 static const GDBusPropertyInfo * const bar_property_info_pointers[] = {&bar_property_info[0], NULL};
247
248 static const GDBusInterfaceInfo bar_interface_info =
249 {
250   -1,
251   "org.example.Bar",
252   (GDBusMethodInfo **) bar_method_info_pointers,
253   (GDBusSignalInfo **) bar_signal_info_pointers,
254   (GDBusPropertyInfo **) bar_property_info_pointers,
255   NULL,
256 };
257
258 /* -------------------- */
259
260 static const GDBusMethodInfo dyna_method_info[] =
261 {
262   {
263     -1,
264     "DynaCyber",
265     NULL,
266     NULL,
267     NULL
268   }
269 };
270 static const GDBusMethodInfo * const dyna_method_info_pointers[] = {&dyna_method_info[0], NULL};
271
272 static const GDBusInterfaceInfo dyna_interface_info =
273 {
274   -1,
275   "org.example.Dyna",
276   (GDBusMethodInfo **) dyna_method_info_pointers,
277   NULL, /* no signals */
278   NULL, /* no properties */
279   NULL,
280 };
281
282 static void
283 dyna_cyber (GDBusConnection *connection,
284             const gchar *sender,
285             const gchar *object_path,
286             const gchar *interface_name,
287             const gchar *method_name,
288             GVariant *parameters,
289             GDBusMethodInvocation *invocation,
290             gpointer user_data)
291 {
292   GPtrArray *data = user_data;
293   gchar *node_name;
294   guint n;
295
296   node_name = strrchr (object_path, '/') + 1;
297
298   /* Add new node if it is not already known */
299   for (n = 0; n < data->len ; n++)
300     {
301       if (g_strcmp0 (g_ptr_array_index (data, n), node_name) == 0)
302         goto out;
303     }
304   g_ptr_array_add (data, g_strdup(node_name));
305
306   out:
307     g_dbus_method_invocation_return_value (invocation, NULL);
308 }
309
310 static const GDBusInterfaceVTable dyna_interface_vtable =
311 {
312   dyna_cyber,
313   NULL,
314   NULL
315 };
316
317 /* ---------------------------------------------------------------------------------------------------- */
318
319 static void
320 introspect_callback (GDBusProxy   *proxy,
321                      GAsyncResult *res,
322                      gpointer      user_data)
323 {
324   gchar **xml_data = user_data;
325   GVariant *result;
326   GError *error;
327
328   error = NULL;
329   result = g_dbus_proxy_call_finish (proxy,
330                                               res,
331                                               &error);
332   g_assert_no_error (error);
333   g_assert (result != NULL);
334   g_variant_get (result, "(s)", xml_data);
335   g_variant_unref (result);
336
337   g_main_loop_quit (loop);
338 }
339
340 static gchar **
341 get_nodes_at (GDBusConnection  *c,
342               const gchar      *object_path)
343 {
344   GError *error;
345   GDBusProxy *proxy;
346   gchar *xml_data;
347   GPtrArray *p;
348   GDBusNodeInfo *node_info;
349   guint n;
350
351   error = NULL;
352   proxy = g_dbus_proxy_new_sync (c,
353                                  G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
354                                  G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
355                                  NULL,
356                                  g_dbus_connection_get_unique_name (c),
357                                  object_path,
358                                  "org.freedesktop.DBus.Introspectable",
359                                  NULL,
360                                  &error);
361   g_assert_no_error (error);
362   g_assert (proxy != NULL);
363
364   /* do this async to avoid libdbus-1 deadlocks */
365   xml_data = NULL;
366   g_dbus_proxy_call (proxy,
367                      "Introspect",
368                      NULL,
369                      G_DBUS_CALL_FLAGS_NONE,
370                      -1,
371                      NULL,
372                      (GAsyncReadyCallback) introspect_callback,
373                      &xml_data);
374   g_main_loop_run (loop);
375   g_assert (xml_data != NULL);
376
377   node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
378   g_assert_no_error (error);
379   g_assert (node_info != NULL);
380
381   p = g_ptr_array_new ();
382   for (n = 0; node_info->nodes != NULL && node_info->nodes[n] != NULL; n++)
383     {
384       const GDBusNodeInfo *sub_node_info = node_info->nodes[n];
385       g_ptr_array_add (p, g_strdup (sub_node_info->path));
386     }
387   g_ptr_array_add (p, NULL);
388
389   g_object_unref (proxy);
390   g_free (xml_data);
391   g_dbus_node_info_unref (node_info);
392
393   return (gchar **) g_ptr_array_free (p, FALSE);
394 }
395
396 static gboolean
397 has_interface (GDBusConnection *c,
398                const gchar     *object_path,
399                const gchar     *interface_name)
400 {
401   GError *error;
402   GDBusProxy *proxy;
403   gchar *xml_data;
404   GDBusNodeInfo *node_info;
405   gboolean ret;
406
407   error = NULL;
408   proxy = g_dbus_proxy_new_sync (c,
409                                  G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
410                                  G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
411                                  NULL,
412                                  g_dbus_connection_get_unique_name (c),
413                                  object_path,
414                                  "org.freedesktop.DBus.Introspectable",
415                                  NULL,
416                                  &error);
417   g_assert_no_error (error);
418   g_assert (proxy != NULL);
419
420   /* do this async to avoid libdbus-1 deadlocks */
421   xml_data = NULL;
422   g_dbus_proxy_call (proxy,
423                      "Introspect",
424                      NULL,
425                      G_DBUS_CALL_FLAGS_NONE,
426                      -1,
427                      NULL,
428                      (GAsyncReadyCallback) introspect_callback,
429                      &xml_data);
430   g_main_loop_run (loop);
431   g_assert (xml_data != NULL);
432
433   node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
434   g_assert_no_error (error);
435   g_assert (node_info != NULL);
436
437   ret = (g_dbus_node_info_lookup_interface (node_info, interface_name) != NULL);
438
439   g_object_unref (proxy);
440   g_free (xml_data);
441   g_dbus_node_info_unref (node_info);
442
443   return ret;
444 }
445
446 static guint
447 count_interfaces (GDBusConnection *c,
448                   const gchar     *object_path)
449 {
450   GError *error;
451   GDBusProxy *proxy;
452   gchar *xml_data;
453   GDBusNodeInfo *node_info;
454   guint ret;
455
456   error = NULL;
457   proxy = g_dbus_proxy_new_sync (c,
458                                  G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
459                                  G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
460                                  NULL,
461                                  g_dbus_connection_get_unique_name (c),
462                                  object_path,
463                                  "org.freedesktop.DBus.Introspectable",
464                                  NULL,
465                                  &error);
466   g_assert_no_error (error);
467   g_assert (proxy != NULL);
468
469   /* do this async to avoid libdbus-1 deadlocks */
470   xml_data = NULL;
471   g_dbus_proxy_call (proxy,
472                      "Introspect",
473                      NULL,
474                      G_DBUS_CALL_FLAGS_NONE,
475                      -1,
476                      NULL,
477                      (GAsyncReadyCallback) introspect_callback,
478                      &xml_data);
479   g_main_loop_run (loop);
480   g_assert (xml_data != NULL);
481
482   node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
483   g_assert_no_error (error);
484   g_assert (node_info != NULL);
485
486   ret = 0;
487   while (node_info->interfaces != NULL && node_info->interfaces[ret] != NULL)
488     ret++;
489
490   g_object_unref (proxy);
491   g_free (xml_data);
492   g_dbus_node_info_unref (node_info);
493
494   return ret;
495 }
496
497 static void
498 dyna_create_callback (GDBusProxy   *proxy,
499                       GAsyncResult  *res,
500                       gpointer      user_data)
501 {
502   GVariant *result;
503   GError *error;
504
505   error = NULL;
506   result = g_dbus_proxy_call_finish (proxy,
507                                      res,
508                                      &error);
509   g_assert_no_error (error);
510   g_assert (result != NULL);
511   g_variant_unref (result);
512
513   g_main_loop_quit (loop);
514 }
515
516 /* Dynamically create @object_name under /foo/dyna */
517 static void
518 dyna_create (GDBusConnection *c,
519              const gchar     *object_name)
520 {
521   GError *error;
522   GDBusProxy *proxy;
523   gchar *object_path;
524
525   object_path = g_strconcat ("/foo/dyna/", object_name, NULL);
526
527   error = NULL;
528   proxy = g_dbus_proxy_new_sync (c,
529                                  G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
530                                  G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
531                                  NULL,
532                                  g_dbus_connection_get_unique_name (c),
533                                  object_path,
534                                  "org.example.Dyna",
535                                  NULL,
536                                  &error);
537   g_assert_no_error (error);
538   g_assert (proxy != NULL);
539
540   /* do this async to avoid libdbus-1 deadlocks */
541   g_dbus_proxy_call (proxy,
542                      "DynaCyber",
543                      g_variant_new ("()"),
544                      G_DBUS_CALL_FLAGS_NONE,
545                      -1,
546                      NULL,
547                      (GAsyncReadyCallback) dyna_create_callback,
548                      NULL);
549   g_main_loop_run (loop);
550
551   g_assert_no_error (error);
552
553   g_object_unref (proxy);
554   g_free (object_path);
555
556   return;
557 }
558
559 typedef struct
560 {
561   guint num_unregistered_calls;
562   guint num_unregistered_subtree_calls;
563   guint num_subtree_nodes;
564 } ObjectRegistrationData;
565
566 static void
567 on_object_unregistered (gpointer user_data)
568 {
569   ObjectRegistrationData *data = user_data;
570
571   data->num_unregistered_calls++;
572 }
573
574 static void
575 on_subtree_unregistered (gpointer user_data)
576 {
577   ObjectRegistrationData *data = user_data;
578
579   data->num_unregistered_subtree_calls++;
580 }
581
582 static gboolean
583 _g_strv_has_string (const gchar* const * haystack,
584                     const gchar *needle)
585 {
586   guint n;
587
588   for (n = 0; haystack != NULL && haystack[n] != NULL; n++)
589     {
590       if (g_strcmp0 (haystack[n], needle) == 0)
591         return TRUE;
592     }
593   return FALSE;
594 }
595
596 /* -------------------- */
597
598 static gchar **
599 subtree_enumerate (GDBusConnection       *connection,
600                    const gchar           *sender,
601                    const gchar           *object_path,
602                    gpointer               user_data)
603 {
604   ObjectRegistrationData *data = user_data;
605   GPtrArray *p;
606   gchar **nodes;
607   guint n;
608
609   p = g_ptr_array_new ();
610
611   for (n = 0; n < data->num_subtree_nodes; n++)
612     {
613       g_ptr_array_add (p, g_strdup_printf ("vp%d", n));
614       g_ptr_array_add (p, g_strdup_printf ("evp%d", n));
615     }
616   g_ptr_array_add (p, NULL);
617
618   nodes = (gchar **) g_ptr_array_free (p, FALSE);
619
620   return nodes;
621 }
622
623 /* Only allows certain objects, and aborts on unknowns */
624 static GDBusInterfaceInfo **
625 subtree_introspect (GDBusConnection       *connection,
626                     const gchar           *sender,
627                     const gchar           *object_path,
628                     const gchar           *node,
629                     gpointer               user_data)
630 {
631   const GDBusInterfaceInfo *interfaces[2] = {
632      NULL /* filled in below */, NULL
633   };
634
635   /* VPs implement the Foo interface, EVPs implement the Bar interface. The root
636    * does not implement any interfaces
637    */
638   if (node == NULL)
639     {
640       return NULL;
641     }
642   else if (g_str_has_prefix (node, "vp"))
643     {
644       interfaces[0] = &foo_interface_info;
645     }
646   else if (g_str_has_prefix (node, "evp"))
647     {
648       interfaces[0] = &bar_interface_info;
649     }
650   else
651     {
652       g_assert_not_reached ();
653     }
654
655   return g_memdup (interfaces, 2 * sizeof (void *));
656 }
657
658 static const GDBusInterfaceVTable *
659 subtree_dispatch (GDBusConnection             *connection,
660                   const gchar                 *sender,
661                   const gchar                 *object_path,
662                   const gchar                 *interface_name,
663                   const gchar                 *node,
664                   gpointer                    *out_user_data,
665                   gpointer                     user_data)
666 {
667   if (g_strcmp0 (interface_name, "org.example.Foo") == 0)
668     return &foo_vtable;
669   else
670     return NULL;
671 }
672
673 static const GDBusSubtreeVTable subtree_vtable =
674 {
675   subtree_enumerate,
676   subtree_introspect,
677   subtree_dispatch
678 };
679
680 /* -------------------- */
681
682 static gchar **
683 dynamic_subtree_enumerate (GDBusConnection       *connection,
684                            const gchar           *sender,
685                            const gchar           *object_path,
686                            gpointer               user_data)
687 {
688   GPtrArray *data = user_data;
689   gchar **nodes = g_new (gchar*, data->len + 1);
690   guint n;
691
692   for (n = 0; n < data->len; n++)
693     {
694       nodes[n] = g_strdup (g_ptr_array_index (data, n));
695     }
696   nodes[data->len] = NULL;
697
698   return nodes;
699 }
700
701 /* Allow all objects to be introspected */
702 static GDBusInterfaceInfo **
703 dynamic_subtree_introspect (GDBusConnection       *connection,
704                             const gchar           *sender,
705                             const gchar           *object_path,
706                             const gchar           *node,
707                             gpointer               user_data)
708 {
709   const GDBusInterfaceInfo *interfaces[2] = { &dyna_interface_info, NULL };
710
711   return g_memdup (interfaces, 2 * sizeof (void *));
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_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS |
749                                      G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
750                                      NULL,
751                                      g_dbus_connection_get_unique_name (c),
752                                      object_path,
753                                      "org.example.Foo",
754                                      NULL,
755                                      &error);
756   g_assert (foo_proxy != NULL);
757
758   /* generic interfaces */
759   error = NULL;
760   value = g_dbus_proxy_call_sync (foo_proxy,
761                                   "org.freedesktop.DBus.Peer.Ping",
762                                   NULL,
763                                   G_DBUS_CALL_FLAGS_NONE,
764                                   -1,
765                                   NULL,
766                                   &error);
767   g_assert_no_error (error);
768   g_assert (value != NULL);
769   g_variant_unref (value);
770
771   /* user methods */
772   error = NULL;
773   value = g_dbus_proxy_call_sync (foo_proxy,
774                                   "Method1",
775                                   g_variant_new ("(s)", "winwinwin"),
776                                   G_DBUS_CALL_FLAGS_NONE,
777                                   -1,
778                                   NULL,
779                                   &error);
780   g_assert_no_error (error);
781   g_assert (value != NULL);
782   g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("(s)")));
783   g_variant_get (value, "(&s)", &value_str);
784   g_assert_cmpstr (value_str, ==, "You passed the string 'winwinwin'. Jolly good!");
785   g_variant_unref (value);
786
787   error = NULL;
788   value = g_dbus_proxy_call_sync (foo_proxy,
789                                   "Method2",
790                                   NULL,
791                                   G_DBUS_CALL_FLAGS_NONE,
792                                   -1,
793                                   NULL,
794                                   &error);
795   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
796   g_assert_cmpstr (error->message, ==, "GDBus.Error:org.example.SomeError: How do you like them apples, buddy!");
797   g_error_free (error);
798   g_assert (value == NULL);
799
800   error = NULL;
801   value = g_dbus_proxy_call_sync (foo_proxy,
802                                   "Method2",
803                                   g_variant_new ("(s)", "failfailfail"),
804                                   G_DBUS_CALL_FLAGS_NONE,
805                                   -1,
806                                   NULL,
807                                   &error);
808   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS);
809   g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.InvalidArgs: Type of message, '(s)', does not match expected type '()'");
810   g_error_free (error);
811   g_assert (value == NULL);
812
813   error = NULL;
814   value = g_dbus_proxy_call_sync (foo_proxy,
815                                   "NonExistantMethod",
816                                   NULL,
817                                   G_DBUS_CALL_FLAGS_NONE,
818                                   -1,
819                                   NULL,
820                                   &error);
821   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
822   g_assert_cmpstr (error->message, ==, "GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod: No such method 'NonExistantMethod'");
823   g_error_free (error);
824   g_assert (value == NULL);
825
826   error = NULL;
827   value = g_dbus_proxy_call_sync (foo_proxy,
828                                   "org.example.FooXYZ.NonExistant",
829                                   NULL,
830                                   G_DBUS_CALL_FLAGS_NONE,
831                                   -1,
832                                   NULL,
833                                   &error);
834   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
835   g_error_free (error);
836   g_assert (value == NULL);
837
838   /* user properties */
839   error = NULL;
840   value = g_dbus_proxy_call_sync (foo_proxy,
841                                   "org.freedesktop.DBus.Properties.Get",
842                                   g_variant_new ("(ss)",
843                                                  "org.example.Foo",
844                                                  "PropertyUno"),
845                                   G_DBUS_CALL_FLAGS_NONE,
846                                   -1,
847                                   NULL,
848                                   &error);
849   g_assert_no_error (error);
850   g_assert (value != NULL);
851   g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE ("(v)")));
852   g_variant_get (value, "(v)", &inner);
853   g_assert (g_variant_is_of_type (inner, G_VARIANT_TYPE_STRING));
854   g_assert_cmpstr (g_variant_get_string (inner, NULL), ==, "Property 'PropertyUno' Is What It Is!");
855   g_variant_unref (value);
856   g_variant_unref (inner);
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
948   /* run this in a thread to avoid deadlocks */
949   thread = g_thread_new ("test_dispatch",
950                          test_dispatch_thread_func,
951                          (gpointer) object_path);
952   g_main_loop_run (loop);
953   g_thread_join (thread);
954 }
955
956 static void
957 test_object_registration (void)
958 {
959   GError *error;
960   ObjectRegistrationData data;
961   GPtrArray *dyna_data;
962   gchar **nodes;
963   guint boss_foo_reg_id;
964   guint boss_bar_reg_id;
965   guint worker1_foo_reg_id;
966   guint worker1p1_foo_reg_id;
967   guint worker2_bar_reg_id;
968   guint intern1_foo_reg_id;
969   guint intern2_bar_reg_id;
970   guint intern2_foo_reg_id;
971   guint intern3_bar_reg_id;
972   guint registration_id;
973   guint subtree_registration_id;
974   guint non_subtree_object_path_foo_reg_id;
975   guint non_subtree_object_path_bar_reg_id;
976   guint dyna_subtree_registration_id;
977   guint num_successful_registrations;
978
979   data.num_unregistered_calls = 0;
980   data.num_unregistered_subtree_calls = 0;
981   data.num_subtree_nodes = 0;
982
983   num_successful_registrations = 0;
984
985   error = NULL;
986   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
987   g_assert_no_error (error);
988   g_assert (c != NULL);
989
990   registration_id = g_dbus_connection_register_object (c,
991                                                        "/foo/boss",
992                                                        (GDBusInterfaceInfo *) &foo_interface_info,
993                                                        &foo_vtable,
994                                                        &data,
995                                                        on_object_unregistered,
996                                                        &error);
997   g_assert_no_error (error);
998   g_assert (registration_id > 0);
999   boss_foo_reg_id = registration_id;
1000   num_successful_registrations++;
1001
1002   registration_id = g_dbus_connection_register_object (c,
1003                                                        "/foo/boss",
1004                                                        (GDBusInterfaceInfo *) &bar_interface_info,
1005                                                        NULL,
1006                                                        &data,
1007                                                        on_object_unregistered,
1008                                                        &error);
1009   g_assert_no_error (error);
1010   g_assert (registration_id > 0);
1011   boss_bar_reg_id = registration_id;
1012   num_successful_registrations++;
1013
1014   registration_id = g_dbus_connection_register_object (c,
1015                                                        "/foo/boss/worker1",
1016                                                        (GDBusInterfaceInfo *) &foo_interface_info,
1017                                                        NULL,
1018                                                        &data,
1019                                                        on_object_unregistered,
1020                                                        &error);
1021   g_assert_no_error (error);
1022   g_assert (registration_id > 0);
1023   worker1_foo_reg_id = registration_id;
1024   num_successful_registrations++;
1025
1026   registration_id = g_dbus_connection_register_object (c,
1027                                                        "/foo/boss/worker1p1",
1028                                                        (GDBusInterfaceInfo *) &foo_interface_info,
1029                                                        NULL,
1030                                                        &data,
1031                                                        on_object_unregistered,
1032                                                        &error);
1033   g_assert_no_error (error);
1034   g_assert (registration_id > 0);
1035   worker1p1_foo_reg_id = registration_id;
1036   num_successful_registrations++;
1037
1038   registration_id = g_dbus_connection_register_object (c,
1039                                                        "/foo/boss/worker2",
1040                                                        (GDBusInterfaceInfo *) &bar_interface_info,
1041                                                        NULL,
1042                                                        &data,
1043                                                        on_object_unregistered,
1044                                                        &error);
1045   g_assert_no_error (error);
1046   g_assert (registration_id > 0);
1047   worker2_bar_reg_id = registration_id;
1048   num_successful_registrations++;
1049
1050   registration_id = g_dbus_connection_register_object (c,
1051                                                        "/foo/boss/interns/intern1",
1052                                                        (GDBusInterfaceInfo *) &foo_interface_info,
1053                                                        NULL,
1054                                                        &data,
1055                                                        on_object_unregistered,
1056                                                        &error);
1057   g_assert_no_error (error);
1058   g_assert (registration_id > 0);
1059   intern1_foo_reg_id = registration_id;
1060   num_successful_registrations++;
1061
1062   /* ... and try again at another path */
1063   registration_id = g_dbus_connection_register_object (c,
1064                                                        "/foo/boss/interns/intern2",
1065                                                        (GDBusInterfaceInfo *) &bar_interface_info,
1066                                                        NULL,
1067                                                        &data,
1068                                                        on_object_unregistered,
1069                                                        &error);
1070   g_assert_no_error (error);
1071   g_assert (registration_id > 0);
1072   intern2_bar_reg_id = registration_id;
1073   num_successful_registrations++;
1074
1075   /* register at the same path/interface - this should fail */
1076   registration_id = g_dbus_connection_register_object (c,
1077                                                        "/foo/boss/interns/intern2",
1078                                                        (GDBusInterfaceInfo *) &bar_interface_info,
1079                                                        NULL,
1080                                                        &data,
1081                                                        on_object_unregistered,
1082                                                        &error);
1083   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
1084   g_assert (!g_dbus_error_is_remote_error (error));
1085   g_error_free (error);
1086   error = NULL;
1087   g_assert (registration_id == 0);
1088
1089   /* register at different interface - shouldn't fail */
1090   registration_id = g_dbus_connection_register_object (c,
1091                                                        "/foo/boss/interns/intern2",
1092                                                        (GDBusInterfaceInfo *) &foo_interface_info,
1093                                                        NULL,
1094                                                        &data,
1095                                                        on_object_unregistered,
1096                                                        &error);
1097   g_assert_no_error (error);
1098   g_assert (registration_id > 0);
1099   intern2_foo_reg_id = registration_id;
1100   num_successful_registrations++;
1101
1102   /* unregister it via the id */
1103   g_assert (g_dbus_connection_unregister_object (c, registration_id));
1104   g_main_context_iteration (NULL, FALSE);
1105   g_assert_cmpint (data.num_unregistered_calls, ==, 1);
1106   intern2_foo_reg_id = 0;
1107
1108   /* register it back */
1109   registration_id = g_dbus_connection_register_object (c,
1110                                                        "/foo/boss/interns/intern2",
1111                                                        (GDBusInterfaceInfo *) &foo_interface_info,
1112                                                        NULL,
1113                                                        &data,
1114                                                        on_object_unregistered,
1115                                                        &error);
1116   g_assert_no_error (error);
1117   g_assert (registration_id > 0);
1118   intern2_foo_reg_id = registration_id;
1119   num_successful_registrations++;
1120
1121   registration_id = g_dbus_connection_register_object (c,
1122                                                        "/foo/boss/interns/intern3",
1123                                                        (GDBusInterfaceInfo *) &bar_interface_info,
1124                                                        NULL,
1125                                                        &data,
1126                                                        on_object_unregistered,
1127                                                        &error);
1128   g_assert_no_error (error);
1129   g_assert (registration_id > 0);
1130   intern3_bar_reg_id = registration_id;
1131   num_successful_registrations++;
1132
1133   /* now register a whole subtree at /foo/boss/executives */
1134   subtree_registration_id = g_dbus_connection_register_subtree (c,
1135                                                                 "/foo/boss/executives",
1136                                                                 &subtree_vtable,
1137                                                                 G_DBUS_SUBTREE_FLAGS_NONE,
1138                                                                 &data,
1139                                                                 on_subtree_unregistered,
1140                                                                 &error);
1141   g_assert_no_error (error);
1142   g_assert (subtree_registration_id > 0);
1143   /* try registering it again.. this should fail */
1144   registration_id = g_dbus_connection_register_subtree (c,
1145                                                         "/foo/boss/executives",
1146                                                         &subtree_vtable,
1147                                                         G_DBUS_SUBTREE_FLAGS_NONE,
1148                                                         &data,
1149                                                         on_subtree_unregistered,
1150                                                         &error);
1151   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_EXISTS);
1152   g_assert (!g_dbus_error_is_remote_error (error));
1153   g_error_free (error);
1154   error = NULL;
1155   g_assert (registration_id == 0);
1156
1157   /* unregister it, then register it again */
1158   g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 0);
1159   g_assert (g_dbus_connection_unregister_subtree (c, subtree_registration_id));
1160   g_main_context_iteration (NULL, FALSE);
1161   g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 1);
1162   subtree_registration_id = g_dbus_connection_register_subtree (c,
1163                                                                 "/foo/boss/executives",
1164                                                                 &subtree_vtable,
1165                                                                 G_DBUS_SUBTREE_FLAGS_NONE,
1166                                                                 &data,
1167                                                                 on_subtree_unregistered,
1168                                                                 &error);
1169   g_assert_no_error (error);
1170   g_assert (subtree_registration_id > 0);
1171
1172   /* try to register something under /foo/boss/executives - this should work
1173    * because registered subtrees and registered objects can coexist.
1174    *
1175    * Make the exported object implement *two* interfaces so we can check
1176    * that the right introspection handler is invoked.
1177    */
1178   registration_id = g_dbus_connection_register_object (c,
1179                                                        "/foo/boss/executives/non_subtree_object",
1180                                                        (GDBusInterfaceInfo *) &bar_interface_info,
1181                                                        NULL,
1182                                                        &data,
1183                                                        on_object_unregistered,
1184                                                        &error);
1185   g_assert_no_error (error);
1186   g_assert (registration_id > 0);
1187   non_subtree_object_path_bar_reg_id = registration_id;
1188   num_successful_registrations++;
1189   registration_id = g_dbus_connection_register_object (c,
1190                                                        "/foo/boss/executives/non_subtree_object",
1191                                                        (GDBusInterfaceInfo *) &foo_interface_info,
1192                                                        NULL,
1193                                                        &data,
1194                                                        on_object_unregistered,
1195                                                        &error);
1196   g_assert_no_error (error);
1197   g_assert (registration_id > 0);
1198   non_subtree_object_path_foo_reg_id = registration_id;
1199   num_successful_registrations++;
1200
1201   /* now register a dynamic subtree, spawning objects as they are called */
1202   dyna_data = g_ptr_array_new_with_free_func (g_free);
1203   dyna_subtree_registration_id = g_dbus_connection_register_subtree (c,
1204                                                                      "/foo/dyna",
1205                                                                      &dynamic_subtree_vtable,
1206                                                                      G_DBUS_SUBTREE_FLAGS_DISPATCH_TO_UNENUMERATED_NODES,
1207                                                                      dyna_data,
1208                                                                      (GDestroyNotify)g_ptr_array_unref,
1209                                                                      &error);
1210   g_assert_no_error (error);
1211   g_assert (dyna_subtree_registration_id > 0);
1212
1213   /* First assert that we have no nodes in the dynamic subtree */
1214   nodes = get_nodes_at (c, "/foo/dyna");
1215   g_assert (nodes != NULL);
1216   g_assert_cmpint (g_strv_length (nodes), ==, 0);
1217   g_strfreev (nodes);
1218   g_assert_cmpint (count_interfaces (c, "/foo/dyna"), ==, 4);
1219
1220   /* Install three nodes in the dynamic subtree via the dyna_data backdoor and
1221    * assert that they show up correctly in the introspection data */
1222   g_ptr_array_add (dyna_data, g_strdup ("lol"));
1223   g_ptr_array_add (dyna_data, g_strdup ("cat"));
1224   g_ptr_array_add (dyna_data, g_strdup ("cheezburger"));
1225   nodes = get_nodes_at (c, "/foo/dyna");
1226   g_assert (nodes != NULL);
1227   g_assert_cmpint (g_strv_length (nodes), ==, 3);
1228   g_assert_cmpstr (nodes[0], ==, "lol");
1229   g_assert_cmpstr (nodes[1], ==, "cat");
1230   g_assert_cmpstr (nodes[2], ==, "cheezburger");
1231   g_strfreev (nodes);
1232   g_assert_cmpint (count_interfaces (c, "/foo/dyna/lol"), ==, 4);
1233   g_assert_cmpint (count_interfaces (c, "/foo/dyna/cat"), ==, 4);
1234   g_assert_cmpint (count_interfaces (c, "/foo/dyna/cheezburger"), ==, 4);
1235
1236   /* Call a non-existing object path and assert that it has been created */
1237   dyna_create (c, "dynamicallycreated");
1238   nodes = get_nodes_at (c, "/foo/dyna");
1239   g_assert (nodes != NULL);
1240   g_assert_cmpint (g_strv_length (nodes), ==, 4);
1241   g_assert_cmpstr (nodes[0], ==, "lol");
1242   g_assert_cmpstr (nodes[1], ==, "cat");
1243   g_assert_cmpstr (nodes[2], ==, "cheezburger");
1244   g_assert_cmpstr (nodes[3], ==, "dynamicallycreated");
1245   g_strfreev (nodes);
1246   g_assert_cmpint (count_interfaces (c, "/foo/dyna/dynamicallycreated"), ==, 4);
1247
1248   /* now check that the object hierarachy is properly generated... yes, it's a bit
1249    * perverse that we round-trip to the bus to introspect ourselves ;-)
1250    */
1251   nodes = get_nodes_at (c, "/");
1252   g_assert (nodes != NULL);
1253   g_assert_cmpint (g_strv_length (nodes), ==, 1);
1254   g_assert_cmpstr (nodes[0], ==, "foo");
1255   g_strfreev (nodes);
1256   g_assert_cmpint (count_interfaces (c, "/"), ==, 0);
1257
1258   nodes = get_nodes_at (c, "/foo");
1259   g_assert (nodes != NULL);
1260   g_assert_cmpint (g_strv_length (nodes), ==, 2);
1261   g_assert_cmpstr (nodes[0], ==, "boss");
1262   g_assert_cmpstr (nodes[1], ==, "dyna");
1263   g_strfreev (nodes);
1264   g_assert_cmpint (count_interfaces (c, "/foo"), ==, 0);
1265
1266   nodes = get_nodes_at (c, "/foo/boss");
1267   g_assert (nodes != NULL);
1268   g_assert_cmpint (g_strv_length (nodes), ==, 5);
1269   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker1"));
1270   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker1p1"));
1271   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "worker2"));
1272   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "interns"));
1273   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "executives"));
1274   g_strfreev (nodes);
1275   /* any registered object always implement org.freedesktop.DBus.[Peer,Introspectable,Properties] */
1276   g_assert_cmpint (count_interfaces (c, "/foo/boss"), ==, 5);
1277   g_assert (has_interface (c, "/foo/boss", foo_interface_info.name));
1278   g_assert (has_interface (c, "/foo/boss", bar_interface_info.name));
1279
1280   /* check subtree nodes - we should have only non_subtree_object in /foo/boss/executives
1281    * because data.num_subtree_nodes is 0
1282    */
1283   nodes = get_nodes_at (c, "/foo/boss/executives");
1284   g_assert (nodes != NULL);
1285   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1286   g_assert_cmpint (g_strv_length (nodes), ==, 1);
1287   g_strfreev (nodes);
1288   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives"), ==, 0);
1289
1290   /* now change data.num_subtree_nodes and check */
1291   data.num_subtree_nodes = 2;
1292   nodes = get_nodes_at (c, "/foo/boss/executives");
1293   g_assert (nodes != NULL);
1294   g_assert_cmpint (g_strv_length (nodes), ==, 5);
1295   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1296   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp0"));
1297   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp1"));
1298   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp0"));
1299   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp1"));
1300   /* check that /foo/boss/executives/non_subtree_object is not handled by the
1301    * subtree handlers - we can do this because objects from subtree handlers
1302    * has exactly one interface and non_subtree_object has two
1303    */
1304   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/non_subtree_object"), ==, 5);
1305   g_assert (has_interface (c, "/foo/boss/executives/non_subtree_object", foo_interface_info.name));
1306   g_assert (has_interface (c, "/foo/boss/executives/non_subtree_object", bar_interface_info.name));
1307   /* check that the vp and evp objects are handled by the subtree handlers */
1308   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/vp0"), ==, 4);
1309   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/vp1"), ==, 4);
1310   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/evp0"), ==, 4);
1311   g_assert_cmpint (count_interfaces (c, "/foo/boss/executives/evp1"), ==, 4);
1312   g_assert (has_interface (c, "/foo/boss/executives/vp0", foo_interface_info.name));
1313   g_assert (has_interface (c, "/foo/boss/executives/vp1", foo_interface_info.name));
1314   g_assert (has_interface (c, "/foo/boss/executives/evp0", bar_interface_info.name));
1315   g_assert (has_interface (c, "/foo/boss/executives/evp1", bar_interface_info.name));
1316   g_strfreev (nodes);
1317   data.num_subtree_nodes = 3;
1318   nodes = get_nodes_at (c, "/foo/boss/executives");
1319   g_assert (nodes != NULL);
1320   g_assert_cmpint (g_strv_length (nodes), ==, 7);
1321   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1322   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp0"));
1323   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp1"));
1324   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "vp2"));
1325   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp0"));
1326   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp1"));
1327   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "evp2"));
1328   g_strfreev (nodes);
1329
1330   /* This is to check that a bug (rather, class of bugs) in gdbusconnection.c's
1331    *
1332    *  g_dbus_connection_list_registered_unlocked()
1333    *
1334    * where /foo/boss/worker1 reported a child '1', is now fixed.
1335    */
1336   nodes = get_nodes_at (c, "/foo/boss/worker1");
1337   g_assert (nodes != NULL);
1338   g_assert_cmpint (g_strv_length (nodes), ==, 0);
1339   g_strfreev (nodes);
1340
1341   /* check that calls are properly dispatched to the functions in foo_vtable for objects
1342    * implementing the org.example.Foo interface
1343    *
1344    * We do this for both a regular registered object (/foo/boss) and also for an object
1345    * registered through the subtree mechanism.
1346    */
1347   test_dispatch ("/foo/boss");
1348   test_dispatch ("/foo/boss/executives/vp0");
1349
1350   /* To prevent from exiting and attaching a D-Bus tool like D-Feet; uncomment: */
1351 #if 0
1352   g_debug ("Point D-feet or other tool at: %s", g_test_dbus_get_temporary_address());
1353   g_main_loop_run (loop);
1354 #endif
1355
1356   /* check that unregistering the subtree handler works */
1357   g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 1);
1358   g_assert (g_dbus_connection_unregister_subtree (c, subtree_registration_id));
1359   g_main_context_iteration (NULL, FALSE);
1360   g_assert_cmpint (data.num_unregistered_subtree_calls, ==, 2);
1361   nodes = get_nodes_at (c, "/foo/boss/executives");
1362   g_assert (nodes != NULL);
1363   g_assert_cmpint (g_strv_length (nodes), ==, 1);
1364   g_assert (_g_strv_has_string ((const gchar* const *) nodes, "non_subtree_object"));
1365   g_strfreev (nodes);
1366
1367   g_assert (g_dbus_connection_unregister_object (c, boss_foo_reg_id));
1368   g_assert (g_dbus_connection_unregister_object (c, boss_bar_reg_id));
1369   g_assert (g_dbus_connection_unregister_object (c, worker1_foo_reg_id));
1370   g_assert (g_dbus_connection_unregister_object (c, worker1p1_foo_reg_id));
1371   g_assert (g_dbus_connection_unregister_object (c, worker2_bar_reg_id));
1372   g_assert (g_dbus_connection_unregister_object (c, intern1_foo_reg_id));
1373   g_assert (g_dbus_connection_unregister_object (c, intern2_bar_reg_id));
1374   g_assert (g_dbus_connection_unregister_object (c, intern2_foo_reg_id));
1375   g_assert (g_dbus_connection_unregister_object (c, intern3_bar_reg_id));
1376   g_assert (g_dbus_connection_unregister_object (c, non_subtree_object_path_bar_reg_id));
1377   g_assert (g_dbus_connection_unregister_object (c, non_subtree_object_path_foo_reg_id));
1378
1379   g_main_context_iteration (NULL, FALSE);
1380   g_assert_cmpint (data.num_unregistered_calls, ==, num_successful_registrations);
1381
1382   /* check that we no longer export any objects - TODO: it looks like there's a bug in
1383    * libdbus-1 here: libdbus still reports the '/foo' object; so disable the test for now
1384    */
1385 #if 0
1386   nodes = get_nodes_at (c, "/");
1387   g_assert (nodes != NULL);
1388   g_assert_cmpint (g_strv_length (nodes), ==, 0);
1389   g_strfreev (nodes);
1390 #endif
1391
1392   g_object_unref (c);
1393 }
1394
1395 static const GDBusInterfaceInfo test_interface_info1 =
1396 {
1397   -1,
1398   "org.example.Foo",
1399   (GDBusMethodInfo **) NULL,
1400   (GDBusSignalInfo **) NULL,
1401   (GDBusPropertyInfo **) NULL,
1402   NULL,
1403 };
1404
1405 static const GDBusInterfaceInfo test_interface_info2 =
1406 {
1407   -1,
1408   "org.freedesktop.DBus.Properties",
1409   (GDBusMethodInfo **) NULL,
1410   (GDBusSignalInfo **) NULL,
1411   (GDBusPropertyInfo **) NULL,
1412   NULL,
1413 };
1414
1415 static void
1416 check_interfaces (GDBusConnection  *c,
1417                   const gchar      *object_path,
1418                   const gchar     **interfaces)
1419 {
1420   GError *error;
1421   GDBusProxy *proxy;
1422   gchar *xml_data;
1423   GDBusNodeInfo *node_info;
1424   gint i, j;
1425
1426   error = NULL;
1427   proxy = g_dbus_proxy_new_sync (c,
1428                                  G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
1429                                  G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
1430                                  NULL,
1431                                  g_dbus_connection_get_unique_name (c),
1432                                  object_path,
1433                                  "org.freedesktop.DBus.Introspectable",
1434                                  NULL,
1435                                  &error);
1436   g_assert_no_error (error);
1437   g_assert (proxy != NULL);
1438
1439   /* do this async to avoid libdbus-1 deadlocks */
1440   xml_data = NULL;
1441   g_dbus_proxy_call (proxy,
1442                      "Introspect",
1443                      NULL,
1444                      G_DBUS_CALL_FLAGS_NONE,
1445                      -1,
1446                      NULL,
1447                      (GAsyncReadyCallback) introspect_callback,
1448                      &xml_data);
1449   g_main_loop_run (loop);
1450   g_assert (xml_data != NULL);
1451
1452   node_info = g_dbus_node_info_new_for_xml (xml_data, &error);
1453   g_assert_no_error (error);
1454   g_assert (node_info != NULL);
1455
1456   g_assert (node_info->interfaces != NULL);
1457   for (i = 0; node_info->interfaces[i]; i++) ;
1458 #if 0
1459   if (g_strv_length ((gchar**)interfaces) != i - 1)
1460     {
1461       g_print ("expected ");
1462       for (i = 0; interfaces[i]; i++)
1463         g_print ("%s ", interfaces[i]);
1464       g_print ("\ngot ");
1465       for (i = 0; node_info->interfaces[i]; i++)
1466         g_print ("%s ", node_info->interfaces[i]->name);
1467       g_print ("\n");
1468     }
1469 #endif
1470   g_assert_cmpint (g_strv_length ((gchar**)interfaces), ==, i - 1);
1471
1472   for (i = 0; interfaces[i]; i++)
1473     {
1474       for (j = 0; node_info->interfaces[j]; j++)
1475         {
1476           if (strcmp (interfaces[i], node_info->interfaces[j]->name) == 0)
1477             goto found;
1478         }
1479
1480       g_assert_not_reached ();
1481
1482  found: ;
1483     }
1484
1485   g_object_unref (proxy);
1486   g_free (xml_data);
1487   g_dbus_node_info_unref (node_info);
1488 }
1489
1490 static void
1491 test_registered_interfaces (void)
1492 {
1493   GError *error;
1494   guint id1, id2;
1495   const gchar *interfaces[] = {
1496     "org.example.Foo",
1497     "org.freedesktop.DBus.Properties",
1498     "org.freedesktop.DBus.Introspectable",
1499     NULL,
1500   };
1501
1502   error = NULL;
1503   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
1504   g_assert_no_error (error);
1505   g_assert (c != NULL);
1506
1507   id1 = g_dbus_connection_register_object (c,
1508                                            "/test",
1509                                            (GDBusInterfaceInfo *) &test_interface_info1,
1510                                            NULL,
1511                                            NULL,
1512                                            NULL,
1513                                            &error);
1514   g_assert_no_error (error);
1515   g_assert (id1 > 0);
1516   id2 = g_dbus_connection_register_object (c,
1517                                            "/test",
1518                                            (GDBusInterfaceInfo *) &test_interface_info2,
1519                                            NULL,
1520                                            NULL,
1521                                            NULL,
1522                                            &error);
1523   g_assert_no_error (error);
1524   g_assert (id2 > 0);
1525
1526   check_interfaces (c, "/test", interfaces);
1527
1528   g_assert (g_dbus_connection_unregister_object (c, id1));
1529   g_assert (g_dbus_connection_unregister_object (c, id2));
1530   g_object_unref (c);
1531 }
1532
1533
1534 /* ---------------------------------------------------------------------------------------------------- */
1535
1536 static void
1537 test_async_method_call (GDBusConnection       *connection,
1538                         const gchar           *sender,
1539                         const gchar           *object_path,
1540                         const gchar           *interface_name,
1541                         const gchar           *method_name,
1542                         GVariant              *parameters,
1543                         GDBusMethodInvocation *invocation,
1544                         gpointer               user_data)
1545 {
1546   const GDBusPropertyInfo *property;
1547
1548   /* Strictly speaking, this function should also expect to receive
1549    * method calls not on the org.freedesktop.DBus.Properties interface,
1550    * but we don't do any during this testcase, so assert that.
1551    */
1552   g_assert_cmpstr (interface_name, ==, "org.freedesktop.DBus.Properties");
1553   g_assert (g_dbus_method_invocation_get_method_info (invocation) == NULL);
1554
1555   property = g_dbus_method_invocation_get_property_info (invocation);
1556
1557   /* We should never be seeing any property calls on the com.example.Bar
1558    * interface because it doesn't export any properties.
1559    *
1560    * In each case below make sure the interface is org.example.Foo.
1561    */
1562
1563   /* Do a whole lot of asserts to make sure that invalid calls are still
1564    * getting properly rejected by GDBusConnection and that our
1565    * environment is as we expect it to be.
1566    */
1567   if (g_str_equal (method_name, "Get"))
1568     {
1569       const gchar *iface_name, *prop_name;
1570
1571       g_variant_get (parameters, "(&s&s)", &iface_name, &prop_name);
1572       g_assert_cmpstr (iface_name, ==, "org.example.Foo");
1573       g_assert (property != NULL);
1574       g_assert_cmpstr (prop_name, ==, property->name);
1575       g_assert (property->flags & G_DBUS_PROPERTY_INFO_FLAGS_READABLE);
1576       g_dbus_method_invocation_return_value (invocation, g_variant_new ("(v)", g_variant_new_string (prop_name)));
1577     }
1578
1579   else if (g_str_equal (method_name, "Set"))
1580     {
1581       const gchar *iface_name, *prop_name;
1582       GVariant *value;
1583
1584       g_variant_get (parameters, "(&s&sv)", &iface_name, &prop_name, &value);
1585       g_assert_cmpstr (iface_name, ==, "org.example.Foo");
1586       g_assert (property != NULL);
1587       g_assert_cmpstr (prop_name, ==, property->name);
1588       g_assert (property->flags & G_DBUS_PROPERTY_INFO_FLAGS_WRITABLE);
1589       g_assert (g_variant_is_of_type (value, G_VARIANT_TYPE (property->signature)));
1590       g_dbus_method_invocation_return_value (invocation, g_variant_new ("()"));
1591       g_variant_unref (value);
1592     }
1593
1594   else if (g_str_equal (method_name, "GetAll"))
1595     {
1596       const gchar *iface_name;
1597
1598       g_variant_get (parameters, "(&s)", &iface_name);
1599       g_assert_cmpstr (iface_name, ==, "org.example.Foo");
1600       g_assert (property == NULL);
1601       g_dbus_method_invocation_return_value (invocation,
1602                                              g_variant_new_parsed ("({ 'PropertyUno': < 'uno' >,"
1603                                                                    "   'NotWritable': < 'notwrite' > },)"));
1604     }
1605
1606   else
1607     g_assert_not_reached ();
1608 }
1609
1610 static gint outstanding_cases;
1611
1612 static void
1613 ensure_result_cb (GObject      *source,
1614                   GAsyncResult *result,
1615                   gpointer      user_data)
1616 {
1617   GDBusConnection *connection = G_DBUS_CONNECTION (source);
1618   GVariant *reply;
1619
1620   reply = g_dbus_connection_call_finish (connection, result, NULL);
1621
1622   if (user_data == NULL)
1623     {
1624       /* Expected an error */
1625       g_assert (reply == NULL);
1626     }
1627   else
1628     {
1629       /* Expected a reply of a particular format. */
1630       gchar *str;
1631
1632       g_assert (reply != NULL);
1633       str = g_variant_print (reply, TRUE);
1634       g_assert_cmpstr (str, ==, (const gchar *) user_data);
1635       g_free (str);
1636
1637       g_variant_unref (reply);
1638     }
1639
1640   g_assert_cmpint (outstanding_cases, >, 0);
1641   outstanding_cases--;
1642 }
1643
1644 static void
1645 test_async_case (GDBusConnection *connection,
1646                  const gchar     *expected_reply,
1647                  const gchar     *method,
1648                  const gchar     *format_string,
1649                  ...)
1650 {
1651   va_list ap;
1652
1653   va_start (ap, format_string);
1654
1655   g_dbus_connection_call (connection, g_dbus_connection_get_unique_name (connection), "/foo",
1656                           "org.freedesktop.DBus.Properties", method, g_variant_new_va (format_string, NULL, &ap),
1657                           NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, ensure_result_cb, (gpointer) expected_reply);
1658
1659   va_end (ap);
1660
1661   outstanding_cases++;
1662 }
1663
1664 static void
1665 test_async_properties (void)
1666 {
1667   GError *error = NULL;
1668   guint registration_id, registration_id2;
1669   static const GDBusInterfaceVTable vtable = {
1670     test_async_method_call, NULL, NULL
1671   };
1672
1673   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
1674   g_assert_no_error (error);
1675   g_assert (c != NULL);
1676
1677   registration_id = g_dbus_connection_register_object (c,
1678                                                        "/foo",
1679                                                        (GDBusInterfaceInfo *) &foo_interface_info,
1680                                                        &vtable, NULL, NULL, &error);
1681   g_assert_no_error (error);
1682   g_assert (registration_id);
1683   registration_id2 = g_dbus_connection_register_object (c,
1684                                                         "/foo",
1685                                                         (GDBusInterfaceInfo *) &foo2_interface_info,
1686                                                         &vtable, NULL, NULL, &error);
1687   g_assert_no_error (error);
1688   g_assert (registration_id);
1689
1690   test_async_case (c, NULL, "random", "()");
1691
1692   /* Test a variety of error cases */
1693   test_async_case (c, NULL, "Get", "(si)", "wrong signature", 5);
1694   test_async_case (c, NULL, "Get", "(ss)", "org.example.WrongInterface", "zzz");
1695   test_async_case (c, NULL, "Get", "(ss)", "org.example.Foo", "NoSuchProperty");
1696   test_async_case (c, NULL, "Get", "(ss)", "org.example.Foo", "NotReadable");
1697
1698   test_async_case (c, NULL, "Set", "(si)", "wrong signature", 5);
1699   test_async_case (c, NULL, "Set", "(ssv)", "org.example.WrongInterface", "zzz", g_variant_new_string (""));
1700   test_async_case (c, NULL, "Set", "(ssv)", "org.example.Foo", "NoSuchProperty", g_variant_new_string (""));
1701   test_async_case (c, NULL, "Set", "(ssv)", "org.example.Foo", "NotWritable", g_variant_new_string (""));
1702   test_async_case (c, NULL, "Set", "(ssv)", "org.example.Foo", "PropertyUno", g_variant_new_object_path ("/wrong"));
1703
1704   test_async_case (c, NULL, "GetAll", "(si)", "wrong signature", 5);
1705   test_async_case (c, NULL, "GetAll", "(s)", "org.example.WrongInterface");
1706
1707   /* Make sure that we get no unexpected async property calls for com.example.Foo2 */
1708   test_async_case (c, NULL, "Get", "(ss)", "org.example.Foo2", "zzz");
1709   test_async_case (c, NULL, "Set", "(ssv)", "org.example.Foo2", "zzz", g_variant_new_string (""));
1710   test_async_case (c, "(@a{sv} {},)", "GetAll", "(s)", "org.example.Foo2");
1711
1712   /* Now do the proper things */
1713   test_async_case (c, "(<'PropertyUno'>,)", "Get", "(ss)", "org.example.Foo", "PropertyUno");
1714   test_async_case (c, "(<'NotWritable'>,)", "Get", "(ss)", "org.example.Foo", "NotWritable");
1715   test_async_case (c, "()", "Set", "(ssv)", "org.example.Foo", "PropertyUno", g_variant_new_string (""));
1716   test_async_case (c, "()", "Set", "(ssv)", "org.example.Foo", "NotReadable", g_variant_new_string (""));
1717   test_async_case (c, "({'PropertyUno': <'uno'>, 'NotWritable': <'notwrite'>},)", "GetAll", "(s)", "org.example.Foo");
1718
1719   while (outstanding_cases)
1720     g_main_context_iteration (NULL, TRUE);
1721
1722   g_dbus_connection_unregister_object (c, registration_id);
1723   g_dbus_connection_unregister_object (c, registration_id2);
1724   g_object_unref (c);
1725 }
1726
1727 /* ---------------------------------------------------------------------------------------------------- */
1728
1729 int
1730 main (int   argc,
1731       char *argv[])
1732 {
1733   gint ret;
1734
1735   g_test_init (&argc, &argv, NULL);
1736
1737   /* all the tests rely on a shared main loop */
1738   loop = g_main_loop_new (NULL, FALSE);
1739
1740   g_test_add_func ("/gdbus/object-registration", test_object_registration);
1741   g_test_add_func ("/gdbus/registered-interfaces", test_registered_interfaces);
1742   g_test_add_func ("/gdbus/async-properties", test_async_properties);
1743
1744   /* TODO: check that we spit out correct introspection data */
1745   /* TODO: check that registering a whole subtree works */
1746
1747   ret = session_bus_run ();
1748
1749   g_main_loop_unref (loop);
1750
1751   return ret;
1752 }