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>
34 #include <glib/gstdio.h>
36 #include <gio/gnetworkingprivate.h>
37 #include <gio/gunixsocketaddress.h>
38 #include <gio/gunixfdlist.h>
40 /* used in test_overflow */
42 #include <gio/gunixconnection.h>
46 #include "gdbus-tests.h"
49 static gboolean is_unix = TRUE;
51 static gboolean is_unix = FALSE;
54 static gchar *test_guid = NULL;
55 static GMainLoop *service_loop = NULL;
56 static GDBusServer *server = NULL;
57 static GMainLoop *loop = NULL;
59 /* ---------------------------------------------------------------------------------------------------- */
60 /* Test that peer-to-peer connections work */
61 /* ---------------------------------------------------------------------------------------------------- */
66 gboolean accept_connection;
67 gint num_connection_attempts;
68 GPtrArray *current_connections;
69 guint num_method_calls;
70 gboolean signal_received;
73 static const gchar *test_interface_introspection_xml =
75 " <interface name='org.gtk.GDBus.PeerTestInterface'>"
76 " <method name='HelloPeer'>"
77 " <arg type='s' name='greeting' direction='in'/>"
78 " <arg type='s' name='response' direction='out'/>"
80 " <method name='EmitSignal'/>"
81 " <method name='EmitSignalWithNameSet'/>"
82 " <method name='OpenFile'>"
83 " <arg type='s' name='path' direction='in'/>"
85 " <signal name='PeerSignal'>"
86 " <arg type='s' name='a_string'/>"
88 " <property type='s' name='PeerProperty' access='read'/>"
91 static GDBusInterfaceInfo *test_interface_introspection_data = NULL;
94 test_interface_method_call (GDBusConnection *connection,
96 const gchar *object_path,
97 const gchar *interface_name,
98 const gchar *method_name,
100 GDBusMethodInvocation *invocation,
103 PeerData *data = user_data;
104 const GDBusMethodInfo *info;
106 data->num_method_calls++;
108 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
109 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
111 info = g_dbus_method_invocation_get_method_info (invocation);
112 g_assert_cmpstr (info->name, ==, method_name);
114 if (g_strcmp0 (method_name, "HelloPeer") == 0)
116 const gchar *greeting;
119 g_variant_get (parameters, "(&s)", &greeting);
121 response = g_strdup_printf ("You greeted me with '%s'.",
123 g_dbus_method_invocation_return_value (invocation,
124 g_variant_new ("(s)", response));
127 else if (g_strcmp0 (method_name, "EmitSignal") == 0)
132 g_dbus_connection_emit_signal (connection,
134 "/org/gtk/GDBus/PeerTestObject",
135 "org.gtk.GDBus.PeerTestInterface",
139 g_assert_no_error (error);
140 g_dbus_method_invocation_return_value (invocation, NULL);
142 else if (g_strcmp0 (method_name, "EmitSignalWithNameSet") == 0)
146 GDBusMessage *message;
148 message = g_dbus_message_new_signal ("/org/gtk/GDBus/PeerTestObject",
149 "org.gtk.GDBus.PeerTestInterface",
150 "PeerSignalWithNameSet");
151 g_dbus_message_set_sender (message, ":1.42");
154 ret = g_dbus_connection_send_message (connection, message, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error);
155 g_assert_no_error (error);
157 g_object_unref (message);
159 g_dbus_method_invocation_return_value (invocation, NULL);
161 else if (g_strcmp0 (method_name, "OpenFile") == 0)
168 GUnixFDList *fd_list;
170 g_variant_get (parameters, "(&s)", &path);
172 fd_list = g_unix_fd_list_new ();
176 fd = open (path, O_RDONLY);
177 g_unix_fd_list_append (fd_list, fd, &error);
178 g_assert_no_error (error);
181 reply = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
182 g_dbus_message_set_unix_fd_list (reply, fd_list);
183 g_object_unref (fd_list);
184 g_object_unref (invocation);
187 g_dbus_connection_send_message (connection,
189 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
190 NULL, /* out_serial */
192 g_assert_no_error (error);
193 g_object_unref (reply);
195 g_dbus_method_invocation_return_dbus_error (invocation,
196 "org.gtk.GDBus.NotOnUnix",
197 "Your OS does not support file descriptor passing");
202 g_assert_not_reached ();
207 test_interface_get_property (GDBusConnection *connection,
209 const gchar *object_path,
210 const gchar *interface_name,
211 const gchar *property_name,
215 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
216 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
217 g_assert_cmpstr (property_name, ==, "PeerProperty");
219 return g_variant_new_string ("ThePropertyValue");
223 static const GDBusInterfaceVTable test_interface_vtable =
225 test_interface_method_call,
226 test_interface_get_property,
227 NULL /* set_property */
231 on_proxy_signal_received (GDBusProxy *proxy,
234 GVariant *parameters,
237 PeerData *data = user_data;
239 data->signal_received = TRUE;
241 g_assert (sender_name == NULL);
242 g_assert_cmpstr (signal_name, ==, "PeerSignal");
243 g_main_loop_quit (loop);
247 on_proxy_signal_received_with_name_set (GDBusProxy *proxy,
250 GVariant *parameters,
253 PeerData *data = user_data;
255 data->signal_received = TRUE;
257 g_assert_cmpstr (sender_name, ==, ":1.42");
258 g_assert_cmpstr (signal_name, ==, "PeerSignalWithNameSet");
259 g_main_loop_quit (loop);
262 /* ---------------------------------------------------------------------------------------------------- */
265 on_authorize_authenticated_peer (GDBusAuthObserver *observer,
267 GCredentials *credentials,
270 PeerData *data = user_data;
273 data->num_connection_attempts++;
276 if (!data->accept_connection)
279 g_main_loop_quit (loop);
285 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
287 on_new_connection (GDBusServer *server,
288 GDBusConnection *connection,
291 PeerData *data = user_data;
295 //g_print ("Client connected.\n"
296 // "Negotiated capabilities: unix-fd-passing=%d\n",
297 // g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
299 g_ptr_array_add (data->current_connections, g_object_ref (connection));
301 /* export object on the newly established connection */
303 reg_id = g_dbus_connection_register_object (connection,
304 "/org/gtk/GDBus/PeerTestObject",
305 test_interface_introspection_data,
306 &test_interface_vtable,
308 NULL, /* GDestroyNotify for data */
310 g_assert_no_error (error);
311 g_assert (reg_id > 0);
313 g_main_loop_quit (loop);
317 service_thread_func (gpointer user_data)
319 PeerData *data = user_data;
320 GMainContext *service_context;
321 GDBusAuthObserver *observer;
324 service_context = g_main_context_new ();
325 g_main_context_push_thread_default (service_context);
328 observer = g_dbus_auth_observer_new ();
329 server = g_dbus_server_new_sync (is_unix ? "unix:tmpdir=/tmp/gdbus-test-" : "nonce-tcp:",
330 G_DBUS_SERVER_FLAGS_NONE,
333 NULL, /* cancellable */
335 g_assert_no_error (error);
337 g_signal_connect (server,
339 G_CALLBACK (on_new_connection),
341 g_signal_connect (observer,
342 "authorize-authenticated-peer",
343 G_CALLBACK (on_authorize_authenticated_peer),
345 g_object_unref (observer);
347 g_dbus_server_start (server);
349 service_loop = g_main_loop_new (service_context, FALSE);
350 g_main_loop_run (service_loop);
352 g_main_context_pop_thread_default (service_context);
354 g_main_loop_unref (service_loop);
355 g_main_context_unref (service_context);
357 /* test code specifically unrefs the server - see below */
358 g_assert (server == NULL);
365 on_incoming_connection (GSocketService *service,
366 GSocketConnection *socket_connection,
367 GObject *source_object,
370 PeerData *data = user_data;
372 if (data->accept_connection)
376 GDBusConnection *connection;
379 connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
381 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
382 NULL, /* cancellable */
384 g_assert_no_error (error);
386 g_ptr_array_add (data->current_connections, connection);
388 /* export object on the newly established connection */
390 reg_id = g_dbus_connection_register_object (connection,
391 "/org/gtk/GDBus/PeerTestObject",
392 &test_interface_introspection_data,
393 &test_interface_vtable,
395 NULL, /* GDestroyNotify for data */
397 g_assert_no_error (error);
398 g_assert (reg_id > 0);
403 /* don't do anything */
406 data->num_connection_attempts++;
408 g_main_loop_quit (loop);
410 /* stops other signal handlers from being invoked */
415 service_thread_func (gpointer data)
417 GMainContext *service_context;
419 GSocketAddress *address;
422 service_context = g_main_context_new ();
423 g_main_context_push_thread_default (service_context);
425 socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
426 address = g_unix_socket_address_new (socket_path);
428 service = g_socket_service_new ();
430 g_socket_listener_add_address (G_SOCKET_LISTENER (service),
432 G_SOCKET_TYPE_STREAM,
433 G_SOCKET_PROTOCOL_DEFAULT,
434 NULL, /* source_object */
435 NULL, /* effective_address */
437 g_assert_no_error (error);
438 g_signal_connect (service,
440 G_CALLBACK (on_incoming_connection),
442 g_socket_service_start (service);
444 service_loop = g_main_loop_new (service_context, FALSE);
445 g_main_loop_run (service_loop);
447 g_main_context_pop_thread_default (service_context);
449 g_main_loop_unref (service_loop);
450 g_main_context_unref (service_context);
452 g_object_unref (address);
453 g_free (socket_path);
458 /* ---------------------------------------------------------------------------------------------------- */
462 check_connection (gpointer user_data)
464 PeerData *data = user_data;
467 for (n = 0; n < data->current_connections->len; n++)
472 c = G_DBUS_CONNECTION (data->current_connections->pdata[n]);
473 stream = g_dbus_connection_get_stream (c);
475 g_debug ("In check_connection for %d: connection %p, stream %p", n, c, stream);
476 g_debug ("closed = %d", g_io_stream_is_closed (stream));
479 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
480 g_debug ("socket_closed = %d", g_socket_is_closed (socket));
481 g_debug ("socket_condition_check = %d", g_socket_condition_check (socket, G_IO_IN|G_IO_OUT|G_IO_ERR|G_IO_HUP));
487 num_read = g_input_stream_read (g_io_stream_get_input_stream (stream),
494 g_debug ("error: %s", error->message);
495 g_error_free (error);
499 g_debug ("no error, read %d bytes", (gint) num_read);
507 on_do_disconnect_in_idle (gpointer data)
509 GDBusConnection *c = G_DBUS_CONNECTION (data);
510 g_debug ("GDC %p has ref_count %d", c, G_OBJECT (c)->ref_count);
511 g_dbus_connection_disconnect (c);
519 read_all_from_fd (gint fd, gsize *out_len, GError **error)
525 str = g_string_new (NULL);
529 num_read = read (fd, buf, sizeof (buf));
532 if (errno == EAGAIN || errno == EWOULDBLOCK)
536 g_io_error_from_errno (errno),
537 "Failed reading %d bytes into offset %d: %s",
543 else if (num_read > 0)
545 g_string_append_len (str, buf, num_read);
547 else if (num_read == 0)
556 return g_string_free (str, FALSE);
561 g_string_free (str, TRUE);
577 GThread *service_thread;
578 gulong signal_handler_id;
580 memset (&data, '\0', sizeof (PeerData));
581 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
583 /* first try to connect when there is no server */
585 c = g_dbus_connection_new_for_address_sync (is_unix ? "unix:path=/tmp/gdbus-test-does-not-exist-pid" :
586 /* NOTE: Even if something is listening on port 12345 the connection
587 * will fail because the nonce file doesn't exist */
588 "nonce-tcp:host=localhost,port=12345,noncefile=this-does-not-exist-gdbus",
589 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
590 NULL, /* GDBusAuthObserver */
591 NULL, /* cancellable */
593 _g_assert_error_domain (error, G_IO_ERROR);
594 g_assert (!g_dbus_error_is_remote_error (error));
595 g_clear_error (&error);
596 g_assert (c == NULL);
598 /* bring up a server - we run the server in a different thread to avoid deadlocks */
600 service_thread = g_thread_create (service_thread_func,
604 while (service_loop == NULL)
606 g_assert (server != NULL);
608 /* bring up a connection and accept it */
609 data.accept_connection = TRUE;
611 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
612 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
613 NULL, /* GDBusAuthObserver */
614 NULL, /* cancellable */
616 g_assert_no_error (error);
617 g_assert (c != NULL);
618 while (data.current_connections->len < 1)
619 g_main_loop_run (loop);
620 g_assert_cmpint (data.current_connections->len, ==, 1);
621 g_assert_cmpint (data.num_connection_attempts, ==, 1);
622 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
623 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
625 /* check that we create a proxy, read properties, receive signals and invoke
626 * the HelloPeer() method. Since the server runs in another thread it's fine
627 * to use synchronous blocking API here.
630 proxy = g_dbus_proxy_new_sync (c,
631 G_DBUS_PROXY_FLAGS_NONE,
634 "/org/gtk/GDBus/PeerTestObject",
635 "org.gtk.GDBus.PeerTestInterface",
636 NULL, /* GCancellable */
638 g_assert_no_error (error);
639 g_assert (proxy != NULL);
641 value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
642 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
644 /* try invoking a method */
646 result = g_dbus_proxy_call_sync (proxy,
648 g_variant_new ("(s)", "Hey Peer!"),
649 G_DBUS_CALL_FLAGS_NONE,
651 NULL, /* GCancellable */
653 g_assert_no_error (error);
654 g_variant_get (result, "(&s)", &s);
655 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
656 g_variant_unref (result);
657 g_assert_cmpint (data.num_method_calls, ==, 1);
659 /* make the other peer emit a signal - catch it */
660 signal_handler_id = g_signal_connect (proxy,
662 G_CALLBACK (on_proxy_signal_received),
664 g_assert (!data.signal_received);
665 g_dbus_proxy_call (proxy,
667 NULL, /* no arguments */
668 G_DBUS_CALL_FLAGS_NONE,
670 NULL, /* GCancellable */
671 NULL, /* GAsyncReadyCallback - we don't care about the result */
672 NULL); /* user_data */
673 g_main_loop_run (loop);
674 g_assert (data.signal_received);
675 g_assert_cmpint (data.num_method_calls, ==, 2);
676 g_signal_handler_disconnect (proxy, signal_handler_id);
678 /* Also ensure that messages with the sender header-field set gets
679 * delivered to the proxy - note that this doesn't really make sense
680 * e.g. names are meaning-less in a peer-to-peer case... but we
681 * support it because it makes sense in certain bridging
682 * applications - see e.g. #623815.
684 signal_handler_id = g_signal_connect (proxy,
686 G_CALLBACK (on_proxy_signal_received_with_name_set),
688 data.signal_received = FALSE;
689 g_dbus_proxy_call (proxy,
690 "EmitSignalWithNameSet",
691 NULL, /* no arguments */
692 G_DBUS_CALL_FLAGS_NONE,
694 NULL, /* GCancellable */
695 NULL, /* GAsyncReadyCallback - we don't care about the result */
696 NULL); /* user_data */
697 g_main_loop_run (loop);
698 g_assert (data.signal_received);
699 g_assert_cmpint (data.num_method_calls, ==, 3);
700 g_signal_handler_disconnect (proxy, signal_handler_id);
702 /* check for UNIX fd passing */
705 GDBusMessage *method_call_message;
706 GDBusMessage *method_reply_message;
707 GUnixFDList *fd_list;
714 method_call_message = g_dbus_message_new_method_call (NULL, /* name */
715 "/org/gtk/GDBus/PeerTestObject",
716 "org.gtk.GDBus.PeerTestInterface",
718 g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", "/etc/hosts"));
720 method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
722 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
724 NULL, /* out_serial */
725 NULL, /* cancellable */
727 g_assert_no_error (error);
728 g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
729 fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
730 g_assert (fd_list != NULL);
731 g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
733 fd = g_unix_fd_list_get (fd_list, 0, &error);
734 g_assert_no_error (error);
735 g_object_unref (method_call_message);
736 g_object_unref (method_reply_message);
739 buf = read_all_from_fd (fd, &len, &error);
740 g_assert_no_error (error);
741 g_assert (buf != NULL);
745 g_file_get_contents ("/etc/hosts",
749 g_assert_no_error (error);
750 g_assert_cmpint (len, ==, len2);
751 g_assert (memcmp (buf, buf2, len) == 0);
757 result = g_dbus_proxy_call_sync (proxy,
759 g_variant_new ("(s)", "boo"),
760 G_DBUS_CALL_FLAGS_NONE,
762 NULL, /* GCancellable */
764 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
765 g_assert (result == NULL);
766 g_error_free (error);
767 #endif /* G_OS_UNIX */
769 /* Check that g_socket_get_credentials() work - this really should
770 * be in a GSocket-specific test suite but no such test suite exists
775 GCredentials *credentials;
776 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (g_dbus_connection_get_stream (c)));
777 g_assert (G_IS_SOCKET (socket));
779 credentials = g_socket_get_credentials (socket, &error);
782 struct ucred *native_creds;
783 g_assert_no_error (error);
784 g_assert (G_IS_CREDENTIALS (credentials));
785 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED);
786 g_assert (native_creds != NULL);
787 g_assert (native_creds->uid == getuid ());
788 g_assert (native_creds->gid == getgid ());
789 g_assert (native_creds->pid == getpid ());
791 g_object_unref (credentials);
793 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
794 g_assert (credentials == NULL);
799 /* bring up a connection - don't accept it - this should fail
801 data.accept_connection = FALSE;
803 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
804 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
805 NULL, /* GDBusAuthObserver */
806 NULL, /* cancellable */
808 _g_assert_error_domain (error, G_IO_ERROR);
809 g_assert (c2 == NULL);
812 /* TODO: THIS TEST DOESN'T WORK YET */
814 /* bring up a connection - accept it.. then disconnect from the client side - check
815 * that the server side gets the disconnect signal.
818 data.accept_connection = TRUE;
819 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
820 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
821 NULL, /* GDBusAuthObserver */
822 NULL, /* cancellable */
824 g_assert_no_error (error);
825 g_assert (c2 != NULL);
826 g_assert (!g_dbus_connection_get_is_disconnected (c2));
827 while (data.num_connection_attempts < 3)
828 g_main_loop_run (loop);
829 g_assert_cmpint (data.current_connections->len, ==, 2);
830 g_assert_cmpint (data.num_connection_attempts, ==, 3);
831 g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
832 g_idle_add (on_do_disconnect_in_idle, c2);
833 g_debug ("==================================================");
834 g_debug ("==================================================");
835 g_debug ("==================================================");
836 g_debug ("waiting for disconnect on connection %p, stream %p",
837 data.current_connections->pdata[1],
838 g_dbus_connection_get_stream (data.current_connections->pdata[1]));
840 g_timeout_add (2000, check_connection, &data);
841 //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
842 g_main_loop_run (loop);
843 g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
844 g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
847 /* unref the server and stop listening for new connections
849 * This won't bring down the established connections - check that c is still connected
850 * by invoking a method
852 //g_socket_service_stop (service);
853 //g_object_unref (service);
854 g_dbus_server_stop (server);
855 g_object_unref (server);
859 result = g_dbus_proxy_call_sync (proxy,
861 g_variant_new ("(s)", "Hey Again Peer!"),
862 G_DBUS_CALL_FLAGS_NONE,
864 NULL, /* GCancellable */
866 g_assert_no_error (error);
867 g_variant_get (result, "(&s)", &s);
868 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
869 g_variant_unref (result);
870 g_assert_cmpint (data.num_method_calls, ==, 5);
873 /* TODO: THIS TEST DOESN'T WORK YET */
875 /* now disconnect from the server side - check that the client side gets the signal */
876 g_assert_cmpint (data.current_connections->len, ==, 1);
877 g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
878 g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
879 if (!g_dbus_connection_get_is_disconnected (c))
880 _g_assert_signal_received (c, "closed");
881 g_assert (g_dbus_connection_get_is_disconnected (c));
885 g_ptr_array_unref (data.current_connections);
886 g_object_unref (proxy);
888 g_main_loop_quit (service_loop);
889 g_thread_join (service_thread);
892 /* ---------------------------------------------------------------------------------------------------- */
897 GMainContext *context;
904 dmp_data_free (DmpData *data)
906 g_main_loop_unref (data->loop);
907 g_main_context_unref (data->context);
908 g_object_unref (data->server);
909 g_list_foreach (data->connections, (GFunc) g_object_unref, NULL);
910 g_list_free (data->connections);
915 dmp_on_method_call (GDBusConnection *connection,
917 const gchar *object_path,
918 const gchar *interface_name,
919 const gchar *method_name,
920 GVariant *parameters,
921 GDBusMethodInvocation *invocation,
924 //DmpData *data = user_data;
927 g_variant_get (parameters,
931 g_dbus_method_invocation_return_value (invocation,
932 g_variant_new ("(i)", first + second));
935 static const GDBusInterfaceVTable dmp_interface_vtable =
938 NULL, /* get_property */
939 NULL /* set_property */
943 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
945 dmp_on_new_connection (GDBusServer *server,
946 GDBusConnection *connection,
949 DmpData *data = user_data;
953 /* accept the connection */
954 data->connections = g_list_prepend (data->connections, g_object_ref (connection));
957 node = g_dbus_node_info_new_for_xml ("<node>"
958 " <interface name='org.gtk.GDBus.DmpInterface'>"
959 " <method name='AddPair'>"
960 " <arg type='i' name='first' direction='in'/>"
961 " <arg type='i' name='second' direction='in'/>"
962 " <arg type='i' name='sum' direction='out'/>"
967 g_assert_no_error (error);
969 /* sleep 100ms before exporting an object - this is to test that
970 * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
971 * (GDBusServer uses this feature).
975 /* export an object */
977 g_dbus_connection_register_object (connection,
980 &dmp_interface_vtable,
984 g_dbus_node_info_unref (node);
988 dmp_thread_func (gpointer user_data)
990 DmpData *data = user_data;
994 data->context = g_main_context_new ();
995 g_main_context_push_thread_default (data->context);
998 guid = g_dbus_generate_guid ();
999 data->server = g_dbus_server_new_sync ("unix:tmpdir=/tmp/gdbus-test-",
1000 G_DBUS_SERVER_FLAGS_NONE,
1002 NULL, /* GDBusAuthObserver */
1003 NULL, /* GCancellable */
1005 g_assert_no_error (error);
1006 g_signal_connect (data->server,
1008 G_CALLBACK (dmp_on_new_connection),
1011 g_dbus_server_start (data->server);
1013 data->loop = g_main_loop_new (data->context, FALSE);
1014 g_main_loop_run (data->loop);
1016 g_main_context_pop_thread_default (data->context);
1023 delayed_message_processing (void)
1027 GThread *service_thread;
1030 data = g_new0 (DmpData, 1);
1033 service_thread = g_thread_create (dmp_thread_func,
1037 while (data->server == NULL || !g_dbus_server_is_active (data->server))
1040 for (n = 0; n < 5; n++)
1047 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
1048 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1049 NULL, /* GDBusAuthObserver */
1050 NULL, /* GCancellable */
1052 g_assert_no_error (error);
1055 res = g_dbus_connection_call_sync (c,
1056 NULL, /* bus name */
1058 "org.gtk.GDBus.DmpInterface",
1060 g_variant_new ("(ii)", 2, n),
1061 G_VARIANT_TYPE ("(i)"),
1062 G_DBUS_CALL_FLAGS_NONE,
1063 -1, /* timeout_msec */
1064 NULL, /* GCancellable */
1066 g_assert_no_error (error);
1067 g_variant_get (res, "(i)", &val);
1068 g_assert_cmpint (val, ==, 2 + n);
1069 g_variant_unref (res);
1073 g_main_loop_quit (data->loop);
1074 g_thread_join (service_thread);
1075 dmp_data_free (data);
1078 /* ---------------------------------------------------------------------------------------------------- */
1081 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1083 GCredentials *credentials,
1086 PeerData *data = user_data;
1087 gboolean authorized;
1089 data->num_connection_attempts++;
1092 if (!data->accept_connection)
1095 g_main_loop_quit (loop);
1101 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1103 nonce_tcp_on_new_connection (GDBusServer *server,
1104 GDBusConnection *connection,
1107 PeerData *data = user_data;
1109 g_ptr_array_add (data->current_connections, g_object_ref (connection));
1111 g_main_loop_quit (loop);
1115 nonce_tcp_service_thread_func (gpointer user_data)
1117 PeerData *data = user_data;
1118 GMainContext *service_context;
1119 GDBusAuthObserver *observer;
1122 service_context = g_main_context_new ();
1123 g_main_context_push_thread_default (service_context);
1126 observer = g_dbus_auth_observer_new ();
1127 server = g_dbus_server_new_sync ("nonce-tcp:",
1128 G_DBUS_SERVER_FLAGS_NONE,
1131 NULL, /* cancellable */
1133 g_assert_no_error (error);
1135 g_signal_connect (server,
1137 G_CALLBACK (nonce_tcp_on_new_connection),
1139 g_signal_connect (observer,
1140 "authorize-authenticated-peer",
1141 G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1143 g_object_unref (observer);
1145 g_dbus_server_start (server);
1147 service_loop = g_main_loop_new (service_context, FALSE);
1148 g_main_loop_run (service_loop);
1150 g_main_context_pop_thread_default (service_context);
1152 g_main_loop_unref (service_loop);
1153 g_main_context_unref (service_context);
1155 /* test code specifically unrefs the server - see below */
1156 g_assert (server == NULL);
1162 test_nonce_tcp (void)
1166 GThread *service_thread;
1171 const gchar *address;
1173 memset (&data, '\0', sizeof (PeerData));
1174 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1178 service_loop = NULL;
1179 service_thread = g_thread_create (nonce_tcp_service_thread_func,
1183 while (service_loop == NULL)
1185 g_assert (server != NULL);
1188 /* bring up a connection and accept it */
1189 data.accept_connection = TRUE;
1191 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1192 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1193 NULL, /* GDBusAuthObserver */
1194 NULL, /* cancellable */
1196 g_assert_no_error (error);
1197 g_assert (c != NULL);
1198 while (data.current_connections->len < 1)
1199 g_main_loop_run (loop);
1200 g_assert_cmpint (data.current_connections->len, ==, 1);
1201 g_assert_cmpint (data.num_connection_attempts, ==, 1);
1202 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1203 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1206 /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1209 address = g_dbus_server_get_client_address (server);
1211 s = strstr (address, "noncefile=");
1212 g_assert (s != NULL);
1213 s += sizeof "noncefile=" - 1;
1214 nonce_file = g_strdup (s);
1216 /* First try invalid data in the nonce file - this will actually
1217 * make the client send this and the server will reject it. The way
1218 * it works is that if the nonce doesn't match, the server will
1219 * simply close the connection. So, from the client point of view,
1220 * we can see a variety of errors.
1223 res = g_file_set_contents (nonce_file,
1227 g_assert_no_error (error);
1229 c = g_dbus_connection_new_for_address_sync (address,
1230 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1231 NULL, /* GDBusAuthObserver */
1232 NULL, /* cancellable */
1234 _g_assert_error_domain (error, G_IO_ERROR);
1235 g_assert (c == NULL);
1237 /* Then try with a nonce-file of incorrect length - this will make
1238 * the client complain - we won't even try connecting to the server
1242 res = g_file_set_contents (nonce_file,
1243 "0123456789012345_",
1246 g_assert_no_error (error);
1248 c = g_dbus_connection_new_for_address_sync (address,
1249 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1250 NULL, /* GDBusAuthObserver */
1251 NULL, /* cancellable */
1253 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1254 g_assert (c == NULL);
1256 /* Finally try with no nonce-file at all */
1257 g_assert_cmpint (g_unlink (nonce_file), ==, 0);
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_assert (c == NULL);
1267 g_free (nonce_file);
1269 g_dbus_server_stop (server);
1270 g_object_unref (server);
1273 g_main_loop_quit (service_loop);
1274 g_thread_join (service_thread);
1278 test_credentials (void)
1280 GCredentials *c1, *c2;
1284 c1 = g_credentials_new ();
1285 c2 = g_credentials_new ();
1288 if (g_credentials_set_unix_user (c2, getuid (), &error))
1289 g_assert_no_error (error);
1291 g_clear_error (&error);
1292 g_assert (g_credentials_is_same_user (c1, c2, &error));
1293 g_assert_no_error (error);
1295 desc = g_credentials_to_string (c1);
1296 g_assert (desc != NULL);
1299 g_object_unref (c1);
1300 g_object_unref (c2);
1303 /* ---------------------------------------------------------------------------------------------------- */
1307 /* Chosen to be big enough to overflow the socket buffer */
1308 #define OVERFLOW_NUM_SIGNALS 5000
1309 #define OVERFLOW_TIMEOUT_SEC 10
1311 static GDBusMessageFilterResult
1312 overflow_filter_func (GDBusConnection *connection,
1313 GDBusMessage *message,
1317 volatile gint *counter = user_data;
1319 return G_DBUS_MESSAGE_FILTER_RESULT_NO_EFFECT;
1323 overflow_on_500ms_later_func (gpointer user_data)
1325 g_main_loop_quit (loop);
1326 return FALSE; /* don't keep the idle */
1330 test_overflow (void)
1335 GSocketConnection *socket_connection;
1336 GDBusConnection *producer, *consumer;
1339 volatile gint n_messages_received;
1340 volatile gint n_messages_sent;
1342 g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
1345 socket = g_socket_new_from_fd (sv[0], &error);
1346 g_assert_no_error (error);
1347 socket_connection = g_socket_connection_factory_create_connection (socket);
1348 g_assert (socket_connection != NULL);
1349 g_object_unref (socket);
1350 producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1352 G_DBUS_CONNECTION_FLAGS_NONE,
1353 NULL, /* GDBusAuthObserver */
1354 NULL, /* GCancellable */
1356 g_dbus_connection_set_exit_on_close (producer, TRUE);
1357 g_assert_no_error (error);
1358 g_object_unref (socket_connection);
1359 n_messages_sent = 0;
1360 g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
1362 /* send enough data that we get an EAGAIN */
1363 for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
1366 g_dbus_connection_emit_signal (producer,
1367 NULL, /* destination */
1369 "org.foo.Interface",
1371 g_variant_new ("(s)", "a string"),
1373 g_assert_no_error (error);
1376 /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
1377 * kernel buffers) and verify that n_messages_sent <
1378 * OVERFLOW_NUM_SIGNALS
1380 * This is to verify that not all the submitted messages have been
1381 * sent to the underlying transport.
1383 g_timeout_add (500, overflow_on_500ms_later_func, NULL);
1384 g_main_loop_run (loop);
1385 g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
1387 /* now suck it all out as a client, and add it up */
1388 socket = g_socket_new_from_fd (sv[1], &error);
1389 g_assert_no_error (error);
1390 socket_connection = g_socket_connection_factory_create_connection (socket);
1391 g_assert (socket_connection != NULL);
1392 g_object_unref (socket);
1393 consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1395 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
1396 NULL, /* GDBusAuthObserver */
1397 NULL, /* GCancellable */
1399 g_assert_no_error (error);
1400 g_object_unref (socket_connection);
1401 n_messages_received = 0;
1402 g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
1403 g_dbus_connection_start_message_processing (consumer);
1405 timer = g_timer_new ();
1406 g_timer_start (timer);
1408 while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
1409 g_main_context_iteration (NULL, FALSE);
1411 g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
1412 g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
1414 g_timer_destroy (timer);
1415 g_object_unref (consumer);
1416 g_object_unref (producer);
1420 test_overflow (void)
1422 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
1426 /* ---------------------------------------------------------------------------------------------------- */
1433 GDBusNodeInfo *introspection_data = NULL;
1436 g_thread_init (NULL);
1437 g_test_init (&argc, &argv, NULL);
1439 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1440 g_assert (introspection_data != NULL);
1441 test_interface_introspection_data = introspection_data->interfaces[0];
1443 test_guid = g_dbus_generate_guid ();
1445 /* all the tests rely on a shared main loop */
1446 loop = g_main_loop_new (NULL, FALSE);
1448 g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1449 g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1450 g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1451 g_test_add_func ("/gdbus/credentials", test_credentials);
1452 g_test_add_func ("/gdbus/overflow", test_overflow);
1456 g_main_loop_unref (loop);
1458 g_dbus_node_info_unref (introspection_data);