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 *test_guid = NULL;
57 static GMainLoop *service_loop = NULL;
58 static GDBusServer *server = NULL;
59 static GMainLoop *loop = NULL;
61 /* ---------------------------------------------------------------------------------------------------- */
62 /* Test that peer-to-peer connections work */
63 /* ---------------------------------------------------------------------------------------------------- */
68 gboolean accept_connection;
69 gint num_connection_attempts;
70 GPtrArray *current_connections;
71 guint num_method_calls;
72 gboolean signal_received;
75 static const gchar *test_interface_introspection_xml =
77 " <interface name='org.gtk.GDBus.PeerTestInterface'>"
78 " <method name='HelloPeer'>"
79 " <arg type='s' name='greeting' direction='in'/>"
80 " <arg type='s' name='response' direction='out'/>"
82 " <method name='EmitSignal'/>"
83 " <method name='EmitSignalWithNameSet'/>"
84 " <method name='OpenFile'>"
85 " <arg type='s' name='path' direction='in'/>"
87 " <signal name='PeerSignal'>"
88 " <arg type='s' name='a_string'/>"
90 " <property type='s' name='PeerProperty' access='read'/>"
93 static GDBusInterfaceInfo *test_interface_introspection_data = NULL;
96 test_interface_method_call (GDBusConnection *connection,
98 const gchar *object_path,
99 const gchar *interface_name,
100 const gchar *method_name,
101 GVariant *parameters,
102 GDBusMethodInvocation *invocation,
105 PeerData *data = user_data;
106 const GDBusMethodInfo *info;
108 data->num_method_calls++;
110 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
111 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
113 info = g_dbus_method_invocation_get_method_info (invocation);
114 g_assert_cmpstr (info->name, ==, method_name);
116 if (g_strcmp0 (method_name, "HelloPeer") == 0)
118 const gchar *greeting;
121 g_variant_get (parameters, "(&s)", &greeting);
123 response = g_strdup_printf ("You greeted me with '%s'.",
125 g_dbus_method_invocation_return_value (invocation,
126 g_variant_new ("(s)", response));
129 else if (g_strcmp0 (method_name, "EmitSignal") == 0)
134 g_dbus_connection_emit_signal (connection,
136 "/org/gtk/GDBus/PeerTestObject",
137 "org.gtk.GDBus.PeerTestInterface",
141 g_assert_no_error (error);
142 g_dbus_method_invocation_return_value (invocation, NULL);
144 else if (g_strcmp0 (method_name, "EmitSignalWithNameSet") == 0)
148 GDBusMessage *message;
150 message = g_dbus_message_new_signal ("/org/gtk/GDBus/PeerTestObject",
151 "org.gtk.GDBus.PeerTestInterface",
152 "PeerSignalWithNameSet");
153 g_dbus_message_set_sender (message, ":1.42");
156 ret = g_dbus_connection_send_message (connection, message, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error);
157 g_assert_no_error (error);
159 g_object_unref (message);
161 g_dbus_method_invocation_return_value (invocation, NULL);
163 else if (g_strcmp0 (method_name, "OpenFile") == 0)
170 GUnixFDList *fd_list;
172 g_variant_get (parameters, "(&s)", &path);
174 fd_list = g_unix_fd_list_new ();
178 fd = open (path, O_RDONLY);
179 g_unix_fd_list_append (fd_list, fd, &error);
180 g_assert_no_error (error);
183 reply = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
184 g_dbus_message_set_unix_fd_list (reply, fd_list);
185 g_object_unref (fd_list);
186 g_object_unref (invocation);
189 g_dbus_connection_send_message (connection,
191 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
192 NULL, /* out_serial */
194 g_assert_no_error (error);
195 g_object_unref (reply);
197 g_dbus_method_invocation_return_dbus_error (invocation,
198 "org.gtk.GDBus.NotOnUnix",
199 "Your OS does not support file descriptor passing");
204 g_assert_not_reached ();
209 test_interface_get_property (GDBusConnection *connection,
211 const gchar *object_path,
212 const gchar *interface_name,
213 const gchar *property_name,
217 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
218 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
219 g_assert_cmpstr (property_name, ==, "PeerProperty");
221 return g_variant_new_string ("ThePropertyValue");
225 static const GDBusInterfaceVTable test_interface_vtable =
227 test_interface_method_call,
228 test_interface_get_property,
229 NULL /* set_property */
233 on_proxy_signal_received (GDBusProxy *proxy,
236 GVariant *parameters,
239 PeerData *data = user_data;
241 data->signal_received = TRUE;
243 g_assert (sender_name == NULL);
244 g_assert_cmpstr (signal_name, ==, "PeerSignal");
245 g_main_loop_quit (loop);
249 on_proxy_signal_received_with_name_set (GDBusProxy *proxy,
252 GVariant *parameters,
255 PeerData *data = user_data;
257 data->signal_received = TRUE;
259 g_assert_cmpstr (sender_name, ==, ":1.42");
260 g_assert_cmpstr (signal_name, ==, "PeerSignalWithNameSet");
261 g_main_loop_quit (loop);
264 /* ---------------------------------------------------------------------------------------------------- */
267 on_authorize_authenticated_peer (GDBusAuthObserver *observer,
269 GCredentials *credentials,
272 PeerData *data = user_data;
275 data->num_connection_attempts++;
278 if (!data->accept_connection)
281 g_main_loop_quit (loop);
287 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
289 on_new_connection (GDBusServer *server,
290 GDBusConnection *connection,
293 PeerData *data = user_data;
297 //g_print ("Client connected.\n"
298 // "Negotiated capabilities: unix-fd-passing=%d\n",
299 // g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
301 g_ptr_array_add (data->current_connections, g_object_ref (connection));
303 /* export object on the newly established connection */
305 reg_id = g_dbus_connection_register_object (connection,
306 "/org/gtk/GDBus/PeerTestObject",
307 test_interface_introspection_data,
308 &test_interface_vtable,
310 NULL, /* GDestroyNotify for data */
312 g_assert_no_error (error);
313 g_assert (reg_id > 0);
315 g_main_loop_quit (loop);
321 service_thread_func (gpointer user_data)
323 PeerData *data = user_data;
324 GMainContext *service_context;
325 GDBusAuthObserver *observer;
328 service_context = g_main_context_new ();
329 g_main_context_push_thread_default (service_context);
332 observer = g_dbus_auth_observer_new ();
333 server = g_dbus_server_new_sync (is_unix ? "unix:tmpdir=/tmp/gdbus-test-" : "nonce-tcp:",
334 G_DBUS_SERVER_FLAGS_NONE,
337 NULL, /* cancellable */
339 g_assert_no_error (error);
341 g_signal_connect (server,
343 G_CALLBACK (on_new_connection),
345 g_signal_connect (observer,
346 "authorize-authenticated-peer",
347 G_CALLBACK (on_authorize_authenticated_peer),
349 g_object_unref (observer);
351 g_dbus_server_start (server);
353 service_loop = g_main_loop_new (service_context, FALSE);
354 g_main_loop_run (service_loop);
356 g_main_context_pop_thread_default (service_context);
358 g_main_loop_unref (service_loop);
359 g_main_context_unref (service_context);
361 /* test code specifically unrefs the server - see below */
362 g_assert (server == NULL);
369 on_incoming_connection (GSocketService *service,
370 GSocketConnection *socket_connection,
371 GObject *source_object,
374 PeerData *data = user_data;
376 if (data->accept_connection)
380 GDBusConnection *connection;
383 connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
385 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
386 NULL, /* cancellable */
388 g_assert_no_error (error);
390 g_ptr_array_add (data->current_connections, connection);
392 /* export object on the newly established connection */
394 reg_id = g_dbus_connection_register_object (connection,
395 "/org/gtk/GDBus/PeerTestObject",
396 &test_interface_introspection_data,
397 &test_interface_vtable,
399 NULL, /* GDestroyNotify for data */
401 g_assert_no_error (error);
402 g_assert (reg_id > 0);
407 /* don't do anything */
410 data->num_connection_attempts++;
412 g_main_loop_quit (loop);
414 /* stops other signal handlers from being invoked */
419 service_thread_func (gpointer data)
421 GMainContext *service_context;
423 GSocketAddress *address;
426 service_context = g_main_context_new ();
427 g_main_context_push_thread_default (service_context);
429 socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
430 address = g_unix_socket_address_new (socket_path);
432 service = g_socket_service_new ();
434 g_socket_listener_add_address (G_SOCKET_LISTENER (service),
436 G_SOCKET_TYPE_STREAM,
437 G_SOCKET_PROTOCOL_DEFAULT,
438 NULL, /* source_object */
439 NULL, /* effective_address */
441 g_assert_no_error (error);
442 g_signal_connect (service,
444 G_CALLBACK (on_incoming_connection),
446 g_socket_service_start (service);
448 service_loop = g_main_loop_new (service_context, FALSE);
449 g_main_loop_run (service_loop);
451 g_main_context_pop_thread_default (service_context);
453 g_main_loop_unref (service_loop);
454 g_main_context_unref (service_context);
456 g_object_unref (address);
457 g_free (socket_path);
462 /* ---------------------------------------------------------------------------------------------------- */
466 check_connection (gpointer user_data)
468 PeerData *data = user_data;
471 for (n = 0; n < data->current_connections->len; n++)
476 c = G_DBUS_CONNECTION (data->current_connections->pdata[n]);
477 stream = g_dbus_connection_get_stream (c);
479 g_debug ("In check_connection for %d: connection %p, stream %p", n, c, stream);
480 g_debug ("closed = %d", g_io_stream_is_closed (stream));
483 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
484 g_debug ("socket_closed = %d", g_socket_is_closed (socket));
485 g_debug ("socket_condition_check = %d", g_socket_condition_check (socket, G_IO_IN|G_IO_OUT|G_IO_ERR|G_IO_HUP));
491 num_read = g_input_stream_read (g_io_stream_get_input_stream (stream),
498 g_debug ("error: %s", error->message);
499 g_error_free (error);
503 g_debug ("no error, read %d bytes", (gint) num_read);
511 on_do_disconnect_in_idle (gpointer data)
513 GDBusConnection *c = G_DBUS_CONNECTION (data);
514 g_debug ("GDC %p has ref_count %d", c, G_OBJECT (c)->ref_count);
515 g_dbus_connection_disconnect (c);
523 read_all_from_fd (gint fd, gsize *out_len, GError **error)
529 str = g_string_new (NULL);
533 num_read = read (fd, buf, sizeof (buf));
536 if (errno == EAGAIN || errno == EWOULDBLOCK)
540 g_io_error_from_errno (errno),
541 "Failed reading %d bytes into offset %d: %s",
547 else if (num_read > 0)
549 g_string_append_len (str, buf, num_read);
551 else if (num_read == 0)
560 return g_string_free (str, FALSE);
565 g_string_free (str, TRUE);
581 GThread *service_thread;
582 gulong signal_handler_id;
584 memset (&data, '\0', sizeof (PeerData));
585 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
587 /* first try to connect when there is no server */
589 c = g_dbus_connection_new_for_address_sync (is_unix ? "unix:path=/tmp/gdbus-test-does-not-exist-pid" :
590 /* NOTE: Even if something is listening on port 12345 the connection
591 * will fail because the nonce file doesn't exist */
592 "nonce-tcp:host=localhost,port=12345,noncefile=this-does-not-exist-gdbus",
593 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
594 NULL, /* GDBusAuthObserver */
595 NULL, /* cancellable */
597 _g_assert_error_domain (error, G_IO_ERROR);
598 g_assert (!g_dbus_error_is_remote_error (error));
599 g_clear_error (&error);
600 g_assert (c == NULL);
602 /* bring up a server - we run the server in a different thread to avoid deadlocks */
604 service_thread = g_thread_create (service_thread_func,
608 while (service_loop == NULL)
610 g_assert (server != NULL);
612 /* bring up a connection and accept it */
613 data.accept_connection = TRUE;
615 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
616 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
617 NULL, /* GDBusAuthObserver */
618 NULL, /* cancellable */
620 g_assert_no_error (error);
621 g_assert (c != NULL);
622 while (data.current_connections->len < 1)
623 g_main_loop_run (loop);
624 g_assert_cmpint (data.current_connections->len, ==, 1);
625 g_assert_cmpint (data.num_connection_attempts, ==, 1);
626 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
627 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
629 /* check that we create a proxy, read properties, receive signals and invoke
630 * the HelloPeer() method. Since the server runs in another thread it's fine
631 * to use synchronous blocking API here.
634 proxy = g_dbus_proxy_new_sync (c,
635 G_DBUS_PROXY_FLAGS_NONE,
638 "/org/gtk/GDBus/PeerTestObject",
639 "org.gtk.GDBus.PeerTestInterface",
640 NULL, /* GCancellable */
642 g_assert_no_error (error);
643 g_assert (proxy != NULL);
645 value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
646 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
648 /* try invoking a method */
650 result = g_dbus_proxy_call_sync (proxy,
652 g_variant_new ("(s)", "Hey Peer!"),
653 G_DBUS_CALL_FLAGS_NONE,
655 NULL, /* GCancellable */
657 g_assert_no_error (error);
658 g_variant_get (result, "(&s)", &s);
659 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
660 g_variant_unref (result);
661 g_assert_cmpint (data.num_method_calls, ==, 1);
663 /* make the other peer emit a signal - catch it */
664 signal_handler_id = g_signal_connect (proxy,
666 G_CALLBACK (on_proxy_signal_received),
668 g_assert (!data.signal_received);
669 g_dbus_proxy_call (proxy,
671 NULL, /* no arguments */
672 G_DBUS_CALL_FLAGS_NONE,
674 NULL, /* GCancellable */
675 NULL, /* GAsyncReadyCallback - we don't care about the result */
676 NULL); /* user_data */
677 g_main_loop_run (loop);
678 g_assert (data.signal_received);
679 g_assert_cmpint (data.num_method_calls, ==, 2);
680 g_signal_handler_disconnect (proxy, signal_handler_id);
682 /* Also ensure that messages with the sender header-field set gets
683 * delivered to the proxy - note that this doesn't really make sense
684 * e.g. names are meaning-less in a peer-to-peer case... but we
685 * support it because it makes sense in certain bridging
686 * applications - see e.g. #623815.
688 signal_handler_id = g_signal_connect (proxy,
690 G_CALLBACK (on_proxy_signal_received_with_name_set),
692 data.signal_received = FALSE;
693 g_dbus_proxy_call (proxy,
694 "EmitSignalWithNameSet",
695 NULL, /* no arguments */
696 G_DBUS_CALL_FLAGS_NONE,
698 NULL, /* GCancellable */
699 NULL, /* GAsyncReadyCallback - we don't care about the result */
700 NULL); /* user_data */
701 g_main_loop_run (loop);
702 g_assert (data.signal_received);
703 g_assert_cmpint (data.num_method_calls, ==, 3);
704 g_signal_handler_disconnect (proxy, signal_handler_id);
706 /* check for UNIX fd passing */
709 GDBusMessage *method_call_message;
710 GDBusMessage *method_reply_message;
711 GUnixFDList *fd_list;
718 method_call_message = g_dbus_message_new_method_call (NULL, /* name */
719 "/org/gtk/GDBus/PeerTestObject",
720 "org.gtk.GDBus.PeerTestInterface",
722 g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", "/etc/hosts"));
724 method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
726 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
728 NULL, /* out_serial */
729 NULL, /* cancellable */
731 g_assert_no_error (error);
732 g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
733 fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
734 g_assert (fd_list != NULL);
735 g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
737 fd = g_unix_fd_list_get (fd_list, 0, &error);
738 g_assert_no_error (error);
739 g_object_unref (method_call_message);
740 g_object_unref (method_reply_message);
744 buf = read_all_from_fd (fd, &len, &error);
745 g_assert_no_error (error);
746 g_assert (buf != NULL);
750 g_file_get_contents ("/etc/hosts",
754 g_assert_no_error (error);
755 g_assert_cmpint (len, ==, len2);
756 g_assert (memcmp (buf, buf2, len) == 0);
762 result = g_dbus_proxy_call_sync (proxy,
764 g_variant_new ("(s)", "boo"),
765 G_DBUS_CALL_FLAGS_NONE,
767 NULL, /* GCancellable */
769 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
770 g_assert (result == NULL);
771 g_error_free (error);
772 #endif /* G_OS_UNIX */
774 /* Check that g_socket_get_credentials() work - this really should
775 * be in a GSocket-specific test suite but no such test suite exists
780 GCredentials *credentials;
781 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (g_dbus_connection_get_stream (c)));
782 g_assert (G_IS_SOCKET (socket));
784 credentials = g_socket_get_credentials (socket, &error);
787 struct ucred *native_creds;
788 g_assert_no_error (error);
789 g_assert (G_IS_CREDENTIALS (credentials));
790 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED);
791 g_assert (native_creds != NULL);
792 g_assert (native_creds->uid == getuid ());
793 g_assert (native_creds->gid == getgid ());
794 g_assert (native_creds->pid == getpid ());
796 g_object_unref (credentials);
798 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
799 g_assert (credentials == NULL);
804 /* bring up a connection - don't accept it - this should fail
806 data.accept_connection = FALSE;
808 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
809 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
810 NULL, /* GDBusAuthObserver */
811 NULL, /* cancellable */
813 _g_assert_error_domain (error, G_IO_ERROR);
814 g_error_free (error);
815 g_assert (c2 == NULL);
818 /* TODO: THIS TEST DOESN'T WORK YET */
820 /* bring up a connection - accept it.. then disconnect from the client side - check
821 * that the server side gets the disconnect signal.
824 data.accept_connection = TRUE;
825 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
826 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
827 NULL, /* GDBusAuthObserver */
828 NULL, /* cancellable */
830 g_assert_no_error (error);
831 g_assert (c2 != NULL);
832 g_assert (!g_dbus_connection_get_is_disconnected (c2));
833 while (data.num_connection_attempts < 3)
834 g_main_loop_run (loop);
835 g_assert_cmpint (data.current_connections->len, ==, 2);
836 g_assert_cmpint (data.num_connection_attempts, ==, 3);
837 g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
838 g_idle_add (on_do_disconnect_in_idle, c2);
839 g_debug ("==================================================");
840 g_debug ("==================================================");
841 g_debug ("==================================================");
842 g_debug ("waiting for disconnect on connection %p, stream %p",
843 data.current_connections->pdata[1],
844 g_dbus_connection_get_stream (data.current_connections->pdata[1]));
846 g_timeout_add (2000, check_connection, &data);
847 //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
848 g_main_loop_run (loop);
849 g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
850 g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
853 /* unref the server and stop listening for new connections
855 * This won't bring down the established connections - check that c is still connected
856 * by invoking a method
858 //g_socket_service_stop (service);
859 //g_object_unref (service);
860 g_dbus_server_stop (server);
861 g_object_unref (server);
865 result = g_dbus_proxy_call_sync (proxy,
867 g_variant_new ("(s)", "Hey Again Peer!"),
868 G_DBUS_CALL_FLAGS_NONE,
870 NULL, /* GCancellable */
872 g_assert_no_error (error);
873 g_variant_get (result, "(&s)", &s);
874 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
875 g_variant_unref (result);
876 g_assert_cmpint (data.num_method_calls, ==, 5);
879 /* TODO: THIS TEST DOESN'T WORK YET */
881 /* now disconnect from the server side - check that the client side gets the signal */
882 g_assert_cmpint (data.current_connections->len, ==, 1);
883 g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
884 g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
885 if (!g_dbus_connection_get_is_disconnected (c))
886 _g_assert_signal_received (c, "closed");
887 g_assert (g_dbus_connection_get_is_disconnected (c));
891 g_ptr_array_unref (data.current_connections);
892 g_object_unref (proxy);
894 g_main_loop_quit (service_loop);
895 g_thread_join (service_thread);
898 /* ---------------------------------------------------------------------------------------------------- */
903 GMainContext *context;
910 dmp_data_free (DmpData *data)
912 g_main_loop_unref (data->loop);
913 g_main_context_unref (data->context);
914 g_object_unref (data->server);
915 g_list_foreach (data->connections, (GFunc) g_object_unref, NULL);
916 g_list_free (data->connections);
921 dmp_on_method_call (GDBusConnection *connection,
923 const gchar *object_path,
924 const gchar *interface_name,
925 const gchar *method_name,
926 GVariant *parameters,
927 GDBusMethodInvocation *invocation,
930 //DmpData *data = user_data;
933 g_variant_get (parameters,
937 g_dbus_method_invocation_return_value (invocation,
938 g_variant_new ("(i)", first + second));
941 static const GDBusInterfaceVTable dmp_interface_vtable =
944 NULL, /* get_property */
945 NULL /* set_property */
949 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
951 dmp_on_new_connection (GDBusServer *server,
952 GDBusConnection *connection,
955 DmpData *data = user_data;
959 /* accept the connection */
960 data->connections = g_list_prepend (data->connections, g_object_ref (connection));
963 node = g_dbus_node_info_new_for_xml ("<node>"
964 " <interface name='org.gtk.GDBus.DmpInterface'>"
965 " <method name='AddPair'>"
966 " <arg type='i' name='first' direction='in'/>"
967 " <arg type='i' name='second' direction='in'/>"
968 " <arg type='i' name='sum' direction='out'/>"
973 g_assert_no_error (error);
975 /* sleep 100ms before exporting an object - this is to test that
976 * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
977 * (GDBusServer uses this feature).
981 /* export an object */
983 g_dbus_connection_register_object (connection,
986 &dmp_interface_vtable,
990 g_dbus_node_info_unref (node);
996 dmp_thread_func (gpointer user_data)
998 DmpData *data = user_data;
1002 data->context = g_main_context_new ();
1003 g_main_context_push_thread_default (data->context);
1006 guid = g_dbus_generate_guid ();
1007 data->server = g_dbus_server_new_sync ("unix:tmpdir=/tmp/gdbus-test-",
1008 G_DBUS_SERVER_FLAGS_NONE,
1010 NULL, /* GDBusAuthObserver */
1011 NULL, /* GCancellable */
1013 g_assert_no_error (error);
1014 g_signal_connect (data->server,
1016 G_CALLBACK (dmp_on_new_connection),
1019 g_dbus_server_start (data->server);
1021 data->loop = g_main_loop_new (data->context, FALSE);
1022 g_main_loop_run (data->loop);
1024 g_main_context_pop_thread_default (data->context);
1031 delayed_message_processing (void)
1035 GThread *service_thread;
1038 data = g_new0 (DmpData, 1);
1041 service_thread = g_thread_create (dmp_thread_func,
1045 while (data->server == NULL || !g_dbus_server_is_active (data->server))
1048 for (n = 0; n < 5; n++)
1055 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
1056 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1057 NULL, /* GDBusAuthObserver */
1058 NULL, /* GCancellable */
1060 g_assert_no_error (error);
1063 res = g_dbus_connection_call_sync (c,
1064 NULL, /* bus name */
1066 "org.gtk.GDBus.DmpInterface",
1068 g_variant_new ("(ii)", 2, n),
1069 G_VARIANT_TYPE ("(i)"),
1070 G_DBUS_CALL_FLAGS_NONE,
1071 -1, /* timeout_msec */
1072 NULL, /* GCancellable */
1074 g_assert_no_error (error);
1075 g_variant_get (res, "(i)", &val);
1076 g_assert_cmpint (val, ==, 2 + n);
1077 g_variant_unref (res);
1081 g_main_loop_quit (data->loop);
1082 g_thread_join (service_thread);
1083 dmp_data_free (data);
1086 /* ---------------------------------------------------------------------------------------------------- */
1089 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1091 GCredentials *credentials,
1094 PeerData *data = user_data;
1095 gboolean authorized;
1097 data->num_connection_attempts++;
1100 if (!data->accept_connection)
1103 g_main_loop_quit (loop);
1109 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1111 nonce_tcp_on_new_connection (GDBusServer *server,
1112 GDBusConnection *connection,
1115 PeerData *data = user_data;
1117 g_ptr_array_add (data->current_connections, g_object_ref (connection));
1119 g_main_loop_quit (loop);
1125 nonce_tcp_service_thread_func (gpointer user_data)
1127 PeerData *data = user_data;
1128 GMainContext *service_context;
1129 GDBusAuthObserver *observer;
1132 service_context = g_main_context_new ();
1133 g_main_context_push_thread_default (service_context);
1136 observer = g_dbus_auth_observer_new ();
1137 server = g_dbus_server_new_sync ("nonce-tcp:",
1138 G_DBUS_SERVER_FLAGS_NONE,
1141 NULL, /* cancellable */
1143 g_assert_no_error (error);
1145 g_signal_connect (server,
1147 G_CALLBACK (nonce_tcp_on_new_connection),
1149 g_signal_connect (observer,
1150 "authorize-authenticated-peer",
1151 G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1153 g_object_unref (observer);
1155 g_dbus_server_start (server);
1157 service_loop = g_main_loop_new (service_context, FALSE);
1158 g_main_loop_run (service_loop);
1160 g_main_context_pop_thread_default (service_context);
1162 g_main_loop_unref (service_loop);
1163 g_main_context_unref (service_context);
1165 /* test code specifically unrefs the server - see below */
1166 g_assert (server == NULL);
1172 test_nonce_tcp (void)
1176 GThread *service_thread;
1181 const gchar *address;
1183 memset (&data, '\0', sizeof (PeerData));
1184 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1188 service_loop = NULL;
1189 service_thread = g_thread_create (nonce_tcp_service_thread_func,
1193 while (service_loop == NULL)
1195 g_assert (server != NULL);
1198 /* bring up a connection and accept it */
1199 data.accept_connection = TRUE;
1201 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1202 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1203 NULL, /* GDBusAuthObserver */
1204 NULL, /* cancellable */
1206 g_assert_no_error (error);
1207 g_assert (c != NULL);
1208 while (data.current_connections->len < 1)
1209 g_main_loop_run (loop);
1210 g_assert_cmpint (data.current_connections->len, ==, 1);
1211 g_assert_cmpint (data.num_connection_attempts, ==, 1);
1212 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1213 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1216 /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1219 address = g_dbus_server_get_client_address (server);
1221 s = strstr (address, "noncefile=");
1222 g_assert (s != NULL);
1223 s += sizeof "noncefile=" - 1;
1224 nonce_file = g_strdup (s);
1226 /* First try invalid data in the nonce file - this will actually
1227 * make the client send this and the server will reject it. The way
1228 * it works is that if the nonce doesn't match, the server will
1229 * simply close the connection. So, from the client point of view,
1230 * we can see a variety of errors.
1233 res = g_file_set_contents (nonce_file,
1237 g_assert_no_error (error);
1239 c = g_dbus_connection_new_for_address_sync (address,
1240 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1241 NULL, /* GDBusAuthObserver */
1242 NULL, /* cancellable */
1244 _g_assert_error_domain (error, G_IO_ERROR);
1245 g_error_free (error);
1246 g_assert (c == NULL);
1248 /* Then try with a nonce-file of incorrect length - this will make
1249 * the client complain - we won't even try connecting to the server
1253 res = g_file_set_contents (nonce_file,
1254 "0123456789012345_",
1257 g_assert_no_error (error);
1259 c = g_dbus_connection_new_for_address_sync (address,
1260 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1261 NULL, /* GDBusAuthObserver */
1262 NULL, /* cancellable */
1264 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1265 g_error_free (error);
1266 g_assert (c == NULL);
1268 /* Finally try with no nonce-file at all */
1269 g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1271 c = g_dbus_connection_new_for_address_sync (address,
1272 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1273 NULL, /* GDBusAuthObserver */
1274 NULL, /* cancellable */
1276 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1277 g_error_free (error);
1278 g_assert (c == NULL);
1280 g_free (nonce_file);
1282 g_dbus_server_stop (server);
1283 g_object_unref (server);
1286 g_main_loop_quit (service_loop);
1287 g_thread_join (service_thread);
1291 test_credentials (void)
1293 GCredentials *c1, *c2;
1297 c1 = g_credentials_new ();
1298 c2 = g_credentials_new ();
1301 if (g_credentials_set_unix_user (c2, getuid (), &error))
1302 g_assert_no_error (error);
1304 g_clear_error (&error);
1305 g_assert (g_credentials_is_same_user (c1, c2, &error));
1306 g_assert_no_error (error);
1308 desc = g_credentials_to_string (c1);
1309 g_assert (desc != NULL);
1312 g_object_unref (c1);
1313 g_object_unref (c2);
1316 /* ---------------------------------------------------------------------------------------------------- */
1320 /* Chosen to be big enough to overflow the socket buffer */
1321 #define OVERFLOW_NUM_SIGNALS 5000
1322 #define OVERFLOW_TIMEOUT_SEC 10
1324 static GDBusMessage *
1325 overflow_filter_func (GDBusConnection *connection,
1326 GDBusMessage *message,
1330 volatile gint *counter = user_data;
1336 overflow_on_500ms_later_func (gpointer user_data)
1338 g_main_loop_quit (loop);
1339 return FALSE; /* don't keep the idle */
1343 test_overflow (void)
1348 GSocketConnection *socket_connection;
1349 GDBusConnection *producer, *consumer;
1352 volatile gint n_messages_received;
1353 volatile gint n_messages_sent;
1355 g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
1358 socket = g_socket_new_from_fd (sv[0], &error);
1359 g_assert_no_error (error);
1360 socket_connection = g_socket_connection_factory_create_connection (socket);
1361 g_assert (socket_connection != NULL);
1362 g_object_unref (socket);
1363 producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1365 G_DBUS_CONNECTION_FLAGS_NONE,
1366 NULL, /* GDBusAuthObserver */
1367 NULL, /* GCancellable */
1369 g_dbus_connection_set_exit_on_close (producer, TRUE);
1370 g_assert_no_error (error);
1371 g_object_unref (socket_connection);
1372 n_messages_sent = 0;
1373 g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
1375 /* send enough data that we get an EAGAIN */
1376 for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
1379 g_dbus_connection_emit_signal (producer,
1380 NULL, /* destination */
1382 "org.foo.Interface",
1384 g_variant_new ("(s)", "a string"),
1386 g_assert_no_error (error);
1389 /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
1390 * kernel buffers) and verify that n_messages_sent <
1391 * OVERFLOW_NUM_SIGNALS
1393 * This is to verify that not all the submitted messages have been
1394 * sent to the underlying transport.
1396 g_timeout_add (500, overflow_on_500ms_later_func, NULL);
1397 g_main_loop_run (loop);
1398 g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
1400 /* now suck it all out as a client, and add it up */
1401 socket = g_socket_new_from_fd (sv[1], &error);
1402 g_assert_no_error (error);
1403 socket_connection = g_socket_connection_factory_create_connection (socket);
1404 g_assert (socket_connection != NULL);
1405 g_object_unref (socket);
1406 consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1408 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
1409 NULL, /* GDBusAuthObserver */
1410 NULL, /* GCancellable */
1412 g_assert_no_error (error);
1413 g_object_unref (socket_connection);
1414 n_messages_received = 0;
1415 g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
1416 g_dbus_connection_start_message_processing (consumer);
1418 timer = g_timer_new ();
1419 g_timer_start (timer);
1421 while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
1422 g_main_context_iteration (NULL, FALSE);
1424 g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
1425 g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
1427 g_timer_destroy (timer);
1428 g_object_unref (consumer);
1429 g_object_unref (producer);
1433 test_overflow (void)
1435 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
1439 /* ---------------------------------------------------------------------------------------------------- */
1442 tcp_anonymous_on_new_connection (GDBusServer *server,
1443 GDBusConnection *connection,
1446 gboolean *seen_connection = user_data;
1447 *seen_connection = TRUE;
1452 tcp_anonymous_service_thread_func (gpointer user_data)
1454 gboolean *seen_connection = user_data;
1455 GMainContext *service_context;
1458 service_context = g_main_context_new ();
1459 g_main_context_push_thread_default (service_context);
1462 server = g_dbus_server_new_sync ("tcp:",
1463 G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS,
1465 NULL, /* GDBusObserver* */
1466 NULL, /* GCancellable* */
1468 g_assert_no_error (error);
1470 g_signal_connect (server,
1472 G_CALLBACK (tcp_anonymous_on_new_connection),
1475 g_dbus_server_start (server);
1477 service_loop = g_main_loop_new (service_context, FALSE);
1478 g_main_loop_run (service_loop);
1480 g_main_context_pop_thread_default (service_context);
1482 g_main_loop_unref (service_loop);
1483 g_main_context_unref (service_context);
1489 test_tcp_anonymous (void)
1491 gboolean seen_connection;
1492 GThread *service_thread;
1493 GDBusConnection *connection;
1496 seen_connection = FALSE;
1497 service_loop = NULL;
1498 service_thread = g_thread_create (tcp_anonymous_service_thread_func,
1499 &seen_connection, /* user_data */
1500 TRUE, /* joinable */
1502 while (service_loop == NULL)
1504 g_assert (server != NULL);
1507 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1508 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1509 NULL, /* GDBusAuthObserver* */
1510 NULL, /* GCancellable */
1512 g_assert_no_error (error);
1513 g_assert (connection != NULL);
1515 while (!seen_connection)
1518 g_object_unref (connection);
1520 g_main_loop_quit (service_loop);
1521 g_dbus_server_stop (server);
1522 g_object_unref (server);
1525 g_thread_join (service_thread);
1528 /* ---------------------------------------------------------------------------------------------------- */
1535 GDBusNodeInfo *introspection_data = NULL;
1538 g_thread_init (NULL);
1539 g_test_init (&argc, &argv, NULL);
1541 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1542 g_assert (introspection_data != NULL);
1543 test_interface_introspection_data = introspection_data->interfaces[0];
1545 test_guid = g_dbus_generate_guid ();
1547 /* all the tests rely on a shared main loop */
1548 loop = g_main_loop_new (NULL, FALSE);
1550 g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1551 g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1552 g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1553 g_test_add_func ("/gdbus/tcp-anonymous", test_tcp_anonymous);
1554 g_test_add_func ("/gdbus/credentials", test_credentials);
1555 g_test_add_func ("/gdbus/overflow", test_overflow);
1559 g_main_loop_unref (loop);
1561 g_dbus_node_info_unref (introspection_data);