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 GMainLoop *service_loop = NULL;
70 static GDBusServer *server = NULL;
71 static GMainLoop *loop = NULL;
73 /* ---------------------------------------------------------------------------------------------------- */
74 /* Test that peer-to-peer connections work */
75 /* ---------------------------------------------------------------------------------------------------- */
80 gboolean accept_connection;
81 gint num_connection_attempts;
82 GPtrArray *current_connections;
83 guint num_method_calls;
84 gboolean signal_received;
87 static const gchar *test_interface_introspection_xml =
89 " <interface name='org.gtk.GDBus.PeerTestInterface'>"
90 " <method name='HelloPeer'>"
91 " <arg type='s' name='greeting' direction='in'/>"
92 " <arg type='s' name='response' direction='out'/>"
94 " <method name='EmitSignal'/>"
95 " <method name='EmitSignalWithNameSet'/>"
96 " <method name='OpenFile'>"
97 " <arg type='s' name='path' direction='in'/>"
99 " <signal name='PeerSignal'>"
100 " <arg type='s' name='a_string'/>"
102 " <property type='s' name='PeerProperty' access='read'/>"
105 static GDBusInterfaceInfo *test_interface_introspection_data = NULL;
108 test_interface_method_call (GDBusConnection *connection,
110 const gchar *object_path,
111 const gchar *interface_name,
112 const gchar *method_name,
113 GVariant *parameters,
114 GDBusMethodInvocation *invocation,
117 PeerData *data = user_data;
118 const GDBusMethodInfo *info;
120 data->num_method_calls++;
122 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
123 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
125 info = g_dbus_method_invocation_get_method_info (invocation);
126 g_assert_cmpstr (info->name, ==, method_name);
128 if (g_strcmp0 (method_name, "HelloPeer") == 0)
130 const gchar *greeting;
133 g_variant_get (parameters, "(&s)", &greeting);
135 response = g_strdup_printf ("You greeted me with '%s'.",
137 g_dbus_method_invocation_return_value (invocation,
138 g_variant_new ("(s)", response));
141 else if (g_strcmp0 (method_name, "EmitSignal") == 0)
146 g_dbus_connection_emit_signal (connection,
148 "/org/gtk/GDBus/PeerTestObject",
149 "org.gtk.GDBus.PeerTestInterface",
153 g_assert_no_error (error);
154 g_dbus_method_invocation_return_value (invocation, NULL);
156 else if (g_strcmp0 (method_name, "EmitSignalWithNameSet") == 0)
160 GDBusMessage *message;
162 message = g_dbus_message_new_signal ("/org/gtk/GDBus/PeerTestObject",
163 "org.gtk.GDBus.PeerTestInterface",
164 "PeerSignalWithNameSet");
165 g_dbus_message_set_sender (message, ":1.42");
168 ret = g_dbus_connection_send_message (connection, message, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error);
169 g_assert_no_error (error);
171 g_object_unref (message);
173 g_dbus_method_invocation_return_value (invocation, NULL);
175 else if (g_strcmp0 (method_name, "OpenFile") == 0)
182 GUnixFDList *fd_list;
184 g_variant_get (parameters, "(&s)", &path);
186 fd_list = g_unix_fd_list_new ();
190 fd = g_open (path, O_RDONLY, 0);
192 g_unix_fd_list_append (fd_list, fd, &error);
193 g_assert_no_error (error);
196 reply = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
197 g_dbus_message_set_unix_fd_list (reply, fd_list);
198 g_object_unref (fd_list);
199 g_object_unref (invocation);
202 g_dbus_connection_send_message (connection,
204 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
205 NULL, /* out_serial */
207 g_assert_no_error (error);
208 g_object_unref (reply);
210 g_dbus_method_invocation_return_dbus_error (invocation,
211 "org.gtk.GDBus.NotOnUnix",
212 "Your OS does not support file descriptor passing");
217 g_assert_not_reached ();
222 test_interface_get_property (GDBusConnection *connection,
224 const gchar *object_path,
225 const gchar *interface_name,
226 const gchar *property_name,
230 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
231 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
232 g_assert_cmpstr (property_name, ==, "PeerProperty");
234 return g_variant_new_string ("ThePropertyValue");
238 static const GDBusInterfaceVTable test_interface_vtable =
240 test_interface_method_call,
241 test_interface_get_property,
242 NULL /* set_property */
246 on_proxy_signal_received (GDBusProxy *proxy,
249 GVariant *parameters,
252 PeerData *data = user_data;
254 data->signal_received = TRUE;
256 g_assert (sender_name == NULL);
257 g_assert_cmpstr (signal_name, ==, "PeerSignal");
258 g_main_loop_quit (loop);
262 on_proxy_signal_received_with_name_set (GDBusProxy *proxy,
265 GVariant *parameters,
268 PeerData *data = user_data;
270 data->signal_received = TRUE;
272 g_assert_cmpstr (sender_name, ==, ":1.42");
273 g_assert_cmpstr (signal_name, ==, "PeerSignalWithNameSet");
274 g_main_loop_quit (loop);
277 /* ---------------------------------------------------------------------------------------------------- */
280 on_authorize_authenticated_peer (GDBusAuthObserver *observer,
282 GCredentials *credentials,
285 PeerData *data = user_data;
288 data->num_connection_attempts++;
291 if (!data->accept_connection)
294 g_main_loop_quit (loop);
300 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
302 on_new_connection (GDBusServer *server,
303 GDBusConnection *connection,
306 PeerData *data = user_data;
310 //g_print ("Client connected.\n"
311 // "Negotiated capabilities: unix-fd-passing=%d\n",
312 // g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
314 g_ptr_array_add (data->current_connections, g_object_ref (connection));
316 #ifdef SHOULD_HAVE_CREDENTIALS_PASSING
318 GCredentials *credentials;
320 credentials = g_dbus_connection_get_peer_credentials (connection);
322 g_assert (credentials != NULL);
323 g_assert_cmpuint (g_credentials_get_unix_user (credentials, NULL), ==,
325 g_assert_cmpuint (g_credentials_get_unix_pid (credentials, NULL), ==,
330 /* export object on the newly established connection */
332 reg_id = g_dbus_connection_register_object (connection,
333 "/org/gtk/GDBus/PeerTestObject",
334 test_interface_introspection_data,
335 &test_interface_vtable,
337 NULL, /* GDestroyNotify for data */
339 g_assert_no_error (error);
340 g_assert (reg_id > 0);
342 g_main_loop_quit (loop);
348 service_thread_func (gpointer user_data)
350 PeerData *data = user_data;
351 GMainContext *service_context;
352 GDBusAuthObserver *observer, *o;
358 service_context = g_main_context_new ();
359 g_main_context_push_thread_default (service_context);
362 observer = g_dbus_auth_observer_new ();
363 server = g_dbus_server_new_sync (tmp_address,
364 G_DBUS_SERVER_FLAGS_NONE,
367 NULL, /* cancellable */
369 g_assert_no_error (error);
371 g_signal_connect (server,
373 G_CALLBACK (on_new_connection),
375 g_signal_connect (observer,
376 "authorize-authenticated-peer",
377 G_CALLBACK (on_authorize_authenticated_peer),
380 g_assert_cmpint (g_dbus_server_get_flags (server), ==, G_DBUS_SERVER_FLAGS_NONE);
381 g_assert_cmpstr (g_dbus_server_get_guid (server), ==, test_guid);
382 g_object_get (server,
387 "authentication-observer", &o,
389 g_assert_cmpint (f, ==, G_DBUS_SERVER_FLAGS_NONE);
390 g_assert_cmpstr (a, ==, tmp_address);
391 g_assert_cmpstr (g, ==, test_guid);
393 g_assert (o == observer);
398 g_object_unref (observer);
400 g_dbus_server_start (server);
402 service_loop = g_main_loop_new (service_context, FALSE);
403 g_main_loop_run (service_loop);
405 g_main_context_pop_thread_default (service_context);
407 g_main_loop_unref (service_loop);
408 g_main_context_unref (service_context);
410 /* test code specifically unrefs the server - see below */
411 g_assert (server == NULL);
418 on_incoming_connection (GSocketService *service,
419 GSocketConnection *socket_connection,
420 GObject *source_object,
423 PeerData *data = user_data;
425 if (data->accept_connection)
429 GDBusConnection *connection;
432 connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
434 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
435 NULL, /* cancellable */
437 g_assert_no_error (error);
439 g_ptr_array_add (data->current_connections, connection);
441 /* export object on the newly established connection */
443 reg_id = g_dbus_connection_register_object (connection,
444 "/org/gtk/GDBus/PeerTestObject",
445 &test_interface_introspection_data,
446 &test_interface_vtable,
448 NULL, /* GDestroyNotify for data */
450 g_assert_no_error (error);
451 g_assert (reg_id > 0);
456 /* don't do anything */
459 data->num_connection_attempts++;
461 g_main_loop_quit (loop);
463 /* stops other signal handlers from being invoked */
468 service_thread_func (gpointer data)
470 GMainContext *service_context;
472 GSocketAddress *address;
475 service_context = g_main_context_new ();
476 g_main_context_push_thread_default (service_context);
478 socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
479 address = g_unix_socket_address_new (socket_path);
481 service = g_socket_service_new ();
483 g_socket_listener_add_address (G_SOCKET_LISTENER (service),
485 G_SOCKET_TYPE_STREAM,
486 G_SOCKET_PROTOCOL_DEFAULT,
487 NULL, /* source_object */
488 NULL, /* effective_address */
490 g_assert_no_error (error);
491 g_signal_connect (service,
493 G_CALLBACK (on_incoming_connection),
495 g_socket_service_start (service);
497 service_loop = g_main_loop_new (service_context, FALSE);
498 g_main_loop_run (service_loop);
500 g_main_context_pop_thread_default (service_context);
502 g_main_loop_unref (service_loop);
503 g_main_context_unref (service_context);
505 g_object_unref (address);
506 g_free (socket_path);
511 /* ---------------------------------------------------------------------------------------------------- */
515 check_connection (gpointer user_data)
517 PeerData *data = user_data;
520 for (n = 0; n < data->current_connections->len; n++)
525 c = G_DBUS_CONNECTION (data->current_connections->pdata[n]);
526 stream = g_dbus_connection_get_stream (c);
528 g_debug ("In check_connection for %d: connection %p, stream %p", n, c, stream);
529 g_debug ("closed = %d", g_io_stream_is_closed (stream));
532 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
533 g_debug ("socket_closed = %d", g_socket_is_closed (socket));
534 g_debug ("socket_condition_check = %d", g_socket_condition_check (socket, G_IO_IN|G_IO_OUT|G_IO_ERR|G_IO_HUP));
540 num_read = g_input_stream_read (g_io_stream_get_input_stream (stream),
547 g_debug ("error: %s", error->message);
548 g_error_free (error);
552 g_debug ("no error, read %d bytes", (gint) num_read);
560 on_do_disconnect_in_idle (gpointer data)
562 GDBusConnection *c = G_DBUS_CONNECTION (data);
563 g_debug ("GDC %p has ref_count %d", c, G_OBJECT (c)->ref_count);
564 g_dbus_connection_disconnect (c);
572 read_all_from_fd (gint fd, gsize *out_len, GError **error)
578 str = g_string_new (NULL);
582 num_read = read (fd, buf, sizeof (buf));
585 if (errno == EAGAIN || errno == EWOULDBLOCK)
589 g_io_error_from_errno (errno),
590 "Failed reading %d bytes into offset %d: %s",
596 else if (num_read > 0)
598 g_string_append_len (str, buf, num_read);
600 else if (num_read == 0)
609 return g_string_free (str, FALSE);
614 g_string_free (str, TRUE);
630 GThread *service_thread;
631 gulong signal_handler_id;
633 memset (&data, '\0', sizeof (PeerData));
634 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
636 /* first try to connect when there is no server */
638 c = g_dbus_connection_new_for_address_sync (is_unix ? "unix:path=/tmp/gdbus-test-does-not-exist-pid" :
639 /* NOTE: Even if something is listening on port 12345 the connection
640 * will fail because the nonce file doesn't exist */
641 "nonce-tcp:host=localhost,port=12345,noncefile=this-does-not-exist-gdbus",
642 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
643 NULL, /* GDBusAuthObserver */
644 NULL, /* cancellable */
646 _g_assert_error_domain (error, G_IO_ERROR);
647 g_assert (!g_dbus_error_is_remote_error (error));
648 g_clear_error (&error);
649 g_assert (c == NULL);
651 /* bring up a server - we run the server in a different thread to avoid deadlocks */
653 service_thread = g_thread_new ("test_peer",
656 while (service_loop == NULL)
658 g_assert (server != NULL);
660 /* bring up a connection and accept it */
661 data.accept_connection = TRUE;
663 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
664 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
665 NULL, /* GDBusAuthObserver */
666 NULL, /* cancellable */
668 g_assert_no_error (error);
669 g_assert (c != NULL);
670 while (data.current_connections->len < 1)
671 g_main_loop_run (loop);
672 g_assert_cmpint (data.current_connections->len, ==, 1);
673 g_assert_cmpint (data.num_connection_attempts, ==, 1);
674 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
675 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
677 /* check that we create a proxy, read properties, receive signals and invoke
678 * the HelloPeer() method. Since the server runs in another thread it's fine
679 * to use synchronous blocking API here.
682 proxy = g_dbus_proxy_new_sync (c,
683 G_DBUS_PROXY_FLAGS_NONE,
686 "/org/gtk/GDBus/PeerTestObject",
687 "org.gtk.GDBus.PeerTestInterface",
688 NULL, /* GCancellable */
690 g_assert_no_error (error);
691 g_assert (proxy != NULL);
693 value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
694 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
696 /* try invoking a method */
698 result = g_dbus_proxy_call_sync (proxy,
700 g_variant_new ("(s)", "Hey Peer!"),
701 G_DBUS_CALL_FLAGS_NONE,
703 NULL, /* GCancellable */
705 g_assert_no_error (error);
706 g_variant_get (result, "(&s)", &s);
707 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
708 g_variant_unref (result);
709 g_assert_cmpint (data.num_method_calls, ==, 1);
711 /* make the other peer emit a signal - catch it */
712 signal_handler_id = g_signal_connect (proxy,
714 G_CALLBACK (on_proxy_signal_received),
716 g_assert (!data.signal_received);
717 g_dbus_proxy_call (proxy,
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, ==, 2);
728 g_signal_handler_disconnect (proxy, signal_handler_id);
730 /* Also ensure that messages with the sender header-field set gets
731 * delivered to the proxy - note that this doesn't really make sense
732 * e.g. names are meaning-less in a peer-to-peer case... but we
733 * support it because it makes sense in certain bridging
734 * applications - see e.g. #623815.
736 signal_handler_id = g_signal_connect (proxy,
738 G_CALLBACK (on_proxy_signal_received_with_name_set),
740 data.signal_received = FALSE;
741 g_dbus_proxy_call (proxy,
742 "EmitSignalWithNameSet",
743 NULL, /* no arguments */
744 G_DBUS_CALL_FLAGS_NONE,
746 NULL, /* GCancellable */
747 NULL, /* GAsyncReadyCallback - we don't care about the result */
748 NULL); /* user_data */
749 g_main_loop_run (loop);
750 g_assert (data.signal_received);
751 g_assert_cmpint (data.num_method_calls, ==, 3);
752 g_signal_handler_disconnect (proxy, signal_handler_id);
754 /* check for UNIX fd passing */
757 GDBusMessage *method_call_message;
758 GDBusMessage *method_reply_message;
759 GUnixFDList *fd_list;
765 char *testfile = g_build_filename (datapath, "file.c", NULL);
767 method_call_message = g_dbus_message_new_method_call (NULL, /* name */
768 "/org/gtk/GDBus/PeerTestObject",
769 "org.gtk.GDBus.PeerTestInterface",
771 g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", testfile));
773 method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
775 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
777 NULL, /* out_serial */
778 NULL, /* cancellable */
780 g_assert_no_error (error);
781 g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
782 fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
783 g_assert (fd_list != NULL);
784 g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
786 fd = g_unix_fd_list_get (fd_list, 0, &error);
787 g_assert_no_error (error);
788 g_object_unref (method_call_message);
789 g_object_unref (method_reply_message);
793 buf = read_all_from_fd (fd, &len, &error);
794 g_assert_no_error (error);
795 g_assert (buf != NULL);
799 g_file_get_contents (testfile,
803 g_assert_no_error (error);
804 g_assert_cmpint (len, ==, len2);
805 g_assert (memcmp (buf, buf2, len) == 0);
812 result = g_dbus_proxy_call_sync (proxy,
814 g_variant_new ("(s)", "boo"),
815 G_DBUS_CALL_FLAGS_NONE,
817 NULL, /* GCancellable */
819 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
820 g_assert (result == NULL);
821 g_error_free (error);
822 #endif /* G_OS_UNIX */
824 /* Check that g_socket_get_credentials() work - this really should
825 * be in a GSocket-specific test suite but no such test suite exists
830 GCredentials *credentials;
831 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (g_dbus_connection_get_stream (c)));
832 g_assert (G_IS_SOCKET (socket));
834 credentials = g_socket_get_credentials (socket, &error);
837 struct ucred *native_creds;
838 g_assert_no_error (error);
839 g_assert (G_IS_CREDENTIALS (credentials));
840 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED);
841 g_assert (native_creds != NULL);
842 g_assert (native_creds->uid == getuid ());
843 g_assert (native_creds->gid == getgid ());
844 g_assert (native_creds->pid == getpid ());
846 g_object_unref (credentials);
847 #elif defined (__OpenBSD__)
849 struct sockpeercred *native_creds;
850 g_assert_no_error (error);
851 g_assert (G_IS_CREDENTIALS (credentials));
852 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED);
853 g_assert (native_creds != NULL);
854 g_assert (native_creds->uid == getuid ());
855 g_assert (native_creds->gid == getgid ());
856 g_assert (native_creds->pid == getpid ());
858 g_object_unref (credentials);
860 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
861 g_assert (credentials == NULL);
866 /* bring up a connection - don't accept it - this should fail
868 data.accept_connection = FALSE;
870 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
871 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
872 NULL, /* GDBusAuthObserver */
873 NULL, /* cancellable */
875 _g_assert_error_domain (error, G_IO_ERROR);
876 g_error_free (error);
877 g_assert (c2 == NULL);
880 /* TODO: THIS TEST DOESN'T WORK YET */
882 /* bring up a connection - accept it.. then disconnect from the client side - check
883 * that the server side gets the disconnect signal.
886 data.accept_connection = TRUE;
887 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
888 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
889 NULL, /* GDBusAuthObserver */
890 NULL, /* cancellable */
892 g_assert_no_error (error);
893 g_assert (c2 != NULL);
894 g_assert (!g_dbus_connection_get_is_disconnected (c2));
895 while (data.num_connection_attempts < 3)
896 g_main_loop_run (loop);
897 g_assert_cmpint (data.current_connections->len, ==, 2);
898 g_assert_cmpint (data.num_connection_attempts, ==, 3);
899 g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
900 g_idle_add (on_do_disconnect_in_idle, c2);
901 g_debug ("==================================================");
902 g_debug ("==================================================");
903 g_debug ("==================================================");
904 g_debug ("waiting for disconnect on connection %p, stream %p",
905 data.current_connections->pdata[1],
906 g_dbus_connection_get_stream (data.current_connections->pdata[1]));
908 g_timeout_add (2000, check_connection, &data);
909 //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
910 g_main_loop_run (loop);
911 g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
912 g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
915 /* unref the server and stop listening for new connections
917 * This won't bring down the established connections - check that c is still connected
918 * by invoking a method
920 //g_socket_service_stop (service);
921 //g_object_unref (service);
922 g_dbus_server_stop (server);
923 g_object_unref (server);
927 result = g_dbus_proxy_call_sync (proxy,
929 g_variant_new ("(s)", "Hey Again Peer!"),
930 G_DBUS_CALL_FLAGS_NONE,
932 NULL, /* GCancellable */
934 g_assert_no_error (error);
935 g_variant_get (result, "(&s)", &s);
936 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
937 g_variant_unref (result);
938 g_assert_cmpint (data.num_method_calls, ==, 5);
941 /* TODO: THIS TEST DOESN'T WORK YET */
943 /* now disconnect from the server side - check that the client side gets the signal */
944 g_assert_cmpint (data.current_connections->len, ==, 1);
945 g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
946 g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
947 if (!g_dbus_connection_get_is_disconnected (c))
948 _g_assert_signal_received (c, "closed");
949 g_assert (g_dbus_connection_get_is_disconnected (c));
953 g_ptr_array_unref (data.current_connections);
954 g_object_unref (proxy);
956 g_main_loop_quit (service_loop);
957 g_thread_join (service_thread);
960 /* ---------------------------------------------------------------------------------------------------- */
965 GMainContext *context;
972 dmp_data_free (DmpData *data)
974 g_main_loop_unref (data->loop);
975 g_main_context_unref (data->context);
976 g_object_unref (data->server);
977 g_list_free_full (data->connections, g_object_unref);
982 dmp_on_method_call (GDBusConnection *connection,
984 const gchar *object_path,
985 const gchar *interface_name,
986 const gchar *method_name,
987 GVariant *parameters,
988 GDBusMethodInvocation *invocation,
991 //DmpData *data = user_data;
994 g_variant_get (parameters,
998 g_dbus_method_invocation_return_value (invocation,
999 g_variant_new ("(i)", first + second));
1002 static const GDBusInterfaceVTable dmp_interface_vtable =
1005 NULL, /* get_property */
1006 NULL /* set_property */
1010 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1012 dmp_on_new_connection (GDBusServer *server,
1013 GDBusConnection *connection,
1016 DmpData *data = user_data;
1017 GDBusNodeInfo *node;
1020 /* accept the connection */
1021 data->connections = g_list_prepend (data->connections, g_object_ref (connection));
1024 node = g_dbus_node_info_new_for_xml ("<node>"
1025 " <interface name='org.gtk.GDBus.DmpInterface'>"
1026 " <method name='AddPair'>"
1027 " <arg type='i' name='first' direction='in'/>"
1028 " <arg type='i' name='second' direction='in'/>"
1029 " <arg type='i' name='sum' direction='out'/>"
1034 g_assert_no_error (error);
1036 /* sleep 100ms before exporting an object - this is to test that
1037 * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
1038 * (GDBusServer uses this feature).
1040 usleep (100 * 1000);
1042 /* export an object */
1044 g_dbus_connection_register_object (connection,
1046 node->interfaces[0],
1047 &dmp_interface_vtable,
1051 g_dbus_node_info_unref (node);
1057 dmp_thread_func (gpointer user_data)
1059 DmpData *data = user_data;
1063 data->context = g_main_context_new ();
1064 g_main_context_push_thread_default (data->context);
1067 guid = g_dbus_generate_guid ();
1068 data->server = g_dbus_server_new_sync (tmp_address,
1069 G_DBUS_SERVER_FLAGS_NONE,
1071 NULL, /* GDBusAuthObserver */
1072 NULL, /* GCancellable */
1074 g_assert_no_error (error);
1075 g_signal_connect (data->server,
1077 G_CALLBACK (dmp_on_new_connection),
1080 g_dbus_server_start (data->server);
1082 data->loop = g_main_loop_new (data->context, FALSE);
1083 g_main_loop_run (data->loop);
1085 g_main_context_pop_thread_default (data->context);
1092 delayed_message_processing (void)
1096 GThread *service_thread;
1099 data = g_new0 (DmpData, 1);
1101 service_thread = g_thread_new ("dmp",
1104 while (data->server == NULL || !g_dbus_server_is_active (data->server))
1107 for (n = 0; n < 5; n++)
1114 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
1115 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1116 NULL, /* GDBusAuthObserver */
1117 NULL, /* GCancellable */
1119 g_assert_no_error (error);
1122 res = g_dbus_connection_call_sync (c,
1123 NULL, /* bus name */
1125 "org.gtk.GDBus.DmpInterface",
1127 g_variant_new ("(ii)", 2, n),
1128 G_VARIANT_TYPE ("(i)"),
1129 G_DBUS_CALL_FLAGS_NONE,
1130 -1, /* timeout_msec */
1131 NULL, /* GCancellable */
1133 g_assert_no_error (error);
1134 g_variant_get (res, "(i)", &val);
1135 g_assert_cmpint (val, ==, 2 + n);
1136 g_variant_unref (res);
1140 g_main_loop_quit (data->loop);
1141 g_thread_join (service_thread);
1142 dmp_data_free (data);
1145 /* ---------------------------------------------------------------------------------------------------- */
1148 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1150 GCredentials *credentials,
1153 PeerData *data = user_data;
1154 gboolean authorized;
1156 data->num_connection_attempts++;
1159 if (!data->accept_connection)
1162 g_main_loop_quit (loop);
1168 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1170 nonce_tcp_on_new_connection (GDBusServer *server,
1171 GDBusConnection *connection,
1174 PeerData *data = user_data;
1176 g_ptr_array_add (data->current_connections, g_object_ref (connection));
1178 g_main_loop_quit (loop);
1184 nonce_tcp_service_thread_func (gpointer user_data)
1186 PeerData *data = user_data;
1187 GMainContext *service_context;
1188 GDBusAuthObserver *observer;
1191 service_context = g_main_context_new ();
1192 g_main_context_push_thread_default (service_context);
1195 observer = g_dbus_auth_observer_new ();
1196 server = g_dbus_server_new_sync ("nonce-tcp:",
1197 G_DBUS_SERVER_FLAGS_NONE,
1200 NULL, /* cancellable */
1202 g_assert_no_error (error);
1204 g_signal_connect (server,
1206 G_CALLBACK (nonce_tcp_on_new_connection),
1208 g_signal_connect (observer,
1209 "authorize-authenticated-peer",
1210 G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1212 g_object_unref (observer);
1214 g_dbus_server_start (server);
1216 service_loop = g_main_loop_new (service_context, FALSE);
1217 g_main_loop_run (service_loop);
1219 g_main_context_pop_thread_default (service_context);
1221 g_main_loop_unref (service_loop);
1222 g_main_context_unref (service_context);
1224 /* test code specifically unrefs the server - see below */
1225 g_assert (server == NULL);
1231 test_nonce_tcp (void)
1235 GThread *service_thread;
1240 const gchar *address;
1242 memset (&data, '\0', sizeof (PeerData));
1243 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1247 service_loop = NULL;
1248 service_thread = g_thread_new ("nonce-tcp-service",
1249 nonce_tcp_service_thread_func,
1251 while (service_loop == NULL)
1253 g_assert (server != NULL);
1256 /* bring up a connection and accept it */
1257 data.accept_connection = TRUE;
1259 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1260 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1261 NULL, /* GDBusAuthObserver */
1262 NULL, /* cancellable */
1264 g_assert_no_error (error);
1265 g_assert (c != NULL);
1266 while (data.current_connections->len < 1)
1268 g_assert_cmpint (data.current_connections->len, ==, 1);
1269 g_assert_cmpint (data.num_connection_attempts, ==, 1);
1270 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1271 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1274 /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1277 address = g_dbus_server_get_client_address (server);
1279 s = strstr (address, "noncefile=");
1280 g_assert (s != NULL);
1281 s += sizeof "noncefile=" - 1;
1282 nonce_file = g_strdup (s);
1284 /* First try invalid data in the nonce file - this will actually
1285 * make the client send this and the server will reject it. The way
1286 * it works is that if the nonce doesn't match, the server will
1287 * simply close the connection. So, from the client point of view,
1288 * we can see a variety of errors.
1291 res = g_file_set_contents (nonce_file,
1295 g_assert_no_error (error);
1297 c = g_dbus_connection_new_for_address_sync (address,
1298 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1299 NULL, /* GDBusAuthObserver */
1300 NULL, /* cancellable */
1302 _g_assert_error_domain (error, G_IO_ERROR);
1303 g_error_free (error);
1304 g_assert (c == NULL);
1306 /* Then try with a nonce-file of incorrect length - this will make
1307 * the client complain - we won't even try connecting to the server
1311 res = g_file_set_contents (nonce_file,
1312 "0123456789012345_",
1315 g_assert_no_error (error);
1317 c = g_dbus_connection_new_for_address_sync (address,
1318 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1319 NULL, /* GDBusAuthObserver */
1320 NULL, /* cancellable */
1322 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1323 g_error_free (error);
1324 g_assert (c == NULL);
1326 /* Finally try with no nonce-file at all */
1327 g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1329 c = g_dbus_connection_new_for_address_sync (address,
1330 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1331 NULL, /* GDBusAuthObserver */
1332 NULL, /* cancellable */
1334 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1335 g_error_free (error);
1336 g_assert (c == NULL);
1338 g_free (nonce_file);
1340 g_dbus_server_stop (server);
1341 g_object_unref (server);
1344 g_main_loop_quit (service_loop);
1345 g_thread_join (service_thread);
1349 test_credentials (void)
1351 GCredentials *c1, *c2;
1355 c1 = g_credentials_new ();
1356 c2 = g_credentials_new ();
1359 if (g_credentials_set_unix_user (c2, getuid (), &error))
1360 g_assert_no_error (error);
1362 g_clear_error (&error);
1363 g_assert (g_credentials_is_same_user (c1, c2, &error));
1364 g_assert_no_error (error);
1366 desc = g_credentials_to_string (c1);
1367 g_assert (desc != NULL);
1370 g_object_unref (c1);
1371 g_object_unref (c2);
1374 /* ---------------------------------------------------------------------------------------------------- */
1378 /* Chosen to be big enough to overflow the socket buffer */
1379 #define OVERFLOW_NUM_SIGNALS 5000
1380 #define OVERFLOW_TIMEOUT_SEC 10
1382 static GDBusMessage *
1383 overflow_filter_func (GDBusConnection *connection,
1384 GDBusMessage *message,
1388 volatile gint *counter = user_data;
1394 overflow_on_500ms_later_func (gpointer user_data)
1396 g_main_loop_quit (loop);
1397 return FALSE; /* don't keep the idle */
1401 test_overflow (void)
1406 GSocketConnection *socket_connection;
1407 GDBusConnection *producer, *consumer;
1410 volatile gint n_messages_received;
1411 volatile gint n_messages_sent;
1413 g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
1416 socket = g_socket_new_from_fd (sv[0], &error);
1417 g_assert_no_error (error);
1418 socket_connection = g_socket_connection_factory_create_connection (socket);
1419 g_assert (socket_connection != NULL);
1420 g_object_unref (socket);
1421 producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1423 G_DBUS_CONNECTION_FLAGS_NONE,
1424 NULL, /* GDBusAuthObserver */
1425 NULL, /* GCancellable */
1427 g_dbus_connection_set_exit_on_close (producer, TRUE);
1428 g_assert_no_error (error);
1429 g_object_unref (socket_connection);
1430 n_messages_sent = 0;
1431 g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
1433 /* send enough data that we get an EAGAIN */
1434 for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
1437 g_dbus_connection_emit_signal (producer,
1438 NULL, /* destination */
1440 "org.foo.Interface",
1442 g_variant_new ("(s)", "a string"),
1444 g_assert_no_error (error);
1447 /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
1448 * kernel buffers) and verify that n_messages_sent <
1449 * OVERFLOW_NUM_SIGNALS
1451 * This is to verify that not all the submitted messages have been
1452 * sent to the underlying transport.
1454 g_timeout_add (500, overflow_on_500ms_later_func, NULL);
1455 g_main_loop_run (loop);
1456 g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
1458 /* now suck it all out as a client, and add it up */
1459 socket = g_socket_new_from_fd (sv[1], &error);
1460 g_assert_no_error (error);
1461 socket_connection = g_socket_connection_factory_create_connection (socket);
1462 g_assert (socket_connection != NULL);
1463 g_object_unref (socket);
1464 consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1466 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
1467 NULL, /* GDBusAuthObserver */
1468 NULL, /* GCancellable */
1470 g_assert_no_error (error);
1471 g_object_unref (socket_connection);
1472 n_messages_received = 0;
1473 g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
1474 g_dbus_connection_start_message_processing (consumer);
1476 timer = g_timer_new ();
1477 g_timer_start (timer);
1479 while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
1480 g_main_context_iteration (NULL, FALSE);
1482 g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
1483 g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
1485 g_timer_destroy (timer);
1486 g_object_unref (consumer);
1487 g_object_unref (producer);
1491 test_overflow (void)
1493 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
1497 /* ---------------------------------------------------------------------------------------------------- */
1500 tcp_anonymous_on_new_connection (GDBusServer *server,
1501 GDBusConnection *connection,
1504 gboolean *seen_connection = user_data;
1505 *seen_connection = TRUE;
1510 tcp_anonymous_service_thread_func (gpointer user_data)
1512 gboolean *seen_connection = user_data;
1513 GMainContext *service_context;
1516 service_context = g_main_context_new ();
1517 g_main_context_push_thread_default (service_context);
1520 server = g_dbus_server_new_sync ("tcp:",
1521 G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS,
1523 NULL, /* GDBusObserver* */
1524 NULL, /* GCancellable* */
1526 g_assert_no_error (error);
1528 g_signal_connect (server,
1530 G_CALLBACK (tcp_anonymous_on_new_connection),
1533 g_dbus_server_start (server);
1535 service_loop = g_main_loop_new (service_context, FALSE);
1536 g_main_loop_run (service_loop);
1538 g_main_context_pop_thread_default (service_context);
1540 g_main_loop_unref (service_loop);
1541 g_main_context_unref (service_context);
1547 test_tcp_anonymous (void)
1549 gboolean seen_connection;
1550 GThread *service_thread;
1551 GDBusConnection *connection;
1554 seen_connection = FALSE;
1555 service_loop = NULL;
1556 service_thread = g_thread_new ("tcp-anon-service",
1557 tcp_anonymous_service_thread_func,
1559 while (service_loop == NULL)
1561 g_assert (server != NULL);
1564 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1565 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1566 NULL, /* GDBusAuthObserver* */
1567 NULL, /* GCancellable */
1569 g_assert_no_error (error);
1570 g_assert (connection != NULL);
1572 while (!seen_connection)
1575 g_object_unref (connection);
1577 g_main_loop_quit (service_loop);
1578 g_dbus_server_stop (server);
1579 g_object_unref (server);
1582 g_thread_join (service_thread);
1585 /* ---------------------------------------------------------------------------------------------------- */
1587 static GDBusServer *codegen_server = NULL;
1590 codegen_on_animal_poke (ExampleAnimal *animal,
1591 GDBusMethodInvocation *invocation,
1593 gboolean make_happy,
1596 if ((make_sad && make_happy) || (!make_sad && !make_happy))
1598 g_main_loop_quit (service_loop);
1600 g_dbus_method_invocation_return_dbus_error (invocation,
1601 "org.gtk.GDBus.Examples.ObjectManager.Error.Failed",
1602 "Exactly one of make_sad or make_happy must be TRUE");
1608 if (g_strcmp0 (example_animal_get_mood (animal), "Sad") == 0)
1610 g_dbus_method_invocation_return_dbus_error (invocation,
1611 "org.gtk.GDBus.Examples.ObjectManager.Error.SadAnimalIsSad",
1612 "Sad animal is already sad");
1616 example_animal_set_mood (animal, "Sad");
1617 example_animal_complete_poke (animal, invocation);
1623 if (g_strcmp0 (example_animal_get_mood (animal), "Happy") == 0)
1625 g_dbus_method_invocation_return_dbus_error (invocation,
1626 "org.gtk.GDBus.Examples.ObjectManager.Error.HappyAnimalIsHappy",
1627 "Happy animal is already happy");
1631 example_animal_set_mood (animal, "Happy");
1632 example_animal_complete_poke (animal, invocation);
1636 g_assert_not_reached ();
1639 return TRUE; /* to indicate that the method was handled */
1642 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1644 codegen_on_new_connection (GDBusServer *server,
1645 GDBusConnection *connection,
1648 ExampleAnimal *animal = user_data;
1649 GError *error = NULL;
1651 /* g_print ("Client connected.\n" */
1652 /* "Negotiated capabilities: unix-fd-passing=%d\n", */
1653 /* g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING); */
1655 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (animal), connection,
1656 "/Example/Animals/000", &error);
1657 g_assert_no_error (error);
1663 codegen_service_thread_func (gpointer user_data)
1665 GMainContext *service_context;
1666 ExampleAnimal *animal;
1667 GError *error = NULL;
1669 service_context = g_main_context_new ();
1670 g_main_context_push_thread_default (service_context);
1672 /* Create the animal in the right thread context */
1673 animal = example_animal_skeleton_new ();
1675 /* Handle Poke() D-Bus method invocations on the .Animal interface */
1676 g_signal_connect (animal, "handle-poke",
1677 G_CALLBACK (codegen_on_animal_poke),
1678 NULL); /* user_data */
1680 codegen_server = g_dbus_server_new_sync (tmp_address,
1681 G_DBUS_SERVER_FLAGS_NONE,
1683 NULL, /* observer */
1684 NULL, /* cancellable */
1686 g_assert_no_error (error);
1687 g_dbus_server_start (codegen_server);
1689 g_signal_connect (codegen_server, "new-connection",
1690 G_CALLBACK (codegen_on_new_connection),
1693 service_loop = g_main_loop_new (service_context, FALSE);
1694 g_main_loop_run (service_loop);
1696 g_object_unref (animal);
1698 g_main_context_pop_thread_default (service_context);
1700 g_main_loop_unref (service_loop);
1701 g_main_context_unref (service_context);
1703 g_dbus_server_stop (codegen_server);
1704 g_object_unref (codegen_server);
1705 codegen_server = NULL;
1712 codegen_quit_mainloop_timeout (gpointer data)
1714 g_main_loop_quit (loop);
1719 codegen_test_peer (void)
1721 GDBusConnection *connection;
1722 ExampleAnimal *animal1, *animal2;
1723 GThread *service_thread;
1724 GError *error = NULL;
1727 /* bring up a server - we run the server in a different thread to avoid deadlocks */
1728 service_loop = NULL;
1729 service_thread = g_thread_new ("codegen_test_peer",
1730 codegen_service_thread_func,
1732 while (service_loop == NULL)
1734 g_assert (codegen_server != NULL);
1736 /* Get an animal 1 ... */
1737 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (codegen_server),
1738 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1739 NULL, /* GDBusAuthObserver */
1740 NULL, /* cancellable */
1742 g_assert_no_error (error);
1743 g_assert (connection != NULL);
1745 animal1 = example_animal_proxy_new_sync (connection, 0, NULL,
1746 "/Example/Animals/000", NULL, &error);
1747 g_assert_no_error (error);
1748 g_assert (animal1 != NULL);
1749 g_object_unref (connection);
1751 /* Get animal 2 ... */
1752 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (codegen_server),
1753 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1754 NULL, /* GDBusAuthObserver */
1755 NULL, /* cancellable */
1757 g_assert_no_error (error);
1758 g_assert (connection != NULL);
1760 animal2 = example_animal_proxy_new_sync (connection, 0, NULL,
1761 "/Example/Animals/000", NULL, &error);
1762 g_assert_no_error (error);
1763 g_assert (animal2 != NULL);
1764 g_object_unref (connection);
1766 /* Make animal sad via animal1 */
1767 example_animal_call_poke_sync (animal1, TRUE, FALSE, NULL, &error);
1768 g_assert_no_error (error);
1770 /* Poke server and make sure animal is updated */
1771 value = g_dbus_proxy_call_sync (G_DBUS_PROXY (animal1),
1772 "org.freedesktop.DBus.Peer.Ping",
1773 NULL, G_DBUS_CALL_FLAGS_NONE, -1,
1775 g_assert_no_error (error);
1776 g_assert (value != NULL);
1777 g_variant_unref (value);
1779 /* Give the proxies a chance to refresh in the defaul main loop */
1780 g_timeout_add (100, codegen_quit_mainloop_timeout, NULL);
1781 g_main_loop_run (loop);
1783 /* Assert animals are sad */
1784 g_assert_cmpstr (example_animal_get_mood (animal1), ==, "Sad");
1785 g_assert_cmpstr (example_animal_get_mood (animal2), ==, "Sad");
1787 /* Make animal happy via animal2 */
1788 example_animal_call_poke_sync (animal2, FALSE, TRUE, NULL, &error);
1789 g_assert_no_error (error);
1791 /* Poke server and make sure animal is updated */
1792 value = g_dbus_proxy_call_sync (G_DBUS_PROXY (animal2),
1793 "org.freedesktop.DBus.Peer.Ping",
1794 NULL, G_DBUS_CALL_FLAGS_NONE, -1,
1796 g_assert_no_error (error);
1797 g_assert (value != NULL);
1798 g_variant_unref (value);
1800 /* Give the proxies a chance to refresh in the defaul main loop */
1801 g_timeout_add (1000, codegen_quit_mainloop_timeout, NULL);
1802 g_main_loop_run (loop);
1804 /* Assert animals are happy */
1805 g_assert_cmpstr (example_animal_get_mood (animal1), ==, "Happy");
1806 g_assert_cmpstr (example_animal_get_mood (animal2), ==, "Happy");
1808 /* This final call making the animal happy and sad will cause
1809 * the server to quit, when the server quits we dont get property
1810 * change notifications anyway because those are done from an idle handler
1812 example_animal_call_poke_sync (animal2, TRUE, TRUE, NULL, &error);
1814 g_object_unref (animal1);
1815 g_object_unref (animal2);
1816 g_thread_join (service_thread);
1819 /* ---------------------------------------------------------------------------------------------------- */
1827 GDBusNodeInfo *introspection_data = NULL;
1828 gchar *tmpdir = NULL;
1830 if (g_getenv ("G_TEST_DATA"))
1831 datapath = g_getenv ("G_TEST_DATA");
1835 g_test_init (&argc, &argv, NULL);
1837 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1838 g_assert (introspection_data != NULL);
1839 test_interface_introspection_data = introspection_data->interfaces[0];
1841 test_guid = g_dbus_generate_guid ();
1845 if (g_unix_socket_address_abstract_names_supported ())
1846 tmp_address = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
1849 tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
1850 tmp_address = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
1854 tmp_address = g_strdup ("nonce-tcp:");
1856 /* all the tests rely on a shared main loop */
1857 loop = g_main_loop_new (NULL, FALSE);
1859 g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1860 g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1861 g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1862 g_test_add_func ("/gdbus/tcp-anonymous", test_tcp_anonymous);
1863 g_test_add_func ("/gdbus/credentials", test_credentials);
1864 g_test_add_func ("/gdbus/overflow", test_overflow);
1865 g_test_add_func ("/gdbus/codegen-peer-to-peer", codegen_test_peer);
1869 g_main_loop_unref (loop);
1871 g_dbus_node_info_unref (introspection_data);
1873 g_free (tmp_address);