gdbus-codegen: Use G_TYPE_STRV/gchar** for GVariant type 'ao'
[platform/upstream/glib.git] / gio / tests / gdbus-test-codegen.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2008-2011 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 #include <stdio.h>
27
28 #include "gdbus-tests.h"
29
30 #include "gdbus-test-codegen-generated.h"
31
32 /* ---------------------------------------------------------------------------------------------------- */
33
34 static guint
35 count_annotations (GDBusAnnotationInfo **annotations)
36 {
37   guint ret;
38   ret = 0;
39   while (annotations != NULL && annotations[ret] != NULL)
40     ret++;
41   return ret;
42 }
43
44 /* checks that
45  *
46  *  - non-internal annotations are written out correctly; and
47  *  - injection via --annotation --key --value works
48  */
49 static void
50 test_annotations (void)
51 {
52   GDBusInterfaceInfo *iface;
53   GDBusMethodInfo *method;
54   GDBusSignalInfo *signal;
55   GDBusPropertyInfo *property;
56
57   iface = foo_bar_interface_info ();
58   g_assert (iface != NULL);
59
60   /* see Makefile.am for where these annotations are injected */
61   g_assert_cmpint (count_annotations (iface->annotations), ==, 1);
62   g_assert_cmpstr (g_dbus_annotation_info_lookup (iface->annotations, "Key1"), ==, "Value1");
63
64   method = g_dbus_interface_info_lookup_method (iface, "HelloWorld");
65   g_assert (method != NULL);
66   g_assert_cmpint (count_annotations (method->annotations), ==, 2);
67   g_assert_cmpstr (g_dbus_annotation_info_lookup (method->annotations, "ExistingAnnotation"), ==, "blah");
68   g_assert_cmpstr (g_dbus_annotation_info_lookup (method->annotations, "Key3"), ==, "Value3");
69
70   signal = g_dbus_interface_info_lookup_signal (iface, "TestSignal");
71   g_assert (signal != NULL);
72   g_assert_cmpint (count_annotations (signal->annotations), ==, 1);
73   g_assert_cmpstr (g_dbus_annotation_info_lookup (signal->annotations, "Key4"), ==, "Value4");
74   g_assert_cmpstr (g_dbus_annotation_info_lookup (signal->args[1]->annotations, "Key8"), ==, "Value8");
75
76   property = g_dbus_interface_info_lookup_property (iface, "ay");
77   g_assert (property != NULL);
78   g_assert_cmpint (count_annotations (property->annotations), ==, 1);
79   g_assert_cmpstr (g_dbus_annotation_info_lookup (property->annotations, "Key5"), ==, "Value5");
80
81   method = g_dbus_interface_info_lookup_method (iface, "TestPrimitiveTypes");
82   g_assert (method != NULL);
83   g_assert_cmpstr (g_dbus_annotation_info_lookup (method->in_args[4]->annotations, "Key6"), ==, "Value6");
84   g_assert_cmpstr (g_dbus_annotation_info_lookup (method->out_args[5]->annotations, "Key7"), ==, "Value7");
85 }
86
87 /* ---------------------------------------------------------------------------------------------------- */
88
89 static gboolean
90 on_handle_hello_world (FooBar                 *object,
91                        GDBusMethodInvocation  *invocation,
92                        const gchar            *greeting,
93                        gpointer                user_data)
94 {
95   gchar *response;
96   response = g_strdup_printf ("Word! You said `%s'. I'm Skeleton, btw!", greeting);
97   foo_bar_complete_hello_world (object, invocation, response);
98   g_free (response);
99   return TRUE;
100 }
101
102 static gboolean
103 on_handle_test_primitive_types (FooBar                *object,
104                                 GDBusMethodInvocation *invocation,
105                                 guchar                 val_byte,
106                                 gboolean               val_boolean,
107                                 gint16                 val_int16,
108                                 guint16                val_uint16,
109                                 gint                   val_int32,
110                                 guint                  val_uint32,
111                                 gint64                 val_int64,
112                                 guint64                val_uint64,
113                                 gdouble                val_double,
114                                 const gchar           *val_string,
115                                 const gchar           *val_objpath,
116                                 const gchar           *val_signature,
117                                 const gchar           *val_bytestring,
118                                 gpointer               user_data)
119 {
120   gchar *s1;
121   gchar *s2;
122   gchar *s3;
123   s1 = g_strdup_printf ("Word! You said `%s'. Rock'n'roll!", val_string);
124   s2 = g_strdup_printf ("/modified%s", val_objpath);
125   s3 = g_strdup_printf ("assgit%s", val_signature);
126   foo_bar_complete_test_primitive_types (object,
127                                          invocation,
128                                          10 + val_byte,
129                                          !val_boolean,
130                                          100 + val_int16,
131                                          1000 + val_uint16,
132                                          10000 + val_int32,
133                                          100000 + val_uint32,
134                                          1000000 + val_int64,
135                                          10000000 + val_uint64,
136                                          val_double / G_PI,
137                                          s1,
138                                          s2,
139                                          s3,
140                                          "bytestring!\xff");
141   g_free (s1);
142   g_free (s2);
143   g_free (s3);
144   return TRUE;
145 }
146
147 static gboolean
148 on_handle_test_non_primitive_types (FooBar                *object,
149                                     GDBusMethodInvocation *invocation,
150                                     GVariant              *dict_s_to_s,
151                                     GVariant              *dict_s_to_pairs,
152                                     GVariant              *a_struct,
153                                     const gchar* const    *array_of_strings,
154                                     const gchar* const    *array_of_objpaths,
155                                     GVariant              *array_of_signatures,
156                                     const gchar* const    *array_of_bytestrings,
157                                     gpointer               user_data)
158 {
159   gchar *s;
160   GString *str;
161   str = g_string_new (NULL);
162   s = g_variant_print (dict_s_to_s, TRUE); g_string_append (str, s); g_free (s);
163   s = g_variant_print (dict_s_to_pairs, TRUE); g_string_append (str, s); g_free (s);
164   s = g_variant_print (a_struct, TRUE); g_string_append (str, s); g_free (s);
165   s = g_strjoinv (", ", (gchar **) array_of_strings);
166   g_string_append_printf (str, "array_of_strings: [%s] ", s);
167   g_free (s);
168   s = g_strjoinv (", ", (gchar **) array_of_objpaths);
169   g_string_append_printf (str, "array_of_objpaths: [%s] ", s);
170   g_free (s);
171   s = g_variant_print (array_of_signatures, TRUE);
172   g_string_append_printf (str, "array_of_signatures: %s ", s);
173   g_free (s);
174   s = g_strjoinv (", ", (gchar **) array_of_bytestrings);
175   g_string_append_printf (str, "array_of_bytestrings: [%s] ", s);
176   g_free (s);
177   foo_bar_complete_test_non_primitive_types (object, invocation, str->str);
178   g_string_free (str, TRUE);
179   return TRUE;
180 }
181
182 static gboolean
183 on_handle_request_signal_emission (FooBar                 *object,
184                                    GDBusMethodInvocation  *invocation,
185                                    gint                    which_one,
186                                    gpointer                user_data)
187 {
188   if (which_one == 0)
189     {
190       const gchar *a_strv[] = {"foo", "bar", NULL};
191       const gchar *a_bytestring_array[] = {"foo\xff", "bar\xff", NULL};
192       GVariant *a_variant = g_variant_new_parsed ("{'first': (42, 42), 'second': (43, 43)}");
193       foo_bar_emit_test_signal (object, 43, a_strv, a_bytestring_array, a_variant); /* consumes a_variant */
194       foo_bar_complete_request_signal_emission (object, invocation);
195     }
196   return TRUE;
197 }
198
199 static gboolean
200 on_handle_request_multi_property_mods (FooBar                 *object,
201                                        GDBusMethodInvocation  *invocation,
202                                        gpointer                user_data)
203 {
204   foo_bar_set_y (object, foo_bar_get_y (object) + 1);
205   foo_bar_set_i (object, foo_bar_get_i (object) + 1);
206   foo_bar_set_y (object, foo_bar_get_y (object) + 1);
207   foo_bar_set_i (object, foo_bar_get_i (object) + 1);
208   g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (object));
209   foo_bar_set_y (object, foo_bar_get_y (object) + 1);
210   foo_bar_set_i (object, foo_bar_get_i (object) + 1);
211   foo_bar_complete_request_multi_property_mods (object, invocation);
212   return TRUE;
213 }
214
215 static gboolean
216 on_handle_property_cancellation (FooBar                 *object,
217                                  GDBusMethodInvocation  *invocation,
218                                  gpointer                user_data)
219 {
220   guint n;
221   n = foo_bar_get_n (object);
222   /* This queues up a PropertiesChange event */
223   foo_bar_set_n (object, n + 1);
224   /* this modifies the queued up event */
225   foo_bar_set_n (object, n);
226   /* this flushes all PropertiesChanges event (sends the D-Bus message right
227    * away, if any - there should not be any)
228    */
229   g_dbus_interface_skeleton_flush (G_DBUS_INTERFACE_SKELETON (object));
230   /* this makes us return the reply D-Bus method */
231   foo_bar_complete_property_cancellation (object, invocation);
232   return TRUE;
233 }
234
235 /* ---------------------------------------------------------------------------------------------------- */
236
237 static gboolean
238 on_handle_force_method (FooBat                 *object,
239                         GDBusMethodInvocation  *invocation,
240                         GVariant               *force_in_i,
241                         GVariant               *force_in_s,
242                         GVariant               *force_in_ay,
243                         GVariant               *force_in_struct,
244                         gpointer                user_data)
245 {
246   GVariant *ret_i;
247   GVariant *ret_s;
248   GVariant *ret_ay;
249   GVariant *ret_struct;
250   gint32 val;
251   gchar *s;
252
253   ret_i = g_variant_new_int32 (g_variant_get_int32 (force_in_i) + 10);
254   s = g_strdup_printf ("%s_foo", g_variant_get_string (force_in_s, NULL));
255   ret_s = g_variant_new_string (s);
256   g_free (s);
257   s = g_strdup_printf ("%s_foo\xff", g_variant_get_bytestring (force_in_ay));
258   ret_ay = g_variant_new_bytestring (s);
259   g_free (s);
260
261   g_variant_get (force_in_struct, "(i)", &val);
262   ret_struct = g_variant_new ("(i)", val + 10);
263
264   g_variant_ref_sink (ret_i);
265   g_variant_ref_sink (ret_s);
266   g_variant_ref_sink (ret_ay);
267   g_variant_ref_sink (ret_struct);
268
269   foo_bat_emit_force_signal (object,
270                              ret_i,
271                              ret_s,
272                              ret_ay,
273                              ret_struct);
274
275   foo_bat_complete_force_method (object,
276                                  invocation,
277                                  ret_i,
278                                  ret_s,
279                                  ret_ay,
280                                  ret_struct);
281
282   g_variant_unref (ret_i);
283   g_variant_unref (ret_s);
284   g_variant_unref (ret_ay);
285   g_variant_unref (ret_struct);
286
287   return TRUE;
288 }
289
290
291 /* ---------------------------------------------------------------------------------------------------- */
292
293 static gboolean
294 my_g_authorize_method_handler (GDBusInterfaceSkeleton *interface,
295                                GDBusMethodInvocation  *invocation,
296                                gpointer                user_data)
297 {
298   const gchar *method_name;
299   gboolean authorized;
300
301   authorized = FALSE;
302
303   method_name = g_dbus_method_invocation_get_method_name (invocation);
304   if (g_strcmp0 (method_name, "CheckNotAuthorized") == 0)
305     {
306       authorized = FALSE;
307     }
308   else if (g_strcmp0 (method_name, "CheckAuthorized") == 0)
309     {
310       authorized = TRUE;
311     }
312   else if (g_strcmp0 (method_name, "CheckNotAuthorizedFromObject") == 0)
313     {
314       authorized = TRUE;
315     }
316   else
317     {
318       g_assert_not_reached ();
319     }
320
321   if (!authorized)
322     {
323       g_dbus_method_invocation_return_error (invocation,
324                                              G_IO_ERROR,
325                                              G_IO_ERROR_PERMISSION_DENIED,
326                                              "not authorized...");
327     }
328   return authorized;
329 }
330
331 static gboolean
332 my_object_authorize_method_handler (GDBusObjectSkeleton     *object,
333                                     GDBusInterfaceSkeleton  *interface,
334                                     GDBusMethodInvocation   *invocation,
335                                     gpointer                 user_data)
336 {
337   const gchar *method_name;
338   gboolean authorized;
339
340   authorized = FALSE;
341
342   method_name = g_dbus_method_invocation_get_method_name (invocation);
343   if (g_strcmp0 (method_name, "CheckNotAuthorized") == 0)
344     {
345       authorized = TRUE;
346     }
347   else if (g_strcmp0 (method_name, "CheckAuthorized") == 0)
348     {
349       authorized = TRUE;
350     }
351   else if (g_strcmp0 (method_name, "CheckNotAuthorizedFromObject") == 0)
352     {
353       authorized = FALSE;
354     }
355   else
356     {
357       g_assert_not_reached ();
358     }
359
360   if (!authorized)
361     {
362       g_dbus_method_invocation_return_error (invocation,
363                                              G_IO_ERROR,
364                                              G_IO_ERROR_PENDING,
365                                              "not authorized (from object)...");
366     }
367   return authorized;
368 }
369
370 static gboolean
371 on_handle_check_not_authorized (FooAuthorize           *object,
372                                 GDBusMethodInvocation  *invocation,
373                                 gpointer                user_data)
374 {
375   foo_authorize_complete_check_not_authorized (object, invocation);
376   return TRUE;
377 }
378
379 static gboolean
380 on_handle_check_authorized (FooAuthorize           *object,
381                             GDBusMethodInvocation  *invocation,
382                             gpointer                user_data)
383 {
384   foo_authorize_complete_check_authorized (object, invocation);
385   return TRUE;
386 }
387
388 static gboolean
389 on_handle_check_not_authorized_from_object (FooAuthorize           *object,
390                                             GDBusMethodInvocation  *invocation,
391                                             gpointer                user_data)
392 {
393   foo_authorize_complete_check_not_authorized_from_object (object, invocation);
394   return TRUE;
395 }
396
397 /* ---------------------------------------------------------------------------------------------------- */
398
399 static gboolean
400 on_handle_get_self (FooMethodThreads       *object,
401                     GDBusMethodInvocation  *invocation,
402                     gpointer                user_data)
403 {
404   gchar *s;
405   s = g_strdup_printf ("%p", g_thread_self ());
406   foo_method_threads_complete_get_self (object, invocation, s);
407   g_free (s);
408   return TRUE;
409 }
410
411 /* ---------------------------------------------------------------------------------------------------- */
412
413 static GThread *method_handler_thread = NULL;
414
415 static FooBar *exported_bar_object = NULL;
416 static FooBat *exported_bat_object = NULL;
417 static FooAuthorize *exported_authorize_object = NULL;
418 static GDBusObjectSkeleton *authorize_enclosing_object = NULL;
419 static FooMethodThreads *exported_thread_object_1 = NULL;
420 static FooMethodThreads *exported_thread_object_2 = NULL;
421
422 static void
423 on_bus_acquired (GDBusConnection *connection,
424                  const gchar     *name,
425                  gpointer         user_data)
426 {
427   GError *error;
428
429   /* Test that we can export an object using the generated
430    * FooBarSkeleton subclass. Notes:
431    *
432    * 1. We handle methods by simply connecting to the appropriate
433    * GObject signal.
434    *
435    * 2. Property storage is taken care of by the class; we can
436    *    use g_object_get()/g_object_set() (and the generated
437    *    C bindings at will)
438    */
439   error = NULL;
440   exported_bar_object = foo_bar_skeleton_new ();
441   foo_bar_set_ay (exported_bar_object, "ABCabc");
442   foo_bar_set_y (exported_bar_object, 42);
443   foo_bar_set_d (exported_bar_object, 43.0);
444   foo_bar_set_finally_normal_name (exported_bar_object, "There aint no place like home");
445   foo_bar_set_writeonly_property (exported_bar_object, "Mr. Burns");
446
447   /* The following works because it's on the Skeleton object - it will
448    * fail (at run-time) on a Proxy (see on_proxy_appeared() below)
449    */
450   foo_bar_set_readonly_property (exported_bar_object, "blah");
451   g_assert_cmpstr (foo_bar_get_writeonly_property (exported_bar_object), ==, "Mr. Burns");
452
453   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_bar_object),
454                                     connection,
455                                     "/bar",
456                                     &error);
457   g_assert_no_error (error);
458   g_signal_connect (exported_bar_object,
459                     "handle-hello-world",
460                     G_CALLBACK (on_handle_hello_world),
461                     NULL);
462   g_signal_connect (exported_bar_object,
463                     "handle-test-primitive-types",
464                     G_CALLBACK (on_handle_test_primitive_types),
465                     NULL);
466   g_signal_connect (exported_bar_object,
467                     "handle-test-non-primitive-types",
468                     G_CALLBACK (on_handle_test_non_primitive_types),
469                     NULL);
470   g_signal_connect (exported_bar_object,
471                     "handle-request-signal-emission",
472                     G_CALLBACK (on_handle_request_signal_emission),
473                     NULL);
474   g_signal_connect (exported_bar_object,
475                     "handle-request-multi-property-mods",
476                     G_CALLBACK (on_handle_request_multi_property_mods),
477                     NULL);
478   g_signal_connect (exported_bar_object,
479                     "handle-property-cancellation",
480                     G_CALLBACK (on_handle_property_cancellation),
481                     NULL);
482
483   exported_bat_object = foo_bat_skeleton_new ();
484   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_bat_object),
485                                     connection,
486                                     "/bat",
487                                     &error);
488   g_assert_no_error (error);
489   g_signal_connect (exported_bat_object,
490                     "handle-force-method",
491                     G_CALLBACK (on_handle_force_method),
492                     NULL);
493   g_object_set (exported_bat_object,
494                 "force-i", g_variant_new_int32 (43),
495                 "force-s", g_variant_new_string ("prop string"),
496                 "force-ay", g_variant_new_bytestring ("prop bytestring\xff"),
497                 "force-struct", g_variant_new ("(i)", 4300),
498                 NULL);
499
500   authorize_enclosing_object = g_dbus_object_skeleton_new ("/authorize");
501   g_signal_connect (authorize_enclosing_object,
502                     "authorize-method",
503                     G_CALLBACK (my_object_authorize_method_handler),
504                     NULL);
505   exported_authorize_object = foo_authorize_skeleton_new ();
506   g_dbus_object_skeleton_add_interface (authorize_enclosing_object,
507                                         G_DBUS_INTERFACE_SKELETON (exported_authorize_object));
508   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_authorize_object),
509                                     connection,
510                                     "/authorize",
511                                     &error);
512   g_assert_no_error (error);
513   g_signal_connect (exported_authorize_object,
514                     "g-authorize-method",
515                     G_CALLBACK (my_g_authorize_method_handler),
516                     NULL);
517   g_signal_connect (exported_authorize_object,
518                     "handle-check-not-authorized",
519                     G_CALLBACK (on_handle_check_not_authorized),
520                     NULL);
521   g_signal_connect (exported_authorize_object,
522                     "handle-check-authorized",
523                     G_CALLBACK (on_handle_check_authorized),
524                     NULL);
525   g_signal_connect (exported_authorize_object,
526                     "handle-check-not-authorized-from-object",
527                     G_CALLBACK (on_handle_check_not_authorized_from_object),
528                     NULL);
529
530
531   /* only object 1 has the G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD flag set */
532   exported_thread_object_1 = foo_method_threads_skeleton_new ();
533   g_dbus_interface_skeleton_set_flags (G_DBUS_INTERFACE_SKELETON (exported_thread_object_1),
534                                        G_DBUS_INTERFACE_SKELETON_FLAGS_HANDLE_METHOD_INVOCATIONS_IN_THREAD);
535   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_thread_object_1),
536                                     connection,
537                                     "/method_threads_1",
538                                     &error);
539   g_assert_no_error (error);
540   g_signal_connect (exported_thread_object_1,
541                     "handle-get-self",
542                     G_CALLBACK (on_handle_get_self),
543                     NULL);
544
545   exported_thread_object_2 = foo_method_threads_skeleton_new ();
546   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (exported_thread_object_2),
547                                     connection,
548                                     "/method_threads_2",
549                                     &error);
550   g_assert_no_error (error);
551   g_signal_connect (exported_thread_object_2,
552                     "handle-get-self",
553                     G_CALLBACK (on_handle_get_self),
554                     NULL);
555
556   method_handler_thread = g_thread_self ();
557 }
558
559 static gpointer check_proxies_in_thread (gpointer user_data);
560
561 static void
562 on_name_acquired (GDBusConnection *connection,
563                   const gchar     *name,
564                   gpointer         user_data)
565 {
566   GMainLoop *loop = user_data;
567
568   g_thread_create (check_proxies_in_thread,
569                    loop,
570                    TRUE,
571                    NULL);
572 }
573
574 static void
575 on_name_lost (GDBusConnection *connection,
576               const gchar     *name,
577               gpointer         user_data)
578 {
579   g_assert_not_reached ();
580 }
581
582 /* ---------------------------------------------------------------------------------------------------- */
583
584 typedef struct
585 {
586   GMainLoop *thread_loop;
587   gint initial_y;
588   gint initial_i;
589   guint num_g_properties_changed;
590   gboolean received_test_signal;
591   guint num_notify_u;
592   guint num_notify_n;
593 } ClientData;
594
595 static void
596 on_notify_u (GObject    *object,
597            GParamSpec *pspec,
598            gpointer    user_data)
599 {
600   ClientData *data = user_data;
601   g_assert_cmpstr (pspec->name, ==, "u");
602   data->num_notify_u += 1;
603 }
604
605 static void
606 on_notify_n (GObject    *object,
607              GParamSpec *pspec,
608              gpointer    user_data)
609 {
610   ClientData *data = user_data;
611   g_assert_cmpstr (pspec->name, ==, "n");
612   data->num_notify_n += 1;
613 }
614
615 static void
616 on_g_properties_changed (GDBusProxy          *_proxy,
617                          GVariant            *changed_properties,
618                          const gchar* const  *invalidated_properties,
619                          gpointer             user_data)
620 {
621   ClientData *data = user_data;
622   FooBar *proxy = FOO_BAR (_proxy);
623
624   g_assert_cmpint (g_variant_n_children (changed_properties), ==, 2);
625
626   if (data->num_g_properties_changed == 0)
627     {
628       g_assert_cmpint (data->initial_y, ==, foo_bar_get_y (proxy) - 2);
629       g_assert_cmpint (data->initial_i, ==, foo_bar_get_i (proxy) - 2);
630     }
631   else if (data->num_g_properties_changed == 1)
632     {
633       g_assert_cmpint (data->initial_y, ==, foo_bar_get_y (proxy) - 3);
634       g_assert_cmpint (data->initial_i, ==, foo_bar_get_i (proxy) - 3);
635     }
636   else
637     g_assert_not_reached ();
638
639   data->num_g_properties_changed++;
640
641   if (data->num_g_properties_changed == 2)
642     g_main_loop_quit (data->thread_loop);
643 }
644
645 static void
646 on_test_signal (FooBar              *proxy,
647                 gint                 val_int32,
648                 const gchar* const  *array_of_strings,
649                 const gchar* const  *array_of_bytestrings,
650                 GVariant            *dict_s_to_pairs,
651                 gpointer             user_data)
652 {
653   ClientData *data = user_data;
654
655   g_assert_cmpint (val_int32, ==, 43);
656   g_assert_cmpstr (array_of_strings[0], ==, "foo");
657   g_assert_cmpstr (array_of_strings[1], ==, "bar");
658   g_assert (array_of_strings[2] == NULL);
659   g_assert_cmpstr (array_of_bytestrings[0], ==, "foo\xff");
660   g_assert_cmpstr (array_of_bytestrings[1], ==, "bar\xff");
661   g_assert (array_of_bytestrings[2] == NULL);
662
663   data->received_test_signal = TRUE;
664   g_main_loop_quit (data->thread_loop);
665 }
666
667 static void
668 on_property_cancellation_cb (FooBar        *proxy,
669                              GAsyncResult  *res,
670                              gpointer       user_data)
671 {
672   ClientData *data = user_data;
673   gboolean ret;
674   GError *error = NULL;
675
676   error = NULL;
677   ret = foo_bar_call_property_cancellation_finish (proxy, res, &error);
678   g_assert_no_error (error);
679   g_assert (ret);
680
681   g_main_loop_quit (data->thread_loop);
682 }
683
684 static void
685 check_bar_proxy (FooBar    *proxy,
686                  GMainLoop *thread_loop)
687 {
688   guchar ret_val_byte;
689   gboolean ret_val_boolean;
690   gint16 ret_val_int16;
691   guint16 ret_val_uint16;
692   gint ret_val_int32;
693   guint ret_val_uint32;
694   gint64 ret_val_int64;
695   guint64 ret_val_uint64;
696   gdouble ret_val_double;
697   gchar *ret_val_string;
698   gchar *ret_val_objpath;
699   gchar *ret_val_signature;
700   gchar *ret_val_bytestring;
701   gboolean ret;
702   GError *error;
703   ClientData *data;
704   guchar val_y;
705   gboolean val_b;
706   gint val_n;
707   guint val_q;
708   gint val_i;
709   guint val_u;
710   gint64 val_x;
711   guint64 val_t;
712   gdouble val_d;
713   gchar *val_s;
714   gchar *val_o;
715   gchar *val_g;
716   gchar *val_ay;
717   gchar **val_as;
718   gchar **val_ao;
719   GVariant *val_ag;
720   gint32 val_unset_i;
721   gdouble val_unset_d;
722   gchar *val_unset_s;
723   gchar *val_unset_o;
724   gchar *val_unset_g;
725   gchar *val_unset_ay;
726   gchar **val_unset_as;
727   gchar **val_unset_ao;
728   GVariant *val_unset_ag;
729   GVariant *val_unset_struct;
730   gchar *val_finally_normal_name;
731   GVariant *v;
732   gchar *s;
733
734   data = g_new0 (ClientData, 1);
735   data->thread_loop = thread_loop;
736
737   v = g_dbus_proxy_get_cached_property (G_DBUS_PROXY (proxy), "y");
738   g_assert (v != NULL);
739   g_variant_unref (v);
740
741   /* set empty values to non-empty */
742   val_unset_i = 42;
743   val_unset_d = 42.0;
744   val_unset_s = "42";
745   val_unset_o = "42";
746   val_unset_g = "42";
747   val_unset_ay = NULL;
748   val_unset_as = NULL;
749   val_unset_ao = NULL;
750   val_unset_ag = NULL;
751   val_unset_struct = NULL;
752   /* check properties */
753   g_object_get (proxy,
754                 "y", &val_y,
755                 "b", &val_b,
756                 "n", &val_n,
757                 "q", &val_q,
758                 "i", &val_i,
759                 "u", &val_u,
760                 "x", &val_x,
761                 "t", &val_t,
762                 "d", &val_d,
763                 "s", &val_s,
764                 "o", &val_o,
765                 "g", &val_g,
766                 "ay", &val_ay,
767                 "as", &val_as,
768                 "ao", &val_ao,
769                 "ag", &val_ag,
770                 "unset_i", &val_unset_i,
771                 "unset_d", &val_unset_d,
772                 "unset_s", &val_unset_s,
773                 "unset_o", &val_unset_o,
774                 "unset_g", &val_unset_g,
775                 "unset_ay", &val_unset_ay,
776                 "unset_as", &val_unset_as,
777                 "unset_ao", &val_unset_ao,
778                 "unset_ag", &val_unset_ag,
779                 "unset_struct", &val_unset_struct,
780                 "finally-normal-name", &val_finally_normal_name,
781                 NULL);
782   g_assert_cmpint (val_y, ==, 42);
783   g_assert_cmpstr (val_finally_normal_name, ==, "There aint no place like home");
784   g_free (val_s);
785   g_free (val_o);
786   g_free (val_g);
787   g_assert_cmpstr (val_ay, ==, "ABCabc");
788   g_free (val_ay);
789   g_strfreev (val_as);
790   g_strfreev (val_ao);
791   g_variant_unref (val_ag);
792   g_free (val_finally_normal_name);
793   /* check empty values */
794   g_assert_cmpint (val_unset_i, ==, 0);
795   g_assert_cmpfloat (val_unset_d, ==, 0.0);
796   g_assert_cmpstr (val_unset_s, ==, "");
797   g_assert_cmpstr (val_unset_o, ==, "/");
798   g_assert_cmpstr (val_unset_g, ==, "");
799   g_free (val_unset_s);
800   g_free (val_unset_o);
801   g_free (val_unset_g);
802   g_assert_cmpstr (val_unset_ay, ==, "");
803   g_assert (val_unset_as[0] == NULL);
804   g_assert (val_unset_ao[0] == NULL);
805   g_assert (g_variant_is_of_type (val_unset_ag, G_VARIANT_TYPE ("ag")));
806   g_assert (g_variant_is_of_type (val_unset_struct, G_VARIANT_TYPE ("(idsogayasaoag)")));
807   s = g_variant_print (val_unset_struct, TRUE);
808   g_assert_cmpstr (s, ==, "(0, 0.0, '', objectpath '/', signature '', @ay [], @as [], @ao [], @ag [])");
809   g_free (s);
810   g_free (val_unset_ay);
811   g_strfreev (val_unset_as);
812   g_strfreev (val_unset_ao);
813   g_variant_unref (val_unset_ag);
814   g_variant_unref (val_unset_struct);
815
816   /* Try setting a property. This causes the generated glue to invoke
817    * the org.fd.DBus.Properties.Set() method asynchronously. So we
818    * have to wait for properties-changed...
819    */
820   foo_bar_set_finally_normal_name (proxy, "foo!");
821   _g_assert_property_notify (proxy, "finally-normal-name");
822   g_assert_cmpstr (foo_bar_get_finally_normal_name (proxy), ==, "foo!");
823
824   /* Try setting properties that requires memory management. This
825    * is to exercise the paths that frees the references.
826    */
827   const gchar *array_of_strings[3] = {"one", "two", NULL};
828   const gchar *array_of_objpaths[3] = {"/one", "/one/two", NULL};
829   const gchar *array_of_bytestrings[3] = {"one\xff", "two\xff", NULL};
830
831   g_object_set (proxy,
832                 "s", "a string",
833                 "o", "/a/path",
834                 "g", "asig",
835                 "ay", g_variant_new_parsed ("[byte 0x65, 0x67]"),
836                 "as", array_of_strings,
837                 "ao", array_of_objpaths,
838                 "ag", g_variant_new_parsed ("[@g 'ass', 'git']"),
839                 NULL);
840
841   error = NULL;
842   ret = foo_bar_call_test_primitive_types_sync (proxy,
843                                                 10,
844                                                 TRUE,
845                                                 11,
846                                                 12,
847                                                 13,
848                                                 14,
849                                                 15,
850                                                 16,
851                                                 17,
852                                                 "a string",
853                                                 "/a/path",
854                                                 "asig",
855                                                 "bytestring\xff",
856                                                 &ret_val_byte,
857                                                 &ret_val_boolean,
858                                                 &ret_val_int16,
859                                                 &ret_val_uint16,
860                                                 &ret_val_int32,
861                                                 &ret_val_uint32,
862                                                 &ret_val_int64,
863                                                 &ret_val_uint64,
864                                                 &ret_val_double,
865                                                 &ret_val_string,
866                                                 &ret_val_objpath,
867                                                 &ret_val_signature,
868                                                 &ret_val_bytestring,
869                                                 NULL, /* GCancellable */
870                                                 &error);
871   g_assert_no_error (error);
872   g_assert (ret);
873
874   error = NULL;
875   ret = foo_bar_call_test_non_primitive_types_sync (proxy,
876                                                     g_variant_new_parsed ("{'one': 'red',"
877                                                                           " 'two': 'blue'}"),
878                                                     g_variant_new_parsed ("{'first': (42, 42), "
879                                                                           "'second': (43, 43)}"),
880                                                     g_variant_new_parsed ("(42, 'foo', 'bar')"),
881                                                     array_of_strings,
882                                                     array_of_objpaths,
883                                                     g_variant_new_parsed ("[@g 'ass', 'git']"),
884                                                     array_of_bytestrings,
885                                                     &s,
886                                                     NULL, /* GCancellable */
887                                                     &error);
888
889   g_assert_no_error (error);
890   g_assert (ret);
891
892   /* Check that org.freedesktop.DBus.Error.UnknownMethod is returned on
893    * unimplemented methods.
894    */
895   error = NULL;
896   ret = foo_bar_call_unimplemented_method_sync (proxy, NULL /* GCancellable */, &error);
897   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
898   g_error_free (error);
899   error = NULL;
900   g_assert (!ret);
901
902   g_signal_connect (proxy,
903                     "test-signal",
904                     G_CALLBACK (on_test_signal),
905                     data);
906   error = NULL;
907   ret = foo_bar_call_request_signal_emission_sync (proxy, 0, NULL, &error);
908   g_assert_no_error (error);
909   g_assert (ret);
910
911   g_assert (!data->received_test_signal);
912   g_main_loop_run (thread_loop);
913   g_assert (data->received_test_signal);
914
915   /* Try setting a property. This causes the generated glue to invoke
916    * the org.fd.DBus.Properties.Set() method asynchronously. So we
917    * have to wait for properties-changed...
918    */
919   foo_bar_set_finally_normal_name (proxy, "hey back!");
920   _g_assert_property_notify (proxy, "finally-normal-name");
921   g_assert_cmpstr (foo_bar_get_finally_normal_name (proxy), ==, "hey back!");
922
923   /* Check that grouping changes in idle works.
924    *
925    * See on_handle_request_multi_property_mods(). The server should
926    * emit exactly two PropertiesChanged signals each containing two
927    * properties.
928    *
929    * On the first reception, y and i should both be increased by
930    * two. On the the second reception, only by one. The signal handler
931    * checks this.
932    *
933    * This also checks that _drain_notify() works.
934    */
935   data->initial_y = foo_bar_get_y (proxy);
936   data->initial_i = foo_bar_get_i (proxy);
937   g_signal_connect (proxy,
938                     "g-properties-changed",
939                     G_CALLBACK (on_g_properties_changed),
940                     data);
941   error = NULL;
942   ret = foo_bar_call_request_multi_property_mods_sync (proxy, NULL, &error);
943   g_assert_no_error (error);
944   g_assert (ret);
945   g_main_loop_run (thread_loop);
946   g_assert_cmpint (data->num_g_properties_changed, ==, 2);
947   g_signal_handlers_disconnect_by_func (proxy,
948                                         G_CALLBACK (on_g_properties_changed),
949                                         data);
950
951   /* Check that we don't emit PropertiesChanged() if the property
952    * didn't change... we actually get two notifies.. one for the
953    * local set (without a value change) and one when receiving
954    * the PropertiesChanged() signal generated from the remote end.
955    */
956   g_assert_cmpint (data->num_notify_u, ==, 0);
957   g_signal_connect (proxy,
958                     "notify::u",
959                     G_CALLBACK (on_notify_u),
960                     data);
961   foo_bar_set_u (proxy, 1042);
962   g_assert_cmpint (data->num_notify_u, ==, 1);
963   g_assert_cmpint (foo_bar_get_u (proxy), ==, 0);
964   _g_assert_property_notify (proxy, "u");
965   g_assert_cmpint (foo_bar_get_u (proxy), ==, 1042);
966   g_assert_cmpint (data->num_notify_u, ==, 2);
967
968   /* Now change u again to the same value.. this will cause a
969    * local notify:: notify and the usual Properties.Set() call
970    *
971    * (Btw, why also the Set() call if the value in the cache is
972    * the same? Because someone else might have changed it
973    * in the mean time and we're just waiting to receive the
974    * PropertiesChanged() signal...)
975    *
976    * More tricky - how do we check for the *absence* of the
977    * notification that u changed? Simple: we change another
978    * property and wait for that PropertiesChanged() message
979    * to arrive.
980    */
981   foo_bar_set_u (proxy, 1042);
982   g_assert_cmpint (data->num_notify_u, ==, 3);
983
984   g_assert_cmpint (data->num_notify_n, ==, 0);
985   g_signal_connect (proxy,
986                     "notify::n",
987                     G_CALLBACK (on_notify_n),
988                     data);
989   foo_bar_set_n (proxy, 10042);
990   g_assert_cmpint (data->num_notify_n, ==, 1);
991   g_assert_cmpint (foo_bar_get_n (proxy), ==, 0);
992   _g_assert_property_notify (proxy, "n");
993   g_assert_cmpint (foo_bar_get_n (proxy), ==, 10042);
994   g_assert_cmpint (data->num_notify_n, ==, 2);
995   /* Checks that u didn't change at all */
996   g_assert_cmpint (data->num_notify_u, ==, 3);
997
998   /* Now we check that if the service does
999    *
1000    *   guint n = foo_bar_get_n (foo);
1001    *   foo_bar_set_n (foo, n + 1);
1002    *   foo_bar_set_n (foo, n);
1003    *
1004    *  then no PropertiesChanged() signal is emitted!
1005    */
1006   error = NULL;
1007   foo_bar_call_property_cancellation (proxy,
1008                                       NULL, /* GCancellable */
1009                                       (GAsyncReadyCallback) on_property_cancellation_cb,
1010                                       data);
1011   g_main_loop_run (thread_loop);
1012   /* Checks that n didn't change at all */
1013   g_assert_cmpint (data->num_notify_n, ==, 2);
1014
1015   /* cleanup */
1016   g_free (data);
1017 }
1018
1019 /* ---------------------------------------------------------------------------------------------------- */
1020
1021 static void
1022 on_force_signal (FooBat    *proxy,
1023                  GVariant  *force_i,
1024                  GVariant  *force_s,
1025                  GVariant  *force_ay,
1026                  GVariant  *force_struct,
1027                  gpointer   user_data)
1028 {
1029   gboolean *signal_received = user_data;
1030   gint val;
1031
1032   g_assert (!(*signal_received));
1033
1034   g_assert_cmpint (g_variant_get_int32 (force_i), ==, 42 + 10);
1035   g_assert_cmpstr (g_variant_get_string (force_s, NULL), ==, "a string_foo");
1036   g_assert_cmpstr (g_variant_get_bytestring (force_ay), ==, "a bytestring\xff_foo\xff");
1037   g_variant_get (force_struct, "(i)", &val);
1038   g_assert_cmpint (val, ==, 4200 + 10);
1039
1040   *signal_received = TRUE;
1041 }
1042
1043 static void
1044 check_bat_proxy (FooBat *proxy,
1045                  GMainLoop *thread_loop)
1046 {
1047   GError *error;
1048   GVariant *ret_i;
1049   GVariant *ret_s;
1050   GVariant *ret_ay;
1051   GVariant *ret_struct;
1052   gint val;
1053   gboolean force_signal_received;
1054
1055   /* --------------------------------------------------- */
1056   /* Check type-mapping where we force use of a GVariant */
1057   /* --------------------------------------------------- */
1058
1059   /* check properties */
1060   g_object_get (proxy,
1061                 "force-i", &ret_i,
1062                 "force-s", &ret_s,
1063                 "force-ay", &ret_ay,
1064                 "force-struct", &ret_struct,
1065                 NULL);
1066   g_assert_cmpint (g_variant_get_int32 (ret_i), ==, 43);
1067   g_assert_cmpstr (g_variant_get_string (ret_s, NULL), ==, "prop string");
1068   g_assert_cmpstr (g_variant_get_bytestring (ret_ay), ==, "prop bytestring\xff");
1069   g_variant_get (ret_struct, "(i)", &val);
1070   g_assert_cmpint (val, ==, 4300);
1071   g_variant_unref (ret_i);
1072   g_variant_unref (ret_s);
1073   g_variant_unref (ret_ay);
1074   g_variant_unref (ret_struct);
1075
1076   /* check method and signal */
1077   force_signal_received = FALSE;
1078   g_signal_connect (proxy,
1079                     "force-signal",
1080                     G_CALLBACK (on_force_signal),
1081                     &force_signal_received);
1082
1083   error = NULL;
1084   foo_bat_call_force_method_sync (proxy,
1085                                   g_variant_new_int32 (42),
1086                                   g_variant_new_string ("a string"),
1087                                   g_variant_new_bytestring ("a bytestring\xff"),
1088                                   g_variant_new ("(i)", 4200),
1089                                   &ret_i,
1090                                   &ret_s,
1091                                   &ret_ay,
1092                                   &ret_struct,
1093                                   NULL, /* GCancellable* */
1094                                   &error);
1095   g_assert_no_error (error);
1096   g_assert_cmpint (g_variant_get_int32 (ret_i), ==, 42 + 10);
1097   g_assert_cmpstr (g_variant_get_string (ret_s, NULL), ==, "a string_foo");
1098   g_assert_cmpstr (g_variant_get_bytestring (ret_ay), ==, "a bytestring\xff_foo\xff");
1099   g_variant_get (ret_struct, "(i)", &val);
1100   g_assert_cmpint (val, ==, 4200 + 10);
1101   g_variant_unref (ret_i);
1102   g_variant_unref (ret_s);
1103   g_variant_unref (ret_ay);
1104   g_variant_unref (ret_struct);
1105   _g_assert_signal_received (proxy, "force-signal");
1106   g_assert (force_signal_received);
1107 }
1108
1109 /* ---------------------------------------------------------------------------------------------------- */
1110
1111 static void
1112 check_authorize_proxy (FooAuthorize *proxy,
1113                        GMainLoop *thread_loop)
1114 {
1115   GError *error;
1116   gboolean ret;
1117
1118   /* Check that g-authorize-method works as intended */
1119
1120   error = NULL;
1121   ret = foo_authorize_call_check_not_authorized_sync (proxy, NULL, &error);
1122   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PERMISSION_DENIED);
1123   g_error_free (error);
1124   g_assert (!ret);
1125
1126   error = NULL;
1127   ret = foo_authorize_call_check_authorized_sync (proxy, NULL, &error);
1128   g_assert_no_error (error);
1129   g_assert (ret);
1130
1131   error = NULL;
1132   ret = foo_authorize_call_check_not_authorized_from_object_sync (proxy, NULL, &error);
1133   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_PENDING);
1134   g_error_free (error);
1135   g_assert (!ret);
1136 }
1137
1138 /* ---------------------------------------------------------------------------------------------------- */
1139
1140 static GThread *
1141 get_self_via_proxy (FooMethodThreads *proxy_1)
1142 {
1143   GError *error;
1144   gchar *self_str;
1145   gboolean ret;
1146   gpointer self;
1147
1148   error = NULL;
1149   ret = foo_method_threads_call_get_self_sync (proxy_1, &self_str, NULL, &error);
1150   g_assert_no_error (error);
1151   g_assert (ret);
1152
1153   g_assert_cmpint (sscanf (self_str, "%p", &self), ==, 1);
1154
1155   g_free (self_str);
1156
1157   return self;
1158 }
1159
1160 static void
1161 check_thread_proxies (FooMethodThreads *proxy_1,
1162                       FooMethodThreads *proxy_2,
1163                       GMainLoop *thread_loop)
1164 {
1165   /* proxy_1 is indeed using threads so should never get the handler thread */
1166   g_assert (get_self_via_proxy (proxy_1) != method_handler_thread);
1167
1168   /* proxy_2 is not using threads so should get the handler thread */
1169   g_assert (get_self_via_proxy (proxy_2) == method_handler_thread);
1170 }
1171
1172 /* ---------------------------------------------------------------------------------------------------- */
1173
1174 static gpointer
1175 check_proxies_in_thread (gpointer user_data)
1176 {
1177   GMainLoop *loop = user_data;
1178   GMainContext *thread_context;
1179   GMainLoop *thread_loop;
1180   GError *error;
1181   FooBar *bar_proxy;
1182   FooBat *bat_proxy;
1183   FooAuthorize *authorize_proxy;
1184   FooMethodThreads *thread_proxy_1;
1185   FooMethodThreads *thread_proxy_2;
1186
1187   thread_context = g_main_context_new ();
1188   thread_loop = g_main_loop_new (thread_context, FALSE);
1189   g_main_context_push_thread_default (thread_context);
1190
1191   /* Check the object */
1192   error = NULL;
1193   bar_proxy = foo_bar_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1194                                               G_DBUS_PROXY_FLAGS_NONE,
1195                                               "org.gtk.GDBus.BindingsTool.Test",
1196                                               "/bar",
1197                                               NULL, /* GCancellable* */
1198                                               &error);
1199   check_bar_proxy (bar_proxy, thread_loop);
1200   g_assert_no_error (error);
1201   g_object_unref (bar_proxy);
1202
1203   error = NULL;
1204   bat_proxy = foo_bat_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1205                                               G_DBUS_PROXY_FLAGS_NONE,
1206                                               "org.gtk.GDBus.BindingsTool.Test",
1207                                               "/bat",
1208                                               NULL, /* GCancellable* */
1209                                               &error);
1210   check_bat_proxy (bat_proxy, thread_loop);
1211   g_assert_no_error (error);
1212   g_object_unref (bat_proxy);
1213
1214   error = NULL;
1215   authorize_proxy = foo_authorize_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1216                                                           G_DBUS_PROXY_FLAGS_NONE,
1217                                                           "org.gtk.GDBus.BindingsTool.Test",
1218                                                           "/authorize",
1219                                                           NULL, /* GCancellable* */
1220                                                           &error);
1221   check_authorize_proxy (authorize_proxy, thread_loop);
1222   g_assert_no_error (error);
1223   g_object_unref (authorize_proxy);
1224
1225   error = NULL;
1226   thread_proxy_1 = foo_method_threads_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1227                                                               G_DBUS_PROXY_FLAGS_NONE,
1228                                                               "org.gtk.GDBus.BindingsTool.Test",
1229                                                               "/method_threads_1",
1230                                                               NULL, /* GCancellable* */
1231                                                               &error);
1232   g_assert_no_error (error);
1233   thread_proxy_2 = foo_method_threads_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
1234                                                               G_DBUS_PROXY_FLAGS_NONE,
1235                                                               "org.gtk.GDBus.BindingsTool.Test",
1236                                                               "/method_threads_2",
1237                                                               NULL, /* GCancellable* */
1238                                                               &error);
1239   g_assert_no_error (error);
1240   check_thread_proxies (thread_proxy_1, thread_proxy_2, thread_loop);
1241   g_object_unref (thread_proxy_1);
1242   g_object_unref (thread_proxy_2);
1243
1244   g_main_loop_unref (thread_loop);
1245   g_main_context_unref (thread_context);
1246
1247   /* this breaks out of the loop in main() (below) */
1248   g_main_loop_quit (loop);
1249   return NULL;
1250 }
1251
1252 /* ---------------------------------------------------------------------------------------------------- */
1253
1254 typedef struct
1255 {
1256   gchar *xml;
1257   GMainLoop *loop;
1258 } IntrospectData;
1259
1260 static void
1261 introspect_cb (GDBusConnection   *connection,
1262                GAsyncResult      *res,
1263                gpointer           user_data)
1264 {
1265   IntrospectData *data = user_data;
1266   GVariant *result;
1267   GError *error;
1268
1269   error = NULL;
1270   result = g_dbus_connection_call_finish (connection,
1271                                           res,
1272                                           &error);
1273   g_assert_no_error (error);
1274   g_assert (result != NULL);
1275   g_variant_get (result, "(s)", &data->xml);
1276   g_variant_unref (result);
1277
1278   g_main_loop_quit (data->loop);
1279 }
1280
1281 static GDBusNodeInfo *
1282 introspect (GDBusConnection  *connection,
1283             const gchar      *name,
1284             const gchar      *object_path,
1285             GMainLoop        *loop)
1286 {
1287   GError *error;
1288   GDBusNodeInfo *node_info;
1289   IntrospectData *data;
1290
1291   data = g_new0 (IntrospectData, 1);
1292   data->xml = NULL;
1293   data->loop = loop;
1294
1295   /* do this async to avoid deadlocks */
1296   g_dbus_connection_call (connection,
1297                           name,
1298                           object_path,
1299                           "org.freedesktop.DBus.Introspectable",
1300                           "Introspect",
1301                           NULL, /* params */
1302                           G_VARIANT_TYPE ("(s)"),
1303                           G_DBUS_CALL_FLAGS_NONE,
1304                           -1,
1305                           NULL,
1306                           (GAsyncReadyCallback) introspect_cb,
1307                           data);
1308   g_main_loop_run (loop);
1309   g_assert (data->xml != NULL);
1310
1311   error = NULL;
1312   node_info = g_dbus_node_info_new_for_xml (data->xml, &error);
1313   g_assert_no_error (error);
1314   g_assert (node_info != NULL);
1315   g_free (data->xml);
1316   g_free (data);
1317
1318   return node_info;
1319 }
1320
1321 static guint
1322 count_interfaces (GDBusNodeInfo *info)
1323 {
1324   guint n;
1325   for (n = 0; info->interfaces != NULL && info->interfaces[n] != NULL; n++)
1326     ;
1327   return n;
1328 }
1329
1330 static guint
1331 count_nodes (GDBusNodeInfo *info)
1332 {
1333   guint n;
1334   for (n = 0; info->nodes != NULL && info->nodes[n] != NULL; n++)
1335     ;
1336   return n;
1337 }
1338
1339 static guint
1340 has_interface (GDBusNodeInfo *info,
1341                const gchar   *name)
1342 {
1343   guint n;
1344   for (n = 0; info->interfaces != NULL && info->interfaces[n] != NULL; n++)
1345     {
1346       if (g_strcmp0 (info->interfaces[n]->name, name) == 0)
1347         return TRUE;
1348     }
1349   return FALSE;
1350 }
1351
1352 /* ---------------------------------------------------------------------------------------------------- */
1353
1354 typedef struct {
1355   GMainLoop *loop;
1356   GVariant *result;
1357 } OMGetManagedObjectsData;
1358
1359 static void
1360 om_get_all_cb (GDBusConnection *connection,
1361                GAsyncResult    *res,
1362                gpointer         user_data)
1363 {
1364   OMGetManagedObjectsData *data = user_data;
1365   GError *error;
1366
1367   error = NULL;
1368   data->result = g_dbus_connection_call_finish (connection,
1369                                                 res,
1370                                                 &error);
1371   g_assert_no_error (error);
1372   g_assert (data->result != NULL);
1373   g_main_loop_quit (data->loop);
1374 }
1375
1376 static void
1377 om_check_get_all (GDBusConnection *c,
1378                   GMainLoop       *loop,
1379                   const gchar     *str)
1380 {
1381   OMGetManagedObjectsData data;
1382   gchar *s;
1383
1384   data.loop = loop;
1385   data.result = NULL;
1386
1387   /* do this async to avoid deadlocks */
1388   g_dbus_connection_call (c,
1389                           g_dbus_connection_get_unique_name (c),
1390                           "/managed",
1391                           "org.freedesktop.DBus.ObjectManager",
1392                           "GetManagedObjects",
1393                           NULL, /* params */
1394                           G_VARIANT_TYPE ("(a{oa{sa{sv}}})"),
1395                           G_DBUS_CALL_FLAGS_NONE,
1396                           -1,
1397                           NULL,
1398                           (GAsyncReadyCallback) om_get_all_cb,
1399                           &data);
1400   g_main_loop_run (loop);
1401   g_assert (data.result != NULL);
1402   s = g_variant_print (data.result, TRUE);
1403   g_assert_cmpstr (s, ==, str);
1404   g_free (s);
1405   g_variant_unref (data.result);
1406 }
1407
1408 typedef struct
1409 {
1410   GMainLoop *loop;
1411   guint state;
1412
1413   guint num_object_proxy_added_signals;
1414   guint num_object_proxy_removed_signals;
1415   guint num_interface_added_signals;
1416   guint num_interface_removed_signals;
1417 } OMData;
1418
1419 static gint
1420 my_pstrcmp (const gchar **a, const gchar **b)
1421 {
1422   return g_strcmp0 (*a, *b);
1423 }
1424
1425 static void
1426 om_check_interfaces_added (const gchar *signal_name,
1427                            GVariant *parameters,
1428                            const gchar *object_path,
1429                            const gchar *first_interface_name,
1430                            ...)
1431 {
1432   const gchar *path;
1433   GVariant *array;
1434   guint n;
1435   GPtrArray *interfaces;
1436   GPtrArray *interfaces_in_message;
1437   va_list var_args;
1438   const gchar *str;
1439
1440   interfaces = g_ptr_array_new ();
1441   g_ptr_array_add (interfaces, (gpointer) first_interface_name);
1442   va_start (var_args, first_interface_name);
1443   do
1444     {
1445       str = va_arg (var_args, const gchar *);
1446       if (str == NULL)
1447         break;
1448       g_ptr_array_add (interfaces, (gpointer) str);
1449     }
1450   while (TRUE);
1451   va_end (var_args);
1452
1453   g_variant_get (parameters, "(&o*)", &path, &array);
1454   g_assert_cmpstr (signal_name, ==, "InterfacesAdded");
1455   g_assert_cmpstr (path, ==, object_path);
1456   g_assert_cmpint (g_variant_n_children (array), ==, interfaces->len);
1457   interfaces_in_message = g_ptr_array_new ();
1458   for (n = 0; n < interfaces->len; n++)
1459     {
1460       const gchar *iface_name;
1461       g_variant_get_child (array, n, "{&sa{sv}}", &iface_name, NULL);
1462       g_ptr_array_add (interfaces_in_message, (gpointer) iface_name);
1463     }
1464   g_assert_cmpint (interfaces_in_message->len, ==, interfaces->len);
1465   g_ptr_array_sort (interfaces, (GCompareFunc) my_pstrcmp);
1466   g_ptr_array_sort (interfaces_in_message, (GCompareFunc) my_pstrcmp);
1467   for (n = 0; n < interfaces->len; n++)
1468     g_assert_cmpstr (interfaces->pdata[n], ==, interfaces_in_message->pdata[n]);
1469   g_ptr_array_unref (interfaces_in_message);
1470   g_ptr_array_unref (interfaces);
1471   g_variant_unref (array);
1472 }
1473
1474 static void
1475 om_check_interfaces_removed (const gchar *signal_name,
1476                              GVariant *parameters,
1477                              const gchar *object_path,
1478                              const gchar *first_interface_name,
1479                              ...)
1480 {
1481   const gchar *path;
1482   GVariant *array;
1483   guint n;
1484   GPtrArray *interfaces;
1485   GPtrArray *interfaces_in_message;
1486   va_list var_args;
1487   const gchar *str;
1488
1489   interfaces = g_ptr_array_new ();
1490   g_ptr_array_add (interfaces, (gpointer) first_interface_name);
1491   va_start (var_args, first_interface_name);
1492   do
1493     {
1494       str = va_arg (var_args, const gchar *);
1495       if (str == NULL)
1496         break;
1497       g_ptr_array_add (interfaces, (gpointer) str);
1498     }
1499   while (TRUE);
1500   va_end (var_args);
1501
1502   g_variant_get (parameters, "(&o*)", &path, &array);
1503   g_assert_cmpstr (signal_name, ==, "InterfacesRemoved");
1504   g_assert_cmpstr (path, ==, object_path);
1505   g_assert_cmpint (g_variant_n_children (array), ==, interfaces->len);
1506   interfaces_in_message = g_ptr_array_new ();
1507   for (n = 0; n < interfaces->len; n++)
1508     {
1509       const gchar *iface_name;
1510       g_variant_get_child (array, n, "&s", &iface_name, NULL);
1511       g_ptr_array_add (interfaces_in_message, (gpointer) iface_name);
1512     }
1513   g_assert_cmpint (interfaces_in_message->len, ==, interfaces->len);
1514   g_ptr_array_sort (interfaces, (GCompareFunc) my_pstrcmp);
1515   g_ptr_array_sort (interfaces_in_message, (GCompareFunc) my_pstrcmp);
1516   for (n = 0; n < interfaces->len; n++)
1517     g_assert_cmpstr (interfaces->pdata[n], ==, interfaces_in_message->pdata[n]);
1518   g_ptr_array_unref (interfaces_in_message);
1519   g_ptr_array_unref (interfaces);
1520   g_variant_unref (array);
1521 }
1522
1523 static void
1524 om_on_signal (GDBusConnection *connection,
1525               const gchar     *sender_name,
1526               const gchar     *object_path,
1527               const gchar     *interface_name,
1528               const gchar     *signal_name,
1529               GVariant        *parameters,
1530               gpointer         user_data)
1531 {
1532   OMData *om_data = user_data;
1533
1534   //g_debug ("foo: %s", g_variant_print (parameters, TRUE));
1535
1536   switch (om_data->state)
1537     {
1538     default:
1539     case 0:
1540       g_print ("failing and om_data->state=%d on signal %s, params=%s\n",
1541                om_data->state,
1542                signal_name,
1543                g_variant_print (parameters, TRUE));
1544       g_assert_not_reached ();
1545       break;
1546
1547     case 1:
1548       om_check_interfaces_added (signal_name, parameters, "/managed/first",
1549                                  "org.project.Bar", NULL);
1550       om_data->state = 2;
1551       g_main_loop_quit (om_data->loop);
1552       break;
1553
1554     case 3:
1555       om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1556                                    "org.project.Bar", NULL);
1557       om_data->state = 5;
1558       /* keep running the loop */
1559       break;
1560
1561     case 5:
1562       om_check_interfaces_added (signal_name, parameters, "/managed/first",
1563                                  "org.project.Bar", NULL);
1564       om_data->state = 6;
1565       g_main_loop_quit (om_data->loop);
1566       break;
1567
1568     case 7:
1569       om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1570                                    "org.project.Bar", NULL);
1571       om_data->state = 9;
1572       /* keep running the loop */
1573       break;
1574
1575     case 9:
1576       om_check_interfaces_added (signal_name, parameters, "/managed/first",
1577                                  "org.project.Bar", NULL);
1578       om_data->state = 10;
1579       g_main_loop_quit (om_data->loop);
1580       break;
1581
1582     case 11:
1583       om_check_interfaces_added (signal_name, parameters, "/managed/first",
1584                                  "org.project.Bat", NULL);
1585       om_data->state = 12;
1586       g_main_loop_quit (om_data->loop);
1587       break;
1588
1589     case 13:
1590       om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1591                                    "org.project.Bar", NULL);
1592       om_data->state = 14;
1593       g_main_loop_quit (om_data->loop);
1594       break;
1595
1596     case 15:
1597       om_check_interfaces_removed (signal_name, parameters, "/managed/first",
1598                                    "org.project.Bat", NULL);
1599       om_data->state = 16;
1600       g_main_loop_quit (om_data->loop);
1601       break;
1602
1603     case 17:
1604       om_check_interfaces_added (signal_name, parameters, "/managed/first",
1605                                  "com.acme.Coyote", NULL);
1606       om_data->state = 18;
1607       g_main_loop_quit (om_data->loop);
1608       break;
1609
1610     case 101:
1611       om_check_interfaces_added (signal_name, parameters, "/managed/second",
1612                                  "org.project.Bat", "org.project.Bar", NULL);
1613       om_data->state = 102;
1614       g_main_loop_quit (om_data->loop);
1615       break;
1616
1617     case 103:
1618       om_check_interfaces_removed (signal_name, parameters, "/managed/second",
1619                                    "org.project.Bat", "org.project.Bar", NULL);
1620       om_data->state = 104;
1621       g_main_loop_quit (om_data->loop);
1622       break;
1623     }
1624 }
1625
1626 static GAsyncResult *om_res = NULL;
1627
1628 static void
1629 om_pm_start_cb (FooObjectManagerClient *manager,
1630                 GAsyncResult      *res,
1631                 gpointer           user_data)
1632 {
1633   GMainLoop *loop = user_data;
1634   om_res = g_object_ref (res);
1635   g_main_loop_quit (loop);
1636 }
1637
1638 static void
1639 on_interface_added (GDBusObject    *object,
1640                     GDBusInterface *interface,
1641                     gpointer        user_data)
1642 {
1643   OMData *om_data = user_data;
1644   om_data->num_interface_added_signals += 1;
1645 }
1646
1647 static void
1648 on_interface_removed (GDBusObject    *object,
1649                       GDBusInterface *interface,
1650                       gpointer        user_data)
1651 {
1652   OMData *om_data = user_data;
1653   om_data->num_interface_removed_signals += 1;
1654 }
1655
1656 static void
1657 on_object_proxy_added (GDBusObjectManagerClient  *manager,
1658                        GDBusObjectProxy   *object_proxy,
1659                        gpointer            user_data)
1660 {
1661   OMData *om_data = user_data;
1662   om_data->num_object_proxy_added_signals += 1;
1663   g_signal_connect (object_proxy,
1664                     "interface-added",
1665                     G_CALLBACK (on_interface_added),
1666                     om_data);
1667   g_signal_connect (object_proxy,
1668                     "interface-removed",
1669                     G_CALLBACK (on_interface_removed),
1670                     om_data);
1671 }
1672
1673 static void
1674 on_object_proxy_removed (GDBusObjectManagerClient  *manager,
1675                          GDBusObjectProxy   *object_proxy,
1676                          gpointer            user_data)
1677 {
1678   OMData *om_data = user_data;
1679   om_data->num_object_proxy_removed_signals += 1;
1680   g_assert_cmpint (g_signal_handlers_disconnect_by_func (object_proxy,
1681                                                          G_CALLBACK (on_interface_added),
1682                                                          om_data), ==, 1);
1683   g_assert_cmpint (g_signal_handlers_disconnect_by_func (object_proxy,
1684                                                          G_CALLBACK (on_interface_removed),
1685                                                          om_data), ==, 1);
1686 }
1687
1688 static void
1689 om_check_property_and_signal_emission (GMainLoop  *loop,
1690                                        FooBar     *skeleton,
1691                                        FooBar     *proxy)
1692 {
1693   /* First PropertiesChanged */
1694   g_assert_cmpint (foo_bar_get_i (skeleton), ==, 0);
1695   g_assert_cmpint (foo_bar_get_i (proxy), ==, 0);
1696   foo_bar_set_i (skeleton, 1);
1697   _g_assert_property_notify (proxy, "i");
1698   g_assert_cmpint (foo_bar_get_i (skeleton), ==, 1);
1699   g_assert_cmpint (foo_bar_get_i (proxy), ==, 1);
1700
1701   /* Then just a regular signal */
1702   foo_bar_emit_another_signal (skeleton, "word");
1703   _g_assert_signal_received (proxy, "another-signal");
1704 }
1705
1706 static void
1707 check_object_manager (void)
1708 {
1709   FooObjectSkeleton *o;
1710   FooObjectSkeleton *o2;
1711   GDBusInterfaceSkeleton *i;
1712   GDBusConnection *c;
1713   GDBusObjectManagerServer *manager;
1714   GDBusNodeInfo *info;
1715   GError *error;
1716   GMainLoop *loop;
1717   OMData *om_data;
1718   guint om_signal_id;
1719   GDBusObjectManager *pm;
1720   GList *object_proxies;
1721   GList *proxies;
1722   GDBusObject *op;
1723   GDBusProxy *p;
1724   FooBar *bar_skeleton;
1725
1726   loop = g_main_loop_new (NULL, FALSE);
1727
1728   om_data = g_new0 (OMData, 1);
1729   om_data->loop = loop;
1730   om_data->state = 0;
1731
1732   error = NULL;
1733   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
1734   g_assert_no_error (error);
1735   g_assert (c != NULL);
1736
1737   om_signal_id = g_dbus_connection_signal_subscribe (c,
1738                                                      NULL, /* sender */
1739                                                      "org.freedesktop.DBus.ObjectManager",
1740                                                      NULL, /* member */
1741                                                      NULL, /* object_path */
1742                                                      NULL, /* arg0 */
1743                                                      G_DBUS_SIGNAL_FLAGS_NONE,
1744                                                      om_on_signal,
1745                                                      om_data,
1746                                                      NULL); /* user_data_free_func */
1747
1748   /* Our GDBusObjectManagerClient tests are simple - we basically just count the
1749    * number of times the various signals have been emitted (we don't check
1750    * that the right objects/interfaces are passed though - that's checked
1751    * in the lower-level tests in om_on_signal()...)
1752    *
1753    * Note that these tests rely on the D-Bus signal handlers used by
1754    * GDBusObjectManagerClient firing before om_on_signal().
1755    */
1756   error = NULL;
1757   pm = foo_object_manager_client_new_sync (c,
1758                                            G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
1759                                            g_dbus_connection_get_unique_name (c),
1760                                            "/managed",
1761                                            NULL, /* GCancellable */
1762                                            &error);
1763   g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
1764   g_error_free (error);
1765   g_assert (pm == NULL);
1766
1767   manager = g_dbus_object_manager_server_new ("/managed");
1768   g_dbus_object_manager_server_set_connection (manager, c);
1769
1770   /* Check that the manager object is visible */
1771   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed", loop);
1772   g_assert_cmpint (count_interfaces (info), ==, 4); /* ObjectManager + Properties,Introspectable,Peer */
1773   g_assert (has_interface (info, "org.freedesktop.DBus.ObjectManager"));
1774   g_assert_cmpint (count_nodes (info), ==, 0);
1775   g_dbus_node_info_unref (info);
1776
1777   /* Check GetManagedObjects() - should be empty since we have no objects */
1778   om_check_get_all (c, loop,
1779                     "(@a{oa{sa{sv}}} {},)");
1780
1781   /* Now try to create the the proxy manager again - this time it should work */
1782   error = NULL;
1783   foo_object_manager_client_new (c,
1784                                  G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
1785                                  g_dbus_connection_get_unique_name (c),
1786                                  "/managed",
1787                                  NULL, /* GCancellable */
1788                                  (GAsyncReadyCallback) om_pm_start_cb,
1789                                  loop);
1790   g_main_loop_run (loop);
1791   error = NULL;
1792   pm = foo_object_manager_client_new_finish (om_res, &error);
1793   g_object_unref (om_res);
1794   g_assert_no_error (error);
1795   g_assert (pm != NULL);
1796   g_signal_connect (pm,
1797                     "object-added",
1798                     G_CALLBACK (on_object_proxy_added),
1799                     om_data);
1800   g_signal_connect (pm,
1801                     "object-removed",
1802                     G_CALLBACK (on_object_proxy_removed),
1803                     om_data);
1804   /* ... check there are no object proxies yet */
1805   object_proxies = g_dbus_object_manager_get_objects (pm);
1806   g_assert (object_proxies == NULL);
1807
1808   /* First, export an object with a single interface (also check that
1809    * g_dbus_interface_get_object() works and that the object isn't reffed)
1810    */
1811   o = foo_object_skeleton_new ("/managed/first");
1812   i = G_DBUS_INTERFACE_SKELETON (foo_bar_skeleton_new ());
1813   g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == NULL);
1814   g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1815   foo_object_skeleton_set_bar (o, FOO_BAR (i));
1816   g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1817   g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == G_DBUS_OBJECT (o));
1818   g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1819   g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1820   foo_object_skeleton_set_bar (o, NULL);
1821   g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == NULL);
1822   g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1823   foo_object_skeleton_set_bar (o, FOO_BAR (i));
1824   g_assert (g_dbus_interface_get_object (G_DBUS_INTERFACE (i)) == G_DBUS_OBJECT (o));
1825   g_assert_cmpint (G_OBJECT (o)->ref_count, ==, 1);
1826   g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (o));
1827
1828   /* ... check we get the InterfacesAdded signal */
1829   om_data->state = 1;
1830   g_main_loop_run (om_data->loop);
1831   g_assert_cmpint (om_data->state, ==, 2);
1832   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 1);
1833   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 0);
1834   g_assert_cmpint (om_data->num_interface_added_signals, ==, 0);
1835   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
1836   /* ... check there's one non-standard interfaces */
1837   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1838   g_assert_cmpint (count_interfaces (info), ==, 4); /* Bar + Properties,Introspectable,Peer */
1839   g_assert (has_interface (info, "org.project.Bar"));
1840   g_dbus_node_info_unref (info);
1841
1842   /* Now, check adding the same interface replaces the existing one */
1843   foo_object_skeleton_set_bar (o, FOO_BAR (i));
1844   /* ... check we get the InterfacesRemoved */
1845   om_data->state = 3;
1846   g_main_loop_run (om_data->loop);
1847   /* ... and then check we get the InterfacesAdded */
1848   g_assert_cmpint (om_data->state, ==, 6);
1849   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 2);
1850   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 1);
1851   g_assert_cmpint (om_data->num_interface_added_signals, ==, 0);
1852   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
1853   /* ... check introspection data */
1854   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1855   g_assert_cmpint (count_interfaces (info), ==, 4); /* Bar + Properties,Introspectable,Peer */
1856   g_assert (has_interface (info, "org.project.Bar"));
1857   g_dbus_node_info_unref (info);
1858   g_object_unref (i);
1859
1860   /* check adding an interface of same type (but not same object) replaces the existing one */
1861   i = G_DBUS_INTERFACE_SKELETON (foo_bar_skeleton_new ());
1862   foo_object_skeleton_set_bar (o, FOO_BAR (i));
1863   /* ... check we get the InterfacesRemoved and then InterfacesAdded */
1864   om_data->state = 7;
1865   g_main_loop_run (om_data->loop);
1866   g_assert_cmpint (om_data->state, ==, 10);
1867   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
1868   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 2);
1869   g_assert_cmpint (om_data->num_interface_added_signals, ==, 0);
1870   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
1871   /* ... check introspection data */
1872   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1873   g_assert_cmpint (count_interfaces (info), ==, 4); /* Bar + Properties,Introspectable,Peer */
1874   g_assert (has_interface (info, "org.project.Bar"));
1875   g_dbus_node_info_unref (info);
1876   g_object_unref (i);
1877
1878   /* check adding an interface of another type doesn't replace the existing one */
1879   i = G_DBUS_INTERFACE_SKELETON (foo_bat_skeleton_new ());
1880   foo_object_skeleton_set_bat (o, FOO_BAT (i));
1881   g_object_unref (i);
1882   /* ... check we get the InterfacesAdded */
1883   om_data->state = 11;
1884   g_main_loop_run (om_data->loop);
1885   g_assert_cmpint (om_data->state, ==, 12);
1886   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
1887   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 2);
1888   g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
1889   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 0);
1890   /* ... check introspection data */
1891   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1892   g_assert_cmpint (count_interfaces (info), ==, 5); /* Bar,Bat + Properties,Introspectable,Peer */
1893   g_assert (has_interface (info, "org.project.Bar"));
1894   g_assert (has_interface (info, "org.project.Bat"));
1895   g_dbus_node_info_unref (info);
1896
1897   /* check we can remove an interface */
1898   foo_object_skeleton_set_bar (o, NULL);
1899   /* ... check we get the InterfacesRemoved */
1900   om_data->state = 13;
1901   g_main_loop_run (om_data->loop);
1902   g_assert_cmpint (om_data->state, ==, 14);
1903   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
1904   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 2);
1905   g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
1906   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
1907   /* ... check introspection data */
1908   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1909   g_assert_cmpint (count_interfaces (info), ==, 4); /* Bat + Properties,Introspectable,Peer */
1910   g_assert (has_interface (info, "org.project.Bat"));
1911   g_dbus_node_info_unref (info);
1912   /* also and that the call only has effect if the interface actually exists
1913    *
1914    * (Note: if a signal was emitted we'd assert in the signal handler
1915    * because we're in state 14)
1916    */
1917   foo_object_skeleton_set_bar (o, NULL);
1918   /* ... check introspection data */
1919   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1920   g_assert_cmpint (count_interfaces (info), ==, 4); /* Bat + Properties,Introspectable,Peer */
1921   g_assert (has_interface (info, "org.project.Bat"));
1922   g_dbus_node_info_unref (info);
1923
1924   /* remove the last interface */
1925   foo_object_skeleton_set_bat (o, NULL);
1926   /* ... check we get the InterfacesRemoved */
1927   om_data->state = 15;
1928   g_main_loop_run (om_data->loop);
1929   g_assert_cmpint (om_data->state, ==, 16);
1930   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 3);
1931   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 3);
1932   g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
1933   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
1934   /* ... check introspection data */
1935   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1936   g_assert_cmpint (count_interfaces (info), ==, 0); /* nothing */
1937   g_dbus_node_info_unref (info);
1938
1939   /* and add an interface again */
1940   i = G_DBUS_INTERFACE_SKELETON (foo_com_acme_coyote_skeleton_new ());
1941   foo_object_skeleton_set_com_acme_coyote (o, FOO_COM_ACME_COYOTE (i));
1942   g_object_unref (i);
1943   /* ... check we get the InterfacesAdded */
1944   om_data->state = 17;
1945   g_main_loop_run (om_data->loop);
1946   g_assert_cmpint (om_data->state, ==, 18);
1947   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 4);
1948   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 3);
1949   g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
1950   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
1951   /* ... check introspection data */
1952   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/first", loop);
1953   g_assert_cmpint (count_interfaces (info), ==, 4); /* com.acme.Coyote + Properties,Introspectable,Peer */
1954   g_assert (has_interface (info, "com.acme.Coyote"));
1955   g_dbus_node_info_unref (info);
1956
1957   /* Check GetManagedObjects() - should be just the Coyote */
1958   om_check_get_all (c, loop,
1959                     "({objectpath '/managed/first': {'com.acme.Coyote': {'Mood': <''>}}},)");
1960
1961   /* -------------------------------------------------- */
1962
1963   /* create a new object with two interfaces */
1964   o2 = foo_object_skeleton_new ("/managed/second");
1965   i = G_DBUS_INTERFACE_SKELETON (foo_bar_skeleton_new ());
1966   bar_skeleton = FOO_BAR (i); /* save for later test */
1967   foo_object_skeleton_set_bar (o2, FOO_BAR (i));
1968   g_object_unref (i);
1969   i = G_DBUS_INTERFACE_SKELETON (foo_bat_skeleton_new ());
1970   foo_object_skeleton_set_bat (o2, FOO_BAT (i));
1971   g_object_unref (i);
1972   /* ... add it */
1973   g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (o2));
1974   /* ... check we get the InterfacesAdded with _two_ interfaces */
1975   om_data->state = 101;
1976   g_main_loop_run (om_data->loop);
1977   g_assert_cmpint (om_data->state, ==, 102);
1978   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 5);
1979   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 3);
1980   g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
1981   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
1982
1983   /* -------------------------------------------------- */
1984
1985   /* Now that we have a couple of objects with interfaces, check
1986    * that ObjectManager.GetManagedObjects() works
1987    */
1988   om_check_get_all (c, loop,
1989                     "({objectpath '/managed/first': {'com.acme.Coyote': {'Mood': <''>}}, '/managed/second': {'org.project.Bar': {'y': <byte 0x00>, 'b': <false>, 'n': <int16 0>, 'q': <uint16 0>, 'i': <0>, 'u': <uint32 0>, 'x': <int64 0>, 't': <uint64 0>, 'd': <0.0>, 's': <''>, 'o': <objectpath '/'>, 'g': <signature ''>, 'ay': <b''>, 'as': <@as []>, 'aay': <@aay []>, 'ao': <@ao []>, 'ag': <@ag []>, 'FinallyNormalName': <''>, 'ReadonlyProperty': <''>, 'unset_i': <0>, 'unset_d': <0.0>, 'unset_s': <''>, 'unset_o': <objectpath '/'>, 'unset_g': <signature ''>, 'unset_ay': <b''>, 'unset_as': <@as []>, 'unset_ao': <@ao []>, 'unset_ag': <@ag []>, 'unset_struct': <(0, 0.0, '', objectpath '/', signature '', @ay [], @as [], @ao [], @ag [])>}, 'org.project.Bat': {'force_i': <0>, 'force_s': <''>, 'force_ay': <@ay []>, 'force_struct': <(0,)>}}},)");
1990
1991   /* Set connection to NULL, causing everything to be unexported.. verify this.. and
1992    * then set the connection back.. and then check things still work
1993    */
1994   g_dbus_object_manager_server_set_connection (manager, NULL);
1995   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed", loop);
1996   g_assert_cmpint (count_interfaces (info), ==, 0); /* nothing */
1997   g_dbus_node_info_unref (info);
1998   g_dbus_object_manager_server_set_connection (manager, c);
1999   om_check_get_all (c, loop,
2000                     "({objectpath '/managed/first': {'com.acme.Coyote': {'Mood': <''>}}, '/managed/second': {'org.project.Bar': {'y': <byte 0x00>, 'b': <false>, 'n': <int16 0>, 'q': <uint16 0>, 'i': <0>, 'u': <uint32 0>, 'x': <int64 0>, 't': <uint64 0>, 'd': <0.0>, 's': <''>, 'o': <objectpath '/'>, 'g': <signature ''>, 'ay': <b''>, 'as': <@as []>, 'aay': <@aay []>, 'ao': <@ao []>, 'ag': <@ag []>, 'FinallyNormalName': <''>, 'ReadonlyProperty': <''>, 'unset_i': <0>, 'unset_d': <0.0>, 'unset_s': <''>, 'unset_o': <objectpath '/'>, 'unset_g': <signature ''>, 'unset_ay': <b''>, 'unset_as': <@as []>, 'unset_ao': <@ao []>, 'unset_ag': <@ag []>, 'unset_struct': <(0, 0.0, '', objectpath '/', signature '', @ay [], @as [], @ao [], @ag [])>}, 'org.project.Bat': {'force_i': <0>, 'force_s': <''>, 'force_ay': <@ay []>, 'force_struct': <(0,)>}}},)");
2001
2002   /* Also check that the ObjectManagerClient returns these objects - and
2003    * that they are of the right GType cf. what was requested via
2004    * the generated ::get-proxy-type signal handler
2005    */
2006   object_proxies = g_dbus_object_manager_get_objects (pm);
2007   g_assert (g_list_length (object_proxies) == 2);
2008   g_list_foreach (object_proxies, (GFunc) g_object_unref, NULL);
2009   g_list_free (object_proxies);
2010   op = g_dbus_object_manager_get_object (pm, "/managed/first");
2011   g_assert (op != NULL);
2012   g_assert (FOO_IS_OBJECT_PROXY (op));
2013   g_assert_cmpstr (g_dbus_object_get_object_path (op), ==, "/managed/first");
2014   proxies = g_dbus_object_get_interfaces (op);
2015   g_assert (g_list_length (proxies) == 1);
2016   g_list_foreach (proxies, (GFunc) g_object_unref, NULL);
2017   g_list_free (proxies);
2018   p = G_DBUS_PROXY (foo_object_get_com_acme_coyote (FOO_OBJECT (op)));
2019   g_assert (p != NULL);
2020   g_assert_cmpint (G_TYPE_FROM_INSTANCE (p), ==, FOO_TYPE_COM_ACME_COYOTE_PROXY);
2021   g_assert (g_type_is_a (G_TYPE_FROM_INSTANCE (p), FOO_TYPE_COM_ACME_COYOTE));
2022   g_object_unref (p);
2023   p = (GDBusProxy *) g_dbus_object_get_interface (op, "org.project.NonExisting");
2024   g_assert (p == NULL);
2025   g_object_unref (op);
2026   /* -- */
2027   op = g_dbus_object_manager_get_object (pm, "/managed/second");
2028   g_assert (op != NULL);
2029   g_assert (FOO_IS_OBJECT_PROXY (op));
2030   g_assert_cmpstr (g_dbus_object_get_object_path (op), ==, "/managed/second");
2031   proxies = g_dbus_object_get_interfaces (op);
2032   g_assert (g_list_length (proxies) == 2);
2033   g_list_foreach (proxies, (GFunc) g_object_unref, NULL);
2034   g_list_free (proxies);
2035   p = G_DBUS_PROXY (foo_object_get_bat (FOO_OBJECT (op)));
2036   g_assert (p != NULL);
2037   g_assert_cmpint (G_TYPE_FROM_INSTANCE (p), ==, FOO_TYPE_BAT_PROXY);
2038   g_assert (g_type_is_a (G_TYPE_FROM_INSTANCE (p), FOO_TYPE_BAT));
2039   g_object_unref (p);
2040   p = G_DBUS_PROXY (foo_object_get_bar (FOO_OBJECT (op)));
2041   g_assert (p != NULL);
2042   g_assert_cmpint (G_TYPE_FROM_INSTANCE (p), ==, FOO_TYPE_BAR_PROXY);
2043   g_assert (g_type_is_a (G_TYPE_FROM_INSTANCE (p), FOO_TYPE_BAR));
2044   /* ... now that we have a Bar instance around, also check that we get signals
2045    *     and property changes...
2046    */
2047   om_check_property_and_signal_emission (loop, bar_skeleton, FOO_BAR (p));
2048   g_object_unref (p);
2049   p = (GDBusProxy *) g_dbus_object_get_interface (op, "org.project.NonExisting");
2050   g_assert (p == NULL);
2051   g_object_unref (op);
2052
2053   /* -------------------------------------------------- */
2054
2055   /* Now remove the second object added above */
2056   g_dbus_object_manager_server_unexport (manager, "/managed/second");
2057   /* ... check we get InterfacesRemoved with both interfaces */
2058   om_data->state = 103;
2059   g_main_loop_run (om_data->loop);
2060   g_assert_cmpint (om_data->state, ==, 104);
2061   g_assert_cmpint (om_data->num_object_proxy_added_signals, ==, 5);
2062   g_assert_cmpint (om_data->num_object_proxy_removed_signals, ==, 4);
2063   g_assert_cmpint (om_data->num_interface_added_signals, ==, 1);
2064   g_assert_cmpint (om_data->num_interface_removed_signals, ==, 1);
2065   /* ... check introspection data (there should be nothing) */
2066   info = introspect (c, g_dbus_connection_get_unique_name (c), "/managed/second", loop);
2067   g_assert_cmpint (count_nodes (info), ==, 0);
2068   g_assert_cmpint (count_interfaces (info), ==, 0);
2069   g_dbus_node_info_unref (info);
2070
2071   /* Check GetManagedObjects() again */
2072   om_check_get_all (c, loop,
2073                     "({objectpath '/managed/first': {'com.acme.Coyote': {'Mood': <''>}}},)");
2074
2075   //g_main_loop_run (loop); /* TODO: tmp */
2076
2077   g_main_loop_unref (loop);
2078
2079   g_dbus_connection_signal_unsubscribe (c, om_signal_id);
2080   g_object_unref (o2);
2081   g_object_unref (o);
2082   g_object_unref (manager);
2083   g_assert_cmpint (g_signal_handlers_disconnect_by_func (pm,
2084                                                          G_CALLBACK (on_object_proxy_added),
2085                                                          om_data), ==, 1);
2086   g_assert_cmpint (g_signal_handlers_disconnect_by_func (pm,
2087                                                          G_CALLBACK (on_object_proxy_removed),
2088                                                          om_data), ==, 1);
2089   g_object_unref (pm);
2090   g_object_unref (c);
2091
2092   g_free (om_data);
2093 }
2094
2095 /* ---------------------------------------------------------------------------------------------------- */
2096
2097 static void
2098 test_object_manager (void)
2099 {
2100   GMainLoop *loop;
2101   guint id;
2102
2103   loop = g_main_loop_new (NULL, FALSE);
2104
2105   id = g_bus_own_name (G_BUS_TYPE_SESSION,
2106                        "org.gtk.GDBus.BindingsTool.Test",
2107                        G_BUS_NAME_OWNER_FLAGS_NONE,
2108                        on_bus_acquired,
2109                        on_name_acquired,
2110                        on_name_lost,
2111                        loop,
2112                        NULL);
2113
2114   g_main_loop_run (loop);
2115
2116   check_object_manager ();
2117
2118   /* uncomment to keep the service around (to e.g. introspect it) */
2119   /* g_main_loop_run (loop); */
2120
2121   g_bus_unown_name (id);
2122   g_main_loop_unref (loop);
2123 }
2124
2125 /* ---------------------------------------------------------------------------------------------------- */
2126 /* This checks that forcing names via org.gtk.GDBus.Name works (see test-codegen.xml) */
2127
2128 extern gpointer name_forcing_1;
2129 extern gpointer name_forcing_2;
2130 extern gpointer name_forcing_3;
2131 extern gpointer name_forcing_4;
2132 extern gpointer name_forcing_5;
2133 extern gpointer name_forcing_6;
2134 extern gpointer name_forcing_7;
2135 gpointer name_forcing_1 = foo_rocket123_get_type;
2136 gpointer name_forcing_2 = foo_rocket123_call_ignite_xyz;
2137 gpointer name_forcing_3 = foo_rocket123_emit_exploded_xyz;
2138 gpointer name_forcing_4 = foo_rocket123_get_speed_xyz;
2139 gpointer name_forcing_5 = foo_test_ugly_case_interface_call_get_iscsi_servers;
2140 gpointer name_forcing_6 = foo_test_ugly_case_interface_emit_servers_updated_now;
2141 gpointer name_forcing_7 = foo_test_ugly_case_interface_get_ugly_name;
2142
2143 /* ---------------------------------------------------------------------------------------------------- */
2144
2145 /* See https://bugzilla.gnome.org/show_bug.cgi?id=647577#c5 for details */
2146
2147 #define CHECK_FIELD(name, v1, v2) g_assert_cmpint (G_STRUCT_OFFSET (FooChangingInterface##v1##Iface, name), ==, G_STRUCT_OFFSET (FooChangingInterface##v2##Iface, name));
2148
2149 static void
2150 test_interface_stability (void)
2151 {
2152   CHECK_FIELD(handle_foo_method, V1, V2);
2153   CHECK_FIELD(handle_bar_method, V1, V2);
2154   CHECK_FIELD(handle_baz_method, V1, V2);
2155   CHECK_FIELD(foo_signal, V1, V2);
2156   CHECK_FIELD(bar_signal, V1, V2);
2157   CHECK_FIELD(baz_signal, V1, V2);
2158   CHECK_FIELD(handle_new_method_in2, V2, V10);
2159   CHECK_FIELD(new_signal_in2, V2, V10);
2160 }
2161
2162 #undef CHECK_FIELD
2163
2164 /* ---------------------------------------------------------------------------------------------------- */
2165
2166 int
2167 main (int   argc,
2168       char *argv[])
2169 {
2170   gint ret;
2171
2172   g_type_init ();
2173   g_test_init (&argc, &argv, NULL);
2174
2175   /* all the tests use a session bus with a well-known address that we can bring up and down
2176    * using session_bus_up() and session_bus_down().
2177    */
2178   g_unsetenv ("DISPLAY");
2179   g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
2180
2181   session_bus_up ();
2182
2183   /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
2184    * until one can connect to the bus but that's not how things work right now
2185    */
2186   usleep (500 * 1000);
2187
2188   g_test_add_func ("/gdbus/codegen/annotations", test_annotations);
2189   g_test_add_func ("/gdbus/codegen/interface_stability", test_interface_stability);
2190   g_test_add_func ("/gdbus/codegen/object-manager", test_object_manager);
2191
2192   ret = g_test_run();
2193
2194   /* tear down bus */
2195   session_bus_down ();
2196
2197   return ret;
2198 }