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 const gchar *datapath;
67 static gchar *tmp_address = NULL;
68 static gchar *test_guid = NULL;
69 static GMutex service_loop_lock;
70 static GCond service_loop_cond;
71 static GMainLoop *service_loop = NULL;
72 static GDBusServer *server = NULL;
73 static GMainLoop *loop = NULL;
75 /* ---------------------------------------------------------------------------------------------------- */
76 /* Test that peer-to-peer connections work */
77 /* ---------------------------------------------------------------------------------------------------- */
82 gboolean accept_connection;
83 gint num_connection_attempts;
84 GPtrArray *current_connections;
85 guint num_method_calls;
86 gboolean signal_received;
89 static const gchar *test_interface_introspection_xml =
91 " <interface name='org.gtk.GDBus.PeerTestInterface'>"
92 " <method name='HelloPeer'>"
93 " <arg type='s' name='greeting' direction='in'/>"
94 " <arg type='s' name='response' direction='out'/>"
96 " <method name='EmitSignal'/>"
97 " <method name='EmitSignalWithNameSet'/>"
98 " <method name='OpenFile'>"
99 " <arg type='s' name='path' direction='in'/>"
101 " <signal name='PeerSignal'>"
102 " <arg type='s' name='a_string'/>"
104 " <property type='s' name='PeerProperty' access='read'/>"
107 static GDBusInterfaceInfo *test_interface_introspection_data = NULL;
110 test_interface_method_call (GDBusConnection *connection,
112 const gchar *object_path,
113 const gchar *interface_name,
114 const gchar *method_name,
115 GVariant *parameters,
116 GDBusMethodInvocation *invocation,
119 PeerData *data = user_data;
120 const GDBusMethodInfo *info;
122 data->num_method_calls++;
124 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
125 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
127 info = g_dbus_method_invocation_get_method_info (invocation);
128 g_assert_cmpstr (info->name, ==, method_name);
130 if (g_strcmp0 (method_name, "HelloPeer") == 0)
132 const gchar *greeting;
135 g_variant_get (parameters, "(&s)", &greeting);
137 response = g_strdup_printf ("You greeted me with '%s'.",
139 g_dbus_method_invocation_return_value (invocation,
140 g_variant_new ("(s)", response));
143 else if (g_strcmp0 (method_name, "EmitSignal") == 0)
148 g_dbus_connection_emit_signal (connection,
150 "/org/gtk/GDBus/PeerTestObject",
151 "org.gtk.GDBus.PeerTestInterface",
155 g_assert_no_error (error);
156 g_dbus_method_invocation_return_value (invocation, NULL);
158 else if (g_strcmp0 (method_name, "EmitSignalWithNameSet") == 0)
162 GDBusMessage *message;
164 message = g_dbus_message_new_signal ("/org/gtk/GDBus/PeerTestObject",
165 "org.gtk.GDBus.PeerTestInterface",
166 "PeerSignalWithNameSet");
167 g_dbus_message_set_sender (message, ":1.42");
170 ret = g_dbus_connection_send_message (connection, message, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error);
171 g_assert_no_error (error);
173 g_object_unref (message);
175 g_dbus_method_invocation_return_value (invocation, NULL);
177 else if (g_strcmp0 (method_name, "OpenFile") == 0)
184 GUnixFDList *fd_list;
186 g_variant_get (parameters, "(&s)", &path);
188 fd_list = g_unix_fd_list_new ();
192 fd = g_open (path, O_RDONLY, 0);
194 g_unix_fd_list_append (fd_list, fd, &error);
195 g_assert_no_error (error);
198 reply = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
199 g_dbus_message_set_unix_fd_list (reply, fd_list);
200 g_object_unref (fd_list);
201 g_object_unref (invocation);
204 g_dbus_connection_send_message (connection,
206 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
207 NULL, /* out_serial */
209 g_assert_no_error (error);
210 g_object_unref (reply);
212 g_dbus_method_invocation_return_dbus_error (invocation,
213 "org.gtk.GDBus.NotOnUnix",
214 "Your OS does not support file descriptor passing");
219 g_assert_not_reached ();
224 test_interface_get_property (GDBusConnection *connection,
226 const gchar *object_path,
227 const gchar *interface_name,
228 const gchar *property_name,
232 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
233 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
234 g_assert_cmpstr (property_name, ==, "PeerProperty");
236 return g_variant_new_string ("ThePropertyValue");
240 static const GDBusInterfaceVTable test_interface_vtable =
242 test_interface_method_call,
243 test_interface_get_property,
244 NULL /* set_property */
248 on_proxy_signal_received (GDBusProxy *proxy,
251 GVariant *parameters,
254 PeerData *data = user_data;
256 data->signal_received = TRUE;
258 g_assert (sender_name == NULL);
259 g_assert_cmpstr (signal_name, ==, "PeerSignal");
260 g_main_loop_quit (loop);
264 on_proxy_signal_received_with_name_set (GDBusProxy *proxy,
267 GVariant *parameters,
270 PeerData *data = user_data;
272 data->signal_received = TRUE;
274 g_assert_cmpstr (sender_name, ==, ":1.42");
275 g_assert_cmpstr (signal_name, ==, "PeerSignalWithNameSet");
276 g_main_loop_quit (loop);
279 /* ---------------------------------------------------------------------------------------------------- */
282 on_authorize_authenticated_peer (GDBusAuthObserver *observer,
284 GCredentials *credentials,
287 PeerData *data = user_data;
290 data->num_connection_attempts++;
293 if (!data->accept_connection)
296 g_main_loop_quit (loop);
302 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
304 on_new_connection (GDBusServer *server,
305 GDBusConnection *connection,
308 PeerData *data = user_data;
312 //g_print ("Client connected.\n"
313 // "Negotiated capabilities: unix-fd-passing=%d\n",
314 // g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
316 g_ptr_array_add (data->current_connections, g_object_ref (connection));
318 #ifdef SHOULD_HAVE_CREDENTIALS_PASSING
320 GCredentials *credentials;
322 credentials = g_dbus_connection_get_peer_credentials (connection);
324 g_assert (credentials != NULL);
325 g_assert_cmpuint (g_credentials_get_unix_user (credentials, NULL), ==,
327 g_assert_cmpuint (g_credentials_get_unix_pid (credentials, NULL), ==,
332 /* export object on the newly established connection */
334 reg_id = g_dbus_connection_register_object (connection,
335 "/org/gtk/GDBus/PeerTestObject",
336 test_interface_introspection_data,
337 &test_interface_vtable,
339 NULL, /* GDestroyNotify for data */
341 g_assert_no_error (error);
342 g_assert (reg_id > 0);
344 g_main_loop_quit (loop);
350 create_service_loop (GMainContext *service_context)
352 g_assert (service_loop == NULL);
353 g_mutex_lock (&service_loop_lock);
354 service_loop = g_main_loop_new (service_context, FALSE);
355 g_cond_broadcast (&service_loop_cond);
356 g_mutex_unlock (&service_loop_lock);
360 teardown_service_loop (void)
362 g_mutex_lock (&service_loop_lock);
363 g_clear_pointer (&service_loop, g_main_loop_unref);
364 g_mutex_unlock (&service_loop_lock);
368 await_service_loop (void)
370 g_mutex_lock (&service_loop_lock);
371 while (service_loop == NULL)
372 g_cond_wait (&service_loop_cond, &service_loop_lock);
373 g_mutex_unlock (&service_loop_lock);
377 service_thread_func (gpointer user_data)
379 PeerData *data = user_data;
380 GMainContext *service_context;
381 GDBusAuthObserver *observer, *o;
387 service_context = g_main_context_new ();
388 g_main_context_push_thread_default (service_context);
391 observer = g_dbus_auth_observer_new ();
392 server = g_dbus_server_new_sync (tmp_address,
393 G_DBUS_SERVER_FLAGS_NONE,
396 NULL, /* cancellable */
398 g_assert_no_error (error);
400 g_signal_connect (server,
402 G_CALLBACK (on_new_connection),
404 g_signal_connect (observer,
405 "authorize-authenticated-peer",
406 G_CALLBACK (on_authorize_authenticated_peer),
409 g_assert_cmpint (g_dbus_server_get_flags (server), ==, G_DBUS_SERVER_FLAGS_NONE);
410 g_assert_cmpstr (g_dbus_server_get_guid (server), ==, test_guid);
411 g_object_get (server,
416 "authentication-observer", &o,
418 g_assert_cmpint (f, ==, G_DBUS_SERVER_FLAGS_NONE);
419 g_assert_cmpstr (a, ==, tmp_address);
420 g_assert_cmpstr (g, ==, test_guid);
422 g_assert (o == observer);
427 g_object_unref (observer);
429 g_dbus_server_start (server);
431 create_service_loop (service_context);
432 g_main_loop_run (service_loop);
434 g_main_context_pop_thread_default (service_context);
436 teardown_service_loop ();
437 g_main_context_unref (service_context);
439 /* test code specifically unrefs the server - see below */
440 g_assert (server == NULL);
447 on_incoming_connection (GSocketService *service,
448 GSocketConnection *socket_connection,
449 GObject *source_object,
452 PeerData *data = user_data;
454 if (data->accept_connection)
458 GDBusConnection *connection;
461 connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
463 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
464 NULL, /* cancellable */
466 g_assert_no_error (error);
468 g_ptr_array_add (data->current_connections, connection);
470 /* export object on the newly established connection */
472 reg_id = g_dbus_connection_register_object (connection,
473 "/org/gtk/GDBus/PeerTestObject",
474 &test_interface_introspection_data,
475 &test_interface_vtable,
477 NULL, /* GDestroyNotify for data */
479 g_assert_no_error (error);
480 g_assert (reg_id > 0);
485 /* don't do anything */
488 data->num_connection_attempts++;
490 g_main_loop_quit (loop);
492 /* stops other signal handlers from being invoked */
497 service_thread_func (gpointer data)
499 GMainContext *service_context;
501 GSocketAddress *address;
504 service_context = g_main_context_new ();
505 g_main_context_push_thread_default (service_context);
507 socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
508 address = g_unix_socket_address_new (socket_path);
510 service = g_socket_service_new ();
512 g_socket_listener_add_address (G_SOCKET_LISTENER (service),
514 G_SOCKET_TYPE_STREAM,
515 G_SOCKET_PROTOCOL_DEFAULT,
516 NULL, /* source_object */
517 NULL, /* effective_address */
519 g_assert_no_error (error);
520 g_signal_connect (service,
522 G_CALLBACK (on_incoming_connection),
524 g_socket_service_start (service);
526 create_service_loop (service_context);
527 g_main_loop_run (service_loop);
529 g_main_context_pop_thread_default (service_context);
531 teardown_service_loop ();
532 g_main_context_unref (service_context);
534 g_object_unref (address);
535 g_free (socket_path);
540 /* ---------------------------------------------------------------------------------------------------- */
544 check_connection (gpointer user_data)
546 PeerData *data = user_data;
549 for (n = 0; n < data->current_connections->len; n++)
554 c = G_DBUS_CONNECTION (data->current_connections->pdata[n]);
555 stream = g_dbus_connection_get_stream (c);
557 g_debug ("In check_connection for %d: connection %p, stream %p", n, c, stream);
558 g_debug ("closed = %d", g_io_stream_is_closed (stream));
561 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
562 g_debug ("socket_closed = %d", g_socket_is_closed (socket));
563 g_debug ("socket_condition_check = %d", g_socket_condition_check (socket, G_IO_IN|G_IO_OUT|G_IO_ERR|G_IO_HUP));
569 num_read = g_input_stream_read (g_io_stream_get_input_stream (stream),
576 g_debug ("error: %s", error->message);
577 g_error_free (error);
581 g_debug ("no error, read %d bytes", (gint) num_read);
589 on_do_disconnect_in_idle (gpointer data)
591 GDBusConnection *c = G_DBUS_CONNECTION (data);
592 g_debug ("GDC %p has ref_count %d", c, G_OBJECT (c)->ref_count);
593 g_dbus_connection_disconnect (c);
601 read_all_from_fd (gint fd, gsize *out_len, GError **error)
607 str = g_string_new (NULL);
611 num_read = read (fd, buf, sizeof (buf));
614 if (errno == EAGAIN || errno == EWOULDBLOCK)
618 g_io_error_from_errno (errno),
619 "Failed reading %d bytes into offset %d: %s",
625 else if (num_read > 0)
627 g_string_append_len (str, buf, num_read);
629 else if (num_read == 0)
638 return g_string_free (str, FALSE);
643 g_string_free (str, TRUE);
659 GThread *service_thread;
660 gulong signal_handler_id;
662 memset (&data, '\0', sizeof (PeerData));
663 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
665 /* first try to connect when there is no server */
667 c = g_dbus_connection_new_for_address_sync (is_unix ? "unix:path=/tmp/gdbus-test-does-not-exist-pid" :
668 /* NOTE: Even if something is listening on port 12345 the connection
669 * will fail because the nonce file doesn't exist */
670 "nonce-tcp:host=localhost,port=12345,noncefile=this-does-not-exist-gdbus",
671 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
672 NULL, /* GDBusAuthObserver */
673 NULL, /* cancellable */
675 _g_assert_error_domain (error, G_IO_ERROR);
676 g_assert (!g_dbus_error_is_remote_error (error));
677 g_clear_error (&error);
678 g_assert (c == NULL);
680 /* bring up a server - we run the server in a different thread to avoid deadlocks */
681 service_thread = g_thread_new ("test_peer",
684 await_service_loop ();
685 g_assert (server != NULL);
687 /* bring up a connection and accept it */
688 data.accept_connection = TRUE;
690 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
691 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
692 NULL, /* GDBusAuthObserver */
693 NULL, /* cancellable */
695 g_assert_no_error (error);
696 g_assert (c != NULL);
697 while (data.current_connections->len < 1)
698 g_main_loop_run (loop);
699 g_assert_cmpint (data.current_connections->len, ==, 1);
700 g_assert_cmpint (data.num_connection_attempts, ==, 1);
701 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
702 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
704 /* check that we create a proxy, read properties, receive signals and invoke
705 * the HelloPeer() method. Since the server runs in another thread it's fine
706 * to use synchronous blocking API here.
709 proxy = g_dbus_proxy_new_sync (c,
710 G_DBUS_PROXY_FLAGS_NONE,
713 "/org/gtk/GDBus/PeerTestObject",
714 "org.gtk.GDBus.PeerTestInterface",
715 NULL, /* GCancellable */
717 g_assert_no_error (error);
718 g_assert (proxy != NULL);
720 value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
721 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
723 /* try invoking a method */
725 result = g_dbus_proxy_call_sync (proxy,
727 g_variant_new ("(s)", "Hey Peer!"),
728 G_DBUS_CALL_FLAGS_NONE,
730 NULL, /* GCancellable */
732 g_assert_no_error (error);
733 g_variant_get (result, "(&s)", &s);
734 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
735 g_variant_unref (result);
736 g_assert_cmpint (data.num_method_calls, ==, 1);
738 /* make the other peer emit a signal - catch it */
739 signal_handler_id = g_signal_connect (proxy,
741 G_CALLBACK (on_proxy_signal_received),
743 g_assert (!data.signal_received);
744 g_dbus_proxy_call (proxy,
746 NULL, /* no arguments */
747 G_DBUS_CALL_FLAGS_NONE,
749 NULL, /* GCancellable */
750 NULL, /* GAsyncReadyCallback - we don't care about the result */
751 NULL); /* user_data */
752 g_main_loop_run (loop);
753 g_assert (data.signal_received);
754 g_assert_cmpint (data.num_method_calls, ==, 2);
755 g_signal_handler_disconnect (proxy, signal_handler_id);
757 /* Also ensure that messages with the sender header-field set gets
758 * delivered to the proxy - note that this doesn't really make sense
759 * e.g. names are meaning-less in a peer-to-peer case... but we
760 * support it because it makes sense in certain bridging
761 * applications - see e.g. #623815.
763 signal_handler_id = g_signal_connect (proxy,
765 G_CALLBACK (on_proxy_signal_received_with_name_set),
767 data.signal_received = FALSE;
768 g_dbus_proxy_call (proxy,
769 "EmitSignalWithNameSet",
770 NULL, /* no arguments */
771 G_DBUS_CALL_FLAGS_NONE,
773 NULL, /* GCancellable */
774 NULL, /* GAsyncReadyCallback - we don't care about the result */
775 NULL); /* user_data */
776 g_main_loop_run (loop);
777 g_assert (data.signal_received);
778 g_assert_cmpint (data.num_method_calls, ==, 3);
779 g_signal_handler_disconnect (proxy, signal_handler_id);
781 /* check for UNIX fd passing */
784 GDBusMessage *method_call_message;
785 GDBusMessage *method_reply_message;
786 GUnixFDList *fd_list;
792 char *testfile = g_build_filename (datapath, "file.c", NULL);
794 method_call_message = g_dbus_message_new_method_call (NULL, /* name */
795 "/org/gtk/GDBus/PeerTestObject",
796 "org.gtk.GDBus.PeerTestInterface",
798 g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", testfile));
800 method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
802 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
804 NULL, /* out_serial */
805 NULL, /* cancellable */
807 g_assert_no_error (error);
808 g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
809 fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
810 g_assert (fd_list != NULL);
811 g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
813 fd = g_unix_fd_list_get (fd_list, 0, &error);
814 g_assert_no_error (error);
815 g_object_unref (method_call_message);
816 g_object_unref (method_reply_message);
820 buf = read_all_from_fd (fd, &len, &error);
821 g_assert_no_error (error);
822 g_assert (buf != NULL);
826 g_file_get_contents (testfile,
830 g_assert_no_error (error);
831 g_assert_cmpint (len, ==, len2);
832 g_assert (memcmp (buf, buf2, len) == 0);
839 result = g_dbus_proxy_call_sync (proxy,
841 g_variant_new ("(s)", "boo"),
842 G_DBUS_CALL_FLAGS_NONE,
844 NULL, /* GCancellable */
846 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
847 g_assert (result == NULL);
848 g_error_free (error);
849 #endif /* G_OS_UNIX */
851 /* Check that g_socket_get_credentials() work - this really should
852 * be in a GSocket-specific test suite but no such test suite exists
857 GCredentials *credentials;
858 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (g_dbus_connection_get_stream (c)));
859 g_assert (G_IS_SOCKET (socket));
861 credentials = g_socket_get_credentials (socket, &error);
864 struct ucred *native_creds;
865 g_assert_no_error (error);
866 g_assert (G_IS_CREDENTIALS (credentials));
867 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED);
868 g_assert (native_creds != NULL);
869 g_assert (native_creds->uid == getuid ());
870 g_assert (native_creds->gid == getgid ());
871 g_assert (native_creds->pid == getpid ());
873 g_object_unref (credentials);
874 #elif defined (__OpenBSD__)
876 struct sockpeercred *native_creds;
877 g_assert_no_error (error);
878 g_assert (G_IS_CREDENTIALS (credentials));
879 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED);
880 g_assert (native_creds != NULL);
881 g_assert (native_creds->uid == getuid ());
882 g_assert (native_creds->gid == getgid ());
883 g_assert (native_creds->pid == getpid ());
885 g_object_unref (credentials);
887 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
888 g_assert (credentials == NULL);
893 /* bring up a connection - don't accept it - this should fail
895 data.accept_connection = FALSE;
897 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
898 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
899 NULL, /* GDBusAuthObserver */
900 NULL, /* cancellable */
902 _g_assert_error_domain (error, G_IO_ERROR);
903 g_error_free (error);
904 g_assert (c2 == NULL);
907 /* TODO: THIS TEST DOESN'T WORK YET */
909 /* bring up a connection - accept it.. then disconnect from the client side - check
910 * that the server side gets the disconnect signal.
913 data.accept_connection = TRUE;
914 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
915 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
916 NULL, /* GDBusAuthObserver */
917 NULL, /* cancellable */
919 g_assert_no_error (error);
920 g_assert (c2 != NULL);
921 g_assert (!g_dbus_connection_get_is_disconnected (c2));
922 while (data.num_connection_attempts < 3)
923 g_main_loop_run (loop);
924 g_assert_cmpint (data.current_connections->len, ==, 2);
925 g_assert_cmpint (data.num_connection_attempts, ==, 3);
926 g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
927 g_idle_add (on_do_disconnect_in_idle, c2);
928 g_debug ("==================================================");
929 g_debug ("==================================================");
930 g_debug ("==================================================");
931 g_debug ("waiting for disconnect on connection %p, stream %p",
932 data.current_connections->pdata[1],
933 g_dbus_connection_get_stream (data.current_connections->pdata[1]));
935 g_timeout_add (2000, check_connection, &data);
936 //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
937 g_main_loop_run (loop);
938 g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
939 g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
942 /* unref the server and stop listening for new connections
944 * This won't bring down the established connections - check that c is still connected
945 * by invoking a method
947 //g_socket_service_stop (service);
948 //g_object_unref (service);
949 g_dbus_server_stop (server);
950 g_object_unref (server);
954 result = g_dbus_proxy_call_sync (proxy,
956 g_variant_new ("(s)", "Hey Again Peer!"),
957 G_DBUS_CALL_FLAGS_NONE,
959 NULL, /* GCancellable */
961 g_assert_no_error (error);
962 g_variant_get (result, "(&s)", &s);
963 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
964 g_variant_unref (result);
965 g_assert_cmpint (data.num_method_calls, ==, 5);
968 /* TODO: THIS TEST DOESN'T WORK YET */
970 /* now disconnect from the server side - check that the client side gets the signal */
971 g_assert_cmpint (data.current_connections->len, ==, 1);
972 g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
973 g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
974 if (!g_dbus_connection_get_is_disconnected (c))
975 _g_assert_signal_received (c, "closed");
976 g_assert (g_dbus_connection_get_is_disconnected (c));
980 g_ptr_array_unref (data.current_connections);
981 g_object_unref (proxy);
983 g_main_loop_quit (service_loop);
984 g_thread_join (service_thread);
987 /* ---------------------------------------------------------------------------------------------------- */
992 GMainContext *context;
999 dmp_data_free (DmpData *data)
1001 g_main_loop_unref (data->loop);
1002 g_main_context_unref (data->context);
1003 g_object_unref (data->server);
1004 g_list_free_full (data->connections, g_object_unref);
1009 dmp_on_method_call (GDBusConnection *connection,
1010 const gchar *sender,
1011 const gchar *object_path,
1012 const gchar *interface_name,
1013 const gchar *method_name,
1014 GVariant *parameters,
1015 GDBusMethodInvocation *invocation,
1018 //DmpData *data = user_data;
1021 g_variant_get (parameters,
1025 g_dbus_method_invocation_return_value (invocation,
1026 g_variant_new ("(i)", first + second));
1029 static const GDBusInterfaceVTable dmp_interface_vtable =
1032 NULL, /* get_property */
1033 NULL /* set_property */
1037 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1039 dmp_on_new_connection (GDBusServer *server,
1040 GDBusConnection *connection,
1043 DmpData *data = user_data;
1044 GDBusNodeInfo *node;
1047 /* accept the connection */
1048 data->connections = g_list_prepend (data->connections, g_object_ref (connection));
1051 node = g_dbus_node_info_new_for_xml ("<node>"
1052 " <interface name='org.gtk.GDBus.DmpInterface'>"
1053 " <method name='AddPair'>"
1054 " <arg type='i' name='first' direction='in'/>"
1055 " <arg type='i' name='second' direction='in'/>"
1056 " <arg type='i' name='sum' direction='out'/>"
1061 g_assert_no_error (error);
1063 /* sleep 100ms before exporting an object - this is to test that
1064 * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
1065 * (GDBusServer uses this feature).
1067 usleep (100 * 1000);
1069 /* export an object */
1071 g_dbus_connection_register_object (connection,
1073 node->interfaces[0],
1074 &dmp_interface_vtable,
1078 g_dbus_node_info_unref (node);
1084 dmp_thread_func (gpointer user_data)
1086 DmpData *data = user_data;
1090 data->context = g_main_context_new ();
1091 g_main_context_push_thread_default (data->context);
1094 guid = g_dbus_generate_guid ();
1095 data->server = g_dbus_server_new_sync (tmp_address,
1096 G_DBUS_SERVER_FLAGS_NONE,
1098 NULL, /* GDBusAuthObserver */
1099 NULL, /* GCancellable */
1101 g_assert_no_error (error);
1102 g_signal_connect (data->server,
1104 G_CALLBACK (dmp_on_new_connection),
1107 g_dbus_server_start (data->server);
1109 data->loop = g_main_loop_new (data->context, FALSE);
1110 g_main_loop_run (data->loop);
1112 g_main_context_pop_thread_default (data->context);
1119 delayed_message_processing (void)
1123 GThread *service_thread;
1126 data = g_new0 (DmpData, 1);
1128 service_thread = g_thread_new ("dmp",
1131 while (data->server == NULL || !g_dbus_server_is_active (data->server))
1134 for (n = 0; n < 5; n++)
1141 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
1142 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1143 NULL, /* GDBusAuthObserver */
1144 NULL, /* GCancellable */
1146 g_assert_no_error (error);
1149 res = g_dbus_connection_call_sync (c,
1150 NULL, /* bus name */
1152 "org.gtk.GDBus.DmpInterface",
1154 g_variant_new ("(ii)", 2, n),
1155 G_VARIANT_TYPE ("(i)"),
1156 G_DBUS_CALL_FLAGS_NONE,
1157 -1, /* timeout_msec */
1158 NULL, /* GCancellable */
1160 g_assert_no_error (error);
1161 g_variant_get (res, "(i)", &val);
1162 g_assert_cmpint (val, ==, 2 + n);
1163 g_variant_unref (res);
1167 g_main_loop_quit (data->loop);
1168 g_thread_join (service_thread);
1169 dmp_data_free (data);
1172 /* ---------------------------------------------------------------------------------------------------- */
1175 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1177 GCredentials *credentials,
1180 PeerData *data = user_data;
1181 gboolean authorized;
1183 data->num_connection_attempts++;
1186 if (!data->accept_connection)
1189 g_main_loop_quit (loop);
1195 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1197 nonce_tcp_on_new_connection (GDBusServer *server,
1198 GDBusConnection *connection,
1201 PeerData *data = user_data;
1203 g_ptr_array_add (data->current_connections, g_object_ref (connection));
1205 g_main_loop_quit (loop);
1211 nonce_tcp_service_thread_func (gpointer user_data)
1213 PeerData *data = user_data;
1214 GMainContext *service_context;
1215 GDBusAuthObserver *observer;
1218 service_context = g_main_context_new ();
1219 g_main_context_push_thread_default (service_context);
1222 observer = g_dbus_auth_observer_new ();
1223 server = g_dbus_server_new_sync ("nonce-tcp:",
1224 G_DBUS_SERVER_FLAGS_NONE,
1227 NULL, /* cancellable */
1229 g_assert_no_error (error);
1231 g_signal_connect (server,
1233 G_CALLBACK (nonce_tcp_on_new_connection),
1235 g_signal_connect (observer,
1236 "authorize-authenticated-peer",
1237 G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1239 g_object_unref (observer);
1241 g_dbus_server_start (server);
1243 create_service_loop (service_context);
1244 g_main_loop_run (service_loop);
1246 g_main_context_pop_thread_default (service_context);
1248 teardown_service_loop ();
1249 g_main_context_unref (service_context);
1251 /* test code specifically unrefs the server - see below */
1252 g_assert (server == NULL);
1258 test_nonce_tcp (void)
1262 GThread *service_thread;
1267 const gchar *address;
1269 memset (&data, '\0', sizeof (PeerData));
1270 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1274 service_thread = g_thread_new ("nonce-tcp-service",
1275 nonce_tcp_service_thread_func,
1277 await_service_loop ();
1278 g_assert (server != NULL);
1280 /* bring up a connection and accept it */
1281 data.accept_connection = TRUE;
1283 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1284 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1285 NULL, /* GDBusAuthObserver */
1286 NULL, /* cancellable */
1288 g_assert_no_error (error);
1289 g_assert (c != NULL);
1290 while (data.current_connections->len < 1)
1292 g_assert_cmpint (data.current_connections->len, ==, 1);
1293 g_assert_cmpint (data.num_connection_attempts, ==, 1);
1294 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1295 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1298 /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1301 address = g_dbus_server_get_client_address (server);
1303 s = strstr (address, "noncefile=");
1304 g_assert (s != NULL);
1305 s += sizeof "noncefile=" - 1;
1306 nonce_file = g_strdup (s);
1308 /* First try invalid data in the nonce file - this will actually
1309 * make the client send this and the server will reject it. The way
1310 * it works is that if the nonce doesn't match, the server will
1311 * simply close the connection. So, from the client point of view,
1312 * we can see a variety of errors.
1315 res = g_file_set_contents (nonce_file,
1319 g_assert_no_error (error);
1321 c = g_dbus_connection_new_for_address_sync (address,
1322 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1323 NULL, /* GDBusAuthObserver */
1324 NULL, /* cancellable */
1326 _g_assert_error_domain (error, G_IO_ERROR);
1327 g_error_free (error);
1328 g_assert (c == NULL);
1330 /* Then try with a nonce-file of incorrect length - this will make
1331 * the client complain - we won't even try connecting to the server
1335 res = g_file_set_contents (nonce_file,
1336 "0123456789012345_",
1339 g_assert_no_error (error);
1341 c = g_dbus_connection_new_for_address_sync (address,
1342 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1343 NULL, /* GDBusAuthObserver */
1344 NULL, /* cancellable */
1346 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1347 g_error_free (error);
1348 g_assert (c == NULL);
1350 /* Finally try with no nonce-file at all */
1351 g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1353 c = g_dbus_connection_new_for_address_sync (address,
1354 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1355 NULL, /* GDBusAuthObserver */
1356 NULL, /* cancellable */
1358 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1359 g_error_free (error);
1360 g_assert (c == NULL);
1362 g_free (nonce_file);
1364 g_dbus_server_stop (server);
1365 g_object_unref (server);
1368 g_main_loop_quit (service_loop);
1369 g_thread_join (service_thread);
1373 test_credentials (void)
1375 GCredentials *c1, *c2;
1379 c1 = g_credentials_new ();
1380 c2 = g_credentials_new ();
1383 if (g_credentials_set_unix_user (c2, getuid (), &error))
1384 g_assert_no_error (error);
1386 g_clear_error (&error);
1387 g_assert (g_credentials_is_same_user (c1, c2, &error));
1388 g_assert_no_error (error);
1390 desc = g_credentials_to_string (c1);
1391 g_assert (desc != NULL);
1394 g_object_unref (c1);
1395 g_object_unref (c2);
1398 /* ---------------------------------------------------------------------------------------------------- */
1402 /* Chosen to be big enough to overflow the socket buffer */
1403 #define OVERFLOW_NUM_SIGNALS 5000
1404 #define OVERFLOW_TIMEOUT_SEC 10
1406 static GDBusMessage *
1407 overflow_filter_func (GDBusConnection *connection,
1408 GDBusMessage *message,
1412 volatile gint *counter = user_data;
1418 overflow_on_500ms_later_func (gpointer user_data)
1420 g_main_loop_quit (loop);
1421 return FALSE; /* don't keep the idle */
1425 test_overflow (void)
1430 GSocketConnection *socket_connection;
1431 GDBusConnection *producer, *consumer;
1434 volatile gint n_messages_received;
1435 volatile gint n_messages_sent;
1437 g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
1440 socket = g_socket_new_from_fd (sv[0], &error);
1441 g_assert_no_error (error);
1442 socket_connection = g_socket_connection_factory_create_connection (socket);
1443 g_assert (socket_connection != NULL);
1444 g_object_unref (socket);
1445 producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1447 G_DBUS_CONNECTION_FLAGS_NONE,
1448 NULL, /* GDBusAuthObserver */
1449 NULL, /* GCancellable */
1451 g_dbus_connection_set_exit_on_close (producer, TRUE);
1452 g_assert_no_error (error);
1453 g_object_unref (socket_connection);
1454 n_messages_sent = 0;
1455 g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
1457 /* send enough data that we get an EAGAIN */
1458 for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
1461 g_dbus_connection_emit_signal (producer,
1462 NULL, /* destination */
1464 "org.foo.Interface",
1466 g_variant_new ("(s)", "a string"),
1468 g_assert_no_error (error);
1471 /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
1472 * kernel buffers) and verify that n_messages_sent <
1473 * OVERFLOW_NUM_SIGNALS
1475 * This is to verify that not all the submitted messages have been
1476 * sent to the underlying transport.
1478 g_timeout_add (500, overflow_on_500ms_later_func, NULL);
1479 g_main_loop_run (loop);
1480 g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
1482 /* now suck it all out as a client, and add it up */
1483 socket = g_socket_new_from_fd (sv[1], &error);
1484 g_assert_no_error (error);
1485 socket_connection = g_socket_connection_factory_create_connection (socket);
1486 g_assert (socket_connection != NULL);
1487 g_object_unref (socket);
1488 consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1490 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
1491 NULL, /* GDBusAuthObserver */
1492 NULL, /* GCancellable */
1494 g_assert_no_error (error);
1495 g_object_unref (socket_connection);
1496 n_messages_received = 0;
1497 g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
1498 g_dbus_connection_start_message_processing (consumer);
1500 timer = g_timer_new ();
1501 g_timer_start (timer);
1503 while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
1504 g_main_context_iteration (NULL, FALSE);
1506 g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
1507 g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
1509 g_timer_destroy (timer);
1510 g_object_unref (consumer);
1511 g_object_unref (producer);
1515 test_overflow (void)
1517 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
1521 /* ---------------------------------------------------------------------------------------------------- */
1524 tcp_anonymous_on_new_connection (GDBusServer *server,
1525 GDBusConnection *connection,
1528 gboolean *seen_connection = user_data;
1529 *seen_connection = TRUE;
1534 tcp_anonymous_service_thread_func (gpointer user_data)
1536 gboolean *seen_connection = user_data;
1537 GMainContext *service_context;
1540 service_context = g_main_context_new ();
1541 g_main_context_push_thread_default (service_context);
1544 server = g_dbus_server_new_sync ("tcp:",
1545 G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS,
1547 NULL, /* GDBusObserver* */
1548 NULL, /* GCancellable* */
1550 g_assert_no_error (error);
1552 g_signal_connect (server,
1554 G_CALLBACK (tcp_anonymous_on_new_connection),
1557 g_dbus_server_start (server);
1559 create_service_loop (service_context);
1560 g_main_loop_run (service_loop);
1562 g_main_context_pop_thread_default (service_context);
1564 teardown_service_loop ();
1565 g_main_context_unref (service_context);
1571 test_tcp_anonymous (void)
1573 gboolean seen_connection;
1574 GThread *service_thread;
1575 GDBusConnection *connection;
1578 seen_connection = FALSE;
1579 service_thread = g_thread_new ("tcp-anon-service",
1580 tcp_anonymous_service_thread_func,
1582 await_service_loop ();
1583 g_assert (server != NULL);
1586 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1587 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1588 NULL, /* GDBusAuthObserver* */
1589 NULL, /* GCancellable */
1591 g_assert_no_error (error);
1592 g_assert (connection != NULL);
1594 while (!seen_connection)
1597 g_object_unref (connection);
1599 g_main_loop_quit (service_loop);
1600 g_dbus_server_stop (server);
1601 g_object_unref (server);
1604 g_thread_join (service_thread);
1607 /* ---------------------------------------------------------------------------------------------------- */
1609 static GDBusServer *codegen_server = NULL;
1612 codegen_on_animal_poke (ExampleAnimal *animal,
1613 GDBusMethodInvocation *invocation,
1615 gboolean make_happy,
1618 if ((make_sad && make_happy) || (!make_sad && !make_happy))
1620 g_main_loop_quit (service_loop);
1622 g_dbus_method_invocation_return_dbus_error (invocation,
1623 "org.gtk.GDBus.Examples.ObjectManager.Error.Failed",
1624 "Exactly one of make_sad or make_happy must be TRUE");
1630 if (g_strcmp0 (example_animal_get_mood (animal), "Sad") == 0)
1632 g_dbus_method_invocation_return_dbus_error (invocation,
1633 "org.gtk.GDBus.Examples.ObjectManager.Error.SadAnimalIsSad",
1634 "Sad animal is already sad");
1638 example_animal_set_mood (animal, "Sad");
1639 example_animal_complete_poke (animal, invocation);
1645 if (g_strcmp0 (example_animal_get_mood (animal), "Happy") == 0)
1647 g_dbus_method_invocation_return_dbus_error (invocation,
1648 "org.gtk.GDBus.Examples.ObjectManager.Error.HappyAnimalIsHappy",
1649 "Happy animal is already happy");
1653 example_animal_set_mood (animal, "Happy");
1654 example_animal_complete_poke (animal, invocation);
1658 g_assert_not_reached ();
1661 return TRUE; /* to indicate that the method was handled */
1664 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1666 codegen_on_new_connection (GDBusServer *server,
1667 GDBusConnection *connection,
1670 ExampleAnimal *animal = user_data;
1671 GError *error = NULL;
1673 /* g_print ("Client connected.\n" */
1674 /* "Negotiated capabilities: unix-fd-passing=%d\n", */
1675 /* g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING); */
1677 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (animal), connection,
1678 "/Example/Animals/000", &error);
1679 g_assert_no_error (error);
1685 codegen_service_thread_func (gpointer user_data)
1687 GMainContext *service_context;
1688 ExampleAnimal *animal;
1689 GError *error = NULL;
1691 service_context = g_main_context_new ();
1692 g_main_context_push_thread_default (service_context);
1694 /* Create the animal in the right thread context */
1695 animal = example_animal_skeleton_new ();
1697 /* Handle Poke() D-Bus method invocations on the .Animal interface */
1698 g_signal_connect (animal, "handle-poke",
1699 G_CALLBACK (codegen_on_animal_poke),
1700 NULL); /* user_data */
1702 codegen_server = g_dbus_server_new_sync (tmp_address,
1703 G_DBUS_SERVER_FLAGS_NONE,
1705 NULL, /* observer */
1706 NULL, /* cancellable */
1708 g_assert_no_error (error);
1709 g_dbus_server_start (codegen_server);
1711 g_signal_connect (codegen_server, "new-connection",
1712 G_CALLBACK (codegen_on_new_connection),
1715 create_service_loop (service_context);
1716 g_main_loop_run (service_loop);
1718 g_object_unref (animal);
1720 g_main_context_pop_thread_default (service_context);
1722 teardown_service_loop ();
1723 g_main_context_unref (service_context);
1725 g_dbus_server_stop (codegen_server);
1726 g_object_unref (codegen_server);
1727 codegen_server = NULL;
1734 codegen_quit_mainloop_timeout (gpointer data)
1736 g_main_loop_quit (loop);
1741 codegen_test_peer (void)
1743 GDBusConnection *connection;
1744 ExampleAnimal *animal1, *animal2;
1745 GThread *service_thread;
1746 GError *error = NULL;
1749 /* bring up a server - we run the server in a different thread to avoid deadlocks */
1750 service_thread = g_thread_new ("codegen_test_peer",
1751 codegen_service_thread_func,
1753 await_service_loop ();
1754 g_assert (codegen_server != NULL);
1756 /* Get an animal 1 ... */
1757 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (codegen_server),
1758 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1759 NULL, /* GDBusAuthObserver */
1760 NULL, /* cancellable */
1762 g_assert_no_error (error);
1763 g_assert (connection != NULL);
1765 animal1 = example_animal_proxy_new_sync (connection, 0, NULL,
1766 "/Example/Animals/000", NULL, &error);
1767 g_assert_no_error (error);
1768 g_assert (animal1 != NULL);
1769 g_object_unref (connection);
1771 /* Get animal 2 ... */
1772 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (codegen_server),
1773 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1774 NULL, /* GDBusAuthObserver */
1775 NULL, /* cancellable */
1777 g_assert_no_error (error);
1778 g_assert (connection != NULL);
1780 animal2 = example_animal_proxy_new_sync (connection, 0, NULL,
1781 "/Example/Animals/000", NULL, &error);
1782 g_assert_no_error (error);
1783 g_assert (animal2 != NULL);
1784 g_object_unref (connection);
1786 /* Make animal sad via animal1 */
1787 example_animal_call_poke_sync (animal1, TRUE, FALSE, NULL, &error);
1788 g_assert_no_error (error);
1790 /* Poke server and make sure animal is updated */
1791 value = g_dbus_proxy_call_sync (G_DBUS_PROXY (animal1),
1792 "org.freedesktop.DBus.Peer.Ping",
1793 NULL, G_DBUS_CALL_FLAGS_NONE, -1,
1795 g_assert_no_error (error);
1796 g_assert (value != NULL);
1797 g_variant_unref (value);
1799 /* Give the proxies a chance to refresh in the defaul main loop */
1800 g_timeout_add (100, codegen_quit_mainloop_timeout, NULL);
1801 g_main_loop_run (loop);
1803 /* Assert animals are sad */
1804 g_assert_cmpstr (example_animal_get_mood (animal1), ==, "Sad");
1805 g_assert_cmpstr (example_animal_get_mood (animal2), ==, "Sad");
1807 /* Make animal happy via animal2 */
1808 example_animal_call_poke_sync (animal2, FALSE, TRUE, NULL, &error);
1809 g_assert_no_error (error);
1811 /* Poke server and make sure animal is updated */
1812 value = g_dbus_proxy_call_sync (G_DBUS_PROXY (animal2),
1813 "org.freedesktop.DBus.Peer.Ping",
1814 NULL, G_DBUS_CALL_FLAGS_NONE, -1,
1816 g_assert_no_error (error);
1817 g_assert (value != NULL);
1818 g_variant_unref (value);
1820 /* Give the proxies a chance to refresh in the defaul main loop */
1821 g_timeout_add (1000, codegen_quit_mainloop_timeout, NULL);
1822 g_main_loop_run (loop);
1824 /* Assert animals are happy */
1825 g_assert_cmpstr (example_animal_get_mood (animal1), ==, "Happy");
1826 g_assert_cmpstr (example_animal_get_mood (animal2), ==, "Happy");
1828 /* This final call making the animal happy and sad will cause
1829 * the server to quit, when the server quits we dont get property
1830 * change notifications anyway because those are done from an idle handler
1832 example_animal_call_poke_sync (animal2, TRUE, TRUE, NULL, &error);
1834 g_object_unref (animal1);
1835 g_object_unref (animal2);
1836 g_thread_join (service_thread);
1839 /* ---------------------------------------------------------------------------------------------------- */
1847 GDBusNodeInfo *introspection_data = NULL;
1848 gchar *tmpdir = NULL;
1850 if (g_getenv ("G_TEST_DATA"))
1851 datapath = g_getenv ("G_TEST_DATA");
1855 g_test_init (&argc, &argv, NULL);
1857 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1858 g_assert (introspection_data != NULL);
1859 test_interface_introspection_data = introspection_data->interfaces[0];
1861 test_guid = g_dbus_generate_guid ();
1865 if (g_unix_socket_address_abstract_names_supported ())
1866 tmp_address = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
1869 tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
1870 tmp_address = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
1874 tmp_address = g_strdup ("nonce-tcp:");
1876 /* all the tests rely on a shared main loop */
1877 loop = g_main_loop_new (NULL, FALSE);
1879 g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1880 g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1881 g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1882 g_test_add_func ("/gdbus/tcp-anonymous", test_tcp_anonymous);
1883 g_test_add_func ("/gdbus/credentials", test_credentials);
1884 g_test_add_func ("/gdbus/overflow", test_overflow);
1885 g_test_add_func ("/gdbus/codegen-peer-to-peer", codegen_test_peer);
1889 g_main_loop_unref (loop);
1891 g_dbus_node_info_unref (introspection_data);
1893 g_free (tmp_address);