2006-01-27 Robert McQueen <robot101@debian.org>
[platform/upstream/dbus.git] / test / glib / test-dbus-glib.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 #include <dbus/dbus-glib.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include "test-service-glib-bindings.h"
7 #include <glib/dbus-gidl.h>
8 #include <glib/dbus-gparser.h>
9 #include <glib.h>
10 #include <glib-object.h>
11 #include "my-object-marshal.h"
12
13 static GMainLoop *loop = NULL;
14 static const char *await_terminating_service = NULL;
15 static int n_times_foo_received = 0;
16 static int n_times_frobnicate_received = 0;
17 static int n_times_frobnicate_received_2 = 0;
18 static int n_times_sig0_received = 0;
19 static int n_times_sig1_received = 0;
20 static int n_times_sig2_received = 0;
21 static guint exit_timeout = 0;
22 static gboolean proxy_destroyed = FALSE;
23 static gboolean proxy_destroy_and_nameowner = FALSE;
24 static gboolean proxy_destroy_and_nameowner_complete = FALSE;
25
26 static void lose (const char *fmt, ...) G_GNUC_NORETURN G_GNUC_PRINTF (1, 2);
27 static void lose_gerror (const char *prefix, GError *error) G_GNUC_NORETURN;
28
29 static void
30 unset_and_free_gvalue (gpointer val)
31 {
32   g_value_unset (val);
33   g_free (val);
34 }
35
36 static gboolean
37 timed_exit (gpointer loop)
38 {
39   g_print ("timed exit!\n");
40   g_main_loop_quit (loop);
41   return TRUE;
42 }
43
44 static void
45 proxy_destroyed_cb (DBusGProxy *proxy, gpointer user_data)
46 {
47   proxy_destroyed = TRUE;
48   if (proxy_destroy_and_nameowner && !proxy_destroy_and_nameowner_complete && await_terminating_service == NULL)
49     {
50       g_source_remove (exit_timeout);
51       g_main_loop_quit (loop);
52       proxy_destroy_and_nameowner_complete = TRUE;
53     } 
54 }
55
56 static void
57 name_owner_changed (DBusGProxy *proxy,
58                     const char *name,
59                     const char *prev_owner,
60                     const char *new_owner,
61                     gpointer   user_data)
62 {
63   g_print ("(signal NameOwnerChanged) name owner changed for %s from %s to %s\n",
64            name, prev_owner, new_owner);
65   if (await_terminating_service &&
66       !strcmp (name, await_terminating_service)
67       && !strcmp ("", new_owner))
68     {
69       g_print ("Caught expected ownership loss for %s\n", name); 
70       await_terminating_service = NULL;
71       if (proxy_destroy_and_nameowner && !proxy_destroy_and_nameowner_complete && proxy_destroyed)
72         {
73           g_source_remove (exit_timeout);
74           g_main_loop_quit (loop);
75           proxy_destroy_and_nameowner_complete = TRUE;
76         } 
77       else if (!proxy_destroy_and_nameowner)
78         {
79           g_source_remove (exit_timeout);
80           g_main_loop_quit (loop);
81         }
82     }
83 }
84
85 static void
86 foo_signal_handler (DBusGProxy  *proxy,
87                     double       d,
88                     void        *user_data)
89 {
90   n_times_foo_received += 1;
91
92   g_print ("Got Foo signal\n");
93
94   g_main_loop_quit (loop);
95   g_source_remove (exit_timeout);
96 }
97
98 static void
99 frobnicate_signal_handler (DBusGProxy  *proxy,
100                            int          val,
101                            void        *user_data)
102 {
103   n_times_frobnicate_received += 1;
104
105   g_assert (val == 42);
106   g_print ("Got Frobnicate signal\n");
107
108   g_main_loop_quit (loop);
109   g_source_remove (exit_timeout);
110 }
111
112 static void
113 frobnicate_signal_handler_2 (DBusGProxy  *proxy,
114                              int          val,
115                              void        *user_data)
116 {
117   n_times_frobnicate_received_2 += 1;
118
119   g_assert (val == 42);
120   g_print ("Got Frobnicate signal (again)\n");
121 }
122
123 static void
124 sig0_signal_handler (DBusGProxy  *proxy,
125                      const char  *str0,
126                      int          val,
127                      const char  *str1,
128                      void        *user_data)
129 {
130   n_times_sig0_received += 1;
131
132   g_assert (!strcmp (str0, "foo"));
133
134   g_assert (val == 22);
135
136   g_assert (!strcmp (str1, "moo"));
137
138   g_print ("Got Sig0 signal\n");
139
140   g_main_loop_quit (loop);
141   g_source_remove (exit_timeout);
142 }
143
144 static void
145 sig1_signal_handler (DBusGProxy  *proxy,
146                      const char  *str0,
147                      GValue      *value,
148                      void        *user_data)
149 {
150   n_times_sig1_received += 1;
151
152   g_assert (!strcmp (str0, "baz"));
153
154   g_assert (G_VALUE_HOLDS_STRING (value));
155
156   g_assert (!strcmp (g_value_get_string (value), "bar"));
157
158   g_print ("Got Sig1 signal\n");
159
160   g_main_loop_quit (loop);
161   g_source_remove (exit_timeout);
162 }
163
164 static void
165 sig2_signal_handler (DBusGProxy  *proxy,
166                      GHashTable  *table,
167                      void        *user_data)
168 {
169   n_times_sig2_received += 1;
170
171   g_assert (g_hash_table_size (table) == 2);
172
173   g_assert (g_hash_table_lookup (table, "baz") != NULL);
174   g_assert (!strcmp (g_hash_table_lookup (table, "baz"), "cow"));
175   g_assert (g_hash_table_lookup (table, "bar") != NULL);
176   g_assert (!strcmp (g_hash_table_lookup (table, "bar"), "foo"));
177
178   g_print ("Got Sig2 signal\n");
179
180   g_main_loop_quit (loop);
181   g_source_remove (exit_timeout);
182 }
183
184 static DBusGProxyCall *echo_call;
185 static guint n_times_echo_cb_entered;
186 static void
187 echo_received_cb (DBusGProxy *proxy,
188                   DBusGProxyCall *call,
189                   gpointer data)
190 {
191   GError *error;
192   char *echo_data;
193
194   g_assert (call == echo_call);
195   g_assert (data == NULL);
196
197   error = NULL;
198   echo_data = NULL;
199   n_times_echo_cb_entered++;
200
201   if (!dbus_g_proxy_end_call (proxy, call, &error,
202                               G_TYPE_STRING,
203                               &echo_data,
204                               G_TYPE_INVALID))
205     lose_gerror ("Failed to complete async Echo", error);
206   g_assert (echo_data != NULL);
207   g_print ("Async echo gave \"%s\"\n", echo_data); 
208   g_free (echo_data);
209   g_main_loop_quit (loop);
210   g_source_remove (exit_timeout);
211 }
212
213 static void
214 increment_received_cb (DBusGProxy *proxy,
215                        DBusGProxyCall *call,
216                        gpointer data)
217 {
218   GError *error;
219   guint val;
220
221   g_assert (!strcmp (data, "moo"));
222
223   error = NULL;
224   if (!dbus_g_proxy_end_call (proxy, call, &error,
225                               G_TYPE_UINT, &val,
226                               G_TYPE_INVALID))
227     lose_gerror ("Failed to complete (async) Increment call", error);
228
229   if (val != 43)
230     lose ("Increment call returned %d, should be 43", val);
231   
232   g_print ("Async increment gave \"%d\"\n", val); 
233   g_main_loop_quit (loop);
234   g_source_remove (exit_timeout);
235 }
236
237 static void
238 increment_async_cb (DBusGProxy *proxy, guint val, GError *error, gpointer data)
239 {
240   if (error)
241     lose_gerror ("Failed to complete (wrapped async) Increment call", error);
242
243   if (data != NULL)
244     lose ("(wrapped async) Increment call gave unexpected data");
245   if (val != 43)
246     lose ("(wrapped async) Increment call returned %d, should be 43", val);
247
248   g_print ("(wrapped async) increment gave \"%d\"\n", val); 
249   g_main_loop_quit (loop);
250   g_source_remove (exit_timeout);
251 }
252
253
254 static void
255 lose (const char *str, ...)
256 {
257   va_list args;
258
259   va_start (args, str);
260
261   vfprintf (stderr, str, args);
262   fputc ('\n', stderr);
263
264   va_end (args);
265
266   exit (1);
267 }
268
269 static void
270 lose_gerror (const char *prefix, GError *error) 
271 {
272   if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION)
273     lose ("%s (%s): %s", prefix, dbus_g_error_get_name (error),
274           error->message);
275   else
276     lose ("%s: %s", prefix, error->message);
277 }
278
279 static void
280 run_mainloop (void)
281 {
282   GMainContext *ctx;
283
284   ctx = g_main_loop_get_context (loop);
285
286   while (g_main_context_pending (ctx))
287     g_main_context_iteration (ctx, FALSE);
288 }
289
290 int
291 main (int argc, char **argv)
292 {
293   DBusGConnection *connection;
294   GError *error;
295   DBusGProxy *driver;
296   DBusGProxy *proxy;
297   DBusGProxy *proxy2;
298   char **name_list;
299   guint name_list_len;
300   guint i;
301   DBusGProxyCall *call;
302   guint32 result;
303   char *v_STRING_2;
304   guint32 v_UINT32_2;
305   double v_DOUBLE_2;
306     
307   g_type_init ();
308
309   g_log_set_always_fatal (G_LOG_LEVEL_WARNING);
310   
311   loop = g_main_loop_new (NULL, FALSE);
312
313   error = NULL;
314   connection = dbus_g_bus_get (DBUS_BUS_SESSION,
315                                &error);
316   if (connection == NULL)
317     lose_gerror ("Failed to open connection to bus", error);
318
319   /* should always get the same one */
320   g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
321   g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
322   g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
323   
324   /* Create a proxy object for the "bus driver" */
325   
326   driver = dbus_g_proxy_new_for_name (connection,
327                                       DBUS_SERVICE_DBUS,
328                                       DBUS_PATH_DBUS,
329                                       DBUS_INTERFACE_DBUS);
330
331   dbus_g_proxy_add_signal (driver,
332                            "NameOwnerChanged",
333                            G_TYPE_STRING,
334                            G_TYPE_STRING,
335                            G_TYPE_STRING,
336                            G_TYPE_INVALID);
337   
338   dbus_g_proxy_connect_signal (driver,
339                                "NameOwnerChanged", 
340                                G_CALLBACK (name_owner_changed),
341                                NULL,
342                                NULL);
343   /* Call ListNames method */
344   
345   error = NULL;
346   if (!dbus_g_proxy_call (driver, "ListNames", &error,
347                           G_TYPE_INVALID,
348                           G_TYPE_STRV, &name_list,
349                           G_TYPE_INVALID))
350     lose_gerror ("Failed to complete ListNames call", error);
351
352   g_print ("Names on the message bus:\n");
353   i = 0;
354   name_list_len = g_strv_length (name_list);
355   while (i < name_list_len)
356     {
357       g_assert (name_list[i] != NULL);
358       g_print ("  %s\n", name_list[i]);
359       ++i;
360     }
361   g_assert (name_list[i] == NULL);
362
363   g_strfreev (name_list);
364
365   g_print ("calling ThisMethodDoesNotExist\n");
366   /* Test handling of unknown method */
367   if (dbus_g_proxy_call (driver, "ThisMethodDoesNotExist", &error,
368                          G_TYPE_STRING,
369                          "blah blah blah blah blah",
370                          G_TYPE_INT,
371                          10,
372                          G_TYPE_INVALID, G_TYPE_INVALID) != FALSE)
373     lose ("Calling nonexistent method succeeded!");
374
375   g_print ("Got EXPECTED error from calling unknown method: %s\n", error->message);
376   g_clear_error (&error);
377
378   run_mainloop ();
379   
380   /* Activate a service */
381   g_print ("Activating echo service\n");
382   if (!dbus_g_proxy_call (driver, "StartServiceByName", &error,
383                           G_TYPE_STRING,
384                           "org.freedesktop.DBus.TestSuiteEchoService",
385                           G_TYPE_UINT, 0,
386                           G_TYPE_INVALID,
387                           G_TYPE_UINT, &result,
388                           G_TYPE_INVALID))
389     lose_gerror ("Failed to complete Activate call", error);
390
391   g_print ("Starting echo service result = 0x%x\n", result);
392
393   /* Activate a service again */
394   g_print ("Activating echo service again\n");
395   if (!dbus_g_proxy_call (driver, "StartServiceByName", &error,
396                           G_TYPE_STRING,
397                           "org.freedesktop.DBus.TestSuiteEchoService",
398                           G_TYPE_UINT,
399                           0,
400                           G_TYPE_INVALID,
401                           G_TYPE_UINT, &result,
402                           G_TYPE_INVALID))
403     lose_gerror ("Failed to complete Activate call", error);
404
405   g_print ("Duplicate start of echo service = 0x%x\n", result);
406
407   /* Talk to the new service */
408   
409   g_print ("Creating proxy for echo service\n");
410   proxy = dbus_g_proxy_new_for_name_owner (connection,
411                                            "org.freedesktop.DBus.TestSuiteEchoService",
412                                            "/org/freedesktop/TestSuite",
413                                            "org.freedesktop.TestSuite",
414                                            &error);
415   
416   if (proxy == NULL)
417     lose_gerror ("Failed to create proxy for name owner", error);
418
419   run_mainloop ();
420
421   g_print ("Calling Echo\n");
422   if (!dbus_g_proxy_call (proxy, "Echo", &error,
423                           G_TYPE_STRING, "my string hello",
424                           G_TYPE_INVALID,
425                           G_TYPE_STRING, &v_STRING_2,
426                           G_TYPE_INVALID))
427     lose_gerror ("Failed to complete Echo call", error);
428
429   g_print ("String echoed = \"%s\"\n", v_STRING_2);
430   g_free (v_STRING_2);
431
432   g_print ("Calling Echo (async)\n");
433   echo_call = dbus_g_proxy_begin_call (proxy, "Echo",
434                                        echo_received_cb, NULL, NULL,
435                                        G_TYPE_STRING, "my string hello",
436                                        G_TYPE_INVALID);
437   dbus_g_connection_flush (connection);
438   exit_timeout = g_timeout_add (5000, timed_exit, loop);
439   g_main_loop_run (loop);
440
441   /* Test oneway call and signal handling */
442
443   g_print ("Testing Foo emission\n");
444   dbus_g_proxy_add_signal (proxy, "Foo", G_TYPE_DOUBLE, G_TYPE_INVALID);
445   
446   dbus_g_proxy_connect_signal (proxy, "Foo",
447                                G_CALLBACK (foo_signal_handler),
448                                NULL, NULL);
449   
450   dbus_g_proxy_call_no_reply (proxy, "EmitFoo",
451                               G_TYPE_INVALID);
452   
453   dbus_g_connection_flush (connection);
454   exit_timeout = g_timeout_add (5000, timed_exit, loop);
455   g_main_loop_run (loop);
456
457   if (n_times_foo_received != 1)
458     lose ("Foo signal received %d times, should have been 1", n_times_foo_received);
459   
460   /* Activate test servie */ 
461   g_print ("Activating TestSuiteGLibService\n");
462   error = NULL;
463   if (!dbus_g_proxy_call (driver, "StartServiceByName", &error,
464                           G_TYPE_STRING,
465                           "org.freedesktop.DBus.TestSuiteGLibService",
466                           G_TYPE_UINT,
467                           0,
468                           G_TYPE_INVALID,
469                           G_TYPE_UINT, &result,
470                           G_TYPE_INVALID)) {
471     lose_gerror ("Failed to complete Activate call", error);
472   }
473
474   g_print ("TestSuiteGLibService activated\n");
475
476   if (getenv ("DBUS_GLIB_TEST_SLEEP_AFTER_ACTIVATION"))
477     g_usleep (8 * G_USEC_PER_SEC);
478
479   g_object_unref (G_OBJECT (proxy));
480
481   run_mainloop ();
482
483   proxy = dbus_g_proxy_new_for_name_owner (connection,
484                                            "org.freedesktop.DBus.TestSuiteGLibService",
485                                            "/org/freedesktop/DBus/Tests/MyTestObject",
486                                            "org.freedesktop.DBus.Tests.MyObject",
487                                            &error);
488   
489   if (proxy == NULL)
490     lose_gerror ("Failed to create proxy for name owner", error);
491
492   g_print ("Calling DoNothing\n");
493   if (!dbus_g_proxy_call (proxy, "DoNothing", &error,
494                           G_TYPE_INVALID, G_TYPE_INVALID))
495     lose_gerror ("Failed to complete DoNothing call", error);
496
497   g_print ("Calling Increment\n");
498   error = NULL;
499   if (!dbus_g_proxy_call (proxy, "Increment", &error,
500                           G_TYPE_UINT, 42,
501                           G_TYPE_INVALID,
502                           G_TYPE_UINT, &v_UINT32_2,
503                           G_TYPE_INVALID))
504     lose_gerror ("Failed to complete Increment call", error);
505   if (v_UINT32_2 != 43)
506     lose ("Increment call returned %d, should be 43", v_UINT32_2);
507
508   v_UINT32_2 = 0;
509   g_print ("Calling Increment (async)\n");
510   call = dbus_g_proxy_begin_call (proxy, "Increment",
511                                   increment_received_cb, g_strdup ("moo"), g_free,
512                                   G_TYPE_UINT, 42,
513                                   G_TYPE_INVALID);
514   dbus_g_connection_flush (connection);
515   exit_timeout = g_timeout_add (5000, timed_exit, loop);
516   g_main_loop_run (loop);
517
518   g_print ("Calling IncrementRetval\n");
519   error = NULL;
520   v_UINT32_2 = 0;
521   if (!dbus_g_proxy_call (proxy, "IncrementRetval", &error,
522                           G_TYPE_UINT, 42,
523                           G_TYPE_INVALID,
524                           G_TYPE_UINT, &v_UINT32_2,
525                           G_TYPE_INVALID))
526     lose_gerror ("Failed to complete Increment call", error);
527   if (v_UINT32_2 != 43)
528     lose ("IncrementRetval call returned %d, should be 43", v_UINT32_2);
529
530   g_print ("Calling IncrementRetvalError\n");
531   error = NULL;
532   v_UINT32_2 = 0;
533   if (!dbus_g_proxy_call (proxy, "IncrementRetvalError", &error,
534                           G_TYPE_UINT, 5,
535                           G_TYPE_INVALID,
536                           G_TYPE_UINT, &v_UINT32_2,
537                           G_TYPE_INVALID))
538     lose_gerror ("Failed to complete Increment call", error);
539   if (v_UINT32_2 != 6)
540     lose ("IncrementRetval call returned %d, should be 6", v_UINT32_2);
541
542   g_print ("Calling ThrowError\n");
543   if (dbus_g_proxy_call (proxy, "ThrowError", &error,
544                          G_TYPE_INVALID, G_TYPE_INVALID) != FALSE)
545     lose ("ThrowError call unexpectedly succeeded!");
546
547   if (!dbus_g_error_has_name (error, "org.freedesktop.DBus.Tests.MyObject.Foo"))
548     lose ("ThrowError call returned unexpected error \"%s\": %s", dbus_g_error_get_name (error),
549           error->message);
550
551   g_print ("ThrowError failed (as expected) returned error: %s\n", error->message);
552   g_clear_error (&error);
553
554   g_print ("Calling IncrementRetvalError (for error)\n");
555   error = NULL;
556   v_UINT32_2 = 0;
557   if (dbus_g_proxy_call (proxy, "IncrementRetvalError", &error,
558                          G_TYPE_UINT, 20,
559                          G_TYPE_INVALID,
560                          G_TYPE_UINT, &v_UINT32_2,
561                          G_TYPE_INVALID) != FALSE)
562     lose ("IncrementRetvalError call unexpectedly succeeded!");
563   if (!dbus_g_error_has_name (error, "org.freedesktop.DBus.Tests.MyObject.Foo"))
564     lose ("IncrementRetvalError call returned unexpected error \"%s\": %s", dbus_g_error_get_name (error), error->message);
565   g_clear_error (&error);
566
567   error = NULL;
568   g_print ("Calling Uppercase\n");
569   if (!dbus_g_proxy_call (proxy, "Uppercase", &error,
570                           G_TYPE_STRING, "foobar",
571                           G_TYPE_INVALID,
572                           G_TYPE_STRING, &v_STRING_2,
573                           G_TYPE_INVALID))
574     lose_gerror ("Failed to complete Uppercase call", error);
575   if (strcmp ("FOOBAR", v_STRING_2) != 0)
576     lose ("Uppercase call returned unexpected string %s", v_STRING_2);
577   g_free (v_STRING_2);
578
579   run_mainloop ();
580
581   g_print ("Calling ManyArgs\n");
582   if (!dbus_g_proxy_call (proxy, "ManyArgs", &error,
583                           G_TYPE_UINT, 26,
584                           G_TYPE_STRING, "bazwhee",
585                           G_TYPE_DOUBLE, G_PI,
586                           G_TYPE_INVALID,
587                           G_TYPE_DOUBLE, &v_DOUBLE_2,
588                           G_TYPE_STRING, &v_STRING_2,
589                           G_TYPE_INVALID))
590     lose_gerror ("Failed to complete ManyArgs call", error);
591   if (v_DOUBLE_2 < 55 || v_DOUBLE_2 > 56)
592     lose ("ManyArgs call returned unexpected double value %f", v_DOUBLE_2);
593   if (strcmp ("BAZWHEE", v_STRING_2) != 0)
594     lose ("ManyArgs call returned unexpected string %s", v_STRING_2);
595   g_free (v_STRING_2);
596
597   g_print ("Calling (wrapped) do_nothing\n");
598   if (!org_freedesktop_DBus_Tests_MyObject_do_nothing (proxy, &error))
599     lose_gerror ("Failed to complete (wrapped) DoNothing call", error);
600
601   g_print ("Calling (wrapped) increment\n");
602   if (!org_freedesktop_DBus_Tests_MyObject_increment (proxy, 42, &v_UINT32_2, &error))
603     lose_gerror ("Failed to complete (wrapped) Increment call", error);
604
605   if (v_UINT32_2 != 43)
606     lose ("(wrapped) increment call returned %d, should be 43", v_UINT32_2);
607
608   g_print ("Calling (wrapped async) increment\n");
609   if (!org_freedesktop_DBus_Tests_MyObject_increment_async (proxy, 42, increment_async_cb, NULL))
610     lose_gerror ("Failed to complete (wrapped) Increment call", error);
611   dbus_g_connection_flush (connection);
612   exit_timeout = g_timeout_add (5000, timed_exit, loop);
613   g_main_loop_run (loop);
614
615   v_UINT32_2 = 0;
616   if (!org_freedesktop_DBus_Tests_MyObject_async_increment (proxy, 42, &v_UINT32_2, &error))
617     lose_gerror ("Failed to complete (wrapped) AsyncIncrement call", error);
618
619   if (v_UINT32_2 != 43)
620     lose ("(wrapped) async increment call returned %d, should be 43", v_UINT32_2);
621
622   g_print ("Calling (wrapped) throw_error\n");
623   if (org_freedesktop_DBus_Tests_MyObject_throw_error (proxy, &error) != FALSE)
624     lose ("(wrapped) ThrowError call unexpectedly succeeded!");
625
626   g_print ("(wrapped) ThrowError failed (as expected) returned error: %s\n", error->message);
627   g_clear_error (&error);
628
629   if (org_freedesktop_DBus_Tests_MyObject_async_throw_error (proxy, &error) != FALSE)
630     lose ("(wrapped) AsyncThrowError call unexpectedly succeeded!");
631
632   g_print ("(wrapped) AsyncThrowError failed (as expected) returned error: %s\n", error->message);
633   g_clear_error (&error);
634
635   g_print ("Calling (wrapped) uppercase\n");
636   if (!org_freedesktop_DBus_Tests_MyObject_uppercase (proxy, "foobar", &v_STRING_2, &error)) 
637     lose_gerror ("Failed to complete (wrapped) Uppercase call", error);
638   if (strcmp ("FOOBAR", v_STRING_2) != 0)
639     lose ("(wrapped) Uppercase call returned unexpected string %s", v_STRING_2);
640   g_free (v_STRING_2);
641
642   g_print ("Calling (wrapped) many_args\n");
643   if (!org_freedesktop_DBus_Tests_MyObject_many_args (proxy, 26, "bazwhee", G_PI,
644                                                       &v_DOUBLE_2, &v_STRING_2, &error))
645     lose_gerror ("Failed to complete (wrapped) ManyArgs call", error);
646
647   if (v_DOUBLE_2 < 55 || v_DOUBLE_2 > 56)
648     
649     lose ("(wrapped) ManyArgs call returned unexpected double value %f", v_DOUBLE_2);
650
651   if (strcmp ("BAZWHEE", v_STRING_2) != 0)
652     lose ("(wrapped) ManyArgs call returned unexpected string %s", v_STRING_2);
653   g_free (v_STRING_2);
654
655   {
656     guint32 arg0;
657     char *arg1;
658     gint32 arg2;
659     guint32 arg3;
660     guint32 arg4;
661     char *arg5;
662     
663     g_print ("Calling (wrapped) many_return\n");
664     if (!org_freedesktop_DBus_Tests_MyObject_many_return (proxy, &arg0, &arg1, &arg2, &arg3, &arg4, &arg5, &error))
665       lose_gerror ("Failed to complete (wrapped) ManyReturn call", error);
666
667     if (arg0 != 42)
668       lose ("(wrapped) ManyReturn call returned unexpected guint32 value %u", arg0);
669
670     if (strcmp ("42", arg1) != 0)
671       lose ("(wrapped) ManyReturn call returned unexpected string %s", arg1);
672     g_free (arg1);
673
674     if (arg2 != -67)
675       lose ("(wrapped) ManyReturn call returned unexpected gint32 value %u", arg2);
676
677     if (arg3 != 2)
678       lose ("(wrapped) ManyReturn call returned unexpected guint32 value %u", arg3);
679
680     if (arg4 != 26)
681       lose ("(wrapped) ManyReturn call returned unexpected guint32 value %u", arg4);
682
683     if (strcmp ("hello world", arg5))
684       lose ("(wrapped) ManyReturn call returned unexpected string %s", arg5);
685     g_free (arg5);
686   }
687
688   run_mainloop ();
689
690   {
691     GValue value = {0, };
692
693     g_value_init (&value, G_TYPE_STRING);
694     g_value_set_string (&value, "foo");
695
696     g_print ("Calling (wrapped) stringify, with string\n");
697     if (!org_freedesktop_DBus_Tests_MyObject_stringify (proxy,
698                                                         &value,
699                                                         &v_STRING_2,
700                                                         &error))
701       lose_gerror ("Failed to complete (wrapped) stringify call", error);
702     if (strcmp ("foo", v_STRING_2) != 0)
703       lose ("(wrapped) stringify call returned unexpected string %s", v_STRING_2);
704     g_free (v_STRING_2);
705
706     g_value_unset (&value);
707     g_value_init (&value, G_TYPE_INT);
708     g_value_set_int (&value, 42);
709
710     g_print ("Calling (wrapped) stringify, with int\n");
711     if (!org_freedesktop_DBus_Tests_MyObject_stringify (proxy,
712                                                         &value,
713                                                         &v_STRING_2,
714                                                         &error))
715       lose_gerror ("Failed to complete (wrapped) stringify call 2", error);
716     if (strcmp ("42", v_STRING_2) != 0)
717       lose ("(wrapped) stringify call 2 returned unexpected string %s", v_STRING_2);
718     g_value_unset (&value);
719     g_free (v_STRING_2);
720
721     g_value_init (&value, G_TYPE_INT);
722     g_value_set_int (&value, 88);
723     g_print ("Calling (wrapped) stringify, with another int\n");
724     if (!org_freedesktop_DBus_Tests_MyObject_stringify (proxy,
725                                                         &value,
726                                                         NULL,
727                                                         &error))
728       lose_gerror ("Failed to complete (wrapped) stringify call 3", error);
729     g_value_unset (&value);
730
731     g_print ("Calling (wrapped) unstringify, for string\n");
732     if (!org_freedesktop_DBus_Tests_MyObject_unstringify (proxy,
733                                                           "foo",
734                                                           &value,
735                                                           &error))
736       lose_gerror ("Failed to complete (wrapped) unstringify call", error);
737     if (!G_VALUE_HOLDS_STRING (&value))
738       lose ("(wrapped) unstringify call returned unexpected value type %d", (int) G_VALUE_TYPE (&value));
739     if (strcmp (g_value_get_string (&value), "foo"))
740       lose ("(wrapped) unstringify call returned unexpected string %s",
741             g_value_get_string (&value));
742         
743     g_value_unset (&value);
744
745     g_print ("Calling (wrapped) unstringify, for int\n");
746     if (!org_freedesktop_DBus_Tests_MyObject_unstringify (proxy,
747                                                           "10",
748                                                           &value,
749                                                           &error))
750       lose_gerror ("Failed to complete (wrapped) unstringify call", error);
751     if (!G_VALUE_HOLDS_INT (&value))
752       lose ("(wrapped) unstringify call returned unexpected value type %d", (int) G_VALUE_TYPE (&value));
753     if (g_value_get_int (&value) != 10)
754       lose ("(wrapped) unstringify call returned unexpected integer %d",
755             g_value_get_int (&value));
756
757     g_value_unset (&value);
758   }
759
760   run_mainloop ();
761
762   {
763     GArray *array;
764     guint32 val;
765     guint32 arraylen;
766
767     array = g_array_new (FALSE, TRUE, sizeof (guint32));
768     val = 42;
769     g_array_append_val (array, val);
770     val = 69;
771     g_array_append_val (array, val);
772     val = 88;
773     g_array_append_val (array, val);
774     val = 26;
775     g_array_append_val (array, val);
776     val = 2;
777     g_array_append_val (array, val);
778
779     arraylen = 0;
780     g_print ("Calling (wrapped) recursive1\n");
781     if (!org_freedesktop_DBus_Tests_MyObject_recursive1 (proxy, array,
782                                                          &arraylen, &error))
783       lose_gerror ("Failed to complete (wrapped) recursive1 call", error);
784     if (arraylen != 5)
785       lose ("(wrapped) recursive1 call returned invalid length %u", arraylen);
786   }
787
788   {
789     GArray *array = NULL;
790     guint32 *arrayvals;
791     
792     g_print ("Calling (wrapped) recursive2\n");
793     if (!org_freedesktop_DBus_Tests_MyObject_recursive2 (proxy, 2, &array, &error))
794       lose_gerror ("Failed to complete (wrapped) Recursive2 call", error);
795
796     if (array == NULL)
797       lose ("(wrapped) Recursive2 call returned NULL");
798     if (array->len != 5)
799       lose ("(wrapped) Recursive2 call returned unexpected array length %u", array->len);
800
801     arrayvals = (guint32*) array->data;
802     if (arrayvals[0] != 42)
803       lose ("(wrapped) Recursive2 call returned unexpected value %d in position 0", arrayvals[0]);
804     if (arrayvals[1] != 26)
805       lose ("(wrapped) Recursive2 call returned unexpected value %d in position 1", arrayvals[1]);
806     if (arrayvals[4] != 2)
807       lose ("(wrapped) Recursive2 call returned unexpected value %d in position 4", arrayvals[4]);
808
809     g_array_free (array, TRUE);
810   }
811
812   run_mainloop ();
813
814   {
815     char **strs;
816     char **strs_ret;
817
818     strs = g_new0 (char *, 4);
819     strs[0] = "hello";
820     strs[1] = "HellO";
821     strs[2] = "HELLO";
822     strs[3] = NULL;
823
824     strs_ret = NULL;
825     g_print ("Calling (wrapped) many_uppercase\n");
826     if (!org_freedesktop_DBus_Tests_MyObject_many_uppercase (proxy, strs, &strs_ret, &error)) 
827       lose_gerror ("Failed to complete (wrapped) ManyUppercase call", error);
828     g_assert (strs_ret != NULL);
829     if (strcmp ("HELLO", strs_ret[0]) != 0)
830       lose ("(wrapped) ManyUppercase call returned unexpected string %s", strs_ret[0]);
831     if (strcmp ("HELLO", strs_ret[1]) != 0)
832       lose ("(wrapped) ManyUppercase call returned unexpected string %s", strs_ret[1]);
833     if (strcmp ("HELLO", strs_ret[2]) != 0)
834       lose ("(wrapped) ManyUppercase call returned unexpected string %s", strs_ret[2]);
835
836     g_free (strs);
837     g_strfreev (strs_ret);
838   }
839
840   {
841     GHashTable *table;
842     guint len;
843
844     table = g_hash_table_new (g_str_hash, g_str_equal);
845     g_hash_table_insert (table, "moooo", "b");
846     g_hash_table_insert (table, "xxx", "cow!");
847
848     len = 0;
849     g_print ("Calling (wrapped) str_hash_len\n");
850     if (!org_freedesktop_DBus_Tests_MyObject_str_hash_len (proxy, table, &len, &error))
851       lose_gerror ("(wrapped) StrHashLen call failed", error);
852     if (len != 13) 
853       lose ("(wrapped) StrHashLen returned unexpected length %u", len);
854     g_hash_table_destroy (table);
855   }
856
857   {
858     GHashTable *table;
859     const char *val;
860
861     g_print ("Calling (wrapped) get_hash\n");
862     if (!org_freedesktop_DBus_Tests_MyObject_get_hash (proxy, &table, &error))
863       lose_gerror ("(wrapped) GetHash call failed", error);
864     val = g_hash_table_lookup (table, "foo");
865     if (val == NULL || strcmp ("bar", val))
866       lose ("(wrapped) StrHashLen returned invalid value %s for key \"foo\"",
867             val ? val : "(null)");
868     val = g_hash_table_lookup (table, "baz");
869     if (val == NULL || strcmp ("whee", val))
870       lose ("(wrapped) StrHashLen returned invalid value %s for key \"whee\"",
871             val ? val : "(null)");
872     val = g_hash_table_lookup (table, "cow");
873     if (val == NULL || strcmp ("crack", val))
874       lose ("(wrapped) StrHashLen returned invalid value %s for key \"cow\"",
875             val ? val : "(null)");
876     if (g_hash_table_size (table) != 3)
877       lose ("(wrapped) StrHashLen returned unexpected hash size %u",
878             g_hash_table_size (table));
879
880     g_hash_table_destroy (table);
881   }
882
883   run_mainloop ();
884
885   {
886     GValueArray *vals;
887     GValueArray *vals_ret;
888     GValue *val;
889
890     vals = g_value_array_new (3);
891
892     g_value_array_append (vals, NULL);
893     g_value_init (g_value_array_get_nth (vals, vals->n_values - 1), G_TYPE_STRING);
894     g_value_set_string (g_value_array_get_nth (vals, 0), "foo");
895
896     g_value_array_append (vals, NULL);
897     g_value_init (g_value_array_get_nth (vals, vals->n_values - 1), G_TYPE_UINT);
898     g_value_set_uint (g_value_array_get_nth (vals, vals->n_values - 1), 42);
899
900     g_value_array_append (vals, NULL);
901     g_value_init (g_value_array_get_nth (vals, vals->n_values - 1), G_TYPE_VALUE);
902     val = g_new0 (GValue, 1);
903     g_value_init (val, G_TYPE_UCHAR);
904     g_value_set_uchar (val, '!');
905     g_value_set_boxed (g_value_array_get_nth (vals, vals->n_values - 1), val);
906
907     vals_ret = NULL;
908     g_print ("Calling SendCar\n");
909     if (!dbus_g_proxy_call (proxy, "SendCar", &error,
910                             G_TYPE_VALUE_ARRAY, vals,
911                             G_TYPE_INVALID,
912                             G_TYPE_VALUE_ARRAY, &vals_ret,
913                             G_TYPE_INVALID))
914       lose_gerror ("Failed to complete SendCar call", error);
915
916     g_assert (vals_ret != NULL);
917     g_assert (vals_ret->n_values == 2);
918
919     g_assert (G_VALUE_HOLDS_UINT (g_value_array_get_nth (vals_ret, 0)));
920     g_assert (g_value_get_uint (g_value_array_get_nth (vals_ret, 0)) == 43);
921     
922     g_assert (G_VALUE_TYPE (g_value_array_get_nth (vals_ret, 1)) == DBUS_TYPE_G_OBJECT_PATH);
923     g_assert (!strcmp ("/org/freedesktop/DBus/Tests/MyTestObject2",
924                        g_value_get_boxed (g_value_array_get_nth (vals_ret, 1))));
925
926     g_value_array_free (vals);
927     g_value_array_free (vals_ret);
928   }
929
930   {
931     GValue *val;
932     GHashTable *table;
933     GHashTable *ret_table;
934
935     table = g_hash_table_new_full (g_str_hash, g_str_equal,
936                                    g_free, unset_and_free_gvalue);
937     
938     val = g_new0 (GValue, 1);
939     g_value_init (val, G_TYPE_UINT);
940     g_value_set_uint (val, 42);
941     g_hash_table_insert (table, g_strdup ("foo"), val);
942
943     val = g_new0 (GValue, 1);
944     g_value_init (val, G_TYPE_STRING);
945     g_value_set_string (val, "hello");
946     g_hash_table_insert (table, g_strdup ("bar"), val);
947
948     ret_table = NULL;
949     g_print ("Calling ManyStringify\n");
950     if (!dbus_g_proxy_call (proxy, "ManyStringify", &error,
951                             dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), table,
952                             G_TYPE_INVALID,
953                             dbus_g_type_get_map ("GHashTable", G_TYPE_STRING, G_TYPE_VALUE), &ret_table,
954                             G_TYPE_INVALID))
955       lose_gerror ("Failed to complete ManyStringify call", error);
956
957     g_assert (ret_table != NULL);
958     g_assert (g_hash_table_size (ret_table) == 2);
959
960     val = g_hash_table_lookup (ret_table, "foo");
961     g_assert (val != NULL);
962     g_assert (G_VALUE_HOLDS_STRING (val));
963     g_assert (!strcmp ("42", g_value_get_string (val)));
964
965     val = g_hash_table_lookup (ret_table, "bar");
966     g_assert (val != NULL);
967     g_assert (G_VALUE_HOLDS_STRING (val));
968     g_assert (!strcmp ("hello", g_value_get_string (val)));
969
970     g_hash_table_destroy (table);
971     g_hash_table_destroy (ret_table);
972   }
973
974   {
975     GPtrArray *in_array;
976     GPtrArray *out_array;
977     char **strs;
978     GArray *uints;
979
980     in_array = g_ptr_array_new ();
981
982     strs = g_new0 (char *, 3);
983     strs[0] = "foo";
984     strs[1] = "bar";
985     strs[2] = NULL;
986     g_ptr_array_add (in_array, strs);
987
988     strs = g_new0 (char *, 4);
989     strs[0] = "baz";
990     strs[1] = "whee";
991     strs[2] = "moo";
992     strs[3] = NULL;
993     g_ptr_array_add (in_array, strs);
994
995     out_array = NULL;
996     g_print ("Calling RecArrays\n");
997     if (!dbus_g_proxy_call (proxy, "RecArrays", &error,
998                             dbus_g_type_get_collection ("GPtrArray", G_TYPE_STRV), in_array,
999                             G_TYPE_INVALID,
1000                             dbus_g_type_get_collection ("GPtrArray",
1001                                                         dbus_g_type_get_collection ("GPtrArray",
1002                                                                                     G_TYPE_UINT)), &out_array, 
1003                             G_TYPE_INVALID))
1004       lose_gerror ("Failed to complete RecArrays call", error);
1005     g_free (g_ptr_array_index (in_array, 0));
1006     g_free (g_ptr_array_index (in_array, 1));
1007
1008     g_assert (out_array);
1009     g_assert (out_array->len == 2);
1010     uints = g_ptr_array_index (out_array, 0);
1011     g_assert (uints);
1012     g_assert (uints->len == 3);
1013     g_assert (g_array_index (uints, guint, 0) == 10);
1014     g_assert (g_array_index (uints, guint, 1) == 42);
1015     g_assert (g_array_index (uints, guint, 2) == 27);
1016     g_array_free (uints, TRUE);
1017     uints = g_ptr_array_index (out_array, 1);
1018     g_assert (uints);
1019     g_assert (uints->len == 1);
1020     g_assert (g_array_index (uints, guint, 0) == 30);
1021     g_array_free (uints, TRUE);
1022     g_ptr_array_free (out_array, TRUE);
1023   }
1024
1025   {
1026     guint val;
1027     char *ret_path;
1028     DBusGProxy *ret_proxy;
1029
1030     g_print ("Calling (wrapped) objpath\n");
1031     if (!dbus_g_proxy_call (proxy, "Objpath", &error,
1032                             DBUS_TYPE_G_PROXY, proxy, G_TYPE_INVALID,
1033                             DBUS_TYPE_G_PROXY, &ret_proxy, G_TYPE_INVALID))
1034       lose_gerror ("Failed to complete Objpath call", error);
1035     if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject2",
1036                 dbus_g_proxy_get_path (ret_proxy)) != 0)
1037       lose ("(wrapped) objpath call returned unexpected proxy %s",
1038             dbus_g_proxy_get_path (ret_proxy));
1039
1040     g_print ("Doing get/increment val tests\n");
1041     val = 1;
1042     if (!org_freedesktop_DBus_Tests_MyObject_get_val (ret_proxy, &val, &error))
1043       lose_gerror ("Failed to complete (wrapped) GetVal call", error);
1044     if (val != 0)
1045       lose ("(wrapped) GetVal returned invalid value %d", val);
1046
1047     if (!org_freedesktop_DBus_Tests_MyObject_increment_val (ret_proxy, &error))
1048       lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
1049
1050     if (!org_freedesktop_DBus_Tests_MyObject_increment_val (ret_proxy, &error))
1051       lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
1052
1053     if (!org_freedesktop_DBus_Tests_MyObject_increment_val (ret_proxy, &error))
1054       lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
1055
1056     if (!org_freedesktop_DBus_Tests_MyObject_get_val (ret_proxy, &val, &error))
1057       lose_gerror ("Failed to complete (wrapped) GetVal call", error);
1058     if (val != 3)
1059       lose ("(wrapped) GetVal returned invalid value %d", val);
1060
1061     if (!org_freedesktop_DBus_Tests_MyObject_get_val (proxy, &val, &error))
1062       lose_gerror ("Failed to complete (wrapped) GetVal call", error);
1063     if (val != 0)
1064       lose ("(wrapped) GetVal returned invalid value %d", val);
1065
1066     if (!org_freedesktop_DBus_Tests_MyObject_increment_val (proxy, &error))
1067       lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
1068
1069     if (!org_freedesktop_DBus_Tests_MyObject_get_val (proxy, &val, &error))
1070       lose_gerror ("Failed to complete (wrapped) GetVal call", error);
1071     if (val != 1)
1072       lose ("(wrapped) GetVal returned invalid value %d", val);
1073
1074     if (!org_freedesktop_DBus_Tests_MyObject_get_val (ret_proxy, &val, &error))
1075       lose_gerror ("Failed to complete (wrapped) GetVal call", error);
1076     if (val != 3)
1077       lose ("(wrapped) GetVal returned invalid value %d", val);
1078
1079     g_object_unref (G_OBJECT (ret_proxy));
1080
1081     g_print ("Calling objpath again\n");
1082     ret_proxy = NULL;
1083
1084     if (!dbus_g_proxy_call (proxy, "Objpath", &error,
1085                             DBUS_TYPE_G_OBJECT_PATH,
1086                             dbus_g_proxy_get_path (proxy),
1087                             G_TYPE_INVALID,
1088                             DBUS_TYPE_G_OBJECT_PATH,
1089                             &ret_path,
1090                             G_TYPE_INVALID))
1091       lose_gerror ("Failed to complete Objpath call 2", error);
1092     if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject2", ret_path) != 0)
1093       lose ("Objpath call 2 returned unexpected path %s",
1094             ret_path);
1095
1096     ret_proxy = dbus_g_proxy_new_for_name_owner (connection,
1097                                                  "org.freedesktop.DBus.TestSuiteGLibService",
1098                                                  ret_path,
1099                                                  "org.freedesktop.DBus.Tests.FooObject",
1100                                                  &error);
1101     g_free (ret_path);
1102     
1103     val = 0;
1104     if (!org_freedesktop_DBus_Tests_FooObject_get_value (ret_proxy, &val, &error))
1105       lose_gerror ("Failed to complete (wrapped) GetValue call", error);
1106     if (val != 3)
1107       lose ("(wrapped) GetValue returned invalid value %d", val);
1108   }
1109
1110   run_mainloop ();
1111
1112   {
1113     GPtrArray *objs;
1114     guint i;
1115
1116     g_print ("Calling GetObjs\n");
1117
1118     if (!dbus_g_proxy_call (proxy, "GetObjs", &error, G_TYPE_INVALID,
1119                             dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH),
1120                             &objs,
1121                             G_TYPE_INVALID))
1122       lose_gerror ("Failed to complete GetObjs call", error);
1123     if (objs->len != 2)
1124       lose ("GetObjs call returned unexpected number of objects %d, expected 2",
1125             objs->len);
1126
1127     if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject",
1128                 g_ptr_array_index (objs, 0)) != 0)
1129       lose ("GetObjs call returned unexpected path \"%s\" in position 0; expected /org/freedesktop/DBus/Tests/MyTestObject", (char*) g_ptr_array_index (objs, 0));
1130
1131     if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject2",
1132                 g_ptr_array_index (objs, 1)) != 0)
1133       lose ("GetObjs call returned unexpected path \"%s\" in position 1; expected /org/freedesktop/DBus/Tests/MyTestObject2", (char*) g_ptr_array_index (objs, 1));
1134
1135     for (i = 0; i < objs->len; i++)
1136       g_free (g_ptr_array_index (objs, i));
1137     g_ptr_array_free (objs, TRUE);
1138   }
1139   
1140   {
1141     GValue *variant;
1142     GArray *array;
1143     gint i;
1144
1145     g_print ("Calling ProcessVariantOfArrayOfInts123\n");
1146
1147     array = g_array_sized_new (FALSE, FALSE, sizeof(gint), 3);
1148     i = 1;
1149     g_array_append_val (array, i);
1150     i++;
1151     g_array_append_val (array, i);
1152     i++;
1153     g_array_append_val (array, i);
1154
1155     variant = g_new0 (GValue, 1);
1156     g_value_init (variant, dbus_g_type_get_collection ("GArray", G_TYPE_INT));
1157     g_value_set_boxed_take_ownership (variant, array);
1158
1159     if (!dbus_g_proxy_call (proxy, "ProcessVariantOfArrayOfInts123", &error,
1160                             G_TYPE_VALUE, variant,
1161                             G_TYPE_INVALID,
1162                             G_TYPE_INVALID))
1163       lose_gerror ("Failed to send a vairant of array of ints 1, 2 and 3!", error);
1164
1165     g_value_unset (variant);
1166   }
1167
1168   for (i=0; i<3; i++)
1169   {
1170     gchar *val;
1171     GHashTable *table;
1172     GHashTable *subtable;
1173     GHashTable *ret_table;
1174
1175     table = g_hash_table_new_full (g_str_hash, g_str_equal,
1176                                    (GDestroyNotify) (g_free),
1177                                    (GDestroyNotify) (g_hash_table_destroy));
1178
1179     subtable = g_hash_table_new_full (g_str_hash, g_str_equal,
1180                                       (GDestroyNotify) (g_free),
1181                                       (GDestroyNotify) (g_free));
1182     g_hash_table_insert (subtable, g_strdup ("foo"), g_strdup("1"));
1183     g_hash_table_insert (subtable, g_strdup ("bar"), g_strdup("2"));
1184     g_hash_table_insert (subtable, g_strdup ("baz"), g_strdup("3"));
1185
1186     g_hash_table_insert (table, g_strdup("dict1"), subtable);
1187
1188     subtable = g_hash_table_new_full (g_str_hash, g_str_equal,
1189                                       (GDestroyNotify) (g_free),
1190                                       (GDestroyNotify) (g_free));
1191     g_hash_table_insert (subtable, g_strdup ("foo"), g_strdup("4"));
1192     g_hash_table_insert (subtable, g_strdup ("bar"), g_strdup("5"));
1193     g_hash_table_insert (subtable, g_strdup ("baz"), g_strdup("6"));
1194
1195     g_hash_table_insert (table, g_strdup("dict2"), subtable);
1196
1197     subtable = NULL;
1198
1199     ret_table = NULL;
1200
1201     g_print ("Calling DictOfDicts\n");
1202     if (!dbus_g_proxy_call (proxy, "DictOfDicts", &error,
1203                             dbus_g_type_get_map ("GHashTable", G_TYPE_STRING,
1204                               dbus_g_type_get_map ("GHashTable", G_TYPE_STRING,
1205                                 G_TYPE_STRING)), table,
1206                             G_TYPE_INVALID,
1207                             dbus_g_type_get_map ("GHashTable", G_TYPE_STRING,
1208                               dbus_g_type_get_map ("GHashTable", G_TYPE_STRING,
1209                                 G_TYPE_STRING)), &ret_table,
1210                             G_TYPE_INVALID))
1211       lose_gerror ("Failed to complete DictOfDicts call", error);
1212
1213     g_assert (ret_table != NULL);
1214     g_assert (g_hash_table_size (ret_table) == 2);
1215
1216     subtable = g_hash_table_lookup (ret_table, "dict1");
1217     g_assert(subtable);
1218     g_assert (g_hash_table_size (subtable) == 3);
1219
1220     val = g_hash_table_lookup (subtable, "foo");
1221     g_assert (val != NULL);
1222     g_assert (!strcmp ("dict1 1", val));
1223
1224     val = g_hash_table_lookup (subtable, "bar");
1225     g_assert (val != NULL);
1226     g_assert (!strcmp ("dict1 2", val));
1227
1228     val = g_hash_table_lookup (subtable, "baz");
1229     g_assert (val != NULL);
1230     g_assert (!strcmp ("dict1 3", val));
1231
1232     subtable = g_hash_table_lookup (ret_table, "dict2");
1233     g_assert(subtable);
1234     g_assert (g_hash_table_size (subtable) == 3);
1235
1236     val = g_hash_table_lookup (subtable, "foo");
1237     g_assert (val != NULL);
1238     g_assert (!strcmp ("dict2 4", val));
1239
1240     val = g_hash_table_lookup (subtable, "bar");
1241     g_assert (val != NULL);
1242     g_assert (!strcmp ("dict2 5", val));
1243
1244     val = g_hash_table_lookup (subtable, "baz");
1245     g_assert (val != NULL);
1246     g_assert (!strcmp ("dict2 6", val));
1247
1248     g_hash_table_destroy (table);
1249     g_hash_table_destroy (ret_table);
1250
1251     g_mem_profile ();
1252   }
1253
1254
1255
1256   /* Signal handling tests */
1257   
1258   g_print ("Testing signal handling\n");
1259   dbus_g_proxy_add_signal (proxy, "Frobnicate", G_TYPE_INT, G_TYPE_INVALID);
1260   
1261   dbus_g_proxy_connect_signal (proxy, "Frobnicate",
1262                                G_CALLBACK (frobnicate_signal_handler),
1263                                NULL, NULL);
1264   
1265   g_print ("Calling EmitFrobnicate\n");
1266   if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
1267                           G_TYPE_INVALID, G_TYPE_INVALID))
1268     lose_gerror ("Failed to complete EmitFrobnicate call", error);
1269
1270   
1271   dbus_g_connection_flush (connection);
1272   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1273   g_main_loop_run (loop);
1274
1275   if (n_times_frobnicate_received != 1)
1276     lose ("Frobnicate signal received %d times, should have been 1", n_times_frobnicate_received);
1277
1278   g_print ("Calling EmitFrobnicate again\n");
1279   if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
1280                           G_TYPE_INVALID, G_TYPE_INVALID))
1281     lose_gerror ("Failed to complete EmitFrobnicate call", error);
1282   
1283   dbus_g_connection_flush (connection);
1284   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1285   g_main_loop_run (loop);
1286
1287   if (n_times_frobnicate_received != 2)
1288     lose ("Frobnicate signal received %d times, should have been 2", n_times_frobnicate_received);
1289
1290   g_object_unref (G_OBJECT (proxy));
1291
1292   run_mainloop ();
1293
1294   g_print ("Creating proxy for FooObject interface\n");
1295   proxy = dbus_g_proxy_new_for_name_owner (connection,
1296                                            "org.freedesktop.DBus.TestSuiteGLibService",
1297                                            "/org/freedesktop/DBus/Tests/MyTestObject",
1298                                            "org.freedesktop.DBus.Tests.FooObject",
1299                                            &error);
1300   
1301   if (proxy == NULL)
1302     lose_gerror ("Failed to create proxy for name owner", error);
1303
1304   dbus_g_object_register_marshaller (my_object_marshal_VOID__STRING_INT_STRING, 
1305                                      G_TYPE_NONE, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INVALID);
1306
1307   dbus_g_object_register_marshaller (my_object_marshal_VOID__STRING_BOXED, 
1308                                      G_TYPE_NONE, G_TYPE_STRING, G_TYPE_VALUE, G_TYPE_INVALID);
1309
1310   dbus_g_proxy_add_signal (proxy, "Sig0", G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INVALID);
1311   dbus_g_proxy_add_signal (proxy, "Sig1", G_TYPE_STRING, G_TYPE_VALUE, G_TYPE_INVALID);
1312   dbus_g_proxy_add_signal (proxy, "Sig2", DBUS_TYPE_G_STRING_STRING_HASHTABLE, G_TYPE_INVALID);
1313   
1314   dbus_g_proxy_connect_signal (proxy, "Sig0",
1315                                G_CALLBACK (sig0_signal_handler),
1316                                NULL, NULL);
1317   dbus_g_proxy_connect_signal (proxy, "Sig1",
1318                                G_CALLBACK (sig1_signal_handler),
1319                                NULL, NULL);
1320   dbus_g_proxy_connect_signal (proxy, "Sig2",
1321                                G_CALLBACK (sig2_signal_handler),
1322                                NULL, NULL);
1323
1324   g_print ("Calling FooObject EmitSignals\n");
1325   dbus_g_proxy_call_no_reply (proxy, "EmitSignals", G_TYPE_INVALID);
1326
1327   dbus_g_connection_flush (connection);
1328   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1329   g_main_loop_run (loop);
1330   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1331   g_main_loop_run (loop);
1332
1333   if (n_times_sig0_received != 1)
1334     lose ("Sig0 signal received %d times, should have been 1", n_times_sig0_received);
1335   if (n_times_sig1_received != 1)
1336     lose ("Sig1 signal received %d times, should have been 1", n_times_sig1_received);
1337
1338   g_print ("Calling FooObject EmitSignals and EmitSignal2\n");
1339   dbus_g_proxy_call_no_reply (proxy, "EmitSignal2", G_TYPE_INVALID);
1340   dbus_g_connection_flush (connection);
1341
1342   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1343   g_main_loop_run (loop);
1344
1345   if (n_times_sig2_received != 1)
1346     lose ("Sig2 signal received %d times, should have been 1", n_times_sig2_received);
1347
1348   g_print ("Calling FooObject EmitSignals two more times\n");
1349   dbus_g_proxy_call_no_reply (proxy, "EmitSignals", G_TYPE_INVALID);
1350   dbus_g_proxy_call_no_reply (proxy, "EmitSignals", G_TYPE_INVALID);
1351
1352   dbus_g_connection_flush (connection);
1353   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1354   g_main_loop_run (loop);
1355   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1356   g_main_loop_run (loop);
1357   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1358   g_main_loop_run (loop);
1359   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1360   g_main_loop_run (loop);
1361
1362   if (n_times_sig0_received != 3)
1363     lose ("Sig0 signal received %d times, should have been 3", n_times_sig0_received);
1364   if (n_times_sig1_received != 3)
1365     lose ("Sig1 signal received %d times, should have been 3", n_times_sig1_received);
1366
1367   /* Terminate again */
1368   g_print ("Terminating service\n");
1369   await_terminating_service = "org.freedesktop.DBus.TestSuiteGLibService";
1370   dbus_g_proxy_call_no_reply (proxy, "Terminate", G_TYPE_INVALID);
1371
1372   proxy_destroyed = FALSE;
1373   proxy_destroy_and_nameowner = TRUE;
1374   proxy_destroy_and_nameowner_complete = FALSE;
1375
1376   g_signal_connect (G_OBJECT (proxy),
1377                     "destroy",
1378                     G_CALLBACK (proxy_destroyed_cb),
1379                     NULL);
1380
1381   dbus_g_connection_flush (connection);
1382   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1383   g_main_loop_run (loop);
1384
1385   if (await_terminating_service != NULL)
1386     lose ("Didn't see name loss for \"org.freedesktop.DBus.TestSuiteGLibService\"");
1387   if (!proxy_destroyed)
1388     lose ("Didn't get proxy_destroyed");
1389   g_print ("Proxy destroyed successfully\n");
1390
1391   /* Don't need to unref, proxy was destroyed */
1392
1393   run_mainloop ();
1394
1395   /* Create a new proxy for the name; should not be associated */
1396   proxy = dbus_g_proxy_new_for_name (connection,
1397                                      "org.freedesktop.DBus.TestSuiteGLibService",
1398                                      "/org/freedesktop/DBus/Tests/MyTestObject",
1399                                      "org.freedesktop.DBus.Tests.MyObject");
1400   g_assert (proxy != NULL);
1401
1402   proxy_destroyed = FALSE;
1403   proxy_destroy_and_nameowner = FALSE;
1404   proxy_destroy_and_nameowner_complete = FALSE;
1405
1406   g_signal_connect (G_OBJECT (proxy),
1407                     "destroy",
1408                     G_CALLBACK (proxy_destroyed_cb),
1409                     NULL);
1410   
1411   if (!dbus_g_proxy_call (driver, "GetNameOwner", &error,
1412                           G_TYPE_STRING,
1413                           "org.freedesktop.DBus.TestSuiteGLibService",
1414                           G_TYPE_INVALID,
1415                           G_TYPE_STRING,
1416                           &v_STRING_2,
1417                           G_TYPE_INVALID)) {
1418     if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_NAME_HAS_NO_OWNER)
1419       g_print ("Got expected error \"org.freedesktop.DBus.Error.NameHasNoOwner\"\n");
1420     else
1421       lose_gerror ("Unexpected error from GetNameOwner", error);
1422   } else
1423     lose ("GetNameOwner unexpectedly succeeded!");
1424   g_clear_error (&error);
1425
1426   /* This will have the side-effect of activating the service, thus
1427    * causing a NameOwnerChanged, which should let our name proxy
1428    * get signals
1429    */
1430   g_print ("Calling Uppercase for name proxy\n");
1431   if (!dbus_g_proxy_call (proxy, "Uppercase", &error,
1432                           G_TYPE_STRING, "bazwhee",
1433                           G_TYPE_INVALID,
1434                           G_TYPE_STRING, &v_STRING_2,
1435                           G_TYPE_INVALID))
1436     lose_gerror ("Failed to complete Uppercase call", error);
1437   g_free (v_STRING_2);
1438
1439   if (getenv ("DBUS_GLIB_TEST_SLEEP_AFTER_ACTIVATION1"))
1440     g_usleep (8 * G_USEC_PER_SEC);
1441
1442   dbus_g_proxy_add_signal (proxy, "Frobnicate", G_TYPE_INT, G_TYPE_INVALID);
1443   
1444   dbus_g_proxy_connect_signal (proxy, "Frobnicate",
1445                                G_CALLBACK (frobnicate_signal_handler),
1446                                NULL, NULL);
1447   
1448   g_print ("Calling EmitFrobnicate\n");
1449   if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
1450                           G_TYPE_INVALID, G_TYPE_INVALID))
1451     lose_gerror ("Failed to complete EmitFrobnicate call", error);
1452
1453   n_times_frobnicate_received = 0;
1454
1455   dbus_g_connection_flush (connection);
1456   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1457   g_main_loop_run (loop);
1458
1459   if (n_times_frobnicate_received != 1)
1460     lose ("Frobnicate signal received %d times, should have been 1", n_times_frobnicate_received);
1461
1462   /* Now terminate the service, then start it again (implicitly) and wait for signals */
1463   g_print ("Terminating service (2)\n");
1464   await_terminating_service = "org.freedesktop.DBus.TestSuiteGLibService";
1465   dbus_g_proxy_call_no_reply (proxy, "Terminate", G_TYPE_INVALID);
1466   dbus_g_connection_flush (connection);
1467   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1468   g_main_loop_run (loop);
1469   if (await_terminating_service != NULL)
1470     lose ("Didn't see name loss for \"org.freedesktop.DBus.TestSuiteGLibService\"");
1471
1472   if (proxy_destroyed)
1473     lose ("Unexpectedly got proxy_destroyed!");
1474
1475   n_times_frobnicate_received = 0;
1476
1477   g_print ("Calling EmitFrobnicate (2)\n");
1478   if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
1479                           G_TYPE_INVALID, G_TYPE_INVALID))
1480     lose_gerror ("Failed to complete EmitFrobnicate call", error);
1481
1482   if (getenv ("DBUS_GLIB_TEST_SLEEP_AFTER_ACTIVATION2"))
1483     g_usleep (8 * G_USEC_PER_SEC);
1484
1485   dbus_g_connection_flush (connection);
1486   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1487   g_main_loop_run (loop);
1488
1489   if (n_times_frobnicate_received != 1)
1490     lose ("Frobnicate signal received %d times, should have been 1", n_times_frobnicate_received);
1491
1492   if (proxy_destroyed)
1493     lose ("Unexpectedly got proxy_destroyed!");
1494   
1495   /* Create another proxy for the name; should be associated immediately */
1496   proxy2 = dbus_g_proxy_new_for_name (connection,
1497                                      "org.freedesktop.DBus.TestSuiteGLibService",
1498                                      "/org/freedesktop/DBus/Tests/MyTestObject",
1499                                      "org.freedesktop.DBus.Tests.MyObject");
1500   g_assert (proxy2 != NULL);
1501
1502   dbus_g_proxy_add_signal (proxy2, "Frobnicate", G_TYPE_INT, G_TYPE_INVALID);
1503   
1504   dbus_g_proxy_connect_signal (proxy2, "Frobnicate",
1505                                G_CALLBACK (frobnicate_signal_handler_2),
1506                                NULL, NULL);
1507
1508   g_print ("Calling EmitFrobnicate (3)\n");
1509   if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
1510                           G_TYPE_INVALID, G_TYPE_INVALID))
1511     lose_gerror ("Failed to complete EmitFrobnicate call", error);
1512
1513   dbus_g_connection_flush (connection);
1514   exit_timeout = g_timeout_add (5000, timed_exit, loop);
1515   g_main_loop_run (loop);
1516
1517   if (n_times_frobnicate_received != 2)
1518     lose ("Frobnicate signal received %d times for 1st proxy, should have been 2", n_times_frobnicate_received);
1519   if (n_times_frobnicate_received_2 != 1)
1520     lose ("Frobnicate signal received %d times for 2nd proxy, should have been 1", n_times_frobnicate_received_2);
1521
1522   g_object_unref (G_OBJECT (proxy));
1523   g_object_unref (G_OBJECT (proxy2));
1524
1525   run_mainloop ();
1526
1527   /* Test introspection */
1528   proxy = dbus_g_proxy_new_for_name_owner (connection,
1529                                            "org.freedesktop.DBus.TestSuiteGLibService",
1530                                            "/org/freedesktop/DBus/Tests/MyTestObject",
1531                                            "org.freedesktop.DBus.Introspectable",
1532                                            &error);
1533   if (proxy == NULL)
1534     lose_gerror ("Failed to create proxy for name owner", error);
1535
1536   g_print ("Testing introspect\n");
1537   if (!dbus_g_proxy_call (proxy, "Introspect", &error,
1538                           G_TYPE_INVALID,
1539                           G_TYPE_STRING, &v_STRING_2,
1540                           G_TYPE_INVALID))
1541     lose_gerror ("Failed to complete Introspect call", error);
1542
1543   /* Could just do strcmp(), but that seems more fragile */
1544   {
1545     NodeInfo *node;
1546     GSList *elt;
1547     gboolean found_introspectable;
1548     gboolean found_properties;
1549     gboolean found_myobject;
1550     gboolean found_fooobject;
1551
1552     node = description_load_from_string (v_STRING_2, strlen (v_STRING_2), &error);
1553     if (!node)
1554       lose_gerror ("Failed to parse introspection data: %s", error);
1555
1556     found_introspectable = FALSE;
1557     found_properties = FALSE;
1558     found_myobject = FALSE;
1559     found_fooobject = FALSE;
1560     for (elt = node_info_get_interfaces (node); elt ; elt = elt->next)
1561       {
1562         InterfaceInfo *iface = elt->data;
1563
1564         if (!found_introspectable && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Introspectable") == 0)
1565           found_introspectable = TRUE;
1566         else if (!found_properties && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Properties") == 0)
1567           found_properties = TRUE;
1568         else if (!found_myobject && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Tests.MyObject") == 0)
1569           {
1570             GSList *elt;
1571             gboolean found_manyargs;
1572             
1573             found_myobject = TRUE;
1574             
1575             found_manyargs = FALSE;
1576             for (elt = interface_info_get_methods (iface); elt; elt = elt->next)
1577               {
1578                 MethodInfo *method;
1579
1580                 method = elt->data;
1581                 if (strcmp (method_info_get_name (method), "ManyArgs") == 0)
1582                   {
1583                     found_manyargs = TRUE;
1584                     break;
1585                   }
1586               }
1587             if (!found_manyargs)
1588               lose ("Missing method org.freedesktop.DBus.Tests.MyObject.ManyArgs");
1589           }
1590         else if (!found_fooobject && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Tests.FooObject") == 0)
1591           found_fooobject = TRUE;
1592         else
1593           lose ("Unexpected or duplicate interface %s", interface_info_get_name (iface));
1594       }
1595
1596     if (!(found_introspectable && found_myobject && found_properties))
1597       lose ("Missing interface"); 
1598   }
1599   g_free (v_STRING_2);
1600   
1601   g_object_unref (G_OBJECT (driver));
1602
1603   g_print ("Successfully completed %s\n", argv[0]);
1604   
1605   return 0;
1606 }