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>
32 #include <gio/gunixsocketaddress.h>
33 #include <gio/gunixfdlist.h>
35 #include "gdbus-tests.h"
39 static gboolean is_unix = TRUE;
41 static gboolean is_unix = FALSE;
44 static gchar *test_guid = NULL;
45 static GMainLoop *service_loop = NULL;
46 static GDBusServer *server = NULL;
47 static GMainLoop *loop = NULL;
49 /* ---------------------------------------------------------------------------------------------------- */
50 /* Test that peer-to-peer connections work */
51 /* ---------------------------------------------------------------------------------------------------- */
56 gboolean accept_connection;
57 gint num_connection_attempts;
58 GPtrArray *current_connections;
59 guint num_method_calls;
60 gboolean signal_received;
63 static const gchar *test_interface_introspection_xml =
65 " <interface name='org.gtk.GDBus.PeerTestInterface'>"
66 " <method name='HelloPeer'>"
67 " <arg type='s' name='greeting' direction='in'/>"
68 " <arg type='s' name='response' direction='out'/>"
70 " <method name='EmitSignal'/>"
71 " <method name='OpenFile'>"
72 " <arg type='s' name='path' direction='in'/>"
74 " <signal name='PeerSignal'>"
75 " <arg type='s' name='a_string'/>"
77 " <property type='s' name='PeerProperty' access='read'/>"
80 static const GDBusInterfaceInfo *test_interface_introspection_data = NULL;
83 test_interface_method_call (GDBusConnection *connection,
85 const gchar *object_path,
86 const gchar *interface_name,
87 const gchar *method_name,
89 GDBusMethodInvocation *invocation,
92 PeerData *data = user_data;
94 data->num_method_calls++;
96 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
97 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
99 if (g_strcmp0 (method_name, "HelloPeer") == 0)
101 const gchar *greeting;
104 g_variant_get (parameters, "(&s)", &greeting);
106 response = g_strdup_printf ("You greeted me with '%s'.",
108 g_dbus_method_invocation_return_value (invocation,
109 g_variant_new ("(s)", response));
112 else if (g_strcmp0 (method_name, "EmitSignal") == 0)
117 g_dbus_connection_emit_signal (connection,
119 "/org/gtk/GDBus/PeerTestObject",
120 "org.gtk.GDBus.PeerTestInterface",
124 g_assert_no_error (error);
125 g_dbus_method_invocation_return_value (invocation, NULL);
127 else if (g_strcmp0 (method_name, "OpenFile") == 0)
134 GUnixFDList *fd_list;
136 g_variant_get (parameters, "(&s)", &path);
138 fd_list = g_unix_fd_list_new ();
142 fd = open (path, O_RDONLY);
143 g_unix_fd_list_append (fd_list, fd, &error);
144 g_assert_no_error (error);
147 reply = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
148 g_dbus_message_set_unix_fd_list (reply, fd_list);
149 g_object_unref (invocation);
152 g_dbus_connection_send_message (connection,
154 NULL, /* out_serial */
156 g_assert_no_error (error);
157 g_object_unref (reply);
159 g_dbus_method_invocation_return_dbus_error (invocation,
160 "org.gtk.GDBus.NotOnUnix",
161 "Your OS does not support file descriptor passing");
166 g_assert_not_reached ();
171 test_interface_get_property (GDBusConnection *connection,
173 const gchar *object_path,
174 const gchar *interface_name,
175 const gchar *property_name,
179 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
180 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
181 g_assert_cmpstr (property_name, ==, "PeerProperty");
183 return g_variant_new_string ("ThePropertyValue");
187 static const GDBusInterfaceVTable test_interface_vtable =
189 test_interface_method_call,
190 test_interface_get_property,
191 NULL /* set_property */
195 on_proxy_signal_received (GDBusProxy *proxy,
198 GVariant *parameters,
201 PeerData *data = user_data;
203 data->signal_received = TRUE;
205 g_assert (sender_name == NULL);
206 g_assert_cmpstr (signal_name, ==, "PeerSignal");
207 g_main_loop_quit (loop);
210 /* ---------------------------------------------------------------------------------------------------- */
213 on_authorize_authenticated_peer (GDBusAuthObserver *observer,
215 GCredentials *credentials,
218 PeerData *data = user_data;
221 data->num_connection_attempts++;
224 if (!data->accept_connection)
227 g_main_loop_quit (loop);
233 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
235 on_new_connection (GDBusServer *server,
236 GDBusConnection *connection,
239 PeerData *data = user_data;
243 //g_print ("Client connected.\n"
244 // "Negotiated capabilities: unix-fd-passing=%d\n",
245 // g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
247 g_ptr_array_add (data->current_connections, g_object_ref (connection));
249 /* export object on the newly established connection */
251 reg_id = g_dbus_connection_register_object (connection,
252 "/org/gtk/GDBus/PeerTestObject",
253 test_interface_introspection_data,
254 &test_interface_vtable,
256 NULL, /* GDestroyNotify for data */
258 g_assert_no_error (error);
259 g_assert (reg_id > 0);
261 g_main_loop_quit (loop);
265 service_thread_func (gpointer user_data)
267 PeerData *data = user_data;
268 GMainContext *service_context;
269 GDBusAuthObserver *observer;
272 service_context = g_main_context_new ();
273 g_main_context_push_thread_default (service_context);
276 observer = g_dbus_auth_observer_new ();
277 server = g_dbus_server_new_sync (is_unix ? "unix:tmpdir=/tmp/gdbus-test-" : "nonce-tcp:",
278 G_DBUS_SERVER_FLAGS_NONE,
281 NULL, /* cancellable */
283 g_assert_no_error (error);
285 g_signal_connect (server,
287 G_CALLBACK (on_new_connection),
289 g_signal_connect (observer,
290 "authorize-authenticated-peer",
291 G_CALLBACK (on_authorize_authenticated_peer),
293 g_object_unref (observer);
295 g_dbus_server_start (server);
297 service_loop = g_main_loop_new (service_context, FALSE);
298 g_main_loop_run (service_loop);
300 g_main_context_pop_thread_default (service_context);
302 g_main_loop_unref (service_loop);
303 g_main_context_unref (service_context);
305 /* test code specifically unrefs the server - see below */
306 g_assert (server == NULL);
313 on_incoming_connection (GSocketService *service,
314 GSocketConnection *socket_connection,
315 GObject *source_object,
318 PeerData *data = user_data;
320 if (data->accept_connection)
324 GDBusConnection *connection;
327 connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
329 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
330 NULL, /* cancellable */
332 g_assert_no_error (error);
334 g_ptr_array_add (data->current_connections, connection);
336 /* export object on the newly established connection */
338 reg_id = g_dbus_connection_register_object (connection,
339 "/org/gtk/GDBus/PeerTestObject",
340 &test_interface_introspection_data,
341 &test_interface_vtable,
343 NULL, /* GDestroyNotify for data */
345 g_assert_no_error (error);
346 g_assert (reg_id > 0);
351 /* don't do anything */
354 data->num_connection_attempts++;
356 g_main_loop_quit (loop);
358 /* stops other signal handlers from being invoked */
363 service_thread_func (gpointer data)
365 GMainContext *service_context;
367 GSocketAddress *address;
370 service_context = g_main_context_new ();
371 g_main_context_push_thread_default (service_context);
373 socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
374 address = g_unix_socket_address_new (socket_path);
376 service = g_socket_service_new ();
378 g_socket_listener_add_address (G_SOCKET_LISTENER (service),
380 G_SOCKET_TYPE_STREAM,
381 G_SOCKET_PROTOCOL_DEFAULT,
382 NULL, /* source_object */
383 NULL, /* effective_address */
385 g_assert_no_error (error);
386 g_signal_connect (service,
388 G_CALLBACK (on_incoming_connection),
390 g_socket_service_start (service);
392 service_loop = g_main_loop_new (service_context, FALSE);
393 g_main_loop_run (service_loop);
395 g_main_context_pop_thread_default (service_context);
397 g_main_loop_unref (service_loop);
398 g_main_context_unref (service_context);
400 g_object_unref (address);
401 g_free (socket_path);
406 /* ---------------------------------------------------------------------------------------------------- */
410 check_connection (gpointer user_data)
412 PeerData *data = user_data;
415 for (n = 0; n < data->current_connections->len; n++)
420 c = G_DBUS_CONNECTION (data->current_connections->pdata[n]);
421 stream = g_dbus_connection_get_stream (c);
423 g_debug ("In check_connection for %d: connection %p, stream %p", n, c, stream);
424 g_debug ("closed = %d", g_io_stream_is_closed (stream));
427 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
428 g_debug ("socket_closed = %d", g_socket_is_closed (socket));
429 g_debug ("socket_condition_check = %d", g_socket_condition_check (socket, G_IO_IN|G_IO_OUT|G_IO_ERR|G_IO_HUP));
435 num_read = g_input_stream_read (g_io_stream_get_input_stream (stream),
442 g_debug ("error: %s", error->message);
443 g_error_free (error);
447 g_debug ("no error, read %d bytes", (gint) num_read);
455 on_do_disconnect_in_idle (gpointer data)
457 GDBusConnection *c = G_DBUS_CONNECTION (data);
458 g_debug ("GDC %p has ref_count %d", c, G_OBJECT (c)->ref_count);
459 g_dbus_connection_disconnect (c);
476 GThread *service_thread;
478 memset (&data, '\0', sizeof (PeerData));
479 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
481 /* first try to connect when there is no server */
483 c = g_dbus_connection_new_for_address_sync (is_unix ? "unix:path=/tmp/gdbus-test-does-not-exist-pid" :
484 /* NOTE: Even if something is listening on port 12345 the connection
485 * will fail because the nonce file doesn't exist */
486 "nonce-tcp:host=localhost,port=12345,noncefile=this-does-not-exist-gdbus",
487 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
488 NULL, /* GDBusAuthObserver */
489 NULL, /* cancellable */
491 _g_assert_error_domain (error, G_IO_ERROR);
492 g_assert (!g_dbus_error_is_remote_error (error));
493 g_clear_error (&error);
494 g_assert (c == NULL);
496 /* bring up a server - we run the server in a different thread to avoid deadlocks */
498 service_thread = g_thread_create (service_thread_func,
502 while (service_loop == NULL)
504 g_assert (server != NULL);
506 /* bring up a connection and accept it */
507 data.accept_connection = TRUE;
509 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
510 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
511 NULL, /* GDBusAuthObserver */
512 NULL, /* cancellable */
514 g_assert_no_error (error);
515 g_assert (c != NULL);
516 while (data.current_connections->len < 1)
517 g_main_loop_run (loop);
518 g_assert_cmpint (data.current_connections->len, ==, 1);
519 g_assert_cmpint (data.num_connection_attempts, ==, 1);
520 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
521 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
523 /* check that we create a proxy, read properties, receive signals and invoke
524 * the HelloPeer() method. Since the server runs in another thread it's fine
525 * to use synchronous blocking API here.
528 proxy = g_dbus_proxy_new_sync (c,
530 G_DBUS_PROXY_FLAGS_NONE,
533 "/org/gtk/GDBus/PeerTestObject",
534 "org.gtk.GDBus.PeerTestInterface",
535 NULL, /* GCancellable */
537 g_assert_no_error (error);
538 g_assert (proxy != NULL);
540 value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
541 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
543 /* try invoking a method */
545 result = g_dbus_proxy_call_sync (proxy,
547 g_variant_new ("(s)", "Hey Peer!"),
548 G_DBUS_CALL_FLAGS_NONE,
550 NULL, /* GCancellable */
552 g_assert_no_error (error);
553 g_variant_get (result, "(&s)", &s);
554 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
555 g_variant_unref (result);
556 g_assert_cmpint (data.num_method_calls, ==, 1);
558 /* make the other peer emit a signal - catch it */
559 g_signal_connect (proxy,
561 G_CALLBACK (on_proxy_signal_received),
563 g_assert (!data.signal_received);
564 g_dbus_proxy_call (proxy,
566 NULL, /* no arguments */
567 G_DBUS_CALL_FLAGS_NONE,
569 NULL, /* GCancellable */
570 NULL, /* GAsyncReadyCallback - we don't care about the result */
571 NULL); /* user_data */
572 g_main_loop_run (loop);
573 g_assert (data.signal_received);
574 g_assert_cmpint (data.num_method_calls, ==, 2);
576 /* check for UNIX fd passing */
579 GDBusMessage *method_call_message;
580 GDBusMessage *method_reply_message;
581 GUnixFDList *fd_list;
588 method_call_message = g_dbus_message_new_method_call (NULL, /* name */
589 "/org/gtk/GDBus/PeerTestObject",
590 "org.gtk.GDBus.PeerTestInterface",
592 g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", "/etc/hosts"));
594 method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
597 NULL, /* out_serial */
598 NULL, /* cancellable */
600 g_assert_no_error (error);
601 g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
602 fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
603 g_assert (fd_list != NULL);
604 g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
606 fd = g_unix_fd_list_get (fd_list, 0, &error);
607 g_assert_no_error (error);
608 g_object_unref (method_call_message);
609 g_object_unref (method_reply_message);
611 memset (buf, '\0', sizeof (buf));
612 len = read (fd, buf, sizeof (buf) - 1);
616 g_file_get_contents ("/etc/hosts",
620 g_assert_no_error (error);
621 if (len2 > sizeof (buf))
622 buf2[sizeof (buf)] = '\0';
623 g_assert_cmpstr (buf, ==, buf2);
628 result = g_dbus_proxy_call_sync (proxy,
630 g_variant_new ("(s)", "boo"),
631 G_DBUS_CALL_FLAGS_NONE,
633 NULL, /* GCancellable */
635 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
636 g_assert (result == NULL);
637 g_error_free (error);
638 #endif /* G_OS_UNIX */
641 /* bring up a connection - don't accept it - this should fail
643 data.accept_connection = FALSE;
645 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
646 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
647 NULL, /* GDBusAuthObserver */
648 NULL, /* cancellable */
650 _g_assert_error_domain (error, G_IO_ERROR);
651 g_assert (c2 == NULL);
654 /* TODO: THIS TEST DOESN'T WORK YET */
656 /* bring up a connection - accept it.. then disconnect from the client side - check
657 * that the server side gets the disconnect signal.
660 data.accept_connection = TRUE;
661 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
662 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
663 NULL, /* GDBusAuthObserver */
664 NULL, /* cancellable */
666 g_assert_no_error (error);
667 g_assert (c2 != NULL);
668 g_assert (!g_dbus_connection_get_is_disconnected (c2));
669 while (data.num_connection_attempts < 3)
670 g_main_loop_run (loop);
671 g_assert_cmpint (data.current_connections->len, ==, 2);
672 g_assert_cmpint (data.num_connection_attempts, ==, 3);
673 g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
674 g_idle_add (on_do_disconnect_in_idle, c2);
675 g_debug ("==================================================");
676 g_debug ("==================================================");
677 g_debug ("==================================================");
678 g_debug ("waiting for disconnect on connection %p, stream %p",
679 data.current_connections->pdata[1],
680 g_dbus_connection_get_stream (data.current_connections->pdata[1]));
682 g_timeout_add (2000, check_connection, &data);
683 //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
684 g_main_loop_run (loop);
685 g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
686 g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
689 /* unref the server and stop listening for new connections
691 * This won't bring down the established connections - check that c is still connected
692 * by invoking a method
694 //g_socket_service_stop (service);
695 //g_object_unref (service);
696 g_dbus_server_stop (server);
697 g_object_unref (server);
701 result = g_dbus_proxy_call_sync (proxy,
703 g_variant_new ("(s)", "Hey Again Peer!"),
704 G_DBUS_CALL_FLAGS_NONE,
706 NULL, /* GCancellable */
708 g_assert_no_error (error);
709 g_variant_get (result, "(&s)", &s);
710 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
711 g_variant_unref (result);
712 g_assert_cmpint (data.num_method_calls, ==, 4);
715 /* TODO: THIS TEST DOESN'T WORK YET */
717 /* now disconnect from the server side - check that the client side gets the signal */
718 g_assert_cmpint (data.current_connections->len, ==, 1);
719 g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
720 g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
721 if (!g_dbus_connection_get_is_disconnected (c))
722 _g_assert_signal_received (c, "closed");
723 g_assert (g_dbus_connection_get_is_disconnected (c));
727 g_ptr_array_unref (data.current_connections);
728 g_object_unref (proxy);
730 g_main_loop_quit (service_loop);
731 g_thread_join (service_thread);
734 /* ---------------------------------------------------------------------------------------------------- */
741 GDBusNodeInfo *introspection_data = NULL;
744 g_thread_init (NULL);
745 g_test_init (&argc, &argv, NULL);
747 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
748 g_assert (introspection_data != NULL);
749 test_interface_introspection_data = introspection_data->interfaces[0];
751 test_guid = g_dbus_generate_guid ();
753 /* all the tests rely on a shared main loop */
754 loop = g_main_loop_new (NULL, FALSE);
756 g_test_add_func ("/gdbus/peer-to-peer", test_peer);
760 g_main_loop_unref (loop);
762 g_dbus_node_info_unref (introspection_data);