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