Don't use deprecated GThread API in gio tests
[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_new ("test_peer",
606                                  service_thread_func,
607                                  &data,
608                                  TRUE,
609                                  &error);
610   while (service_loop == NULL)
611     g_thread_yield ();
612   g_assert (server != NULL);
613
614   /* bring up a connection and accept it */
615   data.accept_connection = TRUE;
616   error = NULL;
617   c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
618                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
619                                               NULL, /* GDBusAuthObserver */
620                                               NULL, /* cancellable */
621                                               &error);
622   g_assert_no_error (error);
623   g_assert (c != NULL);
624   while (data.current_connections->len < 1)
625     g_main_loop_run (loop);
626   g_assert_cmpint (data.current_connections->len, ==, 1);
627   g_assert_cmpint (data.num_connection_attempts, ==, 1);
628   g_assert (g_dbus_connection_get_unique_name (c) == NULL);
629   g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
630
631   /* check that we create a proxy, read properties, receive signals and invoke
632    * the HelloPeer() method. Since the server runs in another thread it's fine
633    * to use synchronous blocking API here.
634    */
635   error = NULL;
636   proxy = g_dbus_proxy_new_sync (c,
637                                  G_DBUS_PROXY_FLAGS_NONE,
638                                  NULL,
639                                  NULL, /* bus_name */
640                                  "/org/gtk/GDBus/PeerTestObject",
641                                  "org.gtk.GDBus.PeerTestInterface",
642                                  NULL, /* GCancellable */
643                                  &error);
644   g_assert_no_error (error);
645   g_assert (proxy != NULL);
646   error = NULL;
647   value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
648   g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
649
650   /* try invoking a method */
651   error = NULL;
652   result = g_dbus_proxy_call_sync (proxy,
653                                    "HelloPeer",
654                                    g_variant_new ("(s)", "Hey Peer!"),
655                                    G_DBUS_CALL_FLAGS_NONE,
656                                    -1,
657                                    NULL,  /* GCancellable */
658                                    &error);
659   g_assert_no_error (error);
660   g_variant_get (result, "(&s)", &s);
661   g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
662   g_variant_unref (result);
663   g_assert_cmpint (data.num_method_calls, ==, 1);
664
665   /* make the other peer emit a signal - catch it */
666   signal_handler_id = g_signal_connect (proxy,
667                                         "g-signal",
668                                         G_CALLBACK (on_proxy_signal_received),
669                                         &data);
670   g_assert (!data.signal_received);
671   g_dbus_proxy_call (proxy,
672                      "EmitSignal",
673                      NULL,  /* no arguments */
674                      G_DBUS_CALL_FLAGS_NONE,
675                      -1,
676                      NULL,  /* GCancellable */
677                      NULL,  /* GAsyncReadyCallback - we don't care about the result */
678                      NULL); /* user_data */
679   g_main_loop_run (loop);
680   g_assert (data.signal_received);
681   g_assert_cmpint (data.num_method_calls, ==, 2);
682   g_signal_handler_disconnect (proxy, signal_handler_id);
683
684   /* Also ensure that messages with the sender header-field set gets
685    * delivered to the proxy - note that this doesn't really make sense
686    * e.g. names are meaning-less in a peer-to-peer case... but we
687    * support it because it makes sense in certain bridging
688    * applications - see e.g. #623815.
689    */
690   signal_handler_id = g_signal_connect (proxy,
691                                         "g-signal",
692                                         G_CALLBACK (on_proxy_signal_received_with_name_set),
693                                         &data);
694   data.signal_received = FALSE;
695   g_dbus_proxy_call (proxy,
696                      "EmitSignalWithNameSet",
697                      NULL,  /* no arguments */
698                      G_DBUS_CALL_FLAGS_NONE,
699                      -1,
700                      NULL,  /* GCancellable */
701                      NULL,  /* GAsyncReadyCallback - we don't care about the result */
702                      NULL); /* user_data */
703   g_main_loop_run (loop);
704   g_assert (data.signal_received);
705   g_assert_cmpint (data.num_method_calls, ==, 3);
706   g_signal_handler_disconnect (proxy, signal_handler_id);
707
708   /* check for UNIX fd passing */
709 #ifdef G_OS_UNIX
710   {
711     GDBusMessage *method_call_message;
712     GDBusMessage *method_reply_message;
713     GUnixFDList *fd_list;
714     gint fd;
715     gchar *buf;
716     gsize len;
717     gchar *buf2;
718     gsize len2;
719
720     method_call_message = g_dbus_message_new_method_call (NULL, /* name */
721                                                           "/org/gtk/GDBus/PeerTestObject",
722                                                           "org.gtk.GDBus.PeerTestInterface",
723                                                           "OpenFile");
724     g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", "/etc/hosts"));
725     error = NULL;
726     method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
727                                                                            method_call_message,
728                                                                            G_DBUS_SEND_MESSAGE_FLAGS_NONE,
729                                                                            -1,
730                                                                            NULL, /* out_serial */
731                                                                            NULL, /* cancellable */
732                                                                            &error);
733     g_assert_no_error (error);
734     g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
735     fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
736     g_assert (fd_list != NULL);
737     g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
738     error = NULL;
739     fd = g_unix_fd_list_get (fd_list, 0, &error);
740     g_assert_no_error (error);
741     g_object_unref (method_call_message);
742     g_object_unref (method_reply_message);
743
744     error = NULL;
745     len = 0;
746     buf = read_all_from_fd (fd, &len, &error);
747     g_assert_no_error (error);
748     g_assert (buf != NULL);
749     close (fd);
750
751     error = NULL;
752     g_file_get_contents ("/etc/hosts",
753                          &buf2,
754                          &len2,
755                          &error);
756     g_assert_no_error (error);
757     g_assert_cmpint (len, ==, len2);
758     g_assert (memcmp (buf, buf2, len) == 0);
759     g_free (buf2);
760     g_free (buf);
761   }
762 #else
763   error = NULL;
764   result = g_dbus_proxy_call_sync (proxy,
765                                    "OpenFile",
766                                    g_variant_new ("(s)", "boo"),
767                                    G_DBUS_CALL_FLAGS_NONE,
768                                    -1,
769                                    NULL,  /* GCancellable */
770                                    &error);
771   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
772   g_assert (result == NULL);
773   g_error_free (error);
774 #endif /* G_OS_UNIX */
775
776   /* Check that g_socket_get_credentials() work - this really should
777    * be in a GSocket-specific test suite but no such test suite exists
778    * right now.
779    */
780   {
781     GSocket *socket;
782     GCredentials *credentials;
783     socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (g_dbus_connection_get_stream (c)));
784     g_assert (G_IS_SOCKET (socket));
785     error = NULL;
786     credentials = g_socket_get_credentials (socket, &error);
787 #ifdef __linux__
788     {
789       struct ucred *native_creds;
790       g_assert_no_error (error);
791       g_assert (G_IS_CREDENTIALS (credentials));
792       native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED);
793       g_assert (native_creds != NULL);
794       g_assert (native_creds->uid == getuid ());
795       g_assert (native_creds->gid == getgid ());
796       g_assert (native_creds->pid == getpid ());
797     }
798     g_object_unref (credentials);
799 #elif defined (__OpenBSD__)
800     {
801       struct sockpeercred *native_creds;
802       g_assert_no_error (error);
803       g_assert (G_IS_CREDENTIALS (credentials));
804       native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED);
805       g_assert (native_creds != NULL);
806       g_assert (native_creds->uid == getuid ());
807       g_assert (native_creds->gid == getgid ());
808       g_assert (native_creds->pid == getpid ());
809     }
810     g_object_unref (credentials);
811 #else
812     g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
813     g_assert (credentials == NULL);
814 #endif
815   }
816
817
818   /* bring up a connection - don't accept it - this should fail
819    */
820   data.accept_connection = FALSE;
821   error = NULL;
822   c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
823                                                G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
824                                                NULL, /* GDBusAuthObserver */
825                                                NULL, /* cancellable */
826                                                &error);
827   _g_assert_error_domain (error, G_IO_ERROR);
828   g_error_free (error);
829   g_assert (c2 == NULL);
830
831 #if 0
832   /* TODO: THIS TEST DOESN'T WORK YET */
833
834   /* bring up a connection - accept it.. then disconnect from the client side - check
835    * that the server side gets the disconnect signal.
836    */
837   error = NULL;
838   data.accept_connection = TRUE;
839   c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
840                                                G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
841                                                NULL, /* GDBusAuthObserver */
842                                                NULL, /* cancellable */
843                                                &error);
844   g_assert_no_error (error);
845   g_assert (c2 != NULL);
846   g_assert (!g_dbus_connection_get_is_disconnected (c2));
847   while (data.num_connection_attempts < 3)
848     g_main_loop_run (loop);
849   g_assert_cmpint (data.current_connections->len, ==, 2);
850   g_assert_cmpint (data.num_connection_attempts, ==, 3);
851   g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
852   g_idle_add (on_do_disconnect_in_idle, c2);
853   g_debug ("==================================================");
854   g_debug ("==================================================");
855   g_debug ("==================================================");
856   g_debug ("waiting for disconnect on connection %p, stream %p",
857            data.current_connections->pdata[1],
858            g_dbus_connection_get_stream (data.current_connections->pdata[1]));
859
860   g_timeout_add (2000, check_connection, &data);
861   //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
862   g_main_loop_run (loop);
863   g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
864   g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
865 #endif
866
867   /* unref the server and stop listening for new connections
868    *
869    * This won't bring down the established connections - check that c is still connected
870    * by invoking a method
871    */
872   //g_socket_service_stop (service);
873   //g_object_unref (service);
874   g_dbus_server_stop (server);
875   g_object_unref (server);
876   server = NULL;
877
878   error = NULL;
879   result = g_dbus_proxy_call_sync (proxy,
880                                    "HelloPeer",
881                                    g_variant_new ("(s)", "Hey Again Peer!"),
882                                    G_DBUS_CALL_FLAGS_NONE,
883                                    -1,
884                                    NULL,  /* GCancellable */
885                                    &error);
886   g_assert_no_error (error);
887   g_variant_get (result, "(&s)", &s);
888   g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
889   g_variant_unref (result);
890   g_assert_cmpint (data.num_method_calls, ==, 5);
891
892 #if 0
893   /* TODO: THIS TEST DOESN'T WORK YET */
894
895   /* now disconnect from the server side - check that the client side gets the signal */
896   g_assert_cmpint (data.current_connections->len, ==, 1);
897   g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
898   g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
899   if (!g_dbus_connection_get_is_disconnected (c))
900     _g_assert_signal_received (c, "closed");
901   g_assert (g_dbus_connection_get_is_disconnected (c));
902 #endif
903
904   g_object_unref (c);
905   g_ptr_array_unref (data.current_connections);
906   g_object_unref (proxy);
907
908   g_main_loop_quit (service_loop);
909   g_thread_join (service_thread);
910 }
911
912 /* ---------------------------------------------------------------------------------------------------- */
913
914 typedef struct
915 {
916   GDBusServer *server;
917   GMainContext *context;
918   GMainLoop *loop;
919
920   GList *connections;
921 } DmpData;
922
923 static void
924 dmp_data_free (DmpData *data)
925 {
926   g_main_loop_unref (data->loop);
927   g_main_context_unref (data->context);
928   g_object_unref (data->server);
929   g_list_foreach (data->connections, (GFunc) g_object_unref, NULL);
930   g_list_free (data->connections);
931   g_free (data);
932 }
933
934 static void
935 dmp_on_method_call (GDBusConnection       *connection,
936                     const gchar           *sender,
937                     const gchar           *object_path,
938                     const gchar           *interface_name,
939                     const gchar           *method_name,
940                     GVariant              *parameters,
941                     GDBusMethodInvocation *invocation,
942                     gpointer               user_data)
943 {
944   //DmpData *data = user_data;
945   gint32 first;
946   gint32 second;
947   g_variant_get (parameters,
948                  "(ii)",
949                  &first,
950                  &second);
951   g_dbus_method_invocation_return_value (invocation,
952                                          g_variant_new ("(i)", first + second));
953 }
954
955 static const GDBusInterfaceVTable dmp_interface_vtable =
956 {
957   dmp_on_method_call,
958   NULL,  /* get_property */
959   NULL   /* set_property */
960 };
961
962
963 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
964 static gboolean
965 dmp_on_new_connection (GDBusServer     *server,
966                        GDBusConnection *connection,
967                        gpointer         user_data)
968 {
969   DmpData *data = user_data;
970   GDBusNodeInfo *node;
971   GError *error;
972
973   /* accept the connection */
974   data->connections = g_list_prepend (data->connections, g_object_ref (connection));
975
976   error = NULL;
977   node = g_dbus_node_info_new_for_xml ("<node>"
978                                        "  <interface name='org.gtk.GDBus.DmpInterface'>"
979                                        "    <method name='AddPair'>"
980                                        "      <arg type='i' name='first' direction='in'/>"
981                                        "      <arg type='i' name='second' direction='in'/>"
982                                        "      <arg type='i' name='sum' direction='out'/>"
983                                        "    </method>"
984                                        "  </interface>"
985                                        "</node>",
986                                        &error);
987   g_assert_no_error (error);
988
989   /* sleep 100ms before exporting an object - this is to test that
990    * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
991    * (GDBusServer uses this feature).
992    */
993   usleep (100 * 1000);
994
995   /* export an object */
996   error = NULL;
997   g_dbus_connection_register_object (connection,
998                                      "/dmp/test",
999                                      node->interfaces[0],
1000                                      &dmp_interface_vtable,
1001                                      data,
1002                                      NULL,
1003                                      &error);
1004   g_dbus_node_info_unref (node);
1005
1006   return TRUE;
1007 }
1008
1009 static gpointer
1010 dmp_thread_func (gpointer user_data)
1011 {
1012   DmpData *data = user_data;
1013   GError *error;
1014   gchar *guid;
1015
1016   data->context = g_main_context_new ();
1017   g_main_context_push_thread_default (data->context);
1018
1019   error = NULL;
1020   guid = g_dbus_generate_guid ();
1021   data->server = g_dbus_server_new_sync (tmp_address,
1022                                          G_DBUS_SERVER_FLAGS_NONE,
1023                                          guid,
1024                                          NULL, /* GDBusAuthObserver */
1025                                          NULL, /* GCancellable */
1026                                          &error);
1027   g_assert_no_error (error);
1028   g_signal_connect (data->server,
1029                     "new-connection",
1030                     G_CALLBACK (dmp_on_new_connection),
1031                     data);
1032
1033   g_dbus_server_start (data->server);
1034
1035   data->loop = g_main_loop_new (data->context, FALSE);
1036   g_main_loop_run (data->loop);
1037
1038   g_main_context_pop_thread_default (data->context);
1039
1040   g_free (guid);
1041   return NULL;
1042 }
1043
1044 static void
1045 delayed_message_processing (void)
1046 {
1047   GError *error;
1048   DmpData *data;
1049   GThread *service_thread;
1050   guint n;
1051
1052   data = g_new0 (DmpData, 1);
1053
1054   error = NULL;
1055   service_thread = g_thread_new ("dmp",
1056                                  dmp_thread_func,
1057                                  data,
1058                                  TRUE,
1059                                  &error);
1060   while (data->server == NULL || !g_dbus_server_is_active (data->server))
1061     g_thread_yield ();
1062
1063   for (n = 0; n < 5; n++)
1064     {
1065       GDBusConnection *c;
1066       GVariant *res;
1067       gint32 val;
1068
1069       error = NULL;
1070       c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
1071                                                   G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1072                                                   NULL, /* GDBusAuthObserver */
1073                                                   NULL, /* GCancellable */
1074                                                   &error);
1075       g_assert_no_error (error);
1076
1077       error = NULL;
1078       res = g_dbus_connection_call_sync (c,
1079                                          NULL,    /* bus name */
1080                                          "/dmp/test",
1081                                          "org.gtk.GDBus.DmpInterface",
1082                                          "AddPair",
1083                                          g_variant_new ("(ii)", 2, n),
1084                                          G_VARIANT_TYPE ("(i)"),
1085                                          G_DBUS_CALL_FLAGS_NONE,
1086                                          -1, /* timeout_msec */
1087                                          NULL, /* GCancellable */
1088                                          &error);
1089       g_assert_no_error (error);
1090       g_variant_get (res, "(i)", &val);
1091       g_assert_cmpint (val, ==, 2 + n);
1092       g_variant_unref (res);
1093       g_object_unref (c);
1094   }
1095
1096   g_main_loop_quit (data->loop);
1097   g_thread_join (service_thread);
1098   dmp_data_free (data);
1099 }
1100
1101 /* ---------------------------------------------------------------------------------------------------- */
1102
1103 static gboolean
1104 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1105                                            GIOStream         *stream,
1106                                            GCredentials      *credentials,
1107                                            gpointer           user_data)
1108 {
1109   PeerData *data = user_data;
1110   gboolean authorized;
1111
1112   data->num_connection_attempts++;
1113
1114   authorized = TRUE;
1115   if (!data->accept_connection)
1116     {
1117       authorized = FALSE;
1118       g_main_loop_quit (loop);
1119     }
1120
1121   return authorized;
1122 }
1123
1124 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1125 static gboolean
1126 nonce_tcp_on_new_connection (GDBusServer *server,
1127                              GDBusConnection *connection,
1128                              gpointer user_data)
1129 {
1130   PeerData *data = user_data;
1131
1132   g_ptr_array_add (data->current_connections, g_object_ref (connection));
1133
1134   g_main_loop_quit (loop);
1135
1136   return TRUE;
1137 }
1138
1139 static gpointer
1140 nonce_tcp_service_thread_func (gpointer user_data)
1141 {
1142   PeerData *data = user_data;
1143   GMainContext *service_context;
1144   GDBusAuthObserver *observer;
1145   GError *error;
1146
1147   service_context = g_main_context_new ();
1148   g_main_context_push_thread_default (service_context);
1149
1150   error = NULL;
1151   observer = g_dbus_auth_observer_new ();
1152   server = g_dbus_server_new_sync ("nonce-tcp:",
1153                                    G_DBUS_SERVER_FLAGS_NONE,
1154                                    test_guid,
1155                                    observer,
1156                                    NULL, /* cancellable */
1157                                    &error);
1158   g_assert_no_error (error);
1159
1160   g_signal_connect (server,
1161                     "new-connection",
1162                     G_CALLBACK (nonce_tcp_on_new_connection),
1163                     data);
1164   g_signal_connect (observer,
1165                     "authorize-authenticated-peer",
1166                     G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1167                     data);
1168   g_object_unref (observer);
1169
1170   g_dbus_server_start (server);
1171
1172   service_loop = g_main_loop_new (service_context, FALSE);
1173   g_main_loop_run (service_loop);
1174
1175   g_main_context_pop_thread_default (service_context);
1176
1177   g_main_loop_unref (service_loop);
1178   g_main_context_unref (service_context);
1179
1180   /* test code specifically unrefs the server - see below */
1181   g_assert (server == NULL);
1182
1183   return NULL;
1184 }
1185
1186 static void
1187 test_nonce_tcp (void)
1188 {
1189   PeerData data;
1190   GError *error;
1191   GThread *service_thread;
1192   GDBusConnection *c;
1193   gchar *s;
1194   gchar *nonce_file;
1195   gboolean res;
1196   const gchar *address;
1197
1198   memset (&data, '\0', sizeof (PeerData));
1199   data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1200
1201   error = NULL;
1202   server = NULL;
1203   service_loop = NULL;
1204   service_thread = g_thread_new ("nonce-tcp-service",
1205                                  nonce_tcp_service_thread_func,
1206                                  &data,
1207                                  TRUE,
1208                                  &error);
1209   while (service_loop == NULL)
1210     g_thread_yield ();
1211   g_assert (server != NULL);
1212
1213
1214   /* bring up a connection and accept it */
1215   data.accept_connection = TRUE;
1216   error = NULL;
1217   c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1218                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1219                                               NULL, /* GDBusAuthObserver */
1220                                               NULL, /* cancellable */
1221                                               &error);
1222   g_assert_no_error (error);
1223   g_assert (c != NULL);
1224   while (data.current_connections->len < 1)
1225     g_thread_yield ();
1226   g_assert_cmpint (data.current_connections->len, ==, 1);
1227   g_assert_cmpint (data.num_connection_attempts, ==, 1);
1228   g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1229   g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1230   g_object_unref (c);
1231
1232   /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1233    */
1234
1235   address = g_dbus_server_get_client_address (server);
1236
1237   s = strstr (address, "noncefile=");
1238   g_assert (s != NULL);
1239   s += sizeof "noncefile=" - 1;
1240   nonce_file = g_strdup (s);
1241
1242   /* First try invalid data in the nonce file - this will actually
1243    * make the client send this and the server will reject it. The way
1244    * it works is that if the nonce doesn't match, the server will
1245    * simply close the connection. So, from the client point of view,
1246    * we can see a variety of errors.
1247    */
1248   error = NULL;
1249   res = g_file_set_contents (nonce_file,
1250                              "0123456789012345",
1251                              -1,
1252                              &error);
1253   g_assert_no_error (error);
1254   g_assert (res);
1255   c = g_dbus_connection_new_for_address_sync (address,
1256                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1257                                               NULL, /* GDBusAuthObserver */
1258                                               NULL, /* cancellable */
1259                                               &error);
1260   _g_assert_error_domain (error, G_IO_ERROR);
1261   g_error_free (error);
1262   g_assert (c == NULL);
1263
1264   /* Then try with a nonce-file of incorrect length - this will make
1265    * the client complain - we won't even try connecting to the server
1266    * for this
1267    */
1268   error = NULL;
1269   res = g_file_set_contents (nonce_file,
1270                              "0123456789012345_",
1271                              -1,
1272                              &error);
1273   g_assert_no_error (error);
1274   g_assert (res);
1275   c = g_dbus_connection_new_for_address_sync (address,
1276                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1277                                               NULL, /* GDBusAuthObserver */
1278                                               NULL, /* cancellable */
1279                                               &error);
1280   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1281   g_error_free (error);
1282   g_assert (c == NULL);
1283
1284   /* Finally try with no nonce-file at all */
1285   g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1286   error = NULL;
1287   c = g_dbus_connection_new_for_address_sync (address,
1288                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1289                                               NULL, /* GDBusAuthObserver */
1290                                               NULL, /* cancellable */
1291                                               &error);
1292   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1293   g_error_free (error);
1294   g_assert (c == NULL);
1295
1296   g_free (nonce_file);
1297
1298   g_dbus_server_stop (server);
1299   g_object_unref (server);
1300   server = NULL;
1301
1302   g_main_loop_quit (service_loop);
1303   g_thread_join (service_thread);
1304 }
1305
1306 static void
1307 test_credentials (void)
1308 {
1309   GCredentials *c1, *c2;
1310   GError *error;
1311   gchar *desc;
1312
1313   c1 = g_credentials_new ();
1314   c2 = g_credentials_new ();
1315
1316   error = NULL;
1317   if (g_credentials_set_unix_user (c2, getuid (), &error))
1318     g_assert_no_error (error);
1319
1320   g_clear_error (&error);
1321   g_assert (g_credentials_is_same_user (c1, c2, &error));
1322   g_assert_no_error (error);
1323
1324   desc = g_credentials_to_string (c1);
1325   g_assert (desc != NULL);
1326   g_free (desc);
1327
1328   g_object_unref (c1);
1329   g_object_unref (c2);
1330 }
1331
1332 /* ---------------------------------------------------------------------------------------------------- */
1333
1334 #ifdef G_OS_UNIX
1335
1336 /* Chosen to be big enough to overflow the socket buffer */
1337 #define OVERFLOW_NUM_SIGNALS 5000
1338 #define OVERFLOW_TIMEOUT_SEC 10
1339
1340 static GDBusMessage *
1341 overflow_filter_func (GDBusConnection *connection,
1342                       GDBusMessage    *message,
1343                       gboolean         incoming,
1344                       gpointer         user_data)
1345 {
1346   volatile gint *counter = user_data;
1347   *counter += 1;
1348   return message;
1349 }
1350
1351 static gboolean
1352 overflow_on_500ms_later_func (gpointer user_data)
1353 {
1354   g_main_loop_quit (loop);
1355   return FALSE; /* don't keep the idle */
1356 }
1357
1358 static void
1359 test_overflow (void)
1360 {
1361   gint sv[2];
1362   gint n;
1363   GSocket *socket;
1364   GSocketConnection *socket_connection;
1365   GDBusConnection *producer, *consumer;
1366   GError *error;
1367   GTimer *timer;
1368   volatile gint n_messages_received;
1369   volatile gint n_messages_sent;
1370
1371   g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
1372
1373   error = NULL;
1374   socket = g_socket_new_from_fd (sv[0], &error);
1375   g_assert_no_error (error);
1376   socket_connection = g_socket_connection_factory_create_connection (socket);
1377   g_assert (socket_connection != NULL);
1378   g_object_unref (socket);
1379   producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1380                                          NULL, /* guid */
1381                                          G_DBUS_CONNECTION_FLAGS_NONE,
1382                                          NULL, /* GDBusAuthObserver */
1383                                          NULL, /* GCancellable */
1384                                          &error);
1385   g_dbus_connection_set_exit_on_close (producer, TRUE);
1386   g_assert_no_error (error);
1387   g_object_unref (socket_connection);
1388   n_messages_sent = 0;
1389   g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
1390
1391   /* send enough data that we get an EAGAIN */
1392   for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
1393     {
1394       error = NULL;
1395       g_dbus_connection_emit_signal (producer,
1396                                      NULL, /* destination */
1397                                      "/org/foo/Object",
1398                                      "org.foo.Interface",
1399                                      "Member",
1400                                      g_variant_new ("(s)", "a string"),
1401                                      &error);
1402       g_assert_no_error (error);
1403     }
1404
1405   /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
1406    * kernel buffers) and verify that n_messages_sent <
1407    * OVERFLOW_NUM_SIGNALS
1408    *
1409    * This is to verify that not all the submitted messages have been
1410    * sent to the underlying transport.
1411    */
1412   g_timeout_add (500, overflow_on_500ms_later_func, NULL);
1413   g_main_loop_run (loop);
1414   g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
1415
1416   /* now suck it all out as a client, and add it up */
1417   socket = g_socket_new_from_fd (sv[1], &error);
1418   g_assert_no_error (error);
1419   socket_connection = g_socket_connection_factory_create_connection (socket);
1420   g_assert (socket_connection != NULL);
1421   g_object_unref (socket);
1422   consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1423                                          NULL, /* guid */
1424                                          G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
1425                                          NULL, /* GDBusAuthObserver */
1426                                          NULL, /* GCancellable */
1427                                          &error);
1428   g_assert_no_error (error);
1429   g_object_unref (socket_connection);
1430   n_messages_received = 0;
1431   g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
1432   g_dbus_connection_start_message_processing (consumer);
1433
1434   timer = g_timer_new ();
1435   g_timer_start (timer);
1436
1437   while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
1438       g_main_context_iteration (NULL, FALSE);
1439
1440   g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
1441   g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
1442
1443   g_timer_destroy (timer);
1444   g_object_unref (consumer);
1445   g_object_unref (producer);
1446 }
1447 #else
1448 static void
1449 test_overflow (void)
1450 {
1451   /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
1452 }
1453 #endif
1454
1455 /* ---------------------------------------------------------------------------------------------------- */
1456
1457 static gboolean
1458 tcp_anonymous_on_new_connection (GDBusServer     *server,
1459                                  GDBusConnection *connection,
1460                                  gpointer         user_data)
1461 {
1462   gboolean *seen_connection = user_data;
1463   *seen_connection = TRUE;
1464   return TRUE;
1465 }
1466
1467 static gpointer
1468 tcp_anonymous_service_thread_func (gpointer user_data)
1469 {
1470   gboolean *seen_connection = user_data;
1471   GMainContext *service_context;
1472   GError *error;
1473
1474   service_context = g_main_context_new ();
1475   g_main_context_push_thread_default (service_context);
1476
1477   error = NULL;
1478   server = g_dbus_server_new_sync ("tcp:",
1479                                    G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS,
1480                                    test_guid,
1481                                    NULL, /* GDBusObserver* */
1482                                    NULL, /* GCancellable* */
1483                                    &error);
1484   g_assert_no_error (error);
1485
1486   g_signal_connect (server,
1487                     "new-connection",
1488                     G_CALLBACK (tcp_anonymous_on_new_connection),
1489                     seen_connection);
1490
1491   g_dbus_server_start (server);
1492
1493   service_loop = g_main_loop_new (service_context, FALSE);
1494   g_main_loop_run (service_loop);
1495
1496   g_main_context_pop_thread_default (service_context);
1497
1498   g_main_loop_unref (service_loop);
1499   g_main_context_unref (service_context);
1500
1501   return NULL;
1502 }
1503
1504 static void
1505 test_tcp_anonymous (void)
1506 {
1507   gboolean seen_connection;
1508   GThread *service_thread;
1509   GDBusConnection *connection;
1510   GError *error;
1511
1512   seen_connection = FALSE;
1513   service_loop = NULL;
1514   service_thread = g_thread_new ("tcp-anon-service",
1515                                  tcp_anonymous_service_thread_func,
1516                                  &seen_connection, /* user_data */
1517                                  TRUE, /* joinable */
1518                                  &error);
1519   while (service_loop == NULL)
1520     g_thread_yield ();
1521   g_assert (server != NULL);
1522
1523   error = NULL;
1524   connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1525                                                        G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1526                                                        NULL, /* GDBusAuthObserver* */
1527                                                        NULL, /* GCancellable */
1528                                                        &error);
1529   g_assert_no_error (error);
1530   g_assert (connection != NULL);
1531
1532   while (!seen_connection)
1533     g_thread_yield ();
1534
1535   g_object_unref (connection);
1536
1537   g_main_loop_quit (service_loop);
1538   g_dbus_server_stop (server);
1539   g_object_unref (server);
1540   server = NULL;
1541
1542   g_thread_join (service_thread);
1543 }
1544
1545 /* ---------------------------------------------------------------------------------------------------- */
1546
1547 int
1548 main (int   argc,
1549       char *argv[])
1550 {
1551   gint ret;
1552   GDBusNodeInfo *introspection_data = NULL;
1553   gchar *tmpdir = NULL;
1554
1555   g_type_init ();
1556   g_test_init (&argc, &argv, NULL);
1557
1558   g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
1559
1560   introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1561   g_assert (introspection_data != NULL);
1562   test_interface_introspection_data = introspection_data->interfaces[0];
1563
1564   test_guid = g_dbus_generate_guid ();
1565
1566   if (is_unix)
1567     {
1568       if (g_unix_socket_address_abstract_names_supported ())
1569         tmp_address = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
1570       else
1571         {
1572           tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
1573           tmp_address = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
1574         }
1575     }
1576   else
1577     tmp_address = g_strdup ("nonce-tcp:");
1578
1579   /* all the tests rely on a shared main loop */
1580   loop = g_main_loop_new (NULL, FALSE);
1581
1582   g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1583   g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1584   g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1585   g_test_add_func ("/gdbus/tcp-anonymous", test_tcp_anonymous);
1586   g_test_add_func ("/gdbus/credentials", test_credentials);
1587   g_test_add_func ("/gdbus/overflow", test_overflow);
1588
1589   ret = g_test_run();
1590
1591   g_main_loop_unref (loop);
1592   g_free (test_guid);
1593   g_dbus_node_info_unref (introspection_data);
1594   if (is_unix)
1595     g_free (tmp_address);
1596   if (tmpdir)
1597     {
1598       g_rmdir (tmpdir);
1599       g_free (tmpdir);
1600     }
1601
1602   return ret;
1603 }