934ebda324c93cc580ea526bbaa21b8cfcbfa4b5
[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, *o;
329   GError *error;
330   GDBusServerFlags f;
331   gchar *a, *g;
332   gboolean b;
333
334   service_context = g_main_context_new ();
335   g_main_context_push_thread_default (service_context);
336
337   error = NULL;
338   observer = g_dbus_auth_observer_new ();
339   server = g_dbus_server_new_sync (tmp_address,
340                                    G_DBUS_SERVER_FLAGS_NONE,
341                                    test_guid,
342                                    observer,
343                                    NULL, /* cancellable */
344                                    &error);
345   g_assert_no_error (error);
346
347   g_signal_connect (server,
348                     "new-connection",
349                     G_CALLBACK (on_new_connection),
350                     data);
351   g_signal_connect (observer,
352                     "authorize-authenticated-peer",
353                     G_CALLBACK (on_authorize_authenticated_peer),
354                     data);
355
356   g_assert_cmpint (g_dbus_server_get_flags (server), ==, G_DBUS_SERVER_FLAGS_NONE);
357   g_assert_cmpstr (g_dbus_server_get_guid (server), ==, test_guid);
358   g_object_get (server,
359                 "flags", &f,
360                 "address", &a,
361                 "guid", &g,
362                 "active", &b,
363                 "authentication-observer", &o,
364                 NULL);
365   g_assert_cmpint (f, ==, G_DBUS_SERVER_FLAGS_NONE);
366   g_assert_cmpstr (a, ==, tmp_address);
367   g_assert_cmpstr (g, ==, test_guid);
368   g_assert (!b);
369   g_assert (o == observer);
370   g_free (a);
371   g_free (g);
372   g_object_unref (o);
373
374   g_object_unref (observer);
375
376   g_dbus_server_start (server);
377
378   service_loop = g_main_loop_new (service_context, FALSE);
379   g_main_loop_run (service_loop);
380
381   g_main_context_pop_thread_default (service_context);
382
383   g_main_loop_unref (service_loop);
384   g_main_context_unref (service_context);
385
386   /* test code specifically unrefs the server - see below */
387   g_assert (server == NULL);
388
389   return NULL;
390 }
391
392 #if 0
393 static gboolean
394 on_incoming_connection (GSocketService     *service,
395                         GSocketConnection  *socket_connection,
396                         GObject            *source_object,
397                         gpointer           user_data)
398 {
399   PeerData *data = user_data;
400
401   if (data->accept_connection)
402     {
403       GError *error;
404       guint reg_id;
405       GDBusConnection *connection;
406
407       error = NULL;
408       connection = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
409                                                test_guid,
410                                                G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_SERVER,
411                                                NULL, /* cancellable */
412                                                &error);
413       g_assert_no_error (error);
414
415       g_ptr_array_add (data->current_connections, connection);
416
417       /* export object on the newly established connection */
418       error = NULL;
419       reg_id = g_dbus_connection_register_object (connection,
420                                                   "/org/gtk/GDBus/PeerTestObject",
421                                                   &test_interface_introspection_data,
422                                                   &test_interface_vtable,
423                                                   data,
424                                                   NULL, /* GDestroyNotify for data */
425                                                   &error);
426       g_assert_no_error (error);
427       g_assert (reg_id > 0);
428
429     }
430   else
431     {
432       /* don't do anything */
433     }
434
435   data->num_connection_attempts++;
436
437   g_main_loop_quit (loop);
438
439   /* stops other signal handlers from being invoked */
440   return TRUE;
441 }
442
443 static gpointer
444 service_thread_func (gpointer data)
445 {
446   GMainContext *service_context;
447   gchar *socket_path;
448   GSocketAddress *address;
449   GError *error;
450
451   service_context = g_main_context_new ();
452   g_main_context_push_thread_default (service_context);
453
454   socket_path = g_strdup_printf ("/tmp/gdbus-test-pid-%d", getpid ());
455   address = g_unix_socket_address_new (socket_path);
456
457   service = g_socket_service_new ();
458   error = NULL;
459   g_socket_listener_add_address (G_SOCKET_LISTENER (service),
460                                  address,
461                                  G_SOCKET_TYPE_STREAM,
462                                  G_SOCKET_PROTOCOL_DEFAULT,
463                                  NULL, /* source_object */
464                                  NULL, /* effective_address */
465                                  &error);
466   g_assert_no_error (error);
467   g_signal_connect (service,
468                     "incoming",
469                     G_CALLBACK (on_incoming_connection),
470                     data);
471   g_socket_service_start (service);
472
473   service_loop = g_main_loop_new (service_context, FALSE);
474   g_main_loop_run (service_loop);
475
476   g_main_context_pop_thread_default (service_context);
477
478   g_main_loop_unref (service_loop);
479   g_main_context_unref (service_context);
480
481   g_object_unref (address);
482   g_free (socket_path);
483   return NULL;
484 }
485 #endif
486
487 /* ---------------------------------------------------------------------------------------------------- */
488
489 #if 0
490 static gboolean
491 check_connection (gpointer user_data)
492 {
493   PeerData *data = user_data;
494   guint n;
495
496   for (n = 0; n < data->current_connections->len; n++)
497     {
498       GDBusConnection *c;
499       GIOStream *stream;
500
501       c = G_DBUS_CONNECTION (data->current_connections->pdata[n]);
502       stream = g_dbus_connection_get_stream (c);
503
504       g_debug ("In check_connection for %d: connection %p, stream %p", n, c, stream);
505       g_debug ("closed = %d", g_io_stream_is_closed (stream));
506
507       GSocket *socket;
508       socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (stream));
509       g_debug ("socket_closed = %d", g_socket_is_closed (socket));
510       g_debug ("socket_condition_check = %d", g_socket_condition_check (socket, G_IO_IN|G_IO_OUT|G_IO_ERR|G_IO_HUP));
511
512       gchar buf[128];
513       GError *error;
514       gssize num_read;
515       error = NULL;
516       num_read = g_input_stream_read (g_io_stream_get_input_stream (stream),
517                                       buf,
518                                       128,
519                                       NULL,
520                                       &error);
521       if (num_read < 0)
522         {
523           g_debug ("error: %s", error->message);
524           g_error_free (error);
525         }
526       else
527         {
528           g_debug ("no error, read %d bytes", (gint) num_read);
529         }
530     }
531
532   return FALSE;
533 }
534
535 static gboolean
536 on_do_disconnect_in_idle (gpointer data)
537 {
538   GDBusConnection *c = G_DBUS_CONNECTION (data);
539   g_debug ("GDC %p has ref_count %d", c, G_OBJECT (c)->ref_count);
540   g_dbus_connection_disconnect (c);
541   g_object_unref (c);
542   return FALSE;
543 }
544 #endif
545
546 #ifdef G_OS_UNIX
547 static gchar *
548 read_all_from_fd (gint fd, gsize *out_len, GError **error)
549 {
550   GString *str;
551   gchar buf[64];
552   gssize num_read;
553
554   str = g_string_new (NULL);
555
556   do
557     {
558       num_read = read (fd, buf, sizeof (buf));
559       if (num_read == -1)
560         {
561           if (errno == EAGAIN || errno == EWOULDBLOCK)
562             continue;
563           g_set_error (error,
564                        G_IO_ERROR,
565                        g_io_error_from_errno (errno),
566                        "Failed reading %d bytes into offset %d: %s",
567                        (gint) sizeof (buf),
568                        (gint) str->len,
569                        strerror (errno));
570           goto error;
571         }
572       else if (num_read > 0)
573         {
574           g_string_append_len (str, buf, num_read);
575         }
576       else if (num_read == 0)
577         {
578           break;
579         }
580     }
581   while (TRUE);
582
583   if (out_len != NULL)
584     *out_len = str->len;
585   return g_string_free (str, FALSE);
586
587  error:
588   if (out_len != NULL)
589     out_len = 0;
590   g_string_free (str, TRUE);
591   return NULL;
592 }
593 #endif
594
595 static void
596 test_peer (void)
597 {
598   GDBusConnection *c;
599   GDBusConnection *c2;
600   GDBusProxy *proxy;
601   GError *error;
602   PeerData data;
603   GVariant *value;
604   GVariant *result;
605   const gchar *s;
606   GThread *service_thread;
607   gulong signal_handler_id;
608
609   memset (&data, '\0', sizeof (PeerData));
610   data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
611
612   /* first try to connect when there is no server */
613   error = NULL;
614   c = g_dbus_connection_new_for_address_sync (is_unix ? "unix:path=/tmp/gdbus-test-does-not-exist-pid" :
615                                               /* NOTE: Even if something is listening on port 12345 the connection
616                                                * will fail because the nonce file doesn't exist */
617                                               "nonce-tcp:host=localhost,port=12345,noncefile=this-does-not-exist-gdbus",
618                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
619                                               NULL, /* GDBusAuthObserver */
620                                               NULL, /* cancellable */
621                                               &error);
622   _g_assert_error_domain (error, G_IO_ERROR);
623   g_assert (!g_dbus_error_is_remote_error (error));
624   g_clear_error (&error);
625   g_assert (c == NULL);
626
627   /* bring up a server - we run the server in a different thread to avoid deadlocks */
628   service_loop = NULL;
629   service_thread = g_thread_new ("test_peer",
630                                  service_thread_func,
631                                  &data);
632   while (service_loop == NULL)
633     g_thread_yield ();
634   g_assert (server != NULL);
635
636   /* bring up a connection and accept it */
637   data.accept_connection = TRUE;
638   error = NULL;
639   c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
640                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
641                                               NULL, /* GDBusAuthObserver */
642                                               NULL, /* cancellable */
643                                               &error);
644   g_assert_no_error (error);
645   g_assert (c != NULL);
646   while (data.current_connections->len < 1)
647     g_main_loop_run (loop);
648   g_assert_cmpint (data.current_connections->len, ==, 1);
649   g_assert_cmpint (data.num_connection_attempts, ==, 1);
650   g_assert (g_dbus_connection_get_unique_name (c) == NULL);
651   g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
652
653   /* check that we create a proxy, read properties, receive signals and invoke
654    * the HelloPeer() method. Since the server runs in another thread it's fine
655    * to use synchronous blocking API here.
656    */
657   error = NULL;
658   proxy = g_dbus_proxy_new_sync (c,
659                                  G_DBUS_PROXY_FLAGS_NONE,
660                                  NULL,
661                                  NULL, /* bus_name */
662                                  "/org/gtk/GDBus/PeerTestObject",
663                                  "org.gtk.GDBus.PeerTestInterface",
664                                  NULL, /* GCancellable */
665                                  &error);
666   g_assert_no_error (error);
667   g_assert (proxy != NULL);
668   error = NULL;
669   value = g_dbus_proxy_get_cached_property (proxy, "PeerProperty");
670   g_assert_cmpstr (g_variant_get_string (value, NULL), ==, "ThePropertyValue");
671
672   /* try invoking a method */
673   error = NULL;
674   result = g_dbus_proxy_call_sync (proxy,
675                                    "HelloPeer",
676                                    g_variant_new ("(s)", "Hey Peer!"),
677                                    G_DBUS_CALL_FLAGS_NONE,
678                                    -1,
679                                    NULL,  /* GCancellable */
680                                    &error);
681   g_assert_no_error (error);
682   g_variant_get (result, "(&s)", &s);
683   g_assert_cmpstr (s, ==, "You greeted me with 'Hey Peer!'.");
684   g_variant_unref (result);
685   g_assert_cmpint (data.num_method_calls, ==, 1);
686
687   /* make the other peer emit a signal - catch it */
688   signal_handler_id = g_signal_connect (proxy,
689                                         "g-signal",
690                                         G_CALLBACK (on_proxy_signal_received),
691                                         &data);
692   g_assert (!data.signal_received);
693   g_dbus_proxy_call (proxy,
694                      "EmitSignal",
695                      NULL,  /* no arguments */
696                      G_DBUS_CALL_FLAGS_NONE,
697                      -1,
698                      NULL,  /* GCancellable */
699                      NULL,  /* GAsyncReadyCallback - we don't care about the result */
700                      NULL); /* user_data */
701   g_main_loop_run (loop);
702   g_assert (data.signal_received);
703   g_assert_cmpint (data.num_method_calls, ==, 2);
704   g_signal_handler_disconnect (proxy, signal_handler_id);
705
706   /* Also ensure that messages with the sender header-field set gets
707    * delivered to the proxy - note that this doesn't really make sense
708    * e.g. names are meaning-less in a peer-to-peer case... but we
709    * support it because it makes sense in certain bridging
710    * applications - see e.g. #623815.
711    */
712   signal_handler_id = g_signal_connect (proxy,
713                                         "g-signal",
714                                         G_CALLBACK (on_proxy_signal_received_with_name_set),
715                                         &data);
716   data.signal_received = FALSE;
717   g_dbus_proxy_call (proxy,
718                      "EmitSignalWithNameSet",
719                      NULL,  /* no arguments */
720                      G_DBUS_CALL_FLAGS_NONE,
721                      -1,
722                      NULL,  /* GCancellable */
723                      NULL,  /* GAsyncReadyCallback - we don't care about the result */
724                      NULL); /* user_data */
725   g_main_loop_run (loop);
726   g_assert (data.signal_received);
727   g_assert_cmpint (data.num_method_calls, ==, 3);
728   g_signal_handler_disconnect (proxy, signal_handler_id);
729
730   /* check for UNIX fd passing */
731 #ifdef G_OS_UNIX
732   {
733     GDBusMessage *method_call_message;
734     GDBusMessage *method_reply_message;
735     GUnixFDList *fd_list;
736     gint fd;
737     gchar *buf;
738     gsize len;
739     gchar *buf2;
740     gsize len2;
741
742     method_call_message = g_dbus_message_new_method_call (NULL, /* name */
743                                                           "/org/gtk/GDBus/PeerTestObject",
744                                                           "org.gtk.GDBus.PeerTestInterface",
745                                                           "OpenFile");
746     g_dbus_message_set_body (method_call_message, g_variant_new ("(s)", "/etc/hosts"));
747     error = NULL;
748     method_reply_message = g_dbus_connection_send_message_with_reply_sync (c,
749                                                                            method_call_message,
750                                                                            G_DBUS_SEND_MESSAGE_FLAGS_NONE,
751                                                                            -1,
752                                                                            NULL, /* out_serial */
753                                                                            NULL, /* cancellable */
754                                                                            &error);
755     g_assert_no_error (error);
756     g_assert (g_dbus_message_get_message_type (method_reply_message) == G_DBUS_MESSAGE_TYPE_METHOD_RETURN);
757     fd_list = g_dbus_message_get_unix_fd_list (method_reply_message);
758     g_assert (fd_list != NULL);
759     g_assert_cmpint (g_unix_fd_list_get_length (fd_list), ==, 1);
760     error = NULL;
761     fd = g_unix_fd_list_get (fd_list, 0, &error);
762     g_assert_no_error (error);
763     g_object_unref (method_call_message);
764     g_object_unref (method_reply_message);
765
766     error = NULL;
767     len = 0;
768     buf = read_all_from_fd (fd, &len, &error);
769     g_assert_no_error (error);
770     g_assert (buf != NULL);
771     close (fd);
772
773     error = NULL;
774     g_file_get_contents ("/etc/hosts",
775                          &buf2,
776                          &len2,
777                          &error);
778     g_assert_no_error (error);
779     g_assert_cmpint (len, ==, len2);
780     g_assert (memcmp (buf, buf2, len) == 0);
781     g_free (buf2);
782     g_free (buf);
783   }
784 #else
785   error = NULL;
786   result = g_dbus_proxy_call_sync (proxy,
787                                    "OpenFile",
788                                    g_variant_new ("(s)", "boo"),
789                                    G_DBUS_CALL_FLAGS_NONE,
790                                    -1,
791                                    NULL,  /* GCancellable */
792                                    &error);
793   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
794   g_assert (result == NULL);
795   g_error_free (error);
796 #endif /* G_OS_UNIX */
797
798   /* Check that g_socket_get_credentials() work - this really should
799    * be in a GSocket-specific test suite but no such test suite exists
800    * right now.
801    */
802   {
803     GSocket *socket;
804     GCredentials *credentials;
805     socket = g_socket_connection_get_socket (G_SOCKET_CONNECTION (g_dbus_connection_get_stream (c)));
806     g_assert (G_IS_SOCKET (socket));
807     error = NULL;
808     credentials = g_socket_get_credentials (socket, &error);
809 #ifdef __linux__
810     {
811       struct ucred *native_creds;
812       g_assert_no_error (error);
813       g_assert (G_IS_CREDENTIALS (credentials));
814       native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_LINUX_UCRED);
815       g_assert (native_creds != NULL);
816       g_assert (native_creds->uid == getuid ());
817       g_assert (native_creds->gid == getgid ());
818       g_assert (native_creds->pid == getpid ());
819     }
820     g_object_unref (credentials);
821 #elif defined (__OpenBSD__)
822     {
823       struct sockpeercred *native_creds;
824       g_assert_no_error (error);
825       g_assert (G_IS_CREDENTIALS (credentials));
826       native_creds = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED);
827       g_assert (native_creds != NULL);
828       g_assert (native_creds->uid == getuid ());
829       g_assert (native_creds->gid == getgid ());
830       g_assert (native_creds->pid == getpid ());
831     }
832     g_object_unref (credentials);
833 #else
834     g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
835     g_assert (credentials == NULL);
836 #endif
837   }
838
839
840   /* bring up a connection - don't accept it - this should fail
841    */
842   data.accept_connection = FALSE;
843   error = NULL;
844   c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
845                                                G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
846                                                NULL, /* GDBusAuthObserver */
847                                                NULL, /* cancellable */
848                                                &error);
849   _g_assert_error_domain (error, G_IO_ERROR);
850   g_error_free (error);
851   g_assert (c2 == NULL);
852
853 #if 0
854   /* TODO: THIS TEST DOESN'T WORK YET */
855
856   /* bring up a connection - accept it.. then disconnect from the client side - check
857    * that the server side gets the disconnect signal.
858    */
859   error = NULL;
860   data.accept_connection = TRUE;
861   c2 = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
862                                                G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
863                                                NULL, /* GDBusAuthObserver */
864                                                NULL, /* cancellable */
865                                                &error);
866   g_assert_no_error (error);
867   g_assert (c2 != NULL);
868   g_assert (!g_dbus_connection_get_is_disconnected (c2));
869   while (data.num_connection_attempts < 3)
870     g_main_loop_run (loop);
871   g_assert_cmpint (data.current_connections->len, ==, 2);
872   g_assert_cmpint (data.num_connection_attempts, ==, 3);
873   g_assert (!g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
874   g_idle_add (on_do_disconnect_in_idle, c2);
875   g_debug ("==================================================");
876   g_debug ("==================================================");
877   g_debug ("==================================================");
878   g_debug ("waiting for disconnect on connection %p, stream %p",
879            data.current_connections->pdata[1],
880            g_dbus_connection_get_stream (data.current_connections->pdata[1]));
881
882   g_timeout_add (2000, check_connection, &data);
883   //_g_assert_signal_received (G_DBUS_CONNECTION (data.current_connections->pdata[1]), "closed");
884   g_main_loop_run (loop);
885   g_assert (g_dbus_connection_get_is_disconnected (G_DBUS_CONNECTION (data.current_connections->pdata[1])));
886   g_ptr_array_set_size (data.current_connections, 1); /* remove disconnected connection object */
887 #endif
888
889   /* unref the server and stop listening for new connections
890    *
891    * This won't bring down the established connections - check that c is still connected
892    * by invoking a method
893    */
894   //g_socket_service_stop (service);
895   //g_object_unref (service);
896   g_dbus_server_stop (server);
897   g_object_unref (server);
898   server = NULL;
899
900   error = NULL;
901   result = g_dbus_proxy_call_sync (proxy,
902                                    "HelloPeer",
903                                    g_variant_new ("(s)", "Hey Again Peer!"),
904                                    G_DBUS_CALL_FLAGS_NONE,
905                                    -1,
906                                    NULL,  /* GCancellable */
907                                    &error);
908   g_assert_no_error (error);
909   g_variant_get (result, "(&s)", &s);
910   g_assert_cmpstr (s, ==, "You greeted me with 'Hey Again Peer!'.");
911   g_variant_unref (result);
912   g_assert_cmpint (data.num_method_calls, ==, 5);
913
914 #if 0
915   /* TODO: THIS TEST DOESN'T WORK YET */
916
917   /* now disconnect from the server side - check that the client side gets the signal */
918   g_assert_cmpint (data.current_connections->len, ==, 1);
919   g_assert (G_DBUS_CONNECTION (data.current_connections->pdata[0]) != c);
920   g_dbus_connection_disconnect (G_DBUS_CONNECTION (data.current_connections->pdata[0]));
921   if (!g_dbus_connection_get_is_disconnected (c))
922     _g_assert_signal_received (c, "closed");
923   g_assert (g_dbus_connection_get_is_disconnected (c));
924 #endif
925
926   g_object_unref (c);
927   g_ptr_array_unref (data.current_connections);
928   g_object_unref (proxy);
929
930   g_main_loop_quit (service_loop);
931   g_thread_join (service_thread);
932 }
933
934 /* ---------------------------------------------------------------------------------------------------- */
935
936 typedef struct
937 {
938   GDBusServer *server;
939   GMainContext *context;
940   GMainLoop *loop;
941
942   GList *connections;
943 } DmpData;
944
945 static void
946 dmp_data_free (DmpData *data)
947 {
948   g_main_loop_unref (data->loop);
949   g_main_context_unref (data->context);
950   g_object_unref (data->server);
951   g_list_free_full (data->connections, g_object_unref);
952   g_free (data);
953 }
954
955 static void
956 dmp_on_method_call (GDBusConnection       *connection,
957                     const gchar           *sender,
958                     const gchar           *object_path,
959                     const gchar           *interface_name,
960                     const gchar           *method_name,
961                     GVariant              *parameters,
962                     GDBusMethodInvocation *invocation,
963                     gpointer               user_data)
964 {
965   //DmpData *data = user_data;
966   gint32 first;
967   gint32 second;
968   g_variant_get (parameters,
969                  "(ii)",
970                  &first,
971                  &second);
972   g_dbus_method_invocation_return_value (invocation,
973                                          g_variant_new ("(i)", first + second));
974 }
975
976 static const GDBusInterfaceVTable dmp_interface_vtable =
977 {
978   dmp_on_method_call,
979   NULL,  /* get_property */
980   NULL   /* set_property */
981 };
982
983
984 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
985 static gboolean
986 dmp_on_new_connection (GDBusServer     *server,
987                        GDBusConnection *connection,
988                        gpointer         user_data)
989 {
990   DmpData *data = user_data;
991   GDBusNodeInfo *node;
992   GError *error;
993
994   /* accept the connection */
995   data->connections = g_list_prepend (data->connections, g_object_ref (connection));
996
997   error = NULL;
998   node = g_dbus_node_info_new_for_xml ("<node>"
999                                        "  <interface name='org.gtk.GDBus.DmpInterface'>"
1000                                        "    <method name='AddPair'>"
1001                                        "      <arg type='i' name='first' direction='in'/>"
1002                                        "      <arg type='i' name='second' direction='in'/>"
1003                                        "      <arg type='i' name='sum' direction='out'/>"
1004                                        "    </method>"
1005                                        "  </interface>"
1006                                        "</node>",
1007                                        &error);
1008   g_assert_no_error (error);
1009
1010   /* sleep 100ms before exporting an object - this is to test that
1011    * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
1012    * (GDBusServer uses this feature).
1013    */
1014   usleep (100 * 1000);
1015
1016   /* export an object */
1017   error = NULL;
1018   g_dbus_connection_register_object (connection,
1019                                      "/dmp/test",
1020                                      node->interfaces[0],
1021                                      &dmp_interface_vtable,
1022                                      data,
1023                                      NULL,
1024                                      &error);
1025   g_dbus_node_info_unref (node);
1026
1027   return TRUE;
1028 }
1029
1030 static gpointer
1031 dmp_thread_func (gpointer user_data)
1032 {
1033   DmpData *data = user_data;
1034   GError *error;
1035   gchar *guid;
1036
1037   data->context = g_main_context_new ();
1038   g_main_context_push_thread_default (data->context);
1039
1040   error = NULL;
1041   guid = g_dbus_generate_guid ();
1042   data->server = g_dbus_server_new_sync (tmp_address,
1043                                          G_DBUS_SERVER_FLAGS_NONE,
1044                                          guid,
1045                                          NULL, /* GDBusAuthObserver */
1046                                          NULL, /* GCancellable */
1047                                          &error);
1048   g_assert_no_error (error);
1049   g_signal_connect (data->server,
1050                     "new-connection",
1051                     G_CALLBACK (dmp_on_new_connection),
1052                     data);
1053
1054   g_dbus_server_start (data->server);
1055
1056   data->loop = g_main_loop_new (data->context, FALSE);
1057   g_main_loop_run (data->loop);
1058
1059   g_main_context_pop_thread_default (data->context);
1060
1061   g_free (guid);
1062   return NULL;
1063 }
1064
1065 static void
1066 delayed_message_processing (void)
1067 {
1068   GError *error;
1069   DmpData *data;
1070   GThread *service_thread;
1071   guint n;
1072
1073   data = g_new0 (DmpData, 1);
1074
1075   service_thread = g_thread_new ("dmp",
1076                                  dmp_thread_func,
1077                                  data);
1078   while (data->server == NULL || !g_dbus_server_is_active (data->server))
1079     g_thread_yield ();
1080
1081   for (n = 0; n < 5; n++)
1082     {
1083       GDBusConnection *c;
1084       GVariant *res;
1085       gint32 val;
1086
1087       error = NULL;
1088       c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
1089                                                   G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1090                                                   NULL, /* GDBusAuthObserver */
1091                                                   NULL, /* GCancellable */
1092                                                   &error);
1093       g_assert_no_error (error);
1094
1095       error = NULL;
1096       res = g_dbus_connection_call_sync (c,
1097                                          NULL,    /* bus name */
1098                                          "/dmp/test",
1099                                          "org.gtk.GDBus.DmpInterface",
1100                                          "AddPair",
1101                                          g_variant_new ("(ii)", 2, n),
1102                                          G_VARIANT_TYPE ("(i)"),
1103                                          G_DBUS_CALL_FLAGS_NONE,
1104                                          -1, /* timeout_msec */
1105                                          NULL, /* GCancellable */
1106                                          &error);
1107       g_assert_no_error (error);
1108       g_variant_get (res, "(i)", &val);
1109       g_assert_cmpint (val, ==, 2 + n);
1110       g_variant_unref (res);
1111       g_object_unref (c);
1112   }
1113
1114   g_main_loop_quit (data->loop);
1115   g_thread_join (service_thread);
1116   dmp_data_free (data);
1117 }
1118
1119 /* ---------------------------------------------------------------------------------------------------- */
1120
1121 static gboolean
1122 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1123                                            GIOStream         *stream,
1124                                            GCredentials      *credentials,
1125                                            gpointer           user_data)
1126 {
1127   PeerData *data = user_data;
1128   gboolean authorized;
1129
1130   data->num_connection_attempts++;
1131
1132   authorized = TRUE;
1133   if (!data->accept_connection)
1134     {
1135       authorized = FALSE;
1136       g_main_loop_quit (loop);
1137     }
1138
1139   return authorized;
1140 }
1141
1142 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1143 static gboolean
1144 nonce_tcp_on_new_connection (GDBusServer *server,
1145                              GDBusConnection *connection,
1146                              gpointer user_data)
1147 {
1148   PeerData *data = user_data;
1149
1150   g_ptr_array_add (data->current_connections, g_object_ref (connection));
1151
1152   g_main_loop_quit (loop);
1153
1154   return TRUE;
1155 }
1156
1157 static gpointer
1158 nonce_tcp_service_thread_func (gpointer user_data)
1159 {
1160   PeerData *data = user_data;
1161   GMainContext *service_context;
1162   GDBusAuthObserver *observer;
1163   GError *error;
1164
1165   service_context = g_main_context_new ();
1166   g_main_context_push_thread_default (service_context);
1167
1168   error = NULL;
1169   observer = g_dbus_auth_observer_new ();
1170   server = g_dbus_server_new_sync ("nonce-tcp:",
1171                                    G_DBUS_SERVER_FLAGS_NONE,
1172                                    test_guid,
1173                                    observer,
1174                                    NULL, /* cancellable */
1175                                    &error);
1176   g_assert_no_error (error);
1177
1178   g_signal_connect (server,
1179                     "new-connection",
1180                     G_CALLBACK (nonce_tcp_on_new_connection),
1181                     data);
1182   g_signal_connect (observer,
1183                     "authorize-authenticated-peer",
1184                     G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1185                     data);
1186   g_object_unref (observer);
1187
1188   g_dbus_server_start (server);
1189
1190   service_loop = g_main_loop_new (service_context, FALSE);
1191   g_main_loop_run (service_loop);
1192
1193   g_main_context_pop_thread_default (service_context);
1194
1195   g_main_loop_unref (service_loop);
1196   g_main_context_unref (service_context);
1197
1198   /* test code specifically unrefs the server - see below */
1199   g_assert (server == NULL);
1200
1201   return NULL;
1202 }
1203
1204 static void
1205 test_nonce_tcp (void)
1206 {
1207   PeerData data;
1208   GError *error;
1209   GThread *service_thread;
1210   GDBusConnection *c;
1211   gchar *s;
1212   gchar *nonce_file;
1213   gboolean res;
1214   const gchar *address;
1215
1216   memset (&data, '\0', sizeof (PeerData));
1217   data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1218
1219   error = NULL;
1220   server = NULL;
1221   service_loop = NULL;
1222   service_thread = g_thread_new ("nonce-tcp-service",
1223                                  nonce_tcp_service_thread_func,
1224                                  &data);
1225   while (service_loop == NULL)
1226     g_thread_yield ();
1227   g_assert (server != NULL);
1228
1229
1230   /* bring up a connection and accept it */
1231   data.accept_connection = TRUE;
1232   error = NULL;
1233   c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1234                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1235                                               NULL, /* GDBusAuthObserver */
1236                                               NULL, /* cancellable */
1237                                               &error);
1238   g_assert_no_error (error);
1239   g_assert (c != NULL);
1240   while (data.current_connections->len < 1)
1241     g_thread_yield ();
1242   g_assert_cmpint (data.current_connections->len, ==, 1);
1243   g_assert_cmpint (data.num_connection_attempts, ==, 1);
1244   g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1245   g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1246   g_object_unref (c);
1247
1248   /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1249    */
1250
1251   address = g_dbus_server_get_client_address (server);
1252
1253   s = strstr (address, "noncefile=");
1254   g_assert (s != NULL);
1255   s += sizeof "noncefile=" - 1;
1256   nonce_file = g_strdup (s);
1257
1258   /* First try invalid data in the nonce file - this will actually
1259    * make the client send this and the server will reject it. The way
1260    * it works is that if the nonce doesn't match, the server will
1261    * simply close the connection. So, from the client point of view,
1262    * we can see a variety of errors.
1263    */
1264   error = NULL;
1265   res = g_file_set_contents (nonce_file,
1266                              "0123456789012345",
1267                              -1,
1268                              &error);
1269   g_assert_no_error (error);
1270   g_assert (res);
1271   c = g_dbus_connection_new_for_address_sync (address,
1272                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1273                                               NULL, /* GDBusAuthObserver */
1274                                               NULL, /* cancellable */
1275                                               &error);
1276   _g_assert_error_domain (error, G_IO_ERROR);
1277   g_error_free (error);
1278   g_assert (c == NULL);
1279
1280   /* Then try with a nonce-file of incorrect length - this will make
1281    * the client complain - we won't even try connecting to the server
1282    * for this
1283    */
1284   error = NULL;
1285   res = g_file_set_contents (nonce_file,
1286                              "0123456789012345_",
1287                              -1,
1288                              &error);
1289   g_assert_no_error (error);
1290   g_assert (res);
1291   c = g_dbus_connection_new_for_address_sync (address,
1292                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1293                                               NULL, /* GDBusAuthObserver */
1294                                               NULL, /* cancellable */
1295                                               &error);
1296   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1297   g_error_free (error);
1298   g_assert (c == NULL);
1299
1300   /* Finally try with no nonce-file at all */
1301   g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1302   error = NULL;
1303   c = g_dbus_connection_new_for_address_sync (address,
1304                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1305                                               NULL, /* GDBusAuthObserver */
1306                                               NULL, /* cancellable */
1307                                               &error);
1308   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1309   g_error_free (error);
1310   g_assert (c == NULL);
1311
1312   g_free (nonce_file);
1313
1314   g_dbus_server_stop (server);
1315   g_object_unref (server);
1316   server = NULL;
1317
1318   g_main_loop_quit (service_loop);
1319   g_thread_join (service_thread);
1320 }
1321
1322 static void
1323 test_credentials (void)
1324 {
1325   GCredentials *c1, *c2;
1326   GError *error;
1327   gchar *desc;
1328
1329   c1 = g_credentials_new ();
1330   c2 = g_credentials_new ();
1331
1332   error = NULL;
1333   if (g_credentials_set_unix_user (c2, getuid (), &error))
1334     g_assert_no_error (error);
1335
1336   g_clear_error (&error);
1337   g_assert (g_credentials_is_same_user (c1, c2, &error));
1338   g_assert_no_error (error);
1339
1340   desc = g_credentials_to_string (c1);
1341   g_assert (desc != NULL);
1342   g_free (desc);
1343
1344   g_object_unref (c1);
1345   g_object_unref (c2);
1346 }
1347
1348 /* ---------------------------------------------------------------------------------------------------- */
1349
1350 #ifdef G_OS_UNIX
1351
1352 /* Chosen to be big enough to overflow the socket buffer */
1353 #define OVERFLOW_NUM_SIGNALS 5000
1354 #define OVERFLOW_TIMEOUT_SEC 10
1355
1356 static GDBusMessage *
1357 overflow_filter_func (GDBusConnection *connection,
1358                       GDBusMessage    *message,
1359                       gboolean         incoming,
1360                       gpointer         user_data)
1361 {
1362   volatile gint *counter = user_data;
1363   *counter += 1;
1364   return message;
1365 }
1366
1367 static gboolean
1368 overflow_on_500ms_later_func (gpointer user_data)
1369 {
1370   g_main_loop_quit (loop);
1371   return FALSE; /* don't keep the idle */
1372 }
1373
1374 static void
1375 test_overflow (void)
1376 {
1377   gint sv[2];
1378   gint n;
1379   GSocket *socket;
1380   GSocketConnection *socket_connection;
1381   GDBusConnection *producer, *consumer;
1382   GError *error;
1383   GTimer *timer;
1384   volatile gint n_messages_received;
1385   volatile gint n_messages_sent;
1386
1387   g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
1388
1389   error = NULL;
1390   socket = g_socket_new_from_fd (sv[0], &error);
1391   g_assert_no_error (error);
1392   socket_connection = g_socket_connection_factory_create_connection (socket);
1393   g_assert (socket_connection != NULL);
1394   g_object_unref (socket);
1395   producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1396                                          NULL, /* guid */
1397                                          G_DBUS_CONNECTION_FLAGS_NONE,
1398                                          NULL, /* GDBusAuthObserver */
1399                                          NULL, /* GCancellable */
1400                                          &error);
1401   g_dbus_connection_set_exit_on_close (producer, TRUE);
1402   g_assert_no_error (error);
1403   g_object_unref (socket_connection);
1404   n_messages_sent = 0;
1405   g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
1406
1407   /* send enough data that we get an EAGAIN */
1408   for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
1409     {
1410       error = NULL;
1411       g_dbus_connection_emit_signal (producer,
1412                                      NULL, /* destination */
1413                                      "/org/foo/Object",
1414                                      "org.foo.Interface",
1415                                      "Member",
1416                                      g_variant_new ("(s)", "a string"),
1417                                      &error);
1418       g_assert_no_error (error);
1419     }
1420
1421   /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
1422    * kernel buffers) and verify that n_messages_sent <
1423    * OVERFLOW_NUM_SIGNALS
1424    *
1425    * This is to verify that not all the submitted messages have been
1426    * sent to the underlying transport.
1427    */
1428   g_timeout_add (500, overflow_on_500ms_later_func, NULL);
1429   g_main_loop_run (loop);
1430   g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
1431
1432   /* now suck it all out as a client, and add it up */
1433   socket = g_socket_new_from_fd (sv[1], &error);
1434   g_assert_no_error (error);
1435   socket_connection = g_socket_connection_factory_create_connection (socket);
1436   g_assert (socket_connection != NULL);
1437   g_object_unref (socket);
1438   consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1439                                          NULL, /* guid */
1440                                          G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
1441                                          NULL, /* GDBusAuthObserver */
1442                                          NULL, /* GCancellable */
1443                                          &error);
1444   g_assert_no_error (error);
1445   g_object_unref (socket_connection);
1446   n_messages_received = 0;
1447   g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
1448   g_dbus_connection_start_message_processing (consumer);
1449
1450   timer = g_timer_new ();
1451   g_timer_start (timer);
1452
1453   while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
1454       g_main_context_iteration (NULL, FALSE);
1455
1456   g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
1457   g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
1458
1459   g_timer_destroy (timer);
1460   g_object_unref (consumer);
1461   g_object_unref (producer);
1462 }
1463 #else
1464 static void
1465 test_overflow (void)
1466 {
1467   /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
1468 }
1469 #endif
1470
1471 /* ---------------------------------------------------------------------------------------------------- */
1472
1473 static gboolean
1474 tcp_anonymous_on_new_connection (GDBusServer     *server,
1475                                  GDBusConnection *connection,
1476                                  gpointer         user_data)
1477 {
1478   gboolean *seen_connection = user_data;
1479   *seen_connection = TRUE;
1480   return TRUE;
1481 }
1482
1483 static gpointer
1484 tcp_anonymous_service_thread_func (gpointer user_data)
1485 {
1486   gboolean *seen_connection = user_data;
1487   GMainContext *service_context;
1488   GError *error;
1489
1490   service_context = g_main_context_new ();
1491   g_main_context_push_thread_default (service_context);
1492
1493   error = NULL;
1494   server = g_dbus_server_new_sync ("tcp:",
1495                                    G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS,
1496                                    test_guid,
1497                                    NULL, /* GDBusObserver* */
1498                                    NULL, /* GCancellable* */
1499                                    &error);
1500   g_assert_no_error (error);
1501
1502   g_signal_connect (server,
1503                     "new-connection",
1504                     G_CALLBACK (tcp_anonymous_on_new_connection),
1505                     seen_connection);
1506
1507   g_dbus_server_start (server);
1508
1509   service_loop = g_main_loop_new (service_context, FALSE);
1510   g_main_loop_run (service_loop);
1511
1512   g_main_context_pop_thread_default (service_context);
1513
1514   g_main_loop_unref (service_loop);
1515   g_main_context_unref (service_context);
1516
1517   return NULL;
1518 }
1519
1520 static void
1521 test_tcp_anonymous (void)
1522 {
1523   gboolean seen_connection;
1524   GThread *service_thread;
1525   GDBusConnection *connection;
1526   GError *error;
1527
1528   seen_connection = FALSE;
1529   service_loop = NULL;
1530   service_thread = g_thread_new ("tcp-anon-service",
1531                                  tcp_anonymous_service_thread_func,
1532                                  &seen_connection);
1533   while (service_loop == NULL)
1534     g_thread_yield ();
1535   g_assert (server != NULL);
1536
1537   error = NULL;
1538   connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1539                                                        G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1540                                                        NULL, /* GDBusAuthObserver* */
1541                                                        NULL, /* GCancellable */
1542                                                        &error);
1543   g_assert_no_error (error);
1544   g_assert (connection != NULL);
1545
1546   while (!seen_connection)
1547     g_thread_yield ();
1548
1549   g_object_unref (connection);
1550
1551   g_main_loop_quit (service_loop);
1552   g_dbus_server_stop (server);
1553   g_object_unref (server);
1554   server = NULL;
1555
1556   g_thread_join (service_thread);
1557 }
1558
1559 /* ---------------------------------------------------------------------------------------------------- */
1560
1561 static GDBusServer *codegen_server = NULL;
1562
1563 static gboolean
1564 codegen_on_animal_poke (ExampleAnimal          *animal,
1565                         GDBusMethodInvocation  *invocation,
1566                         gboolean                make_sad,
1567                         gboolean                make_happy,
1568                         gpointer                user_data)
1569 {
1570   if ((make_sad && make_happy) || (!make_sad && !make_happy))
1571     {
1572       g_main_loop_quit (service_loop);
1573
1574       g_dbus_method_invocation_return_dbus_error (invocation,
1575                                                   "org.gtk.GDBus.Examples.ObjectManager.Error.Failed",
1576                                                   "Exactly one of make_sad or make_happy must be TRUE");
1577       goto out;
1578     }
1579
1580   if (make_sad)
1581     {
1582       if (g_strcmp0 (example_animal_get_mood (animal), "Sad") == 0)
1583         {
1584           g_dbus_method_invocation_return_dbus_error (invocation,
1585                                                       "org.gtk.GDBus.Examples.ObjectManager.Error.SadAnimalIsSad",
1586                                                       "Sad animal is already sad");
1587           goto out;
1588         }
1589
1590       example_animal_set_mood (animal, "Sad");
1591       example_animal_complete_poke (animal, invocation);
1592       goto out;
1593     }
1594
1595   if (make_happy)
1596     {
1597       if (g_strcmp0 (example_animal_get_mood (animal), "Happy") == 0)
1598         {
1599           g_dbus_method_invocation_return_dbus_error (invocation,
1600                                                       "org.gtk.GDBus.Examples.ObjectManager.Error.HappyAnimalIsHappy",
1601                                                       "Happy animal is already happy");
1602           goto out;
1603         }
1604
1605       example_animal_set_mood (animal, "Happy");
1606       example_animal_complete_poke (animal, invocation);
1607       goto out;
1608     }
1609
1610   g_assert_not_reached ();
1611
1612  out:
1613   return TRUE; /* to indicate that the method was handled */
1614 }
1615
1616 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1617 static gboolean
1618 codegen_on_new_connection (GDBusServer *server,
1619                            GDBusConnection *connection,
1620                            gpointer user_data)
1621 {
1622   ExampleAnimal *animal = user_data;
1623   GError        *error = NULL;
1624
1625   /* g_print ("Client connected.\n" */
1626   /*          "Negotiated capabilities: unix-fd-passing=%d\n", */
1627   /*          g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING); */
1628
1629   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (animal), connection,
1630                                     "/Example/Animals/000", &error);
1631   g_assert_no_error (error);
1632
1633   return TRUE;
1634 }
1635
1636 static gpointer
1637 codegen_service_thread_func (gpointer user_data)
1638 {
1639   GMainContext   *service_context;
1640   ExampleAnimal  *animal;
1641   GError         *error = NULL;
1642
1643   service_context = g_main_context_new ();
1644   g_main_context_push_thread_default (service_context);
1645
1646   /* Create the animal in the right thread context */
1647   animal = example_animal_skeleton_new ();
1648
1649   /* Handle Poke() D-Bus method invocations on the .Animal interface */
1650   g_signal_connect (animal, "handle-poke",
1651                     G_CALLBACK (codegen_on_animal_poke),
1652                     NULL); /* user_data */
1653
1654   codegen_server = g_dbus_server_new_sync (tmp_address,
1655                                            G_DBUS_SERVER_FLAGS_NONE,
1656                                            test_guid,
1657                                            NULL, /* observer */
1658                                            NULL, /* cancellable */
1659                                            &error);
1660   g_assert_no_error (error);
1661   g_dbus_server_start (codegen_server);
1662
1663   g_signal_connect (codegen_server, "new-connection",
1664                     G_CALLBACK (codegen_on_new_connection),
1665                     animal);
1666
1667   service_loop = g_main_loop_new (service_context, FALSE);
1668   g_main_loop_run (service_loop);
1669
1670   g_object_unref (animal);
1671
1672   g_main_context_pop_thread_default (service_context);
1673
1674   g_main_loop_unref (service_loop);
1675   g_main_context_unref (service_context);
1676
1677   g_dbus_server_stop (codegen_server);
1678   g_object_unref (codegen_server);
1679   codegen_server = NULL;
1680
1681   return NULL;
1682 }
1683
1684
1685 gboolean
1686 codegen_quit_mainloop_timeout (gpointer data)
1687 {
1688   g_main_loop_quit (loop);
1689   return FALSE;
1690 }
1691
1692 static void
1693 codegen_test_peer (void)
1694 {
1695   GDBusConnection     *connection;
1696   ExampleAnimal       *animal1, *animal2;
1697   GThread             *service_thread;
1698   GError              *error = NULL;
1699   GVariant            *value;
1700
1701   /* bring up a server - we run the server in a different thread to avoid deadlocks */
1702   service_loop = NULL;
1703   service_thread = g_thread_new ("codegen_test_peer",
1704                                  codegen_service_thread_func,
1705                                  NULL);
1706   while (service_loop == NULL)
1707     g_thread_yield ();
1708   g_assert (codegen_server != NULL);
1709
1710   /* Get an animal 1 ...  */
1711   connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (codegen_server),
1712                                                        G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1713                                                        NULL, /* GDBusAuthObserver */
1714                                                        NULL, /* cancellable */
1715                                                        &error);
1716   g_assert_no_error (error);
1717   g_assert (connection != NULL);
1718
1719   animal1 = example_animal_proxy_new_sync (connection, 0, NULL,
1720                                            "/Example/Animals/000", NULL, &error);
1721   g_assert_no_error (error);
1722   g_assert (animal1 != NULL);
1723   g_object_unref (connection);
1724
1725   /* Get animal 2 ...  */
1726   connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (codegen_server),
1727                                                        G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1728                                                        NULL, /* GDBusAuthObserver */
1729                                                        NULL, /* cancellable */
1730                                                        &error);
1731   g_assert_no_error (error);
1732   g_assert (connection != NULL);
1733
1734   animal2 = example_animal_proxy_new_sync (connection, 0, NULL,
1735                                            "/Example/Animals/000", NULL, &error);
1736   g_assert_no_error (error);
1737   g_assert (animal2 != NULL);
1738   g_object_unref (connection);
1739
1740   /* Make animal sad via animal1  */
1741   example_animal_call_poke_sync (animal1, TRUE, FALSE, 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 (animal1),
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 (100, codegen_quit_mainloop_timeout, NULL);
1755   g_main_loop_run (loop);
1756
1757   /* Assert animals are sad */
1758   g_assert_cmpstr (example_animal_get_mood (animal1), ==, "Sad");
1759   g_assert_cmpstr (example_animal_get_mood (animal2), ==, "Sad");
1760
1761   /* Make animal happy via animal2  */
1762   example_animal_call_poke_sync (animal2, FALSE, TRUE, NULL, &error);
1763   g_assert_no_error (error);
1764
1765   /* Poke server and make sure animal is updated */
1766   value = g_dbus_proxy_call_sync (G_DBUS_PROXY (animal2),
1767                                   "org.freedesktop.DBus.Peer.Ping",
1768                                   NULL, G_DBUS_CALL_FLAGS_NONE, -1,
1769                                   NULL, &error);
1770   g_assert_no_error (error);
1771   g_assert (value != NULL);
1772   g_variant_unref (value);
1773
1774   /* Give the proxies a chance to refresh in the defaul main loop */
1775   g_timeout_add (1000, codegen_quit_mainloop_timeout, NULL);
1776   g_main_loop_run (loop);
1777
1778   /* Assert animals are happy */
1779   g_assert_cmpstr (example_animal_get_mood (animal1), ==, "Happy");
1780   g_assert_cmpstr (example_animal_get_mood (animal2), ==, "Happy");
1781
1782   /* This final call making the animal happy and sad will cause
1783    * the server to quit, when the server quits we dont get property
1784    * change notifications anyway because those are done from an idle handler
1785    */
1786   example_animal_call_poke_sync (animal2, TRUE, TRUE, NULL, &error);
1787
1788   g_object_unref (animal1);
1789   g_object_unref (animal2);
1790   g_thread_join (service_thread);
1791 }
1792
1793 /* ---------------------------------------------------------------------------------------------------- */
1794
1795
1796 int
1797 main (int   argc,
1798       char *argv[])
1799 {
1800   gint ret;
1801   GDBusNodeInfo *introspection_data = NULL;
1802   gchar *tmpdir = NULL;
1803
1804   g_type_init ();
1805   g_test_init (&argc, &argv, NULL);
1806
1807   introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1808   g_assert (introspection_data != NULL);
1809   test_interface_introspection_data = introspection_data->interfaces[0];
1810
1811   test_guid = g_dbus_generate_guid ();
1812
1813   if (is_unix)
1814     {
1815       if (g_unix_socket_address_abstract_names_supported ())
1816         tmp_address = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
1817       else
1818         {
1819           tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
1820           tmp_address = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
1821         }
1822     }
1823   else
1824     tmp_address = g_strdup ("nonce-tcp:");
1825
1826   /* all the tests rely on a shared main loop */
1827   loop = g_main_loop_new (NULL, FALSE);
1828
1829   g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1830   g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1831   g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1832   g_test_add_func ("/gdbus/tcp-anonymous", test_tcp_anonymous);
1833   g_test_add_func ("/gdbus/credentials", test_credentials);
1834   g_test_add_func ("/gdbus/overflow", test_overflow);
1835   g_test_add_func ("/gdbus/codegen-peer-to-peer", codegen_test_peer);
1836
1837   ret = g_test_run();
1838
1839   g_main_loop_unref (loop);
1840   g_free (test_guid);
1841   g_dbus_node_info_unref (introspection_data);
1842   if (is_unix)
1843     g_free (tmp_address);
1844   if (tmpdir)
1845     {
1846       g_rmdir (tmpdir);
1847       g_free (tmpdir);
1848     }
1849
1850   return ret;
1851 }