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>
28 #include <sys/types.h>
33 #include <glib/gstdio.h>
35 #include <gio/gunixsocketaddress.h>
36 #include <gio/gunixfdlist.h>
38 /* for struct ucred */
41 #include <sys/types.h>
42 #include <sys/socket.h>
45 /* for SOCK_STREAM and AF_UNIX */
46 #include <sys/socket.h>
48 /* used in test_overflow */
50 #include <gio/gunixconnection.h>
53 #include "gdbus-tests.h"
56 static gboolean is_unix = TRUE;
58 static gboolean is_unix = FALSE;
61 static gchar *test_guid = NULL;
62 static GMainLoop *service_loop = NULL;
63 static GDBusServer *server = NULL;
64 static GMainLoop *loop = NULL;
66 /* ---------------------------------------------------------------------------------------------------- */
67 /* Test that peer-to-peer connections work */
68 /* ---------------------------------------------------------------------------------------------------- */
73 gboolean accept_connection;
74 gint num_connection_attempts;
75 GPtrArray *current_connections;
76 guint num_method_calls;
77 gboolean signal_received;
80 static const gchar *test_interface_introspection_xml =
82 " <interface name='org.gtk.GDBus.PeerTestInterface'>"
83 " <method name='HelloPeer'>"
84 " <arg type='s' name='greeting' direction='in'/>"
85 " <arg type='s' name='response' direction='out'/>"
87 " <method name='EmitSignal'/>"
88 " <method name='EmitSignalWithNameSet'/>"
89 " <method name='OpenFile'>"
90 " <arg type='s' name='path' direction='in'/>"
92 " <signal name='PeerSignal'>"
93 " <arg type='s' name='a_string'/>"
95 " <property type='s' name='PeerProperty' access='read'/>"
98 static GDBusInterfaceInfo *test_interface_introspection_data = NULL;
101 test_interface_method_call (GDBusConnection *connection,
103 const gchar *object_path,
104 const gchar *interface_name,
105 const gchar *method_name,
106 GVariant *parameters,
107 GDBusMethodInvocation *invocation,
110 PeerData *data = user_data;
112 data->num_method_calls++;
114 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
115 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
117 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 (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);
319 service_thread_func (gpointer user_data)
321 PeerData *data = user_data;
322 GMainContext *service_context;
323 GDBusAuthObserver *observer;
326 service_context = g_main_context_new ();
327 g_main_context_push_thread_default (service_context);
330 observer = g_dbus_auth_observer_new ();
331 server = g_dbus_server_new_sync (is_unix ? "unix:tmpdir=/tmp/gdbus-test-" : "nonce-tcp:",
332 G_DBUS_SERVER_FLAGS_NONE,
335 NULL, /* cancellable */
337 g_assert_no_error (error);
339 g_signal_connect (server,
341 G_CALLBACK (on_new_connection),
343 g_signal_connect (observer,
344 "authorize-authenticated-peer",
345 G_CALLBACK (on_authorize_authenticated_peer),
347 g_object_unref (observer);
349 g_dbus_server_start (server);
351 service_loop = g_main_loop_new (service_context, FALSE);
352 g_main_loop_run (service_loop);
354 g_main_context_pop_thread_default (service_context);
356 g_main_loop_unref (service_loop);
357 g_main_context_unref (service_context);
359 /* test code specifically unrefs the server - see below */
360 g_assert (server == NULL);
367 on_incoming_connection (GSocketService *service,
368 GSocketConnection *socket_connection,
369 GObject *source_object,
372 PeerData *data = user_data;
374 if (data->accept_connection)
378 GDBusConnection *connection;
381 connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
383 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
384 NULL, /* cancellable */
386 g_assert_no_error (error);
388 g_ptr_array_add (data->current_connections, connection);
390 /* export object on the newly established connection */
392 reg_id = g_dbus_connection_register_object (connection,
393 "/org/gtk/GDBus/PeerTestObject",
394 &test_interface_introspection_data,
395 &test_interface_vtable,
397 NULL, /* GDestroyNotify for data */
399 g_assert_no_error (error);
400 g_assert (reg_id > 0);
405 /* don't do anything */
408 data->num_connection_attempts++;
410 g_main_loop_quit (loop);
412 /* stops other signal handlers from being invoked */
417 service_thread_func (gpointer data)
419 GMainContext *service_context;
421 GSocketAddress *address;
424 service_context = g_main_context_new ();
425 g_main_context_push_thread_default (service_context);
427 socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
428 address = g_unix_socket_address_new (socket_path);
430 service = g_socket_service_new ();
432 g_socket_listener_add_address (G_SOCKET_LISTENER (service),
434 G_SOCKET_TYPE_STREAM,
435 G_SOCKET_PROTOCOL_DEFAULT,
436 NULL, /* source_object */
437 NULL, /* effective_address */
439 g_assert_no_error (error);
440 g_signal_connect (service,
442 G_CALLBACK (on_incoming_connection),
444 g_socket_service_start (service);
446 service_loop = g_main_loop_new (service_context, FALSE);
447 g_main_loop_run (service_loop);
449 g_main_context_pop_thread_default (service_context);
451 g_main_loop_unref (service_loop);
452 g_main_context_unref (service_context);
454 g_object_unref (address);
455 g_free (socket_path);
460 /* ---------------------------------------------------------------------------------------------------- */
464 check_connection (gpointer user_data)
466 PeerData *data = user_data;
469 for (n = 0; n < data->current_connections->len; n++)
474 c = G_DBUS_CONNECTION (data->current_connections->pdata[n]);
475 stream = g_dbus_connection_get_stream (c);
477 g_debug ("In check_connection for %d: connection %p, stream %p", n, c, stream);
478 g_debug ("closed = %d", g_io_stream_is_closed (stream));
481 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
482 g_debug ("socket_closed = %d", g_socket_is_closed (socket));
483 g_debug ("socket_condition_check = %d", g_socket_condition_check (socket, G_IO_IN|G_IO_OUT|G_IO_ERR|G_IO_HUP));
489 num_read = g_input_stream_read (g_io_stream_get_input_stream (stream),
496 g_debug ("error: %s", error->message);
497 g_error_free (error);
501 g_debug ("no error, read %d bytes", (gint) num_read);
509 on_do_disconnect_in_idle (gpointer data)
511 GDBusConnection *c = G_DBUS_CONNECTION (data);
512 g_debug ("GDC %p has ref_count %d", c, G_OBJECT (c)->ref_count);
513 g_dbus_connection_disconnect (c);
530 GThread *service_thread;
531 gulong signal_handler_id;
533 memset (&data, '\0', sizeof (PeerData));
534 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
536 /* first try to connect when there is no server */
538 c = g_dbus_connection_new_for_address_sync (is_unix ? "unix:path=/tmp/gdbus-test-does-not-exist-pid" :
539 /* NOTE: Even if something is listening on port 12345 the connection
540 * will fail because the nonce file doesn't exist */
541 "nonce-tcp:host=localhost,port=12345,noncefile=this-does-not-exist-gdbus",
542 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
543 NULL, /* GDBusAuthObserver */
544 NULL, /* cancellable */
546 _g_assert_error_domain (error, G_IO_ERROR);
547 g_assert (!g_dbus_error_is_remote_error (error));
548 g_clear_error (&error);
549 g_assert (c == NULL);
551 /* bring up a server - we run the server in a different thread to avoid deadlocks */
553 service_thread = g_thread_create (service_thread_func,
557 while (service_loop == NULL)
559 g_assert (server != NULL);
561 /* bring up a connection and accept it */
562 data.accept_connection = TRUE;
564 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
565 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
566 NULL, /* GDBusAuthObserver */
567 NULL, /* cancellable */
569 g_assert_no_error (error);
570 g_assert (c != NULL);
571 while (data.current_connections->len < 1)
572 g_main_loop_run (loop);
573 g_assert_cmpint (data.current_connections->len, ==, 1);
574 g_assert_cmpint (data.num_connection_attempts, ==, 1);
575 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
576 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
578 /* check that we create a proxy, read properties, receive signals and invoke
579 * the HelloPeer() method. Since the server runs in another thread it's fine
580 * to use synchronous blocking API here.
583 proxy = g_dbus_proxy_new_sync (c,
584 G_DBUS_PROXY_FLAGS_NONE,
587 "/org/gtk/GDBus/PeerTestObject",
588 "org.gtk.GDBus.PeerTestInterface",
589 NULL, /* GCancellable */
591 g_assert_no_error (error);
592 g_assert (proxy != NULL);
594 value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
595 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
597 /* try invoking a method */
599 result = g_dbus_proxy_call_sync (proxy,
601 g_variant_new ("(s)", "Hey Peer!"),
602 G_DBUS_CALL_FLAGS_NONE,
604 NULL, /* GCancellable */
606 g_assert_no_error (error);
607 g_variant_get (result, "(&s)", &s);
608 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
609 g_variant_unref (result);
610 g_assert_cmpint (data.num_method_calls, ==, 1);
612 /* make the other peer emit a signal - catch it */
613 signal_handler_id = g_signal_connect (proxy,
615 G_CALLBACK (on_proxy_signal_received),
617 g_assert (!data.signal_received);
618 g_dbus_proxy_call (proxy,
620 NULL, /* no arguments */
621 G_DBUS_CALL_FLAGS_NONE,
623 NULL, /* GCancellable */
624 NULL, /* GAsyncReadyCallback - we don't care about the result */
625 NULL); /* user_data */
626 g_main_loop_run (loop);
627 g_assert (data.signal_received);
628 g_assert_cmpint (data.num_method_calls, ==, 2);
629 g_signal_handler_disconnect (proxy, signal_handler_id);
631 /* Also ensure that messages with the sender header-field set gets
632 * delivered to the proxy - note that this doesn't really make sense
633 * e.g. names are meaning-less in a peer-to-peer case... but we
634 * support it because it makes sense in certain bridging
635 * applications - see e.g. #623815.
637 signal_handler_id = g_signal_connect (proxy,
639 G_CALLBACK (on_proxy_signal_received_with_name_set),
641 data.signal_received = FALSE;
642 g_dbus_proxy_call (proxy,
643 "EmitSignalWithNameSet",
644 NULL, /* no arguments */
645 G_DBUS_CALL_FLAGS_NONE,
647 NULL, /* GCancellable */
648 NULL, /* GAsyncReadyCallback - we don't care about the result */
649 NULL); /* user_data */
650 g_main_loop_run (loop);
651 g_assert (data.signal_received);
652 g_assert_cmpint (data.num_method_calls, ==, 3);
653 g_signal_handler_disconnect (proxy, signal_handler_id);
655 /* check for UNIX fd passing */
658 GDBusMessage *method_call_message;
659 GDBusMessage *method_reply_message;
660 GUnixFDList *fd_list;
667 method_call_message = g_dbus_message_new_method_call (NULL, /* name */
668 "/org/gtk/GDBus/PeerTestObject",
669 "org.gtk.GDBus.PeerTestInterface",
671 g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", "/etc/hosts"));
673 method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
675 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
677 NULL, /* out_serial */
678 NULL, /* cancellable */
680 g_assert_no_error (error);
681 g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
682 fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
683 g_assert (fd_list != NULL);
684 g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
686 fd = g_unix_fd_list_get (fd_list, 0, &error);
687 g_assert_no_error (error);
688 g_object_unref (method_call_message);
689 g_object_unref (method_reply_message);
691 memset (buf, '\0', sizeof (buf));
692 len = read (fd, buf, sizeof (buf) - 1);
696 g_file_get_contents ("/etc/hosts",
700 g_assert_no_error (error);
701 if (len2 > sizeof (buf))
702 buf2[sizeof (buf)] = '\0';
703 g_assert_cmpstr (buf, ==, buf2);
708 result = g_dbus_proxy_call_sync (proxy,
710 g_variant_new ("(s)", "boo"),
711 G_DBUS_CALL_FLAGS_NONE,
713 NULL, /* GCancellable */
715 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
716 g_assert (result == NULL);
717 g_error_free (error);
718 #endif /* G_OS_UNIX */
720 /* Check that g_socket_get_credentials() work - this really should
721 * be in a GSocket-specific test suite but no such test suite exists
726 GCredentials *credentials;
727 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (g_dbus_connection_get_stream (c)));
728 g_assert (G_IS_SOCKET (socket));
730 credentials = g_socket_get_credentials (socket, &error);
733 struct ucred *native_creds;
734 g_assert_no_error (error);
735 g_assert (G_IS_CREDENTIALS (credentials));
736 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED);
737 g_assert (native_creds != NULL);
738 g_assert (native_creds->uid == getuid ());
739 g_assert (native_creds->gid == getgid ());
740 g_assert (native_creds->pid == getpid ());
742 g_object_unref (credentials);
744 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
745 g_assert (credentials == NULL);
750 /* bring up a connection - don't accept it - this should fail
752 data.accept_connection = FALSE;
754 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
755 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
756 NULL, /* GDBusAuthObserver */
757 NULL, /* cancellable */
759 _g_assert_error_domain (error, G_IO_ERROR);
760 g_assert (c2 == NULL);
763 /* TODO: THIS TEST DOESN'T WORK YET */
765 /* bring up a connection - accept it.. then disconnect from the client side - check
766 * that the server side gets the disconnect signal.
769 data.accept_connection = TRUE;
770 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
771 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
772 NULL, /* GDBusAuthObserver */
773 NULL, /* cancellable */
775 g_assert_no_error (error);
776 g_assert (c2 != NULL);
777 g_assert (!g_dbus_connection_get_is_disconnected (c2));
778 while (data.num_connection_attempts < 3)
779 g_main_loop_run (loop);
780 g_assert_cmpint (data.current_connections->len, ==, 2);
781 g_assert_cmpint (data.num_connection_attempts, ==, 3);
782 g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
783 g_idle_add (on_do_disconnect_in_idle, c2);
784 g_debug ("==================================================");
785 g_debug ("==================================================");
786 g_debug ("==================================================");
787 g_debug ("waiting for disconnect on connection %p, stream %p",
788 data.current_connections->pdata[1],
789 g_dbus_connection_get_stream (data.current_connections->pdata[1]));
791 g_timeout_add (2000, check_connection, &data);
792 //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
793 g_main_loop_run (loop);
794 g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
795 g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
798 /* unref the server and stop listening for new connections
800 * This won't bring down the established connections - check that c is still connected
801 * by invoking a method
803 //g_socket_service_stop (service);
804 //g_object_unref (service);
805 g_dbus_server_stop (server);
806 g_object_unref (server);
810 result = g_dbus_proxy_call_sync (proxy,
812 g_variant_new ("(s)", "Hey Again Peer!"),
813 G_DBUS_CALL_FLAGS_NONE,
815 NULL, /* GCancellable */
817 g_assert_no_error (error);
818 g_variant_get (result, "(&s)", &s);
819 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
820 g_variant_unref (result);
821 g_assert_cmpint (data.num_method_calls, ==, 5);
824 /* TODO: THIS TEST DOESN'T WORK YET */
826 /* now disconnect from the server side - check that the client side gets the signal */
827 g_assert_cmpint (data.current_connections->len, ==, 1);
828 g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
829 g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
830 if (!g_dbus_connection_get_is_disconnected (c))
831 _g_assert_signal_received (c, "closed");
832 g_assert (g_dbus_connection_get_is_disconnected (c));
836 g_ptr_array_unref (data.current_connections);
837 g_object_unref (proxy);
839 g_main_loop_quit (service_loop);
840 g_thread_join (service_thread);
843 /* ---------------------------------------------------------------------------------------------------- */
848 GMainContext *context;
855 dmp_data_free (DmpData *data)
857 g_main_loop_unref (data->loop);
858 g_main_context_unref (data->context);
859 g_object_unref (data->server);
860 g_list_foreach (data->connections, (GFunc) g_object_unref, NULL);
861 g_list_free (data->connections);
866 dmp_on_method_call (GDBusConnection *connection,
868 const gchar *object_path,
869 const gchar *interface_name,
870 const gchar *method_name,
871 GVariant *parameters,
872 GDBusMethodInvocation *invocation,
875 //DmpData *data = user_data;
878 g_variant_get (parameters,
882 g_dbus_method_invocation_return_value (invocation,
883 g_variant_new ("(i)", first + second));
886 static const GDBusInterfaceVTable dmp_interface_vtable =
889 NULL, /* get_property */
890 NULL /* set_property */
894 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
896 dmp_on_new_connection (GDBusServer *server,
897 GDBusConnection *connection,
900 DmpData *data = user_data;
904 /* accept the connection */
905 data->connections = g_list_prepend (data->connections, g_object_ref (connection));
908 node = g_dbus_node_info_new_for_xml ("<node>"
909 " <interface name='org.gtk.GDBus.DmpInterface'>"
910 " <method name='AddPair'>"
911 " <arg type='i' name='first' direction='in'/>"
912 " <arg type='i' name='second' direction='in'/>"
913 " <arg type='i' name='sum' direction='out'/>"
918 g_assert_no_error (error);
920 /* sleep 100ms before exporting an object - this is to test that
921 * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
922 * (GDBusServer uses this feature).
926 /* export an object */
928 g_dbus_connection_register_object (connection,
931 &dmp_interface_vtable,
935 g_dbus_node_info_unref (node);
939 dmp_thread_func (gpointer user_data)
941 DmpData *data = user_data;
945 data->context = g_main_context_new ();
946 g_main_context_push_thread_default (data->context);
949 guid = g_dbus_generate_guid ();
950 data->server = g_dbus_server_new_sync ("unix:tmpdir=/tmp/gdbus-test-",
951 G_DBUS_SERVER_FLAGS_NONE,
953 NULL, /* GDBusAuthObserver */
954 NULL, /* GCancellable */
956 g_assert_no_error (error);
957 g_signal_connect (data->server,
959 G_CALLBACK (dmp_on_new_connection),
962 g_dbus_server_start (data->server);
964 data->loop = g_main_loop_new (data->context, FALSE);
965 g_main_loop_run (data->loop);
967 g_main_context_pop_thread_default (data->context);
974 delayed_message_processing (void)
978 GThread *service_thread;
981 data = g_new0 (DmpData, 1);
984 service_thread = g_thread_create (dmp_thread_func,
988 while (data->server == NULL || !g_dbus_server_is_active (data->server))
991 for (n = 0; n < 5; n++)
998 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
999 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1000 NULL, /* GDBusAuthObserver */
1001 NULL, /* GCancellable */
1003 g_assert_no_error (error);
1006 res = g_dbus_connection_call_sync (c,
1007 NULL, /* bus name */
1009 "org.gtk.GDBus.DmpInterface",
1011 g_variant_new ("(ii)", 2, n),
1012 G_VARIANT_TYPE ("(i)"),
1013 G_DBUS_CALL_FLAGS_NONE,
1014 -1, /* timeout_msec */
1015 NULL, /* GCancellable */
1017 g_assert_no_error (error);
1018 g_variant_get (res, "(i)", &val);
1019 g_assert_cmpint (val, ==, 2 + n);
1020 g_variant_unref (res);
1024 g_main_loop_quit (data->loop);
1025 g_thread_join (service_thread);
1026 dmp_data_free (data);
1029 /* ---------------------------------------------------------------------------------------------------- */
1032 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1034 GCredentials *credentials,
1037 PeerData *data = user_data;
1038 gboolean authorized;
1040 data->num_connection_attempts++;
1043 if (!data->accept_connection)
1046 g_main_loop_quit (loop);
1052 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1054 nonce_tcp_on_new_connection (GDBusServer *server,
1055 GDBusConnection *connection,
1058 PeerData *data = user_data;
1060 g_ptr_array_add (data->current_connections, g_object_ref (connection));
1062 g_main_loop_quit (loop);
1066 nonce_tcp_service_thread_func (gpointer user_data)
1068 PeerData *data = user_data;
1069 GMainContext *service_context;
1070 GDBusAuthObserver *observer;
1073 service_context = g_main_context_new ();
1074 g_main_context_push_thread_default (service_context);
1077 observer = g_dbus_auth_observer_new ();
1078 server = g_dbus_server_new_sync ("nonce-tcp:",
1079 G_DBUS_SERVER_FLAGS_NONE,
1082 NULL, /* cancellable */
1084 g_assert_no_error (error);
1086 g_signal_connect (server,
1088 G_CALLBACK (nonce_tcp_on_new_connection),
1090 g_signal_connect (observer,
1091 "authorize-authenticated-peer",
1092 G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1094 g_object_unref (observer);
1096 g_dbus_server_start (server);
1098 service_loop = g_main_loop_new (service_context, FALSE);
1099 g_main_loop_run (service_loop);
1101 g_main_context_pop_thread_default (service_context);
1103 g_main_loop_unref (service_loop);
1104 g_main_context_unref (service_context);
1106 /* test code specifically unrefs the server - see below */
1107 g_assert (server == NULL);
1113 test_nonce_tcp (void)
1117 GThread *service_thread;
1122 const gchar *address;
1124 memset (&data, '\0', sizeof (PeerData));
1125 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1129 service_loop = NULL;
1130 service_thread = g_thread_create (nonce_tcp_service_thread_func,
1134 while (service_loop == NULL)
1136 g_assert (server != NULL);
1139 /* bring up a connection and accept it */
1140 data.accept_connection = TRUE;
1142 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1143 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1144 NULL, /* GDBusAuthObserver */
1145 NULL, /* cancellable */
1147 g_assert_no_error (error);
1148 g_assert (c != NULL);
1149 while (data.current_connections->len < 1)
1150 g_main_loop_run (loop);
1151 g_assert_cmpint (data.current_connections->len, ==, 1);
1152 g_assert_cmpint (data.num_connection_attempts, ==, 1);
1153 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1154 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1157 /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1160 address = g_dbus_server_get_client_address (server);
1162 s = strstr (address, "noncefile=");
1163 g_assert (s != NULL);
1164 s += sizeof "noncefile=" - 1;
1165 nonce_file = g_strdup (s);
1167 /* First try invalid data in the nonce file - this will actually
1168 * make the client send this and the server will reject it. The way
1169 * it works is that if the nonce doesn't match, the server will
1170 * simply close the connection. So, from the client point of view,
1171 * we can see a variety of errors.
1174 res = g_file_set_contents (nonce_file,
1178 g_assert_no_error (error);
1180 c = g_dbus_connection_new_for_address_sync (address,
1181 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1182 NULL, /* GDBusAuthObserver */
1183 NULL, /* cancellable */
1185 _g_assert_error_domain (error, G_IO_ERROR);
1186 g_assert (c == NULL);
1188 /* Then try with a nonce-file of incorrect length - this will make
1189 * the client complain - we won't even try connecting to the server
1193 res = g_file_set_contents (nonce_file,
1194 "0123456789012345_",
1197 g_assert_no_error (error);
1199 c = g_dbus_connection_new_for_address_sync (address,
1200 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1201 NULL, /* GDBusAuthObserver */
1202 NULL, /* cancellable */
1204 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1205 g_assert (c == NULL);
1207 /* Finally try with no nonce-file at all */
1208 g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1210 c = g_dbus_connection_new_for_address_sync (address,
1211 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1212 NULL, /* GDBusAuthObserver */
1213 NULL, /* cancellable */
1215 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1216 g_assert (c == NULL);
1218 g_free (nonce_file);
1220 g_dbus_server_stop (server);
1221 g_object_unref (server);
1224 g_main_loop_quit (service_loop);
1225 g_thread_join (service_thread);
1229 test_credentials (void)
1231 GCredentials *c1, *c2;
1235 c1 = g_credentials_new ();
1236 c2 = g_credentials_new ();
1239 if (g_credentials_set_unix_user (c2, getuid (), &error))
1240 g_assert_no_error (error);
1242 g_clear_error (&error);
1243 g_assert (g_credentials_is_same_user (c1, c2, &error));
1244 g_assert_no_error (error);
1246 desc = g_credentials_to_string (c1);
1247 g_assert (desc != NULL);
1250 g_object_unref (c1);
1251 g_object_unref (c2);
1254 /* ---------------------------------------------------------------------------------------------------- */
1258 /* Chosen to be big enough to overflow the socket buffer */
1259 #define OVERFLOW_NUM_SIGNALS 5000
1260 #define OVERFLOW_TIMEOUT_SEC 10
1263 overflow_filter_func (GDBusConnection *connection,
1264 GDBusMessage *message,
1268 volatile gint *counter = user_data;
1270 return FALSE; /* don't drop the message */
1274 overflow_on_500ms_later_func (gpointer user_data)
1276 g_main_loop_quit (loop);
1277 return FALSE; /* don't keep the idle */
1281 test_overflow (void)
1286 GSocketConnection *socket_connection;
1287 GDBusConnection *producer, *consumer;
1290 volatile gint n_messages_received;
1291 volatile gint n_messages_sent;
1293 g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
1296 socket = g_socket_new_from_fd (sv[0], &error);
1297 g_assert_no_error (error);
1298 socket_connection = g_socket_connection_factory_create_connection (socket);
1299 g_assert (socket_connection != NULL);
1300 g_object_unref (socket);
1301 producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1303 G_DBUS_CONNECTION_FLAGS_NONE,
1304 NULL, /* GDBusAuthObserver */
1305 NULL, /* GCancellable */
1307 g_dbus_connection_set_exit_on_close (producer, TRUE);
1308 g_assert_no_error (error);
1309 g_object_unref (socket_connection);
1310 n_messages_sent = 0;
1311 g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
1313 /* send enough data that we get an EAGAIN */
1314 for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
1317 g_dbus_connection_emit_signal (producer,
1318 NULL, /* destination */
1320 "org.foo.Interface",
1322 g_variant_new ("(s)", "a string"),
1324 g_assert_no_error (error);
1327 /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
1328 * kernel buffers) and verify that n_messages_sent <
1329 * OVERFLOW_NUM_SIGNALS
1331 * This is to verify that not all the submitted messages have been
1332 * sent to the underlying transport.
1334 g_timeout_add (500, overflow_on_500ms_later_func, NULL);
1335 g_main_loop_run (loop);
1336 g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
1338 /* now suck it all out as a client, and add it up */
1339 socket = g_socket_new_from_fd (sv[1], &error);
1340 g_assert_no_error (error);
1341 socket_connection = g_socket_connection_factory_create_connection (socket);
1342 g_assert (socket_connection != NULL);
1343 g_object_unref (socket);
1344 consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1346 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
1347 NULL, /* GDBusAuthObserver */
1348 NULL, /* GCancellable */
1350 g_assert_no_error (error);
1351 g_object_unref (socket_connection);
1352 n_messages_received = 0;
1353 g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
1354 g_dbus_connection_start_message_processing (consumer);
1356 timer = g_timer_new ();
1357 g_timer_start (timer);
1359 while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
1360 g_main_context_iteration (NULL, FALSE);
1362 g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
1363 g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
1365 g_timer_destroy (timer);
1366 g_object_unref (consumer);
1367 g_object_unref (producer);
1371 test_overflow (void)
1373 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
1377 /* ---------------------------------------------------------------------------------------------------- */
1384 GDBusNodeInfo *introspection_data = NULL;
1387 g_thread_init (NULL);
1388 g_test_init (&argc, &argv, NULL);
1390 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1391 g_assert (introspection_data != NULL);
1392 test_interface_introspection_data = introspection_data->interfaces[0];
1394 test_guid = g_dbus_generate_guid ();
1396 /* all the tests rely on a shared main loop */
1397 loop = g_main_loop_new (NULL, FALSE);
1399 g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1400 g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1401 g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1402 g_test_add_func ("/gdbus/credentials", test_credentials);
1403 g_test_add_func ("/gdbus/overflow", test_overflow);
1407 g_main_loop_unref (loop);
1409 g_dbus_node_info_unref (introspection_data);