Initialize service_loop before running the service thread
[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 #include "gdbus-example-objectmanager-generated.h"
51
52 #ifdef G_OS_UNIX
53 static gboolean is_unix = TRUE;
54 #else
55 static gboolean is_unix = FALSE;
56 #endif
57
58 static gchar *tmp_address = NULL;
59 static gchar *test_guid = NULL;
60 static GMainLoop *service_loop = NULL;
61 static GDBusServer *server = NULL;
62 static GMainLoop *loop = NULL;
63
64 /* ---------------------------------------------------------------------------------------------------- */
65 /* Test that peer-to-peer connections work */
66 /* ---------------------------------------------------------------------------------------------------- */
67
68
69 typedef struct
70 {
71   gboolean accept_connection;
72   gint num_connection_attempts;
73   GPtrArray *current_connections;
74   guint num_method_calls;
75   gboolean signal_received;
76 } PeerData;
77
78 static const gchar *test_interface_introspection_xml =
79   "<node>"
80   "  <interface name='org.gtk.GDBus.PeerTestInterface'>"
81   "    <method name='HelloPeer'>"
82   "      <arg type='s' name='greeting' direction='in'/>"
83   "      <arg type='s' name='response' direction='out'/>"
84   "    </method>"
85   "    <method name='EmitSignal'/>"
86   "    <method name='EmitSignalWithNameSet'/>"
87   "    <method name='OpenFile'>"
88   "      <arg type='s' name='path' direction='in'/>"
89   "    </method>"
90   "    <signal name='PeerSignal'>"
91   "      <arg type='s' name='a_string'/>"
92   "    </signal>"
93   "    <property type='s' name='PeerProperty' access='read'/>"
94   "  </interface>"
95   "</node>";
96 static GDBusInterfaceInfo *test_interface_introspection_data = NULL;
97
98 static void
99 test_interface_method_call (GDBusConnection       *connection,
100                             const gchar           *sender,
101                             const gchar           *object_path,
102                             const gchar           *interface_name,
103                             const gchar           *method_name,
104                             GVariant              *parameters,
105                             GDBusMethodInvocation *invocation,
106                             gpointer               user_data)
107 {
108   PeerData *data = user_data;
109   const GDBusMethodInfo *info;
110
111   data->num_method_calls++;
112
113   g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
114   g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
115
116   info = g_dbus_method_invocation_get_method_info (invocation);
117   g_assert_cmpstr (info->name, ==, method_name);
118
119   if (g_strcmp0 (method_name, "HelloPeer") == 0)
120     {
121       const gchar *greeting;
122       gchar *response;
123
124       g_variant_get (parameters, "(&s)", &greeting);
125
126       response = g_strdup_printf ("You greeted me with '%s'.",
127                                   greeting);
128       g_dbus_method_invocation_return_value (invocation,
129                                              g_variant_new ("(s)", response));
130       g_free (response);
131     }
132   else if (g_strcmp0 (method_name, "EmitSignal") == 0)
133     {
134       GError *error;
135
136       error = NULL;
137       g_dbus_connection_emit_signal (connection,
138                                      NULL,
139                                      "/org/gtk/GDBus/PeerTestObject",
140                                      "org.gtk.GDBus.PeerTestInterface",
141                                      "PeerSignal",
142                                      NULL,
143                                      &error);
144       g_assert_no_error (error);
145       g_dbus_method_invocation_return_value (invocation, NULL);
146     }
147   else if (g_strcmp0 (method_name, "EmitSignalWithNameSet") == 0)
148     {
149       GError *error;
150       gboolean ret;
151       GDBusMessage *message;
152
153       message = g_dbus_message_new_signal ("/org/gtk/GDBus/PeerTestObject",
154                                            "org.gtk.GDBus.PeerTestInterface",
155                                            "PeerSignalWithNameSet");
156       g_dbus_message_set_sender (message, ":1.42");
157
158       error = NULL;
159       ret = g_dbus_connection_send_message (connection, message, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &error);
160       g_assert_no_error (error);
161       g_assert (ret);
162       g_object_unref (message);
163
164       g_dbus_method_invocation_return_value (invocation, NULL);
165     }
166   else if (g_strcmp0 (method_name, "OpenFile") == 0)
167     {
168 #ifdef G_OS_UNIX
169       const gchar *path;
170       GDBusMessage *reply;
171       GError *error;
172       gint fd;
173       GUnixFDList *fd_list;
174
175       g_variant_get (parameters, "(&s)", &path);
176
177       fd_list = g_unix_fd_list_new ();
178
179       error = NULL;
180
181       fd = open (path, O_RDONLY);
182       g_unix_fd_list_append (fd_list, fd, &error);
183       g_assert_no_error (error);
184       close (fd);
185
186       reply = g_dbus_message_new_method_reply (g_dbus_method_invocation_get_message (invocation));
187       g_dbus_message_set_unix_fd_list (reply, fd_list);
188       g_object_unref (fd_list);
189       g_object_unref (invocation);
190
191       error = NULL;
192       g_dbus_connection_send_message (connection,
193                                       reply,
194                                       G_DBUS_SEND_MESSAGE_FLAGS_NONE,
195                                       NULL, /* out_serial */
196                                       &error);
197       g_assert_no_error (error);
198       g_object_unref (reply);
199 #else
200       g_dbus_method_invocation_return_dbus_error (invocation,
201                                                   "org.gtk.GDBus.NotOnUnix",
202                                                   "Your OS does not support file descriptor passing");
203 #endif
204     }
205   else
206     {
207       g_assert_not_reached ();
208     }
209 }
210
211 static GVariant *
212 test_interface_get_property (GDBusConnection  *connection,
213                              const gchar      *sender,
214                              const gchar      *object_path,
215                              const gchar      *interface_name,
216                              const gchar      *property_name,
217                              GError          **error,
218                              gpointer          user_data)
219 {
220   g_assert_cmpstr (object_path, ==, "/org/gtk/GDBus/PeerTestObject");
221   g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.PeerTestInterface");
222   g_assert_cmpstr (property_name, ==, "PeerProperty");
223
224   return g_variant_new_string ("ThePropertyValue");
225 }
226
227
228 static const GDBusInterfaceVTable test_interface_vtable =
229 {
230   test_interface_method_call,
231   test_interface_get_property,
232   NULL  /* set_property */
233 };
234
235 static void
236 on_proxy_signal_received (GDBusProxy *proxy,
237                           gchar      *sender_name,
238                           gchar      *signal_name,
239                           GVariant   *parameters,
240                           gpointer    user_data)
241 {
242   PeerData *data = user_data;
243
244   data->signal_received = TRUE;
245
246   g_assert (sender_name == NULL);
247   g_assert_cmpstr (signal_name, ==, "PeerSignal");
248   g_main_loop_quit (loop);
249 }
250
251 static void
252 on_proxy_signal_received_with_name_set (GDBusProxy *proxy,
253                                         gchar      *sender_name,
254                                         gchar      *signal_name,
255                                         GVariant   *parameters,
256                                         gpointer    user_data)
257 {
258   PeerData *data = user_data;
259
260   data->signal_received = TRUE;
261
262   g_assert_cmpstr (sender_name, ==, ":1.42");
263   g_assert_cmpstr (signal_name, ==, "PeerSignalWithNameSet");
264   g_main_loop_quit (loop);
265 }
266
267 /* ---------------------------------------------------------------------------------------------------- */
268
269 static gboolean
270 on_authorize_authenticated_peer (GDBusAuthObserver *observer,
271                                  GIOStream         *stream,
272                                  GCredentials      *credentials,
273                                  gpointer           user_data)
274 {
275   PeerData *data = user_data;
276   gboolean authorized;
277
278   data->num_connection_attempts++;
279
280   authorized = TRUE;
281   if (!data->accept_connection)
282     {
283       authorized = FALSE;
284       g_main_loop_quit (loop);
285     }
286
287   return authorized;
288 }
289
290 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
291 static gboolean
292 on_new_connection (GDBusServer *server,
293                    GDBusConnection *connection,
294                    gpointer user_data)
295 {
296   PeerData *data = user_data;
297   GError *error;
298   guint reg_id;
299
300   //g_print ("Client connected.\n"
301   //         "Negotiated capabilities: unix-fd-passing=%d\n",
302   //         g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING);
303
304   g_ptr_array_add (data->current_connections, g_object_ref (connection));
305
306   /* export object on the newly established connection */
307   error = NULL;
308   reg_id = g_dbus_connection_register_object (connection,
309                                               "/org/gtk/GDBus/PeerTestObject",
310                                               test_interface_introspection_data,
311                                               &test_interface_vtable,
312                                               data,
313                                               NULL, /* GDestroyNotify for data */
314                                               &error);
315   g_assert_no_error (error);
316   g_assert (reg_id > 0);
317
318   g_main_loop_quit (loop);
319
320   return TRUE;
321 }
322
323 static gpointer
324 service_thread_func (gpointer user_data)
325 {
326   PeerData *data = user_data;
327   GMainContext *service_context;
328   GDBusAuthObserver *observer;
329   GError *error;
330
331   service_context = g_main_context_new ();
332   g_main_context_push_thread_default (service_context);
333
334   error = NULL;
335   observer = g_dbus_auth_observer_new ();
336   server = g_dbus_server_new_sync (tmp_address,
337                                    G_DBUS_SERVER_FLAGS_NONE,
338                                    test_guid,
339                                    observer,
340                                    NULL, /* cancellable */
341                                    &error);
342   g_assert_no_error (error);
343
344   g_signal_connect (server,
345                     "new-connection",
346                     G_CALLBACK (on_new_connection),
347                     data);
348   g_signal_connect (observer,
349                     "authorize-authenticated-peer",
350                     G_CALLBACK (on_authorize_authenticated_peer),
351                     data);
352   g_object_unref (observer);
353
354   g_dbus_server_start (server);
355
356   service_loop = g_main_loop_new (service_context, FALSE);
357   g_main_loop_run (service_loop);
358
359   g_main_context_pop_thread_default (service_context);
360
361   g_main_loop_unref (service_loop);
362   g_main_context_unref (service_context);
363
364   /* test code specifically unrefs the server - see below */
365   g_assert (server == NULL);
366
367   return NULL;
368 }
369
370 #if 0
371 static gboolean
372 on_incoming_connection (GSocketService     *service,
373                         GSocketConnection  *socket_connection,
374                         GObject            *source_object,
375                         gpointer           user_data)
376 {
377   PeerData *data = user_data;
378
379   if (data->accept_connection)
380     {
381       GError *error;
382       guint reg_id;
383       GDBusConnection *connection;
384
385       error = NULL;
386       connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
387                                                test_guid,
388                                                G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
389                                                NULL, /* cancellable */
390                                                &error);
391       g_assert_no_error (error);
392
393       g_ptr_array_add (data->current_connections, connection);
394
395       /* export object on the newly established connection */
396       error = NULL;
397       reg_id = g_dbus_connection_register_object (connection,
398                                                   "/org/gtk/GDBus/PeerTestObject",
399                                                   &test_interface_introspection_data,
400                                                   &test_interface_vtable,
401                                                   data,
402                                                   NULL, /* GDestroyNotify for data */
403                                                   &error);
404       g_assert_no_error (error);
405       g_assert (reg_id > 0);
406
407     }
408   else
409     {
410       /* don't do anything */
411     }
412
413   data->num_connection_attempts++;
414
415   g_main_loop_quit (loop);
416
417   /* stops other signal handlers from being invoked */
418   return TRUE;
419 }
420
421 static gpointer
422 service_thread_func (gpointer data)
423 {
424   GMainContext *service_context;
425   gchar *socket_path;
426   GSocketAddress *address;
427   GError *error;
428
429   service_context = g_main_context_new ();
430   g_main_context_push_thread_default (service_context);
431
432   socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
433   address = g_unix_socket_address_new (socket_path);
434
435   service = g_socket_service_new ();
436   error = NULL;
437   g_socket_listener_add_address (G_SOCKET_LISTENER (service),
438                                  address,
439                                  G_SOCKET_TYPE_STREAM,
440                                  G_SOCKET_PROTOCOL_DEFAULT,
441                                  NULL, /* source_object */
442                                  NULL, /* effective_address */
443                                  &error);
444   g_assert_no_error (error);
445   g_signal_connect (service,
446                     "incoming",
447                     G_CALLBACK (on_incoming_connection),
448                     data);
449   g_socket_service_start (service);
450
451   service_loop = g_main_loop_new (service_context, FALSE);
452   g_main_loop_run (service_loop);
453
454   g_main_context_pop_thread_default (service_context);
455
456   g_main_loop_unref (service_loop);
457   g_main_context_unref (service_context);
458
459   g_object_unref (address);
460   g_free (socket_path);
461   return NULL;
462 }
463 #endif
464
465 /* ---------------------------------------------------------------------------------------------------- */
466
467 #if 0
468 static gboolean
469 check_connection (gpointer user_data)
470 {
471   PeerData *data = user_data;
472   guint n;
473
474   for (n = 0; n < data->current_connections->len; n++)
475     {
476       GDBusConnection *c;
477       GIOStream *stream;
478
479       c = G_DBUS_CONNECTION (data->current_connections->pdata[n]);
480       stream = g_dbus_connection_get_stream (c);
481
482       g_debug ("In check_connection for %d: connection %p, stream %p", n, c, stream);
483       g_debug ("closed = %d", g_io_stream_is_closed (stream));
484
485       GSocket *socket;
486       socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
487       g_debug ("socket_closed = %d", g_socket_is_closed (socket));
488       g_debug ("socket_condition_check = %d", g_socket_condition_check (socket, G_IO_IN|G_IO_OUT|G_IO_ERR|G_IO_HUP));
489
490       gchar buf[128];
491       GError *error;
492       gssize num_read;
493       error = NULL;
494       num_read = g_input_stream_read (g_io_stream_get_input_stream (stream),
495                                       buf,
496                                       128,
497                                       NULL,
498                                       &error);
499       if (num_read < 0)
500         {
501           g_debug ("error: %s", error->message);
502           g_error_free (error);
503         }
504       else
505         {
506           g_debug ("no error, read %d bytes", (gint) num_read);
507         }
508     }
509
510   return FALSE;
511 }
512
513 static gboolean
514 on_do_disconnect_in_idle (gpointer data)
515 {
516   GDBusConnection *c = G_DBUS_CONNECTION (data);
517   g_debug ("GDC %p has ref_count %d", c, G_OBJECT (c)->ref_count);
518   g_dbus_connection_disconnect (c);
519   g_object_unref (c);
520   return FALSE;
521 }
522 #endif
523
524 #ifdef G_OS_UNIX
525 static gchar *
526 read_all_from_fd (gint fd, gsize *out_len, GError **error)
527 {
528   GString *str;
529   gchar buf[64];
530   gssize num_read;
531
532   str = g_string_new (NULL);
533
534   do
535     {
536       num_read = read (fd, buf, sizeof (buf));
537       if (num_read == -1)
538         {
539           if (errno == EAGAIN || errno == EWOULDBLOCK)
540             continue;
541           g_set_error (error,
542                        G_IO_ERROR,
543                        g_io_error_from_errno (errno),
544                        "Failed reading %d bytes into offset %d: %s",
545                        (gint) sizeof (buf),
546                        (gint) str->len,
547                        strerror (errno));
548           goto error;
549         }
550       else if (num_read > 0)
551         {
552           g_string_append_len (str, buf, num_read);
553         }
554       else if (num_read == 0)
555         {
556           break;
557         }
558     }
559   while (TRUE);
560
561   if (out_len != NULL)
562     *out_len = str->len;
563   return g_string_free (str, FALSE);
564
565  error:
566   if (out_len != NULL)
567     out_len = 0;
568   g_string_free (str, TRUE);
569   return NULL;
570 }
571 #endif
572
573 static void
574 test_peer (void)
575 {
576   GDBusConnection *c;
577   GDBusConnection *c2;
578   GDBusProxy *proxy;
579   GError *error;
580   PeerData data;
581   GVariant *value;
582   GVariant *result;
583   const gchar *s;
584   GThread *service_thread;
585   gulong signal_handler_id;
586
587   memset (&data, '\0', sizeof (PeerData));
588   data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
589
590   /* first try to connect when there is no server */
591   error = NULL;
592   c = g_dbus_connection_new_for_address_sync (is_unix ? "unix:path=/tmp/gdbus-test-does-not-exist-pid" :
593                                               /* NOTE: Even if something is listening on port 12345 the connection
594                                                * will fail because the nonce file doesn't exist */
595                                               "nonce-tcp:host=localhost,port=12345,noncefile=this-does-not-exist-gdbus",
596                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
597                                               NULL, /* GDBusAuthObserver */
598                                               NULL, /* cancellable */
599                                               &error);
600   _g_assert_error_domain (error, G_IO_ERROR);
601   g_assert (!g_dbus_error_is_remote_error (error));
602   g_clear_error (&error);
603   g_assert (c == NULL);
604
605   /* bring up a server - we run the server in a different thread to avoid deadlocks */
606   service_loop = NULL;
607   service_thread = g_thread_new ("test_peer",
608                                  service_thread_func,
609                                  &data);
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   service_thread = g_thread_new ("dmp",
1055                                  dmp_thread_func,
1056                                  data);
1057   while (data->server == NULL || !g_dbus_server_is_active (data->server))
1058     g_thread_yield ();
1059
1060   for (n = 0; n < 5; n++)
1061     {
1062       GDBusConnection *c;
1063       GVariant *res;
1064       gint32 val;
1065
1066       error = NULL;
1067       c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
1068                                                   G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1069                                                   NULL, /* GDBusAuthObserver */
1070                                                   NULL, /* GCancellable */
1071                                                   &error);
1072       g_assert_no_error (error);
1073
1074       error = NULL;
1075       res = g_dbus_connection_call_sync (c,
1076                                          NULL,    /* bus name */
1077                                          "/dmp/test",
1078                                          "org.gtk.GDBus.DmpInterface",
1079                                          "AddPair",
1080                                          g_variant_new ("(ii)", 2, n),
1081                                          G_VARIANT_TYPE ("(i)"),
1082                                          G_DBUS_CALL_FLAGS_NONE,
1083                                          -1, /* timeout_msec */
1084                                          NULL, /* GCancellable */
1085                                          &error);
1086       g_assert_no_error (error);
1087       g_variant_get (res, "(i)", &val);
1088       g_assert_cmpint (val, ==, 2 + n);
1089       g_variant_unref (res);
1090       g_object_unref (c);
1091   }
1092
1093   g_main_loop_quit (data->loop);
1094   g_thread_join (service_thread);
1095   dmp_data_free (data);
1096 }
1097
1098 /* ---------------------------------------------------------------------------------------------------- */
1099
1100 static gboolean
1101 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1102                                            GIOStream         *stream,
1103                                            GCredentials      *credentials,
1104                                            gpointer           user_data)
1105 {
1106   PeerData *data = user_data;
1107   gboolean authorized;
1108
1109   data->num_connection_attempts++;
1110
1111   authorized = TRUE;
1112   if (!data->accept_connection)
1113     {
1114       authorized = FALSE;
1115       g_main_loop_quit (loop);
1116     }
1117
1118   return authorized;
1119 }
1120
1121 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1122 static gboolean
1123 nonce_tcp_on_new_connection (GDBusServer *server,
1124                              GDBusConnection *connection,
1125                              gpointer user_data)
1126 {
1127   PeerData *data = user_data;
1128
1129   g_ptr_array_add (data->current_connections, g_object_ref (connection));
1130
1131   g_main_loop_quit (loop);
1132
1133   return TRUE;
1134 }
1135
1136 static gpointer
1137 nonce_tcp_service_thread_func (gpointer user_data)
1138 {
1139   PeerData *data = user_data;
1140   GMainContext *service_context;
1141   GDBusAuthObserver *observer;
1142   GError *error;
1143
1144   service_context = g_main_context_new ();
1145   g_main_context_push_thread_default (service_context);
1146
1147   error = NULL;
1148   observer = g_dbus_auth_observer_new ();
1149   server = g_dbus_server_new_sync ("nonce-tcp:",
1150                                    G_DBUS_SERVER_FLAGS_NONE,
1151                                    test_guid,
1152                                    observer,
1153                                    NULL, /* cancellable */
1154                                    &error);
1155   g_assert_no_error (error);
1156
1157   g_signal_connect (server,
1158                     "new-connection",
1159                     G_CALLBACK (nonce_tcp_on_new_connection),
1160                     data);
1161   g_signal_connect (observer,
1162                     "authorize-authenticated-peer",
1163                     G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1164                     data);
1165   g_object_unref (observer);
1166
1167   g_dbus_server_start (server);
1168
1169   service_loop = g_main_loop_new (service_context, FALSE);
1170   g_main_loop_run (service_loop);
1171
1172   g_main_context_pop_thread_default (service_context);
1173
1174   g_main_loop_unref (service_loop);
1175   g_main_context_unref (service_context);
1176
1177   /* test code specifically unrefs the server - see below */
1178   g_assert (server == NULL);
1179
1180   return NULL;
1181 }
1182
1183 static void
1184 test_nonce_tcp (void)
1185 {
1186   PeerData data;
1187   GError *error;
1188   GThread *service_thread;
1189   GDBusConnection *c;
1190   gchar *s;
1191   gchar *nonce_file;
1192   gboolean res;
1193   const gchar *address;
1194
1195   memset (&data, '\0', sizeof (PeerData));
1196   data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1197
1198   error = NULL;
1199   server = NULL;
1200   service_loop = NULL;
1201   service_thread = g_thread_new ("nonce-tcp-service",
1202                                  nonce_tcp_service_thread_func,
1203                                  &data);
1204   while (service_loop == NULL)
1205     g_thread_yield ();
1206   g_assert (server != NULL);
1207
1208
1209   /* bring up a connection and accept it */
1210   data.accept_connection = TRUE;
1211   error = NULL;
1212   c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1213                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1214                                               NULL, /* GDBusAuthObserver */
1215                                               NULL, /* cancellable */
1216                                               &error);
1217   g_assert_no_error (error);
1218   g_assert (c != NULL);
1219   while (data.current_connections->len < 1)
1220     g_thread_yield ();
1221   g_assert_cmpint (data.current_connections->len, ==, 1);
1222   g_assert_cmpint (data.num_connection_attempts, ==, 1);
1223   g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1224   g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1225   g_object_unref (c);
1226
1227   /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1228    */
1229
1230   address = g_dbus_server_get_client_address (server);
1231
1232   s = strstr (address, "noncefile=");
1233   g_assert (s != NULL);
1234   s += sizeof "noncefile=" - 1;
1235   nonce_file = g_strdup (s);
1236
1237   /* First try invalid data in the nonce file - this will actually
1238    * make the client send this and the server will reject it. The way
1239    * it works is that if the nonce doesn't match, the server will
1240    * simply close the connection. So, from the client point of view,
1241    * we can see a variety of errors.
1242    */
1243   error = NULL;
1244   res = g_file_set_contents (nonce_file,
1245                              "0123456789012345",
1246                              -1,
1247                              &error);
1248   g_assert_no_error (error);
1249   g_assert (res);
1250   c = g_dbus_connection_new_for_address_sync (address,
1251                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1252                                               NULL, /* GDBusAuthObserver */
1253                                               NULL, /* cancellable */
1254                                               &error);
1255   _g_assert_error_domain (error, G_IO_ERROR);
1256   g_error_free (error);
1257   g_assert (c == NULL);
1258
1259   /* Then try with a nonce-file of incorrect length - this will make
1260    * the client complain - we won't even try connecting to the server
1261    * for this
1262    */
1263   error = NULL;
1264   res = g_file_set_contents (nonce_file,
1265                              "0123456789012345_",
1266                              -1,
1267                              &error);
1268   g_assert_no_error (error);
1269   g_assert (res);
1270   c = g_dbus_connection_new_for_address_sync (address,
1271                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1272                                               NULL, /* GDBusAuthObserver */
1273                                               NULL, /* cancellable */
1274                                               &error);
1275   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1276   g_error_free (error);
1277   g_assert (c == NULL);
1278
1279   /* Finally try with no nonce-file at all */
1280   g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1281   error = NULL;
1282   c = g_dbus_connection_new_for_address_sync (address,
1283                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1284                                               NULL, /* GDBusAuthObserver */
1285                                               NULL, /* cancellable */
1286                                               &error);
1287   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1288   g_error_free (error);
1289   g_assert (c == NULL);
1290
1291   g_free (nonce_file);
1292
1293   g_dbus_server_stop (server);
1294   g_object_unref (server);
1295   server = NULL;
1296
1297   g_main_loop_quit (service_loop);
1298   g_thread_join (service_thread);
1299 }
1300
1301 static void
1302 test_credentials (void)
1303 {
1304   GCredentials *c1, *c2;
1305   GError *error;
1306   gchar *desc;
1307
1308   c1 = g_credentials_new ();
1309   c2 = g_credentials_new ();
1310
1311   error = NULL;
1312   if (g_credentials_set_unix_user (c2, getuid (), &error))
1313     g_assert_no_error (error);
1314
1315   g_clear_error (&error);
1316   g_assert (g_credentials_is_same_user (c1, c2, &error));
1317   g_assert_no_error (error);
1318
1319   desc = g_credentials_to_string (c1);
1320   g_assert (desc != NULL);
1321   g_free (desc);
1322
1323   g_object_unref (c1);
1324   g_object_unref (c2);
1325 }
1326
1327 /* ---------------------------------------------------------------------------------------------------- */
1328
1329 #ifdef G_OS_UNIX
1330
1331 /* Chosen to be big enough to overflow the socket buffer */
1332 #define OVERFLOW_NUM_SIGNALS 5000
1333 #define OVERFLOW_TIMEOUT_SEC 10
1334
1335 static GDBusMessage *
1336 overflow_filter_func (GDBusConnection *connection,
1337                       GDBusMessage    *message,
1338                       gboolean         incoming,
1339                       gpointer         user_data)
1340 {
1341   volatile gint *counter = user_data;
1342   *counter += 1;
1343   return message;
1344 }
1345
1346 static gboolean
1347 overflow_on_500ms_later_func (gpointer user_data)
1348 {
1349   g_main_loop_quit (loop);
1350   return FALSE; /* don't keep the idle */
1351 }
1352
1353 static void
1354 test_overflow (void)
1355 {
1356   gint sv[2];
1357   gint n;
1358   GSocket *socket;
1359   GSocketConnection *socket_connection;
1360   GDBusConnection *producer, *consumer;
1361   GError *error;
1362   GTimer *timer;
1363   volatile gint n_messages_received;
1364   volatile gint n_messages_sent;
1365
1366   g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
1367
1368   error = NULL;
1369   socket = g_socket_new_from_fd (sv[0], &error);
1370   g_assert_no_error (error);
1371   socket_connection = g_socket_connection_factory_create_connection (socket);
1372   g_assert (socket_connection != NULL);
1373   g_object_unref (socket);
1374   producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1375                                          NULL, /* guid */
1376                                          G_DBUS_CONNECTION_FLAGS_NONE,
1377                                          NULL, /* GDBusAuthObserver */
1378                                          NULL, /* GCancellable */
1379                                          &error);
1380   g_dbus_connection_set_exit_on_close (producer, TRUE);
1381   g_assert_no_error (error);
1382   g_object_unref (socket_connection);
1383   n_messages_sent = 0;
1384   g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
1385
1386   /* send enough data that we get an EAGAIN */
1387   for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
1388     {
1389       error = NULL;
1390       g_dbus_connection_emit_signal (producer,
1391                                      NULL, /* destination */
1392                                      "/org/foo/Object",
1393                                      "org.foo.Interface",
1394                                      "Member",
1395                                      g_variant_new ("(s)", "a string"),
1396                                      &error);
1397       g_assert_no_error (error);
1398     }
1399
1400   /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
1401    * kernel buffers) and verify that n_messages_sent <
1402    * OVERFLOW_NUM_SIGNALS
1403    *
1404    * This is to verify that not all the submitted messages have been
1405    * sent to the underlying transport.
1406    */
1407   g_timeout_add (500, overflow_on_500ms_later_func, NULL);
1408   g_main_loop_run (loop);
1409   g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
1410
1411   /* now suck it all out as a client, and add it up */
1412   socket = g_socket_new_from_fd (sv[1], &error);
1413   g_assert_no_error (error);
1414   socket_connection = g_socket_connection_factory_create_connection (socket);
1415   g_assert (socket_connection != NULL);
1416   g_object_unref (socket);
1417   consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1418                                          NULL, /* guid */
1419                                          G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
1420                                          NULL, /* GDBusAuthObserver */
1421                                          NULL, /* GCancellable */
1422                                          &error);
1423   g_assert_no_error (error);
1424   g_object_unref (socket_connection);
1425   n_messages_received = 0;
1426   g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
1427   g_dbus_connection_start_message_processing (consumer);
1428
1429   timer = g_timer_new ();
1430   g_timer_start (timer);
1431
1432   while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
1433       g_main_context_iteration (NULL, FALSE);
1434
1435   g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
1436   g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
1437
1438   g_timer_destroy (timer);
1439   g_object_unref (consumer);
1440   g_object_unref (producer);
1441 }
1442 #else
1443 static void
1444 test_overflow (void)
1445 {
1446   /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
1447 }
1448 #endif
1449
1450 /* ---------------------------------------------------------------------------------------------------- */
1451
1452 static gboolean
1453 tcp_anonymous_on_new_connection (GDBusServer     *server,
1454                                  GDBusConnection *connection,
1455                                  gpointer         user_data)
1456 {
1457   gboolean *seen_connection = user_data;
1458   *seen_connection = TRUE;
1459   return TRUE;
1460 }
1461
1462 static gpointer
1463 tcp_anonymous_service_thread_func (gpointer user_data)
1464 {
1465   gboolean *seen_connection = user_data;
1466   GMainContext *service_context;
1467   GError *error;
1468
1469   service_context = g_main_context_new ();
1470   g_main_context_push_thread_default (service_context);
1471
1472   error = NULL;
1473   server = g_dbus_server_new_sync ("tcp:",
1474                                    G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS,
1475                                    test_guid,
1476                                    NULL, /* GDBusObserver* */
1477                                    NULL, /* GCancellable* */
1478                                    &error);
1479   g_assert_no_error (error);
1480
1481   g_signal_connect (server,
1482                     "new-connection",
1483                     G_CALLBACK (tcp_anonymous_on_new_connection),
1484                     seen_connection);
1485
1486   g_dbus_server_start (server);
1487
1488   service_loop = g_main_loop_new (service_context, FALSE);
1489   g_main_loop_run (service_loop);
1490
1491   g_main_context_pop_thread_default (service_context);
1492
1493   g_main_loop_unref (service_loop);
1494   g_main_context_unref (service_context);
1495
1496   return NULL;
1497 }
1498
1499 static void
1500 test_tcp_anonymous (void)
1501 {
1502   gboolean seen_connection;
1503   GThread *service_thread;
1504   GDBusConnection *connection;
1505   GError *error;
1506
1507   seen_connection = FALSE;
1508   service_loop = NULL;
1509   service_thread = g_thread_new ("tcp-anon-service",
1510                                  tcp_anonymous_service_thread_func,
1511                                  &seen_connection);
1512   while (service_loop == NULL)
1513     g_thread_yield ();
1514   g_assert (server != NULL);
1515
1516   error = NULL;
1517   connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1518                                                        G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1519                                                        NULL, /* GDBusAuthObserver* */
1520                                                        NULL, /* GCancellable */
1521                                                        &error);
1522   g_assert_no_error (error);
1523   g_assert (connection != NULL);
1524
1525   while (!seen_connection)
1526     g_thread_yield ();
1527
1528   g_object_unref (connection);
1529
1530   g_main_loop_quit (service_loop);
1531   g_dbus_server_stop (server);
1532   g_object_unref (server);
1533   server = NULL;
1534
1535   g_thread_join (service_thread);
1536 }
1537
1538 /* ---------------------------------------------------------------------------------------------------- */
1539
1540 static GDBusServer *codegen_server = NULL;
1541
1542 static gboolean
1543 codegen_on_animal_poke (ExampleAnimal          *animal,
1544                         GDBusMethodInvocation  *invocation,
1545                         gboolean                make_sad,
1546                         gboolean                make_happy,
1547                         gpointer                user_data)
1548 {
1549   if ((make_sad && make_happy) || (!make_sad && !make_happy))
1550     {
1551       g_main_loop_quit (service_loop);
1552
1553       g_dbus_method_invocation_return_dbus_error (invocation,
1554                                                   "org.gtk.GDBus.Examples.ObjectManager.Error.Failed",
1555                                                   "Exactly one of make_sad or make_happy must be TRUE");
1556       goto out;
1557     }
1558
1559   if (make_sad)
1560     {
1561       if (g_strcmp0 (example_animal_get_mood (animal), "Sad") == 0)
1562         {
1563           g_dbus_method_invocation_return_dbus_error (invocation,
1564                                                       "org.gtk.GDBus.Examples.ObjectManager.Error.SadAnimalIsSad",
1565                                                       "Sad animal is already sad");
1566           goto out;
1567         }
1568
1569       example_animal_set_mood (animal, "Sad");
1570       example_animal_complete_poke (animal, invocation);
1571       goto out;
1572     }
1573
1574   if (make_happy)
1575     {
1576       if (g_strcmp0 (example_animal_get_mood (animal), "Happy") == 0)
1577         {
1578           g_dbus_method_invocation_return_dbus_error (invocation,
1579                                                       "org.gtk.GDBus.Examples.ObjectManager.Error.HappyAnimalIsHappy",
1580                                                       "Happy animal is already happy");
1581           goto out;
1582         }
1583
1584       example_animal_set_mood (animal, "Happy");
1585       example_animal_complete_poke (animal, invocation);
1586       goto out;
1587     }
1588
1589   g_assert_not_reached ();
1590
1591  out:
1592   return TRUE; /* to indicate that the method was handled */
1593 }
1594
1595 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1596 static gboolean
1597 codegen_on_new_connection (GDBusServer *server,
1598                            GDBusConnection *connection,
1599                            gpointer user_data)
1600 {
1601   ExampleAnimal *animal = user_data;
1602   GError        *error = NULL;
1603
1604   /* g_print ("Client connected.\n" */
1605   /*          "Negotiated capabilities: unix-fd-passing=%d\n", */
1606   /*          g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING); */
1607
1608   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (animal), connection,
1609                                     "/Example/Animals/000", &error);
1610   g_assert_no_error (error);
1611
1612   return TRUE;
1613 }
1614
1615 static gpointer
1616 codegen_service_thread_func (gpointer user_data)
1617 {
1618   GMainContext   *service_context;
1619   ExampleAnimal  *animal;
1620   GError         *error = NULL;
1621
1622   service_context = g_main_context_new ();
1623   g_main_context_push_thread_default (service_context);
1624
1625   /* Create the animal in the right thread context */
1626   animal = example_animal_skeleton_new ();
1627
1628   /* Handle Poke() D-Bus method invocations on the .Animal interface */
1629   g_signal_connect (animal, "handle-poke",
1630                     G_CALLBACK (codegen_on_animal_poke),
1631                     NULL); /* user_data */
1632
1633   codegen_server = g_dbus_server_new_sync (tmp_address,
1634                                            G_DBUS_SERVER_FLAGS_NONE,
1635                                            test_guid,
1636                                            NULL, /* observer */
1637                                            NULL, /* cancellable */
1638                                            &error);
1639   g_assert_no_error (error);
1640   g_dbus_server_start (codegen_server);
1641
1642   g_signal_connect (codegen_server, "new-connection",
1643                     G_CALLBACK (codegen_on_new_connection),
1644                     animal);
1645
1646   service_loop = g_main_loop_new (service_context, FALSE);
1647   g_main_loop_run (service_loop);
1648
1649   g_object_unref (animal);
1650
1651   g_main_context_pop_thread_default (service_context);
1652
1653   g_main_loop_unref (service_loop);
1654   g_main_context_unref (service_context);
1655
1656   g_dbus_server_stop (codegen_server);
1657   g_object_unref (codegen_server);
1658   codegen_server = NULL;
1659
1660   return NULL;
1661 }
1662
1663
1664 gboolean
1665 codegen_quit_mainloop_timeout (gpointer data)
1666 {
1667   g_main_loop_quit (loop);
1668   return FALSE;
1669 }
1670
1671 static void
1672 codegen_test_peer (void)
1673 {
1674   GDBusConnection     *connection;
1675   ExampleAnimal       *animal1, *animal2;
1676   GThread             *service_thread;
1677   GError              *error = NULL;
1678   GVariant            *value;
1679
1680   /* bring up a server - we run the server in a different thread to avoid deadlocks */
1681   service_loop = NULL;
1682   service_thread = g_thread_new ("codegen_test_peer",
1683                                  codegen_service_thread_func,
1684                                  NULL);
1685   while (service_loop == NULL)
1686     g_thread_yield ();
1687   g_assert (codegen_server != NULL);
1688
1689   /* Get an animal 1 ...  */
1690   connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (codegen_server),
1691                                                        G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1692                                                        NULL, /* GDBusAuthObserver */
1693                                                        NULL, /* cancellable */
1694                                                        &error);
1695   g_assert_no_error (error);
1696   g_assert (connection != NULL);
1697
1698   animal1 = example_animal_proxy_new_sync (connection, 0, NULL,
1699                                            "/Example/Animals/000", NULL, &error);
1700   g_assert_no_error (error);
1701   g_assert (animal1 != NULL);
1702   g_object_unref (connection);
1703
1704   /* Get animal 2 ...  */
1705   connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (codegen_server),
1706                                                        G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1707                                                        NULL, /* GDBusAuthObserver */
1708                                                        NULL, /* cancellable */
1709                                                        &error);
1710   g_assert_no_error (error);
1711   g_assert (connection != NULL);
1712
1713   animal2 = example_animal_proxy_new_sync (connection, 0, NULL,
1714                                            "/Example/Animals/000", NULL, &error);
1715   g_assert_no_error (error);
1716   g_assert (animal2 != NULL);
1717   g_object_unref (connection);
1718
1719   /* Make animal sad via animal1  */
1720   example_animal_call_poke_sync (animal1, TRUE, FALSE, NULL, &error);
1721   g_assert_no_error (error);
1722
1723   /* Poke server and make sure animal is updated */
1724   value = g_dbus_proxy_call_sync (G_DBUS_PROXY (animal1),
1725                                   "org.freedesktop.DBus.Peer.Ping",
1726                                   NULL, G_DBUS_CALL_FLAGS_NONE, -1,
1727                                   NULL, &error);
1728   g_assert_no_error (error);
1729   g_assert (value != NULL);
1730   g_variant_unref (value);
1731
1732   /* Give the proxies a chance to refresh in the defaul main loop */
1733   g_timeout_add (100, codegen_quit_mainloop_timeout, NULL);
1734   g_main_loop_run (loop);
1735
1736   /* Assert animals are sad */
1737   g_assert_cmpstr (example_animal_get_mood (animal1), ==, "Sad");
1738   g_assert_cmpstr (example_animal_get_mood (animal2), ==, "Sad");
1739
1740   /* Make animal happy via animal2  */
1741   example_animal_call_poke_sync (animal2, FALSE, TRUE, NULL, &error);
1742   g_assert_no_error (error);
1743
1744   /* Poke server and make sure animal is updated */
1745   value = g_dbus_proxy_call_sync (G_DBUS_PROXY (animal2),
1746                                   "org.freedesktop.DBus.Peer.Ping",
1747                                   NULL, G_DBUS_CALL_FLAGS_NONE, -1,
1748                                   NULL, &error);
1749   g_assert_no_error (error);
1750   g_assert (value != NULL);
1751   g_variant_unref (value);
1752
1753   /* Give the proxies a chance to refresh in the defaul main loop */
1754   g_timeout_add (1000, codegen_quit_mainloop_timeout, NULL);
1755   g_main_loop_run (loop);
1756
1757   /* Assert animals are happy */
1758   g_assert_cmpstr (example_animal_get_mood (animal1), ==, "Happy");
1759   g_assert_cmpstr (example_animal_get_mood (animal2), ==, "Happy");
1760
1761   /* This final call making the animal happy and sad will cause
1762    * the server to quit, when the server quits we dont get property
1763    * change notifications anyway because those are done from an idle handler
1764    */
1765   example_animal_call_poke_sync (animal2, TRUE, TRUE, NULL, &error);
1766
1767   g_object_unref (animal1);
1768   g_object_unref (animal2);
1769   g_thread_join (service_thread);
1770 }
1771
1772 /* ---------------------------------------------------------------------------------------------------- */
1773
1774
1775 int
1776 main (int   argc,
1777       char *argv[])
1778 {
1779   gint ret;
1780   GDBusNodeInfo *introspection_data = NULL;
1781   gchar *tmpdir = NULL;
1782
1783   g_type_init ();
1784   g_test_init (&argc, &argv, NULL);
1785
1786   g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
1787
1788   introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1789   g_assert (introspection_data != NULL);
1790   test_interface_introspection_data = introspection_data->interfaces[0];
1791
1792   test_guid = g_dbus_generate_guid ();
1793
1794   if (is_unix)
1795     {
1796       if (g_unix_socket_address_abstract_names_supported ())
1797         tmp_address = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
1798       else
1799         {
1800           tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
1801           tmp_address = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
1802         }
1803     }
1804   else
1805     tmp_address = g_strdup ("nonce-tcp:");
1806
1807   /* all the tests rely on a shared main loop */
1808   loop = g_main_loop_new (NULL, FALSE);
1809
1810   g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1811   g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1812   g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1813   g_test_add_func ("/gdbus/tcp-anonymous", test_tcp_anonymous);
1814   g_test_add_func ("/gdbus/credentials", test_credentials);
1815   g_test_add_func ("/gdbus/overflow", test_overflow);
1816   g_test_add_func ("/gdbus/codegen-peer-to-peer", codegen_test_peer);
1817
1818   ret = g_test_run();
1819
1820   g_main_loop_unref (loop);
1821   g_free (test_guid);
1822   g_dbus_node_info_unref (introspection_data);
1823   if (is_unix)
1824     g_free (tmp_address);
1825   if (tmpdir)
1826     {
1827       g_rmdir (tmpdir);
1828       g_free (tmpdir);
1829     }
1830
1831   return ret;
1832 }