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 const 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, 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 NULL, /* out_serial */
179 g_assert_no_error (error);
180 g_object_unref (reply);
182 g_dbus_method_invocation_return_dbus_error (invocation,
183 "org.gtk.GDBus.NotOnUnix",
184 "Your OS does not support file descriptor passing");
189 g_assert_not_reached ();
194 test_interface_get_property (GDBusConnection *connection,
196 const gchar *object_path,
197 const gchar *interface_name,
198 const gchar *property_name,
202 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
203 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
204 g_assert_cmpstr (property_name, ==, "PeerProperty");
206 return g_variant_new_string ("ThePropertyValue");
210 static const GDBusInterfaceVTable test_interface_vtable =
212 test_interface_method_call,
213 test_interface_get_property,
214 NULL /* set_property */
218 on_proxy_signal_received (GDBusProxy *proxy,
221 GVariant *parameters,
224 PeerData *data = user_data;
226 data->signal_received = TRUE;
228 g_assert (sender_name == NULL);
229 g_assert_cmpstr (signal_name, ==, "PeerSignal");
230 g_main_loop_quit (loop);
234 on_proxy_signal_received_with_name_set (GDBusProxy *proxy,
237 GVariant *parameters,
240 PeerData *data = user_data;
242 data->signal_received = TRUE;
244 g_assert_cmpstr (sender_name, ==, ":1.42");
245 g_assert_cmpstr (signal_name, ==, "PeerSignalWithNameSet");
246 g_main_loop_quit (loop);
249 /* ---------------------------------------------------------------------------------------------------- */
252 on_authorize_authenticated_peer (GDBusAuthObserver *observer,
254 GCredentials *credentials,
257 PeerData *data = user_data;
260 data->num_connection_attempts++;
263 if (!data->accept_connection)
266 g_main_loop_quit (loop);
272 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
274 on_new_connection (GDBusServer *server,
275 GDBusConnection *connection,
278 PeerData *data = user_data;
282 //g_print ("Client connected.\n"
283 // "Negotiated capabilities: unix-fd-passing=%d\n",
284 // g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
286 g_ptr_array_add (data->current_connections, g_object_ref (connection));
288 /* export object on the newly established connection */
290 reg_id = g_dbus_connection_register_object (connection,
291 "/org/gtk/GDBus/PeerTestObject",
292 test_interface_introspection_data,
293 &test_interface_vtable,
295 NULL, /* GDestroyNotify for data */
297 g_assert_no_error (error);
298 g_assert (reg_id > 0);
300 g_main_loop_quit (loop);
304 service_thread_func (gpointer user_data)
306 PeerData *data = user_data;
307 GMainContext *service_context;
308 GDBusAuthObserver *observer;
311 service_context = g_main_context_new ();
312 g_main_context_push_thread_default (service_context);
315 observer = g_dbus_auth_observer_new ();
316 server = g_dbus_server_new_sync (is_unix ? "unix:tmpdir=/tmp/gdbus-test-" : "nonce-tcp:",
317 G_DBUS_SERVER_FLAGS_NONE,
320 NULL, /* cancellable */
322 g_assert_no_error (error);
324 g_signal_connect (server,
326 G_CALLBACK (on_new_connection),
328 g_signal_connect (observer,
329 "authorize-authenticated-peer",
330 G_CALLBACK (on_authorize_authenticated_peer),
332 g_object_unref (observer);
334 g_dbus_server_start (server);
336 service_loop = g_main_loop_new (service_context, FALSE);
337 g_main_loop_run (service_loop);
339 g_main_context_pop_thread_default (service_context);
341 g_main_loop_unref (service_loop);
342 g_main_context_unref (service_context);
344 /* test code specifically unrefs the server - see below */
345 g_assert (server == NULL);
352 on_incoming_connection (GSocketService *service,
353 GSocketConnection *socket_connection,
354 GObject *source_object,
357 PeerData *data = user_data;
359 if (data->accept_connection)
363 GDBusConnection *connection;
366 connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
368 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
369 NULL, /* cancellable */
371 g_assert_no_error (error);
373 g_ptr_array_add (data->current_connections, connection);
375 /* export object on the newly established connection */
377 reg_id = g_dbus_connection_register_object (connection,
378 "/org/gtk/GDBus/PeerTestObject",
379 &test_interface_introspection_data,
380 &test_interface_vtable,
382 NULL, /* GDestroyNotify for data */
384 g_assert_no_error (error);
385 g_assert (reg_id > 0);
390 /* don't do anything */
393 data->num_connection_attempts++;
395 g_main_loop_quit (loop);
397 /* stops other signal handlers from being invoked */
402 service_thread_func (gpointer data)
404 GMainContext *service_context;
406 GSocketAddress *address;
409 service_context = g_main_context_new ();
410 g_main_context_push_thread_default (service_context);
412 socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
413 address = g_unix_socket_address_new (socket_path);
415 service = g_socket_service_new ();
417 g_socket_listener_add_address (G_SOCKET_LISTENER (service),
419 G_SOCKET_TYPE_STREAM,
420 G_SOCKET_PROTOCOL_DEFAULT,
421 NULL, /* source_object */
422 NULL, /* effective_address */
424 g_assert_no_error (error);
425 g_signal_connect (service,
427 G_CALLBACK (on_incoming_connection),
429 g_socket_service_start (service);
431 service_loop = g_main_loop_new (service_context, FALSE);
432 g_main_loop_run (service_loop);
434 g_main_context_pop_thread_default (service_context);
436 g_main_loop_unref (service_loop);
437 g_main_context_unref (service_context);
439 g_object_unref (address);
440 g_free (socket_path);
445 /* ---------------------------------------------------------------------------------------------------- */
449 check_connection (gpointer user_data)
451 PeerData *data = user_data;
454 for (n = 0; n < data->current_connections->len; n++)
459 c = G_DBUS_CONNECTION (data->current_connections->pdata[n]);
460 stream = g_dbus_connection_get_stream (c);
462 g_debug ("In check_connection for %d: connection %p, stream %p", n, c, stream);
463 g_debug ("closed = %d", g_io_stream_is_closed (stream));
466 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
467 g_debug ("socket_closed = %d", g_socket_is_closed (socket));
468 g_debug ("socket_condition_check = %d", g_socket_condition_check (socket, G_IO_IN|G_IO_OUT|G_IO_ERR|G_IO_HUP));
474 num_read = g_input_stream_read (g_io_stream_get_input_stream (stream),
481 g_debug ("error: %s", error->message);
482 g_error_free (error);
486 g_debug ("no error, read %d bytes", (gint) num_read);
494 on_do_disconnect_in_idle (gpointer data)
496 GDBusConnection *c = G_DBUS_CONNECTION (data);
497 g_debug ("GDC %p has ref_count %d", c, G_OBJECT (c)->ref_count);
498 g_dbus_connection_disconnect (c);
515 GThread *service_thread;
516 gulong signal_handler_id;
518 memset (&data, '\0', sizeof (PeerData));
519 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
521 /* first try to connect when there is no server */
523 c = g_dbus_connection_new_for_address_sync (is_unix ? "unix:path=/tmp/gdbus-test-does-not-exist-pid" :
524 /* NOTE: Even if something is listening on port 12345 the connection
525 * will fail because the nonce file doesn't exist */
526 "nonce-tcp:host=localhost,port=12345,noncefile=this-does-not-exist-gdbus",
527 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
528 NULL, /* GDBusAuthObserver */
529 NULL, /* cancellable */
531 _g_assert_error_domain (error, G_IO_ERROR);
532 g_assert (!g_dbus_error_is_remote_error (error));
533 g_clear_error (&error);
534 g_assert (c == NULL);
536 /* bring up a server - we run the server in a different thread to avoid deadlocks */
538 service_thread = g_thread_create (service_thread_func,
542 while (service_loop == NULL)
544 g_assert (server != NULL);
546 /* bring up a connection and accept it */
547 data.accept_connection = TRUE;
549 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
550 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
551 NULL, /* GDBusAuthObserver */
552 NULL, /* cancellable */
554 g_assert_no_error (error);
555 g_assert (c != NULL);
556 while (data.current_connections->len < 1)
557 g_main_loop_run (loop);
558 g_assert_cmpint (data.current_connections->len, ==, 1);
559 g_assert_cmpint (data.num_connection_attempts, ==, 1);
560 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
561 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
563 /* check that we create a proxy, read properties, receive signals and invoke
564 * the HelloPeer() method. Since the server runs in another thread it's fine
565 * to use synchronous blocking API here.
568 proxy = g_dbus_proxy_new_sync (c,
569 G_DBUS_PROXY_FLAGS_NONE,
572 "/org/gtk/GDBus/PeerTestObject",
573 "org.gtk.GDBus.PeerTestInterface",
574 NULL, /* GCancellable */
576 g_assert_no_error (error);
577 g_assert (proxy != NULL);
579 value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
580 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
582 /* try invoking a method */
584 result = g_dbus_proxy_call_sync (proxy,
586 g_variant_new ("(s)", "Hey Peer!"),
587 G_DBUS_CALL_FLAGS_NONE,
589 NULL, /* GCancellable */
591 g_assert_no_error (error);
592 g_variant_get (result, "(&s)", &s);
593 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
594 g_variant_unref (result);
595 g_assert_cmpint (data.num_method_calls, ==, 1);
597 /* make the other peer emit a signal - catch it */
598 signal_handler_id = g_signal_connect (proxy,
600 G_CALLBACK (on_proxy_signal_received),
602 g_assert (!data.signal_received);
603 g_dbus_proxy_call (proxy,
605 NULL, /* no arguments */
606 G_DBUS_CALL_FLAGS_NONE,
608 NULL, /* GCancellable */
609 NULL, /* GAsyncReadyCallback - we don't care about the result */
610 NULL); /* user_data */
611 g_main_loop_run (loop);
612 g_assert (data.signal_received);
613 g_assert_cmpint (data.num_method_calls, ==, 2);
614 g_signal_handler_disconnect (proxy, signal_handler_id);
616 /* Also ensure that messages with the sender header-field set gets
617 * delivered to the proxy - note that this doesn't really make sense
618 * e.g. names are meaning-less in a peer-to-peer case... but we
619 * support it because it makes sense in certain bridging
620 * applications - see e.g. #623815.
622 signal_handler_id = g_signal_connect (proxy,
624 G_CALLBACK (on_proxy_signal_received_with_name_set),
626 data.signal_received = FALSE;
627 g_dbus_proxy_call (proxy,
628 "EmitSignalWithNameSet",
629 NULL, /* no arguments */
630 G_DBUS_CALL_FLAGS_NONE,
632 NULL, /* GCancellable */
633 NULL, /* GAsyncReadyCallback - we don't care about the result */
634 NULL); /* user_data */
635 g_main_loop_run (loop);
636 g_assert (data.signal_received);
637 g_assert_cmpint (data.num_method_calls, ==, 3);
638 g_signal_handler_disconnect (proxy, signal_handler_id);
640 /* check for UNIX fd passing */
643 GDBusMessage *method_call_message;
644 GDBusMessage *method_reply_message;
645 GUnixFDList *fd_list;
652 method_call_message = g_dbus_message_new_method_call (NULL, /* name */
653 "/org/gtk/GDBus/PeerTestObject",
654 "org.gtk.GDBus.PeerTestInterface",
656 g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", "/etc/hosts"));
658 method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
661 NULL, /* out_serial */
662 NULL, /* cancellable */
664 g_assert_no_error (error);
665 g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
666 fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
667 g_assert (fd_list != NULL);
668 g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
670 fd = g_unix_fd_list_get (fd_list, 0, &error);
671 g_assert_no_error (error);
672 g_object_unref (method_call_message);
673 g_object_unref (method_reply_message);
675 memset (buf, '\0', sizeof (buf));
676 len = read (fd, buf, sizeof (buf) - 1);
680 g_file_get_contents ("/etc/hosts",
684 g_assert_no_error (error);
685 if (len2 > sizeof (buf))
686 buf2[sizeof (buf)] = '\0';
687 g_assert_cmpstr (buf, ==, buf2);
692 result = g_dbus_proxy_call_sync (proxy,
694 g_variant_new ("(s)", "boo"),
695 G_DBUS_CALL_FLAGS_NONE,
697 NULL, /* GCancellable */
699 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
700 g_assert (result == NULL);
701 g_error_free (error);
702 #endif /* G_OS_UNIX */
705 /* bring up a connection - don't accept it - this should fail
707 data.accept_connection = FALSE;
709 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
710 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
711 NULL, /* GDBusAuthObserver */
712 NULL, /* cancellable */
714 _g_assert_error_domain (error, G_IO_ERROR);
715 g_assert (c2 == NULL);
718 /* TODO: THIS TEST DOESN'T WORK YET */
720 /* bring up a connection - accept it.. then disconnect from the client side - check
721 * that the server side gets the disconnect signal.
724 data.accept_connection = TRUE;
725 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
726 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
727 NULL, /* GDBusAuthObserver */
728 NULL, /* cancellable */
730 g_assert_no_error (error);
731 g_assert (c2 != NULL);
732 g_assert (!g_dbus_connection_get_is_disconnected (c2));
733 while (data.num_connection_attempts < 3)
734 g_main_loop_run (loop);
735 g_assert_cmpint (data.current_connections->len, ==, 2);
736 g_assert_cmpint (data.num_connection_attempts, ==, 3);
737 g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
738 g_idle_add (on_do_disconnect_in_idle, c2);
739 g_debug ("==================================================");
740 g_debug ("==================================================");
741 g_debug ("==================================================");
742 g_debug ("waiting for disconnect on connection %p, stream %p",
743 data.current_connections->pdata[1],
744 g_dbus_connection_get_stream (data.current_connections->pdata[1]));
746 g_timeout_add (2000, check_connection, &data);
747 //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
748 g_main_loop_run (loop);
749 g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
750 g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
753 /* unref the server and stop listening for new connections
755 * This won't bring down the established connections - check that c is still connected
756 * by invoking a method
758 //g_socket_service_stop (service);
759 //g_object_unref (service);
760 g_dbus_server_stop (server);
761 g_object_unref (server);
765 result = g_dbus_proxy_call_sync (proxy,
767 g_variant_new ("(s)", "Hey Again Peer!"),
768 G_DBUS_CALL_FLAGS_NONE,
770 NULL, /* GCancellable */
772 g_assert_no_error (error);
773 g_variant_get (result, "(&s)", &s);
774 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
775 g_variant_unref (result);
776 g_assert_cmpint (data.num_method_calls, ==, 5);
779 /* TODO: THIS TEST DOESN'T WORK YET */
781 /* now disconnect from the server side - check that the client side gets the signal */
782 g_assert_cmpint (data.current_connections->len, ==, 1);
783 g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
784 g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
785 if (!g_dbus_connection_get_is_disconnected (c))
786 _g_assert_signal_received (c, "closed");
787 g_assert (g_dbus_connection_get_is_disconnected (c));
791 g_ptr_array_unref (data.current_connections);
792 g_object_unref (proxy);
794 g_main_loop_quit (service_loop);
795 g_thread_join (service_thread);
798 /* ---------------------------------------------------------------------------------------------------- */
803 GMainContext *context;
810 dmp_data_free (DmpData *data)
812 g_main_loop_unref (data->loop);
813 g_main_context_unref (data->context);
814 g_object_unref (data->server);
815 g_list_foreach (data->connections, (GFunc) g_object_unref, NULL);
816 g_list_free (data->connections);
821 dmp_on_method_call (GDBusConnection *connection,
823 const gchar *object_path,
824 const gchar *interface_name,
825 const gchar *method_name,
826 GVariant *parameters,
827 GDBusMethodInvocation *invocation,
830 //DmpData *data = user_data;
833 g_variant_get (parameters,
837 g_dbus_method_invocation_return_value (invocation,
838 g_variant_new ("(i)", first + second));
841 static const GDBusInterfaceVTable dmp_interface_vtable =
844 NULL, /* get_property */
845 NULL /* set_property */
849 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
851 dmp_on_new_connection (GDBusServer *server,
852 GDBusConnection *connection,
855 DmpData *data = user_data;
859 /* accept the connection */
860 data->connections = g_list_prepend (data->connections, g_object_ref (connection));
863 node = g_dbus_node_info_new_for_xml ("<node>"
864 " <interface name='org.gtk.GDBus.DmpInterface'>"
865 " <method name='AddPair'>"
866 " <arg type='i' name='first' direction='in'/>"
867 " <arg type='i' name='second' direction='in'/>"
868 " <arg type='i' name='sum' direction='out'/>"
873 g_assert_no_error (error);
875 /* sleep 100ms before exporting an object - this is to test that
876 * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
877 * (GDBusServer uses this feature).
881 /* export an object */
883 g_dbus_connection_register_object (connection,
886 &dmp_interface_vtable,
890 g_dbus_node_info_unref (node);
894 dmp_thread_func (gpointer user_data)
896 DmpData *data = user_data;
900 data->context = g_main_context_new ();
901 g_main_context_push_thread_default (data->context);
904 guid = g_dbus_generate_guid ();
905 data->server = g_dbus_server_new_sync ("unix:tmpdir=/tmp/gdbus-test-",
906 G_DBUS_SERVER_FLAGS_NONE,
908 NULL, /* GDBusAuthObserver */
909 NULL, /* GCancellable */
911 g_assert_no_error (error);
912 g_signal_connect (data->server,
914 G_CALLBACK (dmp_on_new_connection),
917 g_dbus_server_start (data->server);
919 data->loop = g_main_loop_new (data->context, FALSE);
920 g_main_loop_run (data->loop);
922 g_main_context_pop_thread_default (data->context);
929 delayed_message_processing (void)
933 GThread *service_thread;
936 data = g_new0 (DmpData, 1);
939 service_thread = g_thread_create (dmp_thread_func,
943 while (data->server == NULL || !g_dbus_server_is_active (data->server))
946 for (n = 0; n < 5; n++)
953 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
954 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
955 NULL, /* GDBusAuthObserver */
956 NULL, /* GCancellable */
958 g_assert_no_error (error);
961 res = g_dbus_connection_call_sync (c,
964 "org.gtk.GDBus.DmpInterface",
966 g_variant_new ("(ii)", 2, n),
967 G_VARIANT_TYPE ("(i)"),
968 G_DBUS_CALL_FLAGS_NONE,
969 -1, /* timeout_msec */
970 NULL, /* GCancellable */
972 g_assert_no_error (error);
973 g_variant_get (res, "(i)", &val);
974 g_assert_cmpint (val, ==, 2 + n);
975 g_variant_unref (res);
979 g_main_loop_quit (data->loop);
980 g_thread_join (service_thread);
981 dmp_data_free (data);
984 /* ---------------------------------------------------------------------------------------------------- */
987 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
989 GCredentials *credentials,
992 PeerData *data = user_data;
995 data->num_connection_attempts++;
998 if (!data->accept_connection)
1001 g_main_loop_quit (loop);
1007 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1009 nonce_tcp_on_new_connection (GDBusServer *server,
1010 GDBusConnection *connection,
1013 PeerData *data = user_data;
1015 g_ptr_array_add (data->current_connections, g_object_ref (connection));
1017 g_main_loop_quit (loop);
1021 nonce_tcp_service_thread_func (gpointer user_data)
1023 PeerData *data = user_data;
1024 GMainContext *service_context;
1025 GDBusAuthObserver *observer;
1028 service_context = g_main_context_new ();
1029 g_main_context_push_thread_default (service_context);
1032 observer = g_dbus_auth_observer_new ();
1033 server = g_dbus_server_new_sync ("nonce-tcp:",
1034 G_DBUS_SERVER_FLAGS_NONE,
1037 NULL, /* cancellable */
1039 g_assert_no_error (error);
1041 g_signal_connect (server,
1043 G_CALLBACK (nonce_tcp_on_new_connection),
1045 g_signal_connect (observer,
1046 "authorize-authenticated-peer",
1047 G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1049 g_object_unref (observer);
1051 g_dbus_server_start (server);
1053 service_loop = g_main_loop_new (service_context, FALSE);
1054 g_main_loop_run (service_loop);
1056 g_main_context_pop_thread_default (service_context);
1058 g_main_loop_unref (service_loop);
1059 g_main_context_unref (service_context);
1061 /* test code specifically unrefs the server - see below */
1062 g_assert (server == NULL);
1068 test_nonce_tcp (void)
1072 GThread *service_thread;
1077 const gchar *address;
1079 memset (&data, '\0', sizeof (PeerData));
1080 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1084 service_loop = NULL;
1085 service_thread = g_thread_create (nonce_tcp_service_thread_func,
1089 while (service_loop == NULL)
1091 g_assert (server != NULL);
1094 /* bring up a connection and accept it */
1095 data.accept_connection = TRUE;
1097 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1098 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1099 NULL, /* GDBusAuthObserver */
1100 NULL, /* cancellable */
1102 g_assert_no_error (error);
1103 g_assert (c != NULL);
1104 while (data.current_connections->len < 1)
1105 g_main_loop_run (loop);
1106 g_assert_cmpint (data.current_connections->len, ==, 1);
1107 g_assert_cmpint (data.num_connection_attempts, ==, 1);
1108 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1109 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1112 /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1115 address = g_dbus_server_get_client_address (server);
1117 s = strstr (address, "noncefile=");
1118 g_assert (s != NULL);
1119 s += sizeof "noncefile=" - 1;
1120 nonce_file = g_strdup (s);
1122 /* First try invalid data in the nonce file - this will actually
1123 * make the client send this and the server will reject it. The way
1124 * it works is that if the nonce doesn't match, the server will
1125 * simply close the connection. So, from the client point of view,
1126 * we can see a variety of errors.
1129 res = g_file_set_contents (nonce_file,
1133 g_assert_no_error (error);
1135 c = g_dbus_connection_new_for_address_sync (address,
1136 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1137 NULL, /* GDBusAuthObserver */
1138 NULL, /* cancellable */
1140 _g_assert_error_domain (error, G_IO_ERROR);
1141 g_assert (c == NULL);
1143 /* Then try with a nonce-file of incorrect length - this will make
1144 * the client complain - we won't even try connecting to the server
1148 res = g_file_set_contents (nonce_file,
1149 "0123456789012345_",
1152 g_assert_no_error (error);
1154 c = g_dbus_connection_new_for_address_sync (address,
1155 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1156 NULL, /* GDBusAuthObserver */
1157 NULL, /* cancellable */
1159 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1160 g_assert (c == NULL);
1162 /* Finally try with no nonce-file at all */
1163 g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1165 c = g_dbus_connection_new_for_address_sync (address,
1166 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1167 NULL, /* GDBusAuthObserver */
1168 NULL, /* cancellable */
1170 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1171 g_assert (c == NULL);
1173 g_free (nonce_file);
1175 g_dbus_server_stop (server);
1176 g_object_unref (server);
1179 g_main_loop_quit (service_loop);
1180 g_thread_join (service_thread);
1183 /* ---------------------------------------------------------------------------------------------------- */
1190 GDBusNodeInfo *introspection_data = NULL;
1193 g_thread_init (NULL);
1194 g_test_init (&argc, &argv, NULL);
1196 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1197 g_assert (introspection_data != NULL);
1198 test_interface_introspection_data = introspection_data->interfaces[0];
1200 test_guid = g_dbus_generate_guid ();
1202 /* all the tests rely on a shared main loop */
1203 loop = g_main_loop_new (NULL, FALSE);
1205 g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1206 g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1207 g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1211 g_main_loop_unref (loop);
1213 g_dbus_node_info_unref (introspection_data);