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