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);
740 buf = read_all_from_fd (fd, &len, &error);
741 g_assert_no_error (error);
742 g_assert (buf != NULL);
746 g_file_get_contents ("/etc/hosts",
750 g_assert_no_error (error);
751 g_assert_cmpint (len, ==, len2);
752 g_assert (memcmp (buf, buf2, len) == 0);
758 result = g_dbus_proxy_call_sync (proxy,
760 g_variant_new ("(s)", "boo"),
761 G_DBUS_CALL_FLAGS_NONE,
763 NULL, /* GCancellable */
765 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
766 g_assert (result == NULL);
767 g_error_free (error);
768 #endif /* G_OS_UNIX */
770 /* Check that g_socket_get_credentials() work - this really should
771 * be in a GSocket-specific test suite but no such test suite exists
776 GCredentials *credentials;
777 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (g_dbus_connection_get_stream (c)));
778 g_assert (G_IS_SOCKET (socket));
780 credentials = g_socket_get_credentials (socket, &error);
783 struct ucred *native_creds;
784 g_assert_no_error (error);
785 g_assert (G_IS_CREDENTIALS (credentials));
786 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED);
787 g_assert (native_creds != NULL);
788 g_assert (native_creds->uid == getuid ());
789 g_assert (native_creds->gid == getgid ());
790 g_assert (native_creds->pid == getpid ());
792 g_object_unref (credentials);
794 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
795 g_assert (credentials == NULL);
800 /* bring up a connection - don't accept it - this should fail
802 data.accept_connection = FALSE;
804 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
805 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
806 NULL, /* GDBusAuthObserver */
807 NULL, /* cancellable */
809 _g_assert_error_domain (error, G_IO_ERROR);
810 g_error_free (error);
811 g_assert (c2 == NULL);
814 /* TODO: THIS TEST DOESN'T WORK YET */
816 /* bring up a connection - accept it.. then disconnect from the client side - check
817 * that the server side gets the disconnect signal.
820 data.accept_connection = TRUE;
821 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
822 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
823 NULL, /* GDBusAuthObserver */
824 NULL, /* cancellable */
826 g_assert_no_error (error);
827 g_assert (c2 != NULL);
828 g_assert (!g_dbus_connection_get_is_disconnected (c2));
829 while (data.num_connection_attempts < 3)
830 g_main_loop_run (loop);
831 g_assert_cmpint (data.current_connections->len, ==, 2);
832 g_assert_cmpint (data.num_connection_attempts, ==, 3);
833 g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
834 g_idle_add (on_do_disconnect_in_idle, c2);
835 g_debug ("==================================================");
836 g_debug ("==================================================");
837 g_debug ("==================================================");
838 g_debug ("waiting for disconnect on connection %p, stream %p",
839 data.current_connections->pdata[1],
840 g_dbus_connection_get_stream (data.current_connections->pdata[1]));
842 g_timeout_add (2000, check_connection, &data);
843 //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
844 g_main_loop_run (loop);
845 g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
846 g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
849 /* unref the server and stop listening for new connections
851 * This won't bring down the established connections - check that c is still connected
852 * by invoking a method
854 //g_socket_service_stop (service);
855 //g_object_unref (service);
856 g_dbus_server_stop (server);
857 g_object_unref (server);
861 result = g_dbus_proxy_call_sync (proxy,
863 g_variant_new ("(s)", "Hey Again Peer!"),
864 G_DBUS_CALL_FLAGS_NONE,
866 NULL, /* GCancellable */
868 g_assert_no_error (error);
869 g_variant_get (result, "(&s)", &s);
870 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
871 g_variant_unref (result);
872 g_assert_cmpint (data.num_method_calls, ==, 5);
875 /* TODO: THIS TEST DOESN'T WORK YET */
877 /* now disconnect from the server side - check that the client side gets the signal */
878 g_assert_cmpint (data.current_connections->len, ==, 1);
879 g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
880 g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
881 if (!g_dbus_connection_get_is_disconnected (c))
882 _g_assert_signal_received (c, "closed");
883 g_assert (g_dbus_connection_get_is_disconnected (c));
887 g_ptr_array_unref (data.current_connections);
888 g_object_unref (proxy);
890 g_main_loop_quit (service_loop);
891 g_thread_join (service_thread);
894 /* ---------------------------------------------------------------------------------------------------- */
899 GMainContext *context;
906 dmp_data_free (DmpData *data)
908 g_main_loop_unref (data->loop);
909 g_main_context_unref (data->context);
910 g_object_unref (data->server);
911 g_list_foreach (data->connections, (GFunc) g_object_unref, NULL);
912 g_list_free (data->connections);
917 dmp_on_method_call (GDBusConnection *connection,
919 const gchar *object_path,
920 const gchar *interface_name,
921 const gchar *method_name,
922 GVariant *parameters,
923 GDBusMethodInvocation *invocation,
926 //DmpData *data = user_data;
929 g_variant_get (parameters,
933 g_dbus_method_invocation_return_value (invocation,
934 g_variant_new ("(i)", first + second));
937 static const GDBusInterfaceVTable dmp_interface_vtable =
940 NULL, /* get_property */
941 NULL /* set_property */
945 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
947 dmp_on_new_connection (GDBusServer *server,
948 GDBusConnection *connection,
951 DmpData *data = user_data;
955 /* accept the connection */
956 data->connections = g_list_prepend (data->connections, g_object_ref (connection));
959 node = g_dbus_node_info_new_for_xml ("<node>"
960 " <interface name='org.gtk.GDBus.DmpInterface'>"
961 " <method name='AddPair'>"
962 " <arg type='i' name='first' direction='in'/>"
963 " <arg type='i' name='second' direction='in'/>"
964 " <arg type='i' name='sum' direction='out'/>"
969 g_assert_no_error (error);
971 /* sleep 100ms before exporting an object - this is to test that
972 * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
973 * (GDBusServer uses this feature).
977 /* export an object */
979 g_dbus_connection_register_object (connection,
982 &dmp_interface_vtable,
986 g_dbus_node_info_unref (node);
990 dmp_thread_func (gpointer user_data)
992 DmpData *data = user_data;
996 data->context = g_main_context_new ();
997 g_main_context_push_thread_default (data->context);
1000 guid = g_dbus_generate_guid ();
1001 data->server = g_dbus_server_new_sync ("unix:tmpdir=/tmp/gdbus-test-",
1002 G_DBUS_SERVER_FLAGS_NONE,
1004 NULL, /* GDBusAuthObserver */
1005 NULL, /* GCancellable */
1007 g_assert_no_error (error);
1008 g_signal_connect (data->server,
1010 G_CALLBACK (dmp_on_new_connection),
1013 g_dbus_server_start (data->server);
1015 data->loop = g_main_loop_new (data->context, FALSE);
1016 g_main_loop_run (data->loop);
1018 g_main_context_pop_thread_default (data->context);
1025 delayed_message_processing (void)
1029 GThread *service_thread;
1032 data = g_new0 (DmpData, 1);
1035 service_thread = g_thread_create (dmp_thread_func,
1039 while (data->server == NULL || !g_dbus_server_is_active (data->server))
1042 for (n = 0; n < 5; n++)
1049 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
1050 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1051 NULL, /* GDBusAuthObserver */
1052 NULL, /* GCancellable */
1054 g_assert_no_error (error);
1057 res = g_dbus_connection_call_sync (c,
1058 NULL, /* bus name */
1060 "org.gtk.GDBus.DmpInterface",
1062 g_variant_new ("(ii)", 2, n),
1063 G_VARIANT_TYPE ("(i)"),
1064 G_DBUS_CALL_FLAGS_NONE,
1065 -1, /* timeout_msec */
1066 NULL, /* GCancellable */
1068 g_assert_no_error (error);
1069 g_variant_get (res, "(i)", &val);
1070 g_assert_cmpint (val, ==, 2 + n);
1071 g_variant_unref (res);
1075 g_main_loop_quit (data->loop);
1076 g_thread_join (service_thread);
1077 dmp_data_free (data);
1080 /* ---------------------------------------------------------------------------------------------------- */
1083 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1085 GCredentials *credentials,
1088 PeerData *data = user_data;
1089 gboolean authorized;
1091 data->num_connection_attempts++;
1094 if (!data->accept_connection)
1097 g_main_loop_quit (loop);
1103 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1105 nonce_tcp_on_new_connection (GDBusServer *server,
1106 GDBusConnection *connection,
1109 PeerData *data = user_data;
1111 g_ptr_array_add (data->current_connections, g_object_ref (connection));
1113 g_main_loop_quit (loop);
1117 nonce_tcp_service_thread_func (gpointer user_data)
1119 PeerData *data = user_data;
1120 GMainContext *service_context;
1121 GDBusAuthObserver *observer;
1124 service_context = g_main_context_new ();
1125 g_main_context_push_thread_default (service_context);
1128 observer = g_dbus_auth_observer_new ();
1129 server = g_dbus_server_new_sync ("nonce-tcp:",
1130 G_DBUS_SERVER_FLAGS_NONE,
1133 NULL, /* cancellable */
1135 g_assert_no_error (error);
1137 g_signal_connect (server,
1139 G_CALLBACK (nonce_tcp_on_new_connection),
1141 g_signal_connect (observer,
1142 "authorize-authenticated-peer",
1143 G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1145 g_object_unref (observer);
1147 g_dbus_server_start (server);
1149 service_loop = g_main_loop_new (service_context, FALSE);
1150 g_main_loop_run (service_loop);
1152 g_main_context_pop_thread_default (service_context);
1154 g_main_loop_unref (service_loop);
1155 g_main_context_unref (service_context);
1157 /* test code specifically unrefs the server - see below */
1158 g_assert (server == NULL);
1164 test_nonce_tcp (void)
1168 GThread *service_thread;
1173 const gchar *address;
1175 memset (&data, '\0', sizeof (PeerData));
1176 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1180 service_loop = NULL;
1181 service_thread = g_thread_create (nonce_tcp_service_thread_func,
1185 while (service_loop == NULL)
1187 g_assert (server != NULL);
1190 /* bring up a connection and accept it */
1191 data.accept_connection = TRUE;
1193 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1194 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1195 NULL, /* GDBusAuthObserver */
1196 NULL, /* cancellable */
1198 g_assert_no_error (error);
1199 g_assert (c != NULL);
1200 while (data.current_connections->len < 1)
1201 g_main_loop_run (loop);
1202 g_assert_cmpint (data.current_connections->len, ==, 1);
1203 g_assert_cmpint (data.num_connection_attempts, ==, 1);
1204 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1205 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1208 /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1211 address = g_dbus_server_get_client_address (server);
1213 s = strstr (address, "noncefile=");
1214 g_assert (s != NULL);
1215 s += sizeof "noncefile=" - 1;
1216 nonce_file = g_strdup (s);
1218 /* First try invalid data in the nonce file - this will actually
1219 * make the client send this and the server will reject it. The way
1220 * it works is that if the nonce doesn't match, the server will
1221 * simply close the connection. So, from the client point of view,
1222 * we can see a variety of errors.
1225 res = g_file_set_contents (nonce_file,
1229 g_assert_no_error (error);
1231 c = g_dbus_connection_new_for_address_sync (address,
1232 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1233 NULL, /* GDBusAuthObserver */
1234 NULL, /* cancellable */
1236 _g_assert_error_domain (error, G_IO_ERROR);
1237 g_error_free (error);
1238 g_assert (c == NULL);
1240 /* Then try with a nonce-file of incorrect length - this will make
1241 * the client complain - we won't even try connecting to the server
1245 res = g_file_set_contents (nonce_file,
1246 "0123456789012345_",
1249 g_assert_no_error (error);
1251 c = g_dbus_connection_new_for_address_sync (address,
1252 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1253 NULL, /* GDBusAuthObserver */
1254 NULL, /* cancellable */
1256 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1257 g_error_free (error);
1258 g_assert (c == NULL);
1260 /* Finally try with no nonce-file at all */
1261 g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1263 c = g_dbus_connection_new_for_address_sync (address,
1264 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1265 NULL, /* GDBusAuthObserver */
1266 NULL, /* cancellable */
1268 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1269 g_error_free (error);
1270 g_assert (c == NULL);
1272 g_free (nonce_file);
1274 g_dbus_server_stop (server);
1275 g_object_unref (server);
1278 g_main_loop_quit (service_loop);
1279 g_thread_join (service_thread);
1283 test_credentials (void)
1285 GCredentials *c1, *c2;
1289 c1 = g_credentials_new ();
1290 c2 = g_credentials_new ();
1293 if (g_credentials_set_unix_user (c2, getuid (), &error))
1294 g_assert_no_error (error);
1296 g_clear_error (&error);
1297 g_assert (g_credentials_is_same_user (c1, c2, &error));
1298 g_assert_no_error (error);
1300 desc = g_credentials_to_string (c1);
1301 g_assert (desc != NULL);
1304 g_object_unref (c1);
1305 g_object_unref (c2);
1308 /* ---------------------------------------------------------------------------------------------------- */
1312 /* Chosen to be big enough to overflow the socket buffer */
1313 #define OVERFLOW_NUM_SIGNALS 5000
1314 #define OVERFLOW_TIMEOUT_SEC 10
1316 static GDBusMessage *
1317 overflow_filter_func (GDBusConnection *connection,
1318 GDBusMessage *message,
1322 volatile gint *counter = user_data;
1328 overflow_on_500ms_later_func (gpointer user_data)
1330 g_main_loop_quit (loop);
1331 return FALSE; /* don't keep the idle */
1335 test_overflow (void)
1340 GSocketConnection *socket_connection;
1341 GDBusConnection *producer, *consumer;
1344 volatile gint n_messages_received;
1345 volatile gint n_messages_sent;
1347 g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
1350 socket = g_socket_new_from_fd (sv[0], &error);
1351 g_assert_no_error (error);
1352 socket_connection = g_socket_connection_factory_create_connection (socket);
1353 g_assert (socket_connection != NULL);
1354 g_object_unref (socket);
1355 producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1357 G_DBUS_CONNECTION_FLAGS_NONE,
1358 NULL, /* GDBusAuthObserver */
1359 NULL, /* GCancellable */
1361 g_dbus_connection_set_exit_on_close (producer, TRUE);
1362 g_assert_no_error (error);
1363 g_object_unref (socket_connection);
1364 n_messages_sent = 0;
1365 g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
1367 /* send enough data that we get an EAGAIN */
1368 for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
1371 g_dbus_connection_emit_signal (producer,
1372 NULL, /* destination */
1374 "org.foo.Interface",
1376 g_variant_new ("(s)", "a string"),
1378 g_assert_no_error (error);
1381 /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
1382 * kernel buffers) and verify that n_messages_sent <
1383 * OVERFLOW_NUM_SIGNALS
1385 * This is to verify that not all the submitted messages have been
1386 * sent to the underlying transport.
1388 g_timeout_add (500, overflow_on_500ms_later_func, NULL);
1389 g_main_loop_run (loop);
1390 g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
1392 /* now suck it all out as a client, and add it up */
1393 socket = g_socket_new_from_fd (sv[1], &error);
1394 g_assert_no_error (error);
1395 socket_connection = g_socket_connection_factory_create_connection (socket);
1396 g_assert (socket_connection != NULL);
1397 g_object_unref (socket);
1398 consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1400 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
1401 NULL, /* GDBusAuthObserver */
1402 NULL, /* GCancellable */
1404 g_assert_no_error (error);
1405 g_object_unref (socket_connection);
1406 n_messages_received = 0;
1407 g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
1408 g_dbus_connection_start_message_processing (consumer);
1410 timer = g_timer_new ();
1411 g_timer_start (timer);
1413 while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
1414 g_main_context_iteration (NULL, FALSE);
1416 g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
1417 g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
1419 g_timer_destroy (timer);
1420 g_object_unref (consumer);
1421 g_object_unref (producer);
1425 test_overflow (void)
1427 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
1431 /* ---------------------------------------------------------------------------------------------------- */
1438 GDBusNodeInfo *introspection_data = NULL;
1441 g_thread_init (NULL);
1442 g_test_init (&argc, &argv, NULL);
1444 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1445 g_assert (introspection_data != NULL);
1446 test_interface_introspection_data = introspection_data->interfaces[0];
1448 test_guid = g_dbus_generate_guid ();
1450 /* all the tests rely on a shared main loop */
1451 loop = g_main_loop_new (NULL, FALSE);
1453 g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1454 g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1455 g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1456 g_test_add_func ("/gdbus/credentials", test_credentials);
1457 g_test_add_func ("/gdbus/overflow", test_overflow);
1461 g_main_loop_unref (loop);
1463 g_dbus_node_info_unref (introspection_data);