Add some more cases to the app-id unit tests
[glib.git] / gio / tests / gdbus-peer.c
blob081eef6f59d0b67440857e59eaffb26c2ab26a92
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, see <http://www.gnu.org/licenses/>.
18 * Author: David Zeuthen <davidz@redhat.com>
21 #include "config.h"
23 #include <gio/gio.h>
24 #include <unistd.h>
25 #include <string.h>
27 /* for open(2) */
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <string.h>
33 /* for g_unlink() */
34 #include <glib/gstdio.h>
36 #include <gio/gnetworking.h>
37 #include <gio/gunixsocketaddress.h>
38 #include <gio/gunixfdlist.h>
39 #include <gio/gcredentialsprivate.h>
41 #ifdef G_OS_UNIX
42 #include <gio/gunixconnection.h>
43 #include <errno.h>
44 #endif
46 #include "gdbus-tests.h"
48 #include "gdbus-object-manager-example/gdbus-example-objectmanager-generated.h"
50 #ifdef G_OS_UNIX
51 static gboolean is_unix = TRUE;
52 #else
53 static gboolean is_unix = FALSE;
54 #endif
56 static gchar *tmp_address = NULL;
57 static gchar *test_guid = NULL;
58 static GMutex service_loop_lock;
59 static GCond service_loop_cond;
60 static GMainLoop *service_loop = NULL;
61 static GDBusServer *server = NULL;
62 static GMainLoop *loop = NULL;
64 /* ---------------------------------------------------------------------------------------------------- */
65 /* Test that peer-to-peer connections work */
66 /* ---------------------------------------------------------------------------------------------------- */
69 typedef struct
71 gboolean accept_connection;
72 gint num_connection_attempts;
73 GPtrArray *current_connections;
74 guint num_method_calls;
75 gboolean signal_received;
76 } PeerData;
78 static const gchar *test_interface_introspection_xml =
79 "<node>"
80 " <interface name='org.gtk.GDBus.PeerTestInterface'>"
81 " <method name='HelloPeer'>"
82 " <arg type='s' name='greeting' direction='in'/>"
83 " <arg type='s' name='response' direction='out'/>"
84 " </method>"
85 " <method name='EmitSignal'/>"
86 " <method name='EmitSignalWithNameSet'/>"
87 " <method name='OpenFile'>"
88 " <arg type='s' name='path' direction='in'/>"
89 " </method>"
90 " <signal name='PeerSignal'>"
91 " <arg type='s' name='a_string'/>"
92 " </signal>"
93 " <property type='s' name='PeerProperty' access='read'/>"
94 " </interface>"
95 "</node>";
96 static GDBusInterfaceInfo *test_interface_introspection_data = NULL;
98 static void
99 test_interface_method_call (GDBusConnection *connection,
100 const gchar *sender,
101 const gchar *object_path,
102 const gchar *interface_name,
103 const gchar *method_name,
104 GVariant *parameters,
105 GDBusMethodInvocation *invocation,
106 gpointer user_data)
108 PeerData *data = user_data;
109 const GDBusMethodInfo *info;
111 data->num_method_calls++;
113 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
114 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
116 info = g_dbus_method_invocation_get_method_info (invocation);
117 g_assert_cmpstr (info->name, ==, method_name);
119 if (g_strcmp0 (method_name, "HelloPeer") == 0)
121 const gchar *greeting;
122 gchar *response;
124 g_variant_get (parameters, "(&s)", &greeting);
126 response = g_strdup_printf ("You greeted me with '%s'.",
127 greeting);
128 g_dbus_method_invocation_return_value (invocation,
129 g_variant_new ("(s)", response));
130 g_free (response);
132 else if (g_strcmp0 (method_name, "EmitSignal") == 0)
134 GError *error;
136 error = NULL;
137 g_dbus_connection_emit_signal (connection,
138 NULL,
139 "/org/gtk/GDBus/PeerTestObject",
140 "org.gtk.GDBus.PeerTestInterface",
141 "PeerSignal",
142 NULL,
143 &error);
144 g_assert_no_error (error);
145 g_dbus_method_invocation_return_value (invocation, NULL);
147 else if (g_strcmp0 (method_name, "EmitSignalWithNameSet") == 0)
149 GError *error;
150 gboolean ret;
151 GDBusMessage *message;
153 message = g_dbus_message_new_signal ("/org/gtk/GDBus/PeerTestObject",
154 "org.gtk.GDBus.PeerTestInterface",
155 "PeerSignalWithNameSet");
156 g_dbus_message_set_sender (message, ":1.42");
158 error = NULL;
159 ret = g_dbus_connection_send_message (connection, message, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error);
160 g_assert_no_error (error);
161 g_assert (ret);
162 g_object_unref (message);
164 g_dbus_method_invocation_return_value (invocation, NULL);
166 else if (g_strcmp0 (method_name, "OpenFile") == 0)
168 #ifdef G_OS_UNIX
169 const gchar *path;
170 GDBusMessage *reply;
171 GError *error;
172 gint fd;
173 GUnixFDList *fd_list;
175 g_variant_get (parameters, "(&s)", &path);
177 fd_list = g_unix_fd_list_new ();
179 error = NULL;
181 fd = g_open (path, O_RDONLY, 0);
182 g_assert (fd != -1);
183 g_unix_fd_list_append (fd_list, fd, &error);
184 g_assert_no_error (error);
185 close (fd);
187 reply = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
188 g_dbus_message_set_unix_fd_list (reply, fd_list);
189 g_object_unref (fd_list);
190 g_object_unref (invocation);
192 error = NULL;
193 g_dbus_connection_send_message (connection,
194 reply,
195 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
196 NULL, /* out_serial */
197 &error);
198 g_assert_no_error (error);
199 g_object_unref (reply);
200 #else
201 g_dbus_method_invocation_return_dbus_error (invocation,
202 "org.gtk.GDBus.NotOnUnix",
203 "Your OS does not support file descriptor passing");
204 #endif
206 else
208 g_assert_not_reached ();
212 static GVariant *
213 test_interface_get_property (GDBusConnection *connection,
214 const gchar *sender,
215 const gchar *object_path,
216 const gchar *interface_name,
217 const gchar *property_name,
218 GError **error,
219 gpointer user_data)
221 g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
222 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
223 g_assert_cmpstr (property_name, ==, "PeerProperty");
225 return g_variant_new_string ("ThePropertyValue");
229 static const GDBusInterfaceVTable test_interface_vtable =
231 test_interface_method_call,
232 test_interface_get_property,
233 NULL /* set_property */
236 static void
237 on_proxy_signal_received (GDBusProxy *proxy,
238 gchar *sender_name,
239 gchar *signal_name,
240 GVariant *parameters,
241 gpointer user_data)
243 PeerData *data = user_data;
245 data->signal_received = TRUE;
247 g_assert (sender_name == NULL);
248 g_assert_cmpstr (signal_name, ==, "PeerSignal");
249 g_main_loop_quit (loop);
252 static void
253 on_proxy_signal_received_with_name_set (GDBusProxy *proxy,
254 gchar *sender_name,
255 gchar *signal_name,
256 GVariant *parameters,
257 gpointer user_data)
259 PeerData *data = user_data;
261 data->signal_received = TRUE;
263 g_assert_cmpstr (sender_name, ==, ":1.42");
264 g_assert_cmpstr (signal_name, ==, "PeerSignalWithNameSet");
265 g_main_loop_quit (loop);
268 /* ---------------------------------------------------------------------------------------------------- */
270 static gboolean
271 on_authorize_authenticated_peer (GDBusAuthObserver *observer,
272 GIOStream *stream,
273 GCredentials *credentials,
274 gpointer user_data)
276 PeerData *data = user_data;
277 gboolean authorized;
279 data->num_connection_attempts++;
281 authorized = TRUE;
282 if (!data->accept_connection)
284 authorized = FALSE;
285 g_main_loop_quit (loop);
288 return authorized;
291 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
292 static gboolean
293 on_new_connection (GDBusServer *server,
294 GDBusConnection *connection,
295 gpointer user_data)
297 PeerData *data = user_data;
298 GError *error;
299 guint reg_id;
301 //g_printerr ("Client connected.\n"
302 // "Negotiated capabilities: unix-fd-passing=%d\n",
303 // g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
305 g_ptr_array_add (data->current_connections, g_object_ref (connection));
307 #if G_CREDENTIALS_SUPPORTED
309 GCredentials *credentials;
311 credentials = g_dbus_connection_get_peer_credentials (connection);
313 g_assert (credentials != NULL);
314 g_assert_cmpuint (g_credentials_get_unix_user (credentials, NULL), ==,
315 getuid ());
316 g_assert_cmpuint (g_credentials_get_unix_pid (credentials, NULL), ==,
317 getpid ());
319 #endif
321 /* export object on the newly established connection */
322 error = NULL;
323 reg_id = g_dbus_connection_register_object (connection,
324 "/org/gtk/GDBus/PeerTestObject",
325 test_interface_introspection_data,
326 &test_interface_vtable,
327 data,
328 NULL, /* GDestroyNotify for data */
329 &error);
330 g_assert_no_error (error);
331 g_assert (reg_id > 0);
333 g_main_loop_quit (loop);
335 return TRUE;
338 /* We don't tell the main thread about the new GDBusServer until it has
339 * had a chance to start listening. */
340 static gboolean
341 idle_in_service_loop (gpointer loop)
343 g_assert (service_loop == NULL);
344 g_mutex_lock (&service_loop_lock);
345 service_loop = loop;
346 g_cond_broadcast (&service_loop_cond);
347 g_mutex_unlock (&service_loop_lock);
349 return G_SOURCE_REMOVE;
352 static void
353 run_service_loop (GMainContext *service_context)
355 GMainLoop *loop;
356 GSource *source;
358 g_assert (service_loop == NULL);
360 loop = g_main_loop_new (service_context, FALSE);
361 source = g_idle_source_new ();
362 g_source_set_callback (source, idle_in_service_loop, loop, NULL);
363 g_source_attach (source, service_context);
364 g_source_unref (source);
365 g_main_loop_run (loop);
368 static void
369 teardown_service_loop (void)
371 g_mutex_lock (&service_loop_lock);
372 g_clear_pointer (&service_loop, g_main_loop_unref);
373 g_mutex_unlock (&service_loop_lock);
376 static void
377 await_service_loop (void)
379 g_mutex_lock (&service_loop_lock);
380 while (service_loop == NULL)
381 g_cond_wait (&service_loop_cond, &service_loop_lock);
382 g_mutex_unlock (&service_loop_lock);
385 static gpointer
386 service_thread_func (gpointer user_data)
388 PeerData *data = user_data;
389 GMainContext *service_context;
390 GDBusAuthObserver *observer, *o;
391 GError *error;
392 GDBusServerFlags f;
393 gchar *a, *g;
394 gboolean b;
396 service_context = g_main_context_new ();
397 g_main_context_push_thread_default (service_context);
399 error = NULL;
400 observer = g_dbus_auth_observer_new ();
401 server = g_dbus_server_new_sync (tmp_address,
402 G_DBUS_SERVER_FLAGS_NONE,
403 test_guid,
404 observer,
405 NULL, /* cancellable */
406 &error);
407 g_assert_no_error (error);
409 g_signal_connect (server,
410 "new-connection",
411 G_CALLBACK (on_new_connection),
412 data);
413 g_signal_connect (observer,
414 "authorize-authenticated-peer",
415 G_CALLBACK (on_authorize_authenticated_peer),
416 data);
418 g_assert_cmpint (g_dbus_server_get_flags (server), ==, G_DBUS_SERVER_FLAGS_NONE);
419 g_assert_cmpstr (g_dbus_server_get_guid (server), ==, test_guid);
420 g_object_get (server,
421 "flags", &f,
422 "address", &a,
423 "guid", &g,
424 "active", &b,
425 "authentication-observer", &o,
426 NULL);
427 g_assert_cmpint (f, ==, G_DBUS_SERVER_FLAGS_NONE);
428 g_assert_cmpstr (a, ==, tmp_address);
429 g_assert_cmpstr (g, ==, test_guid);
430 g_assert (!b);
431 g_assert (o == observer);
432 g_free (a);
433 g_free (g);
434 g_object_unref (o);
436 g_object_unref (observer);
438 g_dbus_server_start (server);
440 run_service_loop (service_context);
442 g_main_context_pop_thread_default (service_context);
444 teardown_service_loop ();
445 g_main_context_unref (service_context);
447 /* test code specifically unrefs the server - see below */
448 g_assert (server == NULL);
450 return NULL;
453 #if 0
454 static gboolean
455 on_incoming_connection (GSocketService *service,
456 GSocketConnection *socket_connection,
457 GObject *source_object,
458 gpointer user_data)
460 PeerData *data = user_data;
462 if (data->accept_connection)
464 GError *error;
465 guint reg_id;
466 GDBusConnection *connection;
468 error = NULL;
469 connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
470 test_guid,
471 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
472 NULL, /* cancellable */
473 &error);
474 g_assert_no_error (error);
476 g_ptr_array_add (data->current_connections, connection);
478 /* export object on the newly established connection */
479 error = NULL;
480 reg_id = g_dbus_connection_register_object (connection,
481 "/org/gtk/GDBus/PeerTestObject",
482 &test_interface_introspection_data,
483 &test_interface_vtable,
484 data,
485 NULL, /* GDestroyNotify for data */
486 &error);
487 g_assert_no_error (error);
488 g_assert (reg_id > 0);
491 else
493 /* don't do anything */
496 data->num_connection_attempts++;
498 g_main_loop_quit (loop);
500 /* stops other signal handlers from being invoked */
501 return TRUE;
504 static gpointer
505 service_thread_func (gpointer data)
507 GMainContext *service_context;
508 gchar *socket_path;
509 GSocketAddress *address;
510 GError *error;
512 service_context = g_main_context_new ();
513 g_main_context_push_thread_default (service_context);
515 socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
516 address = g_unix_socket_address_new (socket_path);
518 service = g_socket_service_new ();
519 error = NULL;
520 g_socket_listener_add_address (G_SOCKET_LISTENER (service),
521 address,
522 G_SOCKET_TYPE_STREAM,
523 G_SOCKET_PROTOCOL_DEFAULT,
524 NULL, /* source_object */
525 NULL, /* effective_address */
526 &error);
527 g_assert_no_error (error);
528 g_signal_connect (service,
529 "incoming",
530 G_CALLBACK (on_incoming_connection),
531 data);
532 g_socket_service_start (service);
534 run_service_loop (service_context);
536 g_main_context_pop_thread_default (service_context);
538 teardown_service_loop ();
539 g_main_context_unref (service_context);
541 g_object_unref (address);
542 g_free (socket_path);
543 return NULL;
545 #endif
547 /* ---------------------------------------------------------------------------------------------------- */
549 #if 0
550 static gboolean
551 check_connection (gpointer user_data)
553 PeerData *data = user_data;
554 guint n;
556 for (n = 0; n < data->current_connections->len; n++)
558 GDBusConnection *c;
559 GIOStream *stream;
561 c = G_DBUS_CONNECTION (data->current_connections->pdata[n]);
562 stream = g_dbus_connection_get_stream (c);
564 g_debug ("In check_connection for %d: connection %p, stream %p", n, c, stream);
565 g_debug ("closed = %d", g_io_stream_is_closed (stream));
567 GSocket *socket;
568 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
569 g_debug ("socket_closed = %d", g_socket_is_closed (socket));
570 g_debug ("socket_condition_check = %d", g_socket_condition_check (socket, G_IO_IN|G_IO_OUT|G_IO_ERR|G_IO_HUP));
572 gchar buf[128];
573 GError *error;
574 gssize num_read;
575 error = NULL;
576 num_read = g_input_stream_read (g_io_stream_get_input_stream (stream),
577 buf,
578 128,
579 NULL,
580 &error);
581 if (num_read < 0)
583 g_debug ("error: %s", error->message);
584 g_error_free (error);
586 else
588 g_debug ("no error, read %d bytes", (gint) num_read);
592 return FALSE;
595 static gboolean
596 on_do_disconnect_in_idle (gpointer data)
598 GDBusConnection *c = G_DBUS_CONNECTION (data);
599 g_debug ("GDC %p has ref_count %d", c, G_OBJECT (c)->ref_count);
600 g_dbus_connection_disconnect (c);
601 g_object_unref (c);
602 return FALSE;
604 #endif
606 #ifdef G_OS_UNIX
607 static gchar *
608 read_all_from_fd (gint fd, gsize *out_len, GError **error)
610 GString *str;
611 gchar buf[64];
612 gssize num_read;
614 str = g_string_new (NULL);
618 num_read = read (fd, buf, sizeof (buf));
619 if (num_read == -1)
621 if (errno == EAGAIN || errno == EWOULDBLOCK)
622 continue;
623 g_set_error (error,
624 G_IO_ERROR,
625 g_io_error_from_errno (errno),
626 "Failed reading %d bytes into offset %d: %s",
627 (gint) sizeof (buf),
628 (gint) str->len,
629 strerror (errno));
630 goto error;
632 else if (num_read > 0)
634 g_string_append_len (str, buf, num_read);
636 else if (num_read == 0)
638 break;
641 while (TRUE);
643 if (out_len != NULL)
644 *out_len = str->len;
645 return g_string_free (str, FALSE);
647 error:
648 if (out_len != NULL)
649 *out_len = 0;
650 g_string_free (str, TRUE);
651 return NULL;
653 #endif
655 static void
656 test_peer (void)
658 GDBusConnection *c;
659 GDBusConnection *c2;
660 GDBusProxy *proxy;
661 GError *error;
662 PeerData data;
663 GVariant *value;
664 GVariant *result;
665 const gchar *s;
666 GThread *service_thread;
667 gulong signal_handler_id;
669 memset (&data, '\0', sizeof (PeerData));
670 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
672 /* first try to connect when there is no server */
673 error = NULL;
674 c = g_dbus_connection_new_for_address_sync (is_unix ? "unix:path=/tmp/gdbus-test-does-not-exist-pid" :
675 /* NOTE: Even if something is listening on port 12345 the connection
676 * will fail because the nonce file doesn't exist */
677 "nonce-tcp:host=localhost,port=12345,noncefile=this-does-not-exist-gdbus",
678 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
679 NULL, /* GDBusAuthObserver */
680 NULL, /* cancellable */
681 &error);
682 _g_assert_error_domain (error, G_IO_ERROR);
683 g_assert (!g_dbus_error_is_remote_error (error));
684 g_clear_error (&error);
685 g_assert (c == NULL);
687 /* bring up a server - we run the server in a different thread to avoid deadlocks */
688 service_thread = g_thread_new ("test_peer",
689 service_thread_func,
690 &data);
691 await_service_loop ();
692 g_assert (server != NULL);
694 /* bring up a connection and accept it */
695 data.accept_connection = TRUE;
696 error = NULL;
697 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
698 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
699 NULL, /* GDBusAuthObserver */
700 NULL, /* cancellable */
701 &error);
702 g_assert_no_error (error);
703 g_assert (c != NULL);
704 while (data.current_connections->len < 1)
705 g_main_loop_run (loop);
706 g_assert_cmpint (data.current_connections->len, ==, 1);
707 g_assert_cmpint (data.num_connection_attempts, ==, 1);
708 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
709 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
711 /* check that we create a proxy, read properties, receive signals and invoke
712 * the HelloPeer() method. Since the server runs in another thread it's fine
713 * to use synchronous blocking API here.
715 error = NULL;
716 proxy = g_dbus_proxy_new_sync (c,
717 G_DBUS_PROXY_FLAGS_NONE,
718 NULL,
719 NULL, /* bus_name */
720 "/org/gtk/GDBus/PeerTestObject",
721 "org.gtk.GDBus.PeerTestInterface",
722 NULL, /* GCancellable */
723 &error);
724 g_assert_no_error (error);
725 g_assert (proxy != NULL);
726 error = NULL;
727 value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
728 g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
730 /* try invoking a method */
731 error = NULL;
732 result = g_dbus_proxy_call_sync (proxy,
733 "HelloPeer",
734 g_variant_new ("(s)", "Hey Peer!"),
735 G_DBUS_CALL_FLAGS_NONE,
737 NULL, /* GCancellable */
738 &error);
739 g_assert_no_error (error);
740 g_variant_get (result, "(&s)", &s);
741 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
742 g_variant_unref (result);
743 g_assert_cmpint (data.num_method_calls, ==, 1);
745 /* make the other peer emit a signal - catch it */
746 signal_handler_id = g_signal_connect (proxy,
747 "g-signal",
748 G_CALLBACK (on_proxy_signal_received),
749 &data);
750 g_assert (!data.signal_received);
751 g_dbus_proxy_call (proxy,
752 "EmitSignal",
753 NULL, /* no arguments */
754 G_DBUS_CALL_FLAGS_NONE,
756 NULL, /* GCancellable */
757 NULL, /* GAsyncReadyCallback - we don't care about the result */
758 NULL); /* user_data */
759 g_main_loop_run (loop);
760 g_assert (data.signal_received);
761 g_assert_cmpint (data.num_method_calls, ==, 2);
762 g_signal_handler_disconnect (proxy, signal_handler_id);
764 /* Also ensure that messages with the sender header-field set gets
765 * delivered to the proxy - note that this doesn't really make sense
766 * e.g. names are meaning-less in a peer-to-peer case... but we
767 * support it because it makes sense in certain bridging
768 * applications - see e.g. #623815.
770 signal_handler_id = g_signal_connect (proxy,
771 "g-signal",
772 G_CALLBACK (on_proxy_signal_received_with_name_set),
773 &data);
774 data.signal_received = FALSE;
775 g_dbus_proxy_call (proxy,
776 "EmitSignalWithNameSet",
777 NULL, /* no arguments */
778 G_DBUS_CALL_FLAGS_NONE,
780 NULL, /* GCancellable */
781 NULL, /* GAsyncReadyCallback - we don't care about the result */
782 NULL); /* user_data */
783 g_main_loop_run (loop);
784 g_assert (data.signal_received);
785 g_assert_cmpint (data.num_method_calls, ==, 3);
786 g_signal_handler_disconnect (proxy, signal_handler_id);
788 /* check for UNIX fd passing */
789 #ifdef G_OS_UNIX
791 GDBusMessage *method_call_message;
792 GDBusMessage *method_reply_message;
793 GUnixFDList *fd_list;
794 gint fd;
795 gchar *buf;
796 gsize len;
797 gchar *buf2;
798 gsize len2;
799 const char *testfile = g_test_get_filename (G_TEST_DIST, "file.c", NULL);
801 method_call_message = g_dbus_message_new_method_call (NULL, /* name */
802 "/org/gtk/GDBus/PeerTestObject",
803 "org.gtk.GDBus.PeerTestInterface",
804 "OpenFile");
805 g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", testfile));
806 error = NULL;
807 method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
808 method_call_message,
809 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
811 NULL, /* out_serial */
812 NULL, /* cancellable */
813 &error);
814 g_assert_no_error (error);
815 g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
816 fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
817 g_assert (fd_list != NULL);
818 g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
819 error = NULL;
820 fd = g_unix_fd_list_get (fd_list, 0, &error);
821 g_assert_no_error (error);
822 g_object_unref (method_call_message);
823 g_object_unref (method_reply_message);
825 error = NULL;
826 len = 0;
827 buf = read_all_from_fd (fd, &len, &error);
828 g_assert_no_error (error);
829 g_assert (buf != NULL);
830 close (fd);
832 error = NULL;
833 g_file_get_contents (testfile,
834 &buf2,
835 &len2,
836 &error);
837 g_assert_no_error (error);
838 g_assert_cmpmem (buf, len, buf2, len2);
839 g_free (buf2);
840 g_free (buf);
842 #else
843 error = NULL;
844 result = g_dbus_proxy_call_sync (proxy,
845 "OpenFile",
846 g_variant_new ("(s)", "boo"),
847 G_DBUS_CALL_FLAGS_NONE,
849 NULL, /* GCancellable */
850 &error);
851 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
852 g_assert (result == NULL);
853 g_error_free (error);
854 #endif /* G_OS_UNIX */
856 /* Check that g_socket_get_credentials() work - (though this really
857 * should be in socket.c)
860 GSocket *socket;
861 GCredentials *credentials;
862 socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (g_dbus_connection_get_stream (c)));
863 g_assert (G_IS_SOCKET (socket));
864 error = NULL;
865 credentials = g_socket_get_credentials (socket, &error);
867 #if G_CREDENTIALS_SOCKET_GET_CREDENTIALS_SUPPORTED
868 g_assert_no_error (error);
869 g_assert (G_IS_CREDENTIALS (credentials));
871 g_assert_cmpuint (g_credentials_get_unix_user (credentials, NULL), ==,
872 getuid ());
873 g_assert_cmpuint (g_credentials_get_unix_pid (credentials, NULL), ==,
874 getpid ());
875 #else
876 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
877 g_assert (credentials == NULL);
878 #endif
882 /* bring up a connection - don't accept it - this should fail
884 data.accept_connection = FALSE;
885 error = NULL;
886 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
887 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
888 NULL, /* GDBusAuthObserver */
889 NULL, /* cancellable */
890 &error);
891 _g_assert_error_domain (error, G_IO_ERROR);
892 g_error_free (error);
893 g_assert (c2 == NULL);
895 #if 0
896 /* TODO: THIS TEST DOESN'T WORK YET */
898 /* bring up a connection - accept it.. then disconnect from the client side - check
899 * that the server side gets the disconnect signal.
901 error = NULL;
902 data.accept_connection = TRUE;
903 c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
904 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
905 NULL, /* GDBusAuthObserver */
906 NULL, /* cancellable */
907 &error);
908 g_assert_no_error (error);
909 g_assert (c2 != NULL);
910 g_assert (!g_dbus_connection_get_is_disconnected (c2));
911 while (data.num_connection_attempts < 3)
912 g_main_loop_run (loop);
913 g_assert_cmpint (data.current_connections->len, ==, 2);
914 g_assert_cmpint (data.num_connection_attempts, ==, 3);
915 g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
916 g_idle_add (on_do_disconnect_in_idle, c2);
917 g_debug ("==================================================");
918 g_debug ("==================================================");
919 g_debug ("==================================================");
920 g_debug ("waiting for disconnect on connection %p, stream %p",
921 data.current_connections->pdata[1],
922 g_dbus_connection_get_stream (data.current_connections->pdata[1]));
924 g_timeout_add (2000, check_connection, &data);
925 //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
926 g_main_loop_run (loop);
927 g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
928 g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
929 #endif
931 /* unref the server and stop listening for new connections
933 * This won't bring down the established connections - check that c is still connected
934 * by invoking a method
936 //g_socket_service_stop (service);
937 //g_object_unref (service);
938 g_dbus_server_stop (server);
939 g_object_unref (server);
940 server = NULL;
942 error = NULL;
943 result = g_dbus_proxy_call_sync (proxy,
944 "HelloPeer",
945 g_variant_new ("(s)", "Hey Again Peer!"),
946 G_DBUS_CALL_FLAGS_NONE,
948 NULL, /* GCancellable */
949 &error);
950 g_assert_no_error (error);
951 g_variant_get (result, "(&s)", &s);
952 g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
953 g_variant_unref (result);
954 g_assert_cmpint (data.num_method_calls, ==, 5);
956 #if 0
957 /* TODO: THIS TEST DOESN'T WORK YET */
959 /* now disconnect from the server side - check that the client side gets the signal */
960 g_assert_cmpint (data.current_connections->len, ==, 1);
961 g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
962 g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
963 if (!g_dbus_connection_get_is_disconnected (c))
964 _g_assert_signal_received (c, "closed");
965 g_assert (g_dbus_connection_get_is_disconnected (c));
966 #endif
968 g_object_unref (c);
969 g_ptr_array_unref (data.current_connections);
970 g_object_unref (proxy);
972 g_main_loop_quit (service_loop);
973 g_thread_join (service_thread);
976 /* ---------------------------------------------------------------------------------------------------- */
978 typedef struct
980 GDBusServer *server;
981 GMainContext *context;
982 GMainLoop *loop;
984 GList *connections;
985 } DmpData;
987 static void
988 dmp_data_free (DmpData *data)
990 g_main_loop_unref (data->loop);
991 g_main_context_unref (data->context);
992 g_object_unref (data->server);
993 g_list_free_full (data->connections, g_object_unref);
994 g_free (data);
997 static void
998 dmp_on_method_call (GDBusConnection *connection,
999 const gchar *sender,
1000 const gchar *object_path,
1001 const gchar *interface_name,
1002 const gchar *method_name,
1003 GVariant *parameters,
1004 GDBusMethodInvocation *invocation,
1005 gpointer user_data)
1007 //DmpData *data = user_data;
1008 gint32 first;
1009 gint32 second;
1010 g_variant_get (parameters,
1011 "(ii)",
1012 &first,
1013 &second);
1014 g_dbus_method_invocation_return_value (invocation,
1015 g_variant_new ("(i)", first + second));
1018 static const GDBusInterfaceVTable dmp_interface_vtable =
1020 dmp_on_method_call,
1021 NULL, /* get_property */
1022 NULL /* set_property */
1026 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1027 static gboolean
1028 dmp_on_new_connection (GDBusServer *server,
1029 GDBusConnection *connection,
1030 gpointer user_data)
1032 DmpData *data = user_data;
1033 GDBusNodeInfo *node;
1034 GError *error;
1036 /* accept the connection */
1037 data->connections = g_list_prepend (data->connections, g_object_ref (connection));
1039 error = NULL;
1040 node = g_dbus_node_info_new_for_xml ("<node>"
1041 " <interface name='org.gtk.GDBus.DmpInterface'>"
1042 " <method name='AddPair'>"
1043 " <arg type='i' name='first' direction='in'/>"
1044 " <arg type='i' name='second' direction='in'/>"
1045 " <arg type='i' name='sum' direction='out'/>"
1046 " </method>"
1047 " </interface>"
1048 "</node>",
1049 &error);
1050 g_assert_no_error (error);
1052 /* sleep 100ms before exporting an object - this is to test that
1053 * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
1054 * (GDBusServer uses this feature).
1056 usleep (100 * 1000);
1058 /* export an object */
1059 error = NULL;
1060 g_dbus_connection_register_object (connection,
1061 "/dmp/test",
1062 node->interfaces[0],
1063 &dmp_interface_vtable,
1064 data,
1065 NULL,
1066 &error);
1067 g_dbus_node_info_unref (node);
1069 return TRUE;
1072 static gpointer
1073 dmp_thread_func (gpointer user_data)
1075 DmpData *data = user_data;
1076 GError *error;
1077 gchar *guid;
1079 data->context = g_main_context_new ();
1080 g_main_context_push_thread_default (data->context);
1082 error = NULL;
1083 guid = g_dbus_generate_guid ();
1084 data->server = g_dbus_server_new_sync (tmp_address,
1085 G_DBUS_SERVER_FLAGS_NONE,
1086 guid,
1087 NULL, /* GDBusAuthObserver */
1088 NULL, /* GCancellable */
1089 &error);
1090 g_assert_no_error (error);
1091 g_signal_connect (data->server,
1092 "new-connection",
1093 G_CALLBACK (dmp_on_new_connection),
1094 data);
1096 g_dbus_server_start (data->server);
1098 data->loop = g_main_loop_new (data->context, FALSE);
1099 g_main_loop_run (data->loop);
1101 g_main_context_pop_thread_default (data->context);
1103 g_free (guid);
1104 return NULL;
1107 static void
1108 delayed_message_processing (void)
1110 GError *error;
1111 DmpData *data;
1112 GThread *service_thread;
1113 guint n;
1115 data = g_new0 (DmpData, 1);
1117 service_thread = g_thread_new ("dmp",
1118 dmp_thread_func,
1119 data);
1120 while (data->server == NULL || !g_dbus_server_is_active (data->server))
1121 g_thread_yield ();
1123 for (n = 0; n < 5; n++)
1125 GDBusConnection *c;
1126 GVariant *res;
1127 gint32 val;
1129 error = NULL;
1130 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
1131 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1132 NULL, /* GDBusAuthObserver */
1133 NULL, /* GCancellable */
1134 &error);
1135 g_assert_no_error (error);
1137 error = NULL;
1138 res = g_dbus_connection_call_sync (c,
1139 NULL, /* bus name */
1140 "/dmp/test",
1141 "org.gtk.GDBus.DmpInterface",
1142 "AddPair",
1143 g_variant_new ("(ii)", 2, n),
1144 G_VARIANT_TYPE ("(i)"),
1145 G_DBUS_CALL_FLAGS_NONE,
1146 -1, /* timeout_msec */
1147 NULL, /* GCancellable */
1148 &error);
1149 g_assert_no_error (error);
1150 g_variant_get (res, "(i)", &val);
1151 g_assert_cmpint (val, ==, 2 + n);
1152 g_variant_unref (res);
1153 g_object_unref (c);
1156 g_main_loop_quit (data->loop);
1157 g_thread_join (service_thread);
1158 dmp_data_free (data);
1161 /* ---------------------------------------------------------------------------------------------------- */
1163 static gboolean
1164 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1165 GIOStream *stream,
1166 GCredentials *credentials,
1167 gpointer user_data)
1169 PeerData *data = user_data;
1170 gboolean authorized;
1172 data->num_connection_attempts++;
1174 authorized = TRUE;
1175 if (!data->accept_connection)
1177 authorized = FALSE;
1178 g_main_loop_quit (loop);
1181 return authorized;
1184 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1185 static gboolean
1186 nonce_tcp_on_new_connection (GDBusServer *server,
1187 GDBusConnection *connection,
1188 gpointer user_data)
1190 PeerData *data = user_data;
1192 g_ptr_array_add (data->current_connections, g_object_ref (connection));
1194 g_main_loop_quit (loop);
1196 return TRUE;
1199 static gpointer
1200 nonce_tcp_service_thread_func (gpointer user_data)
1202 PeerData *data = user_data;
1203 GMainContext *service_context;
1204 GDBusAuthObserver *observer;
1205 GError *error;
1207 service_context = g_main_context_new ();
1208 g_main_context_push_thread_default (service_context);
1210 error = NULL;
1211 observer = g_dbus_auth_observer_new ();
1212 server = g_dbus_server_new_sync ("nonce-tcp:",
1213 G_DBUS_SERVER_FLAGS_NONE,
1214 test_guid,
1215 observer,
1216 NULL, /* cancellable */
1217 &error);
1218 g_assert_no_error (error);
1220 g_signal_connect (server,
1221 "new-connection",
1222 G_CALLBACK (nonce_tcp_on_new_connection),
1223 data);
1224 g_signal_connect (observer,
1225 "authorize-authenticated-peer",
1226 G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1227 data);
1228 g_object_unref (observer);
1230 g_dbus_server_start (server);
1232 run_service_loop (service_context);
1234 g_main_context_pop_thread_default (service_context);
1236 teardown_service_loop ();
1237 g_main_context_unref (service_context);
1239 /* test code specifically unrefs the server - see below */
1240 g_assert (server == NULL);
1242 return NULL;
1245 static void
1246 test_nonce_tcp (void)
1248 PeerData data;
1249 GError *error;
1250 GThread *service_thread;
1251 GDBusConnection *c;
1252 gchar *s;
1253 gchar *nonce_file;
1254 gboolean res;
1255 const gchar *address;
1257 memset (&data, '\0', sizeof (PeerData));
1258 data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1260 error = NULL;
1261 server = NULL;
1262 service_thread = g_thread_new ("nonce-tcp-service",
1263 nonce_tcp_service_thread_func,
1264 &data);
1265 await_service_loop ();
1266 g_assert (server != NULL);
1268 /* bring up a connection and accept it */
1269 data.accept_connection = TRUE;
1270 error = NULL;
1271 c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1272 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1273 NULL, /* GDBusAuthObserver */
1274 NULL, /* cancellable */
1275 &error);
1276 g_assert_no_error (error);
1277 g_assert (c != NULL);
1278 while (data.current_connections->len < 1)
1279 g_thread_yield ();
1280 g_assert_cmpint (data.current_connections->len, ==, 1);
1281 g_assert_cmpint (data.num_connection_attempts, ==, 1);
1282 g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1283 g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1284 g_object_unref (c);
1286 /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1289 address = g_dbus_server_get_client_address (server);
1291 s = strstr (address, "noncefile=");
1292 g_assert (s != NULL);
1293 s += sizeof "noncefile=" - 1;
1294 nonce_file = g_strdup (s);
1296 /* First try invalid data in the nonce file - this will actually
1297 * make the client send this and the server will reject it. The way
1298 * it works is that if the nonce doesn't match, the server will
1299 * simply close the connection. So, from the client point of view,
1300 * we can see a variety of errors.
1302 error = NULL;
1303 res = g_file_set_contents (nonce_file,
1304 "0123456789012345",
1306 &error);
1307 g_assert_no_error (error);
1308 g_assert (res);
1309 c = g_dbus_connection_new_for_address_sync (address,
1310 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1311 NULL, /* GDBusAuthObserver */
1312 NULL, /* cancellable */
1313 &error);
1314 _g_assert_error_domain (error, G_IO_ERROR);
1315 g_error_free (error);
1316 g_assert (c == NULL);
1318 /* Then try with a nonce-file of incorrect length - this will make
1319 * the client complain - we won't even try connecting to the server
1320 * for this
1322 error = NULL;
1323 res = g_file_set_contents (nonce_file,
1324 "0123456789012345_",
1326 &error);
1327 g_assert_no_error (error);
1328 g_assert (res);
1329 c = g_dbus_connection_new_for_address_sync (address,
1330 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1331 NULL, /* GDBusAuthObserver */
1332 NULL, /* cancellable */
1333 &error);
1334 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1335 g_error_free (error);
1336 g_assert (c == NULL);
1338 /* Finally try with no nonce-file at all */
1339 g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1340 error = NULL;
1341 c = g_dbus_connection_new_for_address_sync (address,
1342 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1343 NULL, /* GDBusAuthObserver */
1344 NULL, /* cancellable */
1345 &error);
1346 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1347 g_error_free (error);
1348 g_assert (c == NULL);
1350 g_free (nonce_file);
1352 g_dbus_server_stop (server);
1353 g_object_unref (server);
1354 server = NULL;
1356 g_main_loop_quit (service_loop);
1357 g_thread_join (service_thread);
1360 static void
1361 test_credentials (void)
1363 GCredentials *c1, *c2;
1364 GError *error;
1365 gchar *desc;
1367 c1 = g_credentials_new ();
1368 c2 = g_credentials_new ();
1370 error = NULL;
1371 if (g_credentials_set_unix_user (c2, getuid (), &error))
1372 g_assert_no_error (error);
1374 g_clear_error (&error);
1375 g_assert (g_credentials_is_same_user (c1, c2, &error));
1376 g_assert_no_error (error);
1378 desc = g_credentials_to_string (c1);
1379 g_assert (desc != NULL);
1380 g_free (desc);
1382 g_object_unref (c1);
1383 g_object_unref (c2);
1386 /* ---------------------------------------------------------------------------------------------------- */
1388 static gboolean
1389 tcp_anonymous_on_new_connection (GDBusServer *server,
1390 GDBusConnection *connection,
1391 gpointer user_data)
1393 gboolean *seen_connection = user_data;
1394 *seen_connection = TRUE;
1395 return TRUE;
1398 static gpointer
1399 tcp_anonymous_service_thread_func (gpointer user_data)
1401 gboolean *seen_connection = user_data;
1402 GMainContext *service_context;
1403 GError *error;
1405 service_context = g_main_context_new ();
1406 g_main_context_push_thread_default (service_context);
1408 error = NULL;
1409 server = g_dbus_server_new_sync ("tcp:",
1410 G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS,
1411 test_guid,
1412 NULL, /* GDBusObserver* */
1413 NULL, /* GCancellable* */
1414 &error);
1415 g_assert_no_error (error);
1417 g_signal_connect (server,
1418 "new-connection",
1419 G_CALLBACK (tcp_anonymous_on_new_connection),
1420 seen_connection);
1422 g_dbus_server_start (server);
1424 run_service_loop (service_context);
1426 g_main_context_pop_thread_default (service_context);
1428 teardown_service_loop ();
1429 g_main_context_unref (service_context);
1431 return NULL;
1434 static void
1435 test_tcp_anonymous (void)
1437 gboolean seen_connection;
1438 GThread *service_thread;
1439 GDBusConnection *connection;
1440 GError *error;
1442 seen_connection = FALSE;
1443 service_thread = g_thread_new ("tcp-anon-service",
1444 tcp_anonymous_service_thread_func,
1445 &seen_connection);
1446 await_service_loop ();
1447 g_assert (server != NULL);
1449 error = NULL;
1450 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1451 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1452 NULL, /* GDBusAuthObserver* */
1453 NULL, /* GCancellable */
1454 &error);
1455 g_assert_no_error (error);
1456 g_assert (connection != NULL);
1458 while (!seen_connection)
1459 g_thread_yield ();
1461 g_object_unref (connection);
1463 g_main_loop_quit (service_loop);
1464 g_dbus_server_stop (server);
1465 g_object_unref (server);
1466 server = NULL;
1468 g_thread_join (service_thread);
1471 /* ---------------------------------------------------------------------------------------------------- */
1473 static GDBusServer *codegen_server = NULL;
1475 static gboolean
1476 codegen_on_animal_poke (ExampleAnimal *animal,
1477 GDBusMethodInvocation *invocation,
1478 gboolean make_sad,
1479 gboolean make_happy,
1480 gpointer user_data)
1482 if ((make_sad && make_happy) || (!make_sad && !make_happy))
1484 g_main_loop_quit (service_loop);
1486 g_dbus_method_invocation_return_dbus_error (invocation,
1487 "org.gtk.GDBus.Examples.ObjectManager.Error.Failed",
1488 "Exactly one of make_sad or make_happy must be TRUE");
1489 goto out;
1492 if (make_sad)
1494 if (g_strcmp0 (example_animal_get_mood (animal), "Sad") == 0)
1496 g_dbus_method_invocation_return_dbus_error (invocation,
1497 "org.gtk.GDBus.Examples.ObjectManager.Error.SadAnimalIsSad",
1498 "Sad animal is already sad");
1499 goto out;
1502 example_animal_set_mood (animal, "Sad");
1503 example_animal_complete_poke (animal, invocation);
1504 goto out;
1507 if (make_happy)
1509 if (g_strcmp0 (example_animal_get_mood (animal), "Happy") == 0)
1511 g_dbus_method_invocation_return_dbus_error (invocation,
1512 "org.gtk.GDBus.Examples.ObjectManager.Error.HappyAnimalIsHappy",
1513 "Happy animal is already happy");
1514 goto out;
1517 example_animal_set_mood (animal, "Happy");
1518 example_animal_complete_poke (animal, invocation);
1519 goto out;
1522 g_assert_not_reached ();
1524 out:
1525 return TRUE; /* to indicate that the method was handled */
1528 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1529 static gboolean
1530 codegen_on_new_connection (GDBusServer *server,
1531 GDBusConnection *connection,
1532 gpointer user_data)
1534 ExampleAnimal *animal = user_data;
1535 GError *error = NULL;
1537 /* g_printerr ("Client connected.\n" */
1538 /* "Negotiated capabilities: unix-fd-passing=%d\n", */
1539 /* g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING); */
1541 g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (animal), connection,
1542 "/Example/Animals/000", &error);
1543 g_assert_no_error (error);
1545 return TRUE;
1548 static gpointer
1549 codegen_service_thread_func (gpointer user_data)
1551 GMainContext *service_context;
1552 ExampleAnimal *animal;
1553 GError *error = NULL;
1555 service_context = g_main_context_new ();
1556 g_main_context_push_thread_default (service_context);
1558 /* Create the animal in the right thread context */
1559 animal = example_animal_skeleton_new ();
1561 /* Handle Poke() D-Bus method invocations on the .Animal interface */
1562 g_signal_connect (animal, "handle-poke",
1563 G_CALLBACK (codegen_on_animal_poke),
1564 NULL); /* user_data */
1566 codegen_server = g_dbus_server_new_sync (tmp_address,
1567 G_DBUS_SERVER_FLAGS_NONE,
1568 test_guid,
1569 NULL, /* observer */
1570 NULL, /* cancellable */
1571 &error);
1572 g_assert_no_error (error);
1573 g_dbus_server_start (codegen_server);
1575 g_signal_connect (codegen_server, "new-connection",
1576 G_CALLBACK (codegen_on_new_connection),
1577 animal);
1579 run_service_loop (service_context);
1581 g_object_unref (animal);
1583 g_main_context_pop_thread_default (service_context);
1585 teardown_service_loop ();
1586 g_main_context_unref (service_context);
1588 g_dbus_server_stop (codegen_server);
1589 g_object_unref (codegen_server);
1590 codegen_server = NULL;
1592 return NULL;
1596 static gboolean
1597 codegen_quit_mainloop_timeout (gpointer data)
1599 g_main_loop_quit (loop);
1600 return FALSE;
1603 static void
1604 codegen_test_peer (void)
1606 GDBusConnection *connection;
1607 ExampleAnimal *animal1, *animal2;
1608 GThread *service_thread;
1609 GError *error = NULL;
1610 GVariant *value;
1611 const gchar *s;
1613 /* bring up a server - we run the server in a different thread to avoid deadlocks */
1614 service_thread = g_thread_new ("codegen_test_peer",
1615 codegen_service_thread_func,
1616 NULL);
1617 await_service_loop ();
1618 g_assert (codegen_server != NULL);
1620 /* Get an animal 1 ... */
1621 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (codegen_server),
1622 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1623 NULL, /* GDBusAuthObserver */
1624 NULL, /* cancellable */
1625 &error);
1626 g_assert_no_error (error);
1627 g_assert (connection != NULL);
1629 animal1 = example_animal_proxy_new_sync (connection, 0, NULL,
1630 "/Example/Animals/000", NULL, &error);
1631 g_assert_no_error (error);
1632 g_assert (animal1 != NULL);
1633 g_object_unref (connection);
1635 /* Get animal 2 ... */
1636 connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (codegen_server),
1637 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1638 NULL, /* GDBusAuthObserver */
1639 NULL, /* cancellable */
1640 &error);
1641 g_assert_no_error (error);
1642 g_assert (connection != NULL);
1644 animal2 = example_animal_proxy_new_sync (connection, 0, NULL,
1645 "/Example/Animals/000", NULL, &error);
1646 g_assert_no_error (error);
1647 g_assert (animal2 != NULL);
1648 g_object_unref (connection);
1650 /* Make animal sad via animal1 */
1651 example_animal_call_poke_sync (animal1, TRUE, FALSE, NULL, &error);
1652 g_assert_no_error (error);
1654 /* Poke server and make sure animal is updated */
1655 value = g_dbus_proxy_call_sync (G_DBUS_PROXY (animal1),
1656 "org.freedesktop.DBus.Peer.Ping",
1657 NULL, G_DBUS_CALL_FLAGS_NONE, -1,
1658 NULL, &error);
1659 g_assert_no_error (error);
1660 g_assert (value != NULL);
1661 g_variant_unref (value);
1663 /* Give the proxies a chance to refresh in the defaul main loop */
1664 g_timeout_add (100, codegen_quit_mainloop_timeout, NULL);
1665 g_main_loop_run (loop);
1667 /* Assert animals are sad */
1668 g_assert_cmpstr (example_animal_get_mood (animal1), ==, "Sad");
1669 g_assert_cmpstr (example_animal_get_mood (animal2), ==, "Sad");
1671 /* Make animal happy via animal2 */
1672 example_animal_call_poke_sync (animal2, FALSE, TRUE, NULL, &error);
1673 g_assert_no_error (error);
1675 /* Some random unrelated call, just to get some test coverage */
1676 value = g_dbus_proxy_call_sync (G_DBUS_PROXY (animal2),
1677 "org.freedesktop.DBus.Peer.GetMachineId",
1678 NULL, G_DBUS_CALL_FLAGS_NONE, -1,
1679 NULL, &error);
1680 g_assert_no_error (error);
1681 g_variant_get (value, "(&s)", &s);
1682 g_assert (g_dbus_is_guid (s));
1683 g_variant_unref (value);
1685 /* Poke server and make sure animal is updated */
1686 value = g_dbus_proxy_call_sync (G_DBUS_PROXY (animal2),
1687 "org.freedesktop.DBus.Peer.Ping",
1688 NULL, G_DBUS_CALL_FLAGS_NONE, -1,
1689 NULL, &error);
1690 g_assert_no_error (error);
1691 g_assert (value != NULL);
1692 g_variant_unref (value);
1694 /* Give the proxies a chance to refresh in the defaul main loop */
1695 g_timeout_add (1000, codegen_quit_mainloop_timeout, NULL);
1696 g_main_loop_run (loop);
1698 /* Assert animals are happy */
1699 g_assert_cmpstr (example_animal_get_mood (animal1), ==, "Happy");
1700 g_assert_cmpstr (example_animal_get_mood (animal2), ==, "Happy");
1702 /* This final call making the animal happy and sad will cause
1703 * the server to quit, when the server quits we dont get property
1704 * change notifications anyway because those are done from an idle handler
1706 example_animal_call_poke_sync (animal2, TRUE, TRUE, NULL, &error);
1708 g_object_unref (animal1);
1709 g_object_unref (animal2);
1710 g_thread_join (service_thread);
1713 /* ---------------------------------------------------------------------------------------------------- */
1717 main (int argc,
1718 char *argv[])
1720 gint ret;
1721 GDBusNodeInfo *introspection_data = NULL;
1722 gchar *tmpdir = NULL;
1724 g_test_init (&argc, &argv, NULL);
1726 introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1727 g_assert (introspection_data != NULL);
1728 test_interface_introspection_data = introspection_data->interfaces[0];
1730 test_guid = g_dbus_generate_guid ();
1732 if (is_unix)
1734 if (g_unix_socket_address_abstract_names_supported ())
1735 tmp_address = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
1736 else
1738 tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
1739 tmp_address = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
1742 else
1743 tmp_address = g_strdup ("nonce-tcp:");
1745 /* all the tests rely on a shared main loop */
1746 loop = g_main_loop_new (NULL, FALSE);
1748 g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1749 g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1750 g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1752 g_test_add_func ("/gdbus/tcp-anonymous", test_tcp_anonymous);
1753 g_test_add_func ("/gdbus/credentials", test_credentials);
1754 g_test_add_func ("/gdbus/codegen-peer-to-peer", codegen_test_peer);
1756 ret = g_test_run();
1758 g_main_loop_unref (loop);
1759 g_free (test_guid);
1760 g_dbus_node_info_unref (introspection_data);
1761 if (is_unix)
1762 g_free (tmp_address);
1763 if (tmpdir)
1765 g_rmdir (tmpdir);
1766 g_free (tmpdir);
1769 return ret;