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/gunixsocketaddress.h>
36 #include <gio/gunixfdlist.h>
38 /* for struct ucred */
41 #include <sys/types.h>
42 #include <sys/socket.h>
45 #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;
105 data->num_method_calls++;
107 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
108 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
110 if (g_strcmp0 (method_name, "HelloPeer") == 0)
112 const gchar *greeting;
115 g_variant_get (parameters, "(&s)", &greeting);
117 response = g_strdup_printf ("You greeted me with '%s'.",
119 g_dbus_method_invocation_return_value (invocation,
120 g_variant_new ("(s)", response));
123 else if (g_strcmp0 (method_name, "EmitSignal") == 0)
128 g_dbus_connection_emit_signal (connection,
130 "/org/gtk/GDBus/PeerTestObject",
131 "org.gtk.GDBus.PeerTestInterface",
135 g_assert_no_error (error);
136 g_dbus_method_invocation_return_value (invocation, NULL);
138 else if (g_strcmp0 (method_name, "EmitSignalWithNameSet") == 0)
142 GDBusMessage *message;
144 message = g_dbus_message_new_signal ("/org/gtk/GDBus/PeerTestObject",
145 "org.gtk.GDBus.PeerTestInterface",
146 "PeerSignalWithNameSet");
147 g_dbus_message_set_sender (message, ":1.42");
150 ret = g_dbus_connection_send_message (connection, message, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error);
151 g_assert_no_error (error);
153 g_object_unref (message);
155 g_dbus_method_invocation_return_value (invocation, NULL);
157 else if (g_strcmp0 (method_name, "OpenFile") == 0)
164 GUnixFDList *fd_list;
166 g_variant_get (parameters, "(&s)", &path);
168 fd_list = g_unix_fd_list_new ();
172 fd = open (path, O_RDONLY);
173 g_unix_fd_list_append (fd_list, fd, &error);
174 g_assert_no_error (error);
177 reply = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
178 g_dbus_message_set_unix_fd_list (reply, fd_list);
179 g_object_unref (invocation);
182 g_dbus_connection_send_message (connection,
184 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
185 NULL, /* out_serial */
187 g_assert_no_error (error);
188 g_object_unref (reply);
190 g_dbus_method_invocation_return_dbus_error (invocation,
191 "org.gtk.GDBus.NotOnUnix",
192 "Your OS does not support file descriptor passing");
197 g_assert_not_reached ();
202 test_interface_get_property (GDBusConnection *connection,
204 const gchar *object_path,
205 const gchar *interface_name,
206 const gchar *property_name,
210 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
211 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
212 g_assert_cmpstr (property_name, ==, "PeerProperty");
214 return g_variant_new_string ("ThePropertyValue");
218 static const GDBusInterfaceVTable test_interface_vtable =
220 test_interface_method_call,
221 test_interface_get_property,
222 NULL /* set_property */
226 on_proxy_signal_received (GDBusProxy *proxy,
229 GVariant *parameters,
232 PeerData *data = user_data;
234 data->signal_received = TRUE;
236 g_assert (sender_name == NULL);
237 g_assert_cmpstr (signal_name, ==, "PeerSignal");
238 g_main_loop_quit (loop);
242 on_proxy_signal_received_with_name_set (GDBusProxy *proxy,
245 GVariant *parameters,
248 PeerData *data = user_data;
250 data->signal_received = TRUE;
252 g_assert_cmpstr (sender_name, ==, ":1.42");
253 g_assert_cmpstr (signal_name, ==, "PeerSignalWithNameSet");
254 g_main_loop_quit (loop);
257 /* ---------------------------------------------------------------------------------------------------- */
260 on_authorize_authenticated_peer (GDBusAuthObserver *observer,
262 GCredentials *credentials,
265 PeerData *data = user_data;
268 data->num_connection_attempts++;
271 if (!data->accept_connection)
274 g_main_loop_quit (loop);
280 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
282 on_new_connection (GDBusServer *server,
283 GDBusConnection *connection,
286 PeerData *data = user_data;
290 //g_print ("Client connected.\n"
291 // "Negotiated capabilities: unix-fd-passing=%d\n",
292 // g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
294 g_ptr_array_add (data->current_connections, g_object_ref (connection));
296 /* export object on the newly established connection */
298 reg_id = g_dbus_connection_register_object (connection,
299 "/org/gtk/GDBus/PeerTestObject",
300 test_interface_introspection_data,
301 &test_interface_vtable,
303 NULL, /* GDestroyNotify for data */
305 g_assert_no_error (error);
306 g_assert (reg_id > 0);
308 g_main_loop_quit (loop);
312 service_thread_func (gpointer user_data)
314 PeerData *data = user_data;
315 GMainContext *service_context;
316 GDBusAuthObserver *observer;
319 service_context = g_main_context_new ();
320 g_main_context_push_thread_default (service_context);
323 observer = g_dbus_auth_observer_new ();
324 server = g_dbus_server_new_sync (is_unix ? "unix:tmpdir=/tmp/gdbus-test-" : "nonce-tcp:",
325 G_DBUS_SERVER_FLAGS_NONE,
328 NULL, /* cancellable */
330 g_assert_no_error (error);
332 g_signal_connect (server,
334 G_CALLBACK (on_new_connection),
336 g_signal_connect (observer,
337 "authorize-authenticated-peer",
338 G_CALLBACK (on_authorize_authenticated_peer),
340 g_object_unref (observer);
342 g_dbus_server_start (server);
344 service_loop = g_main_loop_new (service_context, FALSE);
345 g_main_loop_run (service_loop);
347 g_main_context_pop_thread_default (service_context);
349 g_main_loop_unref (service_loop);
350 g_main_context_unref (service_context);
352 /* test code specifically unrefs the server - see below */
353 g_assert (server == NULL);
360 on_incoming_connection (GSocketService *service,
361 GSocketConnection *socket_connection,
362 GObject *source_object,
365 PeerData *data = user_data;
367 if (data->accept_connection)
371 GDBusConnection *connection;
374 connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
376 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
377 NULL, /* cancellable */
379 g_assert_no_error (error);
381 g_ptr_array_add (data->current_connections, connection);
383 /* export object on the newly established connection */
385 reg_id = g_dbus_connection_register_object (connection,
386 "/org/gtk/GDBus/PeerTestObject",
387 &test_interface_introspection_data,
388 &test_interface_vtable,
390 NULL, /* GDestroyNotify for data */
392 g_assert_no_error (error);
393 g_assert (reg_id > 0);
398 /* don't do anything */
401 data->num_connection_attempts++;
403 g_main_loop_quit (loop);
405 /* stops other signal handlers from being invoked */
410 service_thread_func (gpointer data)
412 GMainContext *service_context;
414 GSocketAddress *address;
417 service_context = g_main_context_new ();
418 g_main_context_push_thread_default (service_context);
420 socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
421 address = g_unix_socket_address_new (socket_path);
423 service = g_socket_service_new ();
425 g_socket_listener_add_address (G_SOCKET_LISTENER (service),
427 G_SOCKET_TYPE_STREAM,
428 G_SOCKET_PROTOCOL_DEFAULT,
429 NULL, /* source_object */
430 NULL, /* effective_address */
432 g_assert_no_error (error);
433 g_signal_connect (service,
435 G_CALLBACK (on_incoming_connection),
437 g_socket_service_start (service);
439 service_loop = g_main_loop_new (service_context, FALSE);
440 g_main_loop_run (service_loop);
442 g_main_context_pop_thread_default (service_context);
444 g_main_loop_unref (service_loop);
445 g_main_context_unref (service_context);
447 g_object_unref (address);
448 g_free (socket_path);
453 /* ---------------------------------------------------------------------------------------------------- */
457 check_connection (gpointer user_data)
459 PeerData *data = user_data;
462 for (n = 0; n < data->current_connections->len; n++)
467 c = G_DBUS_CONNECTION (data->current_connections->pdata[n]);
468 stream = g_dbus_connection_get_stream (c);
470 g_debug ("In check_connection for %d: connection %p, stream %p", n, c, stream);
471 g_debug ("closed = %d", g_io_stream_is_closed (stream));
474 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
475 g_debug ("socket_closed = %d", g_socket_is_closed (socket));
476 g_debug ("socket_condition_check = %d", g_socket_condition_check (socket, G_IO_IN|G_IO_OUT|G_IO_ERR|G_IO_HUP));
482 num_read = g_input_stream_read (g_io_stream_get_input_stream (stream),
489 g_debug ("error: %s", error->message);
490 g_error_free (error);
494 g_debug ("no error, read %d bytes", (gint) num_read);
502 on_do_disconnect_in_idle (gpointer data)
504 GDBusConnection *c = G_DBUS_CONNECTION (data);
505 g_debug ("GDC %p has ref_count %d", c, G_OBJECT (c)->ref_count);
506 g_dbus_connection_disconnect (c);
523 GThread *service_thread;
524 gulong signal_handler_id;
526 memset (&data, '\0', sizeof (PeerData));
527 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
529 /* first try to connect when there is no server */
531 c = g_dbus_connection_new_for_address_sync (is_unix ? "unix:path=/tmp/gdbus-test-does-not-exist-pid" :
532 /* NOTE: Even if something is listening on port 12345 the connection
533 * will fail because the nonce file doesn't exist */
534 "nonce-tcp:host=localhost,port=12345,noncefile=this-does-not-exist-gdbus",
535 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
536 NULL, /* GDBusAuthObserver */
537 NULL, /* cancellable */
539 _g_assert_error_domain (error, G_IO_ERROR);
540 g_assert (!g_dbus_error_is_remote_error (error));
541 g_clear_error (&error);
542 g_assert (c == NULL);
544 /* bring up a server - we run the server in a different thread to avoid deadlocks */
546 service_thread = g_thread_create (service_thread_func,
550 while (service_loop == NULL)
552 g_assert (server != NULL);
554 /* bring up a connection and accept it */
555 data.accept_connection = TRUE;
557 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
558 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
559 NULL, /* GDBusAuthObserver */
560 NULL, /* cancellable */
562 g_assert_no_error (error);
563 g_assert (c != NULL);
564 while (data.current_connections->len < 1)
565 g_main_loop_run (loop);
566 g_assert_cmpint (data.current_connections->len, ==, 1);
567 g_assert_cmpint (data.num_connection_attempts, ==, 1);
568 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
569 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
571 /* check that we create a proxy, read properties, receive signals and invoke
572 * the HelloPeer() method. Since the server runs in another thread it's fine
573 * to use synchronous blocking API here.
576 proxy = g_dbus_proxy_new_sync (c,
577 G_DBUS_PROXY_FLAGS_NONE,
580 "/org/gtk/GDBus/PeerTestObject",
581 "org.gtk.GDBus.PeerTestInterface",
582 NULL, /* GCancellable */
584 g_assert_no_error (error);
585 g_assert (proxy != NULL);
587 value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
588 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
590 /* try invoking a method */
592 result = g_dbus_proxy_call_sync (proxy,
594 g_variant_new ("(s)", "Hey Peer!"),
595 G_DBUS_CALL_FLAGS_NONE,
597 NULL, /* GCancellable */
599 g_assert_no_error (error);
600 g_variant_get (result, "(&s)", &s);
601 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
602 g_variant_unref (result);
603 g_assert_cmpint (data.num_method_calls, ==, 1);
605 /* make the other peer emit a signal - catch it */
606 signal_handler_id = g_signal_connect (proxy,
608 G_CALLBACK (on_proxy_signal_received),
610 g_assert (!data.signal_received);
611 g_dbus_proxy_call (proxy,
613 NULL, /* no arguments */
614 G_DBUS_CALL_FLAGS_NONE,
616 NULL, /* GCancellable */
617 NULL, /* GAsyncReadyCallback - we don't care about the result */
618 NULL); /* user_data */
619 g_main_loop_run (loop);
620 g_assert (data.signal_received);
621 g_assert_cmpint (data.num_method_calls, ==, 2);
622 g_signal_handler_disconnect (proxy, signal_handler_id);
624 /* Also ensure that messages with the sender header-field set gets
625 * delivered to the proxy - note that this doesn't really make sense
626 * e.g. names are meaning-less in a peer-to-peer case... but we
627 * support it because it makes sense in certain bridging
628 * applications - see e.g. #623815.
630 signal_handler_id = g_signal_connect (proxy,
632 G_CALLBACK (on_proxy_signal_received_with_name_set),
634 data.signal_received = FALSE;
635 g_dbus_proxy_call (proxy,
636 "EmitSignalWithNameSet",
637 NULL, /* no arguments */
638 G_DBUS_CALL_FLAGS_NONE,
640 NULL, /* GCancellable */
641 NULL, /* GAsyncReadyCallback - we don't care about the result */
642 NULL); /* user_data */
643 g_main_loop_run (loop);
644 g_assert (data.signal_received);
645 g_assert_cmpint (data.num_method_calls, ==, 3);
646 g_signal_handler_disconnect (proxy, signal_handler_id);
648 /* check for UNIX fd passing */
651 GDBusMessage *method_call_message;
652 GDBusMessage *method_reply_message;
653 GUnixFDList *fd_list;
660 method_call_message = g_dbus_message_new_method_call (NULL, /* name */
661 "/org/gtk/GDBus/PeerTestObject",
662 "org.gtk.GDBus.PeerTestInterface",
664 g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", "/etc/hosts"));
666 method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
668 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
670 NULL, /* out_serial */
671 NULL, /* cancellable */
673 g_assert_no_error (error);
674 g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
675 fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
676 g_assert (fd_list != NULL);
677 g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
679 fd = g_unix_fd_list_get (fd_list, 0, &error);
680 g_assert_no_error (error);
681 g_object_unref (method_call_message);
682 g_object_unref (method_reply_message);
684 memset (buf, '\0', sizeof (buf));
685 len = read (fd, buf, sizeof (buf) - 1);
689 g_file_get_contents ("/etc/hosts",
693 g_assert_no_error (error);
694 if (len2 > sizeof (buf))
695 buf2[sizeof (buf)] = '\0';
696 g_assert_cmpstr (buf, ==, buf2);
701 result = g_dbus_proxy_call_sync (proxy,
703 g_variant_new ("(s)", "boo"),
704 G_DBUS_CALL_FLAGS_NONE,
706 NULL, /* GCancellable */
708 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
709 g_assert (result == NULL);
710 g_error_free (error);
711 #endif /* G_OS_UNIX */
713 /* Check that g_socket_get_credentials() work - this really should
714 * be in a GSocket-specific test suite but no such test suite exists
719 GCredentials *credentials;
720 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (g_dbus_connection_get_stream (c)));
721 g_assert (G_IS_SOCKET (socket));
723 credentials = g_socket_get_credentials (socket, &error);
726 struct ucred *native_creds;
727 g_assert_no_error (error);
728 g_assert (G_IS_CREDENTIALS (credentials));
729 native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED);
730 g_assert (native_creds != NULL);
731 g_assert (native_creds->uid == getuid ());
732 g_assert (native_creds->gid == getgid ());
733 g_assert (native_creds->pid == getpid ());
735 g_object_unref (credentials);
737 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
738 g_assert (credentials == NULL);
743 /* bring up a connection - don't accept it - this should fail
745 data.accept_connection = FALSE;
747 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
748 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
749 NULL, /* GDBusAuthObserver */
750 NULL, /* cancellable */
752 _g_assert_error_domain (error, G_IO_ERROR);
753 g_assert (c2 == NULL);
756 /* TODO: THIS TEST DOESN'T WORK YET */
758 /* bring up a connection - accept it.. then disconnect from the client side - check
759 * that the server side gets the disconnect signal.
762 data.accept_connection = TRUE;
763 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
764 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
765 NULL, /* GDBusAuthObserver */
766 NULL, /* cancellable */
768 g_assert_no_error (error);
769 g_assert (c2 != NULL);
770 g_assert (!g_dbus_connection_get_is_disconnected (c2));
771 while (data.num_connection_attempts < 3)
772 g_main_loop_run (loop);
773 g_assert_cmpint (data.current_connections->len, ==, 2);
774 g_assert_cmpint (data.num_connection_attempts, ==, 3);
775 g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
776 g_idle_add (on_do_disconnect_in_idle, c2);
777 g_debug ("==================================================");
778 g_debug ("==================================================");
779 g_debug ("==================================================");
780 g_debug ("waiting for disconnect on connection %p, stream %p",
781 data.current_connections->pdata[1],
782 g_dbus_connection_get_stream (data.current_connections->pdata[1]));
784 g_timeout_add (2000, check_connection, &data);
785 //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
786 g_main_loop_run (loop);
787 g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
788 g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
791 /* unref the server and stop listening for new connections
793 * This won't bring down the established connections - check that c is still connected
794 * by invoking a method
796 //g_socket_service_stop (service);
797 //g_object_unref (service);
798 g_dbus_server_stop (server);
799 g_object_unref (server);
803 result = g_dbus_proxy_call_sync (proxy,
805 g_variant_new ("(s)", "Hey Again Peer!"),
806 G_DBUS_CALL_FLAGS_NONE,
808 NULL, /* GCancellable */
810 g_assert_no_error (error);
811 g_variant_get (result, "(&s)", &s);
812 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
813 g_variant_unref (result);
814 g_assert_cmpint (data.num_method_calls, ==, 5);
817 /* TODO: THIS TEST DOESN'T WORK YET */
819 /* now disconnect from the server side - check that the client side gets the signal */
820 g_assert_cmpint (data.current_connections->len, ==, 1);
821 g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
822 g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
823 if (!g_dbus_connection_get_is_disconnected (c))
824 _g_assert_signal_received (c, "closed");
825 g_assert (g_dbus_connection_get_is_disconnected (c));
829 g_ptr_array_unref (data.current_connections);
830 g_object_unref (proxy);
832 g_main_loop_quit (service_loop);
833 g_thread_join (service_thread);
836 /* ---------------------------------------------------------------------------------------------------- */
841 GMainContext *context;
848 dmp_data_free (DmpData *data)
850 g_main_loop_unref (data->loop);
851 g_main_context_unref (data->context);
852 g_object_unref (data->server);
853 g_list_foreach (data->connections, (GFunc) g_object_unref, NULL);
854 g_list_free (data->connections);
859 dmp_on_method_call (GDBusConnection *connection,
861 const gchar *object_path,
862 const gchar *interface_name,
863 const gchar *method_name,
864 GVariant *parameters,
865 GDBusMethodInvocation *invocation,
868 //DmpData *data = user_data;
871 g_variant_get (parameters,
875 g_dbus_method_invocation_return_value (invocation,
876 g_variant_new ("(i)", first + second));
879 static const GDBusInterfaceVTable dmp_interface_vtable =
882 NULL, /* get_property */
883 NULL /* set_property */
887 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
889 dmp_on_new_connection (GDBusServer *server,
890 GDBusConnection *connection,
893 DmpData *data = user_data;
897 /* accept the connection */
898 data->connections = g_list_prepend (data->connections, g_object_ref (connection));
901 node = g_dbus_node_info_new_for_xml ("<node>"
902 " <interface name='org.gtk.GDBus.DmpInterface'>"
903 " <method name='AddPair'>"
904 " <arg type='i' name='first' direction='in'/>"
905 " <arg type='i' name='second' direction='in'/>"
906 " <arg type='i' name='sum' direction='out'/>"
911 g_assert_no_error (error);
913 /* sleep 100ms before exporting an object - this is to test that
914 * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
915 * (GDBusServer uses this feature).
919 /* export an object */
921 g_dbus_connection_register_object (connection,
924 &dmp_interface_vtable,
928 g_dbus_node_info_unref (node);
932 dmp_thread_func (gpointer user_data)
934 DmpData *data = user_data;
938 data->context = g_main_context_new ();
939 g_main_context_push_thread_default (data->context);
942 guid = g_dbus_generate_guid ();
943 data->server = g_dbus_server_new_sync ("unix:tmpdir=/tmp/gdbus-test-",
944 G_DBUS_SERVER_FLAGS_NONE,
946 NULL, /* GDBusAuthObserver */
947 NULL, /* GCancellable */
949 g_assert_no_error (error);
950 g_signal_connect (data->server,
952 G_CALLBACK (dmp_on_new_connection),
955 g_dbus_server_start (data->server);
957 data->loop = g_main_loop_new (data->context, FALSE);
958 g_main_loop_run (data->loop);
960 g_main_context_pop_thread_default (data->context);
967 delayed_message_processing (void)
971 GThread *service_thread;
974 data = g_new0 (DmpData, 1);
977 service_thread = g_thread_create (dmp_thread_func,
981 while (data->server == NULL || !g_dbus_server_is_active (data->server))
984 for (n = 0; n < 5; n++)
991 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
992 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
993 NULL, /* GDBusAuthObserver */
994 NULL, /* GCancellable */
996 g_assert_no_error (error);
999 res = g_dbus_connection_call_sync (c,
1000 NULL, /* bus name */
1002 "org.gtk.GDBus.DmpInterface",
1004 g_variant_new ("(ii)", 2, n),
1005 G_VARIANT_TYPE ("(i)"),
1006 G_DBUS_CALL_FLAGS_NONE,
1007 -1, /* timeout_msec */
1008 NULL, /* GCancellable */
1010 g_assert_no_error (error);
1011 g_variant_get (res, "(i)", &val);
1012 g_assert_cmpint (val, ==, 2 + n);
1013 g_variant_unref (res);
1017 g_main_loop_quit (data->loop);
1018 g_thread_join (service_thread);
1019 dmp_data_free (data);
1022 /* ---------------------------------------------------------------------------------------------------- */
1025 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1027 GCredentials *credentials,
1030 PeerData *data = user_data;
1031 gboolean authorized;
1033 data->num_connection_attempts++;
1036 if (!data->accept_connection)
1039 g_main_loop_quit (loop);
1045 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1047 nonce_tcp_on_new_connection (GDBusServer *server,
1048 GDBusConnection *connection,
1051 PeerData *data = user_data;
1053 g_ptr_array_add (data->current_connections, g_object_ref (connection));
1055 g_main_loop_quit (loop);
1059 nonce_tcp_service_thread_func (gpointer user_data)
1061 PeerData *data = user_data;
1062 GMainContext *service_context;
1063 GDBusAuthObserver *observer;
1066 service_context = g_main_context_new ();
1067 g_main_context_push_thread_default (service_context);
1070 observer = g_dbus_auth_observer_new ();
1071 server = g_dbus_server_new_sync ("nonce-tcp:",
1072 G_DBUS_SERVER_FLAGS_NONE,
1075 NULL, /* cancellable */
1077 g_assert_no_error (error);
1079 g_signal_connect (server,
1081 G_CALLBACK (nonce_tcp_on_new_connection),
1083 g_signal_connect (observer,
1084 "authorize-authenticated-peer",
1085 G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1087 g_object_unref (observer);
1089 g_dbus_server_start (server);
1091 service_loop = g_main_loop_new (service_context, FALSE);
1092 g_main_loop_run (service_loop);
1094 g_main_context_pop_thread_default (service_context);
1096 g_main_loop_unref (service_loop);
1097 g_main_context_unref (service_context);
1099 /* test code specifically unrefs the server - see below */
1100 g_assert (server == NULL);
1106 test_nonce_tcp (void)
1110 GThread *service_thread;
1115 const gchar *address;
1117 memset (&data, '\0', sizeof (PeerData));
1118 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1122 service_loop = NULL;
1123 service_thread = g_thread_create (nonce_tcp_service_thread_func,
1127 while (service_loop == NULL)
1129 g_assert (server != NULL);
1132 /* bring up a connection and accept it */
1133 data.accept_connection = TRUE;
1135 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1136 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1137 NULL, /* GDBusAuthObserver */
1138 NULL, /* cancellable */
1140 g_assert_no_error (error);
1141 g_assert (c != NULL);
1142 while (data.current_connections->len < 1)
1143 g_main_loop_run (loop);
1144 g_assert_cmpint (data.current_connections->len, ==, 1);
1145 g_assert_cmpint (data.num_connection_attempts, ==, 1);
1146 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1147 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1150 /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1153 address = g_dbus_server_get_client_address (server);
1155 s = strstr (address, "noncefile=");
1156 g_assert (s != NULL);
1157 s += sizeof "noncefile=" - 1;
1158 nonce_file = g_strdup (s);
1160 /* First try invalid data in the nonce file - this will actually
1161 * make the client send this and the server will reject it. The way
1162 * it works is that if the nonce doesn't match, the server will
1163 * simply close the connection. So, from the client point of view,
1164 * we can see a variety of errors.
1167 res = g_file_set_contents (nonce_file,
1171 g_assert_no_error (error);
1173 c = g_dbus_connection_new_for_address_sync (address,
1174 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1175 NULL, /* GDBusAuthObserver */
1176 NULL, /* cancellable */
1178 _g_assert_error_domain (error, G_IO_ERROR);
1179 g_assert (c == NULL);
1181 /* Then try with a nonce-file of incorrect length - this will make
1182 * the client complain - we won't even try connecting to the server
1186 res = g_file_set_contents (nonce_file,
1187 "0123456789012345_",
1190 g_assert_no_error (error);
1192 c = g_dbus_connection_new_for_address_sync (address,
1193 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1194 NULL, /* GDBusAuthObserver */
1195 NULL, /* cancellable */
1197 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1198 g_assert (c == NULL);
1200 /* Finally try with no nonce-file at all */
1201 g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1203 c = g_dbus_connection_new_for_address_sync (address,
1204 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1205 NULL, /* GDBusAuthObserver */
1206 NULL, /* cancellable */
1208 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1209 g_assert (c == NULL);
1211 g_free (nonce_file);
1213 g_dbus_server_stop (server);
1214 g_object_unref (server);
1217 g_main_loop_quit (service_loop);
1218 g_thread_join (service_thread);
1222 test_credentials (void)
1224 GCredentials *c1, *c2;
1228 c1 = g_credentials_new ();
1229 c2 = g_credentials_new ();
1232 if (g_credentials_set_unix_user (c2, getuid (), &error))
1233 g_assert_no_error (error);
1235 g_clear_error (&error);
1236 g_assert (g_credentials_is_same_user (c1, c2, &error));
1237 g_assert_no_error (error);
1239 desc = g_credentials_to_string (c1);
1240 g_assert (desc != NULL);
1243 g_object_unref (c1);
1244 g_object_unref (c2);
1247 /* ---------------------------------------------------------------------------------------------------- */
1254 GDBusNodeInfo *introspection_data = NULL;
1257 g_thread_init (NULL);
1258 g_test_init (&argc, &argv, NULL);
1260 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1261 g_assert (introspection_data != NULL);
1262 test_interface_introspection_data = introspection_data->interfaces[0];
1264 test_guid = g_dbus_generate_guid ();
1266 /* all the tests rely on a shared main loop */
1267 loop = g_main_loop_new (NULL, FALSE);
1269 g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1270 g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1271 g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1272 g_test_add_func ("/gdbus/credentials", test_credentials);
1276 g_main_loop_unref (loop);
1278 g_dbus_node_info_unref (introspection_data);