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