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);
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\"", val);
225 g_main_loop_quit (loop);
226 g_source_remove (exit_timeout);
230 lose (const char *str, ...)
234 va_start (args, str);
236 vfprintf (stderr, str, args);
237 fputc ('\n', stderr);
245 lose_gerror (const char *prefix, GError *error)
247 if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_REMOTE_EXCEPTION)
248 lose ("%s (%s): %s", prefix, dbus_g_error_get_name (error),
251 lose ("%s: %s", prefix, error->message);
259 ctx = g_main_loop_get_context (loop);
261 while (g_main_context_pending (ctx))
262 g_main_context_iteration (ctx, FALSE);
266 main (int argc, char **argv)
268 DBusGConnection *connection;
276 DBusGProxyCall *call;
284 g_log_set_always_fatal (G_LOG_LEVEL_WARNING);
286 loop = g_main_loop_new (NULL, FALSE);
289 connection = dbus_g_bus_get (DBUS_BUS_SESSION,
291 if (connection == NULL)
292 lose_gerror ("Failed to open connection to bus", error);
294 /* should always get the same one */
295 g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
296 g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
297 g_assert (connection == dbus_g_bus_get (DBUS_BUS_SESSION, NULL));
299 /* Create a proxy object for the "bus driver" */
301 driver = dbus_g_proxy_new_for_name (connection,
304 DBUS_INTERFACE_DBUS);
306 dbus_g_proxy_add_signal (driver,
313 dbus_g_proxy_connect_signal (driver,
315 G_CALLBACK (name_owner_changed),
318 /* Call ListNames method */
321 if (!dbus_g_proxy_call (driver, "ListNames", &error,
323 G_TYPE_STRV, &name_list,
325 lose_gerror ("Failed to complete ListNames call", error);
327 g_print ("Names on the message bus:\n");
329 name_list_len = g_strv_length (name_list);
330 while (i < name_list_len)
332 g_assert (name_list[i] != NULL);
333 g_print (" %s\n", name_list[i]);
336 g_assert (name_list[i] == NULL);
338 g_strfreev (name_list);
340 g_print ("calling ThisMethodDoesNotExist\n");
341 /* Test handling of unknown method */
342 if (dbus_g_proxy_call (driver, "ThisMethodDoesNotExist", &error,
344 "blah blah blah blah blah",
347 G_TYPE_INVALID, G_TYPE_INVALID) != FALSE)
348 lose ("Calling nonexistent method succeeded!");
350 g_print ("Got EXPECTED error from calling unknown method: %s\n", error->message);
351 g_clear_error (&error);
355 /* Activate a service */
356 g_print ("Activating echo service\n");
357 if (!dbus_g_proxy_call (driver, "StartServiceByName", &error,
359 "org.freedesktop.DBus.TestSuiteEchoService",
362 G_TYPE_UINT, &result,
364 lose_gerror ("Failed to complete Activate call", error);
366 g_print ("Starting echo service result = 0x%x\n", result);
368 /* Activate a service again */
369 g_print ("Activating echo service again\n");
370 if (!dbus_g_proxy_call (driver, "StartServiceByName", &error,
372 "org.freedesktop.DBus.TestSuiteEchoService",
376 G_TYPE_UINT, &result,
378 lose_gerror ("Failed to complete Activate call", error);
380 g_print ("Duplicate start of echo service = 0x%x\n", result);
382 /* Talk to the new service */
384 g_print ("Creating proxy for echo service\n");
385 proxy = dbus_g_proxy_new_for_name_owner (connection,
386 "org.freedesktop.DBus.TestSuiteEchoService",
387 "/org/freedesktop/TestSuite",
388 "org.freedesktop.TestSuite",
392 lose_gerror ("Failed to create proxy for name owner", error);
396 g_print ("Calling Echo\n");
397 if (!dbus_g_proxy_call (proxy, "Echo", &error,
398 G_TYPE_STRING, "my string hello",
400 G_TYPE_STRING, &v_STRING_2,
402 lose_gerror ("Failed to complete Echo call", error);
404 g_print ("String echoed = \"%s\"\n", v_STRING_2);
407 g_print ("Calling Echo (async)\n");
408 echo_call = dbus_g_proxy_begin_call (proxy, "Echo",
409 echo_received_cb, NULL, NULL,
410 G_TYPE_STRING, "my string hello",
412 dbus_g_connection_flush (connection);
413 exit_timeout = g_timeout_add (5000, timed_exit, loop);
414 g_main_loop_run (loop);
416 /* Test oneway call and signal handling */
418 g_print ("Testing Foo emission\n");
419 dbus_g_proxy_add_signal (proxy, "Foo", G_TYPE_DOUBLE, G_TYPE_INVALID);
421 dbus_g_proxy_connect_signal (proxy, "Foo",
422 G_CALLBACK (foo_signal_handler),
425 dbus_g_proxy_call_no_reply (proxy, "EmitFoo",
428 dbus_g_connection_flush (connection);
429 exit_timeout = g_timeout_add (5000, timed_exit, loop);
430 g_main_loop_run (loop);
432 if (n_times_foo_received != 1)
433 lose ("Foo signal received %d times, should have been 1", n_times_foo_received);
435 /* Activate test servie */
436 g_print ("Activating TestSuiteGLibService\n");
438 if (!dbus_g_proxy_call (driver, "StartServiceByName", &error,
440 "org.freedesktop.DBus.TestSuiteGLibService",
444 G_TYPE_UINT, &result,
446 lose_gerror ("Failed to complete Activate call", error);
449 g_print ("TestSuiteGLibService activated\n");
451 if (getenv ("DBUS_GLIB_TEST_SLEEP_AFTER_ACTIVATION"))
452 g_usleep (8 * G_USEC_PER_SEC);
454 g_object_unref (G_OBJECT (proxy));
458 proxy = dbus_g_proxy_new_for_name_owner (connection,
459 "org.freedesktop.DBus.TestSuiteGLibService",
460 "/org/freedesktop/DBus/Tests/MyTestObject",
461 "org.freedesktop.DBus.Tests.MyObject",
465 lose_gerror ("Failed to create proxy for name owner", error);
467 g_print ("Calling DoNothing\n");
468 if (!dbus_g_proxy_call (proxy, "DoNothing", &error,
469 G_TYPE_INVALID, G_TYPE_INVALID))
470 lose_gerror ("Failed to complete DoNothing call", error);
472 g_print ("Calling Increment\n");
474 if (!dbus_g_proxy_call (proxy, "Increment", &error,
477 G_TYPE_UINT, &v_UINT32_2,
479 lose_gerror ("Failed to complete Increment call", error);
480 g_assert (n_times_echo_cb_entered == 1);
482 if (v_UINT32_2 != 43)
483 lose ("Increment call returned %d, should be 43", v_UINT32_2);
485 call = dbus_g_proxy_begin_call (proxy, "Increment",
486 increment_received_cb, g_strdup ("moo"), g_free,
489 dbus_g_connection_flush (connection);
490 exit_timeout = g_timeout_add (5000, timed_exit, loop);
491 g_main_loop_run (loop);
493 g_print ("Calling ThrowError\n");
494 if (dbus_g_proxy_call (proxy, "ThrowError", &error,
495 G_TYPE_INVALID, G_TYPE_INVALID) != FALSE)
496 lose ("ThrowError call unexpectedly succeeded!");
498 if (!dbus_g_error_has_name (error, "org.freedesktop.DBus.Tests.MyObject.Foo"))
499 lose ("ThrowError call returned unexpected error \"%s\": %s", dbus_g_error_get_name (error),
502 g_print ("ThrowError failed (as expected) returned error: %s\n", error->message);
503 g_clear_error (&error);
506 g_print ("Calling Uppercase\n");
507 if (!dbus_g_proxy_call (proxy, "Uppercase", &error,
508 G_TYPE_STRING, "foobar",
510 G_TYPE_STRING, &v_STRING_2,
512 lose_gerror ("Failed to complete Uppercase call", error);
513 if (strcmp ("FOOBAR", v_STRING_2) != 0)
514 lose ("Uppercase call returned unexpected string %s", v_STRING_2);
519 g_print ("Calling ManyArgs\n");
520 if (!dbus_g_proxy_call (proxy, "ManyArgs", &error,
522 G_TYPE_STRING, "bazwhee",
525 G_TYPE_DOUBLE, &v_DOUBLE_2,
526 G_TYPE_STRING, &v_STRING_2,
528 lose_gerror ("Failed to complete ManyArgs call", error);
529 if (v_DOUBLE_2 < 55 || v_DOUBLE_2 > 56)
530 lose ("ManyArgs call returned unexpected double value %f", v_DOUBLE_2);
531 if (strcmp ("BAZWHEE", v_STRING_2) != 0)
532 lose ("ManyArgs call returned unexpected string %s", v_STRING_2);
535 g_print ("Calling (wrapped) do_nothing\n");
536 if (!org_freedesktop_DBus_Tests_MyObject_do_nothing (proxy, &error))
537 lose_gerror ("Failed to complete (wrapped) DoNothing call", error);
539 g_print ("Calling (wrapped) increment\n");
540 if (!org_freedesktop_DBus_Tests_MyObject_increment (proxy, 42, &v_UINT32_2, &error))
541 lose_gerror ("Failed to complete (wrapped) Increment call", error);
543 if (v_UINT32_2 != 43)
544 lose ("(wrapped) increment call returned %d, should be 43", v_UINT32_2);
547 if (!org_freedesktop_DBus_Tests_MyObject_async_increment (proxy, 42, &v_UINT32_2, &error))
548 lose_gerror ("Failed to complete (wrapped) AsyncIncrement call", error);
550 if (v_UINT32_2 != 43)
551 lose ("(wrapped) async increment call returned %d, should be 43", v_UINT32_2);
553 g_print ("Calling (wrapped) throw_error\n");
554 if (org_freedesktop_DBus_Tests_MyObject_throw_error (proxy, &error) != FALSE)
555 lose ("(wrapped) ThrowError call unexpectedly succeeded!");
557 g_print ("(wrapped) ThrowError failed (as expected) returned error: %s\n", error->message);
558 g_clear_error (&error);
560 if (org_freedesktop_DBus_Tests_MyObject_async_throw_error (proxy, &error) != FALSE)
561 lose ("(wrapped) AsyncThrowError call unexpectedly succeeded!");
563 g_print ("(wrapped) AsyncThrowError failed (as expected) returned error: %s\n", error->message);
564 g_clear_error (&error);
566 g_print ("Calling (wrapped) uppercase\n");
567 if (!org_freedesktop_DBus_Tests_MyObject_uppercase (proxy, "foobar", &v_STRING_2, &error))
568 lose_gerror ("Failed to complete (wrapped) Uppercase call", error);
569 if (strcmp ("FOOBAR", v_STRING_2) != 0)
570 lose ("(wrapped) Uppercase call returned unexpected string %s", v_STRING_2);
573 g_print ("Calling (wrapped) many_args\n");
574 if (!org_freedesktop_DBus_Tests_MyObject_many_args (proxy, 26, "bazwhee", G_PI,
575 &v_DOUBLE_2, &v_STRING_2, &error))
576 lose_gerror ("Failed to complete (wrapped) ManyArgs call", error);
578 if (v_DOUBLE_2 < 55 || v_DOUBLE_2 > 56)
580 lose ("(wrapped) ManyArgs call returned unexpected double value %f", v_DOUBLE_2);
582 if (strcmp ("BAZWHEE", v_STRING_2) != 0)
583 lose ("(wrapped) ManyArgs call returned unexpected string %s", v_STRING_2);
594 g_print ("Calling (wrapped) many_return\n");
595 if (!org_freedesktop_DBus_Tests_MyObject_many_return (proxy, &arg0, &arg1, &arg2, &arg3, &arg4, &arg5, &error))
596 lose_gerror ("Failed to complete (wrapped) ManyReturn call", error);
599 lose ("(wrapped) ManyReturn call returned unexpected guint32 value %u", arg0);
601 if (strcmp ("42", arg1) != 0)
602 lose ("(wrapped) ManyReturn call returned unexpected string %s", arg1);
606 lose ("(wrapped) ManyReturn call returned unexpected gint32 value %u", arg2);
609 lose ("(wrapped) ManyReturn call returned unexpected guint32 value %u", arg3);
612 lose ("(wrapped) ManyReturn call returned unexpected guint32 value %u", arg4);
614 if (strcmp ("hello world", arg5))
615 lose ("(wrapped) ManyReturn call returned unexpected string %s", arg5);
622 GValue value = {0, };
624 g_value_init (&value, G_TYPE_STRING);
625 g_value_set_string (&value, "foo");
627 g_print ("Calling (wrapped) stringify, with string\n");
628 if (!org_freedesktop_DBus_Tests_MyObject_stringify (proxy,
632 lose_gerror ("Failed to complete (wrapped) stringify call", error);
633 if (strcmp ("foo", v_STRING_2) != 0)
634 lose ("(wrapped) stringify call returned unexpected string %s", v_STRING_2);
637 g_value_unset (&value);
638 g_value_init (&value, G_TYPE_INT);
639 g_value_set_int (&value, 42);
641 g_print ("Calling (wrapped) stringify, with int\n");
642 if (!org_freedesktop_DBus_Tests_MyObject_stringify (proxy,
646 lose_gerror ("Failed to complete (wrapped) stringify call 2", error);
647 if (strcmp ("42", v_STRING_2) != 0)
648 lose ("(wrapped) stringify call 2 returned unexpected string %s", v_STRING_2);
649 g_value_unset (&value);
652 g_value_init (&value, G_TYPE_INT);
653 g_value_set_int (&value, 88);
654 g_print ("Calling (wrapped) stringify, with another int\n");
655 if (!org_freedesktop_DBus_Tests_MyObject_stringify (proxy,
659 lose_gerror ("Failed to complete (wrapped) stringify call 3", error);
660 g_value_unset (&value);
662 g_print ("Calling (wrapped) unstringify, for string\n");
663 if (!org_freedesktop_DBus_Tests_MyObject_unstringify (proxy,
667 lose_gerror ("Failed to complete (wrapped) unstringify call", error);
668 if (!G_VALUE_HOLDS_STRING (&value))
669 lose ("(wrapped) unstringify call returned unexpected value type %d", (int) G_VALUE_TYPE (&value));
670 if (strcmp (g_value_get_string (&value), "foo"))
671 lose ("(wrapped) unstringify call returned unexpected string %s",
672 g_value_get_string (&value));
674 g_value_unset (&value);
676 g_print ("Calling (wrapped) unstringify, for int\n");
677 if (!org_freedesktop_DBus_Tests_MyObject_unstringify (proxy,
681 lose_gerror ("Failed to complete (wrapped) unstringify call", error);
682 if (!G_VALUE_HOLDS_INT (&value))
683 lose ("(wrapped) unstringify call returned unexpected value type %d", (int) G_VALUE_TYPE (&value));
684 if (g_value_get_int (&value) != 10)
685 lose ("(wrapped) unstringify call returned unexpected integer %d",
686 g_value_get_int (&value));
688 g_value_unset (&value);
698 array = g_array_new (FALSE, TRUE, sizeof (guint32));
700 g_array_append_val (array, val);
702 g_array_append_val (array, val);
704 g_array_append_val (array, val);
706 g_array_append_val (array, val);
708 g_array_append_val (array, val);
711 g_print ("Calling (wrapped) recursive1\n");
712 if (!org_freedesktop_DBus_Tests_MyObject_recursive1 (proxy, array,
714 lose_gerror ("Failed to complete (wrapped) recursive1 call", error);
716 lose ("(wrapped) recursive1 call returned invalid length %u", arraylen);
720 GArray *array = NULL;
723 g_print ("Calling (wrapped) recursive2\n");
724 if (!org_freedesktop_DBus_Tests_MyObject_recursive2 (proxy, 2, &array, &error))
725 lose_gerror ("Failed to complete (wrapped) Recursive2 call", error);
728 lose ("(wrapped) Recursive2 call returned NULL");
730 lose ("(wrapped) Recursive2 call returned unexpected array length %u", array->len);
732 arrayvals = (guint32*) array->data;
733 if (arrayvals[0] != 42)
734 lose ("(wrapped) Recursive2 call returned unexpected value %d in position 0", arrayvals[0]);
735 if (arrayvals[1] != 26)
736 lose ("(wrapped) Recursive2 call returned unexpected value %d in position 1", arrayvals[1]);
737 if (arrayvals[4] != 2)
738 lose ("(wrapped) Recursive2 call returned unexpected value %d in position 4", arrayvals[4]);
740 g_array_free (array, TRUE);
749 strs = g_new0 (char *, 4);
756 g_print ("Calling (wrapped) many_uppercase\n");
757 if (!org_freedesktop_DBus_Tests_MyObject_many_uppercase (proxy, strs, &strs_ret, &error))
758 lose_gerror ("Failed to complete (wrapped) ManyUppercase call", error);
759 g_assert (strs_ret != NULL);
760 if (strcmp ("HELLO", strs_ret[0]) != 0)
761 lose ("(wrapped) ManyUppercase call returned unexpected string %s", strs_ret[0]);
762 if (strcmp ("HELLO", strs_ret[1]) != 0)
763 lose ("(wrapped) ManyUppercase call returned unexpected string %s", strs_ret[1]);
764 if (strcmp ("HELLO", strs_ret[2]) != 0)
765 lose ("(wrapped) ManyUppercase call returned unexpected string %s", strs_ret[2]);
768 g_strfreev (strs_ret);
775 table = g_hash_table_new (g_str_hash, g_str_equal);
776 g_hash_table_insert (table, "moooo", "b");
777 g_hash_table_insert (table, "xxx", "cow!");
780 g_print ("Calling (wrapped) str_hash_len\n");
781 if (!org_freedesktop_DBus_Tests_MyObject_str_hash_len (proxy, table, &len, &error))
782 lose_gerror ("(wrapped) StrHashLen call failed", error);
784 lose ("(wrapped) StrHashLen returned unexpected length %u", len);
785 g_hash_table_destroy (table);
792 g_print ("Calling (wrapped) get_hash\n");
793 if (!org_freedesktop_DBus_Tests_MyObject_get_hash (proxy, &table, &error))
794 lose_gerror ("(wrapped) GetHash call failed", error);
795 val = g_hash_table_lookup (table, "foo");
796 if (val == NULL || strcmp ("bar", val))
797 lose ("(wrapped) StrHashLen returned invalid value %s for key \"foo\"",
798 val ? val : "(null)");
799 val = g_hash_table_lookup (table, "baz");
800 if (val == NULL || strcmp ("whee", val))
801 lose ("(wrapped) StrHashLen returned invalid value %s for key \"whee\"",
802 val ? val : "(null)");
803 val = g_hash_table_lookup (table, "cow");
804 if (val == NULL || strcmp ("crack", val))
805 lose ("(wrapped) StrHashLen returned invalid value %s for key \"cow\"",
806 val ? val : "(null)");
807 if (g_hash_table_size (table) != 3)
808 lose ("(wrapped) StrHashLen returned unexpected hash size %u",
809 g_hash_table_size (table));
811 g_hash_table_destroy (table);
819 DBusGProxy *ret_proxy;
821 g_print ("Calling (wrapped) objpath\n");
822 if (!dbus_g_proxy_call (proxy, "Objpath", &error,
823 DBUS_TYPE_G_PROXY, proxy, G_TYPE_INVALID,
824 DBUS_TYPE_G_PROXY, &ret_proxy, G_TYPE_INVALID))
825 lose_gerror ("Failed to complete Objpath call", error);
826 if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject2",
827 dbus_g_proxy_get_path (ret_proxy)) != 0)
828 lose ("(wrapped) objpath call returned unexpected proxy %s",
829 dbus_g_proxy_get_path (ret_proxy));
831 g_print ("Doing get/increment val tests\n");
833 if (!org_freedesktop_DBus_Tests_MyObject_get_val (ret_proxy, &val, &error))
834 lose_gerror ("Failed to complete (wrapped) GetVal call", error);
836 lose ("(wrapped) GetVal returned invalid value %d", val);
838 if (!org_freedesktop_DBus_Tests_MyObject_increment_val (ret_proxy, &error))
839 lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
841 if (!org_freedesktop_DBus_Tests_MyObject_increment_val (ret_proxy, &error))
842 lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
844 if (!org_freedesktop_DBus_Tests_MyObject_increment_val (ret_proxy, &error))
845 lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
847 if (!org_freedesktop_DBus_Tests_MyObject_get_val (ret_proxy, &val, &error))
848 lose_gerror ("Failed to complete (wrapped) GetVal call", error);
850 lose ("(wrapped) GetVal returned invalid value %d", val);
852 if (!org_freedesktop_DBus_Tests_MyObject_get_val (proxy, &val, &error))
853 lose_gerror ("Failed to complete (wrapped) GetVal call", error);
855 lose ("(wrapped) GetVal returned invalid value %d", val);
857 if (!org_freedesktop_DBus_Tests_MyObject_increment_val (proxy, &error))
858 lose_gerror ("Failed to complete (wrapped) IncrementVal call", error);
860 if (!org_freedesktop_DBus_Tests_MyObject_get_val (proxy, &val, &error))
861 lose_gerror ("Failed to complete (wrapped) GetVal call", error);
863 lose ("(wrapped) GetVal returned invalid value %d", val);
865 if (!org_freedesktop_DBus_Tests_MyObject_get_val (ret_proxy, &val, &error))
866 lose_gerror ("Failed to complete (wrapped) GetVal call", error);
868 lose ("(wrapped) GetVal returned invalid value %d", val);
870 g_object_unref (G_OBJECT (ret_proxy));
872 g_print ("Calling objpath again\n");
875 if (!dbus_g_proxy_call (proxy, "Objpath", &error,
876 DBUS_TYPE_G_OBJECT_PATH,
877 dbus_g_proxy_get_path (proxy),
879 DBUS_TYPE_G_OBJECT_PATH,
882 lose_gerror ("Failed to complete Objpath call 2", error);
883 if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject2", ret_path) != 0)
884 lose ("Objpath call 2 returned unexpected path %s",
887 ret_proxy = dbus_g_proxy_new_for_name_owner (connection,
888 "org.freedesktop.DBus.TestSuiteGLibService",
890 "org.freedesktop.DBus.Tests.FooObject",
895 if (!org_freedesktop_DBus_Tests_FooObject_get_value (ret_proxy, &val, &error))
896 lose_gerror ("Failed to complete (wrapped) GetValue call", error);
898 lose ("(wrapped) GetValue returned invalid value %d", val);
907 g_print ("Calling GetObjs\n");
909 if (!dbus_g_proxy_call (proxy, "GetObjs", &error, G_TYPE_INVALID,
910 dbus_g_type_get_collection ("GPtrArray", DBUS_TYPE_G_OBJECT_PATH),
913 lose_gerror ("Failed to complete GetObjs call", error);
915 lose ("GetObjs call returned unexpected number of objects %d, expected 2",
918 if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject",
919 g_ptr_array_index (objs, 0)) != 0)
920 lose ("GetObjs call returned unexpected path \"%s\" in position 0; expected /org/freedesktop/DBus/Tests/MyTestObject", (char*) g_ptr_array_index (objs, 0));
922 if (strcmp ("/org/freedesktop/DBus/Tests/MyTestObject2",
923 g_ptr_array_index (objs, 1)) != 0)
924 lose ("GetObjs call returned unexpected path \"%s\" in position 1; expected /org/freedesktop/DBus/Tests/MyTestObject2", (char*) g_ptr_array_index (objs, 1));
926 for (i = 0; i < objs->len; i++)
927 g_free (g_ptr_array_index (objs, i));
928 g_ptr_array_free (objs, TRUE);
931 /* Signal handling tests */
933 g_print ("Testing signal handling\n");
934 dbus_g_proxy_add_signal (proxy, "Frobnicate", G_TYPE_INT, G_TYPE_INVALID);
936 dbus_g_proxy_connect_signal (proxy, "Frobnicate",
937 G_CALLBACK (frobnicate_signal_handler),
940 g_print ("Calling EmitFrobnicate\n");
941 if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
942 G_TYPE_INVALID, G_TYPE_INVALID))
943 lose_gerror ("Failed to complete EmitFrobnicate call", error);
946 dbus_g_connection_flush (connection);
947 exit_timeout = g_timeout_add (5000, timed_exit, loop);
948 g_main_loop_run (loop);
950 if (n_times_frobnicate_received != 1)
951 lose ("Frobnicate signal received %d times, should have been 1", n_times_frobnicate_received);
953 g_print ("Calling EmitFrobnicate again\n");
954 if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
955 G_TYPE_INVALID, G_TYPE_INVALID))
956 lose_gerror ("Failed to complete EmitFrobnicate call", error);
958 dbus_g_connection_flush (connection);
959 exit_timeout = g_timeout_add (5000, timed_exit, loop);
960 g_main_loop_run (loop);
962 if (n_times_frobnicate_received != 2)
963 lose ("Frobnicate signal received %d times, should have been 2", n_times_frobnicate_received);
965 g_object_unref (G_OBJECT (proxy));
969 g_print ("Creating proxy for FooObject interface\n");
970 proxy = dbus_g_proxy_new_for_name_owner (connection,
971 "org.freedesktop.DBus.TestSuiteGLibService",
972 "/org/freedesktop/DBus/Tests/MyTestObject",
973 "org.freedesktop.DBus.Tests.FooObject",
977 lose_gerror ("Failed to create proxy for name owner", error);
979 dbus_g_object_register_marshaller (my_object_marshal_VOID__STRING_INT_STRING,
980 G_TYPE_NONE, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INVALID);
982 dbus_g_object_register_marshaller (my_object_marshal_VOID__STRING_BOXED,
983 G_TYPE_NONE, G_TYPE_STRING, G_TYPE_VALUE, G_TYPE_INVALID);
985 dbus_g_proxy_add_signal (proxy, "Sig0", G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_INVALID);
986 dbus_g_proxy_add_signal (proxy, "Sig1", G_TYPE_STRING, G_TYPE_VALUE, G_TYPE_INVALID);
987 dbus_g_proxy_add_signal (proxy, "Sig2", DBUS_TYPE_G_STRING_STRING_HASHTABLE, G_TYPE_INVALID);
989 dbus_g_proxy_connect_signal (proxy, "Sig0",
990 G_CALLBACK (sig0_signal_handler),
992 dbus_g_proxy_connect_signal (proxy, "Sig1",
993 G_CALLBACK (sig1_signal_handler),
995 dbus_g_proxy_connect_signal (proxy, "Sig2",
996 G_CALLBACK (sig2_signal_handler),
999 g_print ("Calling FooObject EmitSignals\n");
1000 dbus_g_proxy_call_no_reply (proxy, "EmitSignals", G_TYPE_INVALID);
1002 dbus_g_connection_flush (connection);
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 != 1)
1009 lose ("Sig0 signal received %d times, should have been 1", n_times_sig0_received);
1010 if (n_times_sig1_received != 1)
1011 lose ("Sig1 signal received %d times, should have been 1", n_times_sig1_received);
1013 g_print ("Calling FooObject EmitSignals and EmitSignal2\n");
1014 dbus_g_proxy_call_no_reply (proxy, "EmitSignal2", G_TYPE_INVALID);
1015 dbus_g_connection_flush (connection);
1017 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1018 g_main_loop_run (loop);
1020 if (n_times_sig2_received != 1)
1021 lose ("Sig2 signal received %d times, should have been 1", n_times_sig2_received);
1023 g_print ("Calling FooObject EmitSignals two more times\n");
1024 dbus_g_proxy_call_no_reply (proxy, "EmitSignals", G_TYPE_INVALID);
1025 dbus_g_proxy_call_no_reply (proxy, "EmitSignals", G_TYPE_INVALID);
1027 dbus_g_connection_flush (connection);
1028 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1029 g_main_loop_run (loop);
1030 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1031 g_main_loop_run (loop);
1032 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1033 g_main_loop_run (loop);
1034 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1035 g_main_loop_run (loop);
1037 if (n_times_sig0_received != 3)
1038 lose ("Sig0 signal received %d times, should have been 3", n_times_sig0_received);
1039 if (n_times_sig1_received != 3)
1040 lose ("Sig1 signal received %d times, should have been 3", n_times_sig1_received);
1042 /* Terminate again */
1043 g_print ("Terminating service\n");
1044 await_terminating_service = "org.freedesktop.DBus.TestSuiteGLibService";
1045 dbus_g_proxy_call_no_reply (proxy, "Terminate", G_TYPE_INVALID);
1047 proxy_destroyed = FALSE;
1048 proxy_destroy_and_nameowner = TRUE;
1049 proxy_destroy_and_nameowner_complete = FALSE;
1051 g_signal_connect (G_OBJECT (proxy),
1053 G_CALLBACK (proxy_destroyed_cb),
1056 dbus_g_connection_flush (connection);
1057 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1058 g_main_loop_run (loop);
1060 if (await_terminating_service != NULL)
1061 lose ("Didn't see name loss for \"org.freedesktop.DBus.TestSuiteGLibService\"");
1062 if (!proxy_destroyed)
1063 lose ("Didn't get proxy_destroyed");
1064 g_print ("Proxy destroyed successfully\n");
1066 /* Don't need to unref, proxy was destroyed */
1070 /* Create a new proxy for the name; should not be associated */
1071 proxy = dbus_g_proxy_new_for_name (connection,
1072 "org.freedesktop.DBus.TestSuiteGLibService",
1073 "/org/freedesktop/DBus/Tests/MyTestObject",
1074 "org.freedesktop.DBus.Tests.MyObject");
1075 g_assert (proxy != NULL);
1077 proxy_destroyed = FALSE;
1078 proxy_destroy_and_nameowner = FALSE;
1079 proxy_destroy_and_nameowner_complete = FALSE;
1081 g_signal_connect (G_OBJECT (proxy),
1083 G_CALLBACK (proxy_destroyed_cb),
1086 if (!dbus_g_proxy_call (driver, "GetNameOwner", &error,
1088 "org.freedesktop.DBus.TestSuiteGLibService",
1093 if (error->domain == DBUS_GERROR && error->code == DBUS_GERROR_NAME_HAS_NO_OWNER)
1094 g_print ("Got expected error \"org.freedesktop.DBus.Error.NameHasNoOwner\"\n");
1096 lose_gerror ("Unexpected error from GetNameOwner", error);
1098 lose ("GetNameOwner unexpectedly succeeded!");
1099 g_clear_error (&error);
1101 /* This will have the side-effect of activating the service, thus
1102 * causing a NameOwnerChanged, which should let our name proxy
1105 g_print ("Calling Uppercase for name proxy\n");
1106 if (!dbus_g_proxy_call (proxy, "Uppercase", &error,
1107 G_TYPE_STRING, "bazwhee",
1109 G_TYPE_STRING, &v_STRING_2,
1111 lose_gerror ("Failed to complete Uppercase call", error);
1112 g_free (v_STRING_2);
1114 dbus_g_proxy_add_signal (proxy, "Frobnicate", G_TYPE_INT, G_TYPE_INVALID);
1116 dbus_g_proxy_connect_signal (proxy, "Frobnicate",
1117 G_CALLBACK (frobnicate_signal_handler),
1120 g_print ("Calling EmitFrobnicate\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 n_times_frobnicate_received = 0;
1127 dbus_g_connection_flush (connection);
1128 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1129 g_main_loop_run (loop);
1131 if (n_times_frobnicate_received != 1)
1132 lose ("Frobnicate signal received %d times, should have been 1", n_times_frobnicate_received);
1134 /* Now terminate the service, then start it again (implicitly) and wait for signals */
1135 g_print ("Terminating service (2)\n");
1136 await_terminating_service = "org.freedesktop.DBus.TestSuiteGLibService";
1137 dbus_g_proxy_call_no_reply (proxy, "Terminate", G_TYPE_INVALID);
1138 dbus_g_connection_flush (connection);
1139 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1140 g_main_loop_run (loop);
1141 if (await_terminating_service != NULL)
1142 lose ("Didn't see name loss for \"org.freedesktop.DBus.TestSuiteGLibService\"");
1144 if (proxy_destroyed)
1145 lose ("Unexpectedly got proxy_destroyed!");
1147 n_times_frobnicate_received = 0;
1149 g_print ("Calling EmitFrobnicate (2)\n");
1150 if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
1151 G_TYPE_INVALID, G_TYPE_INVALID))
1152 lose_gerror ("Failed to complete EmitFrobnicate call", error);
1154 dbus_g_connection_flush (connection);
1155 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1156 g_main_loop_run (loop);
1158 if (n_times_frobnicate_received != 1)
1159 lose ("Frobnicate signal received %d times, should have been 1", n_times_frobnicate_received);
1161 if (proxy_destroyed)
1162 lose ("Unexpectedly got proxy_destroyed!");
1164 /* Create another proxy for the name; should be associated immediately */
1165 proxy2 = dbus_g_proxy_new_for_name (connection,
1166 "org.freedesktop.DBus.TestSuiteGLibService",
1167 "/org/freedesktop/DBus/Tests/MyTestObject",
1168 "org.freedesktop.DBus.Tests.MyObject");
1169 g_assert (proxy2 != NULL);
1171 dbus_g_proxy_add_signal (proxy2, "Frobnicate", G_TYPE_INT, G_TYPE_INVALID);
1173 dbus_g_proxy_connect_signal (proxy2, "Frobnicate",
1174 G_CALLBACK (frobnicate_signal_handler_2),
1177 g_print ("Calling EmitFrobnicate (3)\n");
1178 if (!dbus_g_proxy_call (proxy, "EmitFrobnicate", &error,
1179 G_TYPE_INVALID, G_TYPE_INVALID))
1180 lose_gerror ("Failed to complete EmitFrobnicate call", error);
1182 dbus_g_connection_flush (connection);
1183 exit_timeout = g_timeout_add (5000, timed_exit, loop);
1184 g_main_loop_run (loop);
1186 if (n_times_frobnicate_received != 2)
1187 lose ("Frobnicate signal received %d times for 1st proxy, should have been 2", n_times_frobnicate_received);
1188 if (n_times_frobnicate_received_2 != 1)
1189 lose ("Frobnicate signal received %d times for 2nd proxy, should have been 1", n_times_frobnicate_received_2);
1191 g_object_unref (G_OBJECT (proxy));
1192 g_object_unref (G_OBJECT (proxy2));
1196 /* Test introspection */
1197 proxy = dbus_g_proxy_new_for_name_owner (connection,
1198 "org.freedesktop.DBus.TestSuiteGLibService",
1199 "/org/freedesktop/DBus/Tests/MyTestObject",
1200 "org.freedesktop.DBus.Introspectable",
1203 lose_gerror ("Failed to create proxy for name owner", error);
1205 g_print ("Testing introspect\n");
1206 if (!dbus_g_proxy_call (proxy, "Introspect", &error,
1208 G_TYPE_STRING, &v_STRING_2,
1210 lose_gerror ("Failed to complete Introspect call", error);
1212 /* Could just do strcmp(), but that seems more fragile */
1216 gboolean found_introspectable;
1217 gboolean found_properties;
1218 gboolean found_myobject;
1219 gboolean found_fooobject;
1221 node = description_load_from_string (v_STRING_2, strlen (v_STRING_2), &error);
1223 lose_gerror ("Failed to parse introspection data: %s", error);
1225 found_introspectable = FALSE;
1226 found_properties = FALSE;
1227 found_myobject = FALSE;
1228 found_fooobject = FALSE;
1229 for (elt = node_info_get_interfaces (node); elt ; elt = elt->next)
1231 InterfaceInfo *iface = elt->data;
1233 if (!found_introspectable && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Introspectable") == 0)
1234 found_introspectable = TRUE;
1235 else if (!found_properties && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Properties") == 0)
1236 found_properties = TRUE;
1237 else if (!found_myobject && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Tests.MyObject") == 0)
1240 gboolean found_manyargs;
1242 found_myobject = TRUE;
1244 found_manyargs = FALSE;
1245 for (elt = interface_info_get_methods (iface); elt; elt = elt->next)
1250 if (strcmp (method_info_get_name (method), "ManyArgs") == 0)
1252 found_manyargs = TRUE;
1256 if (!found_manyargs)
1257 lose ("Missing method org.freedesktop.DBus.Tests.MyObject.ManyArgs");
1259 else if (!found_fooobject && strcmp (interface_info_get_name (iface), "org.freedesktop.DBus.Tests.FooObject") == 0)
1260 found_fooobject = TRUE;
1262 lose ("Unexpected or duplicate interface %s", interface_info_get_name (iface));
1265 if (!(found_introspectable && found_myobject && found_properties))
1266 lose ("Missing interface");
1268 g_free (v_STRING_2);
1270 g_object_unref (G_OBJECT (driver));
1272 g_print ("Successfully completed %s\n", argv[0]);