2005-06-27 Colin Walters <walters@verbum.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-object.h>
10 #include "my-object-marshal.h"
11
12 static GMainLoop *loop = NULL;
13 static int n_times_foo_received = 0;
14 static int n_times_frobnicate_received = 0;
15 static int n_times_sig0_received = 0;
16 static int n_times_sig1_received = 0;
17 static int n_times_sig2_received = 0;
18 static guint exit_timeout = 0;
19
20 static gboolean
21 timed_exit (gpointer loop)
22 {
23   g_main_loop_quit (loop);
24   return TRUE;
25 }
26
27 static void
28 foo_signal_handler (DBusGProxy  *proxy,
29                     double       d,
30                     void        *user_data)
31 {
32   n_times_foo_received += 1;
33
34   g_main_loop_quit (loop);
35   g_source_remove (exit_timeout);
36 }
37
38 static void
39 frobnicate_signal_handler (DBusGProxy  *proxy,
40                            int          val,
41                            void        *user_data)
42 {
43   n_times_frobnicate_received += 1;
44
45   g_assert (val == 42);
46
47   g_main_loop_quit (loop);
48   g_source_remove (exit_timeout);
49 }
50
51 static void
52 sig0_signal_handler (DBusGProxy  *proxy,
53                      const char  *str0,
54                      int          val,
55                      const char  *str1,
56                      void        *user_data)
57 {
58   n_times_sig0_received += 1;
59
60   g_assert (!strcmp (str0, "foo"));
61
62   g_assert (val == 22);
63
64   g_assert (!strcmp (str1, "moo"));
65
66   g_main_loop_quit (loop);
67   g_source_remove (exit_timeout);
68 }
69
70 static void
71 sig1_signal_handler (DBusGProxy  *proxy,
72                      const char  *str0,
73                      GValue      *value,
74                      void        *user_data)
75 {
76   n_times_sig1_received += 1;
77
78   g_assert (!strcmp (str0, "baz"));
79
80   g_assert (G_VALUE_HOLDS_STRING (value));
81
82   g_assert (!strcmp (g_value_get_string (value), "bar"));
83
84   g_main_loop_quit (loop);
85   g_source_remove (exit_timeout);
86 }
87
88 static void
89 sig2_signal_handler (DBusGProxy  *proxy,
90                      GHashTable  *table,
91                      void        *user_data)
92 {
93   n_times_sig2_received += 1;
94
95   g_assert (g_hash_table_size (table) == 2);
96
97   g_assert (g_hash_table_lookup (table, "baz") != NULL);
98   g_assert (!strcmp (g_hash_table_lookup (table, "baz"), "cow"));
99   g_assert (g_hash_table_lookup (table, "bar") != NULL);
100   g_assert (!strcmp (g_hash_table_lookup (table, "bar"), "foo"));
101
102   g_main_loop_quit (loop);
103   g_source_remove (exit_timeout);
104 }
105
106 static void lose (const char *fmt, ...) G_GNUC_NORETURN G_GNUC_PRINTF (1, 2);
107 static void lose_gerror (const char *prefix, GError *error) G_GNUC_NORETURN;
108
109 static void
110 lose (const char *str, ...)
111 {
112   va_list args;
113
114   va_start (args, str);
115
116   vfprintf (stderr, str, args);
117   fputc ('\n', stderr);
118
119   va_end (args);
120
121   exit (1);
122 }
123
124 static void
125 lose_gerror (const char *prefix, GError *error) 
126 {
127   lose ("%s: %s", prefix, error->message);
128 }
129
130 int
131 main (int argc, char **argv)
132 {
133   DBusGConnection *connection;
134   GError *error;
135   DBusGProxy *driver;
136   DBusGProxy *proxy;
137   DBusGPendingCall *call;
138   char **name_list;
139   guint name_list_len;
140   guint i;
141   guint32 result;
142   const char *v_STRING;
143   char *v_STRING_2;
144   guint32 v_UINT32;
145   guint32 v_UINT32_2;
146   double v_DOUBLE;
147   double v_DOUBLE_2;
148     
149   g_type_init ();
150
151   g_log_set_always_fatal (G_LOG_LEVEL_WARNING);
152   
153   loop = g_main_loop_new (NULL, FALSE);
154
155   error = NULL;
156   connection = dbus_g_bus_get (DBUS_BUS_SESSION,
157                                &error);
158   if (connection == NULL)
159     lose_gerror ("Failed to open connection to bus", error);
160
161   /* should always get the same one */
162   g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
163   g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
164   g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
165   
166   /* Create a proxy object for the "bus driver" */
167   
168   driver = dbus_g_proxy_new_for_name (connection,
169                                       DBUS_SERVICE_DBUS,
170                                       DBUS_PATH_DBUS,
171                                       DBUS_INTERFACE_DBUS);
172
173   /* Call ListNames method */
174   
175   call = dbus_g_proxy_begin_call (driver, "ListNames", G_TYPE_INVALID);
176
177   error = NULL;
178   if (!dbus_g_proxy_end_call (driver, call, &error,
179                               G_TYPE_STRV, &name_list,
180                               G_TYPE_INVALID))
181     lose_gerror ("Failed to complete ListNames call", error);
182
183   g_print ("Names on the message bus:\n");
184   i = 0;
185   name_list_len = g_strv_length (name_list);
186   while (i < name_list_len)
187     {
188       g_assert (name_list[i] != NULL);
189       g_print ("  %s\n", name_list[i]);
190       ++i;
191     }
192   g_assert (name_list[i] == NULL);
193
194   g_strfreev (name_list);
195
196   /* Test handling of unknown method */
197   call = dbus_g_proxy_begin_call (driver, "ThisMethodDoesNotExist",
198                                   G_TYPE_STRING,
199                                   "blah blah blah blah blah",
200                                   G_TYPE_INT,
201                                   10,
202                                   G_TYPE_INVALID);
203
204   error = NULL;
205   if (dbus_g_proxy_end_call (driver, call, &error,
206                              G_TYPE_INVALID))
207     lose ("Calling nonexistent method succeeded!");
208
209   g_print ("Got EXPECTED error from calling unknown method: %s\n", error->message);
210   g_error_free (error);
211   
212   /* Activate a service */
213   call = dbus_g_proxy_begin_call (driver, "StartServiceByName",
214                                   G_TYPE_STRING,
215                                   "org.freedesktop.DBus.TestSuiteEchoService",
216                                   G_TYPE_UINT,
217                                   0,
218                                   G_TYPE_INVALID);
219
220   error = NULL;
221   if (!dbus_g_proxy_end_call (driver, call, &error,
222                               G_TYPE_UINT, &result,
223                               G_TYPE_INVALID))
224     lose_gerror ("Failed to complete Activate call", error);
225
226   g_print ("Starting echo service result = 0x%x\n", result);
227
228   /* Activate a service again */
229   call = dbus_g_proxy_begin_call (driver, "StartServiceByName",
230                                   G_TYPE_STRING,
231                                   "org.freedesktop.DBus.TestSuiteEchoService",
232                                   G_TYPE_UINT,
233                                   0,
234                                   DBUS_TYPE_INVALID);
235
236   error = NULL;
237   if (!dbus_g_proxy_end_call (driver, call, &error,
238                               G_TYPE_UINT, &result,
239                               G_TYPE_INVALID))
240     lose_gerror ("Failed to complete Activate call", error);
241
242   g_print ("Duplicate start of echo service = 0x%x\n", result);
243
244   /* Talk to the new service */
245   
246   proxy = dbus_g_proxy_new_for_name_owner (connection,
247                                            "org.freedesktop.DBus.TestSuiteEchoService",
248                                            "/org/freedesktop/TestSuite",
249                                            "org.freedesktop.TestSuite",
250                                            &error);
251   
252   if (proxy == NULL)
253     lose_gerror ("Failed to create proxy for name owner", error);
254
255   call = dbus_g_proxy_begin_call (proxy, "Echo",
256                                   G_TYPE_STRING,
257                                   "my string hello",
258                                   G_TYPE_INVALID);
259
260   error = NULL;
261   if (!dbus_g_proxy_end_call (proxy, call, &error,
262                               G_TYPE_STRING, &v_STRING_2,
263                               G_TYPE_INVALID))
264     lose_gerror ("Failed to complete Echo call", error);
265
266   g_print ("String echoed = \"%s\"\n", v_STRING_2);
267   g_free (v_STRING_2);
268
269   /* Test oneway call and signal handling */
270
271   dbus_g_proxy_add_signal (proxy, "Foo", G_TYPE_DOUBLE, G_TYPE_INVALID);
272   
273   dbus_g_proxy_connect_signal (proxy, "Foo",
274                                G_CALLBACK (foo_signal_handler),
275                                NULL, NULL);
276   
277   dbus_g_proxy_call_no_reply (proxy, "EmitFoo",
278                               G_TYPE_INVALID);
279   
280   dbus_g_connection_flush (connection);
281   exit_timeout = g_timeout_add (5000, timed_exit, loop);
282   g_main_loop_run (loop);
283
284   if (n_times_foo_received != 1)
285     lose ("Foo signal received %d times, should have been 1", n_times_foo_received);
286   
287   /* Activate test servie */ 
288   g_print ("Activating TestSuiteGLibService\n");
289   error = NULL;
290   if (!dbus_g_proxy_call (driver, "StartServiceByName", &error,
291                           G_TYPE_STRING,
292                           "org.freedesktop.DBus.TestSuiteGLibService",
293                           G_TYPE_UINT,
294                           0,
295                           G_TYPE_INVALID,
296                           G_TYPE_UINT, &result,
297                           G_TYPE_INVALID)) {
298     lose_gerror ("Failed to complete Activate call", error);
299   }
300
301   g_print ("TestSuiteGLibService activated\n");
302
303   if (getenv ("DBUS_GLIB_TEST_SLEEP_AFTER_ACTIVATION"))
304     g_usleep (8 * G_USEC_PER_SEC);
305
306   g_object_unref (G_OBJECT (proxy));
307
308   proxy = dbus_g_proxy_new_for_name_owner (connection,
309                                            "org.freedesktop.DBus.TestSuiteGLibService",
310                                            "/org/freedesktop/DBus/Tests/MyTestObject",
311                                            "org.freedesktop.DBus.Tests.MyObject",
312                                            &error);
313   
314   if (proxy == NULL)
315     lose_gerror ("Failed to create proxy for name owner", error);
316
317   g_print ("Beginning method calls\n");
318
319   call = dbus_g_proxy_begin_call (proxy, "DoNothing",
320                                   G_TYPE_INVALID);
321   error = NULL;
322   if (!dbus_g_proxy_end_call (proxy, call, &error, G_TYPE_INVALID))
323     lose_gerror ("Failed to complete DoNothing call", error);
324
325   error = NULL;
326   if (!dbus_g_proxy_call (proxy, "Increment", &error,
327                           G_TYPE_UINT, 42,
328                           G_TYPE_INVALID,
329                           G_TYPE_UINT, &v_UINT32_2,
330                           G_TYPE_INVALID))
331     lose_gerror ("Failed to complete Increment call", error);
332
333   if (v_UINT32_2 != 43)
334     lose ("Increment call returned %d, should be 43", v_UINT32_2);
335
336   call = dbus_g_proxy_begin_call (proxy, "ThrowError", G_TYPE_INVALID);
337   error = NULL;
338   if (dbus_g_proxy_end_call (proxy, call, &error, G_TYPE_INVALID) != FALSE)
339     lose ("ThrowError call unexpectedly succeeded!");
340   if (!dbus_g_error_has_name (error, "org.freedesktop.DBus.Tests.MyObject.Foo"))
341     lose ("ThrowError call returned unexpected error %s", dbus_g_error_get_name (error));
342
343   g_print ("ThrowError failed (as expected) returned error: %s\n", error->message);
344   g_clear_error (&error);
345
346   call = dbus_g_proxy_begin_call (proxy, "Uppercase",
347                                   G_TYPE_STRING, "foobar",
348                                   G_TYPE_INVALID);
349   error = NULL;
350   if (!dbus_g_proxy_end_call (proxy, call, &error,
351                               G_TYPE_STRING, &v_STRING_2,
352                               G_TYPE_INVALID))
353     lose_gerror ("Failed to complete Uppercase call", error);
354   if (strcmp ("FOOBAR", v_STRING_2) != 0)
355     lose ("Uppercase call returned unexpected string %s", v_STRING_2);
356   g_free (v_STRING_2);
357
358   v_STRING = "bazwhee";
359   v_UINT32 = 26;
360   v_DOUBLE = G_PI;
361   call = dbus_g_proxy_begin_call (proxy, "ManyArgs",
362                                   G_TYPE_UINT, 26,
363                                   G_TYPE_STRING, "bazwhee",
364                                   G_TYPE_DOUBLE, G_PI,
365                                   G_TYPE_INVALID);
366   error = NULL;
367   if (!dbus_g_proxy_end_call (proxy, call, &error,
368                               G_TYPE_DOUBLE, &v_DOUBLE_2,
369                               G_TYPE_STRING, &v_STRING_2,
370                               G_TYPE_INVALID))
371     lose_gerror ("Failed to complete ManyArgs call", error);
372   if (v_DOUBLE_2 < 55 || v_DOUBLE_2 > 56)
373     lose ("ManyArgs call returned unexpected double value %f", v_DOUBLE_2);
374   if (strcmp ("BAZWHEE", v_STRING_2) != 0)
375     lose ("ManyArgs call returned unexpected string %s", v_STRING_2);
376   g_free (v_STRING_2);
377
378   if (!org_freedesktop_DBus_Tests_MyObject_do_nothing (proxy, &error))
379     lose_gerror ("Failed to complete (wrapped) DoNothing call", error);
380
381   if (!org_freedesktop_DBus_Tests_MyObject_increment (proxy, 42, &v_UINT32_2, &error))
382     lose_gerror ("Failed to complete (wrapped) Increment call", error);
383
384   if (v_UINT32_2 != 43)
385     lose ("(wrapped) increment call returned %d, should be 43", v_UINT32_2);
386
387   if (org_freedesktop_DBus_Tests_MyObject_throw_error (proxy, &error) != FALSE)
388     lose ("(wrapped) ThrowError call unexpectedly succeeded!");
389
390   g_print ("(wrapped) ThrowError failed (as expected) returned error: %s\n", error->message);
391   g_clear_error (&error);
392
393   if (!org_freedesktop_DBus_Tests_MyObject_uppercase (proxy, "foobar", &v_STRING_2, &error)) 
394     lose_gerror ("Failed to complete (wrapped) Uppercase call", error);
395   if (strcmp ("FOOBAR", v_STRING_2) != 0)
396     lose ("(wrapped) Uppercase call returned unexpected string %s", v_STRING_2);
397   g_free (v_STRING_2);
398
399   if (!org_freedesktop_DBus_Tests_MyObject_many_args (proxy, 26, "bazwhee", G_PI,
400                                                       &v_DOUBLE_2, &v_STRING_2, &error))
401     lose_gerror ("Failed to complete (wrapped) ManyArgs call", error);
402
403   if (v_DOUBLE_2 < 55 || v_DOUBLE_2 > 56)
404     
405     lose ("(wrapped) ManyArgs call returned unexpected double value %f", v_DOUBLE_2);
406
407   if (strcmp ("BAZWHEE", v_STRING_2) != 0)
408     lose ("(wrapped) ManyArgs call returned unexpected string %s", v_STRING_2);
409   g_free (v_STRING_2);
410
411   {
412     guint32 arg0;
413     char *arg1;
414     gint32 arg2;
415     guint32 arg3;
416     guint32 arg4;
417     char *arg5;
418     
419     if (!org_freedesktop_DBus_Tests_MyObject_many_return (proxy, &arg0, &arg1, &arg2, &arg3, &arg4, &arg5, &error))
420       lose_gerror ("Failed to complete (wrapped) ManyReturn call", error);
421
422     if (arg0 != 42)
423       lose ("(wrapped) ManyReturn call returned unexpected guint32 value %u", arg0);
424
425     if (strcmp ("42", arg1) != 0)
426       lose ("(wrapped) ManyReturn call returned unexpected string %s", arg1);
427     g_free (arg1);
428
429     if (arg2 != -67)
430       lose ("(wrapped) ManyReturn call returned unexpected gint32 value %u", arg2);
431
432     if (arg3 != 2)
433       lose ("(wrapped) ManyReturn call returned unexpected guint32 value %u", arg3);
434
435     if (arg4 != 26)
436       lose ("(wrapped) ManyReturn call returned unexpected guint32 value %u", arg4);
437
438     if (strcmp ("hello world", arg5))
439       lose ("(wrapped) ManyReturn call returned unexpected string %s", arg5);
440     g_free (arg5);
441   }
442
443   {
444     GValue value = {0, };
445
446     g_value_init (&value, G_TYPE_STRING);
447     g_value_set_string (&value, "foo");
448
449     if (!org_freedesktop_DBus_Tests_MyObject_stringify (proxy,
450                                                         &value,
451                                                         &v_STRING_2,
452                                                         &error))
453       lose_gerror ("Failed to complete (wrapped) stringify call", error);
454     if (strcmp ("foo", v_STRING_2) != 0)
455       lose ("(wrapped) stringify call returned unexpected string %s", v_STRING_2);
456     g_free (v_STRING_2);
457
458     g_value_unset (&value);
459     g_value_init (&value, G_TYPE_INT);
460     g_value_set_int (&value, 42);
461
462     if (!org_freedesktop_DBus_Tests_MyObject_stringify (proxy,
463                                                         &value,
464                                                         &v_STRING_2,
465                                                         &error))
466       lose_gerror ("Failed to complete (wrapped) stringify call 2", error);
467     if (strcmp ("42", v_STRING_2) != 0)
468       lose ("(wrapped) stringify call 2 returned unexpected string %s", v_STRING_2);
469     g_value_unset (&value);
470     g_free (v_STRING_2);
471
472     g_value_init (&value, G_TYPE_INT);
473     g_value_set_int (&value, 88);
474     if (!org_freedesktop_DBus_Tests_MyObject_stringify (proxy,
475                                                         &value,
476                                                         NULL,
477                                                         &error))
478       lose_gerror ("Failed to complete (wrapped) stringify call 3", error);
479     g_value_unset (&value);
480
481     if (!org_freedesktop_DBus_Tests_MyObject_unstringify (proxy,
482                                                           "foo",
483                                                           &value,
484                                                           &error))
485       lose_gerror ("Failed to complete (wrapped) unstringify call", error);
486     if (!G_VALUE_HOLDS_STRING (&value))
487       lose ("(wrapped) unstringify call returned unexpected value type %d", (int) G_VALUE_TYPE (&value));
488     if (strcmp (g_value_get_string (&value), "foo"))
489       lose ("(wrapped) unstringify call returned unexpected string %s",
490             g_value_get_string (&value));
491         
492     g_value_unset (&value);
493
494     if (!org_freedesktop_DBus_Tests_MyObject_unstringify (proxy,
495                                                           "10",
496                                                           &value,
497                                                           &error))
498       lose_gerror ("Failed to complete (wrapped) unstringify call", error);
499     if (!G_VALUE_HOLDS_INT (&value))
500       lose ("(wrapped) unstringify call returned unexpected value type %d", (int) G_VALUE_TYPE (&value));
501     if (g_value_get_int (&value) != 10)
502       lose ("(wrapped) unstringify call returned unexpected integer %d",
503             g_value_get_int (&value));
504
505     g_value_unset (&value);
506   }
507
508   {
509     GArray *array;
510     guint32 val;
511     guint32 arraylen;
512
513     array = g_array_new (FALSE, TRUE, sizeof (guint32));
514     val = 42;
515     g_array_append_val (array, val);
516     val = 69;
517     g_array_append_val (array, val);
518     val = 88;
519     g_array_append_val (array, val);
520     val = 26;
521     g_array_append_val (array, val);
522     val = 2;
523     g_array_append_val (array, val);
524
525     arraylen = 0;
526     if (!org_freedesktop_DBus_Tests_MyObject_recursive1 (proxy, array,
527                                                          &arraylen, &error))
528       lose_gerror ("Failed to complete (wrapped) recursive1 call", error);
529     if (arraylen != 5)
530       lose ("(wrapped) recursive1 call returned invalid length %u", arraylen);
531   }
532
533   {
534     GArray *array = NULL;
535     guint32 *arrayvals;
536     
537     if (!org_freedesktop_DBus_Tests_MyObject_recursive2 (proxy, 2, &array, &error))
538       lose_gerror ("Failed to complete (wrapped) Recursive2 call", error);
539
540     if (array == NULL)
541       lose ("(wrapped) Recursive2 call returned NULL");
542     if (array->len != 5)
543       lose ("(wrapped) Recursive2 call returned unexpected array length %u", array->len);
544
545     arrayvals = (guint32*) array->data;
546     if (arrayvals[0] != 42)
547       lose ("(wrapped) Recursive2 call returned unexpected value %d in position 0", arrayvals[0]);
548     if (arrayvals[1] != 26)
549       lose ("(wrapped) Recursive2 call returned unexpected value %d in position 1", arrayvals[1]);
550     if (arrayvals[4] != 2)
551       lose ("(wrapped) Recursive2 call returned unexpected value %d in position 4", arrayvals[4]);
552
553     g_array_free (array, TRUE);
554   }
555
556   {
557     char **strs;
558     char **strs_ret;
559
560     strs = g_new0 (char *, 4);
561     strs[0] = "hello";
562     strs[1] = "HellO";
563     strs[2] = "HELLO";
564     strs[3] = NULL;
565
566     strs_ret = NULL;
567     if (!org_freedesktop_DBus_Tests_MyObject_many_uppercase (proxy, strs, &strs_ret, &error)) 
568       lose_gerror ("Failed to complete (wrapped) ManyUppercase call", error);
569     g_assert (strs_ret != NULL);
570     if (strcmp ("HELLO", strs_ret[0]) != 0)
571       lose ("(wrapped) ManyUppercase call returned unexpected string %s", strs_ret[0]);
572     if (strcmp ("HELLO", strs_ret[1]) != 0)
573       lose ("(wrapped) ManyUppercase call returned unexpected string %s", strs_ret[1]);
574     if (strcmp ("HELLO", strs_ret[2]) != 0)
575       lose ("(wrapped) ManyUppercase call returned unexpected string %s", strs_ret[2]);
576
577     g_strfreev (strs_ret);
578   }
579
580   {
581     GHashTable *table;
582     guint len;
583
584     table = g_hash_table_new (g_str_hash, g_str_equal);
585     g_hash_table_insert (table, "moooo", "b");
586     g_hash_table_insert (table, "xxx", "cow!");
587
588     len = 0;
589     if (!org_freedesktop_DBus_Tests_MyObject_str_hash_len (proxy, table, &len, &error))
590       lose_gerror ("(wrapped) StrHashLen call failed", error);
591     if (len != 13) 
592       lose ("(wrapped) StrHashLen returned unexpected length %u", len);
593     g_hash_table_destroy (table);
594   }
595
596   {
597     GHashTable *table;
598     const char *val;
599
600     if (!org_freedesktop_DBus_Tests_MyObject_get_hash (proxy, &table, &error))
601       lose_gerror ("(wrapped) GetHash call failed", error);
602     val = g_hash_table_lookup (table, "foo");
603     if (val == NULL || strcmp ("bar", val))
604       lose ("(wrapped) StrHashLen returned invalid value %s for key \"foo\"",
605             val ? val : "(null)");
606     val = g_hash_table_lookup (table, "baz");
607     if (val == NULL || strcmp ("whee", val))
608       lose ("(wrapped) StrHashLen returned invalid value %s for key \"whee\"",
609             val ? val : "(null)");
610     val = g_hash_table_lookup (table, "cow");
611     if (val == NULL || strcmp ("crack", val))
612       lose ("(wrapped) StrHashLen returned invalid value %s for key \"cow\"",
613             val ? val : "(null)");
614     if (g_hash_table_size (table) != 3)
615       lose ("(wrapped) StrHashLen returned unexpected hash size %u",
616             g_hash_table_size (table));
617
618     g_hash_table_destroy (table);
619   }
620
621   {
622     guint val;
623     DBusGProxy *ret_proxy;
624
625     if (!org_freedesktop_DBus_Tests_MyObject_objpath (proxy, proxy, &ret_proxy, &error))
626       lose_gerror ("Failed to complete (wrapped) Objpath call", error);
627     if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject2",
628                 dbus_g_proxy_get_path (ret_proxy)) != 0)
629       lose ("(wrapped) objpath call returned unexpected proxy %s",
630             dbus_g_proxy_get_path (ret_proxy));
631
632     val = 1;
633     if (!org_freedesktop_DBus_Tests_MyObject_get_val (ret_proxy, &val, &error))
634       lose_gerror ("Failed to complete (wrapped) GetVal call", error);
635     if (val != 0)
636       lose ("(wrapped) GetVal returned invalid value %d", val);
637
638     if (!org_freedesktop_DBus_Tests_MyObject_increment_val (ret_proxy, &error))
639       lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
640
641     if (!org_freedesktop_DBus_Tests_MyObject_increment_val (ret_proxy, &error))
642       lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
643
644     if (!org_freedesktop_DBus_Tests_MyObject_increment_val (ret_proxy, &error))
645       lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
646
647     if (!org_freedesktop_DBus_Tests_MyObject_get_val (ret_proxy, &val, &error))
648       lose_gerror ("Failed to complete (wrapped) GetVal call", error);
649     if (val != 3)
650       lose ("(wrapped) GetVal returned invalid value %d", val);
651
652     if (!org_freedesktop_DBus_Tests_MyObject_get_val (proxy, &val, &error))
653       lose_gerror ("Failed to complete (wrapped) GetVal call", error);
654     if (val != 0)
655       lose ("(wrapped) GetVal returned invalid value %d", val);
656
657     if (!org_freedesktop_DBus_Tests_MyObject_increment_val (proxy, &error))
658       lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
659
660     if (!org_freedesktop_DBus_Tests_MyObject_get_val (proxy, &val, &error))
661       lose_gerror ("Failed to complete (wrapped) GetVal call", error);
662     if (val != 1)
663       lose ("(wrapped) GetVal returned invalid value %d", val);
664
665     if (!org_freedesktop_DBus_Tests_MyObject_get_val (ret_proxy, &val, &error))
666       lose_gerror ("Failed to complete (wrapped) GetVal call", error);
667     if (val != 3)
668       lose ("(wrapped) GetVal returned invalid value %d", val);
669
670     g_object_unref (G_OBJECT (ret_proxy));
671
672     ret_proxy = NULL;
673     if (!org_freedesktop_DBus_Tests_MyObject_objpath (proxy, proxy, &ret_proxy, &error))
674       lose_gerror ("Failed to complete (wrapped) Objpath call 2", error);
675     if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject2",
676                 dbus_g_proxy_get_path (ret_proxy)) != 0)
677       lose ("(wrapped) objpath call 2 returned unexpected proxy %s",
678             dbus_g_proxy_get_path (ret_proxy));
679     {
680       const char *iface = dbus_g_proxy_get_interface (ret_proxy);
681       g_print ("returned proxy has interface \"%s\"\n",
682                iface ? iface : "(NULL)");
683     }
684
685     dbus_g_proxy_set_interface (ret_proxy, "org.freedesktop.DBus.Tests.FooObject");
686
687     val = 0;
688     if (!org_freedesktop_DBus_Tests_FooObject_get_value (ret_proxy, &val, &error))
689       lose_gerror ("Failed to complete (wrapped) GetValue call", error);
690     if (val != 3)
691       lose ("(wrapped) GetValue returned invalid value %d", val);
692   }
693
694   /* Signal handling tests */
695   
696   dbus_g_proxy_add_signal (proxy, "Frobnicate", G_TYPE_INT, G_TYPE_INVALID);
697   
698   dbus_g_proxy_connect_signal (proxy, "Frobnicate",
699                                G_CALLBACK (frobnicate_signal_handler),
700                                NULL, NULL);
701   
702   if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
703                           G_TYPE_INVALID, G_TYPE_INVALID))
704     lose_gerror ("Failed to complete EmitFrobnicate call", error);
705
706   
707   dbus_g_connection_flush (connection);
708   exit_timeout = g_timeout_add (5000, timed_exit, loop);
709   g_main_loop_run (loop);
710
711   if (n_times_frobnicate_received != 1)
712     lose ("Frobnicate signal received %d times, should have been 1", n_times_frobnicate_received);
713
714   if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
715                           G_TYPE_INVALID, G_TYPE_INVALID))
716     lose_gerror ("Failed to complete EmitFrobnicate call", error);
717   
718   dbus_g_connection_flush (connection);
719   exit_timeout = g_timeout_add (5000, timed_exit, loop);
720   g_main_loop_run (loop);
721
722   if (n_times_frobnicate_received != 2)
723     lose ("Frobnicate signal received %d times, should have been 2", n_times_frobnicate_received);
724
725   g_object_unref (G_OBJECT (proxy));
726
727   proxy = dbus_g_proxy_new_for_name_owner (connection,
728                                            "org.freedesktop.DBus.TestSuiteGLibService",
729                                            "/org/freedesktop/DBus/Tests/MyTestObject",
730                                            "org.freedesktop.DBus.Tests.FooObject",
731                                            &error);
732   
733   if (proxy == NULL)
734     lose_gerror ("Failed to create proxy for name owner", error);
735
736   dbus_g_object_register_marshaller (my_object_marshal_VOID__STRING_INT_STRING, 
737                                      G_TYPE_NONE, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INVALID);
738
739   dbus_g_object_register_marshaller (my_object_marshal_VOID__STRING_BOXED, 
740                                      G_TYPE_NONE, G_TYPE_STRING, G_TYPE_VALUE, G_TYPE_INVALID);
741
742   dbus_g_proxy_add_signal (proxy, "Sig0", G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INVALID);
743   dbus_g_proxy_add_signal (proxy, "Sig1", G_TYPE_STRING, G_TYPE_VALUE, G_TYPE_INVALID);
744   dbus_g_proxy_add_signal (proxy, "Sig2", DBUS_TYPE_G_STRING_STRING_HASHTABLE, G_TYPE_INVALID);
745   
746   dbus_g_proxy_connect_signal (proxy, "Sig0",
747                                G_CALLBACK (sig0_signal_handler),
748                                NULL, NULL);
749   dbus_g_proxy_connect_signal (proxy, "Sig1",
750                                G_CALLBACK (sig1_signal_handler),
751                                NULL, NULL);
752   dbus_g_proxy_connect_signal (proxy, "Sig2",
753                                G_CALLBACK (sig2_signal_handler),
754                                NULL, NULL);
755
756   dbus_g_proxy_call_no_reply (proxy, "EmitSignals", G_TYPE_INVALID);
757
758   dbus_g_connection_flush (connection);
759   exit_timeout = g_timeout_add (5000, timed_exit, loop);
760   g_main_loop_run (loop);
761   exit_timeout = g_timeout_add (5000, timed_exit, loop);
762   g_main_loop_run (loop);
763
764   if (n_times_sig0_received != 1)
765     lose ("Sig0 signal received %d times, should have been 1", n_times_sig0_received);
766   if (n_times_sig1_received != 1)
767     lose ("Sig1 signal received %d times, should have been 1", n_times_sig1_received);
768
769   dbus_g_proxy_call_no_reply (proxy, "EmitSignal2", G_TYPE_INVALID);
770   dbus_g_connection_flush (connection);
771
772   exit_timeout = g_timeout_add (5000, timed_exit, loop);
773   g_main_loop_run (loop);
774
775   if (n_times_sig2_received != 1)
776     lose ("Sig2 signal received %d times, should have been 1", n_times_sig2_received);
777
778   dbus_g_proxy_call_no_reply (proxy, "EmitSignals", G_TYPE_INVALID);
779   dbus_g_proxy_call_no_reply (proxy, "EmitSignals", G_TYPE_INVALID);
780
781   dbus_g_connection_flush (connection);
782   exit_timeout = g_timeout_add (5000, timed_exit, loop);
783   g_main_loop_run (loop);
784   exit_timeout = g_timeout_add (5000, timed_exit, loop);
785   g_main_loop_run (loop);
786   exit_timeout = g_timeout_add (5000, timed_exit, loop);
787   g_main_loop_run (loop);
788   exit_timeout = g_timeout_add (5000, timed_exit, loop);
789   g_main_loop_run (loop);
790
791   if (n_times_sig0_received != 3)
792     lose ("Sig0 signal received %d times, should have been 3", n_times_sig0_received);
793   if (n_times_sig1_received != 3)
794     lose ("Sig1 signal received %d times, should have been 3", n_times_sig1_received);
795   
796   g_object_unref (G_OBJECT (proxy));
797
798   proxy = dbus_g_proxy_new_for_name_owner (connection,
799                                            "org.freedesktop.DBus.TestSuiteGLibService",
800                                            "/org/freedesktop/DBus/Tests/MyTestObject",
801                                            "org.freedesktop.DBus.Introspectable",
802                                            &error);
803   
804   if (proxy == NULL)
805     lose_gerror ("Failed to create proxy for name owner", error);
806
807   call = dbus_g_proxy_begin_call (proxy, "Introspect",
808                                   G_TYPE_INVALID);
809   error = NULL;
810   if (!dbus_g_proxy_end_call (proxy, call, &error,
811                               G_TYPE_STRING, &v_STRING_2,
812                               G_TYPE_INVALID))
813     lose_gerror ("Failed to complete Introspect call", error);
814
815   /* Could just do strcmp(), but that seems more fragile */
816   {
817     NodeInfo *node;
818     GSList *elt;
819     gboolean found_introspectable;
820     gboolean found_properties;
821     gboolean found_myobject;
822     gboolean found_fooobject;
823
824     node = description_load_from_string (v_STRING_2, strlen (v_STRING_2), &error);
825     if (!node)
826       lose_gerror ("Failed to parse introspection data: %s", error);
827
828     found_introspectable = FALSE;
829     found_properties = FALSE;
830     found_myobject = FALSE;
831     found_fooobject = FALSE;
832     for (elt = node_info_get_interfaces (node); elt ; elt = elt->next)
833       {
834         InterfaceInfo *iface = elt->data;
835
836         if (!found_introspectable && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Introspectable") == 0)
837           found_introspectable = TRUE;
838         else if (!found_properties && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Properties") == 0)
839           found_properties = TRUE;
840         else if (!found_myobject && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Tests.MyObject") == 0)
841           {
842             GSList *elt;
843             gboolean found_manyargs;
844             
845             found_myobject = TRUE;
846             
847             found_manyargs = FALSE;
848             for (elt = interface_info_get_methods (iface); elt; elt = elt->next)
849               {
850                 MethodInfo *method;
851
852                 method = elt->data;
853                 if (strcmp (method_info_get_name (method), "ManyArgs") == 0)
854                   {
855                     found_manyargs = TRUE;
856                     break;
857                   }
858               }
859             if (!found_manyargs)
860               lose ("Missing method org.freedesktop.DBus.Tests.MyObject.ManyArgs");
861           }
862         else if (!found_fooobject && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Tests.FooObject") == 0)
863           found_fooobject = TRUE;
864         else
865           lose ("Unexpected or duplicate interface %s", interface_info_get_name (iface));
866       }
867
868     if (!(found_introspectable && found_myobject && found_properties))
869       lose ("Missing interface"); 
870   }
871   g_free (v_STRING_2);
872   
873   g_object_unref (G_OBJECT (driver));
874
875   g_print ("Successfully completed %s\n", argv[0]);
876   
877   return 0;
878 }