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);
319 service_thread_func (gpointer user_data)
321 PeerData *data = user_data;
322 GMainContext *service_context;
323 GDBusAuthObserver *observer;
326 service_context = g_main_context_new ();
327 g_main_context_push_thread_default (service_context);
330 observer = g_dbus_auth_observer_new ();
331 server = g_dbus_server_new_sync (is_unix ? "unix:tmpdir=/tmp/gdbus-test-" : "nonce-tcp:",
332 G_DBUS_SERVER_FLAGS_NONE,
335 NULL, /* cancellable */
337 g_assert_no_error (error);
339 g_signal_connect (server,
341 G_CALLBACK (on_new_connection),
343 g_signal_connect (observer,
344 "authorize-authenticated-peer",
345 G_CALLBACK (on_authorize_authenticated_peer),
347 g_object_unref (observer);
349 g_dbus_server_start (server);
351 service_loop = g_main_loop_new (service_context, FALSE);
352 g_main_loop_run (service_loop);
354 g_main_context_pop_thread_default (service_context);
356 g_main_loop_unref (service_loop);
357 g_main_context_unref (service_context);
359 /* test code specifically unrefs the server - see below */
360 g_assert (server == NULL);
367 on_incoming_connection (GSocketService *service,
368 GSocketConnection *socket_connection,
369 GObject *source_object,
372 PeerData *data = user_data;
374 if (data->accept_connection)
378 GDBusConnection *connection;
381 connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
383 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
384 NULL, /* cancellable */
386 g_assert_no_error (error);
388 g_ptr_array_add (data->current_connections, connection);
390 /* export object on the newly established connection */
392 reg_id = g_dbus_connection_register_object (connection,
393 "/org/gtk/GDBus/PeerTestObject",
394 &test_interface_introspection_data,
395 &test_interface_vtable,
397 NULL, /* GDestroyNotify for data */
399 g_assert_no_error (error);
400 g_assert (reg_id > 0);
405 /* don't do anything */
408 data->num_connection_attempts++;
410 g_main_loop_quit (loop);
412 /* stops other signal handlers from being invoked */
417 service_thread_func (gpointer data)
419 GMainContext *service_context;
421 GSocketAddress *address;
424 service_context = g_main_context_new ();
425 g_main_context_push_thread_default (service_context);
427 socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
428 address = g_unix_socket_address_new (socket_path);
430 service = g_socket_service_new ();
432 g_socket_listener_add_address (G_SOCKET_LISTENER (service),
434 G_SOCKET_TYPE_STREAM,
435 G_SOCKET_PROTOCOL_DEFAULT,
436 NULL, /* source_object */
437 NULL, /* effective_address */
439 g_assert_no_error (error);
440 g_signal_connect (service,
442 G_CALLBACK (on_incoming_connection),
444 g_socket_service_start (service);
446 service_loop = g_main_loop_new (service_context, FALSE);
447 g_main_loop_run (service_loop);
449 g_main_context_pop_thread_default (service_context);
451 g_main_loop_unref (service_loop);
452 g_main_context_unref (service_context);
454 g_object_unref (address);
455 g_free (socket_path);
460 /* ---------------------------------------------------------------------------------------------------- */
464 check_connection (gpointer user_data)
466 PeerData *data = user_data;
469 for (n = 0; n < data->current_connections->len; n++)
474 c = G_DBUS_CONNECTION (data->current_connections->pdata[n]);
475 stream = g_dbus_connection_get_stream (c);
477 g_debug ("In check_connection for %d: connection %p, stream %p", n, c, stream);
478 g_debug ("closed = %d", g_io_stream_is_closed (stream));
481 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
482 g_debug ("socket_closed = %d", g_socket_is_closed (socket));
483 g_debug ("socket_condition_check = %d", g_socket_condition_check (socket, G_IO_IN|G_IO_OUT|G_IO_ERR|G_IO_HUP));
489 num_read = g_input_stream_read (g_io_stream_get_input_stream (stream),
496 g_debug ("error: %s", error->message);
497 g_error_free (error);
501 g_debug ("no error, read %d bytes", (gint) num_read);
509 on_do_disconnect_in_idle (gpointer data)
511 GDBusConnection *c = G_DBUS_CONNECTION (data);
512 g_debug ("GDC %p has ref_count %d", c, G_OBJECT (c)->ref_count);
513 g_dbus_connection_disconnect (c);
521 read_all_from_fd (gint fd, gsize *out_len, GError **error)
527 str = g_string_new (NULL);
531 num_read = read (fd, buf, sizeof (buf));
534 if (errno == EAGAIN || errno == EWOULDBLOCK)
538 g_io_error_from_errno (errno),
539 "Failed reading %d bytes into offset %d: %s",
545 else if (num_read > 0)
547 g_string_append_len (str, buf, num_read);
549 else if (num_read == 0)
558 return g_string_free (str, FALSE);
563 g_string_free (str, TRUE);
579 GThread *service_thread;
580 gulong signal_handler_id;
582 memset (&data, '\0', sizeof (PeerData));
583 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
585 /* first try to connect when there is no server */
587 c = g_dbus_connection_new_for_address_sync (is_unix ? "unix:path=/tmp/gdbus-test-does-not-exist-pid" :
588 /* NOTE: Even if something is listening on port 12345 the connection
589 * will fail because the nonce file doesn't exist */
590 "nonce-tcp:host=localhost,port=12345,noncefile=this-does-not-exist-gdbus",
591 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
592 NULL, /* GDBusAuthObserver */
593 NULL, /* cancellable */
595 _g_assert_error_domain (error, G_IO_ERROR);
596 g_assert (!g_dbus_error_is_remote_error (error));
597 g_clear_error (&error);
598 g_assert (c == NULL);
600 /* bring up a server - we run the server in a different thread to avoid deadlocks */
602 service_thread = g_thread_create (service_thread_func,
606 while (service_loop == NULL)
608 g_assert (server != NULL);
610 /* bring up a connection and accept it */
611 data.accept_connection = TRUE;
613 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
614 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
615 NULL, /* GDBusAuthObserver */
616 NULL, /* cancellable */
618 g_assert_no_error (error);
619 g_assert (c != NULL);
620 while (data.current_connections->len < 1)
621 g_main_loop_run (loop);
622 g_assert_cmpint (data.current_connections->len, ==, 1);
623 g_assert_cmpint (data.num_connection_attempts, ==, 1);
624 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
625 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
627 /* check that we create a proxy, read properties, receive signals and invoke
628 * the HelloPeer() method. Since the server runs in another thread it's fine
629 * to use synchronous blocking API here.
632 proxy = g_dbus_proxy_new_sync (c,
633 G_DBUS_PROXY_FLAGS_NONE,
636 "/org/gtk/GDBus/PeerTestObject",
637 "org.gtk.GDBus.PeerTestInterface",
638 NULL, /* GCancellable */
640 g_assert_no_error (error);
641 g_assert (proxy != NULL);
643 value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
644 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
646 /* try invoking a method */
648 result = g_dbus_proxy_call_sync (proxy,
650 g_variant_new ("(s)", "Hey Peer!"),
651 G_DBUS_CALL_FLAGS_NONE,
653 NULL, /* GCancellable */
655 g_assert_no_error (error);
656 g_variant_get (result, "(&s)", &s);
657 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
658 g_variant_unref (result);
659 g_assert_cmpint (data.num_method_calls, ==, 1);
661 /* make the other peer emit a signal - catch it */
662 signal_handler_id = g_signal_connect (proxy,
664 G_CALLBACK (on_proxy_signal_received),
666 g_assert (!data.signal_received);
667 g_dbus_proxy_call (proxy,
669 NULL, /* no arguments */
670 G_DBUS_CALL_FLAGS_NONE,
672 NULL, /* GCancellable */
673 NULL, /* GAsyncReadyCallback - we don't care about the result */
674 NULL); /* user_data */
675 g_main_loop_run (loop);
676 g_assert (data.signal_received);
677 g_assert_cmpint (data.num_method_calls, ==, 2);
678 g_signal_handler_disconnect (proxy, signal_handler_id);
680 /* Also ensure that messages with the sender header-field set gets
681 * delivered to the proxy - note that this doesn't really make sense
682 * e.g. names are meaning-less in a peer-to-peer case... but we
683 * support it because it makes sense in certain bridging
684 * applications - see e.g. #623815.
686 signal_handler_id = g_signal_connect (proxy,
688 G_CALLBACK (on_proxy_signal_received_with_name_set),
690 data.signal_received = FALSE;
691 g_dbus_proxy_call (proxy,
692 "EmitSignalWithNameSet",
693 NULL, /* no arguments */
694 G_DBUS_CALL_FLAGS_NONE,
696 NULL, /* GCancellable */
697 NULL, /* GAsyncReadyCallback - we don't care about the result */
698 NULL); /* user_data */
699 g_main_loop_run (loop);
700 g_assert (data.signal_received);
701 g_assert_cmpint (data.num_method_calls, ==, 3);
702 g_signal_handler_disconnect (proxy, signal_handler_id);
704 /* check for UNIX fd passing */
707 GDBusMessage *method_call_message;
708 GDBusMessage *method_reply_message;
709 GUnixFDList *fd_list;
716 method_call_message = g_dbus_message_new_method_call (NULL, /* name */
717 "/org/gtk/GDBus/PeerTestObject",
718 "org.gtk.GDBus.PeerTestInterface",
720 g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", "/etc/hosts"));
722 method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
724 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
726 NULL, /* out_serial */
727 NULL, /* cancellable */
729 g_assert_no_error (error);
730 g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
731 fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
732 g_assert (fd_list != NULL);
733 g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
735 fd = g_unix_fd_list_get (fd_list, 0, &error);
736 g_assert_no_error (error);
737 g_object_unref (method_call_message);
738 g_object_unref (method_reply_message);
742 buf = read_all_from_fd (fd, &len, &error);
743 g_assert_no_error (error);
744 g_assert (buf != NULL);
748 g_file_get_contents ("/etc/hosts",
752 g_assert_no_error (error);
753 g_assert_cmpint (len, ==, len2);
754 g_assert (memcmp (buf, buf2, len) == 0);
760 result = g_dbus_proxy_call_sync (proxy,
762 g_variant_new ("(s)", "boo"),
763 G_DBUS_CALL_FLAGS_NONE,
765 NULL, /* GCancellable */
767 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
768 g_assert (result == NULL);
769 g_error_free (error);
770 #endif /* G_OS_UNIX */
772 /* Check that g_socket_get_credentials() work - this really should
773 * be in a GSocket-specific test suite but no such test suite exists
778 GCredentials *credentials;
779 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (g_dbus_connection_get_stream (c)));
780 g_assert (G_IS_SOCKET (socket));
782 credentials = g_socket_get_credentials (socket, &error);
785 struct ucred *native_creds;
786 g_assert_no_error (error);
787 g_assert (G_IS_CREDENTIALS (credentials));
788 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED);
789 g_assert (native_creds != NULL);
790 g_assert (native_creds->uid == getuid ());
791 g_assert (native_creds->gid == getgid ());
792 g_assert (native_creds->pid == getpid ());
794 g_object_unref (credentials);
796 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
797 g_assert (credentials == NULL);
802 /* bring up a connection - don't accept it - this should fail
804 data.accept_connection = FALSE;
806 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
807 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
808 NULL, /* GDBusAuthObserver */
809 NULL, /* cancellable */
811 _g_assert_error_domain (error, G_IO_ERROR);
812 g_error_free (error);
813 g_assert (c2 == NULL);
816 /* TODO: THIS TEST DOESN'T WORK YET */
818 /* bring up a connection - accept it.. then disconnect from the client side - check
819 * that the server side gets the disconnect signal.
822 data.accept_connection = TRUE;
823 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
824 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
825 NULL, /* GDBusAuthObserver */
826 NULL, /* cancellable */
828 g_assert_no_error (error);
829 g_assert (c2 != NULL);
830 g_assert (!g_dbus_connection_get_is_disconnected (c2));
831 while (data.num_connection_attempts < 3)
832 g_main_loop_run (loop);
833 g_assert_cmpint (data.current_connections->len, ==, 2);
834 g_assert_cmpint (data.num_connection_attempts, ==, 3);
835 g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
836 g_idle_add (on_do_disconnect_in_idle, c2);
837 g_debug ("==================================================");
838 g_debug ("==================================================");
839 g_debug ("==================================================");
840 g_debug ("waiting for disconnect on connection %p, stream %p",
841 data.current_connections->pdata[1],
842 g_dbus_connection_get_stream (data.current_connections->pdata[1]));
844 g_timeout_add (2000, check_connection, &data);
845 //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
846 g_main_loop_run (loop);
847 g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
848 g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
851 /* unref the server and stop listening for new connections
853 * This won't bring down the established connections - check that c is still connected
854 * by invoking a method
856 //g_socket_service_stop (service);
857 //g_object_unref (service);
858 g_dbus_server_stop (server);
859 g_object_unref (server);
863 result = g_dbus_proxy_call_sync (proxy,
865 g_variant_new ("(s)", "Hey Again Peer!"),
866 G_DBUS_CALL_FLAGS_NONE,
868 NULL, /* GCancellable */
870 g_assert_no_error (error);
871 g_variant_get (result, "(&s)", &s);
872 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
873 g_variant_unref (result);
874 g_assert_cmpint (data.num_method_calls, ==, 5);
877 /* TODO: THIS TEST DOESN'T WORK YET */
879 /* now disconnect from the server side - check that the client side gets the signal */
880 g_assert_cmpint (data.current_connections->len, ==, 1);
881 g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
882 g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
883 if (!g_dbus_connection_get_is_disconnected (c))
884 _g_assert_signal_received (c, "closed");
885 g_assert (g_dbus_connection_get_is_disconnected (c));
889 g_ptr_array_unref (data.current_connections);
890 g_object_unref (proxy);
892 g_main_loop_quit (service_loop);
893 g_thread_join (service_thread);
896 /* ---------------------------------------------------------------------------------------------------- */
901 GMainContext *context;
908 dmp_data_free (DmpData *data)
910 g_main_loop_unref (data->loop);
911 g_main_context_unref (data->context);
912 g_object_unref (data->server);
913 g_list_foreach (data->connections, (GFunc) g_object_unref, NULL);
914 g_list_free (data->connections);
919 dmp_on_method_call (GDBusConnection *connection,
921 const gchar *object_path,
922 const gchar *interface_name,
923 const gchar *method_name,
924 GVariant *parameters,
925 GDBusMethodInvocation *invocation,
928 //DmpData *data = user_data;
931 g_variant_get (parameters,
935 g_dbus_method_invocation_return_value (invocation,
936 g_variant_new ("(i)", first + second));
939 static const GDBusInterfaceVTable dmp_interface_vtable =
942 NULL, /* get_property */
943 NULL /* set_property */
947 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
949 dmp_on_new_connection (GDBusServer *server,
950 GDBusConnection *connection,
953 DmpData *data = user_data;
957 /* accept the connection */
958 data->connections = g_list_prepend (data->connections, g_object_ref (connection));
961 node = g_dbus_node_info_new_for_xml ("<node>"
962 " <interface name='org.gtk.GDBus.DmpInterface'>"
963 " <method name='AddPair'>"
964 " <arg type='i' name='first' direction='in'/>"
965 " <arg type='i' name='second' direction='in'/>"
966 " <arg type='i' name='sum' direction='out'/>"
971 g_assert_no_error (error);
973 /* sleep 100ms before exporting an object - this is to test that
974 * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
975 * (GDBusServer uses this feature).
979 /* export an object */
981 g_dbus_connection_register_object (connection,
984 &dmp_interface_vtable,
988 g_dbus_node_info_unref (node);
994 dmp_thread_func (gpointer user_data)
996 DmpData *data = user_data;
1000 data->context = g_main_context_new ();
1001 g_main_context_push_thread_default (data->context);
1004 guid = g_dbus_generate_guid ();
1005 data->server = g_dbus_server_new_sync ("unix:tmpdir=/tmp/gdbus-test-",
1006 G_DBUS_SERVER_FLAGS_NONE,
1008 NULL, /* GDBusAuthObserver */
1009 NULL, /* GCancellable */
1011 g_assert_no_error (error);
1012 g_signal_connect (data->server,
1014 G_CALLBACK (dmp_on_new_connection),
1017 g_dbus_server_start (data->server);
1019 data->loop = g_main_loop_new (data->context, FALSE);
1020 g_main_loop_run (data->loop);
1022 g_main_context_pop_thread_default (data->context);
1029 delayed_message_processing (void)
1033 GThread *service_thread;
1036 data = g_new0 (DmpData, 1);
1039 service_thread = g_thread_create (dmp_thread_func,
1043 while (data->server == NULL || !g_dbus_server_is_active (data->server))
1046 for (n = 0; n < 5; n++)
1053 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
1054 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1055 NULL, /* GDBusAuthObserver */
1056 NULL, /* GCancellable */
1058 g_assert_no_error (error);
1061 res = g_dbus_connection_call_sync (c,
1062 NULL, /* bus name */
1064 "org.gtk.GDBus.DmpInterface",
1066 g_variant_new ("(ii)", 2, n),
1067 G_VARIANT_TYPE ("(i)"),
1068 G_DBUS_CALL_FLAGS_NONE,
1069 -1, /* timeout_msec */
1070 NULL, /* GCancellable */
1072 g_assert_no_error (error);
1073 g_variant_get (res, "(i)", &val);
1074 g_assert_cmpint (val, ==, 2 + n);
1075 g_variant_unref (res);
1079 g_main_loop_quit (data->loop);
1080 g_thread_join (service_thread);
1081 dmp_data_free (data);
1084 /* ---------------------------------------------------------------------------------------------------- */
1087 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1089 GCredentials *credentials,
1092 PeerData *data = user_data;
1093 gboolean authorized;
1095 data->num_connection_attempts++;
1098 if (!data->accept_connection)
1101 g_main_loop_quit (loop);
1107 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1109 nonce_tcp_on_new_connection (GDBusServer *server,
1110 GDBusConnection *connection,
1113 PeerData *data = user_data;
1115 g_ptr_array_add (data->current_connections, g_object_ref (connection));
1117 g_main_loop_quit (loop);
1123 nonce_tcp_service_thread_func (gpointer user_data)
1125 PeerData *data = user_data;
1126 GMainContext *service_context;
1127 GDBusAuthObserver *observer;
1130 service_context = g_main_context_new ();
1131 g_main_context_push_thread_default (service_context);
1134 observer = g_dbus_auth_observer_new ();
1135 server = g_dbus_server_new_sync ("nonce-tcp:",
1136 G_DBUS_SERVER_FLAGS_NONE,
1139 NULL, /* cancellable */
1141 g_assert_no_error (error);
1143 g_signal_connect (server,
1145 G_CALLBACK (nonce_tcp_on_new_connection),
1147 g_signal_connect (observer,
1148 "authorize-authenticated-peer",
1149 G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1151 g_object_unref (observer);
1153 g_dbus_server_start (server);
1155 service_loop = g_main_loop_new (service_context, FALSE);
1156 g_main_loop_run (service_loop);
1158 g_main_context_pop_thread_default (service_context);
1160 g_main_loop_unref (service_loop);
1161 g_main_context_unref (service_context);
1163 /* test code specifically unrefs the server - see below */
1164 g_assert (server == NULL);
1170 test_nonce_tcp (void)
1174 GThread *service_thread;
1179 const gchar *address;
1181 memset (&data, '\0', sizeof (PeerData));
1182 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1186 service_loop = NULL;
1187 service_thread = g_thread_create (nonce_tcp_service_thread_func,
1191 while (service_loop == NULL)
1193 g_assert (server != NULL);
1196 /* bring up a connection and accept it */
1197 data.accept_connection = TRUE;
1199 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1200 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1201 NULL, /* GDBusAuthObserver */
1202 NULL, /* cancellable */
1204 g_assert_no_error (error);
1205 g_assert (c != NULL);
1206 while (data.current_connections->len < 1)
1207 g_main_loop_run (loop);
1208 g_assert_cmpint (data.current_connections->len, ==, 1);
1209 g_assert_cmpint (data.num_connection_attempts, ==, 1);
1210 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1211 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1214 /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1217 address = g_dbus_server_get_client_address (server);
1219 s = strstr (address, "noncefile=");
1220 g_assert (s != NULL);
1221 s += sizeof "noncefile=" - 1;
1222 nonce_file = g_strdup (s);
1224 /* First try invalid data in the nonce file - this will actually
1225 * make the client send this and the server will reject it. The way
1226 * it works is that if the nonce doesn't match, the server will
1227 * simply close the connection. So, from the client point of view,
1228 * we can see a variety of errors.
1231 res = g_file_set_contents (nonce_file,
1235 g_assert_no_error (error);
1237 c = g_dbus_connection_new_for_address_sync (address,
1238 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1239 NULL, /* GDBusAuthObserver */
1240 NULL, /* cancellable */
1242 _g_assert_error_domain (error, G_IO_ERROR);
1243 g_error_free (error);
1244 g_assert (c == NULL);
1246 /* Then try with a nonce-file of incorrect length - this will make
1247 * the client complain - we won't even try connecting to the server
1251 res = g_file_set_contents (nonce_file,
1252 "0123456789012345_",
1255 g_assert_no_error (error);
1257 c = g_dbus_connection_new_for_address_sync (address,
1258 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1259 NULL, /* GDBusAuthObserver */
1260 NULL, /* cancellable */
1262 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1263 g_error_free (error);
1264 g_assert (c == NULL);
1266 /* Finally try with no nonce-file at all */
1267 g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1269 c = g_dbus_connection_new_for_address_sync (address,
1270 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1271 NULL, /* GDBusAuthObserver */
1272 NULL, /* cancellable */
1274 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1275 g_error_free (error);
1276 g_assert (c == NULL);
1278 g_free (nonce_file);
1280 g_dbus_server_stop (server);
1281 g_object_unref (server);
1284 g_main_loop_quit (service_loop);
1285 g_thread_join (service_thread);
1289 test_credentials (void)
1291 GCredentials *c1, *c2;
1295 c1 = g_credentials_new ();
1296 c2 = g_credentials_new ();
1299 if (g_credentials_set_unix_user (c2, getuid (), &error))
1300 g_assert_no_error (error);
1302 g_clear_error (&error);
1303 g_assert (g_credentials_is_same_user (c1, c2, &error));
1304 g_assert_no_error (error);
1306 desc = g_credentials_to_string (c1);
1307 g_assert (desc != NULL);
1310 g_object_unref (c1);
1311 g_object_unref (c2);
1314 /* ---------------------------------------------------------------------------------------------------- */
1318 /* Chosen to be big enough to overflow the socket buffer */
1319 #define OVERFLOW_NUM_SIGNALS 5000
1320 #define OVERFLOW_TIMEOUT_SEC 10
1322 static GDBusMessage *
1323 overflow_filter_func (GDBusConnection *connection,
1324 GDBusMessage *message,
1328 volatile gint *counter = user_data;
1334 overflow_on_500ms_later_func (gpointer user_data)
1336 g_main_loop_quit (loop);
1337 return FALSE; /* don't keep the idle */
1341 test_overflow (void)
1346 GSocketConnection *socket_connection;
1347 GDBusConnection *producer, *consumer;
1350 volatile gint n_messages_received;
1351 volatile gint n_messages_sent;
1353 g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
1356 socket = g_socket_new_from_fd (sv[0], &error);
1357 g_assert_no_error (error);
1358 socket_connection = g_socket_connection_factory_create_connection (socket);
1359 g_assert (socket_connection != NULL);
1360 g_object_unref (socket);
1361 producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1363 G_DBUS_CONNECTION_FLAGS_NONE,
1364 NULL, /* GDBusAuthObserver */
1365 NULL, /* GCancellable */
1367 g_dbus_connection_set_exit_on_close (producer, TRUE);
1368 g_assert_no_error (error);
1369 g_object_unref (socket_connection);
1370 n_messages_sent = 0;
1371 g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
1373 /* send enough data that we get an EAGAIN */
1374 for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
1377 g_dbus_connection_emit_signal (producer,
1378 NULL, /* destination */
1380 "org.foo.Interface",
1382 g_variant_new ("(s)", "a string"),
1384 g_assert_no_error (error);
1387 /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
1388 * kernel buffers) and verify that n_messages_sent <
1389 * OVERFLOW_NUM_SIGNALS
1391 * This is to verify that not all the submitted messages have been
1392 * sent to the underlying transport.
1394 g_timeout_add (500, overflow_on_500ms_later_func, NULL);
1395 g_main_loop_run (loop);
1396 g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
1398 /* now suck it all out as a client, and add it up */
1399 socket = g_socket_new_from_fd (sv[1], &error);
1400 g_assert_no_error (error);
1401 socket_connection = g_socket_connection_factory_create_connection (socket);
1402 g_assert (socket_connection != NULL);
1403 g_object_unref (socket);
1404 consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1406 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
1407 NULL, /* GDBusAuthObserver */
1408 NULL, /* GCancellable */
1410 g_assert_no_error (error);
1411 g_object_unref (socket_connection);
1412 n_messages_received = 0;
1413 g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
1414 g_dbus_connection_start_message_processing (consumer);
1416 timer = g_timer_new ();
1417 g_timer_start (timer);
1419 while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
1420 g_main_context_iteration (NULL, FALSE);
1422 g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
1423 g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
1425 g_timer_destroy (timer);
1426 g_object_unref (consumer);
1427 g_object_unref (producer);
1431 test_overflow (void)
1433 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
1437 /* ---------------------------------------------------------------------------------------------------- */
1444 GDBusNodeInfo *introspection_data = NULL;
1447 g_thread_init (NULL);
1448 g_test_init (&argc, &argv, NULL);
1450 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1451 g_assert (introspection_data != NULL);
1452 test_interface_introspection_data = introspection_data->interfaces[0];
1454 test_guid = g_dbus_generate_guid ();
1456 /* all the tests rely on a shared main loop */
1457 loop = g_main_loop_new (NULL, FALSE);
1459 g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1460 g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1461 g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1462 g_test_add_func ("/gdbus/credentials", test_credentials);
1463 g_test_add_func ("/gdbus/overflow", test_overflow);
1467 g_main_loop_unref (loop);
1469 g_dbus_node_info_unref (introspection_data);