1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
30 #include <sys/types.h>
36 #include <glib/gstdio.h>
38 #include <gio/gnetworkingprivate.h>
39 #include <gio/gunixsocketaddress.h>
40 #include <gio/gunixfdlist.h>
42 /* used in test_overflow */
44 #include <gio/gunixconnection.h>
48 #include "gdbus-tests.h"
51 static gboolean is_unix = TRUE;
53 static gboolean is_unix = FALSE;
56 static gchar *tmp_address = NULL;
57 static gchar *test_guid = NULL;
58 static GMainLoop *service_loop = NULL;
59 static GDBusServer *server = NULL;
60 static GMainLoop *loop = NULL;
62 /* ---------------------------------------------------------------------------------------------------- */
63 /* Test that peer-to-peer connections work */
64 /* ---------------------------------------------------------------------------------------------------- */
69 gboolean accept_connection;
70 gint num_connection_attempts;
71 GPtrArray *current_connections;
72 guint num_method_calls;
73 gboolean signal_received;
76 static const gchar *test_interface_introspection_xml =
78 " <interface name='org.gtk.GDBus.PeerTestInterface'>"
79 " <method name='HelloPeer'>"
80 " <arg type='s' name='greeting' direction='in'/>"
81 " <arg type='s' name='response' direction='out'/>"
83 " <method name='EmitSignal'/>"
84 " <method name='EmitSignalWithNameSet'/>"
85 " <method name='OpenFile'>"
86 " <arg type='s' name='path' direction='in'/>"
88 " <signal name='PeerSignal'>"
89 " <arg type='s' name='a_string'/>"
91 " <property type='s' name='PeerProperty' access='read'/>"
94 static GDBusInterfaceInfo *test_interface_introspection_data = NULL;
97 test_interface_method_call (GDBusConnection *connection,
99 const gchar *object_path,
100 const gchar *interface_name,
101 const gchar *method_name,
102 GVariant *parameters,
103 GDBusMethodInvocation *invocation,
106 PeerData *data = user_data;
107 const GDBusMethodInfo *info;
109 data->num_method_calls++;
111 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
112 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
114 info = g_dbus_method_invocation_get_method_info (invocation);
115 g_assert_cmpstr (info->name, ==, method_name);
117 if (g_strcmp0 (method_name, "HelloPeer") == 0)
119 const gchar *greeting;
122 g_variant_get (parameters, "(&s)", &greeting);
124 response = g_strdup_printf ("You greeted me with '%s'.",
126 g_dbus_method_invocation_return_value (invocation,
127 g_variant_new ("(s)", response));
130 else if (g_strcmp0 (method_name, "EmitSignal") == 0)
135 g_dbus_connection_emit_signal (connection,
137 "/org/gtk/GDBus/PeerTestObject",
138 "org.gtk.GDBus.PeerTestInterface",
142 g_assert_no_error (error);
143 g_dbus_method_invocation_return_value (invocation, NULL);
145 else if (g_strcmp0 (method_name, "EmitSignalWithNameSet") == 0)
149 GDBusMessage *message;
151 message = g_dbus_message_new_signal ("/org/gtk/GDBus/PeerTestObject",
152 "org.gtk.GDBus.PeerTestInterface",
153 "PeerSignalWithNameSet");
154 g_dbus_message_set_sender (message, ":1.42");
157 ret = g_dbus_connection_send_message (connection, message, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error);
158 g_assert_no_error (error);
160 g_object_unref (message);
162 g_dbus_method_invocation_return_value (invocation, NULL);
164 else if (g_strcmp0 (method_name, "OpenFile") == 0)
171 GUnixFDList *fd_list;
173 g_variant_get (parameters, "(&s)", &path);
175 fd_list = g_unix_fd_list_new ();
179 fd = open (path, O_RDONLY);
180 g_unix_fd_list_append (fd_list, fd, &error);
181 g_assert_no_error (error);
184 reply = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
185 g_dbus_message_set_unix_fd_list (reply, fd_list);
186 g_object_unref (fd_list);
187 g_object_unref (invocation);
190 g_dbus_connection_send_message (connection,
192 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
193 NULL, /* out_serial */
195 g_assert_no_error (error);
196 g_object_unref (reply);
198 g_dbus_method_invocation_return_dbus_error (invocation,
199 "org.gtk.GDBus.NotOnUnix",
200 "Your OS does not support file descriptor passing");
205 g_assert_not_reached ();
210 test_interface_get_property (GDBusConnection *connection,
212 const gchar *object_path,
213 const gchar *interface_name,
214 const gchar *property_name,
218 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
219 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
220 g_assert_cmpstr (property_name, ==, "PeerProperty");
222 return g_variant_new_string ("ThePropertyValue");
226 static const GDBusInterfaceVTable test_interface_vtable =
228 test_interface_method_call,
229 test_interface_get_property,
230 NULL /* set_property */
234 on_proxy_signal_received (GDBusProxy *proxy,
237 GVariant *parameters,
240 PeerData *data = user_data;
242 data->signal_received = TRUE;
244 g_assert (sender_name == NULL);
245 g_assert_cmpstr (signal_name, ==, "PeerSignal");
246 g_main_loop_quit (loop);
250 on_proxy_signal_received_with_name_set (GDBusProxy *proxy,
253 GVariant *parameters,
256 PeerData *data = user_data;
258 data->signal_received = TRUE;
260 g_assert_cmpstr (sender_name, ==, ":1.42");
261 g_assert_cmpstr (signal_name, ==, "PeerSignalWithNameSet");
262 g_main_loop_quit (loop);
265 /* ---------------------------------------------------------------------------------------------------- */
268 on_authorize_authenticated_peer (GDBusAuthObserver *observer,
270 GCredentials *credentials,
273 PeerData *data = user_data;
276 data->num_connection_attempts++;
279 if (!data->accept_connection)
282 g_main_loop_quit (loop);
288 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
290 on_new_connection (GDBusServer *server,
291 GDBusConnection *connection,
294 PeerData *data = user_data;
298 //g_print ("Client connected.\n"
299 // "Negotiated capabilities: unix-fd-passing=%d\n",
300 // g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
302 g_ptr_array_add (data->current_connections, g_object_ref (connection));
304 /* export object on the newly established connection */
306 reg_id = g_dbus_connection_register_object (connection,
307 "/org/gtk/GDBus/PeerTestObject",
308 test_interface_introspection_data,
309 &test_interface_vtable,
311 NULL, /* GDestroyNotify for data */
313 g_assert_no_error (error);
314 g_assert (reg_id > 0);
316 g_main_loop_quit (loop);
322 service_thread_func (gpointer user_data)
324 PeerData *data = user_data;
325 GMainContext *service_context;
326 GDBusAuthObserver *observer;
329 service_context = g_main_context_new ();
330 g_main_context_push_thread_default (service_context);
333 observer = g_dbus_auth_observer_new ();
334 server = g_dbus_server_new_sync (tmp_address,
335 G_DBUS_SERVER_FLAGS_NONE,
338 NULL, /* cancellable */
340 g_assert_no_error (error);
342 g_signal_connect (server,
344 G_CALLBACK (on_new_connection),
346 g_signal_connect (observer,
347 "authorize-authenticated-peer",
348 G_CALLBACK (on_authorize_authenticated_peer),
350 g_object_unref (observer);
352 g_dbus_server_start (server);
354 service_loop = g_main_loop_new (service_context, FALSE);
355 g_main_loop_run (service_loop);
357 g_main_context_pop_thread_default (service_context);
359 g_main_loop_unref (service_loop);
360 g_main_context_unref (service_context);
362 /* test code specifically unrefs the server - see below */
363 g_assert (server == NULL);
370 on_incoming_connection (GSocketService *service,
371 GSocketConnection *socket_connection,
372 GObject *source_object,
375 PeerData *data = user_data;
377 if (data->accept_connection)
381 GDBusConnection *connection;
384 connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
386 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
387 NULL, /* cancellable */
389 g_assert_no_error (error);
391 g_ptr_array_add (data->current_connections, connection);
393 /* export object on the newly established connection */
395 reg_id = g_dbus_connection_register_object (connection,
396 "/org/gtk/GDBus/PeerTestObject",
397 &test_interface_introspection_data,
398 &test_interface_vtable,
400 NULL, /* GDestroyNotify for data */
402 g_assert_no_error (error);
403 g_assert (reg_id > 0);
408 /* don't do anything */
411 data->num_connection_attempts++;
413 g_main_loop_quit (loop);
415 /* stops other signal handlers from being invoked */
420 service_thread_func (gpointer data)
422 GMainContext *service_context;
424 GSocketAddress *address;
427 service_context = g_main_context_new ();
428 g_main_context_push_thread_default (service_context);
430 socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
431 address = g_unix_socket_address_new (socket_path);
433 service = g_socket_service_new ();
435 g_socket_listener_add_address (G_SOCKET_LISTENER (service),
437 G_SOCKET_TYPE_STREAM,
438 G_SOCKET_PROTOCOL_DEFAULT,
439 NULL, /* source_object */
440 NULL, /* effective_address */
442 g_assert_no_error (error);
443 g_signal_connect (service,
445 G_CALLBACK (on_incoming_connection),
447 g_socket_service_start (service);
449 service_loop = g_main_loop_new (service_context, FALSE);
450 g_main_loop_run (service_loop);
452 g_main_context_pop_thread_default (service_context);
454 g_main_loop_unref (service_loop);
455 g_main_context_unref (service_context);
457 g_object_unref (address);
458 g_free (socket_path);
463 /* ---------------------------------------------------------------------------------------------------- */
467 check_connection (gpointer user_data)
469 PeerData *data = user_data;
472 for (n = 0; n < data->current_connections->len; n++)
477 c = G_DBUS_CONNECTION (data->current_connections->pdata[n]);
478 stream = g_dbus_connection_get_stream (c);
480 g_debug ("In check_connection for %d: connection %p, stream %p", n, c, stream);
481 g_debug ("closed = %d", g_io_stream_is_closed (stream));
484 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
485 g_debug ("socket_closed = %d", g_socket_is_closed (socket));
486 g_debug ("socket_condition_check = %d", g_socket_condition_check (socket, G_IO_IN|G_IO_OUT|G_IO_ERR|G_IO_HUP));
492 num_read = g_input_stream_read (g_io_stream_get_input_stream (stream),
499 g_debug ("error: %s", error->message);
500 g_error_free (error);
504 g_debug ("no error, read %d bytes", (gint) num_read);
512 on_do_disconnect_in_idle (gpointer data)
514 GDBusConnection *c = G_DBUS_CONNECTION (data);
515 g_debug ("GDC %p has ref_count %d", c, G_OBJECT (c)->ref_count);
516 g_dbus_connection_disconnect (c);
524 read_all_from_fd (gint fd, gsize *out_len, GError **error)
530 str = g_string_new (NULL);
534 num_read = read (fd, buf, sizeof (buf));
537 if (errno == EAGAIN || errno == EWOULDBLOCK)
541 g_io_error_from_errno (errno),
542 "Failed reading %d bytes into offset %d: %s",
548 else if (num_read > 0)
550 g_string_append_len (str, buf, num_read);
552 else if (num_read == 0)
561 return g_string_free (str, FALSE);
566 g_string_free (str, TRUE);
582 GThread *service_thread;
583 gulong signal_handler_id;
585 memset (&data, '\0', sizeof (PeerData));
586 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
588 /* first try to connect when there is no server */
590 c = g_dbus_connection_new_for_address_sync (is_unix ? "unix:path=/tmp/gdbus-test-does-not-exist-pid" :
591 /* NOTE: Even if something is listening on port 12345 the connection
592 * will fail because the nonce file doesn't exist */
593 "nonce-tcp:host=localhost,port=12345,noncefile=this-does-not-exist-gdbus",
594 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
595 NULL, /* GDBusAuthObserver */
596 NULL, /* cancellable */
598 _g_assert_error_domain (error, G_IO_ERROR);
599 g_assert (!g_dbus_error_is_remote_error (error));
600 g_clear_error (&error);
601 g_assert (c == NULL);
603 /* bring up a server - we run the server in a different thread to avoid deadlocks */
604 service_thread = g_thread_new ("test_peer",
607 while (service_loop == NULL)
609 g_assert (server != NULL);
611 /* bring up a connection and accept it */
612 data.accept_connection = TRUE;
614 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
615 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
616 NULL, /* GDBusAuthObserver */
617 NULL, /* cancellable */
619 g_assert_no_error (error);
620 g_assert (c != NULL);
621 while (data.current_connections->len < 1)
622 g_main_loop_run (loop);
623 g_assert_cmpint (data.current_connections->len, ==, 1);
624 g_assert_cmpint (data.num_connection_attempts, ==, 1);
625 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
626 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
628 /* check that we create a proxy, read properties, receive signals and invoke
629 * the HelloPeer() method. Since the server runs in another thread it's fine
630 * to use synchronous blocking API here.
633 proxy = g_dbus_proxy_new_sync (c,
634 G_DBUS_PROXY_FLAGS_NONE,
637 "/org/gtk/GDBus/PeerTestObject",
638 "org.gtk.GDBus.PeerTestInterface",
639 NULL, /* GCancellable */
641 g_assert_no_error (error);
642 g_assert (proxy != NULL);
644 value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
645 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
647 /* try invoking a method */
649 result = g_dbus_proxy_call_sync (proxy,
651 g_variant_new ("(s)", "Hey Peer!"),
652 G_DBUS_CALL_FLAGS_NONE,
654 NULL, /* GCancellable */
656 g_assert_no_error (error);
657 g_variant_get (result, "(&s)", &s);
658 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
659 g_variant_unref (result);
660 g_assert_cmpint (data.num_method_calls, ==, 1);
662 /* make the other peer emit a signal - catch it */
663 signal_handler_id = g_signal_connect (proxy,
665 G_CALLBACK (on_proxy_signal_received),
667 g_assert (!data.signal_received);
668 g_dbus_proxy_call (proxy,
670 NULL, /* no arguments */
671 G_DBUS_CALL_FLAGS_NONE,
673 NULL, /* GCancellable */
674 NULL, /* GAsyncReadyCallback - we don't care about the result */
675 NULL); /* user_data */
676 g_main_loop_run (loop);
677 g_assert (data.signal_received);
678 g_assert_cmpint (data.num_method_calls, ==, 2);
679 g_signal_handler_disconnect (proxy, signal_handler_id);
681 /* Also ensure that messages with the sender header-field set gets
682 * delivered to the proxy - note that this doesn't really make sense
683 * e.g. names are meaning-less in a peer-to-peer case... but we
684 * support it because it makes sense in certain bridging
685 * applications - see e.g. #623815.
687 signal_handler_id = g_signal_connect (proxy,
689 G_CALLBACK (on_proxy_signal_received_with_name_set),
691 data.signal_received = FALSE;
692 g_dbus_proxy_call (proxy,
693 "EmitSignalWithNameSet",
694 NULL, /* no arguments */
695 G_DBUS_CALL_FLAGS_NONE,
697 NULL, /* GCancellable */
698 NULL, /* GAsyncReadyCallback - we don't care about the result */
699 NULL); /* user_data */
700 g_main_loop_run (loop);
701 g_assert (data.signal_received);
702 g_assert_cmpint (data.num_method_calls, ==, 3);
703 g_signal_handler_disconnect (proxy, signal_handler_id);
705 /* check for UNIX fd passing */
708 GDBusMessage *method_call_message;
709 GDBusMessage *method_reply_message;
710 GUnixFDList *fd_list;
717 method_call_message = g_dbus_message_new_method_call (NULL, /* name */
718 "/org/gtk/GDBus/PeerTestObject",
719 "org.gtk.GDBus.PeerTestInterface",
721 g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", "/etc/hosts"));
723 method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
725 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
727 NULL, /* out_serial */
728 NULL, /* cancellable */
730 g_assert_no_error (error);
731 g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
732 fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
733 g_assert (fd_list != NULL);
734 g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
736 fd = g_unix_fd_list_get (fd_list, 0, &error);
737 g_assert_no_error (error);
738 g_object_unref (method_call_message);
739 g_object_unref (method_reply_message);
743 buf = read_all_from_fd (fd, &len, &error);
744 g_assert_no_error (error);
745 g_assert (buf != NULL);
749 g_file_get_contents ("/etc/hosts",
753 g_assert_no_error (error);
754 g_assert_cmpint (len, ==, len2);
755 g_assert (memcmp (buf, buf2, len) == 0);
761 result = g_dbus_proxy_call_sync (proxy,
763 g_variant_new ("(s)", "boo"),
764 G_DBUS_CALL_FLAGS_NONE,
766 NULL, /* GCancellable */
768 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
769 g_assert (result == NULL);
770 g_error_free (error);
771 #endif /* G_OS_UNIX */
773 /* Check that g_socket_get_credentials() work - this really should
774 * be in a GSocket-specific test suite but no such test suite exists
779 GCredentials *credentials;
780 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (g_dbus_connection_get_stream (c)));
781 g_assert (G_IS_SOCKET (socket));
783 credentials = g_socket_get_credentials (socket, &error);
786 struct ucred *native_creds;
787 g_assert_no_error (error);
788 g_assert (G_IS_CREDENTIALS (credentials));
789 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED);
790 g_assert (native_creds != NULL);
791 g_assert (native_creds->uid == getuid ());
792 g_assert (native_creds->gid == getgid ());
793 g_assert (native_creds->pid == getpid ());
795 g_object_unref (credentials);
796 #elif defined (__OpenBSD__)
798 struct sockpeercred *native_creds;
799 g_assert_no_error (error);
800 g_assert (G_IS_CREDENTIALS (credentials));
801 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED);
802 g_assert (native_creds != NULL);
803 g_assert (native_creds->uid == getuid ());
804 g_assert (native_creds->gid == getgid ());
805 g_assert (native_creds->pid == getpid ());
807 g_object_unref (credentials);
809 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
810 g_assert (credentials == NULL);
815 /* bring up a connection - don't accept it - this should fail
817 data.accept_connection = FALSE;
819 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
820 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
821 NULL, /* GDBusAuthObserver */
822 NULL, /* cancellable */
824 _g_assert_error_domain (error, G_IO_ERROR);
825 g_error_free (error);
826 g_assert (c2 == NULL);
829 /* TODO: THIS TEST DOESN'T WORK YET */
831 /* bring up a connection - accept it.. then disconnect from the client side - check
832 * that the server side gets the disconnect signal.
835 data.accept_connection = TRUE;
836 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
837 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
838 NULL, /* GDBusAuthObserver */
839 NULL, /* cancellable */
841 g_assert_no_error (error);
842 g_assert (c2 != NULL);
843 g_assert (!g_dbus_connection_get_is_disconnected (c2));
844 while (data.num_connection_attempts < 3)
845 g_main_loop_run (loop);
846 g_assert_cmpint (data.current_connections->len, ==, 2);
847 g_assert_cmpint (data.num_connection_attempts, ==, 3);
848 g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
849 g_idle_add (on_do_disconnect_in_idle, c2);
850 g_debug ("==================================================");
851 g_debug ("==================================================");
852 g_debug ("==================================================");
853 g_debug ("waiting for disconnect on connection %p, stream %p",
854 data.current_connections->pdata[1],
855 g_dbus_connection_get_stream (data.current_connections->pdata[1]));
857 g_timeout_add (2000, check_connection, &data);
858 //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
859 g_main_loop_run (loop);
860 g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
861 g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
864 /* unref the server and stop listening for new connections
866 * This won't bring down the established connections - check that c is still connected
867 * by invoking a method
869 //g_socket_service_stop (service);
870 //g_object_unref (service);
871 g_dbus_server_stop (server);
872 g_object_unref (server);
876 result = g_dbus_proxy_call_sync (proxy,
878 g_variant_new ("(s)", "Hey Again Peer!"),
879 G_DBUS_CALL_FLAGS_NONE,
881 NULL, /* GCancellable */
883 g_assert_no_error (error);
884 g_variant_get (result, "(&s)", &s);
885 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
886 g_variant_unref (result);
887 g_assert_cmpint (data.num_method_calls, ==, 5);
890 /* TODO: THIS TEST DOESN'T WORK YET */
892 /* now disconnect from the server side - check that the client side gets the signal */
893 g_assert_cmpint (data.current_connections->len, ==, 1);
894 g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
895 g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
896 if (!g_dbus_connection_get_is_disconnected (c))
897 _g_assert_signal_received (c, "closed");
898 g_assert (g_dbus_connection_get_is_disconnected (c));
902 g_ptr_array_unref (data.current_connections);
903 g_object_unref (proxy);
905 g_main_loop_quit (service_loop);
906 g_thread_join (service_thread);
909 /* ---------------------------------------------------------------------------------------------------- */
914 GMainContext *context;
921 dmp_data_free (DmpData *data)
923 g_main_loop_unref (data->loop);
924 g_main_context_unref (data->context);
925 g_object_unref (data->server);
926 g_list_foreach (data->connections, (GFunc) g_object_unref, NULL);
927 g_list_free (data->connections);
932 dmp_on_method_call (GDBusConnection *connection,
934 const gchar *object_path,
935 const gchar *interface_name,
936 const gchar *method_name,
937 GVariant *parameters,
938 GDBusMethodInvocation *invocation,
941 //DmpData *data = user_data;
944 g_variant_get (parameters,
948 g_dbus_method_invocation_return_value (invocation,
949 g_variant_new ("(i)", first + second));
952 static const GDBusInterfaceVTable dmp_interface_vtable =
955 NULL, /* get_property */
956 NULL /* set_property */
960 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
962 dmp_on_new_connection (GDBusServer *server,
963 GDBusConnection *connection,
966 DmpData *data = user_data;
970 /* accept the connection */
971 data->connections = g_list_prepend (data->connections, g_object_ref (connection));
974 node = g_dbus_node_info_new_for_xml ("<node>"
975 " <interface name='org.gtk.GDBus.DmpInterface'>"
976 " <method name='AddPair'>"
977 " <arg type='i' name='first' direction='in'/>"
978 " <arg type='i' name='second' direction='in'/>"
979 " <arg type='i' name='sum' direction='out'/>"
984 g_assert_no_error (error);
986 /* sleep 100ms before exporting an object - this is to test that
987 * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
988 * (GDBusServer uses this feature).
992 /* export an object */
994 g_dbus_connection_register_object (connection,
997 &dmp_interface_vtable,
1001 g_dbus_node_info_unref (node);
1007 dmp_thread_func (gpointer user_data)
1009 DmpData *data = user_data;
1013 data->context = g_main_context_new ();
1014 g_main_context_push_thread_default (data->context);
1017 guid = g_dbus_generate_guid ();
1018 data->server = g_dbus_server_new_sync (tmp_address,
1019 G_DBUS_SERVER_FLAGS_NONE,
1021 NULL, /* GDBusAuthObserver */
1022 NULL, /* GCancellable */
1024 g_assert_no_error (error);
1025 g_signal_connect (data->server,
1027 G_CALLBACK (dmp_on_new_connection),
1030 g_dbus_server_start (data->server);
1032 data->loop = g_main_loop_new (data->context, FALSE);
1033 g_main_loop_run (data->loop);
1035 g_main_context_pop_thread_default (data->context);
1042 delayed_message_processing (void)
1046 GThread *service_thread;
1049 data = g_new0 (DmpData, 1);
1051 service_thread = g_thread_new ("dmp",
1054 while (data->server == NULL || !g_dbus_server_is_active (data->server))
1057 for (n = 0; n < 5; n++)
1064 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
1065 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1066 NULL, /* GDBusAuthObserver */
1067 NULL, /* GCancellable */
1069 g_assert_no_error (error);
1072 res = g_dbus_connection_call_sync (c,
1073 NULL, /* bus name */
1075 "org.gtk.GDBus.DmpInterface",
1077 g_variant_new ("(ii)", 2, n),
1078 G_VARIANT_TYPE ("(i)"),
1079 G_DBUS_CALL_FLAGS_NONE,
1080 -1, /* timeout_msec */
1081 NULL, /* GCancellable */
1083 g_assert_no_error (error);
1084 g_variant_get (res, "(i)", &val);
1085 g_assert_cmpint (val, ==, 2 + n);
1086 g_variant_unref (res);
1090 g_main_loop_quit (data->loop);
1091 g_thread_join (service_thread);
1092 dmp_data_free (data);
1095 /* ---------------------------------------------------------------------------------------------------- */
1098 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1100 GCredentials *credentials,
1103 PeerData *data = user_data;
1104 gboolean authorized;
1106 data->num_connection_attempts++;
1109 if (!data->accept_connection)
1112 g_main_loop_quit (loop);
1118 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1120 nonce_tcp_on_new_connection (GDBusServer *server,
1121 GDBusConnection *connection,
1124 PeerData *data = user_data;
1126 g_ptr_array_add (data->current_connections, g_object_ref (connection));
1128 g_main_loop_quit (loop);
1134 nonce_tcp_service_thread_func (gpointer user_data)
1136 PeerData *data = user_data;
1137 GMainContext *service_context;
1138 GDBusAuthObserver *observer;
1141 service_context = g_main_context_new ();
1142 g_main_context_push_thread_default (service_context);
1145 observer = g_dbus_auth_observer_new ();
1146 server = g_dbus_server_new_sync ("nonce-tcp:",
1147 G_DBUS_SERVER_FLAGS_NONE,
1150 NULL, /* cancellable */
1152 g_assert_no_error (error);
1154 g_signal_connect (server,
1156 G_CALLBACK (nonce_tcp_on_new_connection),
1158 g_signal_connect (observer,
1159 "authorize-authenticated-peer",
1160 G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1162 g_object_unref (observer);
1164 g_dbus_server_start (server);
1166 service_loop = g_main_loop_new (service_context, FALSE);
1167 g_main_loop_run (service_loop);
1169 g_main_context_pop_thread_default (service_context);
1171 g_main_loop_unref (service_loop);
1172 g_main_context_unref (service_context);
1174 /* test code specifically unrefs the server - see below */
1175 g_assert (server == NULL);
1181 test_nonce_tcp (void)
1185 GThread *service_thread;
1190 const gchar *address;
1192 memset (&data, '\0', sizeof (PeerData));
1193 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1197 service_loop = NULL;
1198 service_thread = g_thread_new ("nonce-tcp-service",
1199 nonce_tcp_service_thread_func,
1201 while (service_loop == NULL)
1203 g_assert (server != NULL);
1206 /* bring up a connection and accept it */
1207 data.accept_connection = TRUE;
1209 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1210 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1211 NULL, /* GDBusAuthObserver */
1212 NULL, /* cancellable */
1214 g_assert_no_error (error);
1215 g_assert (c != NULL);
1216 while (data.current_connections->len < 1)
1218 g_assert_cmpint (data.current_connections->len, ==, 1);
1219 g_assert_cmpint (data.num_connection_attempts, ==, 1);
1220 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1221 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1224 /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1227 address = g_dbus_server_get_client_address (server);
1229 s = strstr (address, "noncefile=");
1230 g_assert (s != NULL);
1231 s += sizeof "noncefile=" - 1;
1232 nonce_file = g_strdup (s);
1234 /* First try invalid data in the nonce file - this will actually
1235 * make the client send this and the server will reject it. The way
1236 * it works is that if the nonce doesn't match, the server will
1237 * simply close the connection. So, from the client point of view,
1238 * we can see a variety of errors.
1241 res = g_file_set_contents (nonce_file,
1245 g_assert_no_error (error);
1247 c = g_dbus_connection_new_for_address_sync (address,
1248 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1249 NULL, /* GDBusAuthObserver */
1250 NULL, /* cancellable */
1252 _g_assert_error_domain (error, G_IO_ERROR);
1253 g_error_free (error);
1254 g_assert (c == NULL);
1256 /* Then try with a nonce-file of incorrect length - this will make
1257 * the client complain - we won't even try connecting to the server
1261 res = g_file_set_contents (nonce_file,
1262 "0123456789012345_",
1265 g_assert_no_error (error);
1267 c = g_dbus_connection_new_for_address_sync (address,
1268 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1269 NULL, /* GDBusAuthObserver */
1270 NULL, /* cancellable */
1272 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1273 g_error_free (error);
1274 g_assert (c == NULL);
1276 /* Finally try with no nonce-file at all */
1277 g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1279 c = g_dbus_connection_new_for_address_sync (address,
1280 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1281 NULL, /* GDBusAuthObserver */
1282 NULL, /* cancellable */
1284 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1285 g_error_free (error);
1286 g_assert (c == NULL);
1288 g_free (nonce_file);
1290 g_dbus_server_stop (server);
1291 g_object_unref (server);
1294 g_main_loop_quit (service_loop);
1295 g_thread_join (service_thread);
1299 test_credentials (void)
1301 GCredentials *c1, *c2;
1305 c1 = g_credentials_new ();
1306 c2 = g_credentials_new ();
1309 if (g_credentials_set_unix_user (c2, getuid (), &error))
1310 g_assert_no_error (error);
1312 g_clear_error (&error);
1313 g_assert (g_credentials_is_same_user (c1, c2, &error));
1314 g_assert_no_error (error);
1316 desc = g_credentials_to_string (c1);
1317 g_assert (desc != NULL);
1320 g_object_unref (c1);
1321 g_object_unref (c2);
1324 /* ---------------------------------------------------------------------------------------------------- */
1328 /* Chosen to be big enough to overflow the socket buffer */
1329 #define OVERFLOW_NUM_SIGNALS 5000
1330 #define OVERFLOW_TIMEOUT_SEC 10
1332 static GDBusMessage *
1333 overflow_filter_func (GDBusConnection *connection,
1334 GDBusMessage *message,
1338 volatile gint *counter = user_data;
1344 overflow_on_500ms_later_func (gpointer user_data)
1346 g_main_loop_quit (loop);
1347 return FALSE; /* don't keep the idle */
1351 test_overflow (void)
1356 GSocketConnection *socket_connection;
1357 GDBusConnection *producer, *consumer;
1360 volatile gint n_messages_received;
1361 volatile gint n_messages_sent;
1363 g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
1366 socket = g_socket_new_from_fd (sv[0], &error);
1367 g_assert_no_error (error);
1368 socket_connection = g_socket_connection_factory_create_connection (socket);
1369 g_assert (socket_connection != NULL);
1370 g_object_unref (socket);
1371 producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1373 G_DBUS_CONNECTION_FLAGS_NONE,
1374 NULL, /* GDBusAuthObserver */
1375 NULL, /* GCancellable */
1377 g_dbus_connection_set_exit_on_close (producer, TRUE);
1378 g_assert_no_error (error);
1379 g_object_unref (socket_connection);
1380 n_messages_sent = 0;
1381 g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
1383 /* send enough data that we get an EAGAIN */
1384 for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
1387 g_dbus_connection_emit_signal (producer,
1388 NULL, /* destination */
1390 "org.foo.Interface",
1392 g_variant_new ("(s)", "a string"),
1394 g_assert_no_error (error);
1397 /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
1398 * kernel buffers) and verify that n_messages_sent <
1399 * OVERFLOW_NUM_SIGNALS
1401 * This is to verify that not all the submitted messages have been
1402 * sent to the underlying transport.
1404 g_timeout_add (500, overflow_on_500ms_later_func, NULL);
1405 g_main_loop_run (loop);
1406 g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
1408 /* now suck it all out as a client, and add it up */
1409 socket = g_socket_new_from_fd (sv[1], &error);
1410 g_assert_no_error (error);
1411 socket_connection = g_socket_connection_factory_create_connection (socket);
1412 g_assert (socket_connection != NULL);
1413 g_object_unref (socket);
1414 consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1416 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
1417 NULL, /* GDBusAuthObserver */
1418 NULL, /* GCancellable */
1420 g_assert_no_error (error);
1421 g_object_unref (socket_connection);
1422 n_messages_received = 0;
1423 g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
1424 g_dbus_connection_start_message_processing (consumer);
1426 timer = g_timer_new ();
1427 g_timer_start (timer);
1429 while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
1430 g_main_context_iteration (NULL, FALSE);
1432 g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
1433 g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
1435 g_timer_destroy (timer);
1436 g_object_unref (consumer);
1437 g_object_unref (producer);
1441 test_overflow (void)
1443 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
1447 /* ---------------------------------------------------------------------------------------------------- */
1450 tcp_anonymous_on_new_connection (GDBusServer *server,
1451 GDBusConnection *connection,
1454 gboolean *seen_connection = user_data;
1455 *seen_connection = TRUE;
1460 tcp_anonymous_service_thread_func (gpointer user_data)
1462 gboolean *seen_connection = user_data;
1463 GMainContext *service_context;
1466 service_context = g_main_context_new ();
1467 g_main_context_push_thread_default (service_context);
1470 server = g_dbus_server_new_sync ("tcp:",
1471 G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS,
1473 NULL, /* GDBusObserver* */
1474 NULL, /* GCancellable* */
1476 g_assert_no_error (error);
1478 g_signal_connect (server,
1480 G_CALLBACK (tcp_anonymous_on_new_connection),
1483 g_dbus_server_start (server);
1485 service_loop = g_main_loop_new (service_context, FALSE);
1486 g_main_loop_run (service_loop);
1488 g_main_context_pop_thread_default (service_context);
1490 g_main_loop_unref (service_loop);
1491 g_main_context_unref (service_context);
1497 test_tcp_anonymous (void)
1499 gboolean seen_connection;
1500 GThread *service_thread;
1501 GDBusConnection *connection;
1504 seen_connection = FALSE;
1505 service_loop = NULL;
1506 service_thread = g_thread_new ("tcp-anon-service",
1507 tcp_anonymous_service_thread_func,
1509 while (service_loop == NULL)
1511 g_assert (server != NULL);
1514 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1515 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1516 NULL, /* GDBusAuthObserver* */
1517 NULL, /* GCancellable */
1519 g_assert_no_error (error);
1520 g_assert (connection != NULL);
1522 while (!seen_connection)
1525 g_object_unref (connection);
1527 g_main_loop_quit (service_loop);
1528 g_dbus_server_stop (server);
1529 g_object_unref (server);
1532 g_thread_join (service_thread);
1535 /* ---------------------------------------------------------------------------------------------------- */
1542 GDBusNodeInfo *introspection_data = NULL;
1543 gchar *tmpdir = NULL;
1546 g_test_init (&argc, &argv, NULL);
1548 g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
1550 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1551 g_assert (introspection_data != NULL);
1552 test_interface_introspection_data = introspection_data->interfaces[0];
1554 test_guid = g_dbus_generate_guid ();
1558 if (g_unix_socket_address_abstract_names_supported ())
1559 tmp_address = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
1562 tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
1563 tmp_address = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
1567 tmp_address = g_strdup ("nonce-tcp:");
1569 /* all the tests rely on a shared main loop */
1570 loop = g_main_loop_new (NULL, FALSE);
1572 g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1573 g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1574 g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1575 g_test_add_func ("/gdbus/tcp-anonymous", test_tcp_anonymous);
1576 g_test_add_func ("/gdbus/credentials", test_credentials);
1577 g_test_add_func ("/gdbus/overflow", test_overflow);
1581 g_main_loop_unref (loop);
1583 g_dbus_node_info_unref (introspection_data);
1585 g_free (tmp_address);