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>
34 #include "gdbus-tests.h"
38 static gboolean is_unix = TRUE;
40 static gboolean is_unix = FALSE;
43 static gchar *test_guid = NULL;
44 static GMainLoop *service_loop = NULL;
45 static GDBusServer *server = NULL;
46 static GMainLoop *loop = NULL;
48 /* ---------------------------------------------------------------------------------------------------- */
49 /* Test that peer-to-peer connections work */
50 /* ---------------------------------------------------------------------------------------------------- */
55 gboolean accept_connection;
56 gint num_connection_attempts;
57 GPtrArray *current_connections;
58 guint num_method_calls;
59 gboolean signal_received;
62 static const gchar *test_interface_introspection_xml =
64 " <interface name='org.gtk.GDBus.PeerTestInterface'>"
65 " <method name='HelloPeer'>"
66 " <arg type='s' name='greeting' direction='in'/>"
67 " <arg type='s' name='response' direction='out'/>"
69 " <method name='EmitSignal'/>"
70 " <method name='OpenFile'>"
71 " <arg type='s' name='path' direction='in'/>"
73 " <signal name='PeerSignal'>"
74 " <arg type='s' name='a_string'/>"
76 " <property type='s' name='PeerProperty' access='read'/>"
79 static const GDBusInterfaceInfo *test_interface_introspection_data = NULL;
82 test_interface_method_call (GDBusConnection *connection,
84 const gchar *object_path,
85 const gchar *interface_name,
86 const gchar *method_name,
88 GDBusMethodInvocation *invocation,
91 PeerData *data = user_data;
93 data->num_method_calls++;
95 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
96 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
98 if (g_strcmp0 (method_name, "HelloPeer") == 0)
100 const gchar *greeting;
103 g_variant_get (parameters, "(s)", &greeting);
105 response = g_strdup_printf ("You greeted me with '%s'.",
107 g_dbus_method_invocation_return_value (invocation,
108 g_variant_new ("(s)", response));
111 else if (g_strcmp0 (method_name, "EmitSignal") == 0)
116 g_dbus_connection_emit_signal (connection,
118 "/org/gtk/GDBus/PeerTestObject",
119 "org.gtk.GDBus.PeerTestInterface",
123 g_assert_no_error (error);
124 g_dbus_method_invocation_return_value (invocation, NULL);
126 else if (g_strcmp0 (method_name, "OpenFile") == 0)
132 GUnixFDList *fd_list;
134 g_variant_get (parameters, "(s)", &path);
136 fd_list = g_unix_fd_list_new ();
140 fd = open (path, O_RDONLY);
141 g_unix_fd_list_append (fd_list, fd, &error);
142 g_assert_no_error (error);
145 reply = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
146 g_dbus_message_set_unix_fd_list (reply, fd_list);
147 g_object_unref (invocation);
150 g_dbus_connection_send_message (connection,
152 NULL, /* out_serial */
154 g_assert_no_error (error);
155 g_object_unref (reply);
159 g_assert_not_reached ();
164 test_interface_get_property (GDBusConnection *connection,
166 const gchar *object_path,
167 const gchar *interface_name,
168 const gchar *property_name,
172 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
173 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
174 g_assert_cmpstr (property_name, ==, "PeerProperty");
176 return g_variant_new_string ("ThePropertyValue");
180 static const GDBusInterfaceVTable test_interface_vtable =
182 test_interface_method_call,
183 test_interface_get_property,
184 NULL /* set_property */
188 on_proxy_signal_received (GDBusProxy *proxy,
191 GVariant *parameters,
194 PeerData *data = user_data;
196 data->signal_received = TRUE;
198 g_assert (sender_name == NULL);
199 g_assert_cmpstr (signal_name, ==, "PeerSignal");
200 g_main_loop_quit (loop);
203 /* ---------------------------------------------------------------------------------------------------- */
206 on_deny_authenticated_peer (GDBusAuthObserver *observer,
208 GCredentials *credentials,
211 PeerData *data = user_data;
214 data->num_connection_attempts++;
217 if (!data->accept_connection)
220 g_main_loop_quit (loop);
226 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
228 on_new_connection (GDBusServer *server,
229 GDBusConnection *connection,
232 PeerData *data = user_data;
236 //g_print ("Client connected.\n"
237 // "Negotiated capabilities: unix-fd-passing=%d\n",
238 // g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
240 g_ptr_array_add (data->current_connections, g_object_ref (connection));
242 /* export object on the newly established connection */
244 reg_id = g_dbus_connection_register_object (connection,
245 "/org/gtk/GDBus/PeerTestObject",
246 "org.gtk.GDBus.PeerTestInterface",
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 "deny-authenticated-peer",
285 G_CALLBACK (on_deny_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 "org.gtk.GDBus.PeerTestInterface",
335 &test_interface_introspection_data,
336 &test_interface_vtable,
338 NULL, /* GDestroyNotify for data */
340 g_assert_no_error (error);
341 g_assert (reg_id > 0);
346 /* don't do anything */
349 data->num_connection_attempts++;
351 g_main_loop_quit (loop);
353 /* stops other signal handlers from being invoked */
358 service_thread_func (gpointer data)
360 GMainContext *service_context;
362 GSocketAddress *address;
365 service_context = g_main_context_new ();
366 g_main_context_push_thread_default (service_context);
368 socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
369 address = g_unix_socket_address_new (socket_path);
371 service = g_socket_service_new ();
373 g_socket_listener_add_address (G_SOCKET_LISTENER (service),
375 G_SOCKET_TYPE_STREAM,
376 G_SOCKET_PROTOCOL_DEFAULT,
377 NULL, /* source_object */
378 NULL, /* effective_address */
380 g_assert_no_error (error);
381 g_signal_connect (service,
383 G_CALLBACK (on_incoming_connection),
385 g_socket_service_start (service);
387 service_loop = g_main_loop_new (service_context, FALSE);
388 g_main_loop_run (service_loop);
390 g_main_context_pop_thread_default (service_context);
392 g_main_loop_unref (service_loop);
393 g_main_context_unref (service_context);
395 g_object_unref (address);
396 g_free (socket_path);
401 /* ---------------------------------------------------------------------------------------------------- */
405 check_connection (gpointer user_data)
407 PeerData *data = user_data;
410 for (n = 0; n < data->current_connections->len; n++)
415 c = G_DBUS_CONNECTION (data->current_connections->pdata[n]);
416 stream = g_dbus_connection_get_stream (c);
418 g_debug ("In check_connection for %d: connection %p, stream %p", n, c, stream);
419 g_debug ("closed = %d", g_io_stream_is_closed (stream));
422 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
423 g_debug ("socket_closed = %d", g_socket_is_closed (socket));
424 g_debug ("socket_condition_check = %d", g_socket_condition_check (socket, G_IO_IN|G_IO_OUT|G_IO_ERR|G_IO_HUP));
430 num_read = g_input_stream_read (g_io_stream_get_input_stream (stream),
437 g_debug ("error: %s", error->message);
438 g_error_free (error);
442 g_debug ("no error, read %d bytes", (gint) num_read);
450 on_do_disconnect_in_idle (gpointer data)
452 GDBusConnection *c = G_DBUS_CONNECTION (data);
453 g_debug ("GDC %p has ref_count %d", c, G_OBJECT (c)->ref_count);
454 g_dbus_connection_disconnect (c);
471 GThread *service_thread;
473 memset (&data, '\0', sizeof (PeerData));
474 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
476 /* first try to connect when there is no server */
478 c = g_dbus_connection_new_for_address_sync (is_unix ? "unix:path=/tmp/gdbus-test-does-not-exist-pid" :
479 /* NOTE: Even if something is listening on port 12345 the connection
480 * will fail because the nonce file doesn't exist */
481 "nonce-tcp:host=localhost,port=12345,noncefile=this-does-not-exist-gdbus",
482 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
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, /* cancellable */
507 g_assert_no_error (error);
508 g_assert (c != NULL);
509 while (data.current_connections->len < 1)
510 g_main_loop_run (loop);
511 g_assert_cmpint (data.current_connections->len, ==, 1);
512 g_assert_cmpint (data.num_connection_attempts, ==, 1);
513 //g_assert (g_dbus_connection_get_bus_type (c) == G_BUS_TYPE_NONE);
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", &error);
535 g_assert_no_error (error);
536 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
538 /* try invoking a method */
540 result = g_dbus_proxy_call_sync (proxy,
542 g_variant_new ("(s)", "Hey Peer!"),
543 G_DBUS_CALL_FLAGS_NONE,
545 NULL, /* GCancellable */
547 g_assert_no_error (error);
548 g_variant_get (result, "(s)", &s);
549 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
550 g_variant_unref (result);
551 g_assert_cmpint (data.num_method_calls, ==, 1);
553 /* make the other peer emit a signal - catch it */
554 g_signal_connect (proxy,
556 G_CALLBACK (on_proxy_signal_received),
558 g_assert (!data.signal_received);
559 g_dbus_proxy_call (proxy,
561 NULL, /* no arguments */
562 G_DBUS_CALL_FLAGS_NONE,
564 NULL, /* GCancellable */
565 NULL, /* GAsyncReadyCallback - we don't care about the result */
566 NULL); /* user_data */
567 g_main_loop_run (loop);
568 g_assert (data.signal_received);
569 g_assert_cmpint (data.num_method_calls, ==, 2);
571 /* check for UNIX fd passing */
574 GDBusMessage *method_call_message;
575 GDBusMessage *method_reply_message;
576 GUnixFDList *fd_list;
583 method_call_message = g_dbus_message_new_method_call (NULL, /* name */
584 "/org/gtk/GDBus/PeerTestObject",
585 "org.gtk.GDBus.PeerTestInterface",
587 g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", "/etc/hosts"));
589 method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
592 NULL, /* out_serial */
593 NULL, /* cancellable */
595 g_assert_no_error (error);
596 g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
597 fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
598 g_assert (fd_list != NULL);
599 g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
601 fd = g_unix_fd_list_get (fd_list, 0, &error);
602 g_assert_no_error (error);
603 g_object_unref (method_call_message);
604 g_object_unref (method_reply_message);
606 memset (buf, '\0', sizeof (buf));
607 len = read (fd, buf, sizeof (buf) - 1);
611 g_file_get_contents ("/etc/hosts",
615 g_assert_no_error (error);
616 if (len2 > sizeof (buf))
617 buf2[sizeof (buf)] = '\0';
618 g_assert_cmpstr (buf, ==, buf2);
621 #endif /* G_OS_UNIX */
624 /* bring up a connection - don't accept it - this should fail
626 data.accept_connection = FALSE;
628 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
629 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
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, /* cancellable */
647 g_assert_no_error (error);
648 g_assert (c2 != NULL);
649 g_assert (!g_dbus_connection_get_is_disconnected (c2));
650 while (data.num_connection_attempts < 3)
651 g_main_loop_run (loop);
652 g_assert_cmpint (data.current_connections->len, ==, 2);
653 g_assert_cmpint (data.num_connection_attempts, ==, 3);
654 g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
655 g_idle_add (on_do_disconnect_in_idle, c2);
656 g_debug ("==================================================");
657 g_debug ("==================================================");
658 g_debug ("==================================================");
659 g_debug ("waiting for disconnect on connection %p, stream %p",
660 data.current_connections->pdata[1],
661 g_dbus_connection_get_stream (data.current_connections->pdata[1]));
663 g_timeout_add (2000, check_connection, &data);
664 //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
665 g_main_loop_run (loop);
666 g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
667 g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
670 /* unref the server and stop listening for new connections
672 * This won't bring down the established connections - check that c is still connected
673 * by invoking a method
675 //g_socket_service_stop (service);
676 //g_object_unref (service);
677 g_dbus_server_stop (server);
678 g_object_unref (server);
682 result = g_dbus_proxy_call_sync (proxy,
684 g_variant_new ("(s)", "Hey Again Peer!"),
685 G_DBUS_CALL_FLAGS_NONE,
687 NULL, /* GCancellable */
689 g_assert_no_error (error);
690 g_variant_get (result, "(s)", &s);
691 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
692 g_variant_unref (result);
693 g_assert_cmpint (data.num_method_calls, ==, 4);
696 /* TODO: THIS TEST DOESN'T WORK YET */
698 /* now disconnect from the server side - check that the client side gets the signal */
699 g_assert_cmpint (data.current_connections->len, ==, 1);
700 g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
701 g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
702 if (!g_dbus_connection_get_is_disconnected (c))
703 _g_assert_signal_received (c, "closed");
704 g_assert (g_dbus_connection_get_is_disconnected (c));
708 g_ptr_array_unref (data.current_connections);
709 g_object_unref (proxy);
711 g_main_loop_quit (service_loop);
712 g_thread_join (service_thread);
715 /* ---------------------------------------------------------------------------------------------------- */
722 GDBusNodeInfo *introspection_data = NULL;
725 g_thread_init (NULL);
726 g_test_init (&argc, &argv, NULL);
728 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
729 g_assert (introspection_data != NULL);
730 test_interface_introspection_data = introspection_data->interfaces[0];
732 test_guid = g_dbus_generate_guid ();
734 /* all the tests rely on a shared main loop */
735 loop = g_main_loop_new (NULL, FALSE);
737 g_test_add_func ("/gdbus/peer-to-peer", test_peer);
741 g_main_loop_unref (loop);
743 g_dbus_node_info_unref (introspection_data);