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.1 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, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
28 #include <sys/types.h>
34 #include <glib/gstdio.h>
36 #include <gio/gnetworking.h>
37 #include <gio/gunixsocketaddress.h>
38 #include <gio/gunixfdlist.h>
39 #include <gio/gcredentialsprivate.h>
42 #include <gio/gunixconnection.h>
46 #include "gdbus-tests.h"
48 #include "gdbus-object-manager-example/objectmanager-gen.h"
51 static gboolean is_unix = TRUE;
53 static gboolean is_unix = FALSE;
56 static gchar *tmpdir = NULL;
57 static gchar *tmp_address = NULL;
58 static gchar *test_guid = NULL;
59 static GMutex service_loop_lock;
60 static GCond service_loop_cond;
61 static GMainLoop *service_loop = NULL;
62 static GDBusServer *server = NULL;
63 static GMainLoop *loop = NULL;
65 /* ---------------------------------------------------------------------------------------------------- */
66 /* Test that peer-to-peer connections work */
67 /* ---------------------------------------------------------------------------------------------------- */
72 gboolean accept_connection;
73 gint num_connection_attempts;
74 GPtrArray *current_connections;
75 guint num_method_calls;
76 gboolean signal_received;
79 static const gchar *test_interface_introspection_xml =
81 " <interface name='org.gtk.GDBus.PeerTestInterface'>"
82 " <method name='HelloPeer'>"
83 " <arg type='s' name='greeting' direction='in'/>"
84 " <arg type='s' name='response' direction='out'/>"
86 " <method name='EmitSignal'/>"
87 " <method name='EmitSignalWithNameSet'/>"
88 " <method name='OpenFile'>"
89 " <arg type='s' name='path' direction='in'/>"
91 " <signal name='PeerSignal'>"
92 " <arg type='s' name='a_string'/>"
94 " <property type='s' name='PeerProperty' access='read'/>"
97 static GDBusInterfaceInfo *test_interface_introspection_data = NULL;
100 test_interface_method_call (GDBusConnection *connection,
102 const gchar *object_path,
103 const gchar *interface_name,
104 const gchar *method_name,
105 GVariant *parameters,
106 GDBusMethodInvocation *invocation,
109 PeerData *data = user_data;
110 const GDBusMethodInfo *info;
112 data->num_method_calls++;
114 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
115 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
117 info = g_dbus_method_invocation_get_method_info (invocation);
118 g_assert_cmpstr (info->name, ==, method_name);
120 if (g_strcmp0 (method_name, "HelloPeer") == 0)
122 const gchar *greeting;
125 g_variant_get (parameters, "(&s)", &greeting);
127 response = g_strdup_printf ("You greeted me with '%s'.",
129 g_dbus_method_invocation_return_value (invocation,
130 g_variant_new ("(s)", response));
133 else if (g_strcmp0 (method_name, "EmitSignal") == 0)
138 g_dbus_connection_emit_signal (connection,
140 "/org/gtk/GDBus/PeerTestObject",
141 "org.gtk.GDBus.PeerTestInterface",
145 g_assert_no_error (error);
146 g_dbus_method_invocation_return_value (invocation, NULL);
148 else if (g_strcmp0 (method_name, "EmitSignalWithNameSet") == 0)
152 GDBusMessage *message;
154 message = g_dbus_message_new_signal ("/org/gtk/GDBus/PeerTestObject",
155 "org.gtk.GDBus.PeerTestInterface",
156 "PeerSignalWithNameSet");
157 g_dbus_message_set_sender (message, ":1.42");
160 ret = g_dbus_connection_send_message (connection, message, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error);
161 g_assert_no_error (error);
163 g_object_unref (message);
165 g_dbus_method_invocation_return_value (invocation, NULL);
167 else if (g_strcmp0 (method_name, "OpenFile") == 0)
174 GUnixFDList *fd_list;
176 g_variant_get (parameters, "(&s)", &path);
178 fd_list = g_unix_fd_list_new ();
182 fd = g_open (path, O_RDONLY, 0);
184 g_unix_fd_list_append (fd_list, fd, &error);
185 g_assert_no_error (error);
188 reply = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
189 g_dbus_message_set_unix_fd_list (reply, fd_list);
190 g_object_unref (fd_list);
191 g_object_unref (invocation);
194 g_dbus_connection_send_message (connection,
196 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
197 NULL, /* out_serial */
199 g_assert_no_error (error);
200 g_object_unref (reply);
202 g_dbus_method_invocation_return_dbus_error (invocation,
203 "org.gtk.GDBus.NotOnUnix",
204 "Your OS does not support file descriptor passing");
209 g_assert_not_reached ();
214 test_interface_get_property (GDBusConnection *connection,
216 const gchar *object_path,
217 const gchar *interface_name,
218 const gchar *property_name,
222 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
223 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
224 g_assert_cmpstr (property_name, ==, "PeerProperty");
226 return g_variant_new_string ("ThePropertyValue");
230 static const GDBusInterfaceVTable test_interface_vtable =
232 test_interface_method_call,
233 test_interface_get_property,
234 NULL /* set_property */
238 on_proxy_signal_received (GDBusProxy *proxy,
241 GVariant *parameters,
244 PeerData *data = user_data;
246 data->signal_received = TRUE;
248 g_assert (sender_name == NULL);
249 g_assert_cmpstr (signal_name, ==, "PeerSignal");
250 g_main_loop_quit (loop);
254 on_proxy_signal_received_with_name_set (GDBusProxy *proxy,
257 GVariant *parameters,
260 PeerData *data = user_data;
262 data->signal_received = TRUE;
264 g_assert_cmpstr (sender_name, ==, ":1.42");
265 g_assert_cmpstr (signal_name, ==, "PeerSignalWithNameSet");
266 g_main_loop_quit (loop);
269 /* ---------------------------------------------------------------------------------------------------- */
272 setup_test_address (void)
276 g_test_message ("Testing with unix:tmpdir address");
277 if (g_unix_socket_address_abstract_names_supported ())
278 tmp_address = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
281 tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
282 tmp_address = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
286 tmp_address = g_strdup ("nonce-tcp:");
291 setup_dir_test_address (void)
293 g_test_message ("Testing with unix:dir address");
294 tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
295 tmp_address = g_strdup_printf ("unix:dir=%s", tmpdir);
299 setup_path_test_address (void)
301 g_test_message ("Testing with unix:path address");
302 tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
303 tmp_address = g_strdup_printf ("unix:path=%s/gdbus-peer-socket", tmpdir);
308 teardown_test_address (void)
310 g_free (tmp_address);
313 /* Ensuring the rmdir succeeds also ensures any sockets created on the
314 * filesystem are also deleted.
316 g_assert_cmpint (g_rmdir (tmpdir), ==, 0);
317 g_clear_pointer (&tmpdir, g_free);
321 /* ---------------------------------------------------------------------------------------------------- */
324 on_authorize_authenticated_peer (GDBusAuthObserver *observer,
326 GCredentials *credentials,
329 PeerData *data = user_data;
332 data->num_connection_attempts++;
335 if (!data->accept_connection)
338 g_main_loop_quit (loop);
344 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
346 on_new_connection (GDBusServer *server,
347 GDBusConnection *connection,
350 PeerData *data = user_data;
354 //g_printerr ("Client connected.\n"
355 // "Negotiated capabilities: unix-fd-passing=%d\n",
356 // g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
358 g_ptr_array_add (data->current_connections, g_object_ref (connection));
360 #if G_CREDENTIALS_SUPPORTED
362 GCredentials *credentials;
364 credentials = g_dbus_connection_get_peer_credentials (connection);
366 g_assert (credentials != NULL);
367 g_assert_cmpuint (g_credentials_get_unix_user (credentials, NULL), ==,
369 g_assert_cmpuint (g_credentials_get_unix_pid (credentials, NULL), ==,
374 /* export object on the newly established connection */
376 reg_id = g_dbus_connection_register_object (connection,
377 "/org/gtk/GDBus/PeerTestObject",
378 test_interface_introspection_data,
379 &test_interface_vtable,
381 NULL, /* GDestroyNotify for data */
383 g_assert_no_error (error);
384 g_assert (reg_id > 0);
386 g_main_loop_quit (loop);
391 /* We don't tell the main thread about the new GDBusServer until it has
392 * had a chance to start listening. */
394 idle_in_service_loop (gpointer loop)
396 g_assert (service_loop == NULL);
397 g_mutex_lock (&service_loop_lock);
399 g_cond_broadcast (&service_loop_cond);
400 g_mutex_unlock (&service_loop_lock);
402 return G_SOURCE_REMOVE;
406 run_service_loop (GMainContext *service_context)
411 g_assert (service_loop == NULL);
413 loop = g_main_loop_new (service_context, FALSE);
414 source = g_idle_source_new ();
415 g_source_set_callback (source, idle_in_service_loop, loop, NULL);
416 g_source_attach (source, service_context);
417 g_source_unref (source);
418 g_main_loop_run (loop);
422 teardown_service_loop (void)
424 g_mutex_lock (&service_loop_lock);
425 g_clear_pointer (&service_loop, g_main_loop_unref);
426 g_mutex_unlock (&service_loop_lock);
430 await_service_loop (void)
432 g_mutex_lock (&service_loop_lock);
433 while (service_loop == NULL)
434 g_cond_wait (&service_loop_cond, &service_loop_lock);
435 g_mutex_unlock (&service_loop_lock);
439 service_thread_func (gpointer user_data)
441 PeerData *data = user_data;
442 GMainContext *service_context;
443 GDBusAuthObserver *observer, *o;
449 service_context = g_main_context_new ();
450 g_main_context_push_thread_default (service_context);
453 observer = g_dbus_auth_observer_new ();
454 server = g_dbus_server_new_sync (tmp_address,
455 G_DBUS_SERVER_FLAGS_NONE,
458 NULL, /* cancellable */
460 g_assert_no_error (error);
462 g_signal_connect (server,
464 G_CALLBACK (on_new_connection),
466 g_signal_connect (observer,
467 "authorize-authenticated-peer",
468 G_CALLBACK (on_authorize_authenticated_peer),
471 g_assert_cmpint (g_dbus_server_get_flags (server), ==, G_DBUS_SERVER_FLAGS_NONE);
472 g_assert_cmpstr (g_dbus_server_get_guid (server), ==, test_guid);
473 g_object_get (server,
478 "authentication-observer", &o,
480 g_assert_cmpint (f, ==, G_DBUS_SERVER_FLAGS_NONE);
481 g_assert_cmpstr (a, ==, tmp_address);
482 g_assert_cmpstr (g, ==, test_guid);
484 g_assert (o == observer);
489 g_object_unref (observer);
491 g_dbus_server_start (server);
493 run_service_loop (service_context);
495 g_main_context_pop_thread_default (service_context);
497 teardown_service_loop ();
498 g_main_context_unref (service_context);
500 /* test code specifically unrefs the server - see below */
501 g_assert (server == NULL);
508 on_incoming_connection (GSocketService *service,
509 GSocketConnection *socket_connection,
510 GObject *source_object,
513 PeerData *data = user_data;
515 if (data->accept_connection)
519 GDBusConnection *connection;
522 connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
524 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
525 NULL, /* cancellable */
527 g_assert_no_error (error);
529 g_ptr_array_add (data->current_connections, connection);
531 /* export object on the newly established connection */
533 reg_id = g_dbus_connection_register_object (connection,
534 "/org/gtk/GDBus/PeerTestObject",
535 &test_interface_introspection_data,
536 &test_interface_vtable,
538 NULL, /* GDestroyNotify for data */
540 g_assert_no_error (error);
541 g_assert (reg_id > 0);
546 /* don't do anything */
549 data->num_connection_attempts++;
551 g_main_loop_quit (loop);
553 /* stops other signal handlers from being invoked */
558 service_thread_func (gpointer data)
560 GMainContext *service_context;
562 GSocketAddress *address;
565 service_context = g_main_context_new ();
566 g_main_context_push_thread_default (service_context);
568 socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
569 address = g_unix_socket_address_new (socket_path);
571 service = g_socket_service_new ();
573 g_socket_listener_add_address (G_SOCKET_LISTENER (service),
575 G_SOCKET_TYPE_STREAM,
576 G_SOCKET_PROTOCOL_DEFAULT,
577 NULL, /* source_object */
578 NULL, /* effective_address */
580 g_assert_no_error (error);
581 g_signal_connect (service,
583 G_CALLBACK (on_incoming_connection),
585 g_socket_service_start (service);
587 run_service_loop (service_context);
589 g_main_context_pop_thread_default (service_context);
591 teardown_service_loop ();
592 g_main_context_unref (service_context);
594 g_object_unref (address);
595 g_free (socket_path);
600 /* ---------------------------------------------------------------------------------------------------- */
604 check_connection (gpointer user_data)
606 PeerData *data = user_data;
609 for (n = 0; n < data->current_connections->len; n++)
614 c = G_DBUS_CONNECTION (data->current_connections->pdata[n]);
615 stream = g_dbus_connection_get_stream (c);
617 g_debug ("In check_connection for %d: connection %p, stream %p", n, c, stream);
618 g_debug ("closed = %d", g_io_stream_is_closed (stream));
621 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
622 g_debug ("socket_closed = %d", g_socket_is_closed (socket));
623 g_debug ("socket_condition_check = %d", g_socket_condition_check (socket, G_IO_IN|G_IO_OUT|G_IO_ERR|G_IO_HUP));
629 num_read = g_input_stream_read (g_io_stream_get_input_stream (stream),
636 g_debug ("error: %s", error->message);
637 g_error_free (error);
641 g_debug ("no error, read %d bytes", (gint) num_read);
649 on_do_disconnect_in_idle (gpointer data)
651 GDBusConnection *c = G_DBUS_CONNECTION (data);
652 g_debug ("GDC %p has ref_count %d", c, G_OBJECT (c)->ref_count);
653 g_dbus_connection_disconnect (c);
661 read_all_from_fd (gint fd, gsize *out_len, GError **error)
667 str = g_string_new (NULL);
673 num_read = read (fd, buf, sizeof (buf));
677 if (errsv == EAGAIN || errsv == EWOULDBLOCK)
681 g_io_error_from_errno (errsv),
682 "Failed reading %d bytes into offset %d: %s",
688 else if (num_read > 0)
690 g_string_append_len (str, buf, num_read);
692 else if (num_read == 0)
701 return g_string_free (str, FALSE);
706 g_string_free (str, TRUE);
722 GThread *service_thread;
723 gulong signal_handler_id;
725 memset (&data, '\0', sizeof (PeerData));
726 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
728 /* first try to connect when there is no server */
730 c = g_dbus_connection_new_for_address_sync (is_unix ? "unix:path=/tmp/gdbus-test-does-not-exist-pid" :
731 /* NOTE: Even if something is listening on port 12345 the connection
732 * will fail because the nonce file doesn't exist */
733 "nonce-tcp:host=localhost,port=12345,noncefile=this-does-not-exist-gdbus",
734 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
735 NULL, /* GDBusAuthObserver */
736 NULL, /* cancellable */
738 _g_assert_error_domain (error, G_IO_ERROR);
739 g_assert (!g_dbus_error_is_remote_error (error));
740 g_clear_error (&error);
741 g_assert (c == NULL);
743 /* bring up a server - we run the server in a different thread to avoid deadlocks */
744 service_thread = g_thread_new ("test_peer",
747 await_service_loop ();
748 g_assert (server != NULL);
750 /* bring up a connection and accept it */
751 data.accept_connection = TRUE;
753 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
754 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
755 NULL, /* GDBusAuthObserver */
756 NULL, /* cancellable */
758 g_assert_no_error (error);
759 g_assert (c != NULL);
760 while (data.current_connections->len < 1)
761 g_main_loop_run (loop);
762 g_assert_cmpint (data.current_connections->len, ==, 1);
763 g_assert_cmpint (data.num_connection_attempts, ==, 1);
764 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
765 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
767 /* check that we create a proxy, read properties, receive signals and invoke
768 * the HelloPeer() method. Since the server runs in another thread it's fine
769 * to use synchronous blocking API here.
772 proxy = g_dbus_proxy_new_sync (c,
773 G_DBUS_PROXY_FLAGS_NONE,
776 "/org/gtk/GDBus/PeerTestObject",
777 "org.gtk.GDBus.PeerTestInterface",
778 NULL, /* GCancellable */
780 g_assert_no_error (error);
781 g_assert (proxy != NULL);
783 value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
784 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
786 /* try invoking a method */
788 result = g_dbus_proxy_call_sync (proxy,
790 g_variant_new ("(s)", "Hey Peer!"),
791 G_DBUS_CALL_FLAGS_NONE,
793 NULL, /* GCancellable */
795 g_assert_no_error (error);
796 g_variant_get (result, "(&s)", &s);
797 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
798 g_variant_unref (result);
799 g_assert_cmpint (data.num_method_calls, ==, 1);
801 /* make the other peer emit a signal - catch it */
802 signal_handler_id = g_signal_connect (proxy,
804 G_CALLBACK (on_proxy_signal_received),
806 g_assert (!data.signal_received);
807 g_dbus_proxy_call (proxy,
809 NULL, /* no arguments */
810 G_DBUS_CALL_FLAGS_NONE,
812 NULL, /* GCancellable */
813 NULL, /* GAsyncReadyCallback - we don't care about the result */
814 NULL); /* user_data */
815 g_main_loop_run (loop);
816 g_assert (data.signal_received);
817 g_assert_cmpint (data.num_method_calls, ==, 2);
818 g_signal_handler_disconnect (proxy, signal_handler_id);
820 /* Also ensure that messages with the sender header-field set gets
821 * delivered to the proxy - note that this doesn't really make sense
822 * e.g. names are meaning-less in a peer-to-peer case... but we
823 * support it because it makes sense in certain bridging
824 * applications - see e.g. #623815.
826 signal_handler_id = g_signal_connect (proxy,
828 G_CALLBACK (on_proxy_signal_received_with_name_set),
830 data.signal_received = FALSE;
831 g_dbus_proxy_call (proxy,
832 "EmitSignalWithNameSet",
833 NULL, /* no arguments */
834 G_DBUS_CALL_FLAGS_NONE,
836 NULL, /* GCancellable */
837 NULL, /* GAsyncReadyCallback - we don't care about the result */
838 NULL); /* user_data */
839 g_main_loop_run (loop);
840 g_assert (data.signal_received);
841 g_assert_cmpint (data.num_method_calls, ==, 3);
842 g_signal_handler_disconnect (proxy, signal_handler_id);
844 /* check for UNIX fd passing */
847 GDBusMessage *method_call_message;
848 GDBusMessage *method_reply_message;
849 GUnixFDList *fd_list;
855 const char *testfile = g_test_get_filename (G_TEST_DIST, "file.c", NULL);
857 method_call_message = g_dbus_message_new_method_call (NULL, /* name */
858 "/org/gtk/GDBus/PeerTestObject",
859 "org.gtk.GDBus.PeerTestInterface",
861 g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", testfile));
863 method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
865 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
867 NULL, /* out_serial */
868 NULL, /* cancellable */
870 g_assert_no_error (error);
871 g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
872 fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
873 g_assert (fd_list != NULL);
874 g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
876 fd = g_unix_fd_list_get (fd_list, 0, &error);
877 g_assert_no_error (error);
878 g_object_unref (method_call_message);
879 g_object_unref (method_reply_message);
883 buf = read_all_from_fd (fd, &len, &error);
884 g_assert_no_error (error);
885 g_assert (buf != NULL);
889 g_file_get_contents (testfile,
893 g_assert_no_error (error);
894 g_assert_cmpmem (buf, len, buf2, len2);
900 result = g_dbus_proxy_call_sync (proxy,
902 g_variant_new ("(s)", "boo"),
903 G_DBUS_CALL_FLAGS_NONE,
905 NULL, /* GCancellable */
907 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
908 g_assert (result == NULL);
909 g_error_free (error);
910 #endif /* G_OS_UNIX */
912 /* Check that g_socket_get_credentials() work - (though this really
913 * should be in socket.c)
917 GCredentials *credentials;
918 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (g_dbus_connection_get_stream (c)));
919 g_assert (G_IS_SOCKET (socket));
921 credentials = g_socket_get_credentials (socket, &error);
923 #if G_CREDENTIALS_SOCKET_GET_CREDENTIALS_SUPPORTED
924 g_assert_no_error (error);
925 g_assert (G_IS_CREDENTIALS (credentials));
927 g_assert_cmpuint (g_credentials_get_unix_user (credentials, NULL), ==,
929 g_assert_cmpuint (g_credentials_get_unix_pid (credentials, NULL), ==,
931 g_object_unref (credentials);
933 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
934 g_assert (credentials == NULL);
939 /* bring up a connection - don't accept it - this should fail
941 data.accept_connection = FALSE;
943 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
944 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
945 NULL, /* GDBusAuthObserver */
946 NULL, /* cancellable */
948 _g_assert_error_domain (error, G_IO_ERROR);
949 g_error_free (error);
950 g_assert (c2 == NULL);
953 /* TODO: THIS TEST DOESN'T WORK YET */
955 /* bring up a connection - accept it.. then disconnect from the client side - check
956 * that the server side gets the disconnect signal.
959 data.accept_connection = TRUE;
960 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
961 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
962 NULL, /* GDBusAuthObserver */
963 NULL, /* cancellable */
965 g_assert_no_error (error);
966 g_assert (c2 != NULL);
967 g_assert (!g_dbus_connection_get_is_disconnected (c2));
968 while (data.num_connection_attempts < 3)
969 g_main_loop_run (loop);
970 g_assert_cmpint (data.current_connections->len, ==, 2);
971 g_assert_cmpint (data.num_connection_attempts, ==, 3);
972 g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
973 g_idle_add (on_do_disconnect_in_idle, c2);
974 g_debug ("==================================================");
975 g_debug ("==================================================");
976 g_debug ("==================================================");
977 g_debug ("waiting for disconnect on connection %p, stream %p",
978 data.current_connections->pdata[1],
979 g_dbus_connection_get_stream (data.current_connections->pdata[1]));
981 g_timeout_add (2000, check_connection, &data);
982 //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
983 g_main_loop_run (loop);
984 g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
985 g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
988 /* unref the server and stop listening for new connections
990 * This won't bring down the established connections - check that c is still connected
991 * by invoking a method
993 //g_socket_service_stop (service);
994 //g_object_unref (service);
995 g_dbus_server_stop (server);
996 g_object_unref (server);
1000 result = g_dbus_proxy_call_sync (proxy,
1002 g_variant_new ("(s)", "Hey Again Peer!"),
1003 G_DBUS_CALL_FLAGS_NONE,
1005 NULL, /* GCancellable */
1007 g_assert_no_error (error);
1008 g_variant_get (result, "(&s)", &s);
1009 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
1010 g_variant_unref (result);
1011 g_assert_cmpint (data.num_method_calls, ==, 5);
1014 /* TODO: THIS TEST DOESN'T WORK YET */
1016 /* now disconnect from the server side - check that the client side gets the signal */
1017 g_assert_cmpint (data.current_connections->len, ==, 1);
1018 g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
1019 g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
1020 if (!g_dbus_connection_get_is_disconnected (c))
1021 _g_assert_signal_received (c, "closed");
1022 g_assert (g_dbus_connection_get_is_disconnected (c));
1026 g_ptr_array_unref (data.current_connections);
1027 g_object_unref (proxy);
1029 g_main_loop_quit (service_loop);
1030 g_thread_join (service_thread);
1036 /* Run this test multiple times using different address formats to ensure
1039 setup_test_address ();
1041 teardown_test_address ();
1044 setup_dir_test_address ();
1046 teardown_test_address ();
1048 setup_path_test_address ();
1050 teardown_test_address ();
1054 /* ---------------------------------------------------------------------------------------------------- */
1057 test_peer_signals (void)
1061 GError *error = NULL;
1063 GThread *service_thread;
1065 g_test_bug ("https://gitlab.gnome.org/GNOME/glib/issues/1620");
1067 setup_test_address ();
1068 memset (&data, '\0', sizeof (PeerData));
1069 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1071 /* bring up a server - we run the server in a different thread to avoid deadlocks */
1072 service_thread = g_thread_new ("test_peer",
1073 service_thread_func,
1075 await_service_loop ();
1076 g_assert_nonnull (server);
1078 /* bring up a connection and accept it */
1079 data.accept_connection = TRUE;
1080 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1081 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1082 NULL, /* GDBusAuthObserver */
1083 NULL, /* cancellable */
1085 g_assert_no_error (error);
1086 g_assert_nonnull (c);
1087 while (data.current_connections->len < 1)
1088 g_main_loop_run (loop);
1089 g_assert_cmpint (data.current_connections->len, ==, 1);
1090 g_assert_cmpint (data.num_connection_attempts, ==, 1);
1091 g_assert_null (g_dbus_connection_get_unique_name (c));
1092 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1094 /* Check that we can create a proxy with a non-NULL bus name, even though it's
1095 * irrelevant in the non-message-bus case. Since the server runs in another
1096 * thread it's fine to use synchronous blocking API here.
1098 proxy = g_dbus_proxy_new_sync (c,
1099 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES |
1100 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
1102 ":1.1", /* bus_name */
1103 "/org/gtk/GDBus/PeerTestObject",
1104 "org.gtk.GDBus.PeerTestInterface",
1105 NULL, /* GCancellable */
1107 g_assert_no_error (error);
1108 g_assert_nonnull (proxy);
1110 /* unref the server and stop listening for new connections */
1111 g_dbus_server_stop (server);
1112 g_clear_object (&server);
1115 g_ptr_array_unref (data.current_connections);
1116 g_object_unref (proxy);
1118 g_main_loop_quit (service_loop);
1119 g_thread_join (service_thread);
1121 teardown_test_address ();
1124 /* ---------------------------------------------------------------------------------------------------- */
1128 GDBusServer *server;
1129 GMainContext *context;
1136 dmp_data_free (DmpData *data)
1138 g_main_loop_unref (data->loop);
1139 g_main_context_unref (data->context);
1140 g_object_unref (data->server);
1141 g_list_free_full (data->connections, g_object_unref);
1146 dmp_on_method_call (GDBusConnection *connection,
1147 const gchar *sender,
1148 const gchar *object_path,
1149 const gchar *interface_name,
1150 const gchar *method_name,
1151 GVariant *parameters,
1152 GDBusMethodInvocation *invocation,
1155 //DmpData *data = user_data;
1158 g_variant_get (parameters,
1162 g_dbus_method_invocation_return_value (invocation,
1163 g_variant_new ("(i)", first + second));
1166 static const GDBusInterfaceVTable dmp_interface_vtable =
1169 NULL, /* get_property */
1170 NULL /* set_property */
1174 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1176 dmp_on_new_connection (GDBusServer *server,
1177 GDBusConnection *connection,
1180 DmpData *data = user_data;
1181 GDBusNodeInfo *node;
1184 /* accept the connection */
1185 data->connections = g_list_prepend (data->connections, g_object_ref (connection));
1188 node = g_dbus_node_info_new_for_xml ("<node>"
1189 " <interface name='org.gtk.GDBus.DmpInterface'>"
1190 " <method name='AddPair'>"
1191 " <arg type='i' name='first' direction='in'/>"
1192 " <arg type='i' name='second' direction='in'/>"
1193 " <arg type='i' name='sum' direction='out'/>"
1198 g_assert_no_error (error);
1200 /* sleep 100ms before exporting an object - this is to test that
1201 * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
1202 * (GDBusServer uses this feature).
1204 usleep (100 * 1000);
1206 /* export an object */
1208 g_dbus_connection_register_object (connection,
1210 node->interfaces[0],
1211 &dmp_interface_vtable,
1215 g_dbus_node_info_unref (node);
1221 dmp_thread_func (gpointer user_data)
1223 DmpData *data = user_data;
1227 data->context = g_main_context_new ();
1228 g_main_context_push_thread_default (data->context);
1231 guid = g_dbus_generate_guid ();
1232 data->server = g_dbus_server_new_sync (tmp_address,
1233 G_DBUS_SERVER_FLAGS_NONE,
1235 NULL, /* GDBusAuthObserver */
1236 NULL, /* GCancellable */
1238 g_assert_no_error (error);
1239 g_signal_connect (data->server,
1241 G_CALLBACK (dmp_on_new_connection),
1244 g_dbus_server_start (data->server);
1246 data->loop = g_main_loop_new (data->context, FALSE);
1247 g_main_loop_run (data->loop);
1249 g_main_context_pop_thread_default (data->context);
1256 delayed_message_processing (void)
1260 GThread *service_thread;
1263 setup_test_address ();
1265 data = g_new0 (DmpData, 1);
1267 service_thread = g_thread_new ("dmp",
1270 while (data->server == NULL || !g_dbus_server_is_active (data->server))
1273 for (n = 0; n < 5; n++)
1280 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
1281 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1282 NULL, /* GDBusAuthObserver */
1283 NULL, /* GCancellable */
1285 g_assert_no_error (error);
1288 res = g_dbus_connection_call_sync (c,
1289 NULL, /* bus name */
1291 "org.gtk.GDBus.DmpInterface",
1293 g_variant_new ("(ii)", 2, n),
1294 G_VARIANT_TYPE ("(i)"),
1295 G_DBUS_CALL_FLAGS_NONE,
1296 -1, /* timeout_msec */
1297 NULL, /* GCancellable */
1299 g_assert_no_error (error);
1300 g_variant_get (res, "(i)", &val);
1301 g_assert_cmpint (val, ==, 2 + n);
1302 g_variant_unref (res);
1306 g_main_loop_quit (data->loop);
1307 g_thread_join (service_thread);
1308 dmp_data_free (data);
1309 teardown_test_address ();
1312 /* ---------------------------------------------------------------------------------------------------- */
1315 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1317 GCredentials *credentials,
1320 PeerData *data = user_data;
1321 gboolean authorized;
1323 data->num_connection_attempts++;
1326 if (!data->accept_connection)
1329 g_main_loop_quit (loop);
1335 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1337 nonce_tcp_on_new_connection (GDBusServer *server,
1338 GDBusConnection *connection,
1341 PeerData *data = user_data;
1343 g_ptr_array_add (data->current_connections, g_object_ref (connection));
1345 g_main_loop_quit (loop);
1351 nonce_tcp_service_thread_func (gpointer user_data)
1353 PeerData *data = user_data;
1354 GMainContext *service_context;
1355 GDBusAuthObserver *observer;
1358 service_context = g_main_context_new ();
1359 g_main_context_push_thread_default (service_context);
1362 observer = g_dbus_auth_observer_new ();
1363 server = g_dbus_server_new_sync ("nonce-tcp:",
1364 G_DBUS_SERVER_FLAGS_NONE,
1367 NULL, /* cancellable */
1369 g_assert_no_error (error);
1371 g_signal_connect (server,
1373 G_CALLBACK (nonce_tcp_on_new_connection),
1375 g_signal_connect (observer,
1376 "authorize-authenticated-peer",
1377 G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1379 g_object_unref (observer);
1381 g_dbus_server_start (server);
1383 run_service_loop (service_context);
1385 g_main_context_pop_thread_default (service_context);
1387 teardown_service_loop ();
1388 g_main_context_unref (service_context);
1390 /* test code specifically unrefs the server - see below */
1391 g_assert (server == NULL);
1397 test_nonce_tcp (void)
1401 GThread *service_thread;
1406 const gchar *address;
1408 memset (&data, '\0', sizeof (PeerData));
1409 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1413 service_thread = g_thread_new ("nonce-tcp-service",
1414 nonce_tcp_service_thread_func,
1416 await_service_loop ();
1417 g_assert (server != NULL);
1419 /* bring up a connection and accept it */
1420 data.accept_connection = TRUE;
1422 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1423 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1424 NULL, /* GDBusAuthObserver */
1425 NULL, /* cancellable */
1427 g_assert_no_error (error);
1428 g_assert (c != NULL);
1429 while (data.current_connections->len < 1)
1431 g_assert_cmpint (data.current_connections->len, ==, 1);
1432 g_assert_cmpint (data.num_connection_attempts, ==, 1);
1433 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1434 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1437 /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1440 address = g_dbus_server_get_client_address (server);
1442 s = strstr (address, "noncefile=");
1443 g_assert (s != NULL);
1444 s += sizeof "noncefile=" - 1;
1445 nonce_file = g_strdup (s);
1447 /* First try invalid data in the nonce file - this will actually
1448 * make the client send this and the server will reject it. The way
1449 * it works is that if the nonce doesn't match, the server will
1450 * simply close the connection. So, from the client point of view,
1451 * we can see a variety of errors.
1454 res = g_file_set_contents (nonce_file,
1458 g_assert_no_error (error);
1460 c = g_dbus_connection_new_for_address_sync (address,
1461 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1462 NULL, /* GDBusAuthObserver */
1463 NULL, /* cancellable */
1465 _g_assert_error_domain (error, G_IO_ERROR);
1466 g_error_free (error);
1467 g_assert (c == NULL);
1469 /* Then try with a nonce-file of incorrect length - this will make
1470 * the client complain - we won't even try connecting to the server
1474 res = g_file_set_contents (nonce_file,
1475 "0123456789012345_",
1478 g_assert_no_error (error);
1480 c = g_dbus_connection_new_for_address_sync (address,
1481 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1482 NULL, /* GDBusAuthObserver */
1483 NULL, /* cancellable */
1485 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1486 g_error_free (error);
1487 g_assert (c == NULL);
1489 /* Finally try with no nonce-file at all */
1490 g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1492 c = g_dbus_connection_new_for_address_sync (address,
1493 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1494 NULL, /* GDBusAuthObserver */
1495 NULL, /* cancellable */
1497 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1498 g_error_free (error);
1499 g_assert (c == NULL);
1501 /* Recreate the nonce-file so we can ensure the server deletes it when stopped. */
1502 g_assert_cmpint (g_creat (nonce_file, 0600), !=, -1);
1504 g_dbus_server_stop (server);
1505 g_object_unref (server);
1508 g_assert_false (g_file_test (nonce_file, G_FILE_TEST_EXISTS));
1509 g_free (nonce_file);
1511 g_main_loop_quit (service_loop);
1512 g_thread_join (service_thread);
1514 g_ptr_array_unref (data.current_connections);
1518 test_credentials (void)
1520 GCredentials *c1, *c2;
1524 c1 = g_credentials_new ();
1525 c2 = g_credentials_new ();
1528 if (g_credentials_set_unix_user (c2, getuid (), &error))
1529 g_assert_no_error (error);
1531 g_clear_error (&error);
1532 g_assert (g_credentials_is_same_user (c1, c2, &error));
1533 g_assert_no_error (error);
1535 desc = g_credentials_to_string (c1);
1536 g_assert (desc != NULL);
1539 g_object_unref (c1);
1540 g_object_unref (c2);
1543 /* ---------------------------------------------------------------------------------------------------- */
1546 tcp_anonymous_on_new_connection (GDBusServer *server,
1547 GDBusConnection *connection,
1550 gboolean *seen_connection = user_data;
1551 *seen_connection = TRUE;
1556 tcp_anonymous_service_thread_func (gpointer user_data)
1558 gboolean *seen_connection = user_data;
1559 GMainContext *service_context;
1562 service_context = g_main_context_new ();
1563 g_main_context_push_thread_default (service_context);
1566 server = g_dbus_server_new_sync ("tcp:",
1567 G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS,
1569 NULL, /* GDBusObserver* */
1570 NULL, /* GCancellable* */
1572 g_assert_no_error (error);
1574 g_signal_connect (server,
1576 G_CALLBACK (tcp_anonymous_on_new_connection),
1579 g_dbus_server_start (server);
1581 run_service_loop (service_context);
1583 g_main_context_pop_thread_default (service_context);
1585 teardown_service_loop ();
1586 g_main_context_unref (service_context);
1592 test_tcp_anonymous (void)
1594 gboolean seen_connection;
1595 GThread *service_thread;
1596 GDBusConnection *connection;
1599 seen_connection = FALSE;
1600 service_thread = g_thread_new ("tcp-anon-service",
1601 tcp_anonymous_service_thread_func,
1603 await_service_loop ();
1604 g_assert (server != NULL);
1607 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1608 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1609 NULL, /* GDBusAuthObserver* */
1610 NULL, /* GCancellable */
1612 g_assert_no_error (error);
1613 g_assert (connection != NULL);
1615 while (!seen_connection)
1618 g_object_unref (connection);
1620 g_main_loop_quit (service_loop);
1621 g_dbus_server_stop (server);
1622 g_object_unref (server);
1625 g_thread_join (service_thread);
1628 /* ---------------------------------------------------------------------------------------------------- */
1630 static GDBusServer *codegen_server = NULL;
1633 codegen_on_animal_poke (ExampleAnimal *animal,
1634 GDBusMethodInvocation *invocation,
1636 gboolean make_happy,
1639 if ((make_sad && make_happy) || (!make_sad && !make_happy))
1641 g_main_loop_quit (service_loop);
1643 g_dbus_method_invocation_return_dbus_error (invocation,
1644 "org.gtk.GDBus.Examples.ObjectManager.Error.Failed",
1645 "Exactly one of make_sad or make_happy must be TRUE");
1651 if (g_strcmp0 (example_animal_get_mood (animal), "Sad") == 0)
1653 g_dbus_method_invocation_return_dbus_error (invocation,
1654 "org.gtk.GDBus.Examples.ObjectManager.Error.SadAnimalIsSad",
1655 "Sad animal is already sad");
1659 example_animal_set_mood (animal, "Sad");
1660 example_animal_complete_poke (animal, invocation);
1666 if (g_strcmp0 (example_animal_get_mood (animal), "Happy") == 0)
1668 g_dbus_method_invocation_return_dbus_error (invocation,
1669 "org.gtk.GDBus.Examples.ObjectManager.Error.HappyAnimalIsHappy",
1670 "Happy animal is already happy");
1674 example_animal_set_mood (animal, "Happy");
1675 example_animal_complete_poke (animal, invocation);
1679 g_assert_not_reached ();
1682 return TRUE; /* to indicate that the method was handled */
1685 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1687 codegen_on_new_connection (GDBusServer *server,
1688 GDBusConnection *connection,
1691 ExampleAnimal *animal = user_data;
1692 GError *error = NULL;
1694 /* g_printerr ("Client connected.\n" */
1695 /* "Negotiated capabilities: unix-fd-passing=%d\n", */
1696 /* g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING); */
1698 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (animal), connection,
1699 "/Example/Animals/000", &error);
1700 g_assert_no_error (error);
1706 codegen_service_thread_func (gpointer user_data)
1708 GMainContext *service_context;
1709 ExampleAnimal *animal;
1710 GError *error = NULL;
1712 service_context = g_main_context_new ();
1713 g_main_context_push_thread_default (service_context);
1715 /* Create the animal in the right thread context */
1716 animal = example_animal_skeleton_new ();
1718 /* Handle Poke() D-Bus method invocations on the .Animal interface */
1719 g_signal_connect (animal, "handle-poke",
1720 G_CALLBACK (codegen_on_animal_poke),
1721 NULL); /* user_data */
1723 codegen_server = g_dbus_server_new_sync (tmp_address,
1724 G_DBUS_SERVER_FLAGS_NONE,
1726 NULL, /* observer */
1727 NULL, /* cancellable */
1729 g_assert_no_error (error);
1730 g_dbus_server_start (codegen_server);
1732 g_signal_connect (codegen_server, "new-connection",
1733 G_CALLBACK (codegen_on_new_connection),
1736 run_service_loop (service_context);
1738 g_object_unref (animal);
1740 g_main_context_pop_thread_default (service_context);
1742 teardown_service_loop ();
1743 g_main_context_unref (service_context);
1745 g_dbus_server_stop (codegen_server);
1746 g_object_unref (codegen_server);
1747 codegen_server = NULL;
1754 codegen_quit_mainloop_timeout (gpointer data)
1756 g_main_loop_quit (loop);
1761 codegen_test_peer (void)
1763 GDBusConnection *connection;
1764 ExampleAnimal *animal1, *animal2;
1765 GThread *service_thread;
1766 GError *error = NULL;
1770 setup_test_address ();
1772 /* bring up a server - we run the server in a different thread to avoid deadlocks */
1773 service_thread = g_thread_new ("codegen_test_peer",
1774 codegen_service_thread_func,
1776 await_service_loop ();
1777 g_assert (codegen_server != NULL);
1779 /* Get an animal 1 ... */
1780 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (codegen_server),
1781 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1782 NULL, /* GDBusAuthObserver */
1783 NULL, /* cancellable */
1785 g_assert_no_error (error);
1786 g_assert (connection != NULL);
1788 animal1 = example_animal_proxy_new_sync (connection, 0, NULL,
1789 "/Example/Animals/000", NULL, &error);
1790 g_assert_no_error (error);
1791 g_assert (animal1 != NULL);
1792 g_object_unref (connection);
1794 /* Get animal 2 ... */
1795 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (codegen_server),
1796 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1797 NULL, /* GDBusAuthObserver */
1798 NULL, /* cancellable */
1800 g_assert_no_error (error);
1801 g_assert (connection != NULL);
1803 animal2 = example_animal_proxy_new_sync (connection, 0, NULL,
1804 "/Example/Animals/000", NULL, &error);
1805 g_assert_no_error (error);
1806 g_assert (animal2 != NULL);
1807 g_object_unref (connection);
1809 /* Make animal sad via animal1 */
1810 example_animal_call_poke_sync (animal1, TRUE, FALSE, NULL, &error);
1811 g_assert_no_error (error);
1813 /* Poke server and make sure animal is updated */
1814 value = g_dbus_proxy_call_sync (G_DBUS_PROXY (animal1),
1815 "org.freedesktop.DBus.Peer.Ping",
1816 NULL, G_DBUS_CALL_FLAGS_NONE, -1,
1818 g_assert_no_error (error);
1819 g_assert (value != NULL);
1820 g_variant_unref (value);
1822 /* Give the proxies a chance to refresh in the defaul main loop */
1823 g_timeout_add (100, codegen_quit_mainloop_timeout, NULL);
1824 g_main_loop_run (loop);
1826 /* Assert animals are sad */
1827 g_assert_cmpstr (example_animal_get_mood (animal1), ==, "Sad");
1828 g_assert_cmpstr (example_animal_get_mood (animal2), ==, "Sad");
1830 /* Make animal happy via animal2 */
1831 example_animal_call_poke_sync (animal2, FALSE, TRUE, NULL, &error);
1832 g_assert_no_error (error);
1834 /* Some random unrelated call, just to get some test coverage */
1835 value = g_dbus_proxy_call_sync (G_DBUS_PROXY (animal2),
1836 "org.freedesktop.DBus.Peer.GetMachineId",
1837 NULL, G_DBUS_CALL_FLAGS_NONE, -1,
1839 g_assert_no_error (error);
1840 g_variant_get (value, "(&s)", &s);
1841 g_test_message ("Machine ID: %s", s);
1842 /* It's valid for machine-id inside containers to be empty, so we
1843 * need to test for that possibility
1845 g_assert ((s == NULL || *s == '\0') || g_dbus_is_guid (s));
1846 g_variant_unref (value);
1848 /* Poke server and make sure animal is updated */
1849 value = g_dbus_proxy_call_sync (G_DBUS_PROXY (animal2),
1850 "org.freedesktop.DBus.Peer.Ping",
1851 NULL, G_DBUS_CALL_FLAGS_NONE, -1,
1853 g_assert_no_error (error);
1854 g_assert (value != NULL);
1855 g_variant_unref (value);
1857 /* Give the proxies a chance to refresh in the defaul main loop */
1858 g_timeout_add (1000, codegen_quit_mainloop_timeout, NULL);
1859 g_main_loop_run (loop);
1861 /* Assert animals are happy */
1862 g_assert_cmpstr (example_animal_get_mood (animal1), ==, "Happy");
1863 g_assert_cmpstr (example_animal_get_mood (animal2), ==, "Happy");
1865 /* This final call making the animal happy and sad will cause
1866 * the server to quit, when the server quits we dont get property
1867 * change notifications anyway because those are done from an idle handler
1869 example_animal_call_poke_sync (animal2, TRUE, TRUE, NULL, &error);
1870 g_clear_error (&error);
1872 g_object_unref (animal1);
1873 g_object_unref (animal2);
1874 g_thread_join (service_thread);
1876 teardown_test_address ();
1879 /* ---------------------------------------------------------------------------------------------------- */
1887 GDBusNodeInfo *introspection_data = NULL;
1889 g_test_init (&argc, &argv, NULL);
1891 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1892 g_assert (introspection_data != NULL);
1893 test_interface_introspection_data = introspection_data->interfaces[0];
1895 test_guid = g_dbus_generate_guid ();
1897 /* all the tests rely on a shared main loop */
1898 loop = g_main_loop_new (NULL, FALSE);
1900 g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1901 g_test_add_func ("/gdbus/peer-to-peer/signals", test_peer_signals);
1902 g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1903 g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1905 g_test_add_func ("/gdbus/tcp-anonymous", test_tcp_anonymous);
1906 g_test_add_func ("/gdbus/credentials", test_credentials);
1907 g_test_add_func ("/gdbus/codegen-peer-to-peer", codegen_test_peer);
1911 g_main_loop_unref (loop);
1913 g_dbus_node_info_unref (introspection_data);