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