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",
609 while (service_loop == NULL)
611 g_assert (server != NULL);
613 /* bring up a connection and accept it */
614 data.accept_connection = TRUE;
616 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
617 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
618 NULL, /* GDBusAuthObserver */
619 NULL, /* cancellable */
621 g_assert_no_error (error);
622 g_assert (c != NULL);
623 while (data.current_connections->len < 1)
624 g_main_loop_run (loop);
625 g_assert_cmpint (data.current_connections->len, ==, 1);
626 g_assert_cmpint (data.num_connection_attempts, ==, 1);
627 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
628 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
630 /* check that we create a proxy, read properties, receive signals and invoke
631 * the HelloPeer() method. Since the server runs in another thread it's fine
632 * to use synchronous blocking API here.
635 proxy = g_dbus_proxy_new_sync (c,
636 G_DBUS_PROXY_FLAGS_NONE,
639 "/org/gtk/GDBus/PeerTestObject",
640 "org.gtk.GDBus.PeerTestInterface",
641 NULL, /* GCancellable */
643 g_assert_no_error (error);
644 g_assert (proxy != NULL);
646 value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
647 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
649 /* try invoking a method */
651 result = g_dbus_proxy_call_sync (proxy,
653 g_variant_new ("(s)", "Hey Peer!"),
654 G_DBUS_CALL_FLAGS_NONE,
656 NULL, /* GCancellable */
658 g_assert_no_error (error);
659 g_variant_get (result, "(&s)", &s);
660 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
661 g_variant_unref (result);
662 g_assert_cmpint (data.num_method_calls, ==, 1);
664 /* make the other peer emit a signal - catch it */
665 signal_handler_id = g_signal_connect (proxy,
667 G_CALLBACK (on_proxy_signal_received),
669 g_assert (!data.signal_received);
670 g_dbus_proxy_call (proxy,
672 NULL, /* no arguments */
673 G_DBUS_CALL_FLAGS_NONE,
675 NULL, /* GCancellable */
676 NULL, /* GAsyncReadyCallback - we don't care about the result */
677 NULL); /* user_data */
678 g_main_loop_run (loop);
679 g_assert (data.signal_received);
680 g_assert_cmpint (data.num_method_calls, ==, 2);
681 g_signal_handler_disconnect (proxy, signal_handler_id);
683 /* Also ensure that messages with the sender header-field set gets
684 * delivered to the proxy - note that this doesn't really make sense
685 * e.g. names are meaning-less in a peer-to-peer case... but we
686 * support it because it makes sense in certain bridging
687 * applications - see e.g. #623815.
689 signal_handler_id = g_signal_connect (proxy,
691 G_CALLBACK (on_proxy_signal_received_with_name_set),
693 data.signal_received = FALSE;
694 g_dbus_proxy_call (proxy,
695 "EmitSignalWithNameSet",
696 NULL, /* no arguments */
697 G_DBUS_CALL_FLAGS_NONE,
699 NULL, /* GCancellable */
700 NULL, /* GAsyncReadyCallback - we don't care about the result */
701 NULL); /* user_data */
702 g_main_loop_run (loop);
703 g_assert (data.signal_received);
704 g_assert_cmpint (data.num_method_calls, ==, 3);
705 g_signal_handler_disconnect (proxy, signal_handler_id);
707 /* check for UNIX fd passing */
710 GDBusMessage *method_call_message;
711 GDBusMessage *method_reply_message;
712 GUnixFDList *fd_list;
719 method_call_message = g_dbus_message_new_method_call (NULL, /* name */
720 "/org/gtk/GDBus/PeerTestObject",
721 "org.gtk.GDBus.PeerTestInterface",
723 g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", "/etc/hosts"));
725 method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
727 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
729 NULL, /* out_serial */
730 NULL, /* cancellable */
732 g_assert_no_error (error);
733 g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
734 fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
735 g_assert (fd_list != NULL);
736 g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
738 fd = g_unix_fd_list_get (fd_list, 0, &error);
739 g_assert_no_error (error);
740 g_object_unref (method_call_message);
741 g_object_unref (method_reply_message);
745 buf = read_all_from_fd (fd, &len, &error);
746 g_assert_no_error (error);
747 g_assert (buf != NULL);
751 g_file_get_contents ("/etc/hosts",
755 g_assert_no_error (error);
756 g_assert_cmpint (len, ==, len2);
757 g_assert (memcmp (buf, buf2, len) == 0);
763 result = g_dbus_proxy_call_sync (proxy,
765 g_variant_new ("(s)", "boo"),
766 G_DBUS_CALL_FLAGS_NONE,
768 NULL, /* GCancellable */
770 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
771 g_assert (result == NULL);
772 g_error_free (error);
773 #endif /* G_OS_UNIX */
775 /* Check that g_socket_get_credentials() work - this really should
776 * be in a GSocket-specific test suite but no such test suite exists
781 GCredentials *credentials;
782 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (g_dbus_connection_get_stream (c)));
783 g_assert (G_IS_SOCKET (socket));
785 credentials = g_socket_get_credentials (socket, &error);
788 struct ucred *native_creds;
789 g_assert_no_error (error);
790 g_assert (G_IS_CREDENTIALS (credentials));
791 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED);
792 g_assert (native_creds != NULL);
793 g_assert (native_creds->uid == getuid ());
794 g_assert (native_creds->gid == getgid ());
795 g_assert (native_creds->pid == getpid ());
797 g_object_unref (credentials);
798 #elif defined (__OpenBSD__)
800 struct sockpeercred *native_creds;
801 g_assert_no_error (error);
802 g_assert (G_IS_CREDENTIALS (credentials));
803 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED);
804 g_assert (native_creds != NULL);
805 g_assert (native_creds->uid == getuid ());
806 g_assert (native_creds->gid == getgid ());
807 g_assert (native_creds->pid == getpid ());
809 g_object_unref (credentials);
811 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
812 g_assert (credentials == NULL);
817 /* bring up a connection - don't accept it - this should fail
819 data.accept_connection = FALSE;
821 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
822 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
823 NULL, /* GDBusAuthObserver */
824 NULL, /* cancellable */
826 _g_assert_error_domain (error, G_IO_ERROR);
827 g_error_free (error);
828 g_assert (c2 == NULL);
831 /* TODO: THIS TEST DOESN'T WORK YET */
833 /* bring up a connection - accept it.. then disconnect from the client side - check
834 * that the server side gets the disconnect signal.
837 data.accept_connection = TRUE;
838 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
839 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
840 NULL, /* GDBusAuthObserver */
841 NULL, /* cancellable */
843 g_assert_no_error (error);
844 g_assert (c2 != NULL);
845 g_assert (!g_dbus_connection_get_is_disconnected (c2));
846 while (data.num_connection_attempts < 3)
847 g_main_loop_run (loop);
848 g_assert_cmpint (data.current_connections->len, ==, 2);
849 g_assert_cmpint (data.num_connection_attempts, ==, 3);
850 g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
851 g_idle_add (on_do_disconnect_in_idle, c2);
852 g_debug ("==================================================");
853 g_debug ("==================================================");
854 g_debug ("==================================================");
855 g_debug ("waiting for disconnect on connection %p, stream %p",
856 data.current_connections->pdata[1],
857 g_dbus_connection_get_stream (data.current_connections->pdata[1]));
859 g_timeout_add (2000, check_connection, &data);
860 //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
861 g_main_loop_run (loop);
862 g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
863 g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
866 /* unref the server and stop listening for new connections
868 * This won't bring down the established connections - check that c is still connected
869 * by invoking a method
871 //g_socket_service_stop (service);
872 //g_object_unref (service);
873 g_dbus_server_stop (server);
874 g_object_unref (server);
878 result = g_dbus_proxy_call_sync (proxy,
880 g_variant_new ("(s)", "Hey Again Peer!"),
881 G_DBUS_CALL_FLAGS_NONE,
883 NULL, /* GCancellable */
885 g_assert_no_error (error);
886 g_variant_get (result, "(&s)", &s);
887 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
888 g_variant_unref (result);
889 g_assert_cmpint (data.num_method_calls, ==, 5);
892 /* TODO: THIS TEST DOESN'T WORK YET */
894 /* now disconnect from the server side - check that the client side gets the signal */
895 g_assert_cmpint (data.current_connections->len, ==, 1);
896 g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
897 g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
898 if (!g_dbus_connection_get_is_disconnected (c))
899 _g_assert_signal_received (c, "closed");
900 g_assert (g_dbus_connection_get_is_disconnected (c));
904 g_ptr_array_unref (data.current_connections);
905 g_object_unref (proxy);
907 g_main_loop_quit (service_loop);
908 g_thread_join (service_thread);
911 /* ---------------------------------------------------------------------------------------------------- */
916 GMainContext *context;
923 dmp_data_free (DmpData *data)
925 g_main_loop_unref (data->loop);
926 g_main_context_unref (data->context);
927 g_object_unref (data->server);
928 g_list_foreach (data->connections, (GFunc) g_object_unref, NULL);
929 g_list_free (data->connections);
934 dmp_on_method_call (GDBusConnection *connection,
936 const gchar *object_path,
937 const gchar *interface_name,
938 const gchar *method_name,
939 GVariant *parameters,
940 GDBusMethodInvocation *invocation,
943 //DmpData *data = user_data;
946 g_variant_get (parameters,
950 g_dbus_method_invocation_return_value (invocation,
951 g_variant_new ("(i)", first + second));
954 static const GDBusInterfaceVTable dmp_interface_vtable =
957 NULL, /* get_property */
958 NULL /* set_property */
962 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
964 dmp_on_new_connection (GDBusServer *server,
965 GDBusConnection *connection,
968 DmpData *data = user_data;
972 /* accept the connection */
973 data->connections = g_list_prepend (data->connections, g_object_ref (connection));
976 node = g_dbus_node_info_new_for_xml ("<node>"
977 " <interface name='org.gtk.GDBus.DmpInterface'>"
978 " <method name='AddPair'>"
979 " <arg type='i' name='first' direction='in'/>"
980 " <arg type='i' name='second' direction='in'/>"
981 " <arg type='i' name='sum' direction='out'/>"
986 g_assert_no_error (error);
988 /* sleep 100ms before exporting an object - this is to test that
989 * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
990 * (GDBusServer uses this feature).
994 /* export an object */
996 g_dbus_connection_register_object (connection,
999 &dmp_interface_vtable,
1003 g_dbus_node_info_unref (node);
1009 dmp_thread_func (gpointer user_data)
1011 DmpData *data = user_data;
1015 data->context = g_main_context_new ();
1016 g_main_context_push_thread_default (data->context);
1019 guid = g_dbus_generate_guid ();
1020 data->server = g_dbus_server_new_sync (tmp_address,
1021 G_DBUS_SERVER_FLAGS_NONE,
1023 NULL, /* GDBusAuthObserver */
1024 NULL, /* GCancellable */
1026 g_assert_no_error (error);
1027 g_signal_connect (data->server,
1029 G_CALLBACK (dmp_on_new_connection),
1032 g_dbus_server_start (data->server);
1034 data->loop = g_main_loop_new (data->context, FALSE);
1035 g_main_loop_run (data->loop);
1037 g_main_context_pop_thread_default (data->context);
1044 delayed_message_processing (void)
1048 GThread *service_thread;
1051 data = g_new0 (DmpData, 1);
1054 service_thread = g_thread_new ("dmp",
1058 while (data->server == NULL || !g_dbus_server_is_active (data->server))
1061 for (n = 0; n < 5; n++)
1068 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
1069 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1070 NULL, /* GDBusAuthObserver */
1071 NULL, /* GCancellable */
1073 g_assert_no_error (error);
1076 res = g_dbus_connection_call_sync (c,
1077 NULL, /* bus name */
1079 "org.gtk.GDBus.DmpInterface",
1081 g_variant_new ("(ii)", 2, n),
1082 G_VARIANT_TYPE ("(i)"),
1083 G_DBUS_CALL_FLAGS_NONE,
1084 -1, /* timeout_msec */
1085 NULL, /* GCancellable */
1087 g_assert_no_error (error);
1088 g_variant_get (res, "(i)", &val);
1089 g_assert_cmpint (val, ==, 2 + n);
1090 g_variant_unref (res);
1094 g_main_loop_quit (data->loop);
1095 g_thread_join (service_thread);
1096 dmp_data_free (data);
1099 /* ---------------------------------------------------------------------------------------------------- */
1102 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1104 GCredentials *credentials,
1107 PeerData *data = user_data;
1108 gboolean authorized;
1110 data->num_connection_attempts++;
1113 if (!data->accept_connection)
1116 g_main_loop_quit (loop);
1122 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1124 nonce_tcp_on_new_connection (GDBusServer *server,
1125 GDBusConnection *connection,
1128 PeerData *data = user_data;
1130 g_ptr_array_add (data->current_connections, g_object_ref (connection));
1132 g_main_loop_quit (loop);
1138 nonce_tcp_service_thread_func (gpointer user_data)
1140 PeerData *data = user_data;
1141 GMainContext *service_context;
1142 GDBusAuthObserver *observer;
1145 service_context = g_main_context_new ();
1146 g_main_context_push_thread_default (service_context);
1149 observer = g_dbus_auth_observer_new ();
1150 server = g_dbus_server_new_sync ("nonce-tcp:",
1151 G_DBUS_SERVER_FLAGS_NONE,
1154 NULL, /* cancellable */
1156 g_assert_no_error (error);
1158 g_signal_connect (server,
1160 G_CALLBACK (nonce_tcp_on_new_connection),
1162 g_signal_connect (observer,
1163 "authorize-authenticated-peer",
1164 G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1166 g_object_unref (observer);
1168 g_dbus_server_start (server);
1170 service_loop = g_main_loop_new (service_context, FALSE);
1171 g_main_loop_run (service_loop);
1173 g_main_context_pop_thread_default (service_context);
1175 g_main_loop_unref (service_loop);
1176 g_main_context_unref (service_context);
1178 /* test code specifically unrefs the server - see below */
1179 g_assert (server == NULL);
1185 test_nonce_tcp (void)
1189 GThread *service_thread;
1194 const gchar *address;
1196 memset (&data, '\0', sizeof (PeerData));
1197 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1201 service_loop = NULL;
1202 service_thread = g_thread_new ("nonce-tcp-service",
1203 nonce_tcp_service_thread_func,
1206 while (service_loop == NULL)
1208 g_assert (server != NULL);
1211 /* bring up a connection and accept it */
1212 data.accept_connection = TRUE;
1214 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1215 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1216 NULL, /* GDBusAuthObserver */
1217 NULL, /* cancellable */
1219 g_assert_no_error (error);
1220 g_assert (c != NULL);
1221 while (data.current_connections->len < 1)
1223 g_assert_cmpint (data.current_connections->len, ==, 1);
1224 g_assert_cmpint (data.num_connection_attempts, ==, 1);
1225 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1226 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1229 /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1232 address = g_dbus_server_get_client_address (server);
1234 s = strstr (address, "noncefile=");
1235 g_assert (s != NULL);
1236 s += sizeof "noncefile=" - 1;
1237 nonce_file = g_strdup (s);
1239 /* First try invalid data in the nonce file - this will actually
1240 * make the client send this and the server will reject it. The way
1241 * it works is that if the nonce doesn't match, the server will
1242 * simply close the connection. So, from the client point of view,
1243 * we can see a variety of errors.
1246 res = g_file_set_contents (nonce_file,
1250 g_assert_no_error (error);
1252 c = g_dbus_connection_new_for_address_sync (address,
1253 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1254 NULL, /* GDBusAuthObserver */
1255 NULL, /* cancellable */
1257 _g_assert_error_domain (error, G_IO_ERROR);
1258 g_error_free (error);
1259 g_assert (c == NULL);
1261 /* Then try with a nonce-file of incorrect length - this will make
1262 * the client complain - we won't even try connecting to the server
1266 res = g_file_set_contents (nonce_file,
1267 "0123456789012345_",
1270 g_assert_no_error (error);
1272 c = g_dbus_connection_new_for_address_sync (address,
1273 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1274 NULL, /* GDBusAuthObserver */
1275 NULL, /* cancellable */
1277 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1278 g_error_free (error);
1279 g_assert (c == NULL);
1281 /* Finally try with no nonce-file at all */
1282 g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1284 c = g_dbus_connection_new_for_address_sync (address,
1285 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1286 NULL, /* GDBusAuthObserver */
1287 NULL, /* cancellable */
1289 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1290 g_error_free (error);
1291 g_assert (c == NULL);
1293 g_free (nonce_file);
1295 g_dbus_server_stop (server);
1296 g_object_unref (server);
1299 g_main_loop_quit (service_loop);
1300 g_thread_join (service_thread);
1304 test_credentials (void)
1306 GCredentials *c1, *c2;
1310 c1 = g_credentials_new ();
1311 c2 = g_credentials_new ();
1314 if (g_credentials_set_unix_user (c2, getuid (), &error))
1315 g_assert_no_error (error);
1317 g_clear_error (&error);
1318 g_assert (g_credentials_is_same_user (c1, c2, &error));
1319 g_assert_no_error (error);
1321 desc = g_credentials_to_string (c1);
1322 g_assert (desc != NULL);
1325 g_object_unref (c1);
1326 g_object_unref (c2);
1329 /* ---------------------------------------------------------------------------------------------------- */
1333 /* Chosen to be big enough to overflow the socket buffer */
1334 #define OVERFLOW_NUM_SIGNALS 5000
1335 #define OVERFLOW_TIMEOUT_SEC 10
1337 static GDBusMessage *
1338 overflow_filter_func (GDBusConnection *connection,
1339 GDBusMessage *message,
1343 volatile gint *counter = user_data;
1349 overflow_on_500ms_later_func (gpointer user_data)
1351 g_main_loop_quit (loop);
1352 return FALSE; /* don't keep the idle */
1356 test_overflow (void)
1361 GSocketConnection *socket_connection;
1362 GDBusConnection *producer, *consumer;
1365 volatile gint n_messages_received;
1366 volatile gint n_messages_sent;
1368 g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
1371 socket = g_socket_new_from_fd (sv[0], &error);
1372 g_assert_no_error (error);
1373 socket_connection = g_socket_connection_factory_create_connection (socket);
1374 g_assert (socket_connection != NULL);
1375 g_object_unref (socket);
1376 producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1378 G_DBUS_CONNECTION_FLAGS_NONE,
1379 NULL, /* GDBusAuthObserver */
1380 NULL, /* GCancellable */
1382 g_dbus_connection_set_exit_on_close (producer, TRUE);
1383 g_assert_no_error (error);
1384 g_object_unref (socket_connection);
1385 n_messages_sent = 0;
1386 g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
1388 /* send enough data that we get an EAGAIN */
1389 for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
1392 g_dbus_connection_emit_signal (producer,
1393 NULL, /* destination */
1395 "org.foo.Interface",
1397 g_variant_new ("(s)", "a string"),
1399 g_assert_no_error (error);
1402 /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
1403 * kernel buffers) and verify that n_messages_sent <
1404 * OVERFLOW_NUM_SIGNALS
1406 * This is to verify that not all the submitted messages have been
1407 * sent to the underlying transport.
1409 g_timeout_add (500, overflow_on_500ms_later_func, NULL);
1410 g_main_loop_run (loop);
1411 g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
1413 /* now suck it all out as a client, and add it up */
1414 socket = g_socket_new_from_fd (sv[1], &error);
1415 g_assert_no_error (error);
1416 socket_connection = g_socket_connection_factory_create_connection (socket);
1417 g_assert (socket_connection != NULL);
1418 g_object_unref (socket);
1419 consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1421 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
1422 NULL, /* GDBusAuthObserver */
1423 NULL, /* GCancellable */
1425 g_assert_no_error (error);
1426 g_object_unref (socket_connection);
1427 n_messages_received = 0;
1428 g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
1429 g_dbus_connection_start_message_processing (consumer);
1431 timer = g_timer_new ();
1432 g_timer_start (timer);
1434 while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
1435 g_main_context_iteration (NULL, FALSE);
1437 g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
1438 g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
1440 g_timer_destroy (timer);
1441 g_object_unref (consumer);
1442 g_object_unref (producer);
1446 test_overflow (void)
1448 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
1452 /* ---------------------------------------------------------------------------------------------------- */
1455 tcp_anonymous_on_new_connection (GDBusServer *server,
1456 GDBusConnection *connection,
1459 gboolean *seen_connection = user_data;
1460 *seen_connection = TRUE;
1465 tcp_anonymous_service_thread_func (gpointer user_data)
1467 gboolean *seen_connection = user_data;
1468 GMainContext *service_context;
1471 service_context = g_main_context_new ();
1472 g_main_context_push_thread_default (service_context);
1475 server = g_dbus_server_new_sync ("tcp:",
1476 G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS,
1478 NULL, /* GDBusObserver* */
1479 NULL, /* GCancellable* */
1481 g_assert_no_error (error);
1483 g_signal_connect (server,
1485 G_CALLBACK (tcp_anonymous_on_new_connection),
1488 g_dbus_server_start (server);
1490 service_loop = g_main_loop_new (service_context, FALSE);
1491 g_main_loop_run (service_loop);
1493 g_main_context_pop_thread_default (service_context);
1495 g_main_loop_unref (service_loop);
1496 g_main_context_unref (service_context);
1502 test_tcp_anonymous (void)
1504 gboolean seen_connection;
1505 GThread *service_thread;
1506 GDBusConnection *connection;
1509 seen_connection = FALSE;
1510 service_loop = NULL;
1511 service_thread = g_thread_new ("tcp-anon-service",
1512 tcp_anonymous_service_thread_func,
1513 &seen_connection, /* user_data */
1515 while (service_loop == NULL)
1517 g_assert (server != NULL);
1520 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1521 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1522 NULL, /* GDBusAuthObserver* */
1523 NULL, /* GCancellable */
1525 g_assert_no_error (error);
1526 g_assert (connection != NULL);
1528 while (!seen_connection)
1531 g_object_unref (connection);
1533 g_main_loop_quit (service_loop);
1534 g_dbus_server_stop (server);
1535 g_object_unref (server);
1538 g_thread_join (service_thread);
1541 /* ---------------------------------------------------------------------------------------------------- */
1548 GDBusNodeInfo *introspection_data = NULL;
1549 gchar *tmpdir = NULL;
1552 g_test_init (&argc, &argv, NULL);
1554 g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
1556 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1557 g_assert (introspection_data != NULL);
1558 test_interface_introspection_data = introspection_data->interfaces[0];
1560 test_guid = g_dbus_generate_guid ();
1564 if (g_unix_socket_address_abstract_names_supported ())
1565 tmp_address = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
1568 tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
1569 tmp_address = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
1573 tmp_address = g_strdup ("nonce-tcp:");
1575 /* all the tests rely on a shared main loop */
1576 loop = g_main_loop_new (NULL, FALSE);
1578 g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1579 g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1580 g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1581 g_test_add_func ("/gdbus/tcp-anonymous", test_tcp_anonymous);
1582 g_test_add_func ("/gdbus/credentials", test_credentials);
1583 g_test_add_func ("/gdbus/overflow", test_overflow);
1587 g_main_loop_unref (loop);
1589 g_dbus_node_info_unref (introspection_data);
1591 g_free (tmp_address);