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\"\n", echo_data);
199 g_main_loop_quit (loop);
200 g_source_remove (exit_timeout);
204 increment_received_cb (DBusGProxy *proxy,
205 DBusGProxyCall *call,
212 g_assert (!strcmp (data, "moo"));
216 if (!dbus_g_proxy_end_call (proxy, call, &error,
219 lose_gerror ("Failed to complete (async) Increment call", error);
222 lose ("Increment call returned %d, should be 43", val);
224 g_print ("Async increment gave \"%d\"\n", val);
225 g_main_loop_quit (loop);
226 g_source_remove (exit_timeout);
230 increment_async_cb (DBusGProxy *proxy, guint val, GError *error, gpointer data)
233 lose_gerror ("Failed to complete (wrapped async) Increment call", error);
236 lose ("(wrapped async) Increment call gave unexpected data");
238 lose ("(wrapped async) Increment call returned %d, should be 43", val);
240 g_print ("(wrapped async) increment gave \"%d\"\n", val);
241 g_main_loop_quit (loop);
242 g_source_remove (exit_timeout);
247 lose (const char *str, ...)
251 va_start (args, str);
253 vfprintf (stderr, str, args);
254 fputc ('\n', stderr);
262 lose_gerror (const char *prefix, GError *error)
264 if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION)
265 lose ("%s (%s): %s", prefix, dbus_g_error_get_name (error),
268 lose ("%s: %s", prefix, error->message);
276 ctx = g_main_loop_get_context (loop);
278 while (g_main_context_pending (ctx))
279 g_main_context_iteration (ctx, FALSE);
283 main (int argc, char **argv)
285 DBusGConnection *connection;
293 DBusGProxyCall *call;
301 g_log_set_always_fatal (G_LOG_LEVEL_WARNING);
303 loop = g_main_loop_new (NULL, FALSE);
306 connection = dbus_g_bus_get (DBUS_BUS_SESSION,
308 if (connection == NULL)
309 lose_gerror ("Failed to open connection to bus", error);
311 /* should always get the same one */
312 g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
313 g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
314 g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
316 /* Create a proxy object for the "bus driver" */
318 driver = dbus_g_proxy_new_for_name (connection,
321 DBUS_INTERFACE_DBUS);
323 dbus_g_proxy_add_signal (driver,
330 dbus_g_proxy_connect_signal (driver,
332 G_CALLBACK (name_owner_changed),
335 /* Call ListNames method */
338 if (!dbus_g_proxy_call (driver, "ListNames", &error,
340 G_TYPE_STRV, &name_list,
342 lose_gerror ("Failed to complete ListNames call", error);
344 g_print ("Names on the message bus:\n");
346 name_list_len = g_strv_length (name_list);
347 while (i < name_list_len)
349 g_assert (name_list[i] != NULL);
350 g_print (" %s\n", name_list[i]);
353 g_assert (name_list[i] == NULL);
355 g_strfreev (name_list);
357 g_print ("calling ThisMethodDoesNotExist\n");
358 /* Test handling of unknown method */
359 if (dbus_g_proxy_call (driver, "ThisMethodDoesNotExist", &error,
361 "blah blah blah blah blah",
364 G_TYPE_INVALID, G_TYPE_INVALID) != FALSE)
365 lose ("Calling nonexistent method succeeded!");
367 g_print ("Got EXPECTED error from calling unknown method: %s\n", error->message);
368 g_clear_error (&error);
372 /* Activate a service */
373 g_print ("Activating echo service\n");
374 if (!dbus_g_proxy_call (driver, "StartServiceByName", &error,
376 "org.freedesktop.DBus.TestSuiteEchoService",
379 G_TYPE_UINT, &result,
381 lose_gerror ("Failed to complete Activate call", error);
383 g_print ("Starting echo service result = 0x%x\n", result);
385 /* Activate a service again */
386 g_print ("Activating echo service again\n");
387 if (!dbus_g_proxy_call (driver, "StartServiceByName", &error,
389 "org.freedesktop.DBus.TestSuiteEchoService",
393 G_TYPE_UINT, &result,
395 lose_gerror ("Failed to complete Activate call", error);
397 g_print ("Duplicate start of echo service = 0x%x\n", result);
399 /* Talk to the new service */
401 g_print ("Creating proxy for echo service\n");
402 proxy = dbus_g_proxy_new_for_name_owner (connection,
403 "org.freedesktop.DBus.TestSuiteEchoService",
404 "/org/freedesktop/TestSuite",
405 "org.freedesktop.TestSuite",
409 lose_gerror ("Failed to create proxy for name owner", error);
413 g_print ("Calling Echo\n");
414 if (!dbus_g_proxy_call (proxy, "Echo", &error,
415 G_TYPE_STRING, "my string hello",
417 G_TYPE_STRING, &v_STRING_2,
419 lose_gerror ("Failed to complete Echo call", error);
421 g_print ("String echoed = \"%s\"\n", v_STRING_2);
424 g_print ("Calling Echo (async)\n");
425 echo_call = dbus_g_proxy_begin_call (proxy, "Echo",
426 echo_received_cb, NULL, NULL,
427 G_TYPE_STRING, "my string hello",
429 dbus_g_connection_flush (connection);
430 exit_timeout = g_timeout_add (5000, timed_exit, loop);
431 g_main_loop_run (loop);
433 /* Test oneway call and signal handling */
435 g_print ("Testing Foo emission\n");
436 dbus_g_proxy_add_signal (proxy, "Foo", G_TYPE_DOUBLE, G_TYPE_INVALID);
438 dbus_g_proxy_connect_signal (proxy, "Foo",
439 G_CALLBACK (foo_signal_handler),
442 dbus_g_proxy_call_no_reply (proxy, "EmitFoo",
445 dbus_g_connection_flush (connection);
446 exit_timeout = g_timeout_add (5000, timed_exit, loop);
447 g_main_loop_run (loop);
449 if (n_times_foo_received != 1)
450 lose ("Foo signal received %d times, should have been 1", n_times_foo_received);
452 /* Activate test servie */
453 g_print ("Activating TestSuiteGLibService\n");
455 if (!dbus_g_proxy_call (driver, "StartServiceByName", &error,
457 "org.freedesktop.DBus.TestSuiteGLibService",
461 G_TYPE_UINT, &result,
463 lose_gerror ("Failed to complete Activate call", error);
466 g_print ("TestSuiteGLibService activated\n");
468 if (getenv ("DBUS_GLIB_TEST_SLEEP_AFTER_ACTIVATION"))
469 g_usleep (8 * G_USEC_PER_SEC);
471 g_object_unref (G_OBJECT (proxy));
475 proxy = dbus_g_proxy_new_for_name_owner (connection,
476 "org.freedesktop.DBus.TestSuiteGLibService",
477 "/org/freedesktop/DBus/Tests/MyTestObject",
478 "org.freedesktop.DBus.Tests.MyObject",
482 lose_gerror ("Failed to create proxy for name owner", error);
484 g_print ("Calling DoNothing\n");
485 if (!dbus_g_proxy_call (proxy, "DoNothing", &error,
486 G_TYPE_INVALID, G_TYPE_INVALID))
487 lose_gerror ("Failed to complete DoNothing call", error);
489 g_print ("Calling Increment\n");
491 if (!dbus_g_proxy_call (proxy, "Increment", &error,
494 G_TYPE_UINT, &v_UINT32_2,
496 lose_gerror ("Failed to complete Increment call", error);
497 g_assert (n_times_echo_cb_entered == 1);
499 if (v_UINT32_2 != 43)
500 lose ("Increment call returned %d, should be 43", v_UINT32_2);
502 call = dbus_g_proxy_begin_call (proxy, "Increment",
503 increment_received_cb, g_strdup ("moo"), g_free,
506 dbus_g_connection_flush (connection);
507 exit_timeout = g_timeout_add (5000, timed_exit, loop);
508 g_main_loop_run (loop);
510 g_print ("Calling ThrowError\n");
511 if (dbus_g_proxy_call (proxy, "ThrowError", &error,
512 G_TYPE_INVALID, G_TYPE_INVALID) != FALSE)
513 lose ("ThrowError call unexpectedly succeeded!");
515 if (!dbus_g_error_has_name (error, "org.freedesktop.DBus.Tests.MyObject.Foo"))
516 lose ("ThrowError call returned unexpected error \"%s\": %s", dbus_g_error_get_name (error),
519 g_print ("ThrowError failed (as expected) returned error: %s\n", error->message);
520 g_clear_error (&error);
523 g_print ("Calling Uppercase\n");
524 if (!dbus_g_proxy_call (proxy, "Uppercase", &error,
525 G_TYPE_STRING, "foobar",
527 G_TYPE_STRING, &v_STRING_2,
529 lose_gerror ("Failed to complete Uppercase call", error);
530 if (strcmp ("FOOBAR", v_STRING_2) != 0)
531 lose ("Uppercase call returned unexpected string %s", v_STRING_2);
536 g_print ("Calling ManyArgs\n");
537 if (!dbus_g_proxy_call (proxy, "ManyArgs", &error,
539 G_TYPE_STRING, "bazwhee",
542 G_TYPE_DOUBLE, &v_DOUBLE_2,
543 G_TYPE_STRING, &v_STRING_2,
545 lose_gerror ("Failed to complete ManyArgs call", error);
546 if (v_DOUBLE_2 < 55 || v_DOUBLE_2 > 56)
547 lose ("ManyArgs call returned unexpected double value %f", v_DOUBLE_2);
548 if (strcmp ("BAZWHEE", v_STRING_2) != 0)
549 lose ("ManyArgs call returned unexpected string %s", v_STRING_2);
552 g_print ("Calling (wrapped) do_nothing\n");
553 if (!org_freedesktop_DBus_Tests_MyObject_do_nothing (proxy, &error))
554 lose_gerror ("Failed to complete (wrapped) DoNothing call", error);
556 g_print ("Calling (wrapped) increment\n");
557 if (!org_freedesktop_DBus_Tests_MyObject_increment (proxy, 42, &v_UINT32_2, &error))
558 lose_gerror ("Failed to complete (wrapped) Increment call", error);
560 if (v_UINT32_2 != 43)
561 lose ("(wrapped) increment call returned %d, should be 43", v_UINT32_2);
563 g_print ("Calling (wrapped async) increment\n");
564 if (!org_freedesktop_DBus_Tests_MyObject_increment_async (proxy, 42, increment_async_cb, NULL))
565 lose_gerror ("Failed to complete (wrapped) Increment call", error);
566 dbus_g_connection_flush (connection);
567 exit_timeout = g_timeout_add (5000, timed_exit, loop);
568 g_main_loop_run (loop);
571 if (!org_freedesktop_DBus_Tests_MyObject_async_increment (proxy, 42, &v_UINT32_2, &error))
572 lose_gerror ("Failed to complete (wrapped) AsyncIncrement call", error);
574 if (v_UINT32_2 != 43)
575 lose ("(wrapped) async increment call returned %d, should be 43", v_UINT32_2);
577 g_print ("Calling (wrapped) throw_error\n");
578 if (org_freedesktop_DBus_Tests_MyObject_throw_error (proxy, &error) != FALSE)
579 lose ("(wrapped) ThrowError call unexpectedly succeeded!");
581 g_print ("(wrapped) ThrowError failed (as expected) returned error: %s\n", error->message);
582 g_clear_error (&error);
584 if (org_freedesktop_DBus_Tests_MyObject_async_throw_error (proxy, &error) != FALSE)
585 lose ("(wrapped) AsyncThrowError call unexpectedly succeeded!");
587 g_print ("(wrapped) AsyncThrowError failed (as expected) returned error: %s\n", error->message);
588 g_clear_error (&error);
590 g_print ("Calling (wrapped) uppercase\n");
591 if (!org_freedesktop_DBus_Tests_MyObject_uppercase (proxy, "foobar", &v_STRING_2, &error))
592 lose_gerror ("Failed to complete (wrapped) Uppercase call", error);
593 if (strcmp ("FOOBAR", v_STRING_2) != 0)
594 lose ("(wrapped) Uppercase call returned unexpected string %s", v_STRING_2);
597 g_print ("Calling (wrapped) many_args\n");
598 if (!org_freedesktop_DBus_Tests_MyObject_many_args (proxy, 26, "bazwhee", G_PI,
599 &v_DOUBLE_2, &v_STRING_2, &error))
600 lose_gerror ("Failed to complete (wrapped) ManyArgs call", error);
602 if (v_DOUBLE_2 < 55 || v_DOUBLE_2 > 56)
604 lose ("(wrapped) ManyArgs call returned unexpected double value %f", v_DOUBLE_2);
606 if (strcmp ("BAZWHEE", v_STRING_2) != 0)
607 lose ("(wrapped) ManyArgs call returned unexpected string %s", v_STRING_2);
618 g_print ("Calling (wrapped) many_return\n");
619 if (!org_freedesktop_DBus_Tests_MyObject_many_return (proxy, &arg0, &arg1, &arg2, &arg3, &arg4, &arg5, &error))
620 lose_gerror ("Failed to complete (wrapped) ManyReturn call", error);
623 lose ("(wrapped) ManyReturn call returned unexpected guint32 value %u", arg0);
625 if (strcmp ("42", arg1) != 0)
626 lose ("(wrapped) ManyReturn call returned unexpected string %s", arg1);
630 lose ("(wrapped) ManyReturn call returned unexpected gint32 value %u", arg2);
633 lose ("(wrapped) ManyReturn call returned unexpected guint32 value %u", arg3);
636 lose ("(wrapped) ManyReturn call returned unexpected guint32 value %u", arg4);
638 if (strcmp ("hello world", arg5))
639 lose ("(wrapped) ManyReturn call returned unexpected string %s", arg5);
646 GValue value = {0, };
648 g_value_init (&value, G_TYPE_STRING);
649 g_value_set_string (&value, "foo");
651 g_print ("Calling (wrapped) stringify, with string\n");
652 if (!org_freedesktop_DBus_Tests_MyObject_stringify (proxy,
656 lose_gerror ("Failed to complete (wrapped) stringify call", error);
657 if (strcmp ("foo", v_STRING_2) != 0)
658 lose ("(wrapped) stringify call returned unexpected string %s", v_STRING_2);
661 g_value_unset (&value);
662 g_value_init (&value, G_TYPE_INT);
663 g_value_set_int (&value, 42);
665 g_print ("Calling (wrapped) stringify, with int\n");
666 if (!org_freedesktop_DBus_Tests_MyObject_stringify (proxy,
670 lose_gerror ("Failed to complete (wrapped) stringify call 2", error);
671 if (strcmp ("42", v_STRING_2) != 0)
672 lose ("(wrapped) stringify call 2 returned unexpected string %s", v_STRING_2);
673 g_value_unset (&value);
676 g_value_init (&value, G_TYPE_INT);
677 g_value_set_int (&value, 88);
678 g_print ("Calling (wrapped) stringify, with another int\n");
679 if (!org_freedesktop_DBus_Tests_MyObject_stringify (proxy,
683 lose_gerror ("Failed to complete (wrapped) stringify call 3", error);
684 g_value_unset (&value);
686 g_print ("Calling (wrapped) unstringify, for string\n");
687 if (!org_freedesktop_DBus_Tests_MyObject_unstringify (proxy,
691 lose_gerror ("Failed to complete (wrapped) unstringify call", error);
692 if (!G_VALUE_HOLDS_STRING (&value))
693 lose ("(wrapped) unstringify call returned unexpected value type %d", (int) G_VALUE_TYPE (&value));
694 if (strcmp (g_value_get_string (&value), "foo"))
695 lose ("(wrapped) unstringify call returned unexpected string %s",
696 g_value_get_string (&value));
698 g_value_unset (&value);
700 g_print ("Calling (wrapped) unstringify, for int\n");
701 if (!org_freedesktop_DBus_Tests_MyObject_unstringify (proxy,
705 lose_gerror ("Failed to complete (wrapped) unstringify call", error);
706 if (!G_VALUE_HOLDS_INT (&value))
707 lose ("(wrapped) unstringify call returned unexpected value type %d", (int) G_VALUE_TYPE (&value));
708 if (g_value_get_int (&value) != 10)
709 lose ("(wrapped) unstringify call returned unexpected integer %d",
710 g_value_get_int (&value));
712 g_value_unset (&value);
722 array = g_array_new (FALSE, TRUE, sizeof (guint32));
724 g_array_append_val (array, val);
726 g_array_append_val (array, val);
728 g_array_append_val (array, val);
730 g_array_append_val (array, val);
732 g_array_append_val (array, val);
735 g_print ("Calling (wrapped) recursive1\n");
736 if (!org_freedesktop_DBus_Tests_MyObject_recursive1 (proxy, array,
738 lose_gerror ("Failed to complete (wrapped) recursive1 call", error);
740 lose ("(wrapped) recursive1 call returned invalid length %u", arraylen);
744 GArray *array = NULL;
747 g_print ("Calling (wrapped) recursive2\n");
748 if (!org_freedesktop_DBus_Tests_MyObject_recursive2 (proxy, 2, &array, &error))
749 lose_gerror ("Failed to complete (wrapped) Recursive2 call", error);
752 lose ("(wrapped) Recursive2 call returned NULL");
754 lose ("(wrapped) Recursive2 call returned unexpected array length %u", array->len);
756 arrayvals = (guint32*) array->data;
757 if (arrayvals[0] != 42)
758 lose ("(wrapped) Recursive2 call returned unexpected value %d in position 0", arrayvals[0]);
759 if (arrayvals[1] != 26)
760 lose ("(wrapped) Recursive2 call returned unexpected value %d in position 1", arrayvals[1]);
761 if (arrayvals[4] != 2)
762 lose ("(wrapped) Recursive2 call returned unexpected value %d in position 4", arrayvals[4]);
764 g_array_free (array, TRUE);
773 strs = g_new0 (char *, 4);
780 g_print ("Calling (wrapped) many_uppercase\n");
781 if (!org_freedesktop_DBus_Tests_MyObject_many_uppercase (proxy, strs, &strs_ret, &error))
782 lose_gerror ("Failed to complete (wrapped) ManyUppercase call", error);
783 g_assert (strs_ret != NULL);
784 if (strcmp ("HELLO", strs_ret[0]) != 0)
785 lose ("(wrapped) ManyUppercase call returned unexpected string %s", strs_ret[0]);
786 if (strcmp ("HELLO", strs_ret[1]) != 0)
787 lose ("(wrapped) ManyUppercase call returned unexpected string %s", strs_ret[1]);
788 if (strcmp ("HELLO", strs_ret[2]) != 0)
789 lose ("(wrapped) ManyUppercase call returned unexpected string %s", strs_ret[2]);
792 g_strfreev (strs_ret);
799 table = g_hash_table_new (g_str_hash, g_str_equal);
800 g_hash_table_insert (table, "moooo", "b");
801 g_hash_table_insert (table, "xxx", "cow!");
804 g_print ("Calling (wrapped) str_hash_len\n");
805 if (!org_freedesktop_DBus_Tests_MyObject_str_hash_len (proxy, table, &len, &error))
806 lose_gerror ("(wrapped) StrHashLen call failed", error);
808 lose ("(wrapped) StrHashLen returned unexpected length %u", len);
809 g_hash_table_destroy (table);
816 g_print ("Calling (wrapped) get_hash\n");
817 if (!org_freedesktop_DBus_Tests_MyObject_get_hash (proxy, &table, &error))
818 lose_gerror ("(wrapped) GetHash call failed", error);
819 val = g_hash_table_lookup (table, "foo");
820 if (val == NULL || strcmp ("bar", val))
821 lose ("(wrapped) StrHashLen returned invalid value %s for key \"foo\"",
822 val ? val : "(null)");
823 val = g_hash_table_lookup (table, "baz");
824 if (val == NULL || strcmp ("whee", val))
825 lose ("(wrapped) StrHashLen returned invalid value %s for key \"whee\"",
826 val ? val : "(null)");
827 val = g_hash_table_lookup (table, "cow");
828 if (val == NULL || strcmp ("crack", val))
829 lose ("(wrapped) StrHashLen returned invalid value %s for key \"cow\"",
830 val ? val : "(null)");
831 if (g_hash_table_size (table) != 3)
832 lose ("(wrapped) StrHashLen returned unexpected hash size %u",
833 g_hash_table_size (table));
835 g_hash_table_destroy (table);
843 DBusGProxy *ret_proxy;
845 g_print ("Calling (wrapped) objpath\n");
846 if (!dbus_g_proxy_call (proxy, "Objpath", &error,
847 DBUS_TYPE_G_PROXY, proxy, G_TYPE_INVALID,
848 DBUS_TYPE_G_PROXY, &ret_proxy, G_TYPE_INVALID))
849 lose_gerror ("Failed to complete Objpath call", error);
850 if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject2",
851 dbus_g_proxy_get_path (ret_proxy)) != 0)
852 lose ("(wrapped) objpath call returned unexpected proxy %s",
853 dbus_g_proxy_get_path (ret_proxy));
855 g_print ("Doing get/increment val tests\n");
857 if (!org_freedesktop_DBus_Tests_MyObject_get_val (ret_proxy, &val, &error))
858 lose_gerror ("Failed to complete (wrapped) GetVal call", error);
860 lose ("(wrapped) GetVal returned invalid value %d", val);
862 if (!org_freedesktop_DBus_Tests_MyObject_increment_val (ret_proxy, &error))
863 lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
865 if (!org_freedesktop_DBus_Tests_MyObject_increment_val (ret_proxy, &error))
866 lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
868 if (!org_freedesktop_DBus_Tests_MyObject_increment_val (ret_proxy, &error))
869 lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
871 if (!org_freedesktop_DBus_Tests_MyObject_get_val (ret_proxy, &val, &error))
872 lose_gerror ("Failed to complete (wrapped) GetVal call", error);
874 lose ("(wrapped) GetVal returned invalid value %d", val);
876 if (!org_freedesktop_DBus_Tests_MyObject_get_val (proxy, &val, &error))
877 lose_gerror ("Failed to complete (wrapped) GetVal call", error);
879 lose ("(wrapped) GetVal returned invalid value %d", val);
881 if (!org_freedesktop_DBus_Tests_MyObject_increment_val (proxy, &error))
882 lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
884 if (!org_freedesktop_DBus_Tests_MyObject_get_val (proxy, &val, &error))
885 lose_gerror ("Failed to complete (wrapped) GetVal call", error);
887 lose ("(wrapped) GetVal returned invalid value %d", val);
889 if (!org_freedesktop_DBus_Tests_MyObject_get_val (ret_proxy, &val, &error))
890 lose_gerror ("Failed to complete (wrapped) GetVal call", error);
892 lose ("(wrapped) GetVal returned invalid value %d", val);
894 g_object_unref (G_OBJECT (ret_proxy));
896 g_print ("Calling objpath again\n");
899 if (!dbus_g_proxy_call (proxy, "Objpath", &error,
900 DBUS_TYPE_G_OBJECT_PATH,
901 dbus_g_proxy_get_path (proxy),
903 DBUS_TYPE_G_OBJECT_PATH,
906 lose_gerror ("Failed to complete Objpath call 2", error);
907 if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject2", ret_path) != 0)
908 lose ("Objpath call 2 returned unexpected path %s",
911 ret_proxy = dbus_g_proxy_new_for_name_owner (connection,
912 "org.freedesktop.DBus.TestSuiteGLibService",
914 "org.freedesktop.DBus.Tests.FooObject",
919 if (!org_freedesktop_DBus_Tests_FooObject_get_value (ret_proxy, &val, &error))
920 lose_gerror ("Failed to complete (wrapped) GetValue call", error);
922 lose ("(wrapped) GetValue returned invalid value %d", val);
931 g_print ("Calling GetObjs\n");
933 if (!dbus_g_proxy_call (proxy, "GetObjs", &error, G_TYPE_INVALID,
934 dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH),
937 lose_gerror ("Failed to complete GetObjs call", error);
939 lose ("GetObjs call returned unexpected number of objects %d, expected 2",
942 if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject",
943 g_ptr_array_index (objs, 0)) != 0)
944 lose ("GetObjs call returned unexpected path \"%s\" in position 0; expected /org/freedesktop/DBus/Tests/MyTestObject", (char*) g_ptr_array_index (objs, 0));
946 if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject2",
947 g_ptr_array_index (objs, 1)) != 0)
948 lose ("GetObjs call returned unexpected path \"%s\" in position 1; expected /org/freedesktop/DBus/Tests/MyTestObject2", (char*) g_ptr_array_index (objs, 1));
950 for (i = 0; i < objs->len; i++)
951 g_free (g_ptr_array_index (objs, i));
952 g_ptr_array_free (objs, TRUE);
955 /* Signal handling tests */
957 g_print ("Testing signal handling\n");
958 dbus_g_proxy_add_signal (proxy, "Frobnicate", G_TYPE_INT, G_TYPE_INVALID);
960 dbus_g_proxy_connect_signal (proxy, "Frobnicate",
961 G_CALLBACK (frobnicate_signal_handler),
964 g_print ("Calling EmitFrobnicate\n");
965 if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
966 G_TYPE_INVALID, G_TYPE_INVALID))
967 lose_gerror ("Failed to complete EmitFrobnicate call", error);
970 dbus_g_connection_flush (connection);
971 exit_timeout = g_timeout_add (5000, timed_exit, loop);
972 g_main_loop_run (loop);
974 if (n_times_frobnicate_received != 1)
975 lose ("Frobnicate signal received %d times, should have been 1", n_times_frobnicate_received);
977 g_print ("Calling EmitFrobnicate again\n");
978 if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
979 G_TYPE_INVALID, G_TYPE_INVALID))
980 lose_gerror ("Failed to complete EmitFrobnicate call", error);
982 dbus_g_connection_flush (connection);
983 exit_timeout = g_timeout_add (5000, timed_exit, loop);
984 g_main_loop_run (loop);
986 if (n_times_frobnicate_received != 2)
987 lose ("Frobnicate signal received %d times, should have been 2", n_times_frobnicate_received);
989 g_object_unref (G_OBJECT (proxy));
993 g_print ("Creating proxy for FooObject interface\n");
994 proxy = dbus_g_proxy_new_for_name_owner (connection,
995 "org.freedesktop.DBus.TestSuiteGLibService",
996 "/org/freedesktop/DBus/Tests/MyTestObject",
997 "org.freedesktop.DBus.Tests.FooObject",
1001 lose_gerror ("Failed to create proxy for name owner", error);
1003 dbus_g_object_register_marshaller (my_object_marshal_VOID__STRING_INT_STRING,
1004 G_TYPE_NONE, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INVALID);
1006 dbus_g_object_register_marshaller (my_object_marshal_VOID__STRING_BOXED,
1007 G_TYPE_NONE, G_TYPE_STRING, G_TYPE_VALUE, G_TYPE_INVALID);
1009 dbus_g_proxy_add_signal (proxy, "Sig0", G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INVALID);
1010 dbus_g_proxy_add_signal (proxy, "Sig1", G_TYPE_STRING, G_TYPE_VALUE, G_TYPE_INVALID);
1011 dbus_g_proxy_add_signal (proxy, "Sig2", DBUS_TYPE_G_STRING_STRING_HASHTABLE, G_TYPE_INVALID);
1013 dbus_g_proxy_connect_signal (proxy, "Sig0",
1014 G_CALLBACK (sig0_signal_handler),
1016 dbus_g_proxy_connect_signal (proxy, "Sig1",
1017 G_CALLBACK (sig1_signal_handler),
1019 dbus_g_proxy_connect_signal (proxy, "Sig2",
1020 G_CALLBACK (sig2_signal_handler),
1023 g_print ("Calling FooObject EmitSignals\n");
1024 dbus_g_proxy_call_no_reply (proxy, "EmitSignals", G_TYPE_INVALID);
1026 dbus_g_connection_flush (connection);
1027 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1028 g_main_loop_run (loop);
1029 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1030 g_main_loop_run (loop);
1032 if (n_times_sig0_received != 1)
1033 lose ("Sig0 signal received %d times, should have been 1", n_times_sig0_received);
1034 if (n_times_sig1_received != 1)
1035 lose ("Sig1 signal received %d times, should have been 1", n_times_sig1_received);
1037 g_print ("Calling FooObject EmitSignals and EmitSignal2\n");
1038 dbus_g_proxy_call_no_reply (proxy, "EmitSignal2", G_TYPE_INVALID);
1039 dbus_g_connection_flush (connection);
1041 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1042 g_main_loop_run (loop);
1044 if (n_times_sig2_received != 1)
1045 lose ("Sig2 signal received %d times, should have been 1", n_times_sig2_received);
1047 g_print ("Calling FooObject EmitSignals two more times\n");
1048 dbus_g_proxy_call_no_reply (proxy, "EmitSignals", G_TYPE_INVALID);
1049 dbus_g_proxy_call_no_reply (proxy, "EmitSignals", G_TYPE_INVALID);
1051 dbus_g_connection_flush (connection);
1052 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1053 g_main_loop_run (loop);
1054 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1055 g_main_loop_run (loop);
1056 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1057 g_main_loop_run (loop);
1058 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1059 g_main_loop_run (loop);
1061 if (n_times_sig0_received != 3)
1062 lose ("Sig0 signal received %d times, should have been 3", n_times_sig0_received);
1063 if (n_times_sig1_received != 3)
1064 lose ("Sig1 signal received %d times, should have been 3", n_times_sig1_received);
1066 /* Terminate again */
1067 g_print ("Terminating service\n");
1068 await_terminating_service = "org.freedesktop.DBus.TestSuiteGLibService";
1069 dbus_g_proxy_call_no_reply (proxy, "Terminate", G_TYPE_INVALID);
1071 proxy_destroyed = FALSE;
1072 proxy_destroy_and_nameowner = TRUE;
1073 proxy_destroy_and_nameowner_complete = FALSE;
1075 g_signal_connect (G_OBJECT (proxy),
1077 G_CALLBACK (proxy_destroyed_cb),
1080 dbus_g_connection_flush (connection);
1081 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1082 g_main_loop_run (loop);
1084 if (await_terminating_service != NULL)
1085 lose ("Didn't see name loss for \"org.freedesktop.DBus.TestSuiteGLibService\"");
1086 if (!proxy_destroyed)
1087 lose ("Didn't get proxy_destroyed");
1088 g_print ("Proxy destroyed successfully\n");
1090 /* Don't need to unref, proxy was destroyed */
1094 /* Create a new proxy for the name; should not be associated */
1095 proxy = dbus_g_proxy_new_for_name (connection,
1096 "org.freedesktop.DBus.TestSuiteGLibService",
1097 "/org/freedesktop/DBus/Tests/MyTestObject",
1098 "org.freedesktop.DBus.Tests.MyObject");
1099 g_assert (proxy != NULL);
1101 proxy_destroyed = FALSE;
1102 proxy_destroy_and_nameowner = FALSE;
1103 proxy_destroy_and_nameowner_complete = FALSE;
1105 g_signal_connect (G_OBJECT (proxy),
1107 G_CALLBACK (proxy_destroyed_cb),
1110 if (!dbus_g_proxy_call (driver, "GetNameOwner", &error,
1112 "org.freedesktop.DBus.TestSuiteGLibService",
1117 if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_NAME_HAS_NO_OWNER)
1118 g_print ("Got expected error \"org.freedesktop.DBus.Error.NameHasNoOwner\"\n");
1120 lose_gerror ("Unexpected error from GetNameOwner", error);
1122 lose ("GetNameOwner unexpectedly succeeded!");
1123 g_clear_error (&error);
1125 /* This will have the side-effect of activating the service, thus
1126 * causing a NameOwnerChanged, which should let our name proxy
1129 g_print ("Calling Uppercase for name proxy\n");
1130 if (!dbus_g_proxy_call (proxy, "Uppercase", &error,
1131 G_TYPE_STRING, "bazwhee",
1133 G_TYPE_STRING, &v_STRING_2,
1135 lose_gerror ("Failed to complete Uppercase call", error);
1136 g_free (v_STRING_2);
1138 dbus_g_proxy_add_signal (proxy, "Frobnicate", G_TYPE_INT, G_TYPE_INVALID);
1140 dbus_g_proxy_connect_signal (proxy, "Frobnicate",
1141 G_CALLBACK (frobnicate_signal_handler),
1144 g_print ("Calling EmitFrobnicate\n");
1145 if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
1146 G_TYPE_INVALID, G_TYPE_INVALID))
1147 lose_gerror ("Failed to complete EmitFrobnicate call", error);
1149 n_times_frobnicate_received = 0;
1151 dbus_g_connection_flush (connection);
1152 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1153 g_main_loop_run (loop);
1155 if (n_times_frobnicate_received != 1)
1156 lose ("Frobnicate signal received %d times, should have been 1", n_times_frobnicate_received);
1158 /* Now terminate the service, then start it again (implicitly) and wait for signals */
1159 g_print ("Terminating service (2)\n");
1160 await_terminating_service = "org.freedesktop.DBus.TestSuiteGLibService";
1161 dbus_g_proxy_call_no_reply (proxy, "Terminate", G_TYPE_INVALID);
1162 dbus_g_connection_flush (connection);
1163 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1164 g_main_loop_run (loop);
1165 if (await_terminating_service != NULL)
1166 lose ("Didn't see name loss for \"org.freedesktop.DBus.TestSuiteGLibService\"");
1168 if (proxy_destroyed)
1169 lose ("Unexpectedly got proxy_destroyed!");
1171 n_times_frobnicate_received = 0;
1173 g_print ("Calling EmitFrobnicate (2)\n");
1174 if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
1175 G_TYPE_INVALID, G_TYPE_INVALID))
1176 lose_gerror ("Failed to complete EmitFrobnicate call", error);
1178 dbus_g_connection_flush (connection);
1179 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1180 g_main_loop_run (loop);
1182 if (n_times_frobnicate_received != 1)
1183 lose ("Frobnicate signal received %d times, should have been 1", n_times_frobnicate_received);
1185 if (proxy_destroyed)
1186 lose ("Unexpectedly got proxy_destroyed!");
1188 /* Create another proxy for the name; should be associated immediately */
1189 proxy2 = dbus_g_proxy_new_for_name (connection,
1190 "org.freedesktop.DBus.TestSuiteGLibService",
1191 "/org/freedesktop/DBus/Tests/MyTestObject",
1192 "org.freedesktop.DBus.Tests.MyObject");
1193 g_assert (proxy2 != NULL);
1195 dbus_g_proxy_add_signal (proxy2, "Frobnicate", G_TYPE_INT, G_TYPE_INVALID);
1197 dbus_g_proxy_connect_signal (proxy2, "Frobnicate",
1198 G_CALLBACK (frobnicate_signal_handler_2),
1201 g_print ("Calling EmitFrobnicate (3)\n");
1202 if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
1203 G_TYPE_INVALID, G_TYPE_INVALID))
1204 lose_gerror ("Failed to complete EmitFrobnicate call", error);
1206 dbus_g_connection_flush (connection);
1207 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1208 g_main_loop_run (loop);
1210 if (n_times_frobnicate_received != 2)
1211 lose ("Frobnicate signal received %d times for 1st proxy, should have been 2", n_times_frobnicate_received);
1212 if (n_times_frobnicate_received_2 != 1)
1213 lose ("Frobnicate signal received %d times for 2nd proxy, should have been 1", n_times_frobnicate_received_2);
1215 g_object_unref (G_OBJECT (proxy));
1216 g_object_unref (G_OBJECT (proxy2));
1220 /* Test introspection */
1221 proxy = dbus_g_proxy_new_for_name_owner (connection,
1222 "org.freedesktop.DBus.TestSuiteGLibService",
1223 "/org/freedesktop/DBus/Tests/MyTestObject",
1224 "org.freedesktop.DBus.Introspectable",
1227 lose_gerror ("Failed to create proxy for name owner", error);
1229 g_print ("Testing introspect\n");
1230 if (!dbus_g_proxy_call (proxy, "Introspect", &error,
1232 G_TYPE_STRING, &v_STRING_2,
1234 lose_gerror ("Failed to complete Introspect call", error);
1236 /* Could just do strcmp(), but that seems more fragile */
1240 gboolean found_introspectable;
1241 gboolean found_properties;
1242 gboolean found_myobject;
1243 gboolean found_fooobject;
1245 node = description_load_from_string (v_STRING_2, strlen (v_STRING_2), &error);
1247 lose_gerror ("Failed to parse introspection data: %s", error);
1249 found_introspectable = FALSE;
1250 found_properties = FALSE;
1251 found_myobject = FALSE;
1252 found_fooobject = FALSE;
1253 for (elt = node_info_get_interfaces (node); elt ; elt = elt->next)
1255 InterfaceInfo *iface = elt->data;
1257 if (!found_introspectable && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Introspectable") == 0)
1258 found_introspectable = TRUE;
1259 else if (!found_properties && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Properties") == 0)
1260 found_properties = TRUE;
1261 else if (!found_myobject && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Tests.MyObject") == 0)
1264 gboolean found_manyargs;
1266 found_myobject = TRUE;
1268 found_manyargs = FALSE;
1269 for (elt = interface_info_get_methods (iface); elt; elt = elt->next)
1274 if (strcmp (method_info_get_name (method), "ManyArgs") == 0)
1276 found_manyargs = TRUE;
1280 if (!found_manyargs)
1281 lose ("Missing method org.freedesktop.DBus.Tests.MyObject.ManyArgs");
1283 else if (!found_fooobject && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Tests.FooObject") == 0)
1284 found_fooobject = TRUE;
1286 lose ("Unexpected or duplicate interface %s", interface_info_get_name (iface));
1289 if (!(found_introspectable && found_myobject && found_properties))
1290 lose ("Missing interface");
1292 g_free (v_STRING_2);
1294 g_object_unref (G_OBJECT (driver));
1296 g_print ("Successfully completed %s\n", argv[0]);