gio/tests: Make gdbus-peer work in installed mode
[platform/upstream/glib.git] / gio / tests / gdbus-peer.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include "config.h"
24
25 #include <gio/gio.h>
26 #include <unistd.h>
27 #include <string.h>
28
29 /* for open(2) */
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <string.h>
34
35 /* for g_unlink() */
36 #include <glib/gstdio.h>
37
38 #include <gio/gnetworking.h>
39 #include <gio/gunixsocketaddress.h>
40 #include <gio/gunixfdlist.h>
41
42 /* used in test_overflow */
43 #ifdef G_OS_UNIX
44 #include <gio/gunixconnection.h>
45 #include <errno.h>
46 #endif
47
48 #if (defined(__linux__) || \
49   defined(__FreeBSD__) || \
50   defined(__FreeBSD_kernel__) || \
51   defined(__OpenBSD__))
52 #define SHOULD_HAVE_CREDENTIALS_PASSING
53 #endif
54
55 #include "gdbus-tests.h"
56
57 #include "gdbus-example-objectmanager-generated.h"
58
59 #ifdef G_OS_UNIX
60 static gboolean is_unix = TRUE;
61 #else
62 static gboolean is_unix = FALSE;
63 #endif
64
65 static const gchar *datapath;
66
67 static gchar *tmp_address = NULL;
68 static gchar *test_guid = NULL;
69 static GMainLoop *service_loop = NULL;
70 static GDBusServer *server = NULL;
71 static GMainLoop *loop = NULL;
72
73 /* ---------------------------------------------------------------------------------------------------- */
74 /* Test that peer-to-peer connections work */
75 /* ---------------------------------------------------------------------------------------------------- */
76
77
78 typedef struct
79 {
80   gboolean accept_connection;
81   gint num_connection_attempts;
82   GPtrArray *current_connections;
83   guint num_method_calls;
84   gboolean signal_received;
85 } PeerData;
86
87 static const gchar *test_interface_introspection_xml =
88   "<node>"
89   "  <interface name='org.gtk.GDBus.PeerTestInterface'>"
90   "    <method name='HelloPeer'>"
91   "      <arg type='s' name='greeting' direction='in'/>"
92   "      <arg type='s' name='response' direction='out'/>"
93   "    </method>"
94   "    <method name='EmitSignal'/>"
95   "    <method name='EmitSignalWithNameSet'/>"
96   "    <method name='OpenFile'>"
97   "      <arg type='s' name='path' direction='in'/>"
98   "    </method>"
99   "    <signal name='PeerSignal'>"
100   "      <arg type='s' name='a_string'/>"
101   "    </signal>"
102   "    <property type='s' name='PeerProperty' access='read'/>"
103   "  </interface>"
104   "</node>";
105 static GDBusInterfaceInfo *test_interface_introspection_data = NULL;
106
107 static void
108 test_interface_method_call (GDBusConnection       *connection,
109                             const gchar           *sender,
110                             const gchar           *object_path,
111                             const gchar           *interface_name,
112                             const gchar           *method_name,
113                             GVariant              *parameters,
114                             GDBusMethodInvocation *invocation,
115                             gpointer               user_data)
116 {
117   PeerData *data = user_data;
118   const GDBusMethodInfo *info;
119
120   data->num_method_calls++;
121
122   g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
123   g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
124
125   info = g_dbus_method_invocation_get_method_info (invocation);
126   g_assert_cmpstr (info->name, ==, method_name);
127
128   if (g_strcmp0 (method_name, "HelloPeer") == 0)
129     {
130       const gchar *greeting;
131       gchar *response;
132
133       g_variant_get (parameters, "(&s)", &greeting);
134
135       response = g_strdup_printf ("You greeted me with '%s'.",
136                                   greeting);
137       g_dbus_method_invocation_return_value (invocation,
138                                              g_variant_new ("(s)", response));
139       g_free (response);
140     }
141   else if (g_strcmp0 (method_name, "EmitSignal") == 0)
142     {
143       GError *error;
144
145       error = NULL;
146       g_dbus_connection_emit_signal (connection,
147                                      NULL,
148                                      "/org/gtk/GDBus/PeerTestObject",
149                                      "org.gtk.GDBus.PeerTestInterface",
150                                      "PeerSignal",
151                                      NULL,
152                                      &error);
153       g_assert_no_error (error);
154       g_dbus_method_invocation_return_value (invocation, NULL);
155     }
156   else if (g_strcmp0 (method_name, "EmitSignalWithNameSet") == 0)
157     {
158       GError *error;
159       gboolean ret;
160       GDBusMessage *message;
161
162       message = g_dbus_message_new_signal ("/org/gtk/GDBus/PeerTestObject",
163                                            "org.gtk.GDBus.PeerTestInterface",
164                                            "PeerSignalWithNameSet");
165       g_dbus_message_set_sender (message, ":1.42");
166
167       error = NULL;
168       ret = g_dbus_connection_send_message (connection, message, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error);
169       g_assert_no_error (error);
170       g_assert (ret);
171       g_object_unref (message);
172
173       g_dbus_method_invocation_return_value (invocation, NULL);
174     }
175   else if (g_strcmp0 (method_name, "OpenFile") == 0)
176     {
177 #ifdef G_OS_UNIX
178       const gchar *path;
179       GDBusMessage *reply;
180       GError *error;
181       gint fd;
182       GUnixFDList *fd_list;
183
184       g_variant_get (parameters, "(&s)", &path);
185
186       fd_list = g_unix_fd_list_new ();
187
188       error = NULL;
189
190       fd = g_open (path, O_RDONLY, 0);
191       g_assert (fd != -1);
192       g_unix_fd_list_append (fd_list, fd, &error);
193       g_assert_no_error (error);
194       close (fd);
195
196       reply = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
197       g_dbus_message_set_unix_fd_list (reply, fd_list);
198       g_object_unref (fd_list);
199       g_object_unref (invocation);
200
201       error = NULL;
202       g_dbus_connection_send_message (connection,
203                                       reply,
204                                       G_DBUS_SEND_MESSAGE_FLAGS_NONE,
205                                       NULL, /* out_serial */
206                                       &error);
207       g_assert_no_error (error);
208       g_object_unref (reply);
209 #else
210       g_dbus_method_invocation_return_dbus_error (invocation,
211                                                   "org.gtk.GDBus.NotOnUnix",
212                                                   "Your OS does not support file descriptor passing");
213 #endif
214     }
215   else
216     {
217       g_assert_not_reached ();
218     }
219 }
220
221 static GVariant *
222 test_interface_get_property (GDBusConnection  *connection,
223                              const gchar      *sender,
224                              const gchar      *object_path,
225                              const gchar      *interface_name,
226                              const gchar      *property_name,
227                              GError          **error,
228                              gpointer          user_data)
229 {
230   g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
231   g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
232   g_assert_cmpstr (property_name, ==, "PeerProperty");
233
234   return g_variant_new_string ("ThePropertyValue");
235 }
236
237
238 static const GDBusInterfaceVTable test_interface_vtable =
239 {
240   test_interface_method_call,
241   test_interface_get_property,
242   NULL  /* set_property */
243 };
244
245 static void
246 on_proxy_signal_received (GDBusProxy *proxy,
247                           gchar      *sender_name,
248                           gchar      *signal_name,
249                           GVariant   *parameters,
250                           gpointer    user_data)
251 {
252   PeerData *data = user_data;
253
254   data->signal_received = TRUE;
255
256   g_assert (sender_name == NULL);
257   g_assert_cmpstr (signal_name, ==, "PeerSignal");
258   g_main_loop_quit (loop);
259 }
260
261 static void
262 on_proxy_signal_received_with_name_set (GDBusProxy *proxy,
263                                         gchar      *sender_name,
264                                         gchar      *signal_name,
265                                         GVariant   *parameters,
266                                         gpointer    user_data)
267 {
268   PeerData *data = user_data;
269
270   data->signal_received = TRUE;
271
272   g_assert_cmpstr (sender_name, ==, ":1.42");
273   g_assert_cmpstr (signal_name, ==, "PeerSignalWithNameSet");
274   g_main_loop_quit (loop);
275 }
276
277 /* ---------------------------------------------------------------------------------------------------- */
278
279 static gboolean
280 on_authorize_authenticated_peer (GDBusAuthObserver *observer,
281                                  GIOStream         *stream,
282                                  GCredentials      *credentials,
283                                  gpointer           user_data)
284 {
285   PeerData *data = user_data;
286   gboolean authorized;
287
288   data->num_connection_attempts++;
289
290   authorized = TRUE;
291   if (!data->accept_connection)
292     {
293       authorized = FALSE;
294       g_main_loop_quit (loop);
295     }
296
297   return authorized;
298 }
299
300 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
301 static gboolean
302 on_new_connection (GDBusServer *server,
303                    GDBusConnection *connection,
304                    gpointer user_data)
305 {
306   PeerData *data = user_data;
307   GError *error;
308   guint reg_id;
309
310   //g_print ("Client connected.\n"
311   //         "Negotiated capabilities: unix-fd-passing=%d\n",
312   //         g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
313
314   g_ptr_array_add (data->current_connections, g_object_ref (connection));
315
316 #ifdef SHOULD_HAVE_CREDENTIALS_PASSING
317     {
318       GCredentials *credentials;
319
320       credentials = g_dbus_connection_get_peer_credentials (connection);
321
322       g_assert (credentials != NULL);
323       g_assert_cmpuint (g_credentials_get_unix_user (credentials, NULL), ==,
324                         getuid ());
325       g_assert_cmpuint (g_credentials_get_unix_pid (credentials, NULL), ==,
326                         getpid ());
327     }
328 #endif
329
330   /* export object on the newly established connection */
331   error = NULL;
332   reg_id = g_dbus_connection_register_object (connection,
333                                               "/org/gtk/GDBus/PeerTestObject",
334                                               test_interface_introspection_data,
335                                               &test_interface_vtable,
336                                               data,
337                                               NULL, /* GDestroyNotify for data */
338                                               &error);
339   g_assert_no_error (error);
340   g_assert (reg_id > 0);
341
342   g_main_loop_quit (loop);
343
344   return TRUE;
345 }
346
347 static gpointer
348 service_thread_func (gpointer user_data)
349 {
350   PeerData *data = user_data;
351   GMainContext *service_context;
352   GDBusAuthObserver *observer, *o;
353   GError *error;
354   GDBusServerFlags f;
355   gchar *a, *g;
356   gboolean b;
357
358   service_context = g_main_context_new ();
359   g_main_context_push_thread_default (service_context);
360
361   error = NULL;
362   observer = g_dbus_auth_observer_new ();
363   server = g_dbus_server_new_sync (tmp_address,
364                                    G_DBUS_SERVER_FLAGS_NONE,
365                                    test_guid,
366                                    observer,
367                                    NULL, /* cancellable */
368                                    &error);
369   g_assert_no_error (error);
370
371   g_signal_connect (server,
372                     "new-connection",
373                     G_CALLBACK (on_new_connection),
374                     data);
375   g_signal_connect (observer,
376                     "authorize-authenticated-peer",
377                     G_CALLBACK (on_authorize_authenticated_peer),
378                     data);
379
380   g_assert_cmpint (g_dbus_server_get_flags (server), ==, G_DBUS_SERVER_FLAGS_NONE);
381   g_assert_cmpstr (g_dbus_server_get_guid (server), ==, test_guid);
382   g_object_get (server,
383                 "flags", &f,
384                 "address", &a,
385                 "guid", &g,
386                 "active", &b,
387                 "authentication-observer", &o,
388                 NULL);
389   g_assert_cmpint (f, ==, G_DBUS_SERVER_FLAGS_NONE);
390   g_assert_cmpstr (a, ==, tmp_address);
391   g_assert_cmpstr (g, ==, test_guid);
392   g_assert (!b);
393   g_assert (o == observer);
394   g_free (a);
395   g_free (g);
396   g_object_unref (o);
397
398   g_object_unref (observer);
399
400   g_dbus_server_start (server);
401
402   service_loop = g_main_loop_new (service_context, FALSE);
403   g_main_loop_run (service_loop);
404
405   g_main_context_pop_thread_default (service_context);
406
407   g_main_loop_unref (service_loop);
408   g_main_context_unref (service_context);
409
410   /* test code specifically unrefs the server - see below */
411   g_assert (server == NULL);
412
413   return NULL;
414 }
415
416 #if 0
417 static gboolean
418 on_incoming_connection (GSocketService     *service,
419                         GSocketConnection  *socket_connection,
420                         GObject            *source_object,
421                         gpointer           user_data)
422 {
423   PeerData *data = user_data;
424
425   if (data->accept_connection)
426     {
427       GError *error;
428       guint reg_id;
429       GDBusConnection *connection;
430
431       error = NULL;
432       connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
433                                                test_guid,
434                                                G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
435                                                NULL, /* cancellable */
436                                                &error);
437       g_assert_no_error (error);
438
439       g_ptr_array_add (data->current_connections, connection);
440
441       /* export object on the newly established connection */
442       error = NULL;
443       reg_id = g_dbus_connection_register_object (connection,
444                                                   "/org/gtk/GDBus/PeerTestObject",
445                                                   &test_interface_introspection_data,
446                                                   &test_interface_vtable,
447                                                   data,
448                                                   NULL, /* GDestroyNotify for data */
449                                                   &error);
450       g_assert_no_error (error);
451       g_assert (reg_id > 0);
452
453     }
454   else
455     {
456       /* don't do anything */
457     }
458
459   data->num_connection_attempts++;
460
461   g_main_loop_quit (loop);
462
463   /* stops other signal handlers from being invoked */
464   return TRUE;
465 }
466
467 static gpointer
468 service_thread_func (gpointer data)
469 {
470   GMainContext *service_context;
471   gchar *socket_path;
472   GSocketAddress *address;
473   GError *error;
474
475   service_context = g_main_context_new ();
476   g_main_context_push_thread_default (service_context);
477
478   socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
479   address = g_unix_socket_address_new (socket_path);
480
481   service = g_socket_service_new ();
482   error = NULL;
483   g_socket_listener_add_address (G_SOCKET_LISTENER (service),
484                                  address,
485                                  G_SOCKET_TYPE_STREAM,
486                                  G_SOCKET_PROTOCOL_DEFAULT,
487                                  NULL, /* source_object */
488                                  NULL, /* effective_address */
489                                  &error);
490   g_assert_no_error (error);
491   g_signal_connect (service,
492                     "incoming",
493                     G_CALLBACK (on_incoming_connection),
494                     data);
495   g_socket_service_start (service);
496
497   service_loop = g_main_loop_new (service_context, FALSE);
498   g_main_loop_run (service_loop);
499
500   g_main_context_pop_thread_default (service_context);
501
502   g_main_loop_unref (service_loop);
503   g_main_context_unref (service_context);
504
505   g_object_unref (address);
506   g_free (socket_path);
507   return NULL;
508 }
509 #endif
510
511 /* ---------------------------------------------------------------------------------------------------- */
512
513 #if 0
514 static gboolean
515 check_connection (gpointer user_data)
516 {
517   PeerData *data = user_data;
518   guint n;
519
520   for (n = 0; n < data->current_connections->len; n++)
521     {
522       GDBusConnection *c;
523       GIOStream *stream;
524
525       c = G_DBUS_CONNECTION (data->current_connections->pdata[n]);
526       stream = g_dbus_connection_get_stream (c);
527
528       g_debug ("In check_connection for %d: connection %p, stream %p", n, c, stream);
529       g_debug ("closed = %d", g_io_stream_is_closed (stream));
530
531       GSocket *socket;
532       socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
533       g_debug ("socket_closed = %d", g_socket_is_closed (socket));
534       g_debug ("socket_condition_check = %d", g_socket_condition_check (socket, G_IO_IN|G_IO_OUT|G_IO_ERR|G_IO_HUP));
535
536       gchar buf[128];
537       GError *error;
538       gssize num_read;
539       error = NULL;
540       num_read = g_input_stream_read (g_io_stream_get_input_stream (stream),
541                                       buf,
542                                       128,
543                                       NULL,
544                                       &error);
545       if (num_read < 0)
546         {
547           g_debug ("error: %s", error->message);
548           g_error_free (error);
549         }
550       else
551         {
552           g_debug ("no error, read %d bytes", (gint) num_read);
553         }
554     }
555
556   return FALSE;
557 }
558
559 static gboolean
560 on_do_disconnect_in_idle (gpointer data)
561 {
562   GDBusConnection *c = G_DBUS_CONNECTION (data);
563   g_debug ("GDC %p has ref_count %d", c, G_OBJECT (c)->ref_count);
564   g_dbus_connection_disconnect (c);
565   g_object_unref (c);
566   return FALSE;
567 }
568 #endif
569
570 #ifdef G_OS_UNIX
571 static gchar *
572 read_all_from_fd (gint fd, gsize *out_len, GError **error)
573 {
574   GString *str;
575   gchar buf[64];
576   gssize num_read;
577
578   str = g_string_new (NULL);
579
580   do
581     {
582       num_read = read (fd, buf, sizeof (buf));
583       if (num_read == -1)
584         {
585           if (errno == EAGAIN || errno == EWOULDBLOCK)
586             continue;
587           g_set_error (error,
588                        G_IO_ERROR,
589                        g_io_error_from_errno (errno),
590                        "Failed reading %d bytes into offset %d: %s",
591                        (gint) sizeof (buf),
592                        (gint) str->len,
593                        strerror (errno));
594           goto error;
595         }
596       else if (num_read > 0)
597         {
598           g_string_append_len (str, buf, num_read);
599         }
600       else if (num_read == 0)
601         {
602           break;
603         }
604     }
605   while (TRUE);
606
607   if (out_len != NULL)
608     *out_len = str->len;
609   return g_string_free (str, FALSE);
610
611  error:
612   if (out_len != NULL)
613     out_len = 0;
614   g_string_free (str, TRUE);
615   return NULL;
616 }
617 #endif
618
619 static void
620 test_peer (void)
621 {
622   GDBusConnection *c;
623   GDBusConnection *c2;
624   GDBusProxy *proxy;
625   GError *error;
626   PeerData data;
627   GVariant *value;
628   GVariant *result;
629   const gchar *s;
630   GThread *service_thread;
631   gulong signal_handler_id;
632
633   memset (&data, '\0', sizeof (PeerData));
634   data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
635
636   /* first try to connect when there is no server */
637   error = NULL;
638   c = g_dbus_connection_new_for_address_sync (is_unix ? "unix:path=/tmp/gdbus-test-does-not-exist-pid" :
639                                               /* NOTE: Even if something is listening on port 12345 the connection
640                                                * will fail because the nonce file doesn't exist */
641                                               "nonce-tcp:host=localhost,port=12345,noncefile=this-does-not-exist-gdbus",
642                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
643                                               NULL, /* GDBusAuthObserver */
644                                               NULL, /* cancellable */
645                                               &error);
646   _g_assert_error_domain (error, G_IO_ERROR);
647   g_assert (!g_dbus_error_is_remote_error (error));
648   g_clear_error (&error);
649   g_assert (c == NULL);
650
651   /* bring up a server - we run the server in a different thread to avoid deadlocks */
652   service_loop = NULL;
653   service_thread = g_thread_new ("test_peer",
654                                  service_thread_func,
655                                  &data);
656   while (service_loop == NULL)
657     g_thread_yield ();
658   g_assert (server != NULL);
659
660   /* bring up a connection and accept it */
661   data.accept_connection = TRUE;
662   error = NULL;
663   c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
664                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
665                                               NULL, /* GDBusAuthObserver */
666                                               NULL, /* cancellable */
667                                               &error);
668   g_assert_no_error (error);
669   g_assert (c != NULL);
670   while (data.current_connections->len < 1)
671     g_main_loop_run (loop);
672   g_assert_cmpint (data.current_connections->len, ==, 1);
673   g_assert_cmpint (data.num_connection_attempts, ==, 1);
674   g_assert (g_dbus_connection_get_unique_name (c) == NULL);
675   g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
676
677   /* check that we create a proxy, read properties, receive signals and invoke
678    * the HelloPeer() method. Since the server runs in another thread it's fine
679    * to use synchronous blocking API here.
680    */
681   error = NULL;
682   proxy = g_dbus_proxy_new_sync (c,
683                                  G_DBUS_PROXY_FLAGS_NONE,
684                                  NULL,
685                                  NULL, /* bus_name */
686                                  "/org/gtk/GDBus/PeerTestObject",
687                                  "org.gtk.GDBus.PeerTestInterface",
688                                  NULL, /* GCancellable */
689                                  &error);
690   g_assert_no_error (error);
691   g_assert (proxy != NULL);
692   error = NULL;
693   value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
694   g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
695
696   /* try invoking a method */
697   error = NULL;
698   result = g_dbus_proxy_call_sync (proxy,
699                                    "HelloPeer",
700                                    g_variant_new ("(s)", "Hey Peer!"),
701                                    G_DBUS_CALL_FLAGS_NONE,
702                                    -1,
703                                    NULL,  /* GCancellable */
704                                    &error);
705   g_assert_no_error (error);
706   g_variant_get (result, "(&s)", &s);
707   g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
708   g_variant_unref (result);
709   g_assert_cmpint (data.num_method_calls, ==, 1);
710
711   /* make the other peer emit a signal - catch it */
712   signal_handler_id = g_signal_connect (proxy,
713                                         "g-signal",
714                                         G_CALLBACK (on_proxy_signal_received),
715                                         &data);
716   g_assert (!data.signal_received);
717   g_dbus_proxy_call (proxy,
718                      "EmitSignal",
719                      NULL,  /* no arguments */
720                      G_DBUS_CALL_FLAGS_NONE,
721                      -1,
722                      NULL,  /* GCancellable */
723                      NULL,  /* GAsyncReadyCallback - we don't care about the result */
724                      NULL); /* user_data */
725   g_main_loop_run (loop);
726   g_assert (data.signal_received);
727   g_assert_cmpint (data.num_method_calls, ==, 2);
728   g_signal_handler_disconnect (proxy, signal_handler_id);
729
730   /* Also ensure that messages with the sender header-field set gets
731    * delivered to the proxy - note that this doesn't really make sense
732    * e.g. names are meaning-less in a peer-to-peer case... but we
733    * support it because it makes sense in certain bridging
734    * applications - see e.g. #623815.
735    */
736   signal_handler_id = g_signal_connect (proxy,
737                                         "g-signal",
738                                         G_CALLBACK (on_proxy_signal_received_with_name_set),
739                                         &data);
740   data.signal_received = FALSE;
741   g_dbus_proxy_call (proxy,
742                      "EmitSignalWithNameSet",
743                      NULL,  /* no arguments */
744                      G_DBUS_CALL_FLAGS_NONE,
745                      -1,
746                      NULL,  /* GCancellable */
747                      NULL,  /* GAsyncReadyCallback - we don't care about the result */
748                      NULL); /* user_data */
749   g_main_loop_run (loop);
750   g_assert (data.signal_received);
751   g_assert_cmpint (data.num_method_calls, ==, 3);
752   g_signal_handler_disconnect (proxy, signal_handler_id);
753
754   /* check for UNIX fd passing */
755 #ifdef G_OS_UNIX
756   {
757     GDBusMessage *method_call_message;
758     GDBusMessage *method_reply_message;
759     GUnixFDList *fd_list;
760     gint fd;
761     gchar *buf;
762     gsize len;
763     gchar *buf2;
764     gsize len2;
765     char *testfile = g_build_filename (datapath, "file.c", NULL);
766
767     method_call_message = g_dbus_message_new_method_call (NULL, /* name */
768                                                           "/org/gtk/GDBus/PeerTestObject",
769                                                           "org.gtk.GDBus.PeerTestInterface",
770                                                           "OpenFile");
771     g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", testfile));
772     error = NULL;
773     method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
774                                                                            method_call_message,
775                                                                            G_DBUS_SEND_MESSAGE_FLAGS_NONE,
776                                                                            -1,
777                                                                            NULL, /* out_serial */
778                                                                            NULL, /* cancellable */
779                                                                            &error);
780     g_assert_no_error (error);
781     g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
782     fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
783     g_assert (fd_list != NULL);
784     g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
785     error = NULL;
786     fd = g_unix_fd_list_get (fd_list, 0, &error);
787     g_assert_no_error (error);
788     g_object_unref (method_call_message);
789     g_object_unref (method_reply_message);
790
791     error = NULL;
792     len = 0;
793     buf = read_all_from_fd (fd, &len, &error);
794     g_assert_no_error (error);
795     g_assert (buf != NULL);
796     close (fd);
797
798     error = NULL;
799     g_file_get_contents (testfile,
800                          &buf2,
801                          &len2,
802                          &error);
803     g_assert_no_error (error);
804     g_assert_cmpint (len, ==, len2);
805     g_assert (memcmp (buf, buf2, len) == 0);
806     g_free (buf2);
807     g_free (buf);
808     g_free (testfile);
809   }
810 #else
811   error = NULL;
812   result = g_dbus_proxy_call_sync (proxy,
813                                    "OpenFile",
814                                    g_variant_new ("(s)", "boo"),
815                                    G_DBUS_CALL_FLAGS_NONE,
816                                    -1,
817                                    NULL,  /* GCancellable */
818                                    &error);
819   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
820   g_assert (result == NULL);
821   g_error_free (error);
822 #endif /* G_OS_UNIX */
823
824   /* Check that g_socket_get_credentials() work - this really should
825    * be in a GSocket-specific test suite but no such test suite exists
826    * right now.
827    */
828   {
829     GSocket *socket;
830     GCredentials *credentials;
831     socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (g_dbus_connection_get_stream (c)));
832     g_assert (G_IS_SOCKET (socket));
833     error = NULL;
834     credentials = g_socket_get_credentials (socket, &error);
835 #ifdef __linux__
836     {
837       struct ucred *native_creds;
838       g_assert_no_error (error);
839       g_assert (G_IS_CREDENTIALS (credentials));
840       native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED);
841       g_assert (native_creds != NULL);
842       g_assert (native_creds->uid == getuid ());
843       g_assert (native_creds->gid == getgid ());
844       g_assert (native_creds->pid == getpid ());
845     }
846     g_object_unref (credentials);
847 #elif defined (__OpenBSD__)
848     {
849       struct sockpeercred *native_creds;
850       g_assert_no_error (error);
851       g_assert (G_IS_CREDENTIALS (credentials));
852       native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED);
853       g_assert (native_creds != NULL);
854       g_assert (native_creds->uid == getuid ());
855       g_assert (native_creds->gid == getgid ());
856       g_assert (native_creds->pid == getpid ());
857     }
858     g_object_unref (credentials);
859 #else
860     g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
861     g_assert (credentials == NULL);
862 #endif
863   }
864
865
866   /* bring up a connection - don't accept it - this should fail
867    */
868   data.accept_connection = FALSE;
869   error = NULL;
870   c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
871                                                G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
872                                                NULL, /* GDBusAuthObserver */
873                                                NULL, /* cancellable */
874                                                &error);
875   _g_assert_error_domain (error, G_IO_ERROR);
876   g_error_free (error);
877   g_assert (c2 == NULL);
878
879 #if 0
880   /* TODO: THIS TEST DOESN'T WORK YET */
881
882   /* bring up a connection - accept it.. then disconnect from the client side - check
883    * that the server side gets the disconnect signal.
884    */
885   error = NULL;
886   data.accept_connection = TRUE;
887   c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
888                                                G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
889                                                NULL, /* GDBusAuthObserver */
890                                                NULL, /* cancellable */
891                                                &error);
892   g_assert_no_error (error);
893   g_assert (c2 != NULL);
894   g_assert (!g_dbus_connection_get_is_disconnected (c2));
895   while (data.num_connection_attempts < 3)
896     g_main_loop_run (loop);
897   g_assert_cmpint (data.current_connections->len, ==, 2);
898   g_assert_cmpint (data.num_connection_attempts, ==, 3);
899   g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
900   g_idle_add (on_do_disconnect_in_idle, c2);
901   g_debug ("==================================================");
902   g_debug ("==================================================");
903   g_debug ("==================================================");
904   g_debug ("waiting for disconnect on connection %p, stream %p",
905            data.current_connections->pdata[1],
906            g_dbus_connection_get_stream (data.current_connections->pdata[1]));
907
908   g_timeout_add (2000, check_connection, &data);
909   //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
910   g_main_loop_run (loop);
911   g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
912   g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
913 #endif
914
915   /* unref the server and stop listening for new connections
916    *
917    * This won't bring down the established connections - check that c is still connected
918    * by invoking a method
919    */
920   //g_socket_service_stop (service);
921   //g_object_unref (service);
922   g_dbus_server_stop (server);
923   g_object_unref (server);
924   server = NULL;
925
926   error = NULL;
927   result = g_dbus_proxy_call_sync (proxy,
928                                    "HelloPeer",
929                                    g_variant_new ("(s)", "Hey Again Peer!"),
930                                    G_DBUS_CALL_FLAGS_NONE,
931                                    -1,
932                                    NULL,  /* GCancellable */
933                                    &error);
934   g_assert_no_error (error);
935   g_variant_get (result, "(&s)", &s);
936   g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
937   g_variant_unref (result);
938   g_assert_cmpint (data.num_method_calls, ==, 5);
939
940 #if 0
941   /* TODO: THIS TEST DOESN'T WORK YET */
942
943   /* now disconnect from the server side - check that the client side gets the signal */
944   g_assert_cmpint (data.current_connections->len, ==, 1);
945   g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
946   g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
947   if (!g_dbus_connection_get_is_disconnected (c))
948     _g_assert_signal_received (c, "closed");
949   g_assert (g_dbus_connection_get_is_disconnected (c));
950 #endif
951
952   g_object_unref (c);
953   g_ptr_array_unref (data.current_connections);
954   g_object_unref (proxy);
955
956   g_main_loop_quit (service_loop);
957   g_thread_join (service_thread);
958 }
959
960 /* ---------------------------------------------------------------------------------------------------- */
961
962 typedef struct
963 {
964   GDBusServer *server;
965   GMainContext *context;
966   GMainLoop *loop;
967
968   GList *connections;
969 } DmpData;
970
971 static void
972 dmp_data_free (DmpData *data)
973 {
974   g_main_loop_unref (data->loop);
975   g_main_context_unref (data->context);
976   g_object_unref (data->server);
977   g_list_free_full (data->connections, g_object_unref);
978   g_free (data);
979 }
980
981 static void
982 dmp_on_method_call (GDBusConnection       *connection,
983                     const gchar           *sender,
984                     const gchar           *object_path,
985                     const gchar           *interface_name,
986                     const gchar           *method_name,
987                     GVariant              *parameters,
988                     GDBusMethodInvocation *invocation,
989                     gpointer               user_data)
990 {
991   //DmpData *data = user_data;
992   gint32 first;
993   gint32 second;
994   g_variant_get (parameters,
995                  "(ii)",
996                  &first,
997                  &second);
998   g_dbus_method_invocation_return_value (invocation,
999                                          g_variant_new ("(i)", first + second));
1000 }
1001
1002 static const GDBusInterfaceVTable dmp_interface_vtable =
1003 {
1004   dmp_on_method_call,
1005   NULL,  /* get_property */
1006   NULL   /* set_property */
1007 };
1008
1009
1010 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1011 static gboolean
1012 dmp_on_new_connection (GDBusServer     *server,
1013                        GDBusConnection *connection,
1014                        gpointer         user_data)
1015 {
1016   DmpData *data = user_data;
1017   GDBusNodeInfo *node;
1018   GError *error;
1019
1020   /* accept the connection */
1021   data->connections = g_list_prepend (data->connections, g_object_ref (connection));
1022
1023   error = NULL;
1024   node = g_dbus_node_info_new_for_xml ("<node>"
1025                                        "  <interface name='org.gtk.GDBus.DmpInterface'>"
1026                                        "    <method name='AddPair'>"
1027                                        "      <arg type='i' name='first' direction='in'/>"
1028                                        "      <arg type='i' name='second' direction='in'/>"
1029                                        "      <arg type='i' name='sum' direction='out'/>"
1030                                        "    </method>"
1031                                        "  </interface>"
1032                                        "</node>",
1033                                        &error);
1034   g_assert_no_error (error);
1035
1036   /* sleep 100ms before exporting an object - this is to test that
1037    * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
1038    * (GDBusServer uses this feature).
1039    */
1040   usleep (100 * 1000);
1041
1042   /* export an object */
1043   error = NULL;
1044   g_dbus_connection_register_object (connection,
1045                                      "/dmp/test",
1046                                      node->interfaces[0],
1047                                      &dmp_interface_vtable,
1048                                      data,
1049                                      NULL,
1050                                      &error);
1051   g_dbus_node_info_unref (node);
1052
1053   return TRUE;
1054 }
1055
1056 static gpointer
1057 dmp_thread_func (gpointer user_data)
1058 {
1059   DmpData *data = user_data;
1060   GError *error;
1061   gchar *guid;
1062
1063   data->context = g_main_context_new ();
1064   g_main_context_push_thread_default (data->context);
1065
1066   error = NULL;
1067   guid = g_dbus_generate_guid ();
1068   data->server = g_dbus_server_new_sync (tmp_address,
1069                                          G_DBUS_SERVER_FLAGS_NONE,
1070                                          guid,
1071                                          NULL, /* GDBusAuthObserver */
1072                                          NULL, /* GCancellable */
1073                                          &error);
1074   g_assert_no_error (error);
1075   g_signal_connect (data->server,
1076                     "new-connection",
1077                     G_CALLBACK (dmp_on_new_connection),
1078                     data);
1079
1080   g_dbus_server_start (data->server);
1081
1082   data->loop = g_main_loop_new (data->context, FALSE);
1083   g_main_loop_run (data->loop);
1084
1085   g_main_context_pop_thread_default (data->context);
1086
1087   g_free (guid);
1088   return NULL;
1089 }
1090
1091 static void
1092 delayed_message_processing (void)
1093 {
1094   GError *error;
1095   DmpData *data;
1096   GThread *service_thread;
1097   guint n;
1098
1099   data = g_new0 (DmpData, 1);
1100
1101   service_thread = g_thread_new ("dmp",
1102                                  dmp_thread_func,
1103                                  data);
1104   while (data->server == NULL || !g_dbus_server_is_active (data->server))
1105     g_thread_yield ();
1106
1107   for (n = 0; n < 5; n++)
1108     {
1109       GDBusConnection *c;
1110       GVariant *res;
1111       gint32 val;
1112
1113       error = NULL;
1114       c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
1115                                                   G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1116                                                   NULL, /* GDBusAuthObserver */
1117                                                   NULL, /* GCancellable */
1118                                                   &error);
1119       g_assert_no_error (error);
1120
1121       error = NULL;
1122       res = g_dbus_connection_call_sync (c,
1123                                          NULL,    /* bus name */
1124                                          "/dmp/test",
1125                                          "org.gtk.GDBus.DmpInterface",
1126                                          "AddPair",
1127                                          g_variant_new ("(ii)", 2, n),
1128                                          G_VARIANT_TYPE ("(i)"),
1129                                          G_DBUS_CALL_FLAGS_NONE,
1130                                          -1, /* timeout_msec */
1131                                          NULL, /* GCancellable */
1132                                          &error);
1133       g_assert_no_error (error);
1134       g_variant_get (res, "(i)", &val);
1135       g_assert_cmpint (val, ==, 2 + n);
1136       g_variant_unref (res);
1137       g_object_unref (c);
1138   }
1139
1140   g_main_loop_quit (data->loop);
1141   g_thread_join (service_thread);
1142   dmp_data_free (data);
1143 }
1144
1145 /* ---------------------------------------------------------------------------------------------------- */
1146
1147 static gboolean
1148 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1149                                            GIOStream         *stream,
1150                                            GCredentials      *credentials,
1151                                            gpointer           user_data)
1152 {
1153   PeerData *data = user_data;
1154   gboolean authorized;
1155
1156   data->num_connection_attempts++;
1157
1158   authorized = TRUE;
1159   if (!data->accept_connection)
1160     {
1161       authorized = FALSE;
1162       g_main_loop_quit (loop);
1163     }
1164
1165   return authorized;
1166 }
1167
1168 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1169 static gboolean
1170 nonce_tcp_on_new_connection (GDBusServer *server,
1171                              GDBusConnection *connection,
1172                              gpointer user_data)
1173 {
1174   PeerData *data = user_data;
1175
1176   g_ptr_array_add (data->current_connections, g_object_ref (connection));
1177
1178   g_main_loop_quit (loop);
1179
1180   return TRUE;
1181 }
1182
1183 static gpointer
1184 nonce_tcp_service_thread_func (gpointer user_data)
1185 {
1186   PeerData *data = user_data;
1187   GMainContext *service_context;
1188   GDBusAuthObserver *observer;
1189   GError *error;
1190
1191   service_context = g_main_context_new ();
1192   g_main_context_push_thread_default (service_context);
1193
1194   error = NULL;
1195   observer = g_dbus_auth_observer_new ();
1196   server = g_dbus_server_new_sync ("nonce-tcp:",
1197                                    G_DBUS_SERVER_FLAGS_NONE,
1198                                    test_guid,
1199                                    observer,
1200                                    NULL, /* cancellable */
1201                                    &error);
1202   g_assert_no_error (error);
1203
1204   g_signal_connect (server,
1205                     "new-connection",
1206                     G_CALLBACK (nonce_tcp_on_new_connection),
1207                     data);
1208   g_signal_connect (observer,
1209                     "authorize-authenticated-peer",
1210                     G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1211                     data);
1212   g_object_unref (observer);
1213
1214   g_dbus_server_start (server);
1215
1216   service_loop = g_main_loop_new (service_context, FALSE);
1217   g_main_loop_run (service_loop);
1218
1219   g_main_context_pop_thread_default (service_context);
1220
1221   g_main_loop_unref (service_loop);
1222   g_main_context_unref (service_context);
1223
1224   /* test code specifically unrefs the server - see below */
1225   g_assert (server == NULL);
1226
1227   return NULL;
1228 }
1229
1230 static void
1231 test_nonce_tcp (void)
1232 {
1233   PeerData data;
1234   GError *error;
1235   GThread *service_thread;
1236   GDBusConnection *c;
1237   gchar *s;
1238   gchar *nonce_file;
1239   gboolean res;
1240   const gchar *address;
1241
1242   memset (&data, '\0', sizeof (PeerData));
1243   data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1244
1245   error = NULL;
1246   server = NULL;
1247   service_loop = NULL;
1248   service_thread = g_thread_new ("nonce-tcp-service",
1249                                  nonce_tcp_service_thread_func,
1250                                  &data);
1251   while (service_loop == NULL)
1252     g_thread_yield ();
1253   g_assert (server != NULL);
1254
1255
1256   /* bring up a connection and accept it */
1257   data.accept_connection = TRUE;
1258   error = NULL;
1259   c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1260                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1261                                               NULL, /* GDBusAuthObserver */
1262                                               NULL, /* cancellable */
1263                                               &error);
1264   g_assert_no_error (error);
1265   g_assert (c != NULL);
1266   while (data.current_connections->len < 1)
1267     g_thread_yield ();
1268   g_assert_cmpint (data.current_connections->len, ==, 1);
1269   g_assert_cmpint (data.num_connection_attempts, ==, 1);
1270   g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1271   g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1272   g_object_unref (c);
1273
1274   /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1275    */
1276
1277   address = g_dbus_server_get_client_address (server);
1278
1279   s = strstr (address, "noncefile=");
1280   g_assert (s != NULL);
1281   s += sizeof "noncefile=" - 1;
1282   nonce_file = g_strdup (s);
1283
1284   /* First try invalid data in the nonce file - this will actually
1285    * make the client send this and the server will reject it. The way
1286    * it works is that if the nonce doesn't match, the server will
1287    * simply close the connection. So, from the client point of view,
1288    * we can see a variety of errors.
1289    */
1290   error = NULL;
1291   res = g_file_set_contents (nonce_file,
1292                              "0123456789012345",
1293                              -1,
1294                              &error);
1295   g_assert_no_error (error);
1296   g_assert (res);
1297   c = g_dbus_connection_new_for_address_sync (address,
1298                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1299                                               NULL, /* GDBusAuthObserver */
1300                                               NULL, /* cancellable */
1301                                               &error);
1302   _g_assert_error_domain (error, G_IO_ERROR);
1303   g_error_free (error);
1304   g_assert (c == NULL);
1305
1306   /* Then try with a nonce-file of incorrect length - this will make
1307    * the client complain - we won't even try connecting to the server
1308    * for this
1309    */
1310   error = NULL;
1311   res = g_file_set_contents (nonce_file,
1312                              "0123456789012345_",
1313                              -1,
1314                              &error);
1315   g_assert_no_error (error);
1316   g_assert (res);
1317   c = g_dbus_connection_new_for_address_sync (address,
1318                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1319                                               NULL, /* GDBusAuthObserver */
1320                                               NULL, /* cancellable */
1321                                               &error);
1322   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1323   g_error_free (error);
1324   g_assert (c == NULL);
1325
1326   /* Finally try with no nonce-file at all */
1327   g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1328   error = NULL;
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);
1337
1338   g_free (nonce_file);
1339
1340   g_dbus_server_stop (server);
1341   g_object_unref (server);
1342   server = NULL;
1343
1344   g_main_loop_quit (service_loop);
1345   g_thread_join (service_thread);
1346 }
1347
1348 static void
1349 test_credentials (void)
1350 {
1351   GCredentials *c1, *c2;
1352   GError *error;
1353   gchar *desc;
1354
1355   c1 = g_credentials_new ();
1356   c2 = g_credentials_new ();
1357
1358   error = NULL;
1359   if (g_credentials_set_unix_user (c2, getuid (), &error))
1360     g_assert_no_error (error);
1361
1362   g_clear_error (&error);
1363   g_assert (g_credentials_is_same_user (c1, c2, &error));
1364   g_assert_no_error (error);
1365
1366   desc = g_credentials_to_string (c1);
1367   g_assert (desc != NULL);
1368   g_free (desc);
1369
1370   g_object_unref (c1);
1371   g_object_unref (c2);
1372 }
1373
1374 /* ---------------------------------------------------------------------------------------------------- */
1375
1376 #ifdef G_OS_UNIX
1377
1378 /* Chosen to be big enough to overflow the socket buffer */
1379 #define OVERFLOW_NUM_SIGNALS 5000
1380 #define OVERFLOW_TIMEOUT_SEC 10
1381
1382 static GDBusMessage *
1383 overflow_filter_func (GDBusConnection *connection,
1384                       GDBusMessage    *message,
1385                       gboolean         incoming,
1386                       gpointer         user_data)
1387 {
1388   volatile gint *counter = user_data;
1389   *counter += 1;
1390   return message;
1391 }
1392
1393 static gboolean
1394 overflow_on_500ms_later_func (gpointer user_data)
1395 {
1396   g_main_loop_quit (loop);
1397   return FALSE; /* don't keep the idle */
1398 }
1399
1400 static void
1401 test_overflow (void)
1402 {
1403   gint sv[2];
1404   gint n;
1405   GSocket *socket;
1406   GSocketConnection *socket_connection;
1407   GDBusConnection *producer, *consumer;
1408   GError *error;
1409   GTimer *timer;
1410   volatile gint n_messages_received;
1411   volatile gint n_messages_sent;
1412
1413   g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
1414
1415   error = NULL;
1416   socket = g_socket_new_from_fd (sv[0], &error);
1417   g_assert_no_error (error);
1418   socket_connection = g_socket_connection_factory_create_connection (socket);
1419   g_assert (socket_connection != NULL);
1420   g_object_unref (socket);
1421   producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1422                                          NULL, /* guid */
1423                                          G_DBUS_CONNECTION_FLAGS_NONE,
1424                                          NULL, /* GDBusAuthObserver */
1425                                          NULL, /* GCancellable */
1426                                          &error);
1427   g_dbus_connection_set_exit_on_close (producer, TRUE);
1428   g_assert_no_error (error);
1429   g_object_unref (socket_connection);
1430   n_messages_sent = 0;
1431   g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
1432
1433   /* send enough data that we get an EAGAIN */
1434   for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
1435     {
1436       error = NULL;
1437       g_dbus_connection_emit_signal (producer,
1438                                      NULL, /* destination */
1439                                      "/org/foo/Object",
1440                                      "org.foo.Interface",
1441                                      "Member",
1442                                      g_variant_new ("(s)", "a string"),
1443                                      &error);
1444       g_assert_no_error (error);
1445     }
1446
1447   /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
1448    * kernel buffers) and verify that n_messages_sent <
1449    * OVERFLOW_NUM_SIGNALS
1450    *
1451    * This is to verify that not all the submitted messages have been
1452    * sent to the underlying transport.
1453    */
1454   g_timeout_add (500, overflow_on_500ms_later_func, NULL);
1455   g_main_loop_run (loop);
1456   g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
1457
1458   /* now suck it all out as a client, and add it up */
1459   socket = g_socket_new_from_fd (sv[1], &error);
1460   g_assert_no_error (error);
1461   socket_connection = g_socket_connection_factory_create_connection (socket);
1462   g_assert (socket_connection != NULL);
1463   g_object_unref (socket);
1464   consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1465                                          NULL, /* guid */
1466                                          G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
1467                                          NULL, /* GDBusAuthObserver */
1468                                          NULL, /* GCancellable */
1469                                          &error);
1470   g_assert_no_error (error);
1471   g_object_unref (socket_connection);
1472   n_messages_received = 0;
1473   g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
1474   g_dbus_connection_start_message_processing (consumer);
1475
1476   timer = g_timer_new ();
1477   g_timer_start (timer);
1478
1479   while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
1480       g_main_context_iteration (NULL, FALSE);
1481
1482   g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
1483   g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
1484
1485   g_timer_destroy (timer);
1486   g_object_unref (consumer);
1487   g_object_unref (producer);
1488 }
1489 #else
1490 static void
1491 test_overflow (void)
1492 {
1493   /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
1494 }
1495 #endif
1496
1497 /* ---------------------------------------------------------------------------------------------------- */
1498
1499 static gboolean
1500 tcp_anonymous_on_new_connection (GDBusServer     *server,
1501                                  GDBusConnection *connection,
1502                                  gpointer         user_data)
1503 {
1504   gboolean *seen_connection = user_data;
1505   *seen_connection = TRUE;
1506   return TRUE;
1507 }
1508
1509 static gpointer
1510 tcp_anonymous_service_thread_func (gpointer user_data)
1511 {
1512   gboolean *seen_connection = user_data;
1513   GMainContext *service_context;
1514   GError *error;
1515
1516   service_context = g_main_context_new ();
1517   g_main_context_push_thread_default (service_context);
1518
1519   error = NULL;
1520   server = g_dbus_server_new_sync ("tcp:",
1521                                    G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS,
1522                                    test_guid,
1523                                    NULL, /* GDBusObserver* */
1524                                    NULL, /* GCancellable* */
1525                                    &error);
1526   g_assert_no_error (error);
1527
1528   g_signal_connect (server,
1529                     "new-connection",
1530                     G_CALLBACK (tcp_anonymous_on_new_connection),
1531                     seen_connection);
1532
1533   g_dbus_server_start (server);
1534
1535   service_loop = g_main_loop_new (service_context, FALSE);
1536   g_main_loop_run (service_loop);
1537
1538   g_main_context_pop_thread_default (service_context);
1539
1540   g_main_loop_unref (service_loop);
1541   g_main_context_unref (service_context);
1542
1543   return NULL;
1544 }
1545
1546 static void
1547 test_tcp_anonymous (void)
1548 {
1549   gboolean seen_connection;
1550   GThread *service_thread;
1551   GDBusConnection *connection;
1552   GError *error;
1553
1554   seen_connection = FALSE;
1555   service_loop = NULL;
1556   service_thread = g_thread_new ("tcp-anon-service",
1557                                  tcp_anonymous_service_thread_func,
1558                                  &seen_connection);
1559   while (service_loop == NULL)
1560     g_thread_yield ();
1561   g_assert (server != NULL);
1562
1563   error = NULL;
1564   connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1565                                                        G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1566                                                        NULL, /* GDBusAuthObserver* */
1567                                                        NULL, /* GCancellable */
1568                                                        &error);
1569   g_assert_no_error (error);
1570   g_assert (connection != NULL);
1571
1572   while (!seen_connection)
1573     g_thread_yield ();
1574
1575   g_object_unref (connection);
1576
1577   g_main_loop_quit (service_loop);
1578   g_dbus_server_stop (server);
1579   g_object_unref (server);
1580   server = NULL;
1581
1582   g_thread_join (service_thread);
1583 }
1584
1585 /* ---------------------------------------------------------------------------------------------------- */
1586
1587 static GDBusServer *codegen_server = NULL;
1588
1589 static gboolean
1590 codegen_on_animal_poke (ExampleAnimal          *animal,
1591                         GDBusMethodInvocation  *invocation,
1592                         gboolean                make_sad,
1593                         gboolean                make_happy,
1594                         gpointer                user_data)
1595 {
1596   if ((make_sad && make_happy) || (!make_sad && !make_happy))
1597     {
1598       g_main_loop_quit (service_loop);
1599
1600       g_dbus_method_invocation_return_dbus_error (invocation,
1601                                                   "org.gtk.GDBus.Examples.ObjectManager.Error.Failed",
1602                                                   "Exactly one of make_sad or make_happy must be TRUE");
1603       goto out;
1604     }
1605
1606   if (make_sad)
1607     {
1608       if (g_strcmp0 (example_animal_get_mood (animal), "Sad") == 0)
1609         {
1610           g_dbus_method_invocation_return_dbus_error (invocation,
1611                                                       "org.gtk.GDBus.Examples.ObjectManager.Error.SadAnimalIsSad",
1612                                                       "Sad animal is already sad");
1613           goto out;
1614         }
1615
1616       example_animal_set_mood (animal, "Sad");
1617       example_animal_complete_poke (animal, invocation);
1618       goto out;
1619     }
1620
1621   if (make_happy)
1622     {
1623       if (g_strcmp0 (example_animal_get_mood (animal), "Happy") == 0)
1624         {
1625           g_dbus_method_invocation_return_dbus_error (invocation,
1626                                                       "org.gtk.GDBus.Examples.ObjectManager.Error.HappyAnimalIsHappy",
1627                                                       "Happy animal is already happy");
1628           goto out;
1629         }
1630
1631       example_animal_set_mood (animal, "Happy");
1632       example_animal_complete_poke (animal, invocation);
1633       goto out;
1634     }
1635
1636   g_assert_not_reached ();
1637
1638  out:
1639   return TRUE; /* to indicate that the method was handled */
1640 }
1641
1642 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1643 static gboolean
1644 codegen_on_new_connection (GDBusServer *server,
1645                            GDBusConnection *connection,
1646                            gpointer user_data)
1647 {
1648   ExampleAnimal *animal = user_data;
1649   GError        *error = NULL;
1650
1651   /* g_print ("Client connected.\n" */
1652   /*          "Negotiated capabilities: unix-fd-passing=%d\n", */
1653   /*          g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING); */
1654
1655   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (animal), connection,
1656                                     "/Example/Animals/000", &error);
1657   g_assert_no_error (error);
1658
1659   return TRUE;
1660 }
1661
1662 static gpointer
1663 codegen_service_thread_func (gpointer user_data)
1664 {
1665   GMainContext   *service_context;
1666   ExampleAnimal  *animal;
1667   GError         *error = NULL;
1668
1669   service_context = g_main_context_new ();
1670   g_main_context_push_thread_default (service_context);
1671
1672   /* Create the animal in the right thread context */
1673   animal = example_animal_skeleton_new ();
1674
1675   /* Handle Poke() D-Bus method invocations on the .Animal interface */
1676   g_signal_connect (animal, "handle-poke",
1677                     G_CALLBACK (codegen_on_animal_poke),
1678                     NULL); /* user_data */
1679
1680   codegen_server = g_dbus_server_new_sync (tmp_address,
1681                                            G_DBUS_SERVER_FLAGS_NONE,
1682                                            test_guid,
1683                                            NULL, /* observer */
1684                                            NULL, /* cancellable */
1685                                            &error);
1686   g_assert_no_error (error);
1687   g_dbus_server_start (codegen_server);
1688
1689   g_signal_connect (codegen_server, "new-connection",
1690                     G_CALLBACK (codegen_on_new_connection),
1691                     animal);
1692
1693   service_loop = g_main_loop_new (service_context, FALSE);
1694   g_main_loop_run (service_loop);
1695
1696   g_object_unref (animal);
1697
1698   g_main_context_pop_thread_default (service_context);
1699
1700   g_main_loop_unref (service_loop);
1701   g_main_context_unref (service_context);
1702
1703   g_dbus_server_stop (codegen_server);
1704   g_object_unref (codegen_server);
1705   codegen_server = NULL;
1706
1707   return NULL;
1708 }
1709
1710
1711 static gboolean
1712 codegen_quit_mainloop_timeout (gpointer data)
1713 {
1714   g_main_loop_quit (loop);
1715   return FALSE;
1716 }
1717
1718 static void
1719 codegen_test_peer (void)
1720 {
1721   GDBusConnection     *connection;
1722   ExampleAnimal       *animal1, *animal2;
1723   GThread             *service_thread;
1724   GError              *error = NULL;
1725   GVariant            *value;
1726
1727   /* bring up a server - we run the server in a different thread to avoid deadlocks */
1728   service_loop = NULL;
1729   service_thread = g_thread_new ("codegen_test_peer",
1730                                  codegen_service_thread_func,
1731                                  NULL);
1732   while (service_loop == NULL)
1733     g_thread_yield ();
1734   g_assert (codegen_server != NULL);
1735
1736   /* Get an animal 1 ...  */
1737   connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (codegen_server),
1738                                                        G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1739                                                        NULL, /* GDBusAuthObserver */
1740                                                        NULL, /* cancellable */
1741                                                        &error);
1742   g_assert_no_error (error);
1743   g_assert (connection != NULL);
1744
1745   animal1 = example_animal_proxy_new_sync (connection, 0, NULL,
1746                                            "/Example/Animals/000", NULL, &error);
1747   g_assert_no_error (error);
1748   g_assert (animal1 != NULL);
1749   g_object_unref (connection);
1750
1751   /* Get animal 2 ...  */
1752   connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (codegen_server),
1753                                                        G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1754                                                        NULL, /* GDBusAuthObserver */
1755                                                        NULL, /* cancellable */
1756                                                        &error);
1757   g_assert_no_error (error);
1758   g_assert (connection != NULL);
1759
1760   animal2 = example_animal_proxy_new_sync (connection, 0, NULL,
1761                                            "/Example/Animals/000", NULL, &error);
1762   g_assert_no_error (error);
1763   g_assert (animal2 != NULL);
1764   g_object_unref (connection);
1765
1766   /* Make animal sad via animal1  */
1767   example_animal_call_poke_sync (animal1, TRUE, FALSE, NULL, &error);
1768   g_assert_no_error (error);
1769
1770   /* Poke server and make sure animal is updated */
1771   value = g_dbus_proxy_call_sync (G_DBUS_PROXY (animal1),
1772                                   "org.freedesktop.DBus.Peer.Ping",
1773                                   NULL, G_DBUS_CALL_FLAGS_NONE, -1,
1774                                   NULL, &error);
1775   g_assert_no_error (error);
1776   g_assert (value != NULL);
1777   g_variant_unref (value);
1778
1779   /* Give the proxies a chance to refresh in the defaul main loop */
1780   g_timeout_add (100, codegen_quit_mainloop_timeout, NULL);
1781   g_main_loop_run (loop);
1782
1783   /* Assert animals are sad */
1784   g_assert_cmpstr (example_animal_get_mood (animal1), ==, "Sad");
1785   g_assert_cmpstr (example_animal_get_mood (animal2), ==, "Sad");
1786
1787   /* Make animal happy via animal2  */
1788   example_animal_call_poke_sync (animal2, FALSE, TRUE, NULL, &error);
1789   g_assert_no_error (error);
1790
1791   /* Poke server and make sure animal is updated */
1792   value = g_dbus_proxy_call_sync (G_DBUS_PROXY (animal2),
1793                                   "org.freedesktop.DBus.Peer.Ping",
1794                                   NULL, G_DBUS_CALL_FLAGS_NONE, -1,
1795                                   NULL, &error);
1796   g_assert_no_error (error);
1797   g_assert (value != NULL);
1798   g_variant_unref (value);
1799
1800   /* Give the proxies a chance to refresh in the defaul main loop */
1801   g_timeout_add (1000, codegen_quit_mainloop_timeout, NULL);
1802   g_main_loop_run (loop);
1803
1804   /* Assert animals are happy */
1805   g_assert_cmpstr (example_animal_get_mood (animal1), ==, "Happy");
1806   g_assert_cmpstr (example_animal_get_mood (animal2), ==, "Happy");
1807
1808   /* This final call making the animal happy and sad will cause
1809    * the server to quit, when the server quits we dont get property
1810    * change notifications anyway because those are done from an idle handler
1811    */
1812   example_animal_call_poke_sync (animal2, TRUE, TRUE, NULL, &error);
1813
1814   g_object_unref (animal1);
1815   g_object_unref (animal2);
1816   g_thread_join (service_thread);
1817 }
1818
1819 /* ---------------------------------------------------------------------------------------------------- */
1820
1821
1822 int
1823 main (int   argc,
1824       char *argv[])
1825 {
1826   gint ret;
1827   GDBusNodeInfo *introspection_data = NULL;
1828   gchar *tmpdir = NULL;
1829
1830   if (g_getenv ("G_TEST_DATA"))
1831     datapath = g_getenv ("G_TEST_DATA");
1832   else
1833     datapath = SRCDIR;
1834
1835   g_test_init (&argc, &argv, NULL);
1836
1837   introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1838   g_assert (introspection_data != NULL);
1839   test_interface_introspection_data = introspection_data->interfaces[0];
1840
1841   test_guid = g_dbus_generate_guid ();
1842
1843   if (is_unix)
1844     {
1845       if (g_unix_socket_address_abstract_names_supported ())
1846         tmp_address = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
1847       else
1848         {
1849           tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
1850           tmp_address = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
1851         }
1852     }
1853   else
1854     tmp_address = g_strdup ("nonce-tcp:");
1855
1856   /* all the tests rely on a shared main loop */
1857   loop = g_main_loop_new (NULL, FALSE);
1858
1859   g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1860   g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1861   g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1862   g_test_add_func ("/gdbus/tcp-anonymous", test_tcp_anonymous);
1863   g_test_add_func ("/gdbus/credentials", test_credentials);
1864   g_test_add_func ("/gdbus/overflow", test_overflow);
1865   g_test_add_func ("/gdbus/codegen-peer-to-peer", codegen_test_peer);
1866
1867   ret = g_test_run();
1868
1869   g_main_loop_unref (loop);
1870   g_free (test_guid);
1871   g_dbus_node_info_unref (introspection_data);
1872   if (is_unix)
1873     g_free (tmp_address);
1874   if (tmpdir)
1875     {
1876       g_rmdir (tmpdir);
1877       g_free (tmpdir);
1878     }
1879
1880   return ret;
1881 }