1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
30 #include <sys/types.h>
36 #include <glib/gstdio.h>
38 #include <gio/gnetworking.h>
39 #include <gio/gunixsocketaddress.h>
40 #include <gio/gunixfdlist.h>
42 /* used in test_overflow */
44 #include <gio/gunixconnection.h>
48 #if (defined(__linux__) || \
49 defined(__FreeBSD__) || \
50 defined(__FreeBSD_kernel__) || \
52 #define SHOULD_HAVE_CREDENTIALS_PASSING
55 #include "gdbus-tests.h"
57 #include "gdbus-example-objectmanager-generated.h"
60 static gboolean is_unix = TRUE;
62 static gboolean is_unix = FALSE;
65 static gchar *tmp_address = NULL;
66 static gchar *test_guid = NULL;
67 static GMutex service_loop_lock;
68 static GCond service_loop_cond;
69 static GMainLoop *service_loop = NULL;
70 static GDBusServer *server = NULL;
71 static GMainLoop *loop = NULL;
73 /* ---------------------------------------------------------------------------------------------------- */
74 /* Test that peer-to-peer connections work */
75 /* ---------------------------------------------------------------------------------------------------- */
80 gboolean accept_connection;
81 gint num_connection_attempts;
82 GPtrArray *current_connections;
83 guint num_method_calls;
84 gboolean signal_received;
87 static const gchar *test_interface_introspection_xml =
89 " <interface name='org.gtk.GDBus.PeerTestInterface'>"
90 " <method name='HelloPeer'>"
91 " <arg type='s' name='greeting' direction='in'/>"
92 " <arg type='s' name='response' direction='out'/>"
94 " <method name='EmitSignal'/>"
95 " <method name='EmitSignalWithNameSet'/>"
96 " <method name='OpenFile'>"
97 " <arg type='s' name='path' direction='in'/>"
99 " <signal name='PeerSignal'>"
100 " <arg type='s' name='a_string'/>"
102 " <property type='s' name='PeerProperty' access='read'/>"
105 static GDBusInterfaceInfo *test_interface_introspection_data = NULL;
108 test_interface_method_call (GDBusConnection *connection,
110 const gchar *object_path,
111 const gchar *interface_name,
112 const gchar *method_name,
113 GVariant *parameters,
114 GDBusMethodInvocation *invocation,
117 PeerData *data = user_data;
118 const GDBusMethodInfo *info;
120 data->num_method_calls++;
122 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
123 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
125 info = g_dbus_method_invocation_get_method_info (invocation);
126 g_assert_cmpstr (info->name, ==, method_name);
128 if (g_strcmp0 (method_name, "HelloPeer") == 0)
130 const gchar *greeting;
133 g_variant_get (parameters, "(&s)", &greeting);
135 response = g_strdup_printf ("You greeted me with '%s'.",
137 g_dbus_method_invocation_return_value (invocation,
138 g_variant_new ("(s)", response));
141 else if (g_strcmp0 (method_name, "EmitSignal") == 0)
146 g_dbus_connection_emit_signal (connection,
148 "/org/gtk/GDBus/PeerTestObject",
149 "org.gtk.GDBus.PeerTestInterface",
153 g_assert_no_error (error);
154 g_dbus_method_invocation_return_value (invocation, NULL);
156 else if (g_strcmp0 (method_name, "EmitSignalWithNameSet") == 0)
160 GDBusMessage *message;
162 message = g_dbus_message_new_signal ("/org/gtk/GDBus/PeerTestObject",
163 "org.gtk.GDBus.PeerTestInterface",
164 "PeerSignalWithNameSet");
165 g_dbus_message_set_sender (message, ":1.42");
168 ret = g_dbus_connection_send_message (connection, message, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error);
169 g_assert_no_error (error);
171 g_object_unref (message);
173 g_dbus_method_invocation_return_value (invocation, NULL);
175 else if (g_strcmp0 (method_name, "OpenFile") == 0)
182 GUnixFDList *fd_list;
184 g_variant_get (parameters, "(&s)", &path);
186 fd_list = g_unix_fd_list_new ();
190 fd = g_open (path, O_RDONLY, 0);
192 g_unix_fd_list_append (fd_list, fd, &error);
193 g_assert_no_error (error);
196 reply = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
197 g_dbus_message_set_unix_fd_list (reply, fd_list);
198 g_object_unref (fd_list);
199 g_object_unref (invocation);
202 g_dbus_connection_send_message (connection,
204 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
205 NULL, /* out_serial */
207 g_assert_no_error (error);
208 g_object_unref (reply);
210 g_dbus_method_invocation_return_dbus_error (invocation,
211 "org.gtk.GDBus.NotOnUnix",
212 "Your OS does not support file descriptor passing");
217 g_assert_not_reached ();
222 test_interface_get_property (GDBusConnection *connection,
224 const gchar *object_path,
225 const gchar *interface_name,
226 const gchar *property_name,
230 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
231 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
232 g_assert_cmpstr (property_name, ==, "PeerProperty");
234 return g_variant_new_string ("ThePropertyValue");
238 static const GDBusInterfaceVTable test_interface_vtable =
240 test_interface_method_call,
241 test_interface_get_property,
242 NULL /* set_property */
246 on_proxy_signal_received (GDBusProxy *proxy,
249 GVariant *parameters,
252 PeerData *data = user_data;
254 data->signal_received = TRUE;
256 g_assert (sender_name == NULL);
257 g_assert_cmpstr (signal_name, ==, "PeerSignal");
258 g_main_loop_quit (loop);
262 on_proxy_signal_received_with_name_set (GDBusProxy *proxy,
265 GVariant *parameters,
268 PeerData *data = user_data;
270 data->signal_received = TRUE;
272 g_assert_cmpstr (sender_name, ==, ":1.42");
273 g_assert_cmpstr (signal_name, ==, "PeerSignalWithNameSet");
274 g_main_loop_quit (loop);
277 /* ---------------------------------------------------------------------------------------------------- */
280 on_authorize_authenticated_peer (GDBusAuthObserver *observer,
282 GCredentials *credentials,
285 PeerData *data = user_data;
288 data->num_connection_attempts++;
291 if (!data->accept_connection)
294 g_main_loop_quit (loop);
300 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
302 on_new_connection (GDBusServer *server,
303 GDBusConnection *connection,
306 PeerData *data = user_data;
310 //g_print ("Client connected.\n"
311 // "Negotiated capabilities: unix-fd-passing=%d\n",
312 // g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
314 g_ptr_array_add (data->current_connections, g_object_ref (connection));
316 #ifdef SHOULD_HAVE_CREDENTIALS_PASSING
318 GCredentials *credentials;
320 credentials = g_dbus_connection_get_peer_credentials (connection);
322 g_assert (credentials != NULL);
323 g_assert_cmpuint (g_credentials_get_unix_user (credentials, NULL), ==,
325 g_assert_cmpuint (g_credentials_get_unix_pid (credentials, NULL), ==,
330 /* export object on the newly established connection */
332 reg_id = g_dbus_connection_register_object (connection,
333 "/org/gtk/GDBus/PeerTestObject",
334 test_interface_introspection_data,
335 &test_interface_vtable,
337 NULL, /* GDestroyNotify for data */
339 g_assert_no_error (error);
340 g_assert (reg_id > 0);
342 g_main_loop_quit (loop);
348 create_service_loop (GMainContext *service_context)
350 g_assert (service_loop == NULL);
351 g_mutex_lock (&service_loop_lock);
352 service_loop = g_main_loop_new (service_context, FALSE);
353 g_cond_broadcast (&service_loop_cond);
354 g_mutex_unlock (&service_loop_lock);
358 teardown_service_loop (void)
360 g_mutex_lock (&service_loop_lock);
361 g_clear_pointer (&service_loop, g_main_loop_unref);
362 g_mutex_unlock (&service_loop_lock);
366 await_service_loop (void)
368 g_mutex_lock (&service_loop_lock);
369 while (service_loop == NULL)
370 g_cond_wait (&service_loop_cond, &service_loop_lock);
371 g_mutex_unlock (&service_loop_lock);
375 service_thread_func (gpointer user_data)
377 PeerData *data = user_data;
378 GMainContext *service_context;
379 GDBusAuthObserver *observer, *o;
385 service_context = g_main_context_new ();
386 g_main_context_push_thread_default (service_context);
389 observer = g_dbus_auth_observer_new ();
390 server = g_dbus_server_new_sync (tmp_address,
391 G_DBUS_SERVER_FLAGS_NONE,
394 NULL, /* cancellable */
396 g_assert_no_error (error);
398 g_signal_connect (server,
400 G_CALLBACK (on_new_connection),
402 g_signal_connect (observer,
403 "authorize-authenticated-peer",
404 G_CALLBACK (on_authorize_authenticated_peer),
407 g_assert_cmpint (g_dbus_server_get_flags (server), ==, G_DBUS_SERVER_FLAGS_NONE);
408 g_assert_cmpstr (g_dbus_server_get_guid (server), ==, test_guid);
409 g_object_get (server,
414 "authentication-observer", &o,
416 g_assert_cmpint (f, ==, G_DBUS_SERVER_FLAGS_NONE);
417 g_assert_cmpstr (a, ==, tmp_address);
418 g_assert_cmpstr (g, ==, test_guid);
420 g_assert (o == observer);
425 g_object_unref (observer);
427 g_dbus_server_start (server);
429 create_service_loop (service_context);
430 g_main_loop_run (service_loop);
432 g_main_context_pop_thread_default (service_context);
434 teardown_service_loop ();
435 g_main_context_unref (service_context);
437 /* test code specifically unrefs the server - see below */
438 g_assert (server == NULL);
445 on_incoming_connection (GSocketService *service,
446 GSocketConnection *socket_connection,
447 GObject *source_object,
450 PeerData *data = user_data;
452 if (data->accept_connection)
456 GDBusConnection *connection;
459 connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
461 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
462 NULL, /* cancellable */
464 g_assert_no_error (error);
466 g_ptr_array_add (data->current_connections, connection);
468 /* export object on the newly established connection */
470 reg_id = g_dbus_connection_register_object (connection,
471 "/org/gtk/GDBus/PeerTestObject",
472 &test_interface_introspection_data,
473 &test_interface_vtable,
475 NULL, /* GDestroyNotify for data */
477 g_assert_no_error (error);
478 g_assert (reg_id > 0);
483 /* don't do anything */
486 data->num_connection_attempts++;
488 g_main_loop_quit (loop);
490 /* stops other signal handlers from being invoked */
495 service_thread_func (gpointer data)
497 GMainContext *service_context;
499 GSocketAddress *address;
502 service_context = g_main_context_new ();
503 g_main_context_push_thread_default (service_context);
505 socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
506 address = g_unix_socket_address_new (socket_path);
508 service = g_socket_service_new ();
510 g_socket_listener_add_address (G_SOCKET_LISTENER (service),
512 G_SOCKET_TYPE_STREAM,
513 G_SOCKET_PROTOCOL_DEFAULT,
514 NULL, /* source_object */
515 NULL, /* effective_address */
517 g_assert_no_error (error);
518 g_signal_connect (service,
520 G_CALLBACK (on_incoming_connection),
522 g_socket_service_start (service);
524 create_service_loop (service_context);
525 g_main_loop_run (service_loop);
527 g_main_context_pop_thread_default (service_context);
529 teardown_service_loop ();
530 g_main_context_unref (service_context);
532 g_object_unref (address);
533 g_free (socket_path);
538 /* ---------------------------------------------------------------------------------------------------- */
542 check_connection (gpointer user_data)
544 PeerData *data = user_data;
547 for (n = 0; n < data->current_connections->len; n++)
552 c = G_DBUS_CONNECTION (data->current_connections->pdata[n]);
553 stream = g_dbus_connection_get_stream (c);
555 g_debug ("In check_connection for %d: connection %p, stream %p", n, c, stream);
556 g_debug ("closed = %d", g_io_stream_is_closed (stream));
559 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
560 g_debug ("socket_closed = %d", g_socket_is_closed (socket));
561 g_debug ("socket_condition_check = %d", g_socket_condition_check (socket, G_IO_IN|G_IO_OUT|G_IO_ERR|G_IO_HUP));
567 num_read = g_input_stream_read (g_io_stream_get_input_stream (stream),
574 g_debug ("error: %s", error->message);
575 g_error_free (error);
579 g_debug ("no error, read %d bytes", (gint) num_read);
587 on_do_disconnect_in_idle (gpointer data)
589 GDBusConnection *c = G_DBUS_CONNECTION (data);
590 g_debug ("GDC %p has ref_count %d", c, G_OBJECT (c)->ref_count);
591 g_dbus_connection_disconnect (c);
599 read_all_from_fd (gint fd, gsize *out_len, GError **error)
605 str = g_string_new (NULL);
609 num_read = read (fd, buf, sizeof (buf));
612 if (errno == EAGAIN || errno == EWOULDBLOCK)
616 g_io_error_from_errno (errno),
617 "Failed reading %d bytes into offset %d: %s",
623 else if (num_read > 0)
625 g_string_append_len (str, buf, num_read);
627 else if (num_read == 0)
636 return g_string_free (str, FALSE);
641 g_string_free (str, TRUE);
657 GThread *service_thread;
658 gulong signal_handler_id;
660 memset (&data, '\0', sizeof (PeerData));
661 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
663 /* first try to connect when there is no server */
665 c = g_dbus_connection_new_for_address_sync (is_unix ? "unix:path=/tmp/gdbus-test-does-not-exist-pid" :
666 /* NOTE: Even if something is listening on port 12345 the connection
667 * will fail because the nonce file doesn't exist */
668 "nonce-tcp:host=localhost,port=12345,noncefile=this-does-not-exist-gdbus",
669 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
670 NULL, /* GDBusAuthObserver */
671 NULL, /* cancellable */
673 _g_assert_error_domain (error, G_IO_ERROR);
674 g_assert (!g_dbus_error_is_remote_error (error));
675 g_clear_error (&error);
676 g_assert (c == NULL);
678 /* bring up a server - we run the server in a different thread to avoid deadlocks */
679 service_thread = g_thread_new ("test_peer",
682 await_service_loop ();
683 g_assert (server != NULL);
685 /* bring up a connection and accept it */
686 data.accept_connection = TRUE;
688 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
689 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
690 NULL, /* GDBusAuthObserver */
691 NULL, /* cancellable */
693 g_assert_no_error (error);
694 g_assert (c != NULL);
695 while (data.current_connections->len < 1)
696 g_main_loop_run (loop);
697 g_assert_cmpint (data.current_connections->len, ==, 1);
698 g_assert_cmpint (data.num_connection_attempts, ==, 1);
699 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
700 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
702 /* check that we create a proxy, read properties, receive signals and invoke
703 * the HelloPeer() method. Since the server runs in another thread it's fine
704 * to use synchronous blocking API here.
707 proxy = g_dbus_proxy_new_sync (c,
708 G_DBUS_PROXY_FLAGS_NONE,
711 "/org/gtk/GDBus/PeerTestObject",
712 "org.gtk.GDBus.PeerTestInterface",
713 NULL, /* GCancellable */
715 g_assert_no_error (error);
716 g_assert (proxy != NULL);
718 value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
719 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
721 /* try invoking a method */
723 result = g_dbus_proxy_call_sync (proxy,
725 g_variant_new ("(s)", "Hey Peer!"),
726 G_DBUS_CALL_FLAGS_NONE,
728 NULL, /* GCancellable */
730 g_assert_no_error (error);
731 g_variant_get (result, "(&s)", &s);
732 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
733 g_variant_unref (result);
734 g_assert_cmpint (data.num_method_calls, ==, 1);
736 /* make the other peer emit a signal - catch it */
737 signal_handler_id = g_signal_connect (proxy,
739 G_CALLBACK (on_proxy_signal_received),
741 g_assert (!data.signal_received);
742 g_dbus_proxy_call (proxy,
744 NULL, /* no arguments */
745 G_DBUS_CALL_FLAGS_NONE,
747 NULL, /* GCancellable */
748 NULL, /* GAsyncReadyCallback - we don't care about the result */
749 NULL); /* user_data */
750 g_main_loop_run (loop);
751 g_assert (data.signal_received);
752 g_assert_cmpint (data.num_method_calls, ==, 2);
753 g_signal_handler_disconnect (proxy, signal_handler_id);
755 /* Also ensure that messages with the sender header-field set gets
756 * delivered to the proxy - note that this doesn't really make sense
757 * e.g. names are meaning-less in a peer-to-peer case... but we
758 * support it because it makes sense in certain bridging
759 * applications - see e.g. #623815.
761 signal_handler_id = g_signal_connect (proxy,
763 G_CALLBACK (on_proxy_signal_received_with_name_set),
765 data.signal_received = FALSE;
766 g_dbus_proxy_call (proxy,
767 "EmitSignalWithNameSet",
768 NULL, /* no arguments */
769 G_DBUS_CALL_FLAGS_NONE,
771 NULL, /* GCancellable */
772 NULL, /* GAsyncReadyCallback - we don't care about the result */
773 NULL); /* user_data */
774 g_main_loop_run (loop);
775 g_assert (data.signal_received);
776 g_assert_cmpint (data.num_method_calls, ==, 3);
777 g_signal_handler_disconnect (proxy, signal_handler_id);
779 /* check for UNIX fd passing */
782 GDBusMessage *method_call_message;
783 GDBusMessage *method_reply_message;
784 GUnixFDList *fd_list;
790 const char *testfile = g_test_get_filename (G_TEST_DIST, "file.c", NULL);
792 method_call_message = g_dbus_message_new_method_call (NULL, /* name */
793 "/org/gtk/GDBus/PeerTestObject",
794 "org.gtk.GDBus.PeerTestInterface",
796 g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", testfile));
798 method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
800 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
802 NULL, /* out_serial */
803 NULL, /* cancellable */
805 g_assert_no_error (error);
806 g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
807 fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
808 g_assert (fd_list != NULL);
809 g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
811 fd = g_unix_fd_list_get (fd_list, 0, &error);
812 g_assert_no_error (error);
813 g_object_unref (method_call_message);
814 g_object_unref (method_reply_message);
818 buf = read_all_from_fd (fd, &len, &error);
819 g_assert_no_error (error);
820 g_assert (buf != NULL);
824 g_file_get_contents (testfile,
828 g_assert_no_error (error);
829 g_assert_cmpint (len, ==, len2);
830 g_assert (memcmp (buf, buf2, len) == 0);
836 result = g_dbus_proxy_call_sync (proxy,
838 g_variant_new ("(s)", "boo"),
839 G_DBUS_CALL_FLAGS_NONE,
841 NULL, /* GCancellable */
843 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
844 g_assert (result == NULL);
845 g_error_free (error);
846 #endif /* G_OS_UNIX */
848 /* Check that g_socket_get_credentials() work - this really should
849 * be in a GSocket-specific test suite but no such test suite exists
854 GCredentials *credentials;
855 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (g_dbus_connection_get_stream (c)));
856 g_assert (G_IS_SOCKET (socket));
858 credentials = g_socket_get_credentials (socket, &error);
861 struct ucred *native_creds;
862 g_assert_no_error (error);
863 g_assert (G_IS_CREDENTIALS (credentials));
864 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED);
865 g_assert (native_creds != NULL);
866 g_assert (native_creds->uid == getuid ());
867 g_assert (native_creds->gid == getgid ());
868 g_assert (native_creds->pid == getpid ());
870 g_object_unref (credentials);
871 #elif defined (__OpenBSD__)
873 struct sockpeercred *native_creds;
874 g_assert_no_error (error);
875 g_assert (G_IS_CREDENTIALS (credentials));
876 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED);
877 g_assert (native_creds != NULL);
878 g_assert (native_creds->uid == getuid ());
879 g_assert (native_creds->gid == getgid ());
880 g_assert (native_creds->pid == getpid ());
882 g_object_unref (credentials);
884 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
885 g_assert (credentials == NULL);
890 /* bring up a connection - don't accept it - this should fail
892 data.accept_connection = FALSE;
894 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
895 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
896 NULL, /* GDBusAuthObserver */
897 NULL, /* cancellable */
899 _g_assert_error_domain (error, G_IO_ERROR);
900 g_error_free (error);
901 g_assert (c2 == NULL);
904 /* TODO: THIS TEST DOESN'T WORK YET */
906 /* bring up a connection - accept it.. then disconnect from the client side - check
907 * that the server side gets the disconnect signal.
910 data.accept_connection = TRUE;
911 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
912 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
913 NULL, /* GDBusAuthObserver */
914 NULL, /* cancellable */
916 g_assert_no_error (error);
917 g_assert (c2 != NULL);
918 g_assert (!g_dbus_connection_get_is_disconnected (c2));
919 while (data.num_connection_attempts < 3)
920 g_main_loop_run (loop);
921 g_assert_cmpint (data.current_connections->len, ==, 2);
922 g_assert_cmpint (data.num_connection_attempts, ==, 3);
923 g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
924 g_idle_add (on_do_disconnect_in_idle, c2);
925 g_debug ("==================================================");
926 g_debug ("==================================================");
927 g_debug ("==================================================");
928 g_debug ("waiting for disconnect on connection %p, stream %p",
929 data.current_connections->pdata[1],
930 g_dbus_connection_get_stream (data.current_connections->pdata[1]));
932 g_timeout_add (2000, check_connection, &data);
933 //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
934 g_main_loop_run (loop);
935 g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
936 g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
939 /* unref the server and stop listening for new connections
941 * This won't bring down the established connections - check that c is still connected
942 * by invoking a method
944 //g_socket_service_stop (service);
945 //g_object_unref (service);
946 g_dbus_server_stop (server);
947 g_object_unref (server);
951 result = g_dbus_proxy_call_sync (proxy,
953 g_variant_new ("(s)", "Hey Again Peer!"),
954 G_DBUS_CALL_FLAGS_NONE,
956 NULL, /* GCancellable */
958 g_assert_no_error (error);
959 g_variant_get (result, "(&s)", &s);
960 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
961 g_variant_unref (result);
962 g_assert_cmpint (data.num_method_calls, ==, 5);
965 /* TODO: THIS TEST DOESN'T WORK YET */
967 /* now disconnect from the server side - check that the client side gets the signal */
968 g_assert_cmpint (data.current_connections->len, ==, 1);
969 g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
970 g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
971 if (!g_dbus_connection_get_is_disconnected (c))
972 _g_assert_signal_received (c, "closed");
973 g_assert (g_dbus_connection_get_is_disconnected (c));
977 g_ptr_array_unref (data.current_connections);
978 g_object_unref (proxy);
980 g_main_loop_quit (service_loop);
981 g_thread_join (service_thread);
984 /* ---------------------------------------------------------------------------------------------------- */
989 GMainContext *context;
996 dmp_data_free (DmpData *data)
998 g_main_loop_unref (data->loop);
999 g_main_context_unref (data->context);
1000 g_object_unref (data->server);
1001 g_list_free_full (data->connections, g_object_unref);
1006 dmp_on_method_call (GDBusConnection *connection,
1007 const gchar *sender,
1008 const gchar *object_path,
1009 const gchar *interface_name,
1010 const gchar *method_name,
1011 GVariant *parameters,
1012 GDBusMethodInvocation *invocation,
1015 //DmpData *data = user_data;
1018 g_variant_get (parameters,
1022 g_dbus_method_invocation_return_value (invocation,
1023 g_variant_new ("(i)", first + second));
1026 static const GDBusInterfaceVTable dmp_interface_vtable =
1029 NULL, /* get_property */
1030 NULL /* set_property */
1034 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1036 dmp_on_new_connection (GDBusServer *server,
1037 GDBusConnection *connection,
1040 DmpData *data = user_data;
1041 GDBusNodeInfo *node;
1044 /* accept the connection */
1045 data->connections = g_list_prepend (data->connections, g_object_ref (connection));
1048 node = g_dbus_node_info_new_for_xml ("<node>"
1049 " <interface name='org.gtk.GDBus.DmpInterface'>"
1050 " <method name='AddPair'>"
1051 " <arg type='i' name='first' direction='in'/>"
1052 " <arg type='i' name='second' direction='in'/>"
1053 " <arg type='i' name='sum' direction='out'/>"
1058 g_assert_no_error (error);
1060 /* sleep 100ms before exporting an object - this is to test that
1061 * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
1062 * (GDBusServer uses this feature).
1064 usleep (100 * 1000);
1066 /* export an object */
1068 g_dbus_connection_register_object (connection,
1070 node->interfaces[0],
1071 &dmp_interface_vtable,
1075 g_dbus_node_info_unref (node);
1081 dmp_thread_func (gpointer user_data)
1083 DmpData *data = user_data;
1087 data->context = g_main_context_new ();
1088 g_main_context_push_thread_default (data->context);
1091 guid = g_dbus_generate_guid ();
1092 data->server = g_dbus_server_new_sync (tmp_address,
1093 G_DBUS_SERVER_FLAGS_NONE,
1095 NULL, /* GDBusAuthObserver */
1096 NULL, /* GCancellable */
1098 g_assert_no_error (error);
1099 g_signal_connect (data->server,
1101 G_CALLBACK (dmp_on_new_connection),
1104 g_dbus_server_start (data->server);
1106 data->loop = g_main_loop_new (data->context, FALSE);
1107 g_main_loop_run (data->loop);
1109 g_main_context_pop_thread_default (data->context);
1116 delayed_message_processing (void)
1120 GThread *service_thread;
1123 data = g_new0 (DmpData, 1);
1125 service_thread = g_thread_new ("dmp",
1128 while (data->server == NULL || !g_dbus_server_is_active (data->server))
1131 for (n = 0; n < 5; n++)
1138 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
1139 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1140 NULL, /* GDBusAuthObserver */
1141 NULL, /* GCancellable */
1143 g_assert_no_error (error);
1146 res = g_dbus_connection_call_sync (c,
1147 NULL, /* bus name */
1149 "org.gtk.GDBus.DmpInterface",
1151 g_variant_new ("(ii)", 2, n),
1152 G_VARIANT_TYPE ("(i)"),
1153 G_DBUS_CALL_FLAGS_NONE,
1154 -1, /* timeout_msec */
1155 NULL, /* GCancellable */
1157 g_assert_no_error (error);
1158 g_variant_get (res, "(i)", &val);
1159 g_assert_cmpint (val, ==, 2 + n);
1160 g_variant_unref (res);
1164 g_main_loop_quit (data->loop);
1165 g_thread_join (service_thread);
1166 dmp_data_free (data);
1169 /* ---------------------------------------------------------------------------------------------------- */
1172 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1174 GCredentials *credentials,
1177 PeerData *data = user_data;
1178 gboolean authorized;
1180 data->num_connection_attempts++;
1183 if (!data->accept_connection)
1186 g_main_loop_quit (loop);
1192 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1194 nonce_tcp_on_new_connection (GDBusServer *server,
1195 GDBusConnection *connection,
1198 PeerData *data = user_data;
1200 g_ptr_array_add (data->current_connections, g_object_ref (connection));
1202 g_main_loop_quit (loop);
1208 nonce_tcp_service_thread_func (gpointer user_data)
1210 PeerData *data = user_data;
1211 GMainContext *service_context;
1212 GDBusAuthObserver *observer;
1215 service_context = g_main_context_new ();
1216 g_main_context_push_thread_default (service_context);
1219 observer = g_dbus_auth_observer_new ();
1220 server = g_dbus_server_new_sync ("nonce-tcp:",
1221 G_DBUS_SERVER_FLAGS_NONE,
1224 NULL, /* cancellable */
1226 g_assert_no_error (error);
1228 g_signal_connect (server,
1230 G_CALLBACK (nonce_tcp_on_new_connection),
1232 g_signal_connect (observer,
1233 "authorize-authenticated-peer",
1234 G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1236 g_object_unref (observer);
1238 g_dbus_server_start (server);
1240 create_service_loop (service_context);
1241 g_main_loop_run (service_loop);
1243 g_main_context_pop_thread_default (service_context);
1245 teardown_service_loop ();
1246 g_main_context_unref (service_context);
1248 /* test code specifically unrefs the server - see below */
1249 g_assert (server == NULL);
1255 test_nonce_tcp (void)
1259 GThread *service_thread;
1264 const gchar *address;
1266 memset (&data, '\0', sizeof (PeerData));
1267 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1271 service_thread = g_thread_new ("nonce-tcp-service",
1272 nonce_tcp_service_thread_func,
1274 await_service_loop ();
1275 g_assert (server != NULL);
1277 /* bring up a connection and accept it */
1278 data.accept_connection = TRUE;
1280 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1281 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1282 NULL, /* GDBusAuthObserver */
1283 NULL, /* cancellable */
1285 g_assert_no_error (error);
1286 g_assert (c != NULL);
1287 while (data.current_connections->len < 1)
1289 g_assert_cmpint (data.current_connections->len, ==, 1);
1290 g_assert_cmpint (data.num_connection_attempts, ==, 1);
1291 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1292 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1295 /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1298 address = g_dbus_server_get_client_address (server);
1300 s = strstr (address, "noncefile=");
1301 g_assert (s != NULL);
1302 s += sizeof "noncefile=" - 1;
1303 nonce_file = g_strdup (s);
1305 /* First try invalid data in the nonce file - this will actually
1306 * make the client send this and the server will reject it. The way
1307 * it works is that if the nonce doesn't match, the server will
1308 * simply close the connection. So, from the client point of view,
1309 * we can see a variety of errors.
1312 res = g_file_set_contents (nonce_file,
1316 g_assert_no_error (error);
1318 c = g_dbus_connection_new_for_address_sync (address,
1319 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1320 NULL, /* GDBusAuthObserver */
1321 NULL, /* cancellable */
1323 _g_assert_error_domain (error, G_IO_ERROR);
1324 g_error_free (error);
1325 g_assert (c == NULL);
1327 /* Then try with a nonce-file of incorrect length - this will make
1328 * the client complain - we won't even try connecting to the server
1332 res = g_file_set_contents (nonce_file,
1333 "0123456789012345_",
1336 g_assert_no_error (error);
1338 c = g_dbus_connection_new_for_address_sync (address,
1339 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1340 NULL, /* GDBusAuthObserver */
1341 NULL, /* cancellable */
1343 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1344 g_error_free (error);
1345 g_assert (c == NULL);
1347 /* Finally try with no nonce-file at all */
1348 g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1350 c = g_dbus_connection_new_for_address_sync (address,
1351 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1352 NULL, /* GDBusAuthObserver */
1353 NULL, /* cancellable */
1355 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1356 g_error_free (error);
1357 g_assert (c == NULL);
1359 g_free (nonce_file);
1361 g_dbus_server_stop (server);
1362 g_object_unref (server);
1365 g_main_loop_quit (service_loop);
1366 g_thread_join (service_thread);
1370 test_credentials (void)
1372 GCredentials *c1, *c2;
1376 c1 = g_credentials_new ();
1377 c2 = g_credentials_new ();
1380 if (g_credentials_set_unix_user (c2, getuid (), &error))
1381 g_assert_no_error (error);
1383 g_clear_error (&error);
1384 g_assert (g_credentials_is_same_user (c1, c2, &error));
1385 g_assert_no_error (error);
1387 desc = g_credentials_to_string (c1);
1388 g_assert (desc != NULL);
1391 g_object_unref (c1);
1392 g_object_unref (c2);
1395 /* ---------------------------------------------------------------------------------------------------- */
1399 /* Chosen to be big enough to overflow the socket buffer */
1400 #define OVERFLOW_NUM_SIGNALS 5000
1401 #define OVERFLOW_TIMEOUT_SEC 10
1403 static GDBusMessage *
1404 overflow_filter_func (GDBusConnection *connection,
1405 GDBusMessage *message,
1409 volatile gint *counter = user_data;
1415 overflow_on_500ms_later_func (gpointer user_data)
1417 g_main_loop_quit (loop);
1418 return FALSE; /* don't keep the idle */
1422 test_overflow (void)
1427 GSocketConnection *socket_connection;
1428 GDBusConnection *producer, *consumer;
1431 volatile gint n_messages_received;
1432 volatile gint n_messages_sent;
1434 g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
1437 socket = g_socket_new_from_fd (sv[0], &error);
1438 g_assert_no_error (error);
1439 socket_connection = g_socket_connection_factory_create_connection (socket);
1440 g_assert (socket_connection != NULL);
1441 g_object_unref (socket);
1442 producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1444 G_DBUS_CONNECTION_FLAGS_NONE,
1445 NULL, /* GDBusAuthObserver */
1446 NULL, /* GCancellable */
1448 g_dbus_connection_set_exit_on_close (producer, TRUE);
1449 g_assert_no_error (error);
1450 g_object_unref (socket_connection);
1451 n_messages_sent = 0;
1452 g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
1454 /* send enough data that we get an EAGAIN */
1455 for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
1458 g_dbus_connection_emit_signal (producer,
1459 NULL, /* destination */
1461 "org.foo.Interface",
1463 g_variant_new ("(s)", "a string"),
1465 g_assert_no_error (error);
1468 /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
1469 * kernel buffers) and verify that n_messages_sent <
1470 * OVERFLOW_NUM_SIGNALS
1472 * This is to verify that not all the submitted messages have been
1473 * sent to the underlying transport.
1475 g_timeout_add (500, overflow_on_500ms_later_func, NULL);
1476 g_main_loop_run (loop);
1477 g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
1479 /* now suck it all out as a client, and add it up */
1480 socket = g_socket_new_from_fd (sv[1], &error);
1481 g_assert_no_error (error);
1482 socket_connection = g_socket_connection_factory_create_connection (socket);
1483 g_assert (socket_connection != NULL);
1484 g_object_unref (socket);
1485 consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1487 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
1488 NULL, /* GDBusAuthObserver */
1489 NULL, /* GCancellable */
1491 g_assert_no_error (error);
1492 g_object_unref (socket_connection);
1493 n_messages_received = 0;
1494 g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
1495 g_dbus_connection_start_message_processing (consumer);
1497 timer = g_timer_new ();
1498 g_timer_start (timer);
1500 while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
1501 g_main_context_iteration (NULL, FALSE);
1503 g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
1504 g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
1506 g_timer_destroy (timer);
1507 g_object_unref (consumer);
1508 g_object_unref (producer);
1512 test_overflow (void)
1514 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
1518 /* ---------------------------------------------------------------------------------------------------- */
1521 tcp_anonymous_on_new_connection (GDBusServer *server,
1522 GDBusConnection *connection,
1525 gboolean *seen_connection = user_data;
1526 *seen_connection = TRUE;
1531 tcp_anonymous_service_thread_func (gpointer user_data)
1533 gboolean *seen_connection = user_data;
1534 GMainContext *service_context;
1537 service_context = g_main_context_new ();
1538 g_main_context_push_thread_default (service_context);
1541 server = g_dbus_server_new_sync ("tcp:",
1542 G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS,
1544 NULL, /* GDBusObserver* */
1545 NULL, /* GCancellable* */
1547 g_assert_no_error (error);
1549 g_signal_connect (server,
1551 G_CALLBACK (tcp_anonymous_on_new_connection),
1554 g_dbus_server_start (server);
1556 create_service_loop (service_context);
1557 g_main_loop_run (service_loop);
1559 g_main_context_pop_thread_default (service_context);
1561 teardown_service_loop ();
1562 g_main_context_unref (service_context);
1568 test_tcp_anonymous (void)
1570 gboolean seen_connection;
1571 GThread *service_thread;
1572 GDBusConnection *connection;
1575 seen_connection = FALSE;
1576 service_thread = g_thread_new ("tcp-anon-service",
1577 tcp_anonymous_service_thread_func,
1579 await_service_loop ();
1580 g_assert (server != NULL);
1583 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1584 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1585 NULL, /* GDBusAuthObserver* */
1586 NULL, /* GCancellable */
1588 g_assert_no_error (error);
1589 g_assert (connection != NULL);
1591 while (!seen_connection)
1594 g_object_unref (connection);
1596 g_main_loop_quit (service_loop);
1597 g_dbus_server_stop (server);
1598 g_object_unref (server);
1601 g_thread_join (service_thread);
1604 /* ---------------------------------------------------------------------------------------------------- */
1606 static GDBusServer *codegen_server = NULL;
1609 codegen_on_animal_poke (ExampleAnimal *animal,
1610 GDBusMethodInvocation *invocation,
1612 gboolean make_happy,
1615 if ((make_sad && make_happy) || (!make_sad && !make_happy))
1617 g_main_loop_quit (service_loop);
1619 g_dbus_method_invocation_return_dbus_error (invocation,
1620 "org.gtk.GDBus.Examples.ObjectManager.Error.Failed",
1621 "Exactly one of make_sad or make_happy must be TRUE");
1627 if (g_strcmp0 (example_animal_get_mood (animal), "Sad") == 0)
1629 g_dbus_method_invocation_return_dbus_error (invocation,
1630 "org.gtk.GDBus.Examples.ObjectManager.Error.SadAnimalIsSad",
1631 "Sad animal is already sad");
1635 example_animal_set_mood (animal, "Sad");
1636 example_animal_complete_poke (animal, invocation);
1642 if (g_strcmp0 (example_animal_get_mood (animal), "Happy") == 0)
1644 g_dbus_method_invocation_return_dbus_error (invocation,
1645 "org.gtk.GDBus.Examples.ObjectManager.Error.HappyAnimalIsHappy",
1646 "Happy animal is already happy");
1650 example_animal_set_mood (animal, "Happy");
1651 example_animal_complete_poke (animal, invocation);
1655 g_assert_not_reached ();
1658 return TRUE; /* to indicate that the method was handled */
1661 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1663 codegen_on_new_connection (GDBusServer *server,
1664 GDBusConnection *connection,
1667 ExampleAnimal *animal = user_data;
1668 GError *error = NULL;
1670 /* g_print ("Client connected.\n" */
1671 /* "Negotiated capabilities: unix-fd-passing=%d\n", */
1672 /* g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING); */
1674 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (animal), connection,
1675 "/Example/Animals/000", &error);
1676 g_assert_no_error (error);
1682 codegen_service_thread_func (gpointer user_data)
1684 GMainContext *service_context;
1685 ExampleAnimal *animal;
1686 GError *error = NULL;
1688 service_context = g_main_context_new ();
1689 g_main_context_push_thread_default (service_context);
1691 /* Create the animal in the right thread context */
1692 animal = example_animal_skeleton_new ();
1694 /* Handle Poke() D-Bus method invocations on the .Animal interface */
1695 g_signal_connect (animal, "handle-poke",
1696 G_CALLBACK (codegen_on_animal_poke),
1697 NULL); /* user_data */
1699 codegen_server = g_dbus_server_new_sync (tmp_address,
1700 G_DBUS_SERVER_FLAGS_NONE,
1702 NULL, /* observer */
1703 NULL, /* cancellable */
1705 g_assert_no_error (error);
1706 g_dbus_server_start (codegen_server);
1708 g_signal_connect (codegen_server, "new-connection",
1709 G_CALLBACK (codegen_on_new_connection),
1712 create_service_loop (service_context);
1713 g_main_loop_run (service_loop);
1715 g_object_unref (animal);
1717 g_main_context_pop_thread_default (service_context);
1719 teardown_service_loop ();
1720 g_main_context_unref (service_context);
1722 g_dbus_server_stop (codegen_server);
1723 g_object_unref (codegen_server);
1724 codegen_server = NULL;
1731 codegen_quit_mainloop_timeout (gpointer data)
1733 g_main_loop_quit (loop);
1738 codegen_test_peer (void)
1740 GDBusConnection *connection;
1741 ExampleAnimal *animal1, *animal2;
1742 GThread *service_thread;
1743 GError *error = NULL;
1746 /* bring up a server - we run the server in a different thread to avoid deadlocks */
1747 service_thread = g_thread_new ("codegen_test_peer",
1748 codegen_service_thread_func,
1750 await_service_loop ();
1751 g_assert (codegen_server != NULL);
1753 /* Get an animal 1 ... */
1754 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (codegen_server),
1755 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1756 NULL, /* GDBusAuthObserver */
1757 NULL, /* cancellable */
1759 g_assert_no_error (error);
1760 g_assert (connection != NULL);
1762 animal1 = example_animal_proxy_new_sync (connection, 0, NULL,
1763 "/Example/Animals/000", NULL, &error);
1764 g_assert_no_error (error);
1765 g_assert (animal1 != NULL);
1766 g_object_unref (connection);
1768 /* Get animal 2 ... */
1769 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (codegen_server),
1770 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1771 NULL, /* GDBusAuthObserver */
1772 NULL, /* cancellable */
1774 g_assert_no_error (error);
1775 g_assert (connection != NULL);
1777 animal2 = example_animal_proxy_new_sync (connection, 0, NULL,
1778 "/Example/Animals/000", NULL, &error);
1779 g_assert_no_error (error);
1780 g_assert (animal2 != NULL);
1781 g_object_unref (connection);
1783 /* Make animal sad via animal1 */
1784 example_animal_call_poke_sync (animal1, TRUE, FALSE, NULL, &error);
1785 g_assert_no_error (error);
1787 /* Poke server and make sure animal is updated */
1788 value = g_dbus_proxy_call_sync (G_DBUS_PROXY (animal1),
1789 "org.freedesktop.DBus.Peer.Ping",
1790 NULL, G_DBUS_CALL_FLAGS_NONE, -1,
1792 g_assert_no_error (error);
1793 g_assert (value != NULL);
1794 g_variant_unref (value);
1796 /* Give the proxies a chance to refresh in the defaul main loop */
1797 g_timeout_add (100, codegen_quit_mainloop_timeout, NULL);
1798 g_main_loop_run (loop);
1800 /* Assert animals are sad */
1801 g_assert_cmpstr (example_animal_get_mood (animal1), ==, "Sad");
1802 g_assert_cmpstr (example_animal_get_mood (animal2), ==, "Sad");
1804 /* Make animal happy via animal2 */
1805 example_animal_call_poke_sync (animal2, FALSE, TRUE, NULL, &error);
1806 g_assert_no_error (error);
1808 /* Poke server and make sure animal is updated */
1809 value = g_dbus_proxy_call_sync (G_DBUS_PROXY (animal2),
1810 "org.freedesktop.DBus.Peer.Ping",
1811 NULL, G_DBUS_CALL_FLAGS_NONE, -1,
1813 g_assert_no_error (error);
1814 g_assert (value != NULL);
1815 g_variant_unref (value);
1817 /* Give the proxies a chance to refresh in the defaul main loop */
1818 g_timeout_add (1000, codegen_quit_mainloop_timeout, NULL);
1819 g_main_loop_run (loop);
1821 /* Assert animals are happy */
1822 g_assert_cmpstr (example_animal_get_mood (animal1), ==, "Happy");
1823 g_assert_cmpstr (example_animal_get_mood (animal2), ==, "Happy");
1825 /* This final call making the animal happy and sad will cause
1826 * the server to quit, when the server quits we dont get property
1827 * change notifications anyway because those are done from an idle handler
1829 example_animal_call_poke_sync (animal2, TRUE, TRUE, NULL, &error);
1831 g_object_unref (animal1);
1832 g_object_unref (animal2);
1833 g_thread_join (service_thread);
1836 /* ---------------------------------------------------------------------------------------------------- */
1844 GDBusNodeInfo *introspection_data = NULL;
1845 gchar *tmpdir = NULL;
1847 g_test_init (&argc, &argv, NULL);
1849 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1850 g_assert (introspection_data != NULL);
1851 test_interface_introspection_data = introspection_data->interfaces[0];
1853 test_guid = g_dbus_generate_guid ();
1857 if (g_unix_socket_address_abstract_names_supported ())
1858 tmp_address = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
1861 tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
1862 tmp_address = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
1866 tmp_address = g_strdup ("nonce-tcp:");
1868 /* all the tests rely on a shared main loop */
1869 loop = g_main_loop_new (NULL, FALSE);
1871 g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1872 g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1873 g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1874 g_test_add_func ("/gdbus/tcp-anonymous", test_tcp_anonymous);
1875 g_test_add_func ("/gdbus/credentials", test_credentials);
1876 g_test_add_func ("/gdbus/overflow", test_overflow);
1877 g_test_add_func ("/gdbus/codegen-peer-to-peer", codegen_test_peer);
1881 g_main_loop_unref (loop);
1883 g_dbus_node_info_unref (introspection_data);
1885 g_free (tmp_address);