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