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