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 (invocation);
186 g_dbus_connection_send_message (connection,
188 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
189 NULL, /* out_serial */
191 g_assert_no_error (error);
192 g_object_unref (reply);
194 g_dbus_method_invocation_return_dbus_error (invocation,
195 "org.gtk.GDBus.NotOnUnix",
196 "Your OS does not support file descriptor passing");
201 g_assert_not_reached ();
206 test_interface_get_property (GDBusConnection *connection,
208 const gchar *object_path,
209 const gchar *interface_name,
210 const gchar *property_name,
214 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
215 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
216 g_assert_cmpstr (property_name, ==, "PeerProperty");
218 return g_variant_new_string ("ThePropertyValue");
222 static const GDBusInterfaceVTable test_interface_vtable =
224 test_interface_method_call,
225 test_interface_get_property,
226 NULL /* set_property */
230 on_proxy_signal_received (GDBusProxy *proxy,
233 GVariant *parameters,
236 PeerData *data = user_data;
238 data->signal_received = TRUE;
240 g_assert (sender_name == NULL);
241 g_assert_cmpstr (signal_name, ==, "PeerSignal");
242 g_main_loop_quit (loop);
246 on_proxy_signal_received_with_name_set (GDBusProxy *proxy,
249 GVariant *parameters,
252 PeerData *data = user_data;
254 data->signal_received = TRUE;
256 g_assert_cmpstr (sender_name, ==, ":1.42");
257 g_assert_cmpstr (signal_name, ==, "PeerSignalWithNameSet");
258 g_main_loop_quit (loop);
261 /* ---------------------------------------------------------------------------------------------------- */
264 on_authorize_authenticated_peer (GDBusAuthObserver *observer,
266 GCredentials *credentials,
269 PeerData *data = user_data;
272 data->num_connection_attempts++;
275 if (!data->accept_connection)
278 g_main_loop_quit (loop);
284 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
286 on_new_connection (GDBusServer *server,
287 GDBusConnection *connection,
290 PeerData *data = user_data;
294 //g_print ("Client connected.\n"
295 // "Negotiated capabilities: unix-fd-passing=%d\n",
296 // g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
298 g_ptr_array_add (data->current_connections, g_object_ref (connection));
300 /* export object on the newly established connection */
302 reg_id = g_dbus_connection_register_object (connection,
303 "/org/gtk/GDBus/PeerTestObject",
304 test_interface_introspection_data,
305 &test_interface_vtable,
307 NULL, /* GDestroyNotify for data */
309 g_assert_no_error (error);
310 g_assert (reg_id > 0);
312 g_main_loop_quit (loop);
316 service_thread_func (gpointer user_data)
318 PeerData *data = user_data;
319 GMainContext *service_context;
320 GDBusAuthObserver *observer;
323 service_context = g_main_context_new ();
324 g_main_context_push_thread_default (service_context);
327 observer = g_dbus_auth_observer_new ();
328 server = g_dbus_server_new_sync (is_unix ? "unix:tmpdir=/tmp/gdbus-test-" : "nonce-tcp:",
329 G_DBUS_SERVER_FLAGS_NONE,
332 NULL, /* cancellable */
334 g_assert_no_error (error);
336 g_signal_connect (server,
338 G_CALLBACK (on_new_connection),
340 g_signal_connect (observer,
341 "authorize-authenticated-peer",
342 G_CALLBACK (on_authorize_authenticated_peer),
344 g_object_unref (observer);
346 g_dbus_server_start (server);
348 service_loop = g_main_loop_new (service_context, FALSE);
349 g_main_loop_run (service_loop);
351 g_main_context_pop_thread_default (service_context);
353 g_main_loop_unref (service_loop);
354 g_main_context_unref (service_context);
356 /* test code specifically unrefs the server - see below */
357 g_assert (server == NULL);
364 on_incoming_connection (GSocketService *service,
365 GSocketConnection *socket_connection,
366 GObject *source_object,
369 PeerData *data = user_data;
371 if (data->accept_connection)
375 GDBusConnection *connection;
378 connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
380 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
381 NULL, /* cancellable */
383 g_assert_no_error (error);
385 g_ptr_array_add (data->current_connections, connection);
387 /* export object on the newly established connection */
389 reg_id = g_dbus_connection_register_object (connection,
390 "/org/gtk/GDBus/PeerTestObject",
391 &test_interface_introspection_data,
392 &test_interface_vtable,
394 NULL, /* GDestroyNotify for data */
396 g_assert_no_error (error);
397 g_assert (reg_id > 0);
402 /* don't do anything */
405 data->num_connection_attempts++;
407 g_main_loop_quit (loop);
409 /* stops other signal handlers from being invoked */
414 service_thread_func (gpointer data)
416 GMainContext *service_context;
418 GSocketAddress *address;
421 service_context = g_main_context_new ();
422 g_main_context_push_thread_default (service_context);
424 socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
425 address = g_unix_socket_address_new (socket_path);
427 service = g_socket_service_new ();
429 g_socket_listener_add_address (G_SOCKET_LISTENER (service),
431 G_SOCKET_TYPE_STREAM,
432 G_SOCKET_PROTOCOL_DEFAULT,
433 NULL, /* source_object */
434 NULL, /* effective_address */
436 g_assert_no_error (error);
437 g_signal_connect (service,
439 G_CALLBACK (on_incoming_connection),
441 g_socket_service_start (service);
443 service_loop = g_main_loop_new (service_context, FALSE);
444 g_main_loop_run (service_loop);
446 g_main_context_pop_thread_default (service_context);
448 g_main_loop_unref (service_loop);
449 g_main_context_unref (service_context);
451 g_object_unref (address);
452 g_free (socket_path);
457 /* ---------------------------------------------------------------------------------------------------- */
461 check_connection (gpointer user_data)
463 PeerData *data = user_data;
466 for (n = 0; n < data->current_connections->len; n++)
471 c = G_DBUS_CONNECTION (data->current_connections->pdata[n]);
472 stream = g_dbus_connection_get_stream (c);
474 g_debug ("In check_connection for %d: connection %p, stream %p", n, c, stream);
475 g_debug ("closed = %d", g_io_stream_is_closed (stream));
478 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
479 g_debug ("socket_closed = %d", g_socket_is_closed (socket));
480 g_debug ("socket_condition_check = %d", g_socket_condition_check (socket, G_IO_IN|G_IO_OUT|G_IO_ERR|G_IO_HUP));
486 num_read = g_input_stream_read (g_io_stream_get_input_stream (stream),
493 g_debug ("error: %s", error->message);
494 g_error_free (error);
498 g_debug ("no error, read %d bytes", (gint) num_read);
506 on_do_disconnect_in_idle (gpointer data)
508 GDBusConnection *c = G_DBUS_CONNECTION (data);
509 g_debug ("GDC %p has ref_count %d", c, G_OBJECT (c)->ref_count);
510 g_dbus_connection_disconnect (c);
518 read_all_from_fd (gint fd, gsize *out_len, GError **error)
524 str = g_string_new (NULL);
528 num_read = read (fd, buf, sizeof (buf));
531 if (errno == EAGAIN || errno == EWOULDBLOCK)
535 g_io_error_from_errno (errno),
536 "Failed reading %d bytes into offset %d: %s",
542 else if (num_read > 0)
544 g_string_append_len (str, buf, num_read);
546 else if (num_read == 0)
555 return g_string_free (str, FALSE);
560 g_string_free (str, TRUE);
576 GThread *service_thread;
577 gulong signal_handler_id;
579 memset (&data, '\0', sizeof (PeerData));
580 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
582 /* first try to connect when there is no server */
584 c = g_dbus_connection_new_for_address_sync (is_unix ? "unix:path=/tmp/gdbus-test-does-not-exist-pid" :
585 /* NOTE: Even if something is listening on port 12345 the connection
586 * will fail because the nonce file doesn't exist */
587 "nonce-tcp:host=localhost,port=12345,noncefile=this-does-not-exist-gdbus",
588 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
589 NULL, /* GDBusAuthObserver */
590 NULL, /* cancellable */
592 _g_assert_error_domain (error, G_IO_ERROR);
593 g_assert (!g_dbus_error_is_remote_error (error));
594 g_clear_error (&error);
595 g_assert (c == NULL);
597 /* bring up a server - we run the server in a different thread to avoid deadlocks */
599 service_thread = g_thread_create (service_thread_func,
603 while (service_loop == NULL)
605 g_assert (server != NULL);
607 /* bring up a connection and accept it */
608 data.accept_connection = TRUE;
610 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
611 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
612 NULL, /* GDBusAuthObserver */
613 NULL, /* cancellable */
615 g_assert_no_error (error);
616 g_assert (c != NULL);
617 while (data.current_connections->len < 1)
618 g_main_loop_run (loop);
619 g_assert_cmpint (data.current_connections->len, ==, 1);
620 g_assert_cmpint (data.num_connection_attempts, ==, 1);
621 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
622 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
624 /* check that we create a proxy, read properties, receive signals and invoke
625 * the HelloPeer() method. Since the server runs in another thread it's fine
626 * to use synchronous blocking API here.
629 proxy = g_dbus_proxy_new_sync (c,
630 G_DBUS_PROXY_FLAGS_NONE,
633 "/org/gtk/GDBus/PeerTestObject",
634 "org.gtk.GDBus.PeerTestInterface",
635 NULL, /* GCancellable */
637 g_assert_no_error (error);
638 g_assert (proxy != NULL);
640 value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
641 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
643 /* try invoking a method */
645 result = g_dbus_proxy_call_sync (proxy,
647 g_variant_new ("(s)", "Hey Peer!"),
648 G_DBUS_CALL_FLAGS_NONE,
650 NULL, /* GCancellable */
652 g_assert_no_error (error);
653 g_variant_get (result, "(&s)", &s);
654 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
655 g_variant_unref (result);
656 g_assert_cmpint (data.num_method_calls, ==, 1);
658 /* make the other peer emit a signal - catch it */
659 signal_handler_id = g_signal_connect (proxy,
661 G_CALLBACK (on_proxy_signal_received),
663 g_assert (!data.signal_received);
664 g_dbus_proxy_call (proxy,
666 NULL, /* no arguments */
667 G_DBUS_CALL_FLAGS_NONE,
669 NULL, /* GCancellable */
670 NULL, /* GAsyncReadyCallback - we don't care about the result */
671 NULL); /* user_data */
672 g_main_loop_run (loop);
673 g_assert (data.signal_received);
674 g_assert_cmpint (data.num_method_calls, ==, 2);
675 g_signal_handler_disconnect (proxy, signal_handler_id);
677 /* Also ensure that messages with the sender header-field set gets
678 * delivered to the proxy - note that this doesn't really make sense
679 * e.g. names are meaning-less in a peer-to-peer case... but we
680 * support it because it makes sense in certain bridging
681 * applications - see e.g. #623815.
683 signal_handler_id = g_signal_connect (proxy,
685 G_CALLBACK (on_proxy_signal_received_with_name_set),
687 data.signal_received = FALSE;
688 g_dbus_proxy_call (proxy,
689 "EmitSignalWithNameSet",
690 NULL, /* no arguments */
691 G_DBUS_CALL_FLAGS_NONE,
693 NULL, /* GCancellable */
694 NULL, /* GAsyncReadyCallback - we don't care about the result */
695 NULL); /* user_data */
696 g_main_loop_run (loop);
697 g_assert (data.signal_received);
698 g_assert_cmpint (data.num_method_calls, ==, 3);
699 g_signal_handler_disconnect (proxy, signal_handler_id);
701 /* check for UNIX fd passing */
704 GDBusMessage *method_call_message;
705 GDBusMessage *method_reply_message;
706 GUnixFDList *fd_list;
713 method_call_message = g_dbus_message_new_method_call (NULL, /* name */
714 "/org/gtk/GDBus/PeerTestObject",
715 "org.gtk.GDBus.PeerTestInterface",
717 g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", "/etc/hosts"));
719 method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
721 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
723 NULL, /* out_serial */
724 NULL, /* cancellable */
726 g_assert_no_error (error);
727 g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
728 fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
729 g_assert (fd_list != NULL);
730 g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
732 fd = g_unix_fd_list_get (fd_list, 0, &error);
733 g_assert_no_error (error);
734 g_object_unref (method_call_message);
735 g_object_unref (method_reply_message);
738 buf = read_all_from_fd (fd, &len, &error);
739 g_assert_no_error (error);
740 g_assert (buf != NULL);
744 g_file_get_contents ("/etc/hosts",
748 g_assert_no_error (error);
749 g_assert_cmpint (len, ==, len2);
750 g_assert (memcmp (buf, buf2, len) == 0);
756 result = g_dbus_proxy_call_sync (proxy,
758 g_variant_new ("(s)", "boo"),
759 G_DBUS_CALL_FLAGS_NONE,
761 NULL, /* GCancellable */
763 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
764 g_assert (result == NULL);
765 g_error_free (error);
766 #endif /* G_OS_UNIX */
768 /* Check that g_socket_get_credentials() work - this really should
769 * be in a GSocket-specific test suite but no such test suite exists
774 GCredentials *credentials;
775 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (g_dbus_connection_get_stream (c)));
776 g_assert (G_IS_SOCKET (socket));
778 credentials = g_socket_get_credentials (socket, &error);
781 struct ucred *native_creds;
782 g_assert_no_error (error);
783 g_assert (G_IS_CREDENTIALS (credentials));
784 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED);
785 g_assert (native_creds != NULL);
786 g_assert (native_creds->uid == getuid ());
787 g_assert (native_creds->gid == getgid ());
788 g_assert (native_creds->pid == getpid ());
790 g_object_unref (credentials);
792 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
793 g_assert (credentials == NULL);
798 /* bring up a connection - don't accept it - this should fail
800 data.accept_connection = FALSE;
802 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
803 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
804 NULL, /* GDBusAuthObserver */
805 NULL, /* cancellable */
807 _g_assert_error_domain (error, G_IO_ERROR);
808 g_assert (c2 == NULL);
811 /* TODO: THIS TEST DOESN'T WORK YET */
813 /* bring up a connection - accept it.. then disconnect from the client side - check
814 * that the server side gets the disconnect signal.
817 data.accept_connection = TRUE;
818 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
819 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
820 NULL, /* GDBusAuthObserver */
821 NULL, /* cancellable */
823 g_assert_no_error (error);
824 g_assert (c2 != NULL);
825 g_assert (!g_dbus_connection_get_is_disconnected (c2));
826 while (data.num_connection_attempts < 3)
827 g_main_loop_run (loop);
828 g_assert_cmpint (data.current_connections->len, ==, 2);
829 g_assert_cmpint (data.num_connection_attempts, ==, 3);
830 g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
831 g_idle_add (on_do_disconnect_in_idle, c2);
832 g_debug ("==================================================");
833 g_debug ("==================================================");
834 g_debug ("==================================================");
835 g_debug ("waiting for disconnect on connection %p, stream %p",
836 data.current_connections->pdata[1],
837 g_dbus_connection_get_stream (data.current_connections->pdata[1]));
839 g_timeout_add (2000, check_connection, &data);
840 //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
841 g_main_loop_run (loop);
842 g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
843 g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
846 /* unref the server and stop listening for new connections
848 * This won't bring down the established connections - check that c is still connected
849 * by invoking a method
851 //g_socket_service_stop (service);
852 //g_object_unref (service);
853 g_dbus_server_stop (server);
854 g_object_unref (server);
858 result = g_dbus_proxy_call_sync (proxy,
860 g_variant_new ("(s)", "Hey Again Peer!"),
861 G_DBUS_CALL_FLAGS_NONE,
863 NULL, /* GCancellable */
865 g_assert_no_error (error);
866 g_variant_get (result, "(&s)", &s);
867 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
868 g_variant_unref (result);
869 g_assert_cmpint (data.num_method_calls, ==, 5);
872 /* TODO: THIS TEST DOESN'T WORK YET */
874 /* now disconnect from the server side - check that the client side gets the signal */
875 g_assert_cmpint (data.current_connections->len, ==, 1);
876 g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
877 g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
878 if (!g_dbus_connection_get_is_disconnected (c))
879 _g_assert_signal_received (c, "closed");
880 g_assert (g_dbus_connection_get_is_disconnected (c));
884 g_ptr_array_unref (data.current_connections);
885 g_object_unref (proxy);
887 g_main_loop_quit (service_loop);
888 g_thread_join (service_thread);
891 /* ---------------------------------------------------------------------------------------------------- */
896 GMainContext *context;
903 dmp_data_free (DmpData *data)
905 g_main_loop_unref (data->loop);
906 g_main_context_unref (data->context);
907 g_object_unref (data->server);
908 g_list_foreach (data->connections, (GFunc) g_object_unref, NULL);
909 g_list_free (data->connections);
914 dmp_on_method_call (GDBusConnection *connection,
916 const gchar *object_path,
917 const gchar *interface_name,
918 const gchar *method_name,
919 GVariant *parameters,
920 GDBusMethodInvocation *invocation,
923 //DmpData *data = user_data;
926 g_variant_get (parameters,
930 g_dbus_method_invocation_return_value (invocation,
931 g_variant_new ("(i)", first + second));
934 static const GDBusInterfaceVTable dmp_interface_vtable =
937 NULL, /* get_property */
938 NULL /* set_property */
942 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
944 dmp_on_new_connection (GDBusServer *server,
945 GDBusConnection *connection,
948 DmpData *data = user_data;
952 /* accept the connection */
953 data->connections = g_list_prepend (data->connections, g_object_ref (connection));
956 node = g_dbus_node_info_new_for_xml ("<node>"
957 " <interface name='org.gtk.GDBus.DmpInterface'>"
958 " <method name='AddPair'>"
959 " <arg type='i' name='first' direction='in'/>"
960 " <arg type='i' name='second' direction='in'/>"
961 " <arg type='i' name='sum' direction='out'/>"
966 g_assert_no_error (error);
968 /* sleep 100ms before exporting an object - this is to test that
969 * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
970 * (GDBusServer uses this feature).
974 /* export an object */
976 g_dbus_connection_register_object (connection,
979 &dmp_interface_vtable,
983 g_dbus_node_info_unref (node);
987 dmp_thread_func (gpointer user_data)
989 DmpData *data = user_data;
993 data->context = g_main_context_new ();
994 g_main_context_push_thread_default (data->context);
997 guid = g_dbus_generate_guid ();
998 data->server = g_dbus_server_new_sync ("unix:tmpdir=/tmp/gdbus-test-",
999 G_DBUS_SERVER_FLAGS_NONE,
1001 NULL, /* GDBusAuthObserver */
1002 NULL, /* GCancellable */
1004 g_assert_no_error (error);
1005 g_signal_connect (data->server,
1007 G_CALLBACK (dmp_on_new_connection),
1010 g_dbus_server_start (data->server);
1012 data->loop = g_main_loop_new (data->context, FALSE);
1013 g_main_loop_run (data->loop);
1015 g_main_context_pop_thread_default (data->context);
1022 delayed_message_processing (void)
1026 GThread *service_thread;
1029 data = g_new0 (DmpData, 1);
1032 service_thread = g_thread_create (dmp_thread_func,
1036 while (data->server == NULL || !g_dbus_server_is_active (data->server))
1039 for (n = 0; n < 5; n++)
1046 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
1047 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1048 NULL, /* GDBusAuthObserver */
1049 NULL, /* GCancellable */
1051 g_assert_no_error (error);
1054 res = g_dbus_connection_call_sync (c,
1055 NULL, /* bus name */
1057 "org.gtk.GDBus.DmpInterface",
1059 g_variant_new ("(ii)", 2, n),
1060 G_VARIANT_TYPE ("(i)"),
1061 G_DBUS_CALL_FLAGS_NONE,
1062 -1, /* timeout_msec */
1063 NULL, /* GCancellable */
1065 g_assert_no_error (error);
1066 g_variant_get (res, "(i)", &val);
1067 g_assert_cmpint (val, ==, 2 + n);
1068 g_variant_unref (res);
1072 g_main_loop_quit (data->loop);
1073 g_thread_join (service_thread);
1074 dmp_data_free (data);
1077 /* ---------------------------------------------------------------------------------------------------- */
1080 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1082 GCredentials *credentials,
1085 PeerData *data = user_data;
1086 gboolean authorized;
1088 data->num_connection_attempts++;
1091 if (!data->accept_connection)
1094 g_main_loop_quit (loop);
1100 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1102 nonce_tcp_on_new_connection (GDBusServer *server,
1103 GDBusConnection *connection,
1106 PeerData *data = user_data;
1108 g_ptr_array_add (data->current_connections, g_object_ref (connection));
1110 g_main_loop_quit (loop);
1114 nonce_tcp_service_thread_func (gpointer user_data)
1116 PeerData *data = user_data;
1117 GMainContext *service_context;
1118 GDBusAuthObserver *observer;
1121 service_context = g_main_context_new ();
1122 g_main_context_push_thread_default (service_context);
1125 observer = g_dbus_auth_observer_new ();
1126 server = g_dbus_server_new_sync ("nonce-tcp:",
1127 G_DBUS_SERVER_FLAGS_NONE,
1130 NULL, /* cancellable */
1132 g_assert_no_error (error);
1134 g_signal_connect (server,
1136 G_CALLBACK (nonce_tcp_on_new_connection),
1138 g_signal_connect (observer,
1139 "authorize-authenticated-peer",
1140 G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1142 g_object_unref (observer);
1144 g_dbus_server_start (server);
1146 service_loop = g_main_loop_new (service_context, FALSE);
1147 g_main_loop_run (service_loop);
1149 g_main_context_pop_thread_default (service_context);
1151 g_main_loop_unref (service_loop);
1152 g_main_context_unref (service_context);
1154 /* test code specifically unrefs the server - see below */
1155 g_assert (server == NULL);
1161 test_nonce_tcp (void)
1165 GThread *service_thread;
1170 const gchar *address;
1172 memset (&data, '\0', sizeof (PeerData));
1173 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1177 service_loop = NULL;
1178 service_thread = g_thread_create (nonce_tcp_service_thread_func,
1182 while (service_loop == NULL)
1184 g_assert (server != NULL);
1187 /* bring up a connection and accept it */
1188 data.accept_connection = TRUE;
1190 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1191 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1192 NULL, /* GDBusAuthObserver */
1193 NULL, /* cancellable */
1195 g_assert_no_error (error);
1196 g_assert (c != NULL);
1197 while (data.current_connections->len < 1)
1198 g_main_loop_run (loop);
1199 g_assert_cmpint (data.current_connections->len, ==, 1);
1200 g_assert_cmpint (data.num_connection_attempts, ==, 1);
1201 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1202 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1205 /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1208 address = g_dbus_server_get_client_address (server);
1210 s = strstr (address, "noncefile=");
1211 g_assert (s != NULL);
1212 s += sizeof "noncefile=" - 1;
1213 nonce_file = g_strdup (s);
1215 /* First try invalid data in the nonce file - this will actually
1216 * make the client send this and the server will reject it. The way
1217 * it works is that if the nonce doesn't match, the server will
1218 * simply close the connection. So, from the client point of view,
1219 * we can see a variety of errors.
1222 res = g_file_set_contents (nonce_file,
1226 g_assert_no_error (error);
1228 c = g_dbus_connection_new_for_address_sync (address,
1229 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1230 NULL, /* GDBusAuthObserver */
1231 NULL, /* cancellable */
1233 _g_assert_error_domain (error, G_IO_ERROR);
1234 g_assert (c == NULL);
1236 /* Then try with a nonce-file of incorrect length - this will make
1237 * the client complain - we won't even try connecting to the server
1241 res = g_file_set_contents (nonce_file,
1242 "0123456789012345_",
1245 g_assert_no_error (error);
1247 c = g_dbus_connection_new_for_address_sync (address,
1248 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1249 NULL, /* GDBusAuthObserver */
1250 NULL, /* cancellable */
1252 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1253 g_assert (c == NULL);
1255 /* Finally try with no nonce-file at all */
1256 g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1258 c = g_dbus_connection_new_for_address_sync (address,
1259 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1260 NULL, /* GDBusAuthObserver */
1261 NULL, /* cancellable */
1263 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1264 g_assert (c == NULL);
1266 g_free (nonce_file);
1268 g_dbus_server_stop (server);
1269 g_object_unref (server);
1272 g_main_loop_quit (service_loop);
1273 g_thread_join (service_thread);
1277 test_credentials (void)
1279 GCredentials *c1, *c2;
1283 c1 = g_credentials_new ();
1284 c2 = g_credentials_new ();
1287 if (g_credentials_set_unix_user (c2, getuid (), &error))
1288 g_assert_no_error (error);
1290 g_clear_error (&error);
1291 g_assert (g_credentials_is_same_user (c1, c2, &error));
1292 g_assert_no_error (error);
1294 desc = g_credentials_to_string (c1);
1295 g_assert (desc != NULL);
1298 g_object_unref (c1);
1299 g_object_unref (c2);
1302 /* ---------------------------------------------------------------------------------------------------- */
1306 /* Chosen to be big enough to overflow the socket buffer */
1307 #define OVERFLOW_NUM_SIGNALS 5000
1308 #define OVERFLOW_TIMEOUT_SEC 10
1310 static GDBusMessageFilterResult
1311 overflow_filter_func (GDBusConnection *connection,
1312 GDBusMessage *message,
1316 volatile gint *counter = user_data;
1318 return G_DBUS_MESSAGE_FILTER_RESULT_NO_EFFECT;
1322 overflow_on_500ms_later_func (gpointer user_data)
1324 g_main_loop_quit (loop);
1325 return FALSE; /* don't keep the idle */
1329 test_overflow (void)
1334 GSocketConnection *socket_connection;
1335 GDBusConnection *producer, *consumer;
1338 volatile gint n_messages_received;
1339 volatile gint n_messages_sent;
1341 g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
1344 socket = g_socket_new_from_fd (sv[0], &error);
1345 g_assert_no_error (error);
1346 socket_connection = g_socket_connection_factory_create_connection (socket);
1347 g_assert (socket_connection != NULL);
1348 g_object_unref (socket);
1349 producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1351 G_DBUS_CONNECTION_FLAGS_NONE,
1352 NULL, /* GDBusAuthObserver */
1353 NULL, /* GCancellable */
1355 g_dbus_connection_set_exit_on_close (producer, TRUE);
1356 g_assert_no_error (error);
1357 g_object_unref (socket_connection);
1358 n_messages_sent = 0;
1359 g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
1361 /* send enough data that we get an EAGAIN */
1362 for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
1365 g_dbus_connection_emit_signal (producer,
1366 NULL, /* destination */
1368 "org.foo.Interface",
1370 g_variant_new ("(s)", "a string"),
1372 g_assert_no_error (error);
1375 /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
1376 * kernel buffers) and verify that n_messages_sent <
1377 * OVERFLOW_NUM_SIGNALS
1379 * This is to verify that not all the submitted messages have been
1380 * sent to the underlying transport.
1382 g_timeout_add (500, overflow_on_500ms_later_func, NULL);
1383 g_main_loop_run (loop);
1384 g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
1386 /* now suck it all out as a client, and add it up */
1387 socket = g_socket_new_from_fd (sv[1], &error);
1388 g_assert_no_error (error);
1389 socket_connection = g_socket_connection_factory_create_connection (socket);
1390 g_assert (socket_connection != NULL);
1391 g_object_unref (socket);
1392 consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1394 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
1395 NULL, /* GDBusAuthObserver */
1396 NULL, /* GCancellable */
1398 g_assert_no_error (error);
1399 g_object_unref (socket_connection);
1400 n_messages_received = 0;
1401 g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
1402 g_dbus_connection_start_message_processing (consumer);
1404 timer = g_timer_new ();
1405 g_timer_start (timer);
1407 while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
1408 g_main_context_iteration (NULL, FALSE);
1410 g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
1411 g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
1413 g_timer_destroy (timer);
1414 g_object_unref (consumer);
1415 g_object_unref (producer);
1419 test_overflow (void)
1421 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
1425 /* ---------------------------------------------------------------------------------------------------- */
1432 GDBusNodeInfo *introspection_data = NULL;
1435 g_thread_init (NULL);
1436 g_test_init (&argc, &argv, NULL);
1438 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1439 g_assert (introspection_data != NULL);
1440 test_interface_introspection_data = introspection_data->interfaces[0];
1442 test_guid = g_dbus_generate_guid ();
1444 /* all the tests rely on a shared main loop */
1445 loop = g_main_loop_new (NULL, FALSE);
1447 g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1448 g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1449 g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1450 g_test_add_func ("/gdbus/credentials", test_credentials);
1451 g_test_add_func ("/gdbus/overflow", test_overflow);
1455 g_main_loop_unref (loop);
1457 g_dbus_node_info_unref (introspection_data);