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)
133 GUnixFDList *fd_list;
135 g_variant_get (parameters, "(s)", &path);
137 fd_list = g_unix_fd_list_new ();
141 fd = open (path, O_RDONLY);
142 g_unix_fd_list_append (fd_list, fd, &error);
143 g_assert_no_error (error);
146 reply = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
147 g_dbus_message_set_unix_fd_list (reply, fd_list);
148 g_object_unref (invocation);
151 g_dbus_connection_send_message (connection,
153 NULL, /* out_serial */
155 g_assert_no_error (error);
156 g_object_unref (reply);
160 g_assert_not_reached ();
165 test_interface_get_property (GDBusConnection *connection,
167 const gchar *object_path,
168 const gchar *interface_name,
169 const gchar *property_name,
173 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
174 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
175 g_assert_cmpstr (property_name, ==, "PeerProperty");
177 return g_variant_new_string ("ThePropertyValue");
181 static const GDBusInterfaceVTable test_interface_vtable =
183 test_interface_method_call,
184 test_interface_get_property,
185 NULL /* set_property */
189 on_proxy_signal_received (GDBusProxy *proxy,
192 GVariant *parameters,
195 PeerData *data = user_data;
197 data->signal_received = TRUE;
199 g_assert (sender_name == NULL);
200 g_assert_cmpstr (signal_name, ==, "PeerSignal");
201 g_main_loop_quit (loop);
204 /* ---------------------------------------------------------------------------------------------------- */
207 on_authorize_authenticated_peer (GDBusAuthObserver *observer,
209 GCredentials *credentials,
212 PeerData *data = user_data;
215 data->num_connection_attempts++;
218 if (!data->accept_connection)
221 g_main_loop_quit (loop);
227 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
229 on_new_connection (GDBusServer *server,
230 GDBusConnection *connection,
233 PeerData *data = user_data;
237 //g_print ("Client connected.\n"
238 // "Negotiated capabilities: unix-fd-passing=%d\n",
239 // g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
241 g_ptr_array_add (data->current_connections, g_object_ref (connection));
243 /* export object on the newly established connection */
245 reg_id = g_dbus_connection_register_object (connection,
246 "/org/gtk/GDBus/PeerTestObject",
247 test_interface_introspection_data,
248 &test_interface_vtable,
250 NULL, /* GDestroyNotify for data */
252 g_assert_no_error (error);
253 g_assert (reg_id > 0);
255 g_main_loop_quit (loop);
259 service_thread_func (gpointer user_data)
261 PeerData *data = user_data;
262 GMainContext *service_context;
263 GDBusAuthObserver *observer;
266 service_context = g_main_context_new ();
267 g_main_context_push_thread_default (service_context);
270 observer = g_dbus_auth_observer_new ();
271 server = g_dbus_server_new_sync (is_unix ? "unix:tmpdir=/tmp/gdbus-test-" : "nonce-tcp:",
272 G_DBUS_SERVER_FLAGS_NONE,
275 NULL, /* cancellable */
277 g_assert_no_error (error);
279 g_signal_connect (server,
281 G_CALLBACK (on_new_connection),
283 g_signal_connect (observer,
284 "authorize-authenticated-peer",
285 G_CALLBACK (on_authorize_authenticated_peer),
287 g_object_unref (observer);
289 g_dbus_server_start (server);
291 service_loop = g_main_loop_new (service_context, FALSE);
292 g_main_loop_run (service_loop);
294 g_main_context_pop_thread_default (service_context);
296 g_main_loop_unref (service_loop);
297 g_main_context_unref (service_context);
299 /* test code specifically unrefs the server - see below */
300 g_assert (server == NULL);
307 on_incoming_connection (GSocketService *service,
308 GSocketConnection *socket_connection,
309 GObject *source_object,
312 PeerData *data = user_data;
314 if (data->accept_connection)
318 GDBusConnection *connection;
321 connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
323 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
324 NULL, /* cancellable */
326 g_assert_no_error (error);
328 g_ptr_array_add (data->current_connections, connection);
330 /* export object on the newly established connection */
332 reg_id = g_dbus_connection_register_object (connection,
333 "/org/gtk/GDBus/PeerTestObject",
334 &test_interface_introspection_data,
335 &test_interface_vtable,
337 NULL, /* GDestroyNotify for data */
339 g_assert_no_error (error);
340 g_assert (reg_id > 0);
345 /* don't do anything */
348 data->num_connection_attempts++;
350 g_main_loop_quit (loop);
352 /* stops other signal handlers from being invoked */
357 service_thread_func (gpointer data)
359 GMainContext *service_context;
361 GSocketAddress *address;
364 service_context = g_main_context_new ();
365 g_main_context_push_thread_default (service_context);
367 socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
368 address = g_unix_socket_address_new (socket_path);
370 service = g_socket_service_new ();
372 g_socket_listener_add_address (G_SOCKET_LISTENER (service),
374 G_SOCKET_TYPE_STREAM,
375 G_SOCKET_PROTOCOL_DEFAULT,
376 NULL, /* source_object */
377 NULL, /* effective_address */
379 g_assert_no_error (error);
380 g_signal_connect (service,
382 G_CALLBACK (on_incoming_connection),
384 g_socket_service_start (service);
386 service_loop = g_main_loop_new (service_context, FALSE);
387 g_main_loop_run (service_loop);
389 g_main_context_pop_thread_default (service_context);
391 g_main_loop_unref (service_loop);
392 g_main_context_unref (service_context);
394 g_object_unref (address);
395 g_free (socket_path);
400 /* ---------------------------------------------------------------------------------------------------- */
404 check_connection (gpointer user_data)
406 PeerData *data = user_data;
409 for (n = 0; n < data->current_connections->len; n++)
414 c = G_DBUS_CONNECTION (data->current_connections->pdata[n]);
415 stream = g_dbus_connection_get_stream (c);
417 g_debug ("In check_connection for %d: connection %p, stream %p", n, c, stream);
418 g_debug ("closed = %d", g_io_stream_is_closed (stream));
421 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
422 g_debug ("socket_closed = %d", g_socket_is_closed (socket));
423 g_debug ("socket_condition_check = %d", g_socket_condition_check (socket, G_IO_IN|G_IO_OUT|G_IO_ERR|G_IO_HUP));
429 num_read = g_input_stream_read (g_io_stream_get_input_stream (stream),
436 g_debug ("error: %s", error->message);
437 g_error_free (error);
441 g_debug ("no error, read %d bytes", (gint) num_read);
449 on_do_disconnect_in_idle (gpointer data)
451 GDBusConnection *c = G_DBUS_CONNECTION (data);
452 g_debug ("GDC %p has ref_count %d", c, G_OBJECT (c)->ref_count);
453 g_dbus_connection_disconnect (c);
470 GThread *service_thread;
472 memset (&data, '\0', sizeof (PeerData));
473 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
475 /* first try to connect when there is no server */
477 c = g_dbus_connection_new_for_address_sync (is_unix ? "unix:path=/tmp/gdbus-test-does-not-exist-pid" :
478 /* NOTE: Even if something is listening on port 12345 the connection
479 * will fail because the nonce file doesn't exist */
480 "nonce-tcp:host=localhost,port=12345,noncefile=this-does-not-exist-gdbus",
481 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
482 NULL, /* GDBusAuthObserver */
483 NULL, /* cancellable */
485 _g_assert_error_domain (error, G_IO_ERROR);
486 g_assert (!g_dbus_error_is_remote_error (error));
487 g_clear_error (&error);
488 g_assert (c == NULL);
490 /* bring up a server - we run the server in a different thread to avoid deadlocks */
492 service_thread = g_thread_create (service_thread_func,
496 while (service_loop == NULL)
498 g_assert (server != NULL);
500 /* bring up a connection and accept it */
501 data.accept_connection = TRUE;
503 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
504 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
505 NULL, /* GDBusAuthObserver */
506 NULL, /* cancellable */
508 g_assert_no_error (error);
509 g_assert (c != NULL);
510 while (data.current_connections->len < 1)
511 g_main_loop_run (loop);
512 g_assert_cmpint (data.current_connections->len, ==, 1);
513 g_assert_cmpint (data.num_connection_attempts, ==, 1);
514 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
515 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
517 /* check that we create a proxy, read properties, receive signals and invoke
518 * the HelloPeer() method. Since the server runs in another thread it's fine
519 * to use synchronous blocking API here.
522 proxy = g_dbus_proxy_new_sync (c,
524 G_DBUS_PROXY_FLAGS_NONE,
527 "/org/gtk/GDBus/PeerTestObject",
528 "org.gtk.GDBus.PeerTestInterface",
529 NULL, /* GCancellable */
531 g_assert_no_error (error);
532 g_assert (proxy != NULL);
534 value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
535 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
537 /* try invoking a method */
539 result = g_dbus_proxy_call_sync (proxy,
541 g_variant_new ("(s)", "Hey Peer!"),
542 G_DBUS_CALL_FLAGS_NONE,
544 NULL, /* GCancellable */
546 g_assert_no_error (error);
547 g_variant_get (result, "(s)", &s);
548 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
549 g_variant_unref (result);
550 g_assert_cmpint (data.num_method_calls, ==, 1);
552 /* make the other peer emit a signal - catch it */
553 g_signal_connect (proxy,
555 G_CALLBACK (on_proxy_signal_received),
557 g_assert (!data.signal_received);
558 g_dbus_proxy_call (proxy,
560 NULL, /* no arguments */
561 G_DBUS_CALL_FLAGS_NONE,
563 NULL, /* GCancellable */
564 NULL, /* GAsyncReadyCallback - we don't care about the result */
565 NULL); /* user_data */
566 g_main_loop_run (loop);
567 g_assert (data.signal_received);
568 g_assert_cmpint (data.num_method_calls, ==, 2);
570 /* check for UNIX fd passing */
573 GDBusMessage *method_call_message;
574 GDBusMessage *method_reply_message;
575 GUnixFDList *fd_list;
582 method_call_message = g_dbus_message_new_method_call (NULL, /* name */
583 "/org/gtk/GDBus/PeerTestObject",
584 "org.gtk.GDBus.PeerTestInterface",
586 g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", "/etc/hosts"));
588 method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
591 NULL, /* out_serial */
592 NULL, /* cancellable */
594 g_assert_no_error (error);
595 g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
596 fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
597 g_assert (fd_list != NULL);
598 g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
600 fd = g_unix_fd_list_get (fd_list, 0, &error);
601 g_assert_no_error (error);
602 g_object_unref (method_call_message);
603 g_object_unref (method_reply_message);
605 memset (buf, '\0', sizeof (buf));
606 len = read (fd, buf, sizeof (buf) - 1);
610 g_file_get_contents ("/etc/hosts",
614 g_assert_no_error (error);
615 if (len2 > sizeof (buf))
616 buf2[sizeof (buf)] = '\0';
617 g_assert_cmpstr (buf, ==, buf2);
620 #endif /* G_OS_UNIX */
623 /* bring up a connection - don't accept it - this should fail
625 data.accept_connection = FALSE;
627 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
628 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
629 NULL, /* GDBusAuthObserver */
630 NULL, /* cancellable */
632 _g_assert_error_domain (error, G_IO_ERROR);
633 g_assert (c2 == NULL);
636 /* TODO: THIS TEST DOESN'T WORK YET */
638 /* bring up a connection - accept it.. then disconnect from the client side - check
639 * that the server side gets the disconnect signal.
642 data.accept_connection = TRUE;
643 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
644 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
645 NULL, /* GDBusAuthObserver */
646 NULL, /* cancellable */
648 g_assert_no_error (error);
649 g_assert (c2 != NULL);
650 g_assert (!g_dbus_connection_get_is_disconnected (c2));
651 while (data.num_connection_attempts < 3)
652 g_main_loop_run (loop);
653 g_assert_cmpint (data.current_connections->len, ==, 2);
654 g_assert_cmpint (data.num_connection_attempts, ==, 3);
655 g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
656 g_idle_add (on_do_disconnect_in_idle, c2);
657 g_debug ("==================================================");
658 g_debug ("==================================================");
659 g_debug ("==================================================");
660 g_debug ("waiting for disconnect on connection %p, stream %p",
661 data.current_connections->pdata[1],
662 g_dbus_connection_get_stream (data.current_connections->pdata[1]));
664 g_timeout_add (2000, check_connection, &data);
665 //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
666 g_main_loop_run (loop);
667 g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
668 g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
671 /* unref the server and stop listening for new connections
673 * This won't bring down the established connections - check that c is still connected
674 * by invoking a method
676 //g_socket_service_stop (service);
677 //g_object_unref (service);
678 g_dbus_server_stop (server);
679 g_object_unref (server);
683 result = g_dbus_proxy_call_sync (proxy,
685 g_variant_new ("(s)", "Hey Again Peer!"),
686 G_DBUS_CALL_FLAGS_NONE,
688 NULL, /* GCancellable */
690 g_assert_no_error (error);
691 g_variant_get (result, "(s)", &s);
692 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
693 g_variant_unref (result);
694 g_assert_cmpint (data.num_method_calls, ==, 4);
697 /* TODO: THIS TEST DOESN'T WORK YET */
699 /* now disconnect from the server side - check that the client side gets the signal */
700 g_assert_cmpint (data.current_connections->len, ==, 1);
701 g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
702 g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
703 if (!g_dbus_connection_get_is_disconnected (c))
704 _g_assert_signal_received (c, "closed");
705 g_assert (g_dbus_connection_get_is_disconnected (c));
709 g_ptr_array_unref (data.current_connections);
710 g_object_unref (proxy);
712 g_main_loop_quit (service_loop);
713 g_thread_join (service_thread);
716 /* ---------------------------------------------------------------------------------------------------- */
723 GDBusNodeInfo *introspection_data = NULL;
726 g_thread_init (NULL);
727 g_test_init (&argc, &argv, NULL);
729 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
730 g_assert (introspection_data != NULL);
731 test_interface_introspection_data = introspection_data->interfaces[0];
733 test_guid = g_dbus_generate_guid ();
735 /* all the tests rely on a shared main loop */
736 loop = g_main_loop_new (NULL, FALSE);
738 g_test_add_func ("/gdbus/peer-to-peer", test_peer);
742 g_main_loop_unref (loop);
744 g_dbus_node_info_unref (introspection_data);