1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 #include <dbus/dbus-glib.h>
6 #include "test-service-glib-bindings.h"
7 #include <glib/dbus-gidl.h>
8 #include <glib/dbus-gparser.h>
10 #include <glib-object.h>
11 #include "my-object-marshal.h"
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;
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;
30 timed_exit (gpointer loop)
32 g_print ("timed exit!\n");
33 g_main_loop_quit (loop);
38 proxy_destroyed_cb (DBusGProxy *proxy, gpointer user_data)
40 proxy_destroyed = TRUE;
41 if (proxy_destroy_and_nameowner && !proxy_destroy_and_nameowner_complete && await_terminating_service == NULL)
43 g_main_loop_quit (loop);
44 proxy_destroy_and_nameowner_complete = TRUE;
49 name_owner_changed (DBusGProxy *proxy,
51 const char *prev_owner,
52 const char *new_owner,
55 g_print ("(signal NameOwnerChanged) name owner changed for %s from %s to %s\n",
56 name, prev_owner, new_owner);
57 if (await_terminating_service &&
58 !strcmp (name, await_terminating_service)
59 && !strcmp ("", new_owner))
61 g_print ("Caught expected ownership loss for %s\n", name);
62 await_terminating_service = NULL;
63 if (proxy_destroy_and_nameowner && !proxy_destroy_and_nameowner_complete && proxy_destroyed)
65 g_main_loop_quit (loop);
66 proxy_destroy_and_nameowner_complete = TRUE;
68 else if (!proxy_destroy_and_nameowner)
70 g_main_loop_quit (loop);
76 foo_signal_handler (DBusGProxy *proxy,
80 n_times_foo_received += 1;
82 g_print ("Got Foo signal\n");
84 g_main_loop_quit (loop);
85 g_source_remove (exit_timeout);
89 frobnicate_signal_handler (DBusGProxy *proxy,
93 n_times_frobnicate_received += 1;
96 g_print ("Got Frobnicate signal\n");
98 g_main_loop_quit (loop);
99 g_source_remove (exit_timeout);
103 frobnicate_signal_handler_2 (DBusGProxy *proxy,
107 n_times_frobnicate_received_2 += 1;
109 g_assert (val == 42);
110 g_print ("Got Frobnicate signal (again)\n");
114 sig0_signal_handler (DBusGProxy *proxy,
120 n_times_sig0_received += 1;
122 g_assert (!strcmp (str0, "foo"));
124 g_assert (val == 22);
126 g_assert (!strcmp (str1, "moo"));
128 g_print ("Got Sig0 signal\n");
130 g_main_loop_quit (loop);
131 g_source_remove (exit_timeout);
135 sig1_signal_handler (DBusGProxy *proxy,
140 n_times_sig1_received += 1;
142 g_assert (!strcmp (str0, "baz"));
144 g_assert (G_VALUE_HOLDS_STRING (value));
146 g_assert (!strcmp (g_value_get_string (value), "bar"));
148 g_print ("Got Sig1 signal\n");
150 g_main_loop_quit (loop);
151 g_source_remove (exit_timeout);
155 sig2_signal_handler (DBusGProxy *proxy,
159 n_times_sig2_received += 1;
161 g_assert (g_hash_table_size (table) == 2);
163 g_assert (g_hash_table_lookup (table, "baz") != NULL);
164 g_assert (!strcmp (g_hash_table_lookup (table, "baz"), "cow"));
165 g_assert (g_hash_table_lookup (table, "bar") != NULL);
166 g_assert (!strcmp (g_hash_table_lookup (table, "bar"), "foo"));
168 g_print ("Got Sig2 signal\n");
170 g_main_loop_quit (loop);
171 g_source_remove (exit_timeout);
174 static DBusGProxyCall *echo_call;
175 static guint n_times_echo_cb_entered;
177 echo_received_cb (DBusGProxy *proxy,
178 DBusGProxyCall *call,
184 g_assert (call == echo_call);
185 g_assert (data == NULL);
189 n_times_echo_cb_entered++;
191 if (!dbus_g_proxy_end_call (proxy, call, &error,
195 lose_gerror ("Failed to complete async Echo", error);
196 g_assert (echo_data != NULL);
197 g_print ("Async echo gave \"%s\"", echo_data);
198 g_main_loop_quit (loop);
199 g_source_remove (exit_timeout);
203 increment_received_cb (DBusGProxy *proxy,
204 DBusGProxyCall *call,
211 g_assert (!strcmp (data, "moo"));
215 if (!dbus_g_proxy_end_call (proxy, call, &error,
218 lose_gerror ("Failed to complete (async) Increment call", error);
221 lose ("Increment call returned %d, should be 43", val);
223 g_print ("Async increment gave \"%d\"", val);
224 g_main_loop_quit (loop);
225 g_source_remove (exit_timeout);
229 lose (const char *str, ...)
233 va_start (args, str);
235 vfprintf (stderr, str, args);
236 fputc ('\n', stderr);
244 lose_gerror (const char *prefix, GError *error)
246 if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION)
247 lose ("%s (%s): %s", prefix, dbus_g_error_get_name (error),
250 lose ("%s: %s", prefix, error->message);
258 ctx = g_main_loop_get_context (loop);
260 while (g_main_context_pending (ctx))
261 g_main_context_iteration (ctx, FALSE);
265 main (int argc, char **argv)
267 DBusGConnection *connection;
275 DBusGProxyCall *call;
283 g_log_set_always_fatal (G_LOG_LEVEL_WARNING);
285 loop = g_main_loop_new (NULL, FALSE);
288 connection = dbus_g_bus_get (DBUS_BUS_SESSION,
290 if (connection == NULL)
291 lose_gerror ("Failed to open connection to bus", error);
293 /* should always get the same one */
294 g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
295 g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
296 g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
298 /* Create a proxy object for the "bus driver" */
300 driver = dbus_g_proxy_new_for_name (connection,
303 DBUS_INTERFACE_DBUS);
305 dbus_g_proxy_add_signal (driver,
312 dbus_g_proxy_connect_signal (driver,
314 G_CALLBACK (name_owner_changed),
317 /* Call ListNames method */
320 if (!dbus_g_proxy_call (driver, "ListNames", &error,
322 G_TYPE_STRV, &name_list,
324 lose_gerror ("Failed to complete ListNames call", error);
326 g_print ("Names on the message bus:\n");
328 name_list_len = g_strv_length (name_list);
329 while (i < name_list_len)
331 g_assert (name_list[i] != NULL);
332 g_print (" %s\n", name_list[i]);
335 g_assert (name_list[i] == NULL);
337 g_strfreev (name_list);
339 g_print ("calling ThisMethodDoesNotExist\n");
340 /* Test handling of unknown method */
341 if (dbus_g_proxy_call (driver, "ThisMethodDoesNotExist", &error,
343 "blah blah blah blah blah",
346 G_TYPE_INVALID, G_TYPE_INVALID) != FALSE)
347 lose ("Calling nonexistent method succeeded!");
349 g_print ("Got EXPECTED error from calling unknown method: %s\n", error->message);
350 g_clear_error (&error);
354 /* Activate a service */
355 g_print ("Activating echo service\n");
356 if (!dbus_g_proxy_call (driver, "StartServiceByName", &error,
358 "org.freedesktop.DBus.TestSuiteEchoService",
361 G_TYPE_UINT, &result,
363 lose_gerror ("Failed to complete Activate call", error);
365 g_print ("Starting echo service result = 0x%x\n", result);
367 /* Activate a service again */
368 g_print ("Activating echo service again\n");
369 if (!dbus_g_proxy_call (driver, "StartServiceByName", &error,
371 "org.freedesktop.DBus.TestSuiteEchoService",
375 G_TYPE_UINT, &result,
377 lose_gerror ("Failed to complete Activate call", error);
379 g_print ("Duplicate start of echo service = 0x%x\n", result);
381 /* Talk to the new service */
383 g_print ("Creating proxy for echo service\n");
384 proxy = dbus_g_proxy_new_for_name_owner (connection,
385 "org.freedesktop.DBus.TestSuiteEchoService",
386 "/org/freedesktop/TestSuite",
387 "org.freedesktop.TestSuite",
391 lose_gerror ("Failed to create proxy for name owner", error);
395 g_print ("Calling Echo\n");
396 if (!dbus_g_proxy_call (proxy, "Echo", &error,
397 G_TYPE_STRING, "my string hello",
399 G_TYPE_STRING, &v_STRING_2,
401 lose_gerror ("Failed to complete Echo call", error);
403 g_print ("String echoed = \"%s\"\n", v_STRING_2);
406 g_print ("Calling Echo (async)\n");
407 echo_call = dbus_g_proxy_begin_call (proxy, "Echo",
408 echo_received_cb, NULL, NULL,
409 G_TYPE_STRING, "my string hello",
411 dbus_g_connection_flush (connection);
412 exit_timeout = g_timeout_add (5000, timed_exit, loop);
413 g_main_loop_run (loop);
415 /* Test oneway call and signal handling */
417 g_print ("Testing Foo emission\n");
418 dbus_g_proxy_add_signal (proxy, "Foo", G_TYPE_DOUBLE, G_TYPE_INVALID);
420 dbus_g_proxy_connect_signal (proxy, "Foo",
421 G_CALLBACK (foo_signal_handler),
424 dbus_g_proxy_call_no_reply (proxy, "EmitFoo",
427 dbus_g_connection_flush (connection);
428 exit_timeout = g_timeout_add (5000, timed_exit, loop);
429 g_main_loop_run (loop);
431 if (n_times_foo_received != 1)
432 lose ("Foo signal received %d times, should have been 1", n_times_foo_received);
434 /* Activate test servie */
435 g_print ("Activating TestSuiteGLibService\n");
437 if (!dbus_g_proxy_call (driver, "StartServiceByName", &error,
439 "org.freedesktop.DBus.TestSuiteGLibService",
443 G_TYPE_UINT, &result,
445 lose_gerror ("Failed to complete Activate call", error);
448 g_print ("TestSuiteGLibService activated\n");
450 if (getenv ("DBUS_GLIB_TEST_SLEEP_AFTER_ACTIVATION"))
451 g_usleep (8 * G_USEC_PER_SEC);
453 g_object_unref (G_OBJECT (proxy));
457 proxy = dbus_g_proxy_new_for_name_owner (connection,
458 "org.freedesktop.DBus.TestSuiteGLibService",
459 "/org/freedesktop/DBus/Tests/MyTestObject",
460 "org.freedesktop.DBus.Tests.MyObject",
464 lose_gerror ("Failed to create proxy for name owner", error);
466 g_print ("Calling DoNothing\n");
467 if (!dbus_g_proxy_call (proxy, "DoNothing", &error,
468 G_TYPE_INVALID, G_TYPE_INVALID))
469 lose_gerror ("Failed to complete DoNothing call", error);
471 g_print ("Calling Increment\n");
473 if (!dbus_g_proxy_call (proxy, "Increment", &error,
476 G_TYPE_UINT, &v_UINT32_2,
478 lose_gerror ("Failed to complete Increment call", error);
479 g_assert (n_times_echo_cb_entered == 1);
481 if (v_UINT32_2 != 43)
482 lose ("Increment call returned %d, should be 43", v_UINT32_2);
484 call = dbus_g_proxy_begin_call (proxy, "Increment",
485 increment_received_cb, g_strdup ("moo"), g_free,
488 dbus_g_connection_flush (connection);
489 exit_timeout = g_timeout_add (5000, timed_exit, loop);
490 g_main_loop_run (loop);
492 g_print ("Calling ThrowError\n");
493 if (dbus_g_proxy_call (proxy, "ThrowError", &error,
494 G_TYPE_INVALID, G_TYPE_INVALID) != FALSE)
495 lose ("ThrowError call unexpectedly succeeded!");
497 if (!dbus_g_error_has_name (error, "org.freedesktop.DBus.Tests.MyObject.Foo"))
498 lose ("ThrowError call returned unexpected error \"%s\": %s", dbus_g_error_get_name (error),
501 g_print ("ThrowError failed (as expected) returned error: %s\n", error->message);
502 g_clear_error (&error);
505 g_print ("Calling Uppercase\n");
506 if (!dbus_g_proxy_call (proxy, "Uppercase", &error,
507 G_TYPE_STRING, "foobar",
509 G_TYPE_STRING, &v_STRING_2,
511 lose_gerror ("Failed to complete Uppercase call", error);
512 if (strcmp ("FOOBAR", v_STRING_2) != 0)
513 lose ("Uppercase call returned unexpected string %s", v_STRING_2);
518 g_print ("Calling ManyArgs\n");
519 if (!dbus_g_proxy_call (proxy, "ManyArgs", &error,
521 G_TYPE_STRING, "bazwhee",
524 G_TYPE_DOUBLE, &v_DOUBLE_2,
525 G_TYPE_STRING, &v_STRING_2,
527 lose_gerror ("Failed to complete ManyArgs call", error);
528 if (v_DOUBLE_2 < 55 || v_DOUBLE_2 > 56)
529 lose ("ManyArgs call returned unexpected double value %f", v_DOUBLE_2);
530 if (strcmp ("BAZWHEE", v_STRING_2) != 0)
531 lose ("ManyArgs call returned unexpected string %s", v_STRING_2);
534 g_print ("Calling (wrapped) do_nothing\n");
535 if (!org_freedesktop_DBus_Tests_MyObject_do_nothing (proxy, &error))
536 lose_gerror ("Failed to complete (wrapped) DoNothing call", error);
538 g_print ("Calling (wrapped) increment\n");
539 if (!org_freedesktop_DBus_Tests_MyObject_increment (proxy, 42, &v_UINT32_2, &error))
540 lose_gerror ("Failed to complete (wrapped) Increment call", error);
542 if (v_UINT32_2 != 43)
543 lose ("(wrapped) increment call returned %d, should be 43", v_UINT32_2);
546 if (!org_freedesktop_DBus_Tests_MyObject_async_increment (proxy, 42, &v_UINT32_2, &error))
547 lose_gerror ("Failed to complete (wrapped) AsyncIncrement call", error);
549 if (v_UINT32_2 != 43)
550 lose ("(wrapped) async increment call returned %d, should be 43", v_UINT32_2);
552 g_print ("Calling (wrapped) throw_error\n");
553 if (org_freedesktop_DBus_Tests_MyObject_throw_error (proxy, &error) != FALSE)
554 lose ("(wrapped) ThrowError call unexpectedly succeeded!");
556 g_print ("(wrapped) ThrowError failed (as expected) returned error: %s\n", error->message);
557 g_clear_error (&error);
559 if (org_freedesktop_DBus_Tests_MyObject_async_throw_error (proxy, &error) != FALSE)
560 lose ("(wrapped) AsyncThrowError call unexpectedly succeeded!");
562 g_print ("(wrapped) AsyncThrowError failed (as expected) returned error: %s\n", error->message);
563 g_clear_error (&error);
565 g_print ("Calling (wrapped) uppercase\n");
566 if (!org_freedesktop_DBus_Tests_MyObject_uppercase (proxy, "foobar", &v_STRING_2, &error))
567 lose_gerror ("Failed to complete (wrapped) Uppercase call", error);
568 if (strcmp ("FOOBAR", v_STRING_2) != 0)
569 lose ("(wrapped) Uppercase call returned unexpected string %s", v_STRING_2);
572 g_print ("Calling (wrapped) many_args\n");
573 if (!org_freedesktop_DBus_Tests_MyObject_many_args (proxy, 26, "bazwhee", G_PI,
574 &v_DOUBLE_2, &v_STRING_2, &error))
575 lose_gerror ("Failed to complete (wrapped) ManyArgs call", error);
577 if (v_DOUBLE_2 < 55 || v_DOUBLE_2 > 56)
579 lose ("(wrapped) ManyArgs call returned unexpected double value %f", v_DOUBLE_2);
581 if (strcmp ("BAZWHEE", v_STRING_2) != 0)
582 lose ("(wrapped) ManyArgs call returned unexpected string %s", v_STRING_2);
593 g_print ("Calling (wrapped) many_return\n");
594 if (!org_freedesktop_DBus_Tests_MyObject_many_return (proxy, &arg0, &arg1, &arg2, &arg3, &arg4, &arg5, &error))
595 lose_gerror ("Failed to complete (wrapped) ManyReturn call", error);
598 lose ("(wrapped) ManyReturn call returned unexpected guint32 value %u", arg0);
600 if (strcmp ("42", arg1) != 0)
601 lose ("(wrapped) ManyReturn call returned unexpected string %s", arg1);
605 lose ("(wrapped) ManyReturn call returned unexpected gint32 value %u", arg2);
608 lose ("(wrapped) ManyReturn call returned unexpected guint32 value %u", arg3);
611 lose ("(wrapped) ManyReturn call returned unexpected guint32 value %u", arg4);
613 if (strcmp ("hello world", arg5))
614 lose ("(wrapped) ManyReturn call returned unexpected string %s", arg5);
621 GValue value = {0, };
623 g_value_init (&value, G_TYPE_STRING);
624 g_value_set_string (&value, "foo");
626 g_print ("Calling (wrapped) stringify, with string\n");
627 if (!org_freedesktop_DBus_Tests_MyObject_stringify (proxy,
631 lose_gerror ("Failed to complete (wrapped) stringify call", error);
632 if (strcmp ("foo", v_STRING_2) != 0)
633 lose ("(wrapped) stringify call returned unexpected string %s", v_STRING_2);
636 g_value_unset (&value);
637 g_value_init (&value, G_TYPE_INT);
638 g_value_set_int (&value, 42);
640 g_print ("Calling (wrapped) stringify, with int\n");
641 if (!org_freedesktop_DBus_Tests_MyObject_stringify (proxy,
645 lose_gerror ("Failed to complete (wrapped) stringify call 2", error);
646 if (strcmp ("42", v_STRING_2) != 0)
647 lose ("(wrapped) stringify call 2 returned unexpected string %s", v_STRING_2);
648 g_value_unset (&value);
651 g_value_init (&value, G_TYPE_INT);
652 g_value_set_int (&value, 88);
653 g_print ("Calling (wrapped) stringify, with another int\n");
654 if (!org_freedesktop_DBus_Tests_MyObject_stringify (proxy,
658 lose_gerror ("Failed to complete (wrapped) stringify call 3", error);
659 g_value_unset (&value);
661 g_print ("Calling (wrapped) unstringify, for string\n");
662 if (!org_freedesktop_DBus_Tests_MyObject_unstringify (proxy,
666 lose_gerror ("Failed to complete (wrapped) unstringify call", error);
667 if (!G_VALUE_HOLDS_STRING (&value))
668 lose ("(wrapped) unstringify call returned unexpected value type %d", (int) G_VALUE_TYPE (&value));
669 if (strcmp (g_value_get_string (&value), "foo"))
670 lose ("(wrapped) unstringify call returned unexpected string %s",
671 g_value_get_string (&value));
673 g_value_unset (&value);
675 g_print ("Calling (wrapped) unstringify, for int\n");
676 if (!org_freedesktop_DBus_Tests_MyObject_unstringify (proxy,
680 lose_gerror ("Failed to complete (wrapped) unstringify call", error);
681 if (!G_VALUE_HOLDS_INT (&value))
682 lose ("(wrapped) unstringify call returned unexpected value type %d", (int) G_VALUE_TYPE (&value));
683 if (g_value_get_int (&value) != 10)
684 lose ("(wrapped) unstringify call returned unexpected integer %d",
685 g_value_get_int (&value));
687 g_value_unset (&value);
697 array = g_array_new (FALSE, TRUE, sizeof (guint32));
699 g_array_append_val (array, val);
701 g_array_append_val (array, val);
703 g_array_append_val (array, val);
705 g_array_append_val (array, val);
707 g_array_append_val (array, val);
710 g_print ("Calling (wrapped) recursive1\n");
711 if (!org_freedesktop_DBus_Tests_MyObject_recursive1 (proxy, array,
713 lose_gerror ("Failed to complete (wrapped) recursive1 call", error);
715 lose ("(wrapped) recursive1 call returned invalid length %u", arraylen);
719 GArray *array = NULL;
722 g_print ("Calling (wrapped) recursive2\n");
723 if (!org_freedesktop_DBus_Tests_MyObject_recursive2 (proxy, 2, &array, &error))
724 lose_gerror ("Failed to complete (wrapped) Recursive2 call", error);
727 lose ("(wrapped) Recursive2 call returned NULL");
729 lose ("(wrapped) Recursive2 call returned unexpected array length %u", array->len);
731 arrayvals = (guint32*) array->data;
732 if (arrayvals[0] != 42)
733 lose ("(wrapped) Recursive2 call returned unexpected value %d in position 0", arrayvals[0]);
734 if (arrayvals[1] != 26)
735 lose ("(wrapped) Recursive2 call returned unexpected value %d in position 1", arrayvals[1]);
736 if (arrayvals[4] != 2)
737 lose ("(wrapped) Recursive2 call returned unexpected value %d in position 4", arrayvals[4]);
739 g_array_free (array, TRUE);
748 strs = g_new0 (char *, 4);
755 g_print ("Calling (wrapped) many_uppercase\n");
756 if (!org_freedesktop_DBus_Tests_MyObject_many_uppercase (proxy, strs, &strs_ret, &error))
757 lose_gerror ("Failed to complete (wrapped) ManyUppercase call", error);
758 g_assert (strs_ret != NULL);
759 if (strcmp ("HELLO", strs_ret[0]) != 0)
760 lose ("(wrapped) ManyUppercase call returned unexpected string %s", strs_ret[0]);
761 if (strcmp ("HELLO", strs_ret[1]) != 0)
762 lose ("(wrapped) ManyUppercase call returned unexpected string %s", strs_ret[1]);
763 if (strcmp ("HELLO", strs_ret[2]) != 0)
764 lose ("(wrapped) ManyUppercase call returned unexpected string %s", strs_ret[2]);
767 g_strfreev (strs_ret);
774 table = g_hash_table_new (g_str_hash, g_str_equal);
775 g_hash_table_insert (table, "moooo", "b");
776 g_hash_table_insert (table, "xxx", "cow!");
779 g_print ("Calling (wrapped) str_hash_len\n");
780 if (!org_freedesktop_DBus_Tests_MyObject_str_hash_len (proxy, table, &len, &error))
781 lose_gerror ("(wrapped) StrHashLen call failed", error);
783 lose ("(wrapped) StrHashLen returned unexpected length %u", len);
784 g_hash_table_destroy (table);
791 g_print ("Calling (wrapped) get_hash\n");
792 if (!org_freedesktop_DBus_Tests_MyObject_get_hash (proxy, &table, &error))
793 lose_gerror ("(wrapped) GetHash call failed", error);
794 val = g_hash_table_lookup (table, "foo");
795 if (val == NULL || strcmp ("bar", val))
796 lose ("(wrapped) StrHashLen returned invalid value %s for key \"foo\"",
797 val ? val : "(null)");
798 val = g_hash_table_lookup (table, "baz");
799 if (val == NULL || strcmp ("whee", val))
800 lose ("(wrapped) StrHashLen returned invalid value %s for key \"whee\"",
801 val ? val : "(null)");
802 val = g_hash_table_lookup (table, "cow");
803 if (val == NULL || strcmp ("crack", val))
804 lose ("(wrapped) StrHashLen returned invalid value %s for key \"cow\"",
805 val ? val : "(null)");
806 if (g_hash_table_size (table) != 3)
807 lose ("(wrapped) StrHashLen returned unexpected hash size %u",
808 g_hash_table_size (table));
810 g_hash_table_destroy (table);
818 DBusGProxy *ret_proxy;
820 g_print ("Calling (wrapped) objpath\n");
821 if (!dbus_g_proxy_call (proxy, "Objpath", &error,
822 DBUS_TYPE_G_PROXY, proxy, G_TYPE_INVALID,
823 DBUS_TYPE_G_PROXY, &ret_proxy, G_TYPE_INVALID))
824 lose_gerror ("Failed to complete Objpath call", error);
825 if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject2",
826 dbus_g_proxy_get_path (ret_proxy)) != 0)
827 lose ("(wrapped) objpath call returned unexpected proxy %s",
828 dbus_g_proxy_get_path (ret_proxy));
830 g_print ("Doing get/increment val tests\n");
832 if (!org_freedesktop_DBus_Tests_MyObject_get_val (ret_proxy, &val, &error))
833 lose_gerror ("Failed to complete (wrapped) GetVal call", error);
835 lose ("(wrapped) GetVal returned invalid value %d", val);
837 if (!org_freedesktop_DBus_Tests_MyObject_increment_val (ret_proxy, &error))
838 lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
840 if (!org_freedesktop_DBus_Tests_MyObject_increment_val (ret_proxy, &error))
841 lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
843 if (!org_freedesktop_DBus_Tests_MyObject_increment_val (ret_proxy, &error))
844 lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
846 if (!org_freedesktop_DBus_Tests_MyObject_get_val (ret_proxy, &val, &error))
847 lose_gerror ("Failed to complete (wrapped) GetVal call", error);
849 lose ("(wrapped) GetVal returned invalid value %d", val);
851 if (!org_freedesktop_DBus_Tests_MyObject_get_val (proxy, &val, &error))
852 lose_gerror ("Failed to complete (wrapped) GetVal call", error);
854 lose ("(wrapped) GetVal returned invalid value %d", val);
856 if (!org_freedesktop_DBus_Tests_MyObject_increment_val (proxy, &error))
857 lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
859 if (!org_freedesktop_DBus_Tests_MyObject_get_val (proxy, &val, &error))
860 lose_gerror ("Failed to complete (wrapped) GetVal call", error);
862 lose ("(wrapped) GetVal returned invalid value %d", val);
864 if (!org_freedesktop_DBus_Tests_MyObject_get_val (ret_proxy, &val, &error))
865 lose_gerror ("Failed to complete (wrapped) GetVal call", error);
867 lose ("(wrapped) GetVal returned invalid value %d", val);
869 g_object_unref (G_OBJECT (ret_proxy));
871 g_print ("Calling objpath again\n");
874 if (!dbus_g_proxy_call (proxy, "Objpath", &error,
875 DBUS_TYPE_G_OBJECT_PATH,
876 dbus_g_proxy_get_path (proxy),
878 DBUS_TYPE_G_OBJECT_PATH,
881 lose_gerror ("Failed to complete Objpath call 2", error);
882 if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject2", ret_path) != 0)
883 lose ("Objpath call 2 returned unexpected path %s",
886 ret_proxy = dbus_g_proxy_new_for_name_owner (connection,
887 "org.freedesktop.DBus.TestSuiteGLibService",
889 "org.freedesktop.DBus.Tests.FooObject",
894 if (!org_freedesktop_DBus_Tests_FooObject_get_value (ret_proxy, &val, &error))
895 lose_gerror ("Failed to complete (wrapped) GetValue call", error);
897 lose ("(wrapped) GetValue returned invalid value %d", val);
902 /* Signal handling tests */
904 g_print ("Testing signal handling\n");
905 dbus_g_proxy_add_signal (proxy, "Frobnicate", G_TYPE_INT, G_TYPE_INVALID);
907 dbus_g_proxy_connect_signal (proxy, "Frobnicate",
908 G_CALLBACK (frobnicate_signal_handler),
911 g_print ("Calling EmitFrobnicate\n");
912 if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
913 G_TYPE_INVALID, G_TYPE_INVALID))
914 lose_gerror ("Failed to complete EmitFrobnicate call", error);
917 dbus_g_connection_flush (connection);
918 exit_timeout = g_timeout_add (5000, timed_exit, loop);
919 g_main_loop_run (loop);
921 if (n_times_frobnicate_received != 1)
922 lose ("Frobnicate signal received %d times, should have been 1", n_times_frobnicate_received);
924 g_print ("Calling EmitFrobnicate again\n");
925 if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
926 G_TYPE_INVALID, G_TYPE_INVALID))
927 lose_gerror ("Failed to complete EmitFrobnicate call", error);
929 dbus_g_connection_flush (connection);
930 exit_timeout = g_timeout_add (5000, timed_exit, loop);
931 g_main_loop_run (loop);
933 if (n_times_frobnicate_received != 2)
934 lose ("Frobnicate signal received %d times, should have been 2", n_times_frobnicate_received);
936 g_object_unref (G_OBJECT (proxy));
940 g_print ("Creating proxy for FooObject interface\n");
941 proxy = dbus_g_proxy_new_for_name_owner (connection,
942 "org.freedesktop.DBus.TestSuiteGLibService",
943 "/org/freedesktop/DBus/Tests/MyTestObject",
944 "org.freedesktop.DBus.Tests.FooObject",
948 lose_gerror ("Failed to create proxy for name owner", error);
950 dbus_g_object_register_marshaller (my_object_marshal_VOID__STRING_INT_STRING,
951 G_TYPE_NONE, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INVALID);
953 dbus_g_object_register_marshaller (my_object_marshal_VOID__STRING_BOXED,
954 G_TYPE_NONE, G_TYPE_STRING, G_TYPE_VALUE, G_TYPE_INVALID);
956 dbus_g_proxy_add_signal (proxy, "Sig0", G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INVALID);
957 dbus_g_proxy_add_signal (proxy, "Sig1", G_TYPE_STRING, G_TYPE_VALUE, G_TYPE_INVALID);
958 dbus_g_proxy_add_signal (proxy, "Sig2", DBUS_TYPE_G_STRING_STRING_HASHTABLE, G_TYPE_INVALID);
960 dbus_g_proxy_connect_signal (proxy, "Sig0",
961 G_CALLBACK (sig0_signal_handler),
963 dbus_g_proxy_connect_signal (proxy, "Sig1",
964 G_CALLBACK (sig1_signal_handler),
966 dbus_g_proxy_connect_signal (proxy, "Sig2",
967 G_CALLBACK (sig2_signal_handler),
970 g_print ("Calling FooObject EmitSignals\n");
971 dbus_g_proxy_call_no_reply (proxy, "EmitSignals", G_TYPE_INVALID);
973 dbus_g_connection_flush (connection);
974 exit_timeout = g_timeout_add (5000, timed_exit, loop);
975 g_main_loop_run (loop);
976 exit_timeout = g_timeout_add (5000, timed_exit, loop);
977 g_main_loop_run (loop);
979 if (n_times_sig0_received != 1)
980 lose ("Sig0 signal received %d times, should have been 1", n_times_sig0_received);
981 if (n_times_sig1_received != 1)
982 lose ("Sig1 signal received %d times, should have been 1", n_times_sig1_received);
984 g_print ("Calling FooObject EmitSignals and EmitSignal2\n");
985 dbus_g_proxy_call_no_reply (proxy, "EmitSignal2", G_TYPE_INVALID);
986 dbus_g_connection_flush (connection);
988 exit_timeout = g_timeout_add (5000, timed_exit, loop);
989 g_main_loop_run (loop);
991 if (n_times_sig2_received != 1)
992 lose ("Sig2 signal received %d times, should have been 1", n_times_sig2_received);
994 g_print ("Calling FooObject EmitSignals two more times\n");
995 dbus_g_proxy_call_no_reply (proxy, "EmitSignals", G_TYPE_INVALID);
996 dbus_g_proxy_call_no_reply (proxy, "EmitSignals", G_TYPE_INVALID);
998 dbus_g_connection_flush (connection);
999 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1000 g_main_loop_run (loop);
1001 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1002 g_main_loop_run (loop);
1003 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1004 g_main_loop_run (loop);
1005 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1006 g_main_loop_run (loop);
1008 if (n_times_sig0_received != 3)
1009 lose ("Sig0 signal received %d times, should have been 3", n_times_sig0_received);
1010 if (n_times_sig1_received != 3)
1011 lose ("Sig1 signal received %d times, should have been 3", n_times_sig1_received);
1013 /* Terminate again */
1014 g_print ("Terminating service\n");
1015 await_terminating_service = "org.freedesktop.DBus.TestSuiteGLibService";
1016 dbus_g_proxy_call_no_reply (proxy, "Terminate", G_TYPE_INVALID);
1018 proxy_destroyed = FALSE;
1019 proxy_destroy_and_nameowner = TRUE;
1020 proxy_destroy_and_nameowner_complete = FALSE;
1022 g_signal_connect (G_OBJECT (proxy),
1024 G_CALLBACK (proxy_destroyed_cb),
1027 dbus_g_connection_flush (connection);
1028 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1029 g_main_loop_run (loop);
1031 if (await_terminating_service != NULL)
1032 lose ("Didn't see name loss for \"org.freedesktop.DBus.TestSuiteGLibService\"");
1033 if (!proxy_destroyed)
1034 lose ("Didn't get proxy_destroyed");
1035 g_print ("Proxy destroyed successfully\n");
1037 /* Don't need to unref, proxy was destroyed */
1041 /* Create a new proxy for the name; should not be associated */
1042 proxy = dbus_g_proxy_new_for_name (connection,
1043 "org.freedesktop.DBus.TestSuiteGLibService",
1044 "/org/freedesktop/DBus/Tests/MyTestObject",
1045 "org.freedesktop.DBus.Tests.MyObject");
1046 g_assert (proxy != NULL);
1048 proxy_destroyed = FALSE;
1049 proxy_destroy_and_nameowner = FALSE;
1050 proxy_destroy_and_nameowner_complete = FALSE;
1052 g_signal_connect (G_OBJECT (proxy),
1054 G_CALLBACK (proxy_destroyed_cb),
1057 if (!dbus_g_proxy_call (driver, "GetNameOwner", &error,
1059 "org.freedesktop.DBus.TestSuiteGLibService",
1064 if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_NAME_HAS_NO_OWNER)
1065 g_print ("Got expected error \"org.freedesktop.DBus.Error.NameHasNoOwner\"\n");
1067 lose_gerror ("Unexpected error from GetNameOwner", error);
1069 lose ("GetNameOwner unexpectedly succeeded!");
1070 g_clear_error (&error);
1072 /* This will have the side-effect of activating the service, thus
1073 * causing a NameOwnerChanged, which should let our name proxy
1076 g_print ("Calling Uppercase for name proxy\n");
1077 if (!dbus_g_proxy_call (proxy, "Uppercase", &error,
1078 G_TYPE_STRING, "bazwhee",
1080 G_TYPE_STRING, &v_STRING_2,
1082 lose_gerror ("Failed to complete Uppercase call", error);
1083 g_free (v_STRING_2);
1085 dbus_g_proxy_add_signal (proxy, "Frobnicate", G_TYPE_INT, G_TYPE_INVALID);
1087 dbus_g_proxy_connect_signal (proxy, "Frobnicate",
1088 G_CALLBACK (frobnicate_signal_handler),
1091 g_print ("Calling EmitFrobnicate\n");
1092 if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
1093 G_TYPE_INVALID, G_TYPE_INVALID))
1094 lose_gerror ("Failed to complete EmitFrobnicate call", error);
1096 n_times_frobnicate_received = 0;
1098 dbus_g_connection_flush (connection);
1099 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1100 g_main_loop_run (loop);
1102 if (n_times_frobnicate_received != 1)
1103 lose ("Frobnicate signal received %d times, should have been 1", n_times_frobnicate_received);
1105 /* Now terminate the service, then start it again (implicitly) and wait for signals */
1106 g_print ("Terminating service (2)\n");
1107 await_terminating_service = "org.freedesktop.DBus.TestSuiteGLibService";
1108 dbus_g_proxy_call_no_reply (proxy, "Terminate", G_TYPE_INVALID);
1109 dbus_g_connection_flush (connection);
1110 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1111 g_main_loop_run (loop);
1112 if (await_terminating_service != NULL)
1113 lose ("Didn't see name loss for \"org.freedesktop.DBus.TestSuiteGLibService\"");
1115 if (proxy_destroyed)
1116 lose ("Unexpectedly got proxy_destroyed!");
1118 n_times_frobnicate_received = 0;
1120 g_print ("Calling EmitFrobnicate (2)\n");
1121 if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
1122 G_TYPE_INVALID, G_TYPE_INVALID))
1123 lose_gerror ("Failed to complete EmitFrobnicate call", error);
1125 dbus_g_connection_flush (connection);
1126 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1127 g_main_loop_run (loop);
1129 if (n_times_frobnicate_received != 1)
1130 lose ("Frobnicate signal received %d times, should have been 1", n_times_frobnicate_received);
1132 if (proxy_destroyed)
1133 lose ("Unexpectedly got proxy_destroyed!");
1135 /* Create another proxy for the name; should be associated immediately */
1136 proxy2 = dbus_g_proxy_new_for_name (connection,
1137 "org.freedesktop.DBus.TestSuiteGLibService",
1138 "/org/freedesktop/DBus/Tests/MyTestObject",
1139 "org.freedesktop.DBus.Tests.MyObject");
1140 g_assert (proxy2 != NULL);
1142 dbus_g_proxy_add_signal (proxy2, "Frobnicate", G_TYPE_INT, G_TYPE_INVALID);
1144 dbus_g_proxy_connect_signal (proxy2, "Frobnicate",
1145 G_CALLBACK (frobnicate_signal_handler_2),
1148 g_print ("Calling EmitFrobnicate (3)\n");
1149 if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
1150 G_TYPE_INVALID, G_TYPE_INVALID))
1151 lose_gerror ("Failed to complete EmitFrobnicate call", error);
1153 dbus_g_connection_flush (connection);
1154 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1155 g_main_loop_run (loop);
1157 if (n_times_frobnicate_received != 2)
1158 lose ("Frobnicate signal received %d times for 1st proxy, should have been 2", n_times_frobnicate_received);
1159 if (n_times_frobnicate_received_2 != 1)
1160 lose ("Frobnicate signal received %d times for 2nd proxy, should have been 1", n_times_frobnicate_received_2);
1162 g_object_unref (G_OBJECT (proxy));
1163 g_object_unref (G_OBJECT (proxy2));
1167 /* Test introspection */
1168 proxy = dbus_g_proxy_new_for_name_owner (connection,
1169 "org.freedesktop.DBus.TestSuiteGLibService",
1170 "/org/freedesktop/DBus/Tests/MyTestObject",
1171 "org.freedesktop.DBus.Introspectable",
1174 lose_gerror ("Failed to create proxy for name owner", error);
1176 g_print ("Testing introspect\n");
1177 if (!dbus_g_proxy_call (proxy, "Introspect", &error,
1179 G_TYPE_STRING, &v_STRING_2,
1181 lose_gerror ("Failed to complete Introspect call", error);
1183 /* Could just do strcmp(), but that seems more fragile */
1187 gboolean found_introspectable;
1188 gboolean found_properties;
1189 gboolean found_myobject;
1190 gboolean found_fooobject;
1192 node = description_load_from_string (v_STRING_2, strlen (v_STRING_2), &error);
1194 lose_gerror ("Failed to parse introspection data: %s", error);
1196 found_introspectable = FALSE;
1197 found_properties = FALSE;
1198 found_myobject = FALSE;
1199 found_fooobject = FALSE;
1200 for (elt = node_info_get_interfaces (node); elt ; elt = elt->next)
1202 InterfaceInfo *iface = elt->data;
1204 if (!found_introspectable && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Introspectable") == 0)
1205 found_introspectable = TRUE;
1206 else if (!found_properties && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Properties") == 0)
1207 found_properties = TRUE;
1208 else if (!found_myobject && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Tests.MyObject") == 0)
1211 gboolean found_manyargs;
1213 found_myobject = TRUE;
1215 found_manyargs = FALSE;
1216 for (elt = interface_info_get_methods (iface); elt; elt = elt->next)
1221 if (strcmp (method_info_get_name (method), "ManyArgs") == 0)
1223 found_manyargs = TRUE;
1227 if (!found_manyargs)
1228 lose ("Missing method org.freedesktop.DBus.Tests.MyObject.ManyArgs");
1230 else if (!found_fooobject && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Tests.FooObject") == 0)
1231 found_fooobject = TRUE;
1233 lose ("Unexpected or duplicate interface %s", interface_info_get_name (iface));
1236 if (!(found_introspectable && found_myobject && found_properties))
1237 lose ("Missing interface");
1239 g_free (v_STRING_2);
1241 g_object_unref (G_OBJECT (driver));
1243 g_print ("Successfully completed %s\n", argv[0]);