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 #include "gdbus-tests.h"
42 static gboolean is_unix = TRUE;
44 static gboolean is_unix = FALSE;
47 static gchar *test_guid = NULL;
48 static GMainLoop *service_loop = NULL;
49 static GDBusServer *server = NULL;
50 static GMainLoop *loop = NULL;
52 /* ---------------------------------------------------------------------------------------------------- */
53 /* Test that peer-to-peer connections work */
54 /* ---------------------------------------------------------------------------------------------------- */
59 gboolean accept_connection;
60 gint num_connection_attempts;
61 GPtrArray *current_connections;
62 guint num_method_calls;
63 gboolean signal_received;
66 static const gchar *test_interface_introspection_xml =
68 " <interface name='org.gtk.GDBus.PeerTestInterface'>"
69 " <method name='HelloPeer'>"
70 " <arg type='s' name='greeting' direction='in'/>"
71 " <arg type='s' name='response' direction='out'/>"
73 " <method name='EmitSignal'/>"
74 " <method name='EmitSignalWithNameSet'/>"
75 " <method name='OpenFile'>"
76 " <arg type='s' name='path' direction='in'/>"
78 " <signal name='PeerSignal'>"
79 " <arg type='s' name='a_string'/>"
81 " <property type='s' name='PeerProperty' access='read'/>"
84 static GDBusInterfaceInfo *test_interface_introspection_data = NULL;
87 test_interface_method_call (GDBusConnection *connection,
89 const gchar *object_path,
90 const gchar *interface_name,
91 const gchar *method_name,
93 GDBusMethodInvocation *invocation,
96 PeerData *data = user_data;
98 data->num_method_calls++;
100 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
101 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
103 if (g_strcmp0 (method_name, "HelloPeer") == 0)
105 const gchar *greeting;
108 g_variant_get (parameters, "(&s)", &greeting);
110 response = g_strdup_printf ("You greeted me with '%s'.",
112 g_dbus_method_invocation_return_value (invocation,
113 g_variant_new ("(s)", response));
116 else if (g_strcmp0 (method_name, "EmitSignal") == 0)
121 g_dbus_connection_emit_signal (connection,
123 "/org/gtk/GDBus/PeerTestObject",
124 "org.gtk.GDBus.PeerTestInterface",
128 g_assert_no_error (error);
129 g_dbus_method_invocation_return_value (invocation, NULL);
131 else if (g_strcmp0 (method_name, "EmitSignalWithNameSet") == 0)
135 GDBusMessage *message;
137 message = g_dbus_message_new_signal ("/org/gtk/GDBus/PeerTestObject",
138 "org.gtk.GDBus.PeerTestInterface",
139 "PeerSignalWithNameSet");
140 g_dbus_message_set_sender (message, ":1.42");
143 ret = g_dbus_connection_send_message (connection, message, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error);
144 g_assert_no_error (error);
146 g_object_unref (message);
148 g_dbus_method_invocation_return_value (invocation, NULL);
150 else if (g_strcmp0 (method_name, "OpenFile") == 0)
157 GUnixFDList *fd_list;
159 g_variant_get (parameters, "(&s)", &path);
161 fd_list = g_unix_fd_list_new ();
165 fd = open (path, O_RDONLY);
166 g_unix_fd_list_append (fd_list, fd, &error);
167 g_assert_no_error (error);
170 reply = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
171 g_dbus_message_set_unix_fd_list (reply, fd_list);
172 g_object_unref (invocation);
175 g_dbus_connection_send_message (connection,
177 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
178 NULL, /* out_serial */
180 g_assert_no_error (error);
181 g_object_unref (reply);
183 g_dbus_method_invocation_return_dbus_error (invocation,
184 "org.gtk.GDBus.NotOnUnix",
185 "Your OS does not support file descriptor passing");
190 g_assert_not_reached ();
195 test_interface_get_property (GDBusConnection *connection,
197 const gchar *object_path,
198 const gchar *interface_name,
199 const gchar *property_name,
203 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
204 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
205 g_assert_cmpstr (property_name, ==, "PeerProperty");
207 return g_variant_new_string ("ThePropertyValue");
211 static const GDBusInterfaceVTable test_interface_vtable =
213 test_interface_method_call,
214 test_interface_get_property,
215 NULL /* set_property */
219 on_proxy_signal_received (GDBusProxy *proxy,
222 GVariant *parameters,
225 PeerData *data = user_data;
227 data->signal_received = TRUE;
229 g_assert (sender_name == NULL);
230 g_assert_cmpstr (signal_name, ==, "PeerSignal");
231 g_main_loop_quit (loop);
235 on_proxy_signal_received_with_name_set (GDBusProxy *proxy,
238 GVariant *parameters,
241 PeerData *data = user_data;
243 data->signal_received = TRUE;
245 g_assert_cmpstr (sender_name, ==, ":1.42");
246 g_assert_cmpstr (signal_name, ==, "PeerSignalWithNameSet");
247 g_main_loop_quit (loop);
250 /* ---------------------------------------------------------------------------------------------------- */
253 on_authorize_authenticated_peer (GDBusAuthObserver *observer,
255 GCredentials *credentials,
258 PeerData *data = user_data;
261 data->num_connection_attempts++;
264 if (!data->accept_connection)
267 g_main_loop_quit (loop);
273 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
275 on_new_connection (GDBusServer *server,
276 GDBusConnection *connection,
279 PeerData *data = user_data;
283 //g_print ("Client connected.\n"
284 // "Negotiated capabilities: unix-fd-passing=%d\n",
285 // g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
287 g_ptr_array_add (data->current_connections, g_object_ref (connection));
289 /* export object on the newly established connection */
291 reg_id = g_dbus_connection_register_object (connection,
292 "/org/gtk/GDBus/PeerTestObject",
293 test_interface_introspection_data,
294 &test_interface_vtable,
296 NULL, /* GDestroyNotify for data */
298 g_assert_no_error (error);
299 g_assert (reg_id > 0);
301 g_main_loop_quit (loop);
305 service_thread_func (gpointer user_data)
307 PeerData *data = user_data;
308 GMainContext *service_context;
309 GDBusAuthObserver *observer;
312 service_context = g_main_context_new ();
313 g_main_context_push_thread_default (service_context);
316 observer = g_dbus_auth_observer_new ();
317 server = g_dbus_server_new_sync (is_unix ? "unix:tmpdir=/tmp/gdbus-test-" : "nonce-tcp:",
318 G_DBUS_SERVER_FLAGS_NONE,
321 NULL, /* cancellable */
323 g_assert_no_error (error);
325 g_signal_connect (server,
327 G_CALLBACK (on_new_connection),
329 g_signal_connect (observer,
330 "authorize-authenticated-peer",
331 G_CALLBACK (on_authorize_authenticated_peer),
333 g_object_unref (observer);
335 g_dbus_server_start (server);
337 service_loop = g_main_loop_new (service_context, FALSE);
338 g_main_loop_run (service_loop);
340 g_main_context_pop_thread_default (service_context);
342 g_main_loop_unref (service_loop);
343 g_main_context_unref (service_context);
345 /* test code specifically unrefs the server - see below */
346 g_assert (server == NULL);
353 on_incoming_connection (GSocketService *service,
354 GSocketConnection *socket_connection,
355 GObject *source_object,
358 PeerData *data = user_data;
360 if (data->accept_connection)
364 GDBusConnection *connection;
367 connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
369 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
370 NULL, /* cancellable */
372 g_assert_no_error (error);
374 g_ptr_array_add (data->current_connections, connection);
376 /* export object on the newly established connection */
378 reg_id = g_dbus_connection_register_object (connection,
379 "/org/gtk/GDBus/PeerTestObject",
380 &test_interface_introspection_data,
381 &test_interface_vtable,
383 NULL, /* GDestroyNotify for data */
385 g_assert_no_error (error);
386 g_assert (reg_id > 0);
391 /* don't do anything */
394 data->num_connection_attempts++;
396 g_main_loop_quit (loop);
398 /* stops other signal handlers from being invoked */
403 service_thread_func (gpointer data)
405 GMainContext *service_context;
407 GSocketAddress *address;
410 service_context = g_main_context_new ();
411 g_main_context_push_thread_default (service_context);
413 socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
414 address = g_unix_socket_address_new (socket_path);
416 service = g_socket_service_new ();
418 g_socket_listener_add_address (G_SOCKET_LISTENER (service),
420 G_SOCKET_TYPE_STREAM,
421 G_SOCKET_PROTOCOL_DEFAULT,
422 NULL, /* source_object */
423 NULL, /* effective_address */
425 g_assert_no_error (error);
426 g_signal_connect (service,
428 G_CALLBACK (on_incoming_connection),
430 g_socket_service_start (service);
432 service_loop = g_main_loop_new (service_context, FALSE);
433 g_main_loop_run (service_loop);
435 g_main_context_pop_thread_default (service_context);
437 g_main_loop_unref (service_loop);
438 g_main_context_unref (service_context);
440 g_object_unref (address);
441 g_free (socket_path);
446 /* ---------------------------------------------------------------------------------------------------- */
450 check_connection (gpointer user_data)
452 PeerData *data = user_data;
455 for (n = 0; n < data->current_connections->len; n++)
460 c = G_DBUS_CONNECTION (data->current_connections->pdata[n]);
461 stream = g_dbus_connection_get_stream (c);
463 g_debug ("In check_connection for %d: connection %p, stream %p", n, c, stream);
464 g_debug ("closed = %d", g_io_stream_is_closed (stream));
467 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
468 g_debug ("socket_closed = %d", g_socket_is_closed (socket));
469 g_debug ("socket_condition_check = %d", g_socket_condition_check (socket, G_IO_IN|G_IO_OUT|G_IO_ERR|G_IO_HUP));
475 num_read = g_input_stream_read (g_io_stream_get_input_stream (stream),
482 g_debug ("error: %s", error->message);
483 g_error_free (error);
487 g_debug ("no error, read %d bytes", (gint) num_read);
495 on_do_disconnect_in_idle (gpointer data)
497 GDBusConnection *c = G_DBUS_CONNECTION (data);
498 g_debug ("GDC %p has ref_count %d", c, G_OBJECT (c)->ref_count);
499 g_dbus_connection_disconnect (c);
516 GThread *service_thread;
517 gulong signal_handler_id;
519 memset (&data, '\0', sizeof (PeerData));
520 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
522 /* first try to connect when there is no server */
524 c = g_dbus_connection_new_for_address_sync (is_unix ? "unix:path=/tmp/gdbus-test-does-not-exist-pid" :
525 /* NOTE: Even if something is listening on port 12345 the connection
526 * will fail because the nonce file doesn't exist */
527 "nonce-tcp:host=localhost,port=12345,noncefile=this-does-not-exist-gdbus",
528 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
529 NULL, /* GDBusAuthObserver */
530 NULL, /* cancellable */
532 _g_assert_error_domain (error, G_IO_ERROR);
533 g_assert (!g_dbus_error_is_remote_error (error));
534 g_clear_error (&error);
535 g_assert (c == NULL);
537 /* bring up a server - we run the server in a different thread to avoid deadlocks */
539 service_thread = g_thread_create (service_thread_func,
543 while (service_loop == NULL)
545 g_assert (server != NULL);
547 /* bring up a connection and accept it */
548 data.accept_connection = TRUE;
550 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
551 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
552 NULL, /* GDBusAuthObserver */
553 NULL, /* cancellable */
555 g_assert_no_error (error);
556 g_assert (c != NULL);
557 while (data.current_connections->len < 1)
558 g_main_loop_run (loop);
559 g_assert_cmpint (data.current_connections->len, ==, 1);
560 g_assert_cmpint (data.num_connection_attempts, ==, 1);
561 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
562 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
564 /* check that we create a proxy, read properties, receive signals and invoke
565 * the HelloPeer() method. Since the server runs in another thread it's fine
566 * to use synchronous blocking API here.
569 proxy = g_dbus_proxy_new_sync (c,
570 G_DBUS_PROXY_FLAGS_NONE,
573 "/org/gtk/GDBus/PeerTestObject",
574 "org.gtk.GDBus.PeerTestInterface",
575 NULL, /* GCancellable */
577 g_assert_no_error (error);
578 g_assert (proxy != NULL);
580 value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
581 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
583 /* try invoking a method */
585 result = g_dbus_proxy_call_sync (proxy,
587 g_variant_new ("(s)", "Hey Peer!"),
588 G_DBUS_CALL_FLAGS_NONE,
590 NULL, /* GCancellable */
592 g_assert_no_error (error);
593 g_variant_get (result, "(&s)", &s);
594 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
595 g_variant_unref (result);
596 g_assert_cmpint (data.num_method_calls, ==, 1);
598 /* make the other peer emit a signal - catch it */
599 signal_handler_id = g_signal_connect (proxy,
601 G_CALLBACK (on_proxy_signal_received),
603 g_assert (!data.signal_received);
604 g_dbus_proxy_call (proxy,
606 NULL, /* no arguments */
607 G_DBUS_CALL_FLAGS_NONE,
609 NULL, /* GCancellable */
610 NULL, /* GAsyncReadyCallback - we don't care about the result */
611 NULL); /* user_data */
612 g_main_loop_run (loop);
613 g_assert (data.signal_received);
614 g_assert_cmpint (data.num_method_calls, ==, 2);
615 g_signal_handler_disconnect (proxy, signal_handler_id);
617 /* Also ensure that messages with the sender header-field set gets
618 * delivered to the proxy - note that this doesn't really make sense
619 * e.g. names are meaning-less in a peer-to-peer case... but we
620 * support it because it makes sense in certain bridging
621 * applications - see e.g. #623815.
623 signal_handler_id = g_signal_connect (proxy,
625 G_CALLBACK (on_proxy_signal_received_with_name_set),
627 data.signal_received = FALSE;
628 g_dbus_proxy_call (proxy,
629 "EmitSignalWithNameSet",
630 NULL, /* no arguments */
631 G_DBUS_CALL_FLAGS_NONE,
633 NULL, /* GCancellable */
634 NULL, /* GAsyncReadyCallback - we don't care about the result */
635 NULL); /* user_data */
636 g_main_loop_run (loop);
637 g_assert (data.signal_received);
638 g_assert_cmpint (data.num_method_calls, ==, 3);
639 g_signal_handler_disconnect (proxy, signal_handler_id);
641 /* check for UNIX fd passing */
644 GDBusMessage *method_call_message;
645 GDBusMessage *method_reply_message;
646 GUnixFDList *fd_list;
653 method_call_message = g_dbus_message_new_method_call (NULL, /* name */
654 "/org/gtk/GDBus/PeerTestObject",
655 "org.gtk.GDBus.PeerTestInterface",
657 g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", "/etc/hosts"));
659 method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
661 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
663 NULL, /* out_serial */
664 NULL, /* cancellable */
666 g_assert_no_error (error);
667 g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
668 fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
669 g_assert (fd_list != NULL);
670 g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
672 fd = g_unix_fd_list_get (fd_list, 0, &error);
673 g_assert_no_error (error);
674 g_object_unref (method_call_message);
675 g_object_unref (method_reply_message);
677 memset (buf, '\0', sizeof (buf));
678 len = read (fd, buf, sizeof (buf) - 1);
682 g_file_get_contents ("/etc/hosts",
686 g_assert_no_error (error);
687 if (len2 > sizeof (buf))
688 buf2[sizeof (buf)] = '\0';
689 g_assert_cmpstr (buf, ==, buf2);
694 result = g_dbus_proxy_call_sync (proxy,
696 g_variant_new ("(s)", "boo"),
697 G_DBUS_CALL_FLAGS_NONE,
699 NULL, /* GCancellable */
701 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
702 g_assert (result == NULL);
703 g_error_free (error);
704 #endif /* G_OS_UNIX */
707 /* bring up a connection - don't accept it - this should fail
709 data.accept_connection = FALSE;
711 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
712 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
713 NULL, /* GDBusAuthObserver */
714 NULL, /* cancellable */
716 _g_assert_error_domain (error, G_IO_ERROR);
717 g_assert (c2 == NULL);
720 /* TODO: THIS TEST DOESN'T WORK YET */
722 /* bring up a connection - accept it.. then disconnect from the client side - check
723 * that the server side gets the disconnect signal.
726 data.accept_connection = TRUE;
727 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
728 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
729 NULL, /* GDBusAuthObserver */
730 NULL, /* cancellable */
732 g_assert_no_error (error);
733 g_assert (c2 != NULL);
734 g_assert (!g_dbus_connection_get_is_disconnected (c2));
735 while (data.num_connection_attempts < 3)
736 g_main_loop_run (loop);
737 g_assert_cmpint (data.current_connections->len, ==, 2);
738 g_assert_cmpint (data.num_connection_attempts, ==, 3);
739 g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
740 g_idle_add (on_do_disconnect_in_idle, c2);
741 g_debug ("==================================================");
742 g_debug ("==================================================");
743 g_debug ("==================================================");
744 g_debug ("waiting for disconnect on connection %p, stream %p",
745 data.current_connections->pdata[1],
746 g_dbus_connection_get_stream (data.current_connections->pdata[1]));
748 g_timeout_add (2000, check_connection, &data);
749 //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
750 g_main_loop_run (loop);
751 g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
752 g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
755 /* unref the server and stop listening for new connections
757 * This won't bring down the established connections - check that c is still connected
758 * by invoking a method
760 //g_socket_service_stop (service);
761 //g_object_unref (service);
762 g_dbus_server_stop (server);
763 g_object_unref (server);
767 result = g_dbus_proxy_call_sync (proxy,
769 g_variant_new ("(s)", "Hey Again Peer!"),
770 G_DBUS_CALL_FLAGS_NONE,
772 NULL, /* GCancellable */
774 g_assert_no_error (error);
775 g_variant_get (result, "(&s)", &s);
776 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
777 g_variant_unref (result);
778 g_assert_cmpint (data.num_method_calls, ==, 5);
781 /* TODO: THIS TEST DOESN'T WORK YET */
783 /* now disconnect from the server side - check that the client side gets the signal */
784 g_assert_cmpint (data.current_connections->len, ==, 1);
785 g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
786 g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
787 if (!g_dbus_connection_get_is_disconnected (c))
788 _g_assert_signal_received (c, "closed");
789 g_assert (g_dbus_connection_get_is_disconnected (c));
793 g_ptr_array_unref (data.current_connections);
794 g_object_unref (proxy);
796 g_main_loop_quit (service_loop);
797 g_thread_join (service_thread);
800 /* ---------------------------------------------------------------------------------------------------- */
805 GMainContext *context;
812 dmp_data_free (DmpData *data)
814 g_main_loop_unref (data->loop);
815 g_main_context_unref (data->context);
816 g_object_unref (data->server);
817 g_list_foreach (data->connections, (GFunc) g_object_unref, NULL);
818 g_list_free (data->connections);
823 dmp_on_method_call (GDBusConnection *connection,
825 const gchar *object_path,
826 const gchar *interface_name,
827 const gchar *method_name,
828 GVariant *parameters,
829 GDBusMethodInvocation *invocation,
832 //DmpData *data = user_data;
835 g_variant_get (parameters,
839 g_dbus_method_invocation_return_value (invocation,
840 g_variant_new ("(i)", first + second));
843 static const GDBusInterfaceVTable dmp_interface_vtable =
846 NULL, /* get_property */
847 NULL /* set_property */
851 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
853 dmp_on_new_connection (GDBusServer *server,
854 GDBusConnection *connection,
857 DmpData *data = user_data;
861 /* accept the connection */
862 data->connections = g_list_prepend (data->connections, g_object_ref (connection));
865 node = g_dbus_node_info_new_for_xml ("<node>"
866 " <interface name='org.gtk.GDBus.DmpInterface'>"
867 " <method name='AddPair'>"
868 " <arg type='i' name='first' direction='in'/>"
869 " <arg type='i' name='second' direction='in'/>"
870 " <arg type='i' name='sum' direction='out'/>"
875 g_assert_no_error (error);
877 /* sleep 100ms before exporting an object - this is to test that
878 * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
879 * (GDBusServer uses this feature).
883 /* export an object */
885 g_dbus_connection_register_object (connection,
888 &dmp_interface_vtable,
892 g_dbus_node_info_unref (node);
896 dmp_thread_func (gpointer user_data)
898 DmpData *data = user_data;
902 data->context = g_main_context_new ();
903 g_main_context_push_thread_default (data->context);
906 guid = g_dbus_generate_guid ();
907 data->server = g_dbus_server_new_sync ("unix:tmpdir=/tmp/gdbus-test-",
908 G_DBUS_SERVER_FLAGS_NONE,
910 NULL, /* GDBusAuthObserver */
911 NULL, /* GCancellable */
913 g_assert_no_error (error);
914 g_signal_connect (data->server,
916 G_CALLBACK (dmp_on_new_connection),
919 g_dbus_server_start (data->server);
921 data->loop = g_main_loop_new (data->context, FALSE);
922 g_main_loop_run (data->loop);
924 g_main_context_pop_thread_default (data->context);
931 delayed_message_processing (void)
935 GThread *service_thread;
938 data = g_new0 (DmpData, 1);
941 service_thread = g_thread_create (dmp_thread_func,
945 while (data->server == NULL || !g_dbus_server_is_active (data->server))
948 for (n = 0; n < 5; n++)
955 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
956 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
957 NULL, /* GDBusAuthObserver */
958 NULL, /* GCancellable */
960 g_assert_no_error (error);
963 res = g_dbus_connection_call_sync (c,
966 "org.gtk.GDBus.DmpInterface",
968 g_variant_new ("(ii)", 2, n),
969 G_VARIANT_TYPE ("(i)"),
970 G_DBUS_CALL_FLAGS_NONE,
971 -1, /* timeout_msec */
972 NULL, /* GCancellable */
974 g_assert_no_error (error);
975 g_variant_get (res, "(i)", &val);
976 g_assert_cmpint (val, ==, 2 + n);
977 g_variant_unref (res);
981 g_main_loop_quit (data->loop);
982 g_thread_join (service_thread);
983 dmp_data_free (data);
986 /* ---------------------------------------------------------------------------------------------------- */
989 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
991 GCredentials *credentials,
994 PeerData *data = user_data;
997 data->num_connection_attempts++;
1000 if (!data->accept_connection)
1003 g_main_loop_quit (loop);
1009 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1011 nonce_tcp_on_new_connection (GDBusServer *server,
1012 GDBusConnection *connection,
1015 PeerData *data = user_data;
1017 g_ptr_array_add (data->current_connections, g_object_ref (connection));
1019 g_main_loop_quit (loop);
1023 nonce_tcp_service_thread_func (gpointer user_data)
1025 PeerData *data = user_data;
1026 GMainContext *service_context;
1027 GDBusAuthObserver *observer;
1030 service_context = g_main_context_new ();
1031 g_main_context_push_thread_default (service_context);
1034 observer = g_dbus_auth_observer_new ();
1035 server = g_dbus_server_new_sync ("nonce-tcp:",
1036 G_DBUS_SERVER_FLAGS_NONE,
1039 NULL, /* cancellable */
1041 g_assert_no_error (error);
1043 g_signal_connect (server,
1045 G_CALLBACK (nonce_tcp_on_new_connection),
1047 g_signal_connect (observer,
1048 "authorize-authenticated-peer",
1049 G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1051 g_object_unref (observer);
1053 g_dbus_server_start (server);
1055 service_loop = g_main_loop_new (service_context, FALSE);
1056 g_main_loop_run (service_loop);
1058 g_main_context_pop_thread_default (service_context);
1060 g_main_loop_unref (service_loop);
1061 g_main_context_unref (service_context);
1063 /* test code specifically unrefs the server - see below */
1064 g_assert (server == NULL);
1070 test_nonce_tcp (void)
1074 GThread *service_thread;
1079 const gchar *address;
1081 memset (&data, '\0', sizeof (PeerData));
1082 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1086 service_loop = NULL;
1087 service_thread = g_thread_create (nonce_tcp_service_thread_func,
1091 while (service_loop == NULL)
1093 g_assert (server != NULL);
1096 /* bring up a connection and accept it */
1097 data.accept_connection = TRUE;
1099 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1100 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1101 NULL, /* GDBusAuthObserver */
1102 NULL, /* cancellable */
1104 g_assert_no_error (error);
1105 g_assert (c != NULL);
1106 while (data.current_connections->len < 1)
1107 g_main_loop_run (loop);
1108 g_assert_cmpint (data.current_connections->len, ==, 1);
1109 g_assert_cmpint (data.num_connection_attempts, ==, 1);
1110 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1111 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1114 /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1117 address = g_dbus_server_get_client_address (server);
1119 s = strstr (address, "noncefile=");
1120 g_assert (s != NULL);
1121 s += sizeof "noncefile=" - 1;
1122 nonce_file = g_strdup (s);
1124 /* First try invalid data in the nonce file - this will actually
1125 * make the client send this and the server will reject it. The way
1126 * it works is that if the nonce doesn't match, the server will
1127 * simply close the connection. So, from the client point of view,
1128 * we can see a variety of errors.
1131 res = g_file_set_contents (nonce_file,
1135 g_assert_no_error (error);
1137 c = g_dbus_connection_new_for_address_sync (address,
1138 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1139 NULL, /* GDBusAuthObserver */
1140 NULL, /* cancellable */
1142 _g_assert_error_domain (error, G_IO_ERROR);
1143 g_assert (c == NULL);
1145 /* Then try with a nonce-file of incorrect length - this will make
1146 * the client complain - we won't even try connecting to the server
1150 res = g_file_set_contents (nonce_file,
1151 "0123456789012345_",
1154 g_assert_no_error (error);
1156 c = g_dbus_connection_new_for_address_sync (address,
1157 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1158 NULL, /* GDBusAuthObserver */
1159 NULL, /* cancellable */
1161 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1162 g_assert (c == NULL);
1164 /* Finally try with no nonce-file at all */
1165 g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1167 c = g_dbus_connection_new_for_address_sync (address,
1168 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1169 NULL, /* GDBusAuthObserver */
1170 NULL, /* cancellable */
1172 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1173 g_assert (c == NULL);
1175 g_free (nonce_file);
1177 g_dbus_server_stop (server);
1178 g_object_unref (server);
1181 g_main_loop_quit (service_loop);
1182 g_thread_join (service_thread);
1185 /* ---------------------------------------------------------------------------------------------------- */
1192 GDBusNodeInfo *introspection_data = NULL;
1195 g_thread_init (NULL);
1196 g_test_init (&argc, &argv, NULL);
1198 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1199 g_assert (introspection_data != NULL);
1200 test_interface_introspection_data = introspection_data->interfaces[0];
1202 test_guid = g_dbus_generate_guid ();
1204 /* all the tests rely on a shared main loop */
1205 loop = g_main_loop_new (NULL, FALSE);
1207 g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1208 g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1209 g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1213 g_main_loop_unref (loop);
1215 g_dbus_node_info_unref (introspection_data);