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