1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2010 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: David Zeuthen <davidz@redhat.com>
28 #include <sys/types.h>
33 #include <glib/gstdio.h>
35 #include <gio/gnetworkingprivate.h>
36 #include <gio/gunixsocketaddress.h>
37 #include <gio/gunixfdlist.h>
39 /* used in test_overflow */
41 #include <gio/gunixconnection.h>
44 #include "gdbus-tests.h"
47 static gboolean is_unix = TRUE;
49 static gboolean is_unix = FALSE;
52 static gchar *test_guid = NULL;
53 static GMainLoop *service_loop = NULL;
54 static GDBusServer *server = NULL;
55 static GMainLoop *loop = NULL;
57 /* ---------------------------------------------------------------------------------------------------- */
58 /* Test that peer-to-peer connections work */
59 /* ---------------------------------------------------------------------------------------------------- */
64 gboolean accept_connection;
65 gint num_connection_attempts;
66 GPtrArray *current_connections;
67 guint num_method_calls;
68 gboolean signal_received;
71 static const gchar *test_interface_introspection_xml =
73 " <interface name='org.gtk.GDBus.PeerTestInterface'>"
74 " <method name='HelloPeer'>"
75 " <arg type='s' name='greeting' direction='in'/>"
76 " <arg type='s' name='response' direction='out'/>"
78 " <method name='EmitSignal'/>"
79 " <method name='EmitSignalWithNameSet'/>"
80 " <method name='OpenFile'>"
81 " <arg type='s' name='path' direction='in'/>"
83 " <signal name='PeerSignal'>"
84 " <arg type='s' name='a_string'/>"
86 " <property type='s' name='PeerProperty' access='read'/>"
89 static GDBusInterfaceInfo *test_interface_introspection_data = NULL;
92 test_interface_method_call (GDBusConnection *connection,
94 const gchar *object_path,
95 const gchar *interface_name,
96 const gchar *method_name,
98 GDBusMethodInvocation *invocation,
101 PeerData *data = user_data;
103 data->num_method_calls++;
105 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
106 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
108 if (g_strcmp0 (method_name, "HelloPeer") == 0)
110 const gchar *greeting;
113 g_variant_get (parameters, "(&s)", &greeting);
115 response = g_strdup_printf ("You greeted me with '%s'.",
117 g_dbus_method_invocation_return_value (invocation,
118 g_variant_new ("(s)", response));
121 else if (g_strcmp0 (method_name, "EmitSignal") == 0)
126 g_dbus_connection_emit_signal (connection,
128 "/org/gtk/GDBus/PeerTestObject",
129 "org.gtk.GDBus.PeerTestInterface",
133 g_assert_no_error (error);
134 g_dbus_method_invocation_return_value (invocation, NULL);
136 else if (g_strcmp0 (method_name, "EmitSignalWithNameSet") == 0)
140 GDBusMessage *message;
142 message = g_dbus_message_new_signal ("/org/gtk/GDBus/PeerTestObject",
143 "org.gtk.GDBus.PeerTestInterface",
144 "PeerSignalWithNameSet");
145 g_dbus_message_set_sender (message, ":1.42");
148 ret = g_dbus_connection_send_message (connection, message, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error);
149 g_assert_no_error (error);
151 g_object_unref (message);
153 g_dbus_method_invocation_return_value (invocation, NULL);
155 else if (g_strcmp0 (method_name, "OpenFile") == 0)
162 GUnixFDList *fd_list;
164 g_variant_get (parameters, "(&s)", &path);
166 fd_list = g_unix_fd_list_new ();
170 fd = open (path, O_RDONLY);
171 g_unix_fd_list_append (fd_list, fd, &error);
172 g_assert_no_error (error);
175 reply = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
176 g_dbus_message_set_unix_fd_list (reply, fd_list);
177 g_object_unref (invocation);
180 g_dbus_connection_send_message (connection,
182 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
183 NULL, /* out_serial */
185 g_assert_no_error (error);
186 g_object_unref (reply);
188 g_dbus_method_invocation_return_dbus_error (invocation,
189 "org.gtk.GDBus.NotOnUnix",
190 "Your OS does not support file descriptor passing");
195 g_assert_not_reached ();
200 test_interface_get_property (GDBusConnection *connection,
202 const gchar *object_path,
203 const gchar *interface_name,
204 const gchar *property_name,
208 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
209 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
210 g_assert_cmpstr (property_name, ==, "PeerProperty");
212 return g_variant_new_string ("ThePropertyValue");
216 static const GDBusInterfaceVTable test_interface_vtable =
218 test_interface_method_call,
219 test_interface_get_property,
220 NULL /* set_property */
224 on_proxy_signal_received (GDBusProxy *proxy,
227 GVariant *parameters,
230 PeerData *data = user_data;
232 data->signal_received = TRUE;
234 g_assert (sender_name == NULL);
235 g_assert_cmpstr (signal_name, ==, "PeerSignal");
236 g_main_loop_quit (loop);
240 on_proxy_signal_received_with_name_set (GDBusProxy *proxy,
243 GVariant *parameters,
246 PeerData *data = user_data;
248 data->signal_received = TRUE;
250 g_assert_cmpstr (sender_name, ==, ":1.42");
251 g_assert_cmpstr (signal_name, ==, "PeerSignalWithNameSet");
252 g_main_loop_quit (loop);
255 /* ---------------------------------------------------------------------------------------------------- */
258 on_authorize_authenticated_peer (GDBusAuthObserver *observer,
260 GCredentials *credentials,
263 PeerData *data = user_data;
266 data->num_connection_attempts++;
269 if (!data->accept_connection)
272 g_main_loop_quit (loop);
278 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
280 on_new_connection (GDBusServer *server,
281 GDBusConnection *connection,
284 PeerData *data = user_data;
288 //g_print ("Client connected.\n"
289 // "Negotiated capabilities: unix-fd-passing=%d\n",
290 // g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
292 g_ptr_array_add (data->current_connections, g_object_ref (connection));
294 /* export object on the newly established connection */
296 reg_id = g_dbus_connection_register_object (connection,
297 "/org/gtk/GDBus/PeerTestObject",
298 test_interface_introspection_data,
299 &test_interface_vtable,
301 NULL, /* GDestroyNotify for data */
303 g_assert_no_error (error);
304 g_assert (reg_id > 0);
306 g_main_loop_quit (loop);
310 service_thread_func (gpointer user_data)
312 PeerData *data = user_data;
313 GMainContext *service_context;
314 GDBusAuthObserver *observer;
317 service_context = g_main_context_new ();
318 g_main_context_push_thread_default (service_context);
321 observer = g_dbus_auth_observer_new ();
322 server = g_dbus_server_new_sync (is_unix ? "unix:tmpdir=/tmp/gdbus-test-" : "nonce-tcp:",
323 G_DBUS_SERVER_FLAGS_NONE,
326 NULL, /* cancellable */
328 g_assert_no_error (error);
330 g_signal_connect (server,
332 G_CALLBACK (on_new_connection),
334 g_signal_connect (observer,
335 "authorize-authenticated-peer",
336 G_CALLBACK (on_authorize_authenticated_peer),
338 g_object_unref (observer);
340 g_dbus_server_start (server);
342 service_loop = g_main_loop_new (service_context, FALSE);
343 g_main_loop_run (service_loop);
345 g_main_context_pop_thread_default (service_context);
347 g_main_loop_unref (service_loop);
348 g_main_context_unref (service_context);
350 /* test code specifically unrefs the server - see below */
351 g_assert (server == NULL);
358 on_incoming_connection (GSocketService *service,
359 GSocketConnection *socket_connection,
360 GObject *source_object,
363 PeerData *data = user_data;
365 if (data->accept_connection)
369 GDBusConnection *connection;
372 connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
374 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
375 NULL, /* cancellable */
377 g_assert_no_error (error);
379 g_ptr_array_add (data->current_connections, connection);
381 /* export object on the newly established connection */
383 reg_id = g_dbus_connection_register_object (connection,
384 "/org/gtk/GDBus/PeerTestObject",
385 &test_interface_introspection_data,
386 &test_interface_vtable,
388 NULL, /* GDestroyNotify for data */
390 g_assert_no_error (error);
391 g_assert (reg_id > 0);
396 /* don't do anything */
399 data->num_connection_attempts++;
401 g_main_loop_quit (loop);
403 /* stops other signal handlers from being invoked */
408 service_thread_func (gpointer data)
410 GMainContext *service_context;
412 GSocketAddress *address;
415 service_context = g_main_context_new ();
416 g_main_context_push_thread_default (service_context);
418 socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
419 address = g_unix_socket_address_new (socket_path);
421 service = g_socket_service_new ();
423 g_socket_listener_add_address (G_SOCKET_LISTENER (service),
425 G_SOCKET_TYPE_STREAM,
426 G_SOCKET_PROTOCOL_DEFAULT,
427 NULL, /* source_object */
428 NULL, /* effective_address */
430 g_assert_no_error (error);
431 g_signal_connect (service,
433 G_CALLBACK (on_incoming_connection),
435 g_socket_service_start (service);
437 service_loop = g_main_loop_new (service_context, FALSE);
438 g_main_loop_run (service_loop);
440 g_main_context_pop_thread_default (service_context);
442 g_main_loop_unref (service_loop);
443 g_main_context_unref (service_context);
445 g_object_unref (address);
446 g_free (socket_path);
451 /* ---------------------------------------------------------------------------------------------------- */
455 check_connection (gpointer user_data)
457 PeerData *data = user_data;
460 for (n = 0; n < data->current_connections->len; n++)
465 c = G_DBUS_CONNECTION (data->current_connections->pdata[n]);
466 stream = g_dbus_connection_get_stream (c);
468 g_debug ("In check_connection for %d: connection %p, stream %p", n, c, stream);
469 g_debug ("closed = %d", g_io_stream_is_closed (stream));
472 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
473 g_debug ("socket_closed = %d", g_socket_is_closed (socket));
474 g_debug ("socket_condition_check = %d", g_socket_condition_check (socket, G_IO_IN|G_IO_OUT|G_IO_ERR|G_IO_HUP));
480 num_read = g_input_stream_read (g_io_stream_get_input_stream (stream),
487 g_debug ("error: %s", error->message);
488 g_error_free (error);
492 g_debug ("no error, read %d bytes", (gint) num_read);
500 on_do_disconnect_in_idle (gpointer data)
502 GDBusConnection *c = G_DBUS_CONNECTION (data);
503 g_debug ("GDC %p has ref_count %d", c, G_OBJECT (c)->ref_count);
504 g_dbus_connection_disconnect (c);
521 GThread *service_thread;
522 gulong signal_handler_id;
524 memset (&data, '\0', sizeof (PeerData));
525 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
527 /* first try to connect when there is no server */
529 c = g_dbus_connection_new_for_address_sync (is_unix ? "unix:path=/tmp/gdbus-test-does-not-exist-pid" :
530 /* NOTE: Even if something is listening on port 12345 the connection
531 * will fail because the nonce file doesn't exist */
532 "nonce-tcp:host=localhost,port=12345,noncefile=this-does-not-exist-gdbus",
533 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
534 NULL, /* GDBusAuthObserver */
535 NULL, /* cancellable */
537 _g_assert_error_domain (error, G_IO_ERROR);
538 g_assert (!g_dbus_error_is_remote_error (error));
539 g_clear_error (&error);
540 g_assert (c == NULL);
542 /* bring up a server - we run the server in a different thread to avoid deadlocks */
544 service_thread = g_thread_create (service_thread_func,
548 while (service_loop == NULL)
550 g_assert (server != NULL);
552 /* bring up a connection and accept it */
553 data.accept_connection = TRUE;
555 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
556 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
557 NULL, /* GDBusAuthObserver */
558 NULL, /* cancellable */
560 g_assert_no_error (error);
561 g_assert (c != NULL);
562 while (data.current_connections->len < 1)
563 g_main_loop_run (loop);
564 g_assert_cmpint (data.current_connections->len, ==, 1);
565 g_assert_cmpint (data.num_connection_attempts, ==, 1);
566 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
567 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
569 /* check that we create a proxy, read properties, receive signals and invoke
570 * the HelloPeer() method. Since the server runs in another thread it's fine
571 * to use synchronous blocking API here.
574 proxy = g_dbus_proxy_new_sync (c,
575 G_DBUS_PROXY_FLAGS_NONE,
578 "/org/gtk/GDBus/PeerTestObject",
579 "org.gtk.GDBus.PeerTestInterface",
580 NULL, /* GCancellable */
582 g_assert_no_error (error);
583 g_assert (proxy != NULL);
585 value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
586 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
588 /* try invoking a method */
590 result = g_dbus_proxy_call_sync (proxy,
592 g_variant_new ("(s)", "Hey Peer!"),
593 G_DBUS_CALL_FLAGS_NONE,
595 NULL, /* GCancellable */
597 g_assert_no_error (error);
598 g_variant_get (result, "(&s)", &s);
599 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
600 g_variant_unref (result);
601 g_assert_cmpint (data.num_method_calls, ==, 1);
603 /* make the other peer emit a signal - catch it */
604 signal_handler_id = g_signal_connect (proxy,
606 G_CALLBACK (on_proxy_signal_received),
608 g_assert (!data.signal_received);
609 g_dbus_proxy_call (proxy,
611 NULL, /* no arguments */
612 G_DBUS_CALL_FLAGS_NONE,
614 NULL, /* GCancellable */
615 NULL, /* GAsyncReadyCallback - we don't care about the result */
616 NULL); /* user_data */
617 g_main_loop_run (loop);
618 g_assert (data.signal_received);
619 g_assert_cmpint (data.num_method_calls, ==, 2);
620 g_signal_handler_disconnect (proxy, signal_handler_id);
622 /* Also ensure that messages with the sender header-field set gets
623 * delivered to the proxy - note that this doesn't really make sense
624 * e.g. names are meaning-less in a peer-to-peer case... but we
625 * support it because it makes sense in certain bridging
626 * applications - see e.g. #623815.
628 signal_handler_id = g_signal_connect (proxy,
630 G_CALLBACK (on_proxy_signal_received_with_name_set),
632 data.signal_received = FALSE;
633 g_dbus_proxy_call (proxy,
634 "EmitSignalWithNameSet",
635 NULL, /* no arguments */
636 G_DBUS_CALL_FLAGS_NONE,
638 NULL, /* GCancellable */
639 NULL, /* GAsyncReadyCallback - we don't care about the result */
640 NULL); /* user_data */
641 g_main_loop_run (loop);
642 g_assert (data.signal_received);
643 g_assert_cmpint (data.num_method_calls, ==, 3);
644 g_signal_handler_disconnect (proxy, signal_handler_id);
646 /* check for UNIX fd passing */
649 GDBusMessage *method_call_message;
650 GDBusMessage *method_reply_message;
651 GUnixFDList *fd_list;
658 method_call_message = g_dbus_message_new_method_call (NULL, /* name */
659 "/org/gtk/GDBus/PeerTestObject",
660 "org.gtk.GDBus.PeerTestInterface",
662 g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", "/etc/hosts"));
664 method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
666 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
668 NULL, /* out_serial */
669 NULL, /* cancellable */
671 g_assert_no_error (error);
672 g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
673 fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
674 g_assert (fd_list != NULL);
675 g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
677 fd = g_unix_fd_list_get (fd_list, 0, &error);
678 g_assert_no_error (error);
679 g_object_unref (method_call_message);
680 g_object_unref (method_reply_message);
682 memset (buf, '\0', sizeof (buf));
683 len = read (fd, buf, sizeof (buf) - 1);
687 g_file_get_contents ("/etc/hosts",
691 g_assert_no_error (error);
692 if (len2 > sizeof (buf))
693 buf2[sizeof (buf)] = '\0';
694 g_assert_cmpstr (buf, ==, buf2);
699 result = g_dbus_proxy_call_sync (proxy,
701 g_variant_new ("(s)", "boo"),
702 G_DBUS_CALL_FLAGS_NONE,
704 NULL, /* GCancellable */
706 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
707 g_assert (result == NULL);
708 g_error_free (error);
709 #endif /* G_OS_UNIX */
711 /* Check that g_socket_get_credentials() work - this really should
712 * be in a GSocket-specific test suite but no such test suite exists
717 GCredentials *credentials;
718 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (g_dbus_connection_get_stream (c)));
719 g_assert (G_IS_SOCKET (socket));
721 credentials = g_socket_get_credentials (socket, &error);
724 struct ucred *native_creds;
725 g_assert_no_error (error);
726 g_assert (G_IS_CREDENTIALS (credentials));
727 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED);
728 g_assert (native_creds != NULL);
729 g_assert (native_creds->uid == getuid ());
730 g_assert (native_creds->gid == getgid ());
731 g_assert (native_creds->pid == getpid ());
733 g_object_unref (credentials);
735 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
736 g_assert (credentials == NULL);
741 /* bring up a connection - don't accept it - this should fail
743 data.accept_connection = FALSE;
745 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
746 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
747 NULL, /* GDBusAuthObserver */
748 NULL, /* cancellable */
750 _g_assert_error_domain (error, G_IO_ERROR);
751 g_assert (c2 == NULL);
754 /* TODO: THIS TEST DOESN'T WORK YET */
756 /* bring up a connection - accept it.. then disconnect from the client side - check
757 * that the server side gets the disconnect signal.
760 data.accept_connection = TRUE;
761 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
762 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
763 NULL, /* GDBusAuthObserver */
764 NULL, /* cancellable */
766 g_assert_no_error (error);
767 g_assert (c2 != NULL);
768 g_assert (!g_dbus_connection_get_is_disconnected (c2));
769 while (data.num_connection_attempts < 3)
770 g_main_loop_run (loop);
771 g_assert_cmpint (data.current_connections->len, ==, 2);
772 g_assert_cmpint (data.num_connection_attempts, ==, 3);
773 g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
774 g_idle_add (on_do_disconnect_in_idle, c2);
775 g_debug ("==================================================");
776 g_debug ("==================================================");
777 g_debug ("==================================================");
778 g_debug ("waiting for disconnect on connection %p, stream %p",
779 data.current_connections->pdata[1],
780 g_dbus_connection_get_stream (data.current_connections->pdata[1]));
782 g_timeout_add (2000, check_connection, &data);
783 //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
784 g_main_loop_run (loop);
785 g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
786 g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
789 /* unref the server and stop listening for new connections
791 * This won't bring down the established connections - check that c is still connected
792 * by invoking a method
794 //g_socket_service_stop (service);
795 //g_object_unref (service);
796 g_dbus_server_stop (server);
797 g_object_unref (server);
801 result = g_dbus_proxy_call_sync (proxy,
803 g_variant_new ("(s)", "Hey Again Peer!"),
804 G_DBUS_CALL_FLAGS_NONE,
806 NULL, /* GCancellable */
808 g_assert_no_error (error);
809 g_variant_get (result, "(&s)", &s);
810 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
811 g_variant_unref (result);
812 g_assert_cmpint (data.num_method_calls, ==, 5);
815 /* TODO: THIS TEST DOESN'T WORK YET */
817 /* now disconnect from the server side - check that the client side gets the signal */
818 g_assert_cmpint (data.current_connections->len, ==, 1);
819 g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
820 g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
821 if (!g_dbus_connection_get_is_disconnected (c))
822 _g_assert_signal_received (c, "closed");
823 g_assert (g_dbus_connection_get_is_disconnected (c));
827 g_ptr_array_unref (data.current_connections);
828 g_object_unref (proxy);
830 g_main_loop_quit (service_loop);
831 g_thread_join (service_thread);
834 /* ---------------------------------------------------------------------------------------------------- */
839 GMainContext *context;
846 dmp_data_free (DmpData *data)
848 g_main_loop_unref (data->loop);
849 g_main_context_unref (data->context);
850 g_object_unref (data->server);
851 g_list_foreach (data->connections, (GFunc) g_object_unref, NULL);
852 g_list_free (data->connections);
857 dmp_on_method_call (GDBusConnection *connection,
859 const gchar *object_path,
860 const gchar *interface_name,
861 const gchar *method_name,
862 GVariant *parameters,
863 GDBusMethodInvocation *invocation,
866 //DmpData *data = user_data;
869 g_variant_get (parameters,
873 g_dbus_method_invocation_return_value (invocation,
874 g_variant_new ("(i)", first + second));
877 static const GDBusInterfaceVTable dmp_interface_vtable =
880 NULL, /* get_property */
881 NULL /* set_property */
885 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
887 dmp_on_new_connection (GDBusServer *server,
888 GDBusConnection *connection,
891 DmpData *data = user_data;
895 /* accept the connection */
896 data->connections = g_list_prepend (data->connections, g_object_ref (connection));
899 node = g_dbus_node_info_new_for_xml ("<node>"
900 " <interface name='org.gtk.GDBus.DmpInterface'>"
901 " <method name='AddPair'>"
902 " <arg type='i' name='first' direction='in'/>"
903 " <arg type='i' name='second' direction='in'/>"
904 " <arg type='i' name='sum' direction='out'/>"
909 g_assert_no_error (error);
911 /* sleep 100ms before exporting an object - this is to test that
912 * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
913 * (GDBusServer uses this feature).
917 /* export an object */
919 g_dbus_connection_register_object (connection,
922 &dmp_interface_vtable,
926 g_dbus_node_info_unref (node);
930 dmp_thread_func (gpointer user_data)
932 DmpData *data = user_data;
936 data->context = g_main_context_new ();
937 g_main_context_push_thread_default (data->context);
940 guid = g_dbus_generate_guid ();
941 data->server = g_dbus_server_new_sync ("unix:tmpdir=/tmp/gdbus-test-",
942 G_DBUS_SERVER_FLAGS_NONE,
944 NULL, /* GDBusAuthObserver */
945 NULL, /* GCancellable */
947 g_assert_no_error (error);
948 g_signal_connect (data->server,
950 G_CALLBACK (dmp_on_new_connection),
953 g_dbus_server_start (data->server);
955 data->loop = g_main_loop_new (data->context, FALSE);
956 g_main_loop_run (data->loop);
958 g_main_context_pop_thread_default (data->context);
965 delayed_message_processing (void)
969 GThread *service_thread;
972 data = g_new0 (DmpData, 1);
975 service_thread = g_thread_create (dmp_thread_func,
979 while (data->server == NULL || !g_dbus_server_is_active (data->server))
982 for (n = 0; n < 5; n++)
989 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
990 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
991 NULL, /* GDBusAuthObserver */
992 NULL, /* GCancellable */
994 g_assert_no_error (error);
997 res = g_dbus_connection_call_sync (c,
1000 "org.gtk.GDBus.DmpInterface",
1002 g_variant_new ("(ii)", 2, n),
1003 G_VARIANT_TYPE ("(i)"),
1004 G_DBUS_CALL_FLAGS_NONE,
1005 -1, /* timeout_msec */
1006 NULL, /* GCancellable */
1008 g_assert_no_error (error);
1009 g_variant_get (res, "(i)", &val);
1010 g_assert_cmpint (val, ==, 2 + n);
1011 g_variant_unref (res);
1015 g_main_loop_quit (data->loop);
1016 g_thread_join (service_thread);
1017 dmp_data_free (data);
1020 /* ---------------------------------------------------------------------------------------------------- */
1023 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1025 GCredentials *credentials,
1028 PeerData *data = user_data;
1029 gboolean authorized;
1031 data->num_connection_attempts++;
1034 if (!data->accept_connection)
1037 g_main_loop_quit (loop);
1043 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1045 nonce_tcp_on_new_connection (GDBusServer *server,
1046 GDBusConnection *connection,
1049 PeerData *data = user_data;
1051 g_ptr_array_add (data->current_connections, g_object_ref (connection));
1053 g_main_loop_quit (loop);
1057 nonce_tcp_service_thread_func (gpointer user_data)
1059 PeerData *data = user_data;
1060 GMainContext *service_context;
1061 GDBusAuthObserver *observer;
1064 service_context = g_main_context_new ();
1065 g_main_context_push_thread_default (service_context);
1068 observer = g_dbus_auth_observer_new ();
1069 server = g_dbus_server_new_sync ("nonce-tcp:",
1070 G_DBUS_SERVER_FLAGS_NONE,
1073 NULL, /* cancellable */
1075 g_assert_no_error (error);
1077 g_signal_connect (server,
1079 G_CALLBACK (nonce_tcp_on_new_connection),
1081 g_signal_connect (observer,
1082 "authorize-authenticated-peer",
1083 G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1085 g_object_unref (observer);
1087 g_dbus_server_start (server);
1089 service_loop = g_main_loop_new (service_context, FALSE);
1090 g_main_loop_run (service_loop);
1092 g_main_context_pop_thread_default (service_context);
1094 g_main_loop_unref (service_loop);
1095 g_main_context_unref (service_context);
1097 /* test code specifically unrefs the server - see below */
1098 g_assert (server == NULL);
1104 test_nonce_tcp (void)
1108 GThread *service_thread;
1113 const gchar *address;
1115 memset (&data, '\0', sizeof (PeerData));
1116 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1120 service_loop = NULL;
1121 service_thread = g_thread_create (nonce_tcp_service_thread_func,
1125 while (service_loop == NULL)
1127 g_assert (server != NULL);
1130 /* bring up a connection and accept it */
1131 data.accept_connection = TRUE;
1133 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1134 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1135 NULL, /* GDBusAuthObserver */
1136 NULL, /* cancellable */
1138 g_assert_no_error (error);
1139 g_assert (c != NULL);
1140 while (data.current_connections->len < 1)
1141 g_main_loop_run (loop);
1142 g_assert_cmpint (data.current_connections->len, ==, 1);
1143 g_assert_cmpint (data.num_connection_attempts, ==, 1);
1144 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1145 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1148 /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1151 address = g_dbus_server_get_client_address (server);
1153 s = strstr (address, "noncefile=");
1154 g_assert (s != NULL);
1155 s += sizeof "noncefile=" - 1;
1156 nonce_file = g_strdup (s);
1158 /* First try invalid data in the nonce file - this will actually
1159 * make the client send this and the server will reject it. The way
1160 * it works is that if the nonce doesn't match, the server will
1161 * simply close the connection. So, from the client point of view,
1162 * we can see a variety of errors.
1165 res = g_file_set_contents (nonce_file,
1169 g_assert_no_error (error);
1171 c = g_dbus_connection_new_for_address_sync (address,
1172 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1173 NULL, /* GDBusAuthObserver */
1174 NULL, /* cancellable */
1176 _g_assert_error_domain (error, G_IO_ERROR);
1177 g_assert (c == NULL);
1179 /* Then try with a nonce-file of incorrect length - this will make
1180 * the client complain - we won't even try connecting to the server
1184 res = g_file_set_contents (nonce_file,
1185 "0123456789012345_",
1188 g_assert_no_error (error);
1190 c = g_dbus_connection_new_for_address_sync (address,
1191 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1192 NULL, /* GDBusAuthObserver */
1193 NULL, /* cancellable */
1195 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1196 g_assert (c == NULL);
1198 /* Finally try with no nonce-file at all */
1199 g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1201 c = g_dbus_connection_new_for_address_sync (address,
1202 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1203 NULL, /* GDBusAuthObserver */
1204 NULL, /* cancellable */
1206 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1207 g_assert (c == NULL);
1209 g_free (nonce_file);
1211 g_dbus_server_stop (server);
1212 g_object_unref (server);
1215 g_main_loop_quit (service_loop);
1216 g_thread_join (service_thread);
1220 test_credentials (void)
1222 GCredentials *c1, *c2;
1226 c1 = g_credentials_new ();
1227 c2 = g_credentials_new ();
1230 if (g_credentials_set_unix_user (c2, getuid (), &error))
1231 g_assert_no_error (error);
1233 g_clear_error (&error);
1234 g_assert (g_credentials_is_same_user (c1, c2, &error));
1235 g_assert_no_error (error);
1237 desc = g_credentials_to_string (c1);
1238 g_assert (desc != NULL);
1241 g_object_unref (c1);
1242 g_object_unref (c2);
1245 /* ---------------------------------------------------------------------------------------------------- */
1249 /* Chosen to be big enough to overflow the socket buffer */
1250 #define OVERFLOW_NUM_SIGNALS 5000
1251 #define OVERFLOW_TIMEOUT_SEC 10
1254 overflow_filter_func (GDBusConnection *connection,
1255 GDBusMessage *message,
1259 volatile gint *counter = user_data;
1261 return FALSE; /* don't drop the message */
1265 overflow_on_500ms_later_func (gpointer user_data)
1267 g_main_loop_quit (loop);
1268 return FALSE; /* don't keep the idle */
1272 test_overflow (void)
1277 GSocketConnection *socket_connection;
1278 GDBusConnection *producer, *consumer;
1281 volatile gint n_messages_received;
1282 volatile gint n_messages_sent;
1284 g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
1287 socket = g_socket_new_from_fd (sv[0], &error);
1288 g_assert_no_error (error);
1289 socket_connection = g_socket_connection_factory_create_connection (socket);
1290 g_assert (socket_connection != NULL);
1291 g_object_unref (socket);
1292 producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1294 G_DBUS_CONNECTION_FLAGS_NONE,
1295 NULL, /* GDBusAuthObserver */
1296 NULL, /* GCancellable */
1298 g_dbus_connection_set_exit_on_close (producer, TRUE);
1299 g_assert_no_error (error);
1300 g_object_unref (socket_connection);
1301 n_messages_sent = 0;
1302 g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
1304 /* send enough data that we get an EAGAIN */
1305 for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
1308 g_dbus_connection_emit_signal (producer,
1309 NULL, /* destination */
1311 "org.foo.Interface",
1313 g_variant_new ("(s)", "a string"),
1315 g_assert_no_error (error);
1318 /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
1319 * kernel buffers) and verify that n_messages_sent <
1320 * OVERFLOW_NUM_SIGNALS
1322 * This is to verify that not all the submitted messages have been
1323 * sent to the underlying transport.
1325 g_timeout_add (500, overflow_on_500ms_later_func, NULL);
1326 g_main_loop_run (loop);
1327 g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
1329 /* now suck it all out as a client, and add it up */
1330 socket = g_socket_new_from_fd (sv[1], &error);
1331 g_assert_no_error (error);
1332 socket_connection = g_socket_connection_factory_create_connection (socket);
1333 g_assert (socket_connection != NULL);
1334 g_object_unref (socket);
1335 consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1337 G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
1338 NULL, /* GDBusAuthObserver */
1339 NULL, /* GCancellable */
1341 g_assert_no_error (error);
1342 g_object_unref (socket_connection);
1343 n_messages_received = 0;
1344 g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
1345 g_dbus_connection_start_message_processing (consumer);
1347 timer = g_timer_new ();
1348 g_timer_start (timer);
1350 while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
1351 g_main_context_iteration (NULL, FALSE);
1353 g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
1354 g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
1356 g_timer_destroy (timer);
1357 g_object_unref (consumer);
1358 g_object_unref (producer);
1362 test_overflow (void)
1364 /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
1368 /* ---------------------------------------------------------------------------------------------------- */
1375 GDBusNodeInfo *introspection_data = NULL;
1378 g_thread_init (NULL);
1379 g_test_init (&argc, &argv, NULL);
1381 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1382 g_assert (introspection_data != NULL);
1383 test_interface_introspection_data = introspection_data->interfaces[0];
1385 test_guid = g_dbus_generate_guid ();
1387 /* all the tests rely on a shared main loop */
1388 loop = g_main_loop_new (NULL, FALSE);
1390 g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1391 g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1392 g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1393 g_test_add_func ("/gdbus/credentials", test_credentials);
1394 g_test_add_func ("/gdbus/overflow", test_overflow);
1398 g_main_loop_unref (loop);
1400 g_dbus_node_info_unref (introspection_data);