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/gnetworkingprivate.h>
39 #include <gio/gunixsocketaddress.h>
40 #include <gio/gunixfdlist.h>
42 /* used in test_overflow */
44 #include <gio/gunixconnection.h>
48 #include "gdbus-tests.h"
50 #include "gdbus-example-objectmanager-generated.h"
53 static gboolean is_unix = TRUE;
55 static gboolean is_unix = FALSE;
58 static gchar *tmp_address = NULL;
59 static gchar *test_guid = NULL;
60 static GMainLoop *service_loop = NULL;
61 static GDBusServer *server = NULL;
62 static GMainLoop *loop = NULL;
64 /* ---------------------------------------------------------------------------------------------------- */
65 /* Test that peer-to-peer connections work */
66 /* ---------------------------------------------------------------------------------------------------- */
71 gboolean accept_connection;
72 gint num_connection_attempts;
73 GPtrArray *current_connections;
74 guint num_method_calls;
75 gboolean signal_received;
78 static const gchar *test_interface_introspection_xml =
80 " <interface name='org.gtk.GDBus.PeerTestInterface'>"
81 " <method name='HelloPeer'>"
82 " <arg type='s' name='greeting' direction='in'/>"
83 " <arg type='s' name='response' direction='out'/>"
85 " <method name='EmitSignal'/>"
86 " <method name='EmitSignalWithNameSet'/>"
87 " <method name='OpenFile'>"
88 " <arg type='s' name='path' direction='in'/>"
90 " <signal name='PeerSignal'>"
91 " <arg type='s' name='a_string'/>"
93 " <property type='s' name='PeerProperty' access='read'/>"
96 static GDBusInterfaceInfo *test_interface_introspection_data = NULL;
99 test_interface_method_call (GDBusConnection *connection,
101 const gchar *object_path,
102 const gchar *interface_name,
103 const gchar *method_name,
104 GVariant *parameters,
105 GDBusMethodInvocation *invocation,
108 PeerData *data = user_data;
109 const GDBusMethodInfo *info;
111 data->num_method_calls++;
113 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
114 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
116 info = g_dbus_method_invocation_get_method_info (invocation);
117 g_assert_cmpstr (info->name, ==, method_name);
119 if (g_strcmp0 (method_name, "HelloPeer") == 0)
121 const gchar *greeting;
124 g_variant_get (parameters, "(&s)", &greeting);
126 response = g_strdup_printf ("You greeted me with '%s'.",
128 g_dbus_method_invocation_return_value (invocation,
129 g_variant_new ("(s)", response));
132 else if (g_strcmp0 (method_name, "EmitSignal") == 0)
137 g_dbus_connection_emit_signal (connection,
139 "/org/gtk/GDBus/PeerTestObject",
140 "org.gtk.GDBus.PeerTestInterface",
144 g_assert_no_error (error);
145 g_dbus_method_invocation_return_value (invocation, NULL);
147 else if (g_strcmp0 (method_name, "EmitSignalWithNameSet") == 0)
151 GDBusMessage *message;
153 message = g_dbus_message_new_signal ("/org/gtk/GDBus/PeerTestObject",
154 "org.gtk.GDBus.PeerTestInterface",
155 "PeerSignalWithNameSet");
156 g_dbus_message_set_sender (message, ":1.42");
159 ret = g_dbus_connection_send_message (connection, message, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error);
160 g_assert_no_error (error);
162 g_object_unref (message);
164 g_dbus_method_invocation_return_value (invocation, NULL);
166 else if (g_strcmp0 (method_name, "OpenFile") == 0)
173 GUnixFDList *fd_list;
175 g_variant_get (parameters, "(&s)", &path);
177 fd_list = g_unix_fd_list_new ();
181 fd = open (path, O_RDONLY);
182 g_unix_fd_list_append (fd_list, fd, &error);
183 g_assert_no_error (error);
186 reply = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
187 g_dbus_message_set_unix_fd_list (reply, fd_list);
188 g_object_unref (fd_list);
189 g_object_unref (invocation);
192 g_dbus_connection_send_message (connection,
194 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
195 NULL, /* out_serial */
197 g_assert_no_error (error);
198 g_object_unref (reply);
200 g_dbus_method_invocation_return_dbus_error (invocation,
201 "org.gtk.GDBus.NotOnUnix",
202 "Your OS does not support file descriptor passing");
207 g_assert_not_reached ();
212 test_interface_get_property (GDBusConnection *connection,
214 const gchar *object_path,
215 const gchar *interface_name,
216 const gchar *property_name,
220 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
221 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
222 g_assert_cmpstr (property_name, ==, "PeerProperty");
224 return g_variant_new_string ("ThePropertyValue");
228 static const GDBusInterfaceVTable test_interface_vtable =
230 test_interface_method_call,
231 test_interface_get_property,
232 NULL /* set_property */
236 on_proxy_signal_received (GDBusProxy *proxy,
239 GVariant *parameters,
242 PeerData *data = user_data;
244 data->signal_received = TRUE;
246 g_assert (sender_name == NULL);
247 g_assert_cmpstr (signal_name, ==, "PeerSignal");
248 g_main_loop_quit (loop);
252 on_proxy_signal_received_with_name_set (GDBusProxy *proxy,
255 GVariant *parameters,
258 PeerData *data = user_data;
260 data->signal_received = TRUE;
262 g_assert_cmpstr (sender_name, ==, ":1.42");
263 g_assert_cmpstr (signal_name, ==, "PeerSignalWithNameSet");
264 g_main_loop_quit (loop);
267 /* ---------------------------------------------------------------------------------------------------- */
270 on_authorize_authenticated_peer (GDBusAuthObserver *observer,
272 GCredentials *credentials,
275 PeerData *data = user_data;
278 data->num_connection_attempts++;
281 if (!data->accept_connection)
284 g_main_loop_quit (loop);
290 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
292 on_new_connection (GDBusServer *server,
293 GDBusConnection *connection,
296 PeerData *data = user_data;
300 //g_print ("Client connected.\n"
301 // "Negotiated capabilities: unix-fd-passing=%d\n",
302 // g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
304 g_ptr_array_add (data->current_connections, g_object_ref (connection));
306 /* export object on the newly established connection */
308 reg_id = g_dbus_connection_register_object (connection,
309 "/org/gtk/GDBus/PeerTestObject",
310 test_interface_introspection_data,
311 &test_interface_vtable,
313 NULL, /* GDestroyNotify for data */
315 g_assert_no_error (error);
316 g_assert (reg_id > 0);
318 g_main_loop_quit (loop);
324 service_thread_func (gpointer user_data)
326 PeerData *data = user_data;
327 GMainContext *service_context;
328 GDBusAuthObserver *observer, *o;
334 service_context = g_main_context_new ();
335 g_main_context_push_thread_default (service_context);
338 observer = g_dbus_auth_observer_new ();
339 server = g_dbus_server_new_sync (tmp_address,
340 G_DBUS_SERVER_FLAGS_NONE,
343 NULL, /* cancellable */
345 g_assert_no_error (error);
347 g_signal_connect (server,
349 G_CALLBACK (on_new_connection),
351 g_signal_connect (observer,
352 "authorize-authenticated-peer",
353 G_CALLBACK (on_authorize_authenticated_peer),
356 g_assert_cmpint (g_dbus_server_get_flags (server), ==, G_DBUS_SERVER_FLAGS_NONE);
357 g_assert_cmpstr (g_dbus_server_get_guid (server), ==, test_guid);
358 g_object_get (server,
363 "authentication-observer", &o,
365 g_assert_cmpint (f, ==, G_DBUS_SERVER_FLAGS_NONE);
366 g_assert_cmpstr (a, ==, tmp_address);
367 g_assert_cmpstr (g, ==, test_guid);
369 g_assert (o == observer);
374 g_object_unref (observer);
376 g_dbus_server_start (server);
378 service_loop = g_main_loop_new (service_context, FALSE);
379 g_main_loop_run (service_loop);
381 g_main_context_pop_thread_default (service_context);
383 g_main_loop_unref (service_loop);
384 g_main_context_unref (service_context);
386 /* test code specifically unrefs the server - see below */
387 g_assert (server == NULL);
394 on_incoming_connection (GSocketService *service,
395 GSocketConnection *socket_connection,
396 GObject *source_object,
399 PeerData *data = user_data;
401 if (data->accept_connection)
405 GDBusConnection *connection;
408 connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
410 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
411 NULL, /* cancellable */
413 g_assert_no_error (error);
415 g_ptr_array_add (data->current_connections, connection);
417 /* export object on the newly established connection */
419 reg_id = g_dbus_connection_register_object (connection,
420 "/org/gtk/GDBus/PeerTestObject",
421 &test_interface_introspection_data,
422 &test_interface_vtable,
424 NULL, /* GDestroyNotify for data */
426 g_assert_no_error (error);
427 g_assert (reg_id > 0);
432 /* don't do anything */
435 data->num_connection_attempts++;
437 g_main_loop_quit (loop);
439 /* stops other signal handlers from being invoked */
444 service_thread_func (gpointer data)
446 GMainContext *service_context;
448 GSocketAddress *address;
451 service_context = g_main_context_new ();
452 g_main_context_push_thread_default (service_context);
454 socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
455 address = g_unix_socket_address_new (socket_path);
457 service = g_socket_service_new ();
459 g_socket_listener_add_address (G_SOCKET_LISTENER (service),
461 G_SOCKET_TYPE_STREAM,
462 G_SOCKET_PROTOCOL_DEFAULT,
463 NULL, /* source_object */
464 NULL, /* effective_address */
466 g_assert_no_error (error);
467 g_signal_connect (service,
469 G_CALLBACK (on_incoming_connection),
471 g_socket_service_start (service);
473 service_loop = g_main_loop_new (service_context, FALSE);
474 g_main_loop_run (service_loop);
476 g_main_context_pop_thread_default (service_context);
478 g_main_loop_unref (service_loop);
479 g_main_context_unref (service_context);
481 g_object_unref (address);
482 g_free (socket_path);
487 /* ---------------------------------------------------------------------------------------------------- */
491 check_connection (gpointer user_data)
493 PeerData *data = user_data;
496 for (n = 0; n < data->current_connections->len; n++)
501 c = G_DBUS_CONNECTION (data->current_connections->pdata[n]);
502 stream = g_dbus_connection_get_stream (c);
504 g_debug ("In check_connection for %d: connection %p, stream %p", n, c, stream);
505 g_debug ("closed = %d", g_io_stream_is_closed (stream));
508 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
509 g_debug ("socket_closed = %d", g_socket_is_closed (socket));
510 g_debug ("socket_condition_check = %d", g_socket_condition_check (socket, G_IO_IN|G_IO_OUT|G_IO_ERR|G_IO_HUP));
516 num_read = g_input_stream_read (g_io_stream_get_input_stream (stream),
523 g_debug ("error: %s", error->message);
524 g_error_free (error);
528 g_debug ("no error, read %d bytes", (gint) num_read);
536 on_do_disconnect_in_idle (gpointer data)
538 GDBusConnection *c = G_DBUS_CONNECTION (data);
539 g_debug ("GDC %p has ref_count %d", c, G_OBJECT (c)->ref_count);
540 g_dbus_connection_disconnect (c);
548 read_all_from_fd (gint fd, gsize *out_len, GError **error)
554 str = g_string_new (NULL);
558 num_read = read (fd, buf, sizeof (buf));
561 if (errno == EAGAIN || errno == EWOULDBLOCK)
565 g_io_error_from_errno (errno),
566 "Failed reading %d bytes into offset %d: %s",
572 else if (num_read > 0)
574 g_string_append_len (str, buf, num_read);
576 else if (num_read == 0)
585 return g_string_free (str, FALSE);
590 g_string_free (str, TRUE);
606 GThread *service_thread;
607 gulong signal_handler_id;
609 memset (&data, '\0', sizeof (PeerData));
610 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
612 /* first try to connect when there is no server */
614 c = g_dbus_connection_new_for_address_sync (is_unix ? "unix:path=/tmp/gdbus-test-does-not-exist-pid" :
615 /* NOTE: Even if something is listening on port 12345 the connection
616 * will fail because the nonce file doesn't exist */
617 "nonce-tcp:host=localhost,port=12345,noncefile=this-does-not-exist-gdbus",
618 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
619 NULL, /* GDBusAuthObserver */
620 NULL, /* cancellable */
622 _g_assert_error_domain (error, G_IO_ERROR);
623 g_assert (!g_dbus_error_is_remote_error (error));
624 g_clear_error (&error);
625 g_assert (c == NULL);
627 /* bring up a server - we run the server in a different thread to avoid deadlocks */
629 service_thread = g_thread_new ("test_peer",
632 while (service_loop == NULL)
634 g_assert (server != NULL);
636 /* bring up a connection and accept it */
637 data.accept_connection = TRUE;
639 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
640 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
641 NULL, /* GDBusAuthObserver */
642 NULL, /* cancellable */
644 g_assert_no_error (error);
645 g_assert (c != NULL);
646 while (data.current_connections->len < 1)
647 g_main_loop_run (loop);
648 g_assert_cmpint (data.current_connections->len, ==, 1);
649 g_assert_cmpint (data.num_connection_attempts, ==, 1);
650 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
651 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
653 /* check that we create a proxy, read properties, receive signals and invoke
654 * the HelloPeer() method. Since the server runs in another thread it's fine
655 * to use synchronous blocking API here.
658 proxy = g_dbus_proxy_new_sync (c,
659 G_DBUS_PROXY_FLAGS_NONE,
662 "/org/gtk/GDBus/PeerTestObject",
663 "org.gtk.GDBus.PeerTestInterface",
664 NULL, /* GCancellable */
666 g_assert_no_error (error);
667 g_assert (proxy != NULL);
669 value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
670 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
672 /* try invoking a method */
674 result = g_dbus_proxy_call_sync (proxy,
676 g_variant_new ("(s)", "Hey Peer!"),
677 G_DBUS_CALL_FLAGS_NONE,
679 NULL, /* GCancellable */
681 g_assert_no_error (error);
682 g_variant_get (result, "(&s)", &s);
683 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
684 g_variant_unref (result);
685 g_assert_cmpint (data.num_method_calls, ==, 1);
687 /* make the other peer emit a signal - catch it */
688 signal_handler_id = g_signal_connect (proxy,
690 G_CALLBACK (on_proxy_signal_received),
692 g_assert (!data.signal_received);
693 g_dbus_proxy_call (proxy,
695 NULL, /* no arguments */
696 G_DBUS_CALL_FLAGS_NONE,
698 NULL, /* GCancellable */
699 NULL, /* GAsyncReadyCallback - we don't care about the result */
700 NULL); /* user_data */
701 g_main_loop_run (loop);
702 g_assert (data.signal_received);
703 g_assert_cmpint (data.num_method_calls, ==, 2);
704 g_signal_handler_disconnect (proxy, signal_handler_id);
706 /* Also ensure that messages with the sender header-field set gets
707 * delivered to the proxy - note that this doesn't really make sense
708 * e.g. names are meaning-less in a peer-to-peer case... but we
709 * support it because it makes sense in certain bridging
710 * applications - see e.g. #623815.
712 signal_handler_id = g_signal_connect (proxy,
714 G_CALLBACK (on_proxy_signal_received_with_name_set),
716 data.signal_received = FALSE;
717 g_dbus_proxy_call (proxy,
718 "EmitSignalWithNameSet",
719 NULL, /* no arguments */
720 G_DBUS_CALL_FLAGS_NONE,
722 NULL, /* GCancellable */
723 NULL, /* GAsyncReadyCallback - we don't care about the result */
724 NULL); /* user_data */
725 g_main_loop_run (loop);
726 g_assert (data.signal_received);
727 g_assert_cmpint (data.num_method_calls, ==, 3);
728 g_signal_handler_disconnect (proxy, signal_handler_id);
730 /* check for UNIX fd passing */
733 GDBusMessage *method_call_message;
734 GDBusMessage *method_reply_message;
735 GUnixFDList *fd_list;
742 method_call_message = g_dbus_message_new_method_call (NULL, /* name */
743 "/org/gtk/GDBus/PeerTestObject",
744 "org.gtk.GDBus.PeerTestInterface",
746 g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", "/etc/hosts"));
748 method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
750 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
752 NULL, /* out_serial */
753 NULL, /* cancellable */
755 g_assert_no_error (error);
756 g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
757 fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
758 g_assert (fd_list != NULL);
759 g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
761 fd = g_unix_fd_list_get (fd_list, 0, &error);
762 g_assert_no_error (error);
763 g_object_unref (method_call_message);
764 g_object_unref (method_reply_message);
768 buf = read_all_from_fd (fd, &len, &error);
769 g_assert_no_error (error);
770 g_assert (buf != NULL);
774 g_file_get_contents ("/etc/hosts",
778 g_assert_no_error (error);
779 g_assert_cmpint (len, ==, len2);
780 g_assert (memcmp (buf, buf2, len) == 0);
786 result = g_dbus_proxy_call_sync (proxy,
788 g_variant_new ("(s)", "boo"),
789 G_DBUS_CALL_FLAGS_NONE,
791 NULL, /* GCancellable */
793 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
794 g_assert (result == NULL);
795 g_error_free (error);
796 #endif /* G_OS_UNIX */
798 /* Check that g_socket_get_credentials() work - this really should
799 * be in a GSocket-specific test suite but no such test suite exists
804 GCredentials *credentials;
805 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (g_dbus_connection_get_stream (c)));
806 g_assert (G_IS_SOCKET (socket));
808 credentials = g_socket_get_credentials (socket, &error);
811 struct ucred *native_creds;
812 g_assert_no_error (error);
813 g_assert (G_IS_CREDENTIALS (credentials));
814 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED);
815 g_assert (native_creds != NULL);
816 g_assert (native_creds->uid == getuid ());
817 g_assert (native_creds->gid == getgid ());
818 g_assert (native_creds->pid == getpid ());
820 g_object_unref (credentials);
821 #elif defined (__OpenBSD__)
823 struct sockpeercred *native_creds;
824 g_assert_no_error (error);
825 g_assert (G_IS_CREDENTIALS (credentials));
826 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED);
827 g_assert (native_creds != NULL);
828 g_assert (native_creds->uid == getuid ());
829 g_assert (native_creds->gid == getgid ());
830 g_assert (native_creds->pid == getpid ());
832 g_object_unref (credentials);
834 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
835 g_assert (credentials == NULL);
840 /* bring up a connection - don't accept it - this should fail
842 data.accept_connection = FALSE;
844 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
845 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
846 NULL, /* GDBusAuthObserver */
847 NULL, /* cancellable */
849 _g_assert_error_domain (error, G_IO_ERROR);
850 g_error_free (error);
851 g_assert (c2 == NULL);
854 /* TODO: THIS TEST DOESN'T WORK YET */
856 /* bring up a connection - accept it.. then disconnect from the client side - check
857 * that the server side gets the disconnect signal.
860 data.accept_connection = TRUE;
861 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
862 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
863 NULL, /* GDBusAuthObserver */
864 NULL, /* cancellable */
866 g_assert_no_error (error);
867 g_assert (c2 != NULL);
868 g_assert (!g_dbus_connection_get_is_disconnected (c2));
869 while (data.num_connection_attempts < 3)
870 g_main_loop_run (loop);
871 g_assert_cmpint (data.current_connections->len, ==, 2);
872 g_assert_cmpint (data.num_connection_attempts, ==, 3);
873 g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
874 g_idle_add (on_do_disconnect_in_idle, c2);
875 g_debug ("==================================================");
876 g_debug ("==================================================");
877 g_debug ("==================================================");
878 g_debug ("waiting for disconnect on connection %p, stream %p",
879 data.current_connections->pdata[1],
880 g_dbus_connection_get_stream (data.current_connections->pdata[1]));
882 g_timeout_add (2000, check_connection, &data);
883 //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
884 g_main_loop_run (loop);
885 g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
886 g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
889 /* unref the server and stop listening for new connections
891 * This won't bring down the established connections - check that c is still connected
892 * by invoking a method
894 //g_socket_service_stop (service);
895 //g_object_unref (service);
896 g_dbus_server_stop (server);
897 g_object_unref (server);
901 result = g_dbus_proxy_call_sync (proxy,
903 g_variant_new ("(s)", "Hey Again Peer!"),
904 G_DBUS_CALL_FLAGS_NONE,
906 NULL, /* GCancellable */
908 g_assert_no_error (error);
909 g_variant_get (result, "(&s)", &s);
910 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
911 g_variant_unref (result);
912 g_assert_cmpint (data.num_method_calls, ==, 5);
915 /* TODO: THIS TEST DOESN'T WORK YET */
917 /* now disconnect from the server side - check that the client side gets the signal */
918 g_assert_cmpint (data.current_connections->len, ==, 1);
919 g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
920 g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
921 if (!g_dbus_connection_get_is_disconnected (c))
922 _g_assert_signal_received (c, "closed");
923 g_assert (g_dbus_connection_get_is_disconnected (c));
927 g_ptr_array_unref (data.current_connections);
928 g_object_unref (proxy);
930 g_main_loop_quit (service_loop);
931 g_thread_join (service_thread);
934 /* ---------------------------------------------------------------------------------------------------- */
939 GMainContext *context;
946 dmp_data_free (DmpData *data)
948 g_main_loop_unref (data->loop);
949 g_main_context_unref (data->context);
950 g_object_unref (data->server);
951 g_list_free_full (data->connections, g_object_unref);
956 dmp_on_method_call (GDBusConnection *connection,
958 const gchar *object_path,
959 const gchar *interface_name,
960 const gchar *method_name,
961 GVariant *parameters,
962 GDBusMethodInvocation *invocation,
965 //DmpData *data = user_data;
968 g_variant_get (parameters,
972 g_dbus_method_invocation_return_value (invocation,
973 g_variant_new ("(i)", first + second));
976 static const GDBusInterfaceVTable dmp_interface_vtable =
979 NULL, /* get_property */
980 NULL /* set_property */
984 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
986 dmp_on_new_connection (GDBusServer *server,
987 GDBusConnection *connection,
990 DmpData *data = user_data;
994 /* accept the connection */
995 data->connections = g_list_prepend (data->connections, g_object_ref (connection));
998 node = g_dbus_node_info_new_for_xml ("<node>"
999 " <interface name='org.gtk.GDBus.DmpInterface'>"
1000 " <method name='AddPair'>"
1001 " <arg type='i' name='first' direction='in'/>"
1002 " <arg type='i' name='second' direction='in'/>"
1003 " <arg type='i' name='sum' direction='out'/>"
1008 g_assert_no_error (error);
1010 /* sleep 100ms before exporting an object - this is to test that
1011 * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
1012 * (GDBusServer uses this feature).
1014 usleep (100 * 1000);
1016 /* export an object */
1018 g_dbus_connection_register_object (connection,
1020 node->interfaces[0],
1021 &dmp_interface_vtable,
1025 g_dbus_node_info_unref (node);
1031 dmp_thread_func (gpointer user_data)
1033 DmpData *data = user_data;
1037 data->context = g_main_context_new ();
1038 g_main_context_push_thread_default (data->context);
1041 guid = g_dbus_generate_guid ();
1042 data->server = g_dbus_server_new_sync (tmp_address,
1043 G_DBUS_SERVER_FLAGS_NONE,
1045 NULL, /* GDBusAuthObserver */
1046 NULL, /* GCancellable */
1048 g_assert_no_error (error);
1049 g_signal_connect (data->server,
1051 G_CALLBACK (dmp_on_new_connection),
1054 g_dbus_server_start (data->server);
1056 data->loop = g_main_loop_new (data->context, FALSE);
1057 g_main_loop_run (data->loop);
1059 g_main_context_pop_thread_default (data->context);
1066 delayed_message_processing (void)
1070 GThread *service_thread;
1073 data = g_new0 (DmpData, 1);
1075 service_thread = g_thread_new ("dmp",
1078 while (data->server == NULL || !g_dbus_server_is_active (data->server))
1081 for (n = 0; n < 5; n++)
1088 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
1089 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1090 NULL, /* GDBusAuthObserver */
1091 NULL, /* GCancellable */
1093 g_assert_no_error (error);
1096 res = g_dbus_connection_call_sync (c,
1097 NULL, /* bus name */
1099 "org.gtk.GDBus.DmpInterface",
1101 g_variant_new ("(ii)", 2, n),
1102 G_VARIANT_TYPE ("(i)"),
1103 G_DBUS_CALL_FLAGS_NONE,
1104 -1, /* timeout_msec */
1105 NULL, /* GCancellable */
1107 g_assert_no_error (error);
1108 g_variant_get (res, "(i)", &val);
1109 g_assert_cmpint (val, ==, 2 + n);
1110 g_variant_unref (res);
1114 g_main_loop_quit (data->loop);
1115 g_thread_join (service_thread);
1116 dmp_data_free (data);
1119 /* ---------------------------------------------------------------------------------------------------- */
1122 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1124 GCredentials *credentials,
1127 PeerData *data = user_data;
1128 gboolean authorized;
1130 data->num_connection_attempts++;
1133 if (!data->accept_connection)
1136 g_main_loop_quit (loop);
1142 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1144 nonce_tcp_on_new_connection (GDBusServer *server,
1145 GDBusConnection *connection,
1148 PeerData *data = user_data;
1150 g_ptr_array_add (data->current_connections, g_object_ref (connection));
1152 g_main_loop_quit (loop);
1158 nonce_tcp_service_thread_func (gpointer user_data)
1160 PeerData *data = user_data;
1161 GMainContext *service_context;
1162 GDBusAuthObserver *observer;
1165 service_context = g_main_context_new ();
1166 g_main_context_push_thread_default (service_context);
1169 observer = g_dbus_auth_observer_new ();
1170 server = g_dbus_server_new_sync ("nonce-tcp:",
1171 G_DBUS_SERVER_FLAGS_NONE,
1174 NULL, /* cancellable */
1176 g_assert_no_error (error);
1178 g_signal_connect (server,
1180 G_CALLBACK (nonce_tcp_on_new_connection),
1182 g_signal_connect (observer,
1183 "authorize-authenticated-peer",
1184 G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1186 g_object_unref (observer);
1188 g_dbus_server_start (server);
1190 service_loop = g_main_loop_new (service_context, FALSE);
1191 g_main_loop_run (service_loop);
1193 g_main_context_pop_thread_default (service_context);
1195 g_main_loop_unref (service_loop);
1196 g_main_context_unref (service_context);
1198 /* test code specifically unrefs the server - see below */
1199 g_assert (server == NULL);
1205 test_nonce_tcp (void)
1209 GThread *service_thread;
1214 const gchar *address;
1216 memset (&data, '\0', sizeof (PeerData));
1217 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1221 service_loop = NULL;
1222 service_thread = g_thread_new ("nonce-tcp-service",
1223 nonce_tcp_service_thread_func,
1225 while (service_loop == NULL)
1227 g_assert (server != NULL);
1230 /* bring up a connection and accept it */
1231 data.accept_connection = TRUE;
1233 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1234 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1235 NULL, /* GDBusAuthObserver */
1236 NULL, /* cancellable */
1238 g_assert_no_error (error);
1239 g_assert (c != NULL);
1240 while (data.current_connections->len < 1)
1242 g_assert_cmpint (data.current_connections->len, ==, 1);
1243 g_assert_cmpint (data.num_connection_attempts, ==, 1);
1244 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1245 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1248 /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1251 address = g_dbus_server_get_client_address (server);
1253 s = strstr (address, "noncefile=");
1254 g_assert (s != NULL);
1255 s += sizeof "noncefile=" - 1;
1256 nonce_file = g_strdup (s);
1258 /* First try invalid data in the nonce file - this will actually
1259 * make the client send this and the server will reject it. The way
1260 * it works is that if the nonce doesn't match, the server will
1261 * simply close the connection. So, from the client point of view,
1262 * we can see a variety of errors.
1265 res = g_file_set_contents (nonce_file,
1269 g_assert_no_error (error);
1271 c = g_dbus_connection_new_for_address_sync (address,
1272 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1273 NULL, /* GDBusAuthObserver */
1274 NULL, /* cancellable */
1276 _g_assert_error_domain (error, G_IO_ERROR);
1277 g_error_free (error);
1278 g_assert (c == NULL);
1280 /* Then try with a nonce-file of incorrect length - this will make
1281 * the client complain - we won't even try connecting to the server
1285 res = g_file_set_contents (nonce_file,
1286 "0123456789012345_",
1289 g_assert_no_error (error);
1291 c = g_dbus_connection_new_for_address_sync (address,
1292 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1293 NULL, /* GDBusAuthObserver */
1294 NULL, /* cancellable */
1296 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1297 g_error_free (error);
1298 g_assert (c == NULL);
1300 /* Finally try with no nonce-file at all */
1301 g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1303 c = g_dbus_connection_new_for_address_sync (address,
1304 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1305 NULL, /* GDBusAuthObserver */
1306 NULL, /* cancellable */
1308 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1309 g_error_free (error);
1310 g_assert (c == NULL);
1312 g_free (nonce_file);
1314 g_dbus_server_stop (server);
1315 g_object_unref (server);
1318 g_main_loop_quit (service_loop);
1319 g_thread_join (service_thread);
1323 test_credentials (void)
1325 GCredentials *c1, *c2;
1329 c1 = g_credentials_new ();
1330 c2 = g_credentials_new ();
1333 if (g_credentials_set_unix_user (c2, getuid (), &error))
1334 g_assert_no_error (error);
1336 g_clear_error (&error);
1337 g_assert (g_credentials_is_same_user (c1, c2, &error));
1338 g_assert_no_error (error);
1340 desc = g_credentials_to_string (c1);
1341 g_assert (desc != NULL);
1344 g_object_unref (c1);
1345 g_object_unref (c2);
1348 /* ---------------------------------------------------------------------------------------------------- */
1352 /* Chosen to be big enough to overflow the socket buffer */
1353 #define OVERFLOW_NUM_SIGNALS 5000
1354 #define OVERFLOW_TIMEOUT_SEC 10
1356 static GDBusMessage *
1357 overflow_filter_func (GDBusConnection *connection,
1358 GDBusMessage *message,
1362 volatile gint *counter = user_data;
1368 overflow_on_500ms_later_func (gpointer user_data)
1370 g_main_loop_quit (loop);
1371 return FALSE; /* don't keep the idle */
1375 test_overflow (void)
1380 GSocketConnection *socket_connection;
1381 GDBusConnection *producer, *consumer;
1384 volatile gint n_messages_received;
1385 volatile gint n_messages_sent;
1387 g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
1390 socket = g_socket_new_from_fd (sv[0], &error);
1391 g_assert_no_error (error);
1392 socket_connection = g_socket_connection_factory_create_connection (socket);
1393 g_assert (socket_connection != NULL);
1394 g_object_unref (socket);
1395 producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1397 G_DBUS_CONNECTION_FLAGS_NONE,
1398 NULL, /* GDBusAuthObserver */
1399 NULL, /* GCancellable */
1401 g_dbus_connection_set_exit_on_close (producer, TRUE);
1402 g_assert_no_error (error);
1403 g_object_unref (socket_connection);
1404 n_messages_sent = 0;
1405 g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
1407 /* send enough data that we get an EAGAIN */
1408 for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
1411 g_dbus_connection_emit_signal (producer,
1412 NULL, /* destination */
1414 "org.foo.Interface",
1416 g_variant_new ("(s)", "a string"),
1418 g_assert_no_error (error);
1421 /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
1422 * kernel buffers) and verify that n_messages_sent <
1423 * OVERFLOW_NUM_SIGNALS
1425 * This is to verify that not all the submitted messages have been
1426 * sent to the underlying transport.
1428 g_timeout_add (500, overflow_on_500ms_later_func, NULL);
1429 g_main_loop_run (loop);
1430 g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
1432 /* now suck it all out as a client, and add it up */
1433 socket = g_socket_new_from_fd (sv[1], &error);
1434 g_assert_no_error (error);
1435 socket_connection = g_socket_connection_factory_create_connection (socket);
1436 g_assert (socket_connection != NULL);
1437 g_object_unref (socket);
1438 consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1440 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
1441 NULL, /* GDBusAuthObserver */
1442 NULL, /* GCancellable */
1444 g_assert_no_error (error);
1445 g_object_unref (socket_connection);
1446 n_messages_received = 0;
1447 g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
1448 g_dbus_connection_start_message_processing (consumer);
1450 timer = g_timer_new ();
1451 g_timer_start (timer);
1453 while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
1454 g_main_context_iteration (NULL, FALSE);
1456 g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
1457 g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
1459 g_timer_destroy (timer);
1460 g_object_unref (consumer);
1461 g_object_unref (producer);
1465 test_overflow (void)
1467 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
1471 /* ---------------------------------------------------------------------------------------------------- */
1474 tcp_anonymous_on_new_connection (GDBusServer *server,
1475 GDBusConnection *connection,
1478 gboolean *seen_connection = user_data;
1479 *seen_connection = TRUE;
1484 tcp_anonymous_service_thread_func (gpointer user_data)
1486 gboolean *seen_connection = user_data;
1487 GMainContext *service_context;
1490 service_context = g_main_context_new ();
1491 g_main_context_push_thread_default (service_context);
1494 server = g_dbus_server_new_sync ("tcp:",
1495 G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS,
1497 NULL, /* GDBusObserver* */
1498 NULL, /* GCancellable* */
1500 g_assert_no_error (error);
1502 g_signal_connect (server,
1504 G_CALLBACK (tcp_anonymous_on_new_connection),
1507 g_dbus_server_start (server);
1509 service_loop = g_main_loop_new (service_context, FALSE);
1510 g_main_loop_run (service_loop);
1512 g_main_context_pop_thread_default (service_context);
1514 g_main_loop_unref (service_loop);
1515 g_main_context_unref (service_context);
1521 test_tcp_anonymous (void)
1523 gboolean seen_connection;
1524 GThread *service_thread;
1525 GDBusConnection *connection;
1528 seen_connection = FALSE;
1529 service_loop = NULL;
1530 service_thread = g_thread_new ("tcp-anon-service",
1531 tcp_anonymous_service_thread_func,
1533 while (service_loop == NULL)
1535 g_assert (server != NULL);
1538 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1539 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1540 NULL, /* GDBusAuthObserver* */
1541 NULL, /* GCancellable */
1543 g_assert_no_error (error);
1544 g_assert (connection != NULL);
1546 while (!seen_connection)
1549 g_object_unref (connection);
1551 g_main_loop_quit (service_loop);
1552 g_dbus_server_stop (server);
1553 g_object_unref (server);
1556 g_thread_join (service_thread);
1559 /* ---------------------------------------------------------------------------------------------------- */
1561 static GDBusServer *codegen_server = NULL;
1564 codegen_on_animal_poke (ExampleAnimal *animal,
1565 GDBusMethodInvocation *invocation,
1567 gboolean make_happy,
1570 if ((make_sad && make_happy) || (!make_sad && !make_happy))
1572 g_main_loop_quit (service_loop);
1574 g_dbus_method_invocation_return_dbus_error (invocation,
1575 "org.gtk.GDBus.Examples.ObjectManager.Error.Failed",
1576 "Exactly one of make_sad or make_happy must be TRUE");
1582 if (g_strcmp0 (example_animal_get_mood (animal), "Sad") == 0)
1584 g_dbus_method_invocation_return_dbus_error (invocation,
1585 "org.gtk.GDBus.Examples.ObjectManager.Error.SadAnimalIsSad",
1586 "Sad animal is already sad");
1590 example_animal_set_mood (animal, "Sad");
1591 example_animal_complete_poke (animal, invocation);
1597 if (g_strcmp0 (example_animal_get_mood (animal), "Happy") == 0)
1599 g_dbus_method_invocation_return_dbus_error (invocation,
1600 "org.gtk.GDBus.Examples.ObjectManager.Error.HappyAnimalIsHappy",
1601 "Happy animal is already happy");
1605 example_animal_set_mood (animal, "Happy");
1606 example_animal_complete_poke (animal, invocation);
1610 g_assert_not_reached ();
1613 return TRUE; /* to indicate that the method was handled */
1616 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1618 codegen_on_new_connection (GDBusServer *server,
1619 GDBusConnection *connection,
1622 ExampleAnimal *animal = user_data;
1623 GError *error = NULL;
1625 /* g_print ("Client connected.\n" */
1626 /* "Negotiated capabilities: unix-fd-passing=%d\n", */
1627 /* g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING); */
1629 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (animal), connection,
1630 "/Example/Animals/000", &error);
1631 g_assert_no_error (error);
1637 codegen_service_thread_func (gpointer user_data)
1639 GMainContext *service_context;
1640 ExampleAnimal *animal;
1641 GError *error = NULL;
1643 service_context = g_main_context_new ();
1644 g_main_context_push_thread_default (service_context);
1646 /* Create the animal in the right thread context */
1647 animal = example_animal_skeleton_new ();
1649 /* Handle Poke() D-Bus method invocations on the .Animal interface */
1650 g_signal_connect (animal, "handle-poke",
1651 G_CALLBACK (codegen_on_animal_poke),
1652 NULL); /* user_data */
1654 codegen_server = g_dbus_server_new_sync (tmp_address,
1655 G_DBUS_SERVER_FLAGS_NONE,
1657 NULL, /* observer */
1658 NULL, /* cancellable */
1660 g_assert_no_error (error);
1661 g_dbus_server_start (codegen_server);
1663 g_signal_connect (codegen_server, "new-connection",
1664 G_CALLBACK (codegen_on_new_connection),
1667 service_loop = g_main_loop_new (service_context, FALSE);
1668 g_main_loop_run (service_loop);
1670 g_object_unref (animal);
1672 g_main_context_pop_thread_default (service_context);
1674 g_main_loop_unref (service_loop);
1675 g_main_context_unref (service_context);
1677 g_dbus_server_stop (codegen_server);
1678 g_object_unref (codegen_server);
1679 codegen_server = NULL;
1686 codegen_quit_mainloop_timeout (gpointer data)
1688 g_main_loop_quit (loop);
1693 codegen_test_peer (void)
1695 GDBusConnection *connection;
1696 ExampleAnimal *animal1, *animal2;
1697 GThread *service_thread;
1698 GError *error = NULL;
1701 /* bring up a server - we run the server in a different thread to avoid deadlocks */
1702 service_loop = NULL;
1703 service_thread = g_thread_new ("codegen_test_peer",
1704 codegen_service_thread_func,
1706 while (service_loop == NULL)
1708 g_assert (codegen_server != NULL);
1710 /* Get an animal 1 ... */
1711 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (codegen_server),
1712 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1713 NULL, /* GDBusAuthObserver */
1714 NULL, /* cancellable */
1716 g_assert_no_error (error);
1717 g_assert (connection != NULL);
1719 animal1 = example_animal_proxy_new_sync (connection, 0, NULL,
1720 "/Example/Animals/000", NULL, &error);
1721 g_assert_no_error (error);
1722 g_assert (animal1 != NULL);
1723 g_object_unref (connection);
1725 /* Get animal 2 ... */
1726 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (codegen_server),
1727 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1728 NULL, /* GDBusAuthObserver */
1729 NULL, /* cancellable */
1731 g_assert_no_error (error);
1732 g_assert (connection != NULL);
1734 animal2 = example_animal_proxy_new_sync (connection, 0, NULL,
1735 "/Example/Animals/000", NULL, &error);
1736 g_assert_no_error (error);
1737 g_assert (animal2 != NULL);
1738 g_object_unref (connection);
1740 /* Make animal sad via animal1 */
1741 example_animal_call_poke_sync (animal1, TRUE, FALSE, NULL, &error);
1742 g_assert_no_error (error);
1744 /* Poke server and make sure animal is updated */
1745 value = g_dbus_proxy_call_sync (G_DBUS_PROXY (animal1),
1746 "org.freedesktop.DBus.Peer.Ping",
1747 NULL, G_DBUS_CALL_FLAGS_NONE, -1,
1749 g_assert_no_error (error);
1750 g_assert (value != NULL);
1751 g_variant_unref (value);
1753 /* Give the proxies a chance to refresh in the defaul main loop */
1754 g_timeout_add (100, codegen_quit_mainloop_timeout, NULL);
1755 g_main_loop_run (loop);
1757 /* Assert animals are sad */
1758 g_assert_cmpstr (example_animal_get_mood (animal1), ==, "Sad");
1759 g_assert_cmpstr (example_animal_get_mood (animal2), ==, "Sad");
1761 /* Make animal happy via animal2 */
1762 example_animal_call_poke_sync (animal2, FALSE, TRUE, NULL, &error);
1763 g_assert_no_error (error);
1765 /* Poke server and make sure animal is updated */
1766 value = g_dbus_proxy_call_sync (G_DBUS_PROXY (animal2),
1767 "org.freedesktop.DBus.Peer.Ping",
1768 NULL, G_DBUS_CALL_FLAGS_NONE, -1,
1770 g_assert_no_error (error);
1771 g_assert (value != NULL);
1772 g_variant_unref (value);
1774 /* Give the proxies a chance to refresh in the defaul main loop */
1775 g_timeout_add (1000, codegen_quit_mainloop_timeout, NULL);
1776 g_main_loop_run (loop);
1778 /* Assert animals are happy */
1779 g_assert_cmpstr (example_animal_get_mood (animal1), ==, "Happy");
1780 g_assert_cmpstr (example_animal_get_mood (animal2), ==, "Happy");
1782 /* This final call making the animal happy and sad will cause
1783 * the server to quit, when the server quits we dont get property
1784 * change notifications anyway because those are done from an idle handler
1786 example_animal_call_poke_sync (animal2, TRUE, TRUE, NULL, &error);
1788 g_object_unref (animal1);
1789 g_object_unref (animal2);
1790 g_thread_join (service_thread);
1793 /* ---------------------------------------------------------------------------------------------------- */
1801 GDBusNodeInfo *introspection_data = NULL;
1802 gchar *tmpdir = NULL;
1805 g_test_init (&argc, &argv, NULL);
1807 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1808 g_assert (introspection_data != NULL);
1809 test_interface_introspection_data = introspection_data->interfaces[0];
1811 test_guid = g_dbus_generate_guid ();
1815 if (g_unix_socket_address_abstract_names_supported ())
1816 tmp_address = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
1819 tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
1820 tmp_address = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
1824 tmp_address = g_strdup ("nonce-tcp:");
1826 /* all the tests rely on a shared main loop */
1827 loop = g_main_loop_new (NULL, FALSE);
1829 g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1830 g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1831 g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1832 g_test_add_func ("/gdbus/tcp-anonymous", test_tcp_anonymous);
1833 g_test_add_func ("/gdbus/credentials", test_credentials);
1834 g_test_add_func ("/gdbus/overflow", test_overflow);
1835 g_test_add_func ("/gdbus/codegen-peer-to-peer", codegen_test_peer);
1839 g_main_loop_unref (loop);
1841 g_dbus_node_info_unref (introspection_data);
1843 g_free (tmp_address);