Plug a mem leak in the gdbus-peer test
[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     buf = read_all_from_fd (fd, &len, &error);
740     g_assert_no_error (error);
741     g_assert (buf != NULL);
742     close (fd);
743
744     error = NULL;
745     g_file_get_contents ("/etc/hosts",
746                          &buf2,
747                          &len2,
748                          &error);
749     g_assert_no_error (error);
750     g_assert_cmpint (len, ==, len2);
751     g_assert (memcmp (buf, buf2, len) == 0);
752     g_free (buf2);
753     g_free (buf);
754   }
755 #else
756   error = NULL;
757   result = g_dbus_proxy_call_sync (proxy,
758                                    "OpenFile",
759                                    g_variant_new ("(s)", "boo"),
760                                    G_DBUS_CALL_FLAGS_NONE,
761                                    -1,
762                                    NULL,  /* GCancellable */
763                                    &error);
764   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
765   g_assert (result == NULL);
766   g_error_free (error);
767 #endif /* G_OS_UNIX */
768
769   /* Check that g_socket_get_credentials() work - this really should
770    * be in a GSocket-specific test suite but no such test suite exists
771    * right now.
772    */
773   {
774     GSocket *socket;
775     GCredentials *credentials;
776     socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (g_dbus_connection_get_stream (c)));
777     g_assert (G_IS_SOCKET (socket));
778     error = NULL;
779     credentials = g_socket_get_credentials (socket, &error);
780 #ifdef __linux__
781     {
782       struct ucred *native_creds;
783       g_assert_no_error (error);
784       g_assert (G_IS_CREDENTIALS (credentials));
785       native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED);
786       g_assert (native_creds != NULL);
787       g_assert (native_creds->uid == getuid ());
788       g_assert (native_creds->gid == getgid ());
789       g_assert (native_creds->pid == getpid ());
790     }
791     g_object_unref (credentials);
792 #else
793     g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
794     g_assert (credentials == NULL);
795 #endif
796   }
797
798
799   /* bring up a connection - don't accept it - this should fail
800    */
801   data.accept_connection = FALSE;
802   error = NULL;
803   c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
804                                                G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
805                                                NULL, /* GDBusAuthObserver */
806                                                NULL, /* cancellable */
807                                                &error);
808   _g_assert_error_domain (error, G_IO_ERROR);
809   g_assert (c2 == NULL);
810
811 #if 0
812   /* TODO: THIS TEST DOESN'T WORK YET */
813
814   /* bring up a connection - accept it.. then disconnect from the client side - check
815    * that the server side gets the disconnect signal.
816    */
817   error = NULL;
818   data.accept_connection = TRUE;
819   c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
820                                                G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
821                                                NULL, /* GDBusAuthObserver */
822                                                NULL, /* cancellable */
823                                                &error);
824   g_assert_no_error (error);
825   g_assert (c2 != NULL);
826   g_assert (!g_dbus_connection_get_is_disconnected (c2));
827   while (data.num_connection_attempts < 3)
828     g_main_loop_run (loop);
829   g_assert_cmpint (data.current_connections->len, ==, 2);
830   g_assert_cmpint (data.num_connection_attempts, ==, 3);
831   g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
832   g_idle_add (on_do_disconnect_in_idle, c2);
833   g_debug ("==================================================");
834   g_debug ("==================================================");
835   g_debug ("==================================================");
836   g_debug ("waiting for disconnect on connection %p, stream %p",
837            data.current_connections->pdata[1],
838            g_dbus_connection_get_stream (data.current_connections->pdata[1]));
839
840   g_timeout_add (2000, check_connection, &data);
841   //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
842   g_main_loop_run (loop);
843   g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
844   g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
845 #endif
846
847   /* unref the server and stop listening for new connections
848    *
849    * This won't bring down the established connections - check that c is still connected
850    * by invoking a method
851    */
852   //g_socket_service_stop (service);
853   //g_object_unref (service);
854   g_dbus_server_stop (server);
855   g_object_unref (server);
856   server = NULL;
857
858   error = NULL;
859   result = g_dbus_proxy_call_sync (proxy,
860                                    "HelloPeer",
861                                    g_variant_new ("(s)", "Hey Again Peer!"),
862                                    G_DBUS_CALL_FLAGS_NONE,
863                                    -1,
864                                    NULL,  /* GCancellable */
865                                    &error);
866   g_assert_no_error (error);
867   g_variant_get (result, "(&s)", &s);
868   g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
869   g_variant_unref (result);
870   g_assert_cmpint (data.num_method_calls, ==, 5);
871
872 #if 0
873   /* TODO: THIS TEST DOESN'T WORK YET */
874
875   /* now disconnect from the server side - check that the client side gets the signal */
876   g_assert_cmpint (data.current_connections->len, ==, 1);
877   g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
878   g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
879   if (!g_dbus_connection_get_is_disconnected (c))
880     _g_assert_signal_received (c, "closed");
881   g_assert (g_dbus_connection_get_is_disconnected (c));
882 #endif
883
884   g_object_unref (c);
885   g_ptr_array_unref (data.current_connections);
886   g_object_unref (proxy);
887
888   g_main_loop_quit (service_loop);
889   g_thread_join (service_thread);
890 }
891
892 /* ---------------------------------------------------------------------------------------------------- */
893
894 typedef struct
895 {
896   GDBusServer *server;
897   GMainContext *context;
898   GMainLoop *loop;
899
900   GList *connections;
901 } DmpData;
902
903 static void
904 dmp_data_free (DmpData *data)
905 {
906   g_main_loop_unref (data->loop);
907   g_main_context_unref (data->context);
908   g_object_unref (data->server);
909   g_list_foreach (data->connections, (GFunc) g_object_unref, NULL);
910   g_list_free (data->connections);
911   g_free (data);
912 }
913
914 static void
915 dmp_on_method_call (GDBusConnection       *connection,
916                     const gchar           *sender,
917                     const gchar           *object_path,
918                     const gchar           *interface_name,
919                     const gchar           *method_name,
920                     GVariant              *parameters,
921                     GDBusMethodInvocation *invocation,
922                     gpointer               user_data)
923 {
924   //DmpData *data = user_data;
925   gint32 first;
926   gint32 second;
927   g_variant_get (parameters,
928                  "(ii)",
929                  &first,
930                  &second);
931   g_dbus_method_invocation_return_value (invocation,
932                                          g_variant_new ("(i)", first + second));
933 }
934
935 static const GDBusInterfaceVTable dmp_interface_vtable =
936 {
937   dmp_on_method_call,
938   NULL,  /* get_property */
939   NULL   /* set_property */
940 };
941
942
943 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
944 static void
945 dmp_on_new_connection (GDBusServer     *server,
946                        GDBusConnection *connection,
947                        gpointer         user_data)
948 {
949   DmpData *data = user_data;
950   GDBusNodeInfo *node;
951   GError *error;
952
953   /* accept the connection */
954   data->connections = g_list_prepend (data->connections, g_object_ref (connection));
955
956   error = NULL;
957   node = g_dbus_node_info_new_for_xml ("<node>"
958                                        "  <interface name='org.gtk.GDBus.DmpInterface'>"
959                                        "    <method name='AddPair'>"
960                                        "      <arg type='i' name='first' direction='in'/>"
961                                        "      <arg type='i' name='second' direction='in'/>"
962                                        "      <arg type='i' name='sum' direction='out'/>"
963                                        "    </method>"
964                                        "  </interface>"
965                                        "</node>",
966                                        &error);
967   g_assert_no_error (error);
968
969   /* sleep 100ms before exporting an object - this is to test that
970    * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
971    * (GDBusServer uses this feature).
972    */
973   usleep (100 * 1000);
974
975   /* export an object */
976   error = NULL;
977   g_dbus_connection_register_object (connection,
978                                      "/dmp/test",
979                                      node->interfaces[0],
980                                      &dmp_interface_vtable,
981                                      data,
982                                      NULL,
983                                      &error);
984   g_dbus_node_info_unref (node);
985 }
986
987 static gpointer
988 dmp_thread_func (gpointer user_data)
989 {
990   DmpData *data = user_data;
991   GError *error;
992   gchar *guid;
993
994   data->context = g_main_context_new ();
995   g_main_context_push_thread_default (data->context);
996
997   error = NULL;
998   guid = g_dbus_generate_guid ();
999   data->server = g_dbus_server_new_sync ("unix:tmpdir=/tmp/gdbus-test-",
1000                                          G_DBUS_SERVER_FLAGS_NONE,
1001                                          guid,
1002                                          NULL, /* GDBusAuthObserver */
1003                                          NULL, /* GCancellable */
1004                                          &error);
1005   g_assert_no_error (error);
1006   g_signal_connect (data->server,
1007                     "new-connection",
1008                     G_CALLBACK (dmp_on_new_connection),
1009                     data);
1010
1011   g_dbus_server_start (data->server);
1012
1013   data->loop = g_main_loop_new (data->context, FALSE);
1014   g_main_loop_run (data->loop);
1015
1016   g_main_context_pop_thread_default (data->context);
1017
1018   g_free (guid);
1019   return NULL;
1020 }
1021
1022 static void
1023 delayed_message_processing (void)
1024 {
1025   GError *error;
1026   DmpData *data;
1027   GThread *service_thread;
1028   guint n;
1029
1030   data = g_new0 (DmpData, 1);
1031
1032   error = NULL;
1033   service_thread = g_thread_create (dmp_thread_func,
1034                                     data,
1035                                     TRUE,
1036                                     &error);
1037   while (data->server == NULL || !g_dbus_server_is_active (data->server))
1038     g_thread_yield ();
1039
1040   for (n = 0; n < 5; n++)
1041     {
1042       GDBusConnection *c;
1043       GVariant *res;
1044       gint32 val;
1045
1046       error = NULL;
1047       c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
1048                                                   G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1049                                                   NULL, /* GDBusAuthObserver */
1050                                                   NULL, /* GCancellable */
1051                                                   &error);
1052       g_assert_no_error (error);
1053
1054       error = NULL;
1055       res = g_dbus_connection_call_sync (c,
1056                                          NULL,    /* bus name */
1057                                          "/dmp/test",
1058                                          "org.gtk.GDBus.DmpInterface",
1059                                          "AddPair",
1060                                          g_variant_new ("(ii)", 2, n),
1061                                          G_VARIANT_TYPE ("(i)"),
1062                                          G_DBUS_CALL_FLAGS_NONE,
1063                                          -1, /* timeout_msec */
1064                                          NULL, /* GCancellable */
1065                                          &error);
1066       g_assert_no_error (error);
1067       g_variant_get (res, "(i)", &val);
1068       g_assert_cmpint (val, ==, 2 + n);
1069       g_variant_unref (res);
1070       g_object_unref (c);
1071   }
1072
1073   g_main_loop_quit (data->loop);
1074   g_thread_join (service_thread);
1075   dmp_data_free (data);
1076 }
1077
1078 /* ---------------------------------------------------------------------------------------------------- */
1079
1080 static gboolean
1081 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1082                                            GIOStream         *stream,
1083                                            GCredentials      *credentials,
1084                                            gpointer           user_data)
1085 {
1086   PeerData *data = user_data;
1087   gboolean authorized;
1088
1089   data->num_connection_attempts++;
1090
1091   authorized = TRUE;
1092   if (!data->accept_connection)
1093     {
1094       authorized = FALSE;
1095       g_main_loop_quit (loop);
1096     }
1097
1098   return authorized;
1099 }
1100
1101 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1102 static void
1103 nonce_tcp_on_new_connection (GDBusServer *server,
1104                              GDBusConnection *connection,
1105                              gpointer user_data)
1106 {
1107   PeerData *data = user_data;
1108
1109   g_ptr_array_add (data->current_connections, g_object_ref (connection));
1110
1111   g_main_loop_quit (loop);
1112 }
1113
1114 static gpointer
1115 nonce_tcp_service_thread_func (gpointer user_data)
1116 {
1117   PeerData *data = user_data;
1118   GMainContext *service_context;
1119   GDBusAuthObserver *observer;
1120   GError *error;
1121
1122   service_context = g_main_context_new ();
1123   g_main_context_push_thread_default (service_context);
1124
1125   error = NULL;
1126   observer = g_dbus_auth_observer_new ();
1127   server = g_dbus_server_new_sync ("nonce-tcp:",
1128                                    G_DBUS_SERVER_FLAGS_NONE,
1129                                    test_guid,
1130                                    observer,
1131                                    NULL, /* cancellable */
1132                                    &error);
1133   g_assert_no_error (error);
1134
1135   g_signal_connect (server,
1136                     "new-connection",
1137                     G_CALLBACK (nonce_tcp_on_new_connection),
1138                     data);
1139   g_signal_connect (observer,
1140                     "authorize-authenticated-peer",
1141                     G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1142                     data);
1143   g_object_unref (observer);
1144
1145   g_dbus_server_start (server);
1146
1147   service_loop = g_main_loop_new (service_context, FALSE);
1148   g_main_loop_run (service_loop);
1149
1150   g_main_context_pop_thread_default (service_context);
1151
1152   g_main_loop_unref (service_loop);
1153   g_main_context_unref (service_context);
1154
1155   /* test code specifically unrefs the server - see below */
1156   g_assert (server == NULL);
1157
1158   return NULL;
1159 }
1160
1161 static void
1162 test_nonce_tcp (void)
1163 {
1164   PeerData data;
1165   GError *error;
1166   GThread *service_thread;
1167   GDBusConnection *c;
1168   gchar *s;
1169   gchar *nonce_file;
1170   gboolean res;
1171   const gchar *address;
1172
1173   memset (&data, '\0', sizeof (PeerData));
1174   data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1175
1176   error = NULL;
1177   server = NULL;
1178   service_loop = NULL;
1179   service_thread = g_thread_create (nonce_tcp_service_thread_func,
1180                                     &data,
1181                                     TRUE,
1182                                     &error);
1183   while (service_loop == NULL)
1184     g_thread_yield ();
1185   g_assert (server != NULL);
1186
1187
1188   /* bring up a connection and accept it */
1189   data.accept_connection = TRUE;
1190   error = NULL;
1191   c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1192                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1193                                               NULL, /* GDBusAuthObserver */
1194                                               NULL, /* cancellable */
1195                                               &error);
1196   g_assert_no_error (error);
1197   g_assert (c != NULL);
1198   while (data.current_connections->len < 1)
1199     g_main_loop_run (loop);
1200   g_assert_cmpint (data.current_connections->len, ==, 1);
1201   g_assert_cmpint (data.num_connection_attempts, ==, 1);
1202   g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1203   g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1204   g_object_unref (c);
1205
1206   /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1207    */
1208
1209   address = g_dbus_server_get_client_address (server);
1210
1211   s = strstr (address, "noncefile=");
1212   g_assert (s != NULL);
1213   s += sizeof "noncefile=" - 1;
1214   nonce_file = g_strdup (s);
1215
1216   /* First try invalid data in the nonce file - this will actually
1217    * make the client send this and the server will reject it. The way
1218    * it works is that if the nonce doesn't match, the server will
1219    * simply close the connection. So, from the client point of view,
1220    * we can see a variety of errors.
1221    */
1222   error = NULL;
1223   res = g_file_set_contents (nonce_file,
1224                              "0123456789012345",
1225                              -1,
1226                              &error);
1227   g_assert_no_error (error);
1228   g_assert (res);
1229   c = g_dbus_connection_new_for_address_sync (address,
1230                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1231                                               NULL, /* GDBusAuthObserver */
1232                                               NULL, /* cancellable */
1233                                               &error);
1234   _g_assert_error_domain (error, G_IO_ERROR);
1235   g_assert (c == NULL);
1236
1237   /* Then try with a nonce-file of incorrect length - this will make
1238    * the client complain - we won't even try connecting to the server
1239    * for this
1240    */
1241   error = NULL;
1242   res = g_file_set_contents (nonce_file,
1243                              "0123456789012345_",
1244                              -1,
1245                              &error);
1246   g_assert_no_error (error);
1247   g_assert (res);
1248   c = g_dbus_connection_new_for_address_sync (address,
1249                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1250                                               NULL, /* GDBusAuthObserver */
1251                                               NULL, /* cancellable */
1252                                               &error);
1253   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1254   g_assert (c == NULL);
1255
1256   /* Finally try with no nonce-file at all */
1257   g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1258   error = NULL;
1259   c = g_dbus_connection_new_for_address_sync (address,
1260                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1261                                               NULL, /* GDBusAuthObserver */
1262                                               NULL, /* cancellable */
1263                                               &error);
1264   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1265   g_assert (c == NULL);
1266
1267   g_free (nonce_file);
1268
1269   g_dbus_server_stop (server);
1270   g_object_unref (server);
1271   server = NULL;
1272
1273   g_main_loop_quit (service_loop);
1274   g_thread_join (service_thread);
1275 }
1276
1277 static void
1278 test_credentials (void)
1279 {
1280   GCredentials *c1, *c2;
1281   GError *error;
1282   gchar *desc;
1283
1284   c1 = g_credentials_new ();
1285   c2 = g_credentials_new ();
1286
1287   error = NULL;
1288   if (g_credentials_set_unix_user (c2, getuid (), &error))
1289     g_assert_no_error (error);
1290
1291   g_clear_error (&error);
1292   g_assert (g_credentials_is_same_user (c1, c2, &error));
1293   g_assert_no_error (error);
1294
1295   desc = g_credentials_to_string (c1);
1296   g_assert (desc != NULL);
1297   g_free (desc);
1298
1299   g_object_unref (c1);
1300   g_object_unref (c2);
1301 }
1302
1303 /* ---------------------------------------------------------------------------------------------------- */
1304
1305 #ifdef G_OS_UNIX
1306
1307 /* Chosen to be big enough to overflow the socket buffer */
1308 #define OVERFLOW_NUM_SIGNALS 5000
1309 #define OVERFLOW_TIMEOUT_SEC 10
1310
1311 static GDBusMessageFilterResult
1312 overflow_filter_func (GDBusConnection *connection,
1313                       GDBusMessage    *message,
1314                       gboolean         incoming,
1315                       gpointer         user_data)
1316 {
1317   volatile gint *counter = user_data;
1318   *counter += 1;
1319   return G_DBUS_MESSAGE_FILTER_RESULT_NO_EFFECT;
1320 }
1321
1322 static gboolean
1323 overflow_on_500ms_later_func (gpointer user_data)
1324 {
1325   g_main_loop_quit (loop);
1326   return FALSE; /* don't keep the idle */
1327 }
1328
1329 static void
1330 test_overflow (void)
1331 {
1332   gint sv[2];
1333   gint n;
1334   GSocket *socket;
1335   GSocketConnection *socket_connection;
1336   GDBusConnection *producer, *consumer;
1337   GError *error;
1338   GTimer *timer;
1339   volatile gint n_messages_received;
1340   volatile gint n_messages_sent;
1341
1342   g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
1343
1344   error = NULL;
1345   socket = g_socket_new_from_fd (sv[0], &error);
1346   g_assert_no_error (error);
1347   socket_connection = g_socket_connection_factory_create_connection (socket);
1348   g_assert (socket_connection != NULL);
1349   g_object_unref (socket);
1350   producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1351                                          NULL, /* guid */
1352                                          G_DBUS_CONNECTION_FLAGS_NONE,
1353                                          NULL, /* GDBusAuthObserver */
1354                                          NULL, /* GCancellable */
1355                                          &error);
1356   g_dbus_connection_set_exit_on_close (producer, TRUE);
1357   g_assert_no_error (error);
1358   g_object_unref (socket_connection);
1359   n_messages_sent = 0;
1360   g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
1361
1362   /* send enough data that we get an EAGAIN */
1363   for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
1364     {
1365       error = NULL;
1366       g_dbus_connection_emit_signal (producer,
1367                                      NULL, /* destination */
1368                                      "/org/foo/Object",
1369                                      "org.foo.Interface",
1370                                      "Member",
1371                                      g_variant_new ("(s)", "a string"),
1372                                      &error);
1373       g_assert_no_error (error);
1374     }
1375
1376   /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
1377    * kernel buffers) and verify that n_messages_sent <
1378    * OVERFLOW_NUM_SIGNALS
1379    *
1380    * This is to verify that not all the submitted messages have been
1381    * sent to the underlying transport.
1382    */
1383   g_timeout_add (500, overflow_on_500ms_later_func, NULL);
1384   g_main_loop_run (loop);
1385   g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
1386
1387   /* now suck it all out as a client, and add it up */
1388   socket = g_socket_new_from_fd (sv[1], &error);
1389   g_assert_no_error (error);
1390   socket_connection = g_socket_connection_factory_create_connection (socket);
1391   g_assert (socket_connection != NULL);
1392   g_object_unref (socket);
1393   consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1394                                          NULL, /* guid */
1395                                          G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
1396                                          NULL, /* GDBusAuthObserver */
1397                                          NULL, /* GCancellable */
1398                                          &error);
1399   g_assert_no_error (error);
1400   g_object_unref (socket_connection);
1401   n_messages_received = 0;
1402   g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
1403   g_dbus_connection_start_message_processing (consumer);
1404
1405   timer = g_timer_new ();
1406   g_timer_start (timer);
1407
1408   while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
1409       g_main_context_iteration (NULL, FALSE);
1410
1411   g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
1412   g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
1413
1414   g_timer_destroy (timer);
1415   g_object_unref (consumer);
1416   g_object_unref (producer);
1417 }
1418 #else
1419 static void
1420 test_overflow (void)
1421 {
1422   /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
1423 }
1424 #endif
1425
1426 /* ---------------------------------------------------------------------------------------------------- */
1427
1428 int
1429 main (int   argc,
1430       char *argv[])
1431 {
1432   gint ret;
1433   GDBusNodeInfo *introspection_data = NULL;
1434
1435   g_type_init ();
1436   g_thread_init (NULL);
1437   g_test_init (&argc, &argv, NULL);
1438
1439   introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1440   g_assert (introspection_data != NULL);
1441   test_interface_introspection_data = introspection_data->interfaces[0];
1442
1443   test_guid = g_dbus_generate_guid ();
1444
1445   /* all the tests rely on a shared main loop */
1446   loop = g_main_loop_new (NULL, FALSE);
1447
1448   g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1449   g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1450   g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1451   g_test_add_func ("/gdbus/credentials", test_credentials);
1452   g_test_add_func ("/gdbus/overflow", test_overflow);
1453
1454   ret = g_test_run();
1455
1456   g_main_loop_unref (loop);
1457   g_free (test_guid);
1458   g_dbus_node_info_unref (introspection_data);
1459
1460   return ret;
1461 }