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 */
605 service_thread = g_thread_new ("test_peer",
610 while (service_loop == NULL)
612 g_assert (server != NULL);
614 /* bring up a connection and accept it */
615 data.accept_connection = TRUE;
617 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
618 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
619 NULL, /* GDBusAuthObserver */
620 NULL, /* cancellable */
622 g_assert_no_error (error);
623 g_assert (c != NULL);
624 while (data.current_connections->len < 1)
625 g_main_loop_run (loop);
626 g_assert_cmpint (data.current_connections->len, ==, 1);
627 g_assert_cmpint (data.num_connection_attempts, ==, 1);
628 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
629 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
631 /* check that we create a proxy, read properties, receive signals and invoke
632 * the HelloPeer() method. Since the server runs in another thread it's fine
633 * to use synchronous blocking API here.
636 proxy = g_dbus_proxy_new_sync (c,
637 G_DBUS_PROXY_FLAGS_NONE,
640 "/org/gtk/GDBus/PeerTestObject",
641 "org.gtk.GDBus.PeerTestInterface",
642 NULL, /* GCancellable */
644 g_assert_no_error (error);
645 g_assert (proxy != NULL);
647 value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
648 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
650 /* try invoking a method */
652 result = g_dbus_proxy_call_sync (proxy,
654 g_variant_new ("(s)", "Hey Peer!"),
655 G_DBUS_CALL_FLAGS_NONE,
657 NULL, /* GCancellable */
659 g_assert_no_error (error);
660 g_variant_get (result, "(&s)", &s);
661 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
662 g_variant_unref (result);
663 g_assert_cmpint (data.num_method_calls, ==, 1);
665 /* make the other peer emit a signal - catch it */
666 signal_handler_id = g_signal_connect (proxy,
668 G_CALLBACK (on_proxy_signal_received),
670 g_assert (!data.signal_received);
671 g_dbus_proxy_call (proxy,
673 NULL, /* no arguments */
674 G_DBUS_CALL_FLAGS_NONE,
676 NULL, /* GCancellable */
677 NULL, /* GAsyncReadyCallback - we don't care about the result */
678 NULL); /* user_data */
679 g_main_loop_run (loop);
680 g_assert (data.signal_received);
681 g_assert_cmpint (data.num_method_calls, ==, 2);
682 g_signal_handler_disconnect (proxy, signal_handler_id);
684 /* Also ensure that messages with the sender header-field set gets
685 * delivered to the proxy - note that this doesn't really make sense
686 * e.g. names are meaning-less in a peer-to-peer case... but we
687 * support it because it makes sense in certain bridging
688 * applications - see e.g. #623815.
690 signal_handler_id = g_signal_connect (proxy,
692 G_CALLBACK (on_proxy_signal_received_with_name_set),
694 data.signal_received = FALSE;
695 g_dbus_proxy_call (proxy,
696 "EmitSignalWithNameSet",
697 NULL, /* no arguments */
698 G_DBUS_CALL_FLAGS_NONE,
700 NULL, /* GCancellable */
701 NULL, /* GAsyncReadyCallback - we don't care about the result */
702 NULL); /* user_data */
703 g_main_loop_run (loop);
704 g_assert (data.signal_received);
705 g_assert_cmpint (data.num_method_calls, ==, 3);
706 g_signal_handler_disconnect (proxy, signal_handler_id);
708 /* check for UNIX fd passing */
711 GDBusMessage *method_call_message;
712 GDBusMessage *method_reply_message;
713 GUnixFDList *fd_list;
720 method_call_message = g_dbus_message_new_method_call (NULL, /* name */
721 "/org/gtk/GDBus/PeerTestObject",
722 "org.gtk.GDBus.PeerTestInterface",
724 g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", "/etc/hosts"));
726 method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
728 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
730 NULL, /* out_serial */
731 NULL, /* cancellable */
733 g_assert_no_error (error);
734 g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
735 fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
736 g_assert (fd_list != NULL);
737 g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
739 fd = g_unix_fd_list_get (fd_list, 0, &error);
740 g_assert_no_error (error);
741 g_object_unref (method_call_message);
742 g_object_unref (method_reply_message);
746 buf = read_all_from_fd (fd, &len, &error);
747 g_assert_no_error (error);
748 g_assert (buf != NULL);
752 g_file_get_contents ("/etc/hosts",
756 g_assert_no_error (error);
757 g_assert_cmpint (len, ==, len2);
758 g_assert (memcmp (buf, buf2, len) == 0);
764 result = g_dbus_proxy_call_sync (proxy,
766 g_variant_new ("(s)", "boo"),
767 G_DBUS_CALL_FLAGS_NONE,
769 NULL, /* GCancellable */
771 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
772 g_assert (result == NULL);
773 g_error_free (error);
774 #endif /* G_OS_UNIX */
776 /* Check that g_socket_get_credentials() work - this really should
777 * be in a GSocket-specific test suite but no such test suite exists
782 GCredentials *credentials;
783 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (g_dbus_connection_get_stream (c)));
784 g_assert (G_IS_SOCKET (socket));
786 credentials = g_socket_get_credentials (socket, &error);
789 struct ucred *native_creds;
790 g_assert_no_error (error);
791 g_assert (G_IS_CREDENTIALS (credentials));
792 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED);
793 g_assert (native_creds != NULL);
794 g_assert (native_creds->uid == getuid ());
795 g_assert (native_creds->gid == getgid ());
796 g_assert (native_creds->pid == getpid ());
798 g_object_unref (credentials);
799 #elif defined (__OpenBSD__)
801 struct sockpeercred *native_creds;
802 g_assert_no_error (error);
803 g_assert (G_IS_CREDENTIALS (credentials));
804 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED);
805 g_assert (native_creds != NULL);
806 g_assert (native_creds->uid == getuid ());
807 g_assert (native_creds->gid == getgid ());
808 g_assert (native_creds->pid == getpid ());
810 g_object_unref (credentials);
812 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
813 g_assert (credentials == NULL);
818 /* bring up a connection - don't accept it - this should fail
820 data.accept_connection = FALSE;
822 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
823 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
824 NULL, /* GDBusAuthObserver */
825 NULL, /* cancellable */
827 _g_assert_error_domain (error, G_IO_ERROR);
828 g_error_free (error);
829 g_assert (c2 == NULL);
832 /* TODO: THIS TEST DOESN'T WORK YET */
834 /* bring up a connection - accept it.. then disconnect from the client side - check
835 * that the server side gets the disconnect signal.
838 data.accept_connection = TRUE;
839 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
840 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
841 NULL, /* GDBusAuthObserver */
842 NULL, /* cancellable */
844 g_assert_no_error (error);
845 g_assert (c2 != NULL);
846 g_assert (!g_dbus_connection_get_is_disconnected (c2));
847 while (data.num_connection_attempts < 3)
848 g_main_loop_run (loop);
849 g_assert_cmpint (data.current_connections->len, ==, 2);
850 g_assert_cmpint (data.num_connection_attempts, ==, 3);
851 g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
852 g_idle_add (on_do_disconnect_in_idle, c2);
853 g_debug ("==================================================");
854 g_debug ("==================================================");
855 g_debug ("==================================================");
856 g_debug ("waiting for disconnect on connection %p, stream %p",
857 data.current_connections->pdata[1],
858 g_dbus_connection_get_stream (data.current_connections->pdata[1]));
860 g_timeout_add (2000, check_connection, &data);
861 //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
862 g_main_loop_run (loop);
863 g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
864 g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
867 /* unref the server and stop listening for new connections
869 * This won't bring down the established connections - check that c is still connected
870 * by invoking a method
872 //g_socket_service_stop (service);
873 //g_object_unref (service);
874 g_dbus_server_stop (server);
875 g_object_unref (server);
879 result = g_dbus_proxy_call_sync (proxy,
881 g_variant_new ("(s)", "Hey Again Peer!"),
882 G_DBUS_CALL_FLAGS_NONE,
884 NULL, /* GCancellable */
886 g_assert_no_error (error);
887 g_variant_get (result, "(&s)", &s);
888 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
889 g_variant_unref (result);
890 g_assert_cmpint (data.num_method_calls, ==, 5);
893 /* TODO: THIS TEST DOESN'T WORK YET */
895 /* now disconnect from the server side - check that the client side gets the signal */
896 g_assert_cmpint (data.current_connections->len, ==, 1);
897 g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
898 g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
899 if (!g_dbus_connection_get_is_disconnected (c))
900 _g_assert_signal_received (c, "closed");
901 g_assert (g_dbus_connection_get_is_disconnected (c));
905 g_ptr_array_unref (data.current_connections);
906 g_object_unref (proxy);
908 g_main_loop_quit (service_loop);
909 g_thread_join (service_thread);
912 /* ---------------------------------------------------------------------------------------------------- */
917 GMainContext *context;
924 dmp_data_free (DmpData *data)
926 g_main_loop_unref (data->loop);
927 g_main_context_unref (data->context);
928 g_object_unref (data->server);
929 g_list_foreach (data->connections, (GFunc) g_object_unref, NULL);
930 g_list_free (data->connections);
935 dmp_on_method_call (GDBusConnection *connection,
937 const gchar *object_path,
938 const gchar *interface_name,
939 const gchar *method_name,
940 GVariant *parameters,
941 GDBusMethodInvocation *invocation,
944 //DmpData *data = user_data;
947 g_variant_get (parameters,
951 g_dbus_method_invocation_return_value (invocation,
952 g_variant_new ("(i)", first + second));
955 static const GDBusInterfaceVTable dmp_interface_vtable =
958 NULL, /* get_property */
959 NULL /* set_property */
963 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
965 dmp_on_new_connection (GDBusServer *server,
966 GDBusConnection *connection,
969 DmpData *data = user_data;
973 /* accept the connection */
974 data->connections = g_list_prepend (data->connections, g_object_ref (connection));
977 node = g_dbus_node_info_new_for_xml ("<node>"
978 " <interface name='org.gtk.GDBus.DmpInterface'>"
979 " <method name='AddPair'>"
980 " <arg type='i' name='first' direction='in'/>"
981 " <arg type='i' name='second' direction='in'/>"
982 " <arg type='i' name='sum' direction='out'/>"
987 g_assert_no_error (error);
989 /* sleep 100ms before exporting an object - this is to test that
990 * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
991 * (GDBusServer uses this feature).
995 /* export an object */
997 g_dbus_connection_register_object (connection,
1000 &dmp_interface_vtable,
1004 g_dbus_node_info_unref (node);
1010 dmp_thread_func (gpointer user_data)
1012 DmpData *data = user_data;
1016 data->context = g_main_context_new ();
1017 g_main_context_push_thread_default (data->context);
1020 guid = g_dbus_generate_guid ();
1021 data->server = g_dbus_server_new_sync (tmp_address,
1022 G_DBUS_SERVER_FLAGS_NONE,
1024 NULL, /* GDBusAuthObserver */
1025 NULL, /* GCancellable */
1027 g_assert_no_error (error);
1028 g_signal_connect (data->server,
1030 G_CALLBACK (dmp_on_new_connection),
1033 g_dbus_server_start (data->server);
1035 data->loop = g_main_loop_new (data->context, FALSE);
1036 g_main_loop_run (data->loop);
1038 g_main_context_pop_thread_default (data->context);
1045 delayed_message_processing (void)
1049 GThread *service_thread;
1052 data = g_new0 (DmpData, 1);
1055 service_thread = g_thread_new ("dmp",
1060 while (data->server == NULL || !g_dbus_server_is_active (data->server))
1063 for (n = 0; n < 5; n++)
1070 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
1071 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1072 NULL, /* GDBusAuthObserver */
1073 NULL, /* GCancellable */
1075 g_assert_no_error (error);
1078 res = g_dbus_connection_call_sync (c,
1079 NULL, /* bus name */
1081 "org.gtk.GDBus.DmpInterface",
1083 g_variant_new ("(ii)", 2, n),
1084 G_VARIANT_TYPE ("(i)"),
1085 G_DBUS_CALL_FLAGS_NONE,
1086 -1, /* timeout_msec */
1087 NULL, /* GCancellable */
1089 g_assert_no_error (error);
1090 g_variant_get (res, "(i)", &val);
1091 g_assert_cmpint (val, ==, 2 + n);
1092 g_variant_unref (res);
1096 g_main_loop_quit (data->loop);
1097 g_thread_join (service_thread);
1098 dmp_data_free (data);
1101 /* ---------------------------------------------------------------------------------------------------- */
1104 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1106 GCredentials *credentials,
1109 PeerData *data = user_data;
1110 gboolean authorized;
1112 data->num_connection_attempts++;
1115 if (!data->accept_connection)
1118 g_main_loop_quit (loop);
1124 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1126 nonce_tcp_on_new_connection (GDBusServer *server,
1127 GDBusConnection *connection,
1130 PeerData *data = user_data;
1132 g_ptr_array_add (data->current_connections, g_object_ref (connection));
1134 g_main_loop_quit (loop);
1140 nonce_tcp_service_thread_func (gpointer user_data)
1142 PeerData *data = user_data;
1143 GMainContext *service_context;
1144 GDBusAuthObserver *observer;
1147 service_context = g_main_context_new ();
1148 g_main_context_push_thread_default (service_context);
1151 observer = g_dbus_auth_observer_new ();
1152 server = g_dbus_server_new_sync ("nonce-tcp:",
1153 G_DBUS_SERVER_FLAGS_NONE,
1156 NULL, /* cancellable */
1158 g_assert_no_error (error);
1160 g_signal_connect (server,
1162 G_CALLBACK (nonce_tcp_on_new_connection),
1164 g_signal_connect (observer,
1165 "authorize-authenticated-peer",
1166 G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1168 g_object_unref (observer);
1170 g_dbus_server_start (server);
1172 service_loop = g_main_loop_new (service_context, FALSE);
1173 g_main_loop_run (service_loop);
1175 g_main_context_pop_thread_default (service_context);
1177 g_main_loop_unref (service_loop);
1178 g_main_context_unref (service_context);
1180 /* test code specifically unrefs the server - see below */
1181 g_assert (server == NULL);
1187 test_nonce_tcp (void)
1191 GThread *service_thread;
1196 const gchar *address;
1198 memset (&data, '\0', sizeof (PeerData));
1199 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1203 service_loop = NULL;
1204 service_thread = g_thread_new ("nonce-tcp-service",
1205 nonce_tcp_service_thread_func,
1209 while (service_loop == NULL)
1211 g_assert (server != NULL);
1214 /* bring up a connection and accept it */
1215 data.accept_connection = TRUE;
1217 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1218 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1219 NULL, /* GDBusAuthObserver */
1220 NULL, /* cancellable */
1222 g_assert_no_error (error);
1223 g_assert (c != NULL);
1224 while (data.current_connections->len < 1)
1226 g_assert_cmpint (data.current_connections->len, ==, 1);
1227 g_assert_cmpint (data.num_connection_attempts, ==, 1);
1228 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1229 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1232 /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1235 address = g_dbus_server_get_client_address (server);
1237 s = strstr (address, "noncefile=");
1238 g_assert (s != NULL);
1239 s += sizeof "noncefile=" - 1;
1240 nonce_file = g_strdup (s);
1242 /* First try invalid data in the nonce file - this will actually
1243 * make the client send this and the server will reject it. The way
1244 * it works is that if the nonce doesn't match, the server will
1245 * simply close the connection. So, from the client point of view,
1246 * we can see a variety of errors.
1249 res = g_file_set_contents (nonce_file,
1253 g_assert_no_error (error);
1255 c = g_dbus_connection_new_for_address_sync (address,
1256 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1257 NULL, /* GDBusAuthObserver */
1258 NULL, /* cancellable */
1260 _g_assert_error_domain (error, G_IO_ERROR);
1261 g_error_free (error);
1262 g_assert (c == NULL);
1264 /* Then try with a nonce-file of incorrect length - this will make
1265 * the client complain - we won't even try connecting to the server
1269 res = g_file_set_contents (nonce_file,
1270 "0123456789012345_",
1273 g_assert_no_error (error);
1275 c = g_dbus_connection_new_for_address_sync (address,
1276 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1277 NULL, /* GDBusAuthObserver */
1278 NULL, /* cancellable */
1280 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1281 g_error_free (error);
1282 g_assert (c == NULL);
1284 /* Finally try with no nonce-file at all */
1285 g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1287 c = g_dbus_connection_new_for_address_sync (address,
1288 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1289 NULL, /* GDBusAuthObserver */
1290 NULL, /* cancellable */
1292 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1293 g_error_free (error);
1294 g_assert (c == NULL);
1296 g_free (nonce_file);
1298 g_dbus_server_stop (server);
1299 g_object_unref (server);
1302 g_main_loop_quit (service_loop);
1303 g_thread_join (service_thread);
1307 test_credentials (void)
1309 GCredentials *c1, *c2;
1313 c1 = g_credentials_new ();
1314 c2 = g_credentials_new ();
1317 if (g_credentials_set_unix_user (c2, getuid (), &error))
1318 g_assert_no_error (error);
1320 g_clear_error (&error);
1321 g_assert (g_credentials_is_same_user (c1, c2, &error));
1322 g_assert_no_error (error);
1324 desc = g_credentials_to_string (c1);
1325 g_assert (desc != NULL);
1328 g_object_unref (c1);
1329 g_object_unref (c2);
1332 /* ---------------------------------------------------------------------------------------------------- */
1336 /* Chosen to be big enough to overflow the socket buffer */
1337 #define OVERFLOW_NUM_SIGNALS 5000
1338 #define OVERFLOW_TIMEOUT_SEC 10
1340 static GDBusMessage *
1341 overflow_filter_func (GDBusConnection *connection,
1342 GDBusMessage *message,
1346 volatile gint *counter = user_data;
1352 overflow_on_500ms_later_func (gpointer user_data)
1354 g_main_loop_quit (loop);
1355 return FALSE; /* don't keep the idle */
1359 test_overflow (void)
1364 GSocketConnection *socket_connection;
1365 GDBusConnection *producer, *consumer;
1368 volatile gint n_messages_received;
1369 volatile gint n_messages_sent;
1371 g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
1374 socket = g_socket_new_from_fd (sv[0], &error);
1375 g_assert_no_error (error);
1376 socket_connection = g_socket_connection_factory_create_connection (socket);
1377 g_assert (socket_connection != NULL);
1378 g_object_unref (socket);
1379 producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1381 G_DBUS_CONNECTION_FLAGS_NONE,
1382 NULL, /* GDBusAuthObserver */
1383 NULL, /* GCancellable */
1385 g_dbus_connection_set_exit_on_close (producer, TRUE);
1386 g_assert_no_error (error);
1387 g_object_unref (socket_connection);
1388 n_messages_sent = 0;
1389 g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
1391 /* send enough data that we get an EAGAIN */
1392 for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
1395 g_dbus_connection_emit_signal (producer,
1396 NULL, /* destination */
1398 "org.foo.Interface",
1400 g_variant_new ("(s)", "a string"),
1402 g_assert_no_error (error);
1405 /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
1406 * kernel buffers) and verify that n_messages_sent <
1407 * OVERFLOW_NUM_SIGNALS
1409 * This is to verify that not all the submitted messages have been
1410 * sent to the underlying transport.
1412 g_timeout_add (500, overflow_on_500ms_later_func, NULL);
1413 g_main_loop_run (loop);
1414 g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
1416 /* now suck it all out as a client, and add it up */
1417 socket = g_socket_new_from_fd (sv[1], &error);
1418 g_assert_no_error (error);
1419 socket_connection = g_socket_connection_factory_create_connection (socket);
1420 g_assert (socket_connection != NULL);
1421 g_object_unref (socket);
1422 consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1424 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
1425 NULL, /* GDBusAuthObserver */
1426 NULL, /* GCancellable */
1428 g_assert_no_error (error);
1429 g_object_unref (socket_connection);
1430 n_messages_received = 0;
1431 g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
1432 g_dbus_connection_start_message_processing (consumer);
1434 timer = g_timer_new ();
1435 g_timer_start (timer);
1437 while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
1438 g_main_context_iteration (NULL, FALSE);
1440 g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
1441 g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
1443 g_timer_destroy (timer);
1444 g_object_unref (consumer);
1445 g_object_unref (producer);
1449 test_overflow (void)
1451 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
1455 /* ---------------------------------------------------------------------------------------------------- */
1458 tcp_anonymous_on_new_connection (GDBusServer *server,
1459 GDBusConnection *connection,
1462 gboolean *seen_connection = user_data;
1463 *seen_connection = TRUE;
1468 tcp_anonymous_service_thread_func (gpointer user_data)
1470 gboolean *seen_connection = user_data;
1471 GMainContext *service_context;
1474 service_context = g_main_context_new ();
1475 g_main_context_push_thread_default (service_context);
1478 server = g_dbus_server_new_sync ("tcp:",
1479 G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS,
1481 NULL, /* GDBusObserver* */
1482 NULL, /* GCancellable* */
1484 g_assert_no_error (error);
1486 g_signal_connect (server,
1488 G_CALLBACK (tcp_anonymous_on_new_connection),
1491 g_dbus_server_start (server);
1493 service_loop = g_main_loop_new (service_context, FALSE);
1494 g_main_loop_run (service_loop);
1496 g_main_context_pop_thread_default (service_context);
1498 g_main_loop_unref (service_loop);
1499 g_main_context_unref (service_context);
1505 test_tcp_anonymous (void)
1507 gboolean seen_connection;
1508 GThread *service_thread;
1509 GDBusConnection *connection;
1512 seen_connection = FALSE;
1513 service_loop = NULL;
1514 service_thread = g_thread_new ("tcp-anon-service",
1515 tcp_anonymous_service_thread_func,
1516 &seen_connection, /* user_data */
1517 TRUE, /* joinable */
1519 while (service_loop == NULL)
1521 g_assert (server != NULL);
1524 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1525 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1526 NULL, /* GDBusAuthObserver* */
1527 NULL, /* GCancellable */
1529 g_assert_no_error (error);
1530 g_assert (connection != NULL);
1532 while (!seen_connection)
1535 g_object_unref (connection);
1537 g_main_loop_quit (service_loop);
1538 g_dbus_server_stop (server);
1539 g_object_unref (server);
1542 g_thread_join (service_thread);
1545 /* ---------------------------------------------------------------------------------------------------- */
1552 GDBusNodeInfo *introspection_data = NULL;
1553 gchar *tmpdir = NULL;
1556 g_test_init (&argc, &argv, NULL);
1558 g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
1560 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1561 g_assert (introspection_data != NULL);
1562 test_interface_introspection_data = introspection_data->interfaces[0];
1564 test_guid = g_dbus_generate_guid ();
1568 if (g_unix_socket_address_abstract_names_supported ())
1569 tmp_address = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
1572 tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
1573 tmp_address = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
1577 tmp_address = g_strdup ("nonce-tcp:");
1579 /* all the tests rely on a shared main loop */
1580 loop = g_main_loop_new (NULL, FALSE);
1582 g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1583 g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1584 g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1585 g_test_add_func ("/gdbus/tcp-anonymous", test_tcp_anonymous);
1586 g_test_add_func ("/gdbus/credentials", test_credentials);
1587 g_test_add_func ("/gdbus/overflow", test_overflow);
1591 g_main_loop_unref (loop);
1593 g_dbus_node_info_unref (introspection_data);
1595 g_free (tmp_address);