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 GMainLoop *service_loop = NULL;
68 static GDBusServer *server = NULL;
69 static GMainLoop *loop = NULL;
71 /* ---------------------------------------------------------------------------------------------------- */
72 /* Test that peer-to-peer connections work */
73 /* ---------------------------------------------------------------------------------------------------- */
78 gboolean accept_connection;
79 gint num_connection_attempts;
80 GPtrArray *current_connections;
81 guint num_method_calls;
82 gboolean signal_received;
85 static const gchar *test_interface_introspection_xml =
87 " <interface name='org.gtk.GDBus.PeerTestInterface'>"
88 " <method name='HelloPeer'>"
89 " <arg type='s' name='greeting' direction='in'/>"
90 " <arg type='s' name='response' direction='out'/>"
92 " <method name='EmitSignal'/>"
93 " <method name='EmitSignalWithNameSet'/>"
94 " <method name='OpenFile'>"
95 " <arg type='s' name='path' direction='in'/>"
97 " <signal name='PeerSignal'>"
98 " <arg type='s' name='a_string'/>"
100 " <property type='s' name='PeerProperty' access='read'/>"
103 static GDBusInterfaceInfo *test_interface_introspection_data = NULL;
106 test_interface_method_call (GDBusConnection *connection,
108 const gchar *object_path,
109 const gchar *interface_name,
110 const gchar *method_name,
111 GVariant *parameters,
112 GDBusMethodInvocation *invocation,
115 PeerData *data = user_data;
116 const GDBusMethodInfo *info;
118 data->num_method_calls++;
120 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
121 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
123 info = g_dbus_method_invocation_get_method_info (invocation);
124 g_assert_cmpstr (info->name, ==, method_name);
126 if (g_strcmp0 (method_name, "HelloPeer") == 0)
128 const gchar *greeting;
131 g_variant_get (parameters, "(&s)", &greeting);
133 response = g_strdup_printf ("You greeted me with '%s'.",
135 g_dbus_method_invocation_return_value (invocation,
136 g_variant_new ("(s)", response));
139 else if (g_strcmp0 (method_name, "EmitSignal") == 0)
144 g_dbus_connection_emit_signal (connection,
146 "/org/gtk/GDBus/PeerTestObject",
147 "org.gtk.GDBus.PeerTestInterface",
151 g_assert_no_error (error);
152 g_dbus_method_invocation_return_value (invocation, NULL);
154 else if (g_strcmp0 (method_name, "EmitSignalWithNameSet") == 0)
158 GDBusMessage *message;
160 message = g_dbus_message_new_signal ("/org/gtk/GDBus/PeerTestObject",
161 "org.gtk.GDBus.PeerTestInterface",
162 "PeerSignalWithNameSet");
163 g_dbus_message_set_sender (message, ":1.42");
166 ret = g_dbus_connection_send_message (connection, message, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error);
167 g_assert_no_error (error);
169 g_object_unref (message);
171 g_dbus_method_invocation_return_value (invocation, NULL);
173 else if (g_strcmp0 (method_name, "OpenFile") == 0)
180 GUnixFDList *fd_list;
182 g_variant_get (parameters, "(&s)", &path);
184 fd_list = g_unix_fd_list_new ();
188 fd = g_open (path, O_RDONLY, 0);
189 g_unix_fd_list_append (fd_list, fd, &error);
190 g_assert_no_error (error);
193 reply = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
194 g_dbus_message_set_unix_fd_list (reply, fd_list);
195 g_object_unref (fd_list);
196 g_object_unref (invocation);
199 g_dbus_connection_send_message (connection,
201 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
202 NULL, /* out_serial */
204 g_assert_no_error (error);
205 g_object_unref (reply);
207 g_dbus_method_invocation_return_dbus_error (invocation,
208 "org.gtk.GDBus.NotOnUnix",
209 "Your OS does not support file descriptor passing");
214 g_assert_not_reached ();
219 test_interface_get_property (GDBusConnection *connection,
221 const gchar *object_path,
222 const gchar *interface_name,
223 const gchar *property_name,
227 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
228 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
229 g_assert_cmpstr (property_name, ==, "PeerProperty");
231 return g_variant_new_string ("ThePropertyValue");
235 static const GDBusInterfaceVTable test_interface_vtable =
237 test_interface_method_call,
238 test_interface_get_property,
239 NULL /* set_property */
243 on_proxy_signal_received (GDBusProxy *proxy,
246 GVariant *parameters,
249 PeerData *data = user_data;
251 data->signal_received = TRUE;
253 g_assert (sender_name == NULL);
254 g_assert_cmpstr (signal_name, ==, "PeerSignal");
255 g_main_loop_quit (loop);
259 on_proxy_signal_received_with_name_set (GDBusProxy *proxy,
262 GVariant *parameters,
265 PeerData *data = user_data;
267 data->signal_received = TRUE;
269 g_assert_cmpstr (sender_name, ==, ":1.42");
270 g_assert_cmpstr (signal_name, ==, "PeerSignalWithNameSet");
271 g_main_loop_quit (loop);
274 /* ---------------------------------------------------------------------------------------------------- */
277 on_authorize_authenticated_peer (GDBusAuthObserver *observer,
279 GCredentials *credentials,
282 PeerData *data = user_data;
285 data->num_connection_attempts++;
288 if (!data->accept_connection)
291 g_main_loop_quit (loop);
297 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
299 on_new_connection (GDBusServer *server,
300 GDBusConnection *connection,
303 PeerData *data = user_data;
307 //g_print ("Client connected.\n"
308 // "Negotiated capabilities: unix-fd-passing=%d\n",
309 // g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
311 g_ptr_array_add (data->current_connections, g_object_ref (connection));
313 #ifdef SHOULD_HAVE_CREDENTIALS_PASSING
315 GCredentials *credentials;
317 credentials = g_dbus_connection_get_peer_credentials (connection);
319 g_assert (credentials != NULL);
320 g_assert_cmpuint (g_credentials_get_unix_user (credentials, NULL), ==,
322 g_assert_cmpuint (g_credentials_get_unix_pid (credentials, NULL), ==,
327 /* export object on the newly established connection */
329 reg_id = g_dbus_connection_register_object (connection,
330 "/org/gtk/GDBus/PeerTestObject",
331 test_interface_introspection_data,
332 &test_interface_vtable,
334 NULL, /* GDestroyNotify for data */
336 g_assert_no_error (error);
337 g_assert (reg_id > 0);
339 g_main_loop_quit (loop);
345 service_thread_func (gpointer user_data)
347 PeerData *data = user_data;
348 GMainContext *service_context;
349 GDBusAuthObserver *observer, *o;
355 service_context = g_main_context_new ();
356 g_main_context_push_thread_default (service_context);
359 observer = g_dbus_auth_observer_new ();
360 server = g_dbus_server_new_sync (tmp_address,
361 G_DBUS_SERVER_FLAGS_NONE,
364 NULL, /* cancellable */
366 g_assert_no_error (error);
368 g_signal_connect (server,
370 G_CALLBACK (on_new_connection),
372 g_signal_connect (observer,
373 "authorize-authenticated-peer",
374 G_CALLBACK (on_authorize_authenticated_peer),
377 g_assert_cmpint (g_dbus_server_get_flags (server), ==, G_DBUS_SERVER_FLAGS_NONE);
378 g_assert_cmpstr (g_dbus_server_get_guid (server), ==, test_guid);
379 g_object_get (server,
384 "authentication-observer", &o,
386 g_assert_cmpint (f, ==, G_DBUS_SERVER_FLAGS_NONE);
387 g_assert_cmpstr (a, ==, tmp_address);
388 g_assert_cmpstr (g, ==, test_guid);
390 g_assert (o == observer);
395 g_object_unref (observer);
397 g_dbus_server_start (server);
399 service_loop = g_main_loop_new (service_context, FALSE);
400 g_main_loop_run (service_loop);
402 g_main_context_pop_thread_default (service_context);
404 g_main_loop_unref (service_loop);
405 g_main_context_unref (service_context);
407 /* test code specifically unrefs the server - see below */
408 g_assert (server == NULL);
415 on_incoming_connection (GSocketService *service,
416 GSocketConnection *socket_connection,
417 GObject *source_object,
420 PeerData *data = user_data;
422 if (data->accept_connection)
426 GDBusConnection *connection;
429 connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
431 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
432 NULL, /* cancellable */
434 g_assert_no_error (error);
436 g_ptr_array_add (data->current_connections, connection);
438 /* export object on the newly established connection */
440 reg_id = g_dbus_connection_register_object (connection,
441 "/org/gtk/GDBus/PeerTestObject",
442 &test_interface_introspection_data,
443 &test_interface_vtable,
445 NULL, /* GDestroyNotify for data */
447 g_assert_no_error (error);
448 g_assert (reg_id > 0);
453 /* don't do anything */
456 data->num_connection_attempts++;
458 g_main_loop_quit (loop);
460 /* stops other signal handlers from being invoked */
465 service_thread_func (gpointer data)
467 GMainContext *service_context;
469 GSocketAddress *address;
472 service_context = g_main_context_new ();
473 g_main_context_push_thread_default (service_context);
475 socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
476 address = g_unix_socket_address_new (socket_path);
478 service = g_socket_service_new ();
480 g_socket_listener_add_address (G_SOCKET_LISTENER (service),
482 G_SOCKET_TYPE_STREAM,
483 G_SOCKET_PROTOCOL_DEFAULT,
484 NULL, /* source_object */
485 NULL, /* effective_address */
487 g_assert_no_error (error);
488 g_signal_connect (service,
490 G_CALLBACK (on_incoming_connection),
492 g_socket_service_start (service);
494 service_loop = g_main_loop_new (service_context, FALSE);
495 g_main_loop_run (service_loop);
497 g_main_context_pop_thread_default (service_context);
499 g_main_loop_unref (service_loop);
500 g_main_context_unref (service_context);
502 g_object_unref (address);
503 g_free (socket_path);
508 /* ---------------------------------------------------------------------------------------------------- */
512 check_connection (gpointer user_data)
514 PeerData *data = user_data;
517 for (n = 0; n < data->current_connections->len; n++)
522 c = G_DBUS_CONNECTION (data->current_connections->pdata[n]);
523 stream = g_dbus_connection_get_stream (c);
525 g_debug ("In check_connection for %d: connection %p, stream %p", n, c, stream);
526 g_debug ("closed = %d", g_io_stream_is_closed (stream));
529 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
530 g_debug ("socket_closed = %d", g_socket_is_closed (socket));
531 g_debug ("socket_condition_check = %d", g_socket_condition_check (socket, G_IO_IN|G_IO_OUT|G_IO_ERR|G_IO_HUP));
537 num_read = g_input_stream_read (g_io_stream_get_input_stream (stream),
544 g_debug ("error: %s", error->message);
545 g_error_free (error);
549 g_debug ("no error, read %d bytes", (gint) num_read);
557 on_do_disconnect_in_idle (gpointer data)
559 GDBusConnection *c = G_DBUS_CONNECTION (data);
560 g_debug ("GDC %p has ref_count %d", c, G_OBJECT (c)->ref_count);
561 g_dbus_connection_disconnect (c);
569 read_all_from_fd (gint fd, gsize *out_len, GError **error)
575 str = g_string_new (NULL);
579 num_read = read (fd, buf, sizeof (buf));
582 if (errno == EAGAIN || errno == EWOULDBLOCK)
586 g_io_error_from_errno (errno),
587 "Failed reading %d bytes into offset %d: %s",
593 else if (num_read > 0)
595 g_string_append_len (str, buf, num_read);
597 else if (num_read == 0)
606 return g_string_free (str, FALSE);
611 g_string_free (str, TRUE);
627 GThread *service_thread;
628 gulong signal_handler_id;
630 memset (&data, '\0', sizeof (PeerData));
631 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
633 /* first try to connect when there is no server */
635 c = g_dbus_connection_new_for_address_sync (is_unix ? "unix:path=/tmp/gdbus-test-does-not-exist-pid" :
636 /* NOTE: Even if something is listening on port 12345 the connection
637 * will fail because the nonce file doesn't exist */
638 "nonce-tcp:host=localhost,port=12345,noncefile=this-does-not-exist-gdbus",
639 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
640 NULL, /* GDBusAuthObserver */
641 NULL, /* cancellable */
643 _g_assert_error_domain (error, G_IO_ERROR);
644 g_assert (!g_dbus_error_is_remote_error (error));
645 g_clear_error (&error);
646 g_assert (c == NULL);
648 /* bring up a server - we run the server in a different thread to avoid deadlocks */
650 service_thread = g_thread_new ("test_peer",
653 while (service_loop == NULL)
655 g_assert (server != NULL);
657 /* bring up a connection and accept it */
658 data.accept_connection = TRUE;
660 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
661 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
662 NULL, /* GDBusAuthObserver */
663 NULL, /* cancellable */
665 g_assert_no_error (error);
666 g_assert (c != NULL);
667 while (data.current_connections->len < 1)
668 g_main_loop_run (loop);
669 g_assert_cmpint (data.current_connections->len, ==, 1);
670 g_assert_cmpint (data.num_connection_attempts, ==, 1);
671 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
672 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
674 /* check that we create a proxy, read properties, receive signals and invoke
675 * the HelloPeer() method. Since the server runs in another thread it's fine
676 * to use synchronous blocking API here.
679 proxy = g_dbus_proxy_new_sync (c,
680 G_DBUS_PROXY_FLAGS_NONE,
683 "/org/gtk/GDBus/PeerTestObject",
684 "org.gtk.GDBus.PeerTestInterface",
685 NULL, /* GCancellable */
687 g_assert_no_error (error);
688 g_assert (proxy != NULL);
690 value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
691 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
693 /* try invoking a method */
695 result = g_dbus_proxy_call_sync (proxy,
697 g_variant_new ("(s)", "Hey Peer!"),
698 G_DBUS_CALL_FLAGS_NONE,
700 NULL, /* GCancellable */
702 g_assert_no_error (error);
703 g_variant_get (result, "(&s)", &s);
704 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
705 g_variant_unref (result);
706 g_assert_cmpint (data.num_method_calls, ==, 1);
708 /* make the other peer emit a signal - catch it */
709 signal_handler_id = g_signal_connect (proxy,
711 G_CALLBACK (on_proxy_signal_received),
713 g_assert (!data.signal_received);
714 g_dbus_proxy_call (proxy,
716 NULL, /* no arguments */
717 G_DBUS_CALL_FLAGS_NONE,
719 NULL, /* GCancellable */
720 NULL, /* GAsyncReadyCallback - we don't care about the result */
721 NULL); /* user_data */
722 g_main_loop_run (loop);
723 g_assert (data.signal_received);
724 g_assert_cmpint (data.num_method_calls, ==, 2);
725 g_signal_handler_disconnect (proxy, signal_handler_id);
727 /* Also ensure that messages with the sender header-field set gets
728 * delivered to the proxy - note that this doesn't really make sense
729 * e.g. names are meaning-less in a peer-to-peer case... but we
730 * support it because it makes sense in certain bridging
731 * applications - see e.g. #623815.
733 signal_handler_id = g_signal_connect (proxy,
735 G_CALLBACK (on_proxy_signal_received_with_name_set),
737 data.signal_received = FALSE;
738 g_dbus_proxy_call (proxy,
739 "EmitSignalWithNameSet",
740 NULL, /* no arguments */
741 G_DBUS_CALL_FLAGS_NONE,
743 NULL, /* GCancellable */
744 NULL, /* GAsyncReadyCallback - we don't care about the result */
745 NULL); /* user_data */
746 g_main_loop_run (loop);
747 g_assert (data.signal_received);
748 g_assert_cmpint (data.num_method_calls, ==, 3);
749 g_signal_handler_disconnect (proxy, signal_handler_id);
751 /* check for UNIX fd passing */
754 GDBusMessage *method_call_message;
755 GDBusMessage *method_reply_message;
756 GUnixFDList *fd_list;
763 method_call_message = g_dbus_message_new_method_call (NULL, /* name */
764 "/org/gtk/GDBus/PeerTestObject",
765 "org.gtk.GDBus.PeerTestInterface",
767 g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", "/etc/hosts"));
769 method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
771 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
773 NULL, /* out_serial */
774 NULL, /* cancellable */
776 g_assert_no_error (error);
777 g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
778 fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
779 g_assert (fd_list != NULL);
780 g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
782 fd = g_unix_fd_list_get (fd_list, 0, &error);
783 g_assert_no_error (error);
784 g_object_unref (method_call_message);
785 g_object_unref (method_reply_message);
789 buf = read_all_from_fd (fd, &len, &error);
790 g_assert_no_error (error);
791 g_assert (buf != NULL);
795 g_file_get_contents ("/etc/hosts",
799 g_assert_no_error (error);
800 g_assert_cmpint (len, ==, len2);
801 g_assert (memcmp (buf, buf2, len) == 0);
807 result = g_dbus_proxy_call_sync (proxy,
809 g_variant_new ("(s)", "boo"),
810 G_DBUS_CALL_FLAGS_NONE,
812 NULL, /* GCancellable */
814 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
815 g_assert (result == NULL);
816 g_error_free (error);
817 #endif /* G_OS_UNIX */
819 /* Check that g_socket_get_credentials() work - this really should
820 * be in a GSocket-specific test suite but no such test suite exists
825 GCredentials *credentials;
826 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (g_dbus_connection_get_stream (c)));
827 g_assert (G_IS_SOCKET (socket));
829 credentials = g_socket_get_credentials (socket, &error);
832 struct ucred *native_creds;
833 g_assert_no_error (error);
834 g_assert (G_IS_CREDENTIALS (credentials));
835 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED);
836 g_assert (native_creds != NULL);
837 g_assert (native_creds->uid == getuid ());
838 g_assert (native_creds->gid == getgid ());
839 g_assert (native_creds->pid == getpid ());
841 g_object_unref (credentials);
842 #elif defined (__OpenBSD__)
844 struct sockpeercred *native_creds;
845 g_assert_no_error (error);
846 g_assert (G_IS_CREDENTIALS (credentials));
847 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED);
848 g_assert (native_creds != NULL);
849 g_assert (native_creds->uid == getuid ());
850 g_assert (native_creds->gid == getgid ());
851 g_assert (native_creds->pid == getpid ());
853 g_object_unref (credentials);
855 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
856 g_assert (credentials == NULL);
861 /* bring up a connection - don't accept it - this should fail
863 data.accept_connection = FALSE;
865 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
866 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
867 NULL, /* GDBusAuthObserver */
868 NULL, /* cancellable */
870 _g_assert_error_domain (error, G_IO_ERROR);
871 g_error_free (error);
872 g_assert (c2 == NULL);
875 /* TODO: THIS TEST DOESN'T WORK YET */
877 /* bring up a connection - accept it.. then disconnect from the client side - check
878 * that the server side gets the disconnect signal.
881 data.accept_connection = TRUE;
882 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
883 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
884 NULL, /* GDBusAuthObserver */
885 NULL, /* cancellable */
887 g_assert_no_error (error);
888 g_assert (c2 != NULL);
889 g_assert (!g_dbus_connection_get_is_disconnected (c2));
890 while (data.num_connection_attempts < 3)
891 g_main_loop_run (loop);
892 g_assert_cmpint (data.current_connections->len, ==, 2);
893 g_assert_cmpint (data.num_connection_attempts, ==, 3);
894 g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
895 g_idle_add (on_do_disconnect_in_idle, c2);
896 g_debug ("==================================================");
897 g_debug ("==================================================");
898 g_debug ("==================================================");
899 g_debug ("waiting for disconnect on connection %p, stream %p",
900 data.current_connections->pdata[1],
901 g_dbus_connection_get_stream (data.current_connections->pdata[1]));
903 g_timeout_add (2000, check_connection, &data);
904 //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
905 g_main_loop_run (loop);
906 g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
907 g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
910 /* unref the server and stop listening for new connections
912 * This won't bring down the established connections - check that c is still connected
913 * by invoking a method
915 //g_socket_service_stop (service);
916 //g_object_unref (service);
917 g_dbus_server_stop (server);
918 g_object_unref (server);
922 result = g_dbus_proxy_call_sync (proxy,
924 g_variant_new ("(s)", "Hey Again Peer!"),
925 G_DBUS_CALL_FLAGS_NONE,
927 NULL, /* GCancellable */
929 g_assert_no_error (error);
930 g_variant_get (result, "(&s)", &s);
931 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
932 g_variant_unref (result);
933 g_assert_cmpint (data.num_method_calls, ==, 5);
936 /* TODO: THIS TEST DOESN'T WORK YET */
938 /* now disconnect from the server side - check that the client side gets the signal */
939 g_assert_cmpint (data.current_connections->len, ==, 1);
940 g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
941 g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
942 if (!g_dbus_connection_get_is_disconnected (c))
943 _g_assert_signal_received (c, "closed");
944 g_assert (g_dbus_connection_get_is_disconnected (c));
948 g_ptr_array_unref (data.current_connections);
949 g_object_unref (proxy);
951 g_main_loop_quit (service_loop);
952 g_thread_join (service_thread);
955 /* ---------------------------------------------------------------------------------------------------- */
960 GMainContext *context;
967 dmp_data_free (DmpData *data)
969 g_main_loop_unref (data->loop);
970 g_main_context_unref (data->context);
971 g_object_unref (data->server);
972 g_list_free_full (data->connections, g_object_unref);
977 dmp_on_method_call (GDBusConnection *connection,
979 const gchar *object_path,
980 const gchar *interface_name,
981 const gchar *method_name,
982 GVariant *parameters,
983 GDBusMethodInvocation *invocation,
986 //DmpData *data = user_data;
989 g_variant_get (parameters,
993 g_dbus_method_invocation_return_value (invocation,
994 g_variant_new ("(i)", first + second));
997 static const GDBusInterfaceVTable dmp_interface_vtable =
1000 NULL, /* get_property */
1001 NULL /* set_property */
1005 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1007 dmp_on_new_connection (GDBusServer *server,
1008 GDBusConnection *connection,
1011 DmpData *data = user_data;
1012 GDBusNodeInfo *node;
1015 /* accept the connection */
1016 data->connections = g_list_prepend (data->connections, g_object_ref (connection));
1019 node = g_dbus_node_info_new_for_xml ("<node>"
1020 " <interface name='org.gtk.GDBus.DmpInterface'>"
1021 " <method name='AddPair'>"
1022 " <arg type='i' name='first' direction='in'/>"
1023 " <arg type='i' name='second' direction='in'/>"
1024 " <arg type='i' name='sum' direction='out'/>"
1029 g_assert_no_error (error);
1031 /* sleep 100ms before exporting an object - this is to test that
1032 * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
1033 * (GDBusServer uses this feature).
1035 usleep (100 * 1000);
1037 /* export an object */
1039 g_dbus_connection_register_object (connection,
1041 node->interfaces[0],
1042 &dmp_interface_vtable,
1046 g_dbus_node_info_unref (node);
1052 dmp_thread_func (gpointer user_data)
1054 DmpData *data = user_data;
1058 data->context = g_main_context_new ();
1059 g_main_context_push_thread_default (data->context);
1062 guid = g_dbus_generate_guid ();
1063 data->server = g_dbus_server_new_sync (tmp_address,
1064 G_DBUS_SERVER_FLAGS_NONE,
1066 NULL, /* GDBusAuthObserver */
1067 NULL, /* GCancellable */
1069 g_assert_no_error (error);
1070 g_signal_connect (data->server,
1072 G_CALLBACK (dmp_on_new_connection),
1075 g_dbus_server_start (data->server);
1077 data->loop = g_main_loop_new (data->context, FALSE);
1078 g_main_loop_run (data->loop);
1080 g_main_context_pop_thread_default (data->context);
1087 delayed_message_processing (void)
1091 GThread *service_thread;
1094 data = g_new0 (DmpData, 1);
1096 service_thread = g_thread_new ("dmp",
1099 while (data->server == NULL || !g_dbus_server_is_active (data->server))
1102 for (n = 0; n < 5; n++)
1109 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
1110 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1111 NULL, /* GDBusAuthObserver */
1112 NULL, /* GCancellable */
1114 g_assert_no_error (error);
1117 res = g_dbus_connection_call_sync (c,
1118 NULL, /* bus name */
1120 "org.gtk.GDBus.DmpInterface",
1122 g_variant_new ("(ii)", 2, n),
1123 G_VARIANT_TYPE ("(i)"),
1124 G_DBUS_CALL_FLAGS_NONE,
1125 -1, /* timeout_msec */
1126 NULL, /* GCancellable */
1128 g_assert_no_error (error);
1129 g_variant_get (res, "(i)", &val);
1130 g_assert_cmpint (val, ==, 2 + n);
1131 g_variant_unref (res);
1135 g_main_loop_quit (data->loop);
1136 g_thread_join (service_thread);
1137 dmp_data_free (data);
1140 /* ---------------------------------------------------------------------------------------------------- */
1143 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1145 GCredentials *credentials,
1148 PeerData *data = user_data;
1149 gboolean authorized;
1151 data->num_connection_attempts++;
1154 if (!data->accept_connection)
1157 g_main_loop_quit (loop);
1163 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1165 nonce_tcp_on_new_connection (GDBusServer *server,
1166 GDBusConnection *connection,
1169 PeerData *data = user_data;
1171 g_ptr_array_add (data->current_connections, g_object_ref (connection));
1173 g_main_loop_quit (loop);
1179 nonce_tcp_service_thread_func (gpointer user_data)
1181 PeerData *data = user_data;
1182 GMainContext *service_context;
1183 GDBusAuthObserver *observer;
1186 service_context = g_main_context_new ();
1187 g_main_context_push_thread_default (service_context);
1190 observer = g_dbus_auth_observer_new ();
1191 server = g_dbus_server_new_sync ("nonce-tcp:",
1192 G_DBUS_SERVER_FLAGS_NONE,
1195 NULL, /* cancellable */
1197 g_assert_no_error (error);
1199 g_signal_connect (server,
1201 G_CALLBACK (nonce_tcp_on_new_connection),
1203 g_signal_connect (observer,
1204 "authorize-authenticated-peer",
1205 G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1207 g_object_unref (observer);
1209 g_dbus_server_start (server);
1211 service_loop = g_main_loop_new (service_context, FALSE);
1212 g_main_loop_run (service_loop);
1214 g_main_context_pop_thread_default (service_context);
1216 g_main_loop_unref (service_loop);
1217 g_main_context_unref (service_context);
1219 /* test code specifically unrefs the server - see below */
1220 g_assert (server == NULL);
1226 test_nonce_tcp (void)
1230 GThread *service_thread;
1235 const gchar *address;
1237 memset (&data, '\0', sizeof (PeerData));
1238 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1242 service_loop = NULL;
1243 service_thread = g_thread_new ("nonce-tcp-service",
1244 nonce_tcp_service_thread_func,
1246 while (service_loop == NULL)
1248 g_assert (server != NULL);
1251 /* bring up a connection and accept it */
1252 data.accept_connection = TRUE;
1254 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1255 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1256 NULL, /* GDBusAuthObserver */
1257 NULL, /* cancellable */
1259 g_assert_no_error (error);
1260 g_assert (c != NULL);
1261 while (data.current_connections->len < 1)
1263 g_assert_cmpint (data.current_connections->len, ==, 1);
1264 g_assert_cmpint (data.num_connection_attempts, ==, 1);
1265 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1266 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1269 /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1272 address = g_dbus_server_get_client_address (server);
1274 s = strstr (address, "noncefile=");
1275 g_assert (s != NULL);
1276 s += sizeof "noncefile=" - 1;
1277 nonce_file = g_strdup (s);
1279 /* First try invalid data in the nonce file - this will actually
1280 * make the client send this and the server will reject it. The way
1281 * it works is that if the nonce doesn't match, the server will
1282 * simply close the connection. So, from the client point of view,
1283 * we can see a variety of errors.
1286 res = g_file_set_contents (nonce_file,
1290 g_assert_no_error (error);
1292 c = g_dbus_connection_new_for_address_sync (address,
1293 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1294 NULL, /* GDBusAuthObserver */
1295 NULL, /* cancellable */
1297 _g_assert_error_domain (error, G_IO_ERROR);
1298 g_error_free (error);
1299 g_assert (c == NULL);
1301 /* Then try with a nonce-file of incorrect length - this will make
1302 * the client complain - we won't even try connecting to the server
1306 res = g_file_set_contents (nonce_file,
1307 "0123456789012345_",
1310 g_assert_no_error (error);
1312 c = g_dbus_connection_new_for_address_sync (address,
1313 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1314 NULL, /* GDBusAuthObserver */
1315 NULL, /* cancellable */
1317 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1318 g_error_free (error);
1319 g_assert (c == NULL);
1321 /* Finally try with no nonce-file at all */
1322 g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1324 c = g_dbus_connection_new_for_address_sync (address,
1325 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1326 NULL, /* GDBusAuthObserver */
1327 NULL, /* cancellable */
1329 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1330 g_error_free (error);
1331 g_assert (c == NULL);
1333 g_free (nonce_file);
1335 g_dbus_server_stop (server);
1336 g_object_unref (server);
1339 g_main_loop_quit (service_loop);
1340 g_thread_join (service_thread);
1344 test_credentials (void)
1346 GCredentials *c1, *c2;
1350 c1 = g_credentials_new ();
1351 c2 = g_credentials_new ();
1354 if (g_credentials_set_unix_user (c2, getuid (), &error))
1355 g_assert_no_error (error);
1357 g_clear_error (&error);
1358 g_assert (g_credentials_is_same_user (c1, c2, &error));
1359 g_assert_no_error (error);
1361 desc = g_credentials_to_string (c1);
1362 g_assert (desc != NULL);
1365 g_object_unref (c1);
1366 g_object_unref (c2);
1369 /* ---------------------------------------------------------------------------------------------------- */
1373 /* Chosen to be big enough to overflow the socket buffer */
1374 #define OVERFLOW_NUM_SIGNALS 5000
1375 #define OVERFLOW_TIMEOUT_SEC 10
1377 static GDBusMessage *
1378 overflow_filter_func (GDBusConnection *connection,
1379 GDBusMessage *message,
1383 volatile gint *counter = user_data;
1389 overflow_on_500ms_later_func (gpointer user_data)
1391 g_main_loop_quit (loop);
1392 return FALSE; /* don't keep the idle */
1396 test_overflow (void)
1401 GSocketConnection *socket_connection;
1402 GDBusConnection *producer, *consumer;
1405 volatile gint n_messages_received;
1406 volatile gint n_messages_sent;
1408 g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
1411 socket = g_socket_new_from_fd (sv[0], &error);
1412 g_assert_no_error (error);
1413 socket_connection = g_socket_connection_factory_create_connection (socket);
1414 g_assert (socket_connection != NULL);
1415 g_object_unref (socket);
1416 producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1418 G_DBUS_CONNECTION_FLAGS_NONE,
1419 NULL, /* GDBusAuthObserver */
1420 NULL, /* GCancellable */
1422 g_dbus_connection_set_exit_on_close (producer, TRUE);
1423 g_assert_no_error (error);
1424 g_object_unref (socket_connection);
1425 n_messages_sent = 0;
1426 g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
1428 /* send enough data that we get an EAGAIN */
1429 for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
1432 g_dbus_connection_emit_signal (producer,
1433 NULL, /* destination */
1435 "org.foo.Interface",
1437 g_variant_new ("(s)", "a string"),
1439 g_assert_no_error (error);
1442 /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
1443 * kernel buffers) and verify that n_messages_sent <
1444 * OVERFLOW_NUM_SIGNALS
1446 * This is to verify that not all the submitted messages have been
1447 * sent to the underlying transport.
1449 g_timeout_add (500, overflow_on_500ms_later_func, NULL);
1450 g_main_loop_run (loop);
1451 g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
1453 /* now suck it all out as a client, and add it up */
1454 socket = g_socket_new_from_fd (sv[1], &error);
1455 g_assert_no_error (error);
1456 socket_connection = g_socket_connection_factory_create_connection (socket);
1457 g_assert (socket_connection != NULL);
1458 g_object_unref (socket);
1459 consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1461 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
1462 NULL, /* GDBusAuthObserver */
1463 NULL, /* GCancellable */
1465 g_assert_no_error (error);
1466 g_object_unref (socket_connection);
1467 n_messages_received = 0;
1468 g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
1469 g_dbus_connection_start_message_processing (consumer);
1471 timer = g_timer_new ();
1472 g_timer_start (timer);
1474 while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
1475 g_main_context_iteration (NULL, FALSE);
1477 g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
1478 g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
1480 g_timer_destroy (timer);
1481 g_object_unref (consumer);
1482 g_object_unref (producer);
1486 test_overflow (void)
1488 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
1492 /* ---------------------------------------------------------------------------------------------------- */
1495 tcp_anonymous_on_new_connection (GDBusServer *server,
1496 GDBusConnection *connection,
1499 gboolean *seen_connection = user_data;
1500 *seen_connection = TRUE;
1505 tcp_anonymous_service_thread_func (gpointer user_data)
1507 gboolean *seen_connection = user_data;
1508 GMainContext *service_context;
1511 service_context = g_main_context_new ();
1512 g_main_context_push_thread_default (service_context);
1515 server = g_dbus_server_new_sync ("tcp:",
1516 G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS,
1518 NULL, /* GDBusObserver* */
1519 NULL, /* GCancellable* */
1521 g_assert_no_error (error);
1523 g_signal_connect (server,
1525 G_CALLBACK (tcp_anonymous_on_new_connection),
1528 g_dbus_server_start (server);
1530 service_loop = g_main_loop_new (service_context, FALSE);
1531 g_main_loop_run (service_loop);
1533 g_main_context_pop_thread_default (service_context);
1535 g_main_loop_unref (service_loop);
1536 g_main_context_unref (service_context);
1542 test_tcp_anonymous (void)
1544 gboolean seen_connection;
1545 GThread *service_thread;
1546 GDBusConnection *connection;
1549 seen_connection = FALSE;
1550 service_loop = NULL;
1551 service_thread = g_thread_new ("tcp-anon-service",
1552 tcp_anonymous_service_thread_func,
1554 while (service_loop == NULL)
1556 g_assert (server != NULL);
1559 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1560 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1561 NULL, /* GDBusAuthObserver* */
1562 NULL, /* GCancellable */
1564 g_assert_no_error (error);
1565 g_assert (connection != NULL);
1567 while (!seen_connection)
1570 g_object_unref (connection);
1572 g_main_loop_quit (service_loop);
1573 g_dbus_server_stop (server);
1574 g_object_unref (server);
1577 g_thread_join (service_thread);
1580 /* ---------------------------------------------------------------------------------------------------- */
1582 static GDBusServer *codegen_server = NULL;
1585 codegen_on_animal_poke (ExampleAnimal *animal,
1586 GDBusMethodInvocation *invocation,
1588 gboolean make_happy,
1591 if ((make_sad && make_happy) || (!make_sad && !make_happy))
1593 g_main_loop_quit (service_loop);
1595 g_dbus_method_invocation_return_dbus_error (invocation,
1596 "org.gtk.GDBus.Examples.ObjectManager.Error.Failed",
1597 "Exactly one of make_sad or make_happy must be TRUE");
1603 if (g_strcmp0 (example_animal_get_mood (animal), "Sad") == 0)
1605 g_dbus_method_invocation_return_dbus_error (invocation,
1606 "org.gtk.GDBus.Examples.ObjectManager.Error.SadAnimalIsSad",
1607 "Sad animal is already sad");
1611 example_animal_set_mood (animal, "Sad");
1612 example_animal_complete_poke (animal, invocation);
1618 if (g_strcmp0 (example_animal_get_mood (animal), "Happy") == 0)
1620 g_dbus_method_invocation_return_dbus_error (invocation,
1621 "org.gtk.GDBus.Examples.ObjectManager.Error.HappyAnimalIsHappy",
1622 "Happy animal is already happy");
1626 example_animal_set_mood (animal, "Happy");
1627 example_animal_complete_poke (animal, invocation);
1631 g_assert_not_reached ();
1634 return TRUE; /* to indicate that the method was handled */
1637 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1639 codegen_on_new_connection (GDBusServer *server,
1640 GDBusConnection *connection,
1643 ExampleAnimal *animal = user_data;
1644 GError *error = NULL;
1646 /* g_print ("Client connected.\n" */
1647 /* "Negotiated capabilities: unix-fd-passing=%d\n", */
1648 /* g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING); */
1650 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (animal), connection,
1651 "/Example/Animals/000", &error);
1652 g_assert_no_error (error);
1658 codegen_service_thread_func (gpointer user_data)
1660 GMainContext *service_context;
1661 ExampleAnimal *animal;
1662 GError *error = NULL;
1664 service_context = g_main_context_new ();
1665 g_main_context_push_thread_default (service_context);
1667 /* Create the animal in the right thread context */
1668 animal = example_animal_skeleton_new ();
1670 /* Handle Poke() D-Bus method invocations on the .Animal interface */
1671 g_signal_connect (animal, "handle-poke",
1672 G_CALLBACK (codegen_on_animal_poke),
1673 NULL); /* user_data */
1675 codegen_server = g_dbus_server_new_sync (tmp_address,
1676 G_DBUS_SERVER_FLAGS_NONE,
1678 NULL, /* observer */
1679 NULL, /* cancellable */
1681 g_assert_no_error (error);
1682 g_dbus_server_start (codegen_server);
1684 g_signal_connect (codegen_server, "new-connection",
1685 G_CALLBACK (codegen_on_new_connection),
1688 service_loop = g_main_loop_new (service_context, FALSE);
1689 g_main_loop_run (service_loop);
1691 g_object_unref (animal);
1693 g_main_context_pop_thread_default (service_context);
1695 g_main_loop_unref (service_loop);
1696 g_main_context_unref (service_context);
1698 g_dbus_server_stop (codegen_server);
1699 g_object_unref (codegen_server);
1700 codegen_server = NULL;
1707 codegen_quit_mainloop_timeout (gpointer data)
1709 g_main_loop_quit (loop);
1714 codegen_test_peer (void)
1716 GDBusConnection *connection;
1717 ExampleAnimal *animal1, *animal2;
1718 GThread *service_thread;
1719 GError *error = NULL;
1722 /* bring up a server - we run the server in a different thread to avoid deadlocks */
1723 service_loop = NULL;
1724 service_thread = g_thread_new ("codegen_test_peer",
1725 codegen_service_thread_func,
1727 while (service_loop == NULL)
1729 g_assert (codegen_server != NULL);
1731 /* Get an animal 1 ... */
1732 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (codegen_server),
1733 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1734 NULL, /* GDBusAuthObserver */
1735 NULL, /* cancellable */
1737 g_assert_no_error (error);
1738 g_assert (connection != NULL);
1740 animal1 = example_animal_proxy_new_sync (connection, 0, NULL,
1741 "/Example/Animals/000", NULL, &error);
1742 g_assert_no_error (error);
1743 g_assert (animal1 != NULL);
1744 g_object_unref (connection);
1746 /* Get animal 2 ... */
1747 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (codegen_server),
1748 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1749 NULL, /* GDBusAuthObserver */
1750 NULL, /* cancellable */
1752 g_assert_no_error (error);
1753 g_assert (connection != NULL);
1755 animal2 = example_animal_proxy_new_sync (connection, 0, NULL,
1756 "/Example/Animals/000", NULL, &error);
1757 g_assert_no_error (error);
1758 g_assert (animal2 != NULL);
1759 g_object_unref (connection);
1761 /* Make animal sad via animal1 */
1762 example_animal_call_poke_sync (animal1, TRUE, FALSE, 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 (animal1),
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 (100, codegen_quit_mainloop_timeout, NULL);
1776 g_main_loop_run (loop);
1778 /* Assert animals are sad */
1779 g_assert_cmpstr (example_animal_get_mood (animal1), ==, "Sad");
1780 g_assert_cmpstr (example_animal_get_mood (animal2), ==, "Sad");
1782 /* Make animal happy via animal2 */
1783 example_animal_call_poke_sync (animal2, FALSE, TRUE, NULL, &error);
1784 g_assert_no_error (error);
1786 /* Poke server and make sure animal is updated */
1787 value = g_dbus_proxy_call_sync (G_DBUS_PROXY (animal2),
1788 "org.freedesktop.DBus.Peer.Ping",
1789 NULL, G_DBUS_CALL_FLAGS_NONE, -1,
1791 g_assert_no_error (error);
1792 g_assert (value != NULL);
1793 g_variant_unref (value);
1795 /* Give the proxies a chance to refresh in the defaul main loop */
1796 g_timeout_add (1000, codegen_quit_mainloop_timeout, NULL);
1797 g_main_loop_run (loop);
1799 /* Assert animals are happy */
1800 g_assert_cmpstr (example_animal_get_mood (animal1), ==, "Happy");
1801 g_assert_cmpstr (example_animal_get_mood (animal2), ==, "Happy");
1803 /* This final call making the animal happy and sad will cause
1804 * the server to quit, when the server quits we dont get property
1805 * change notifications anyway because those are done from an idle handler
1807 example_animal_call_poke_sync (animal2, TRUE, TRUE, NULL, &error);
1809 g_object_unref (animal1);
1810 g_object_unref (animal2);
1811 g_thread_join (service_thread);
1814 /* ---------------------------------------------------------------------------------------------------- */
1822 GDBusNodeInfo *introspection_data = NULL;
1823 gchar *tmpdir = NULL;
1825 g_test_init (&argc, &argv, NULL);
1827 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1828 g_assert (introspection_data != NULL);
1829 test_interface_introspection_data = introspection_data->interfaces[0];
1831 test_guid = g_dbus_generate_guid ();
1835 if (g_unix_socket_address_abstract_names_supported ())
1836 tmp_address = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
1839 tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
1840 tmp_address = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
1844 tmp_address = g_strdup ("nonce-tcp:");
1846 /* all the tests rely on a shared main loop */
1847 loop = g_main_loop_new (NULL, FALSE);
1849 g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1850 g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1851 g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1852 g_test_add_func ("/gdbus/tcp-anonymous", test_tcp_anonymous);
1853 g_test_add_func ("/gdbus/credentials", test_credentials);
1854 g_test_add_func ("/gdbus/overflow", test_overflow);
1855 g_test_add_func ("/gdbus/codegen-peer-to-peer", codegen_test_peer);
1859 g_main_loop_unref (loop);
1861 g_dbus_node_info_unref (introspection_data);
1863 g_free (tmp_address);