Merge remote-tracking branch 'gvdb/master'
[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_free_full (data->connections, g_object_unref);
930   g_free (data);
931 }
932
933 static void
934 dmp_on_method_call (GDBusConnection       *connection,
935                     const gchar           *sender,
936                     const gchar           *object_path,
937                     const gchar           *interface_name,
938                     const gchar           *method_name,
939                     GVariant              *parameters,
940                     GDBusMethodInvocation *invocation,
941                     gpointer               user_data)
942 {
943   //DmpData *data = user_data;
944   gint32 first;
945   gint32 second;
946   g_variant_get (parameters,
947                  "(ii)",
948                  &first,
949                  &second);
950   g_dbus_method_invocation_return_value (invocation,
951                                          g_variant_new ("(i)", first + second));
952 }
953
954 static const GDBusInterfaceVTable dmp_interface_vtable =
955 {
956   dmp_on_method_call,
957   NULL,  /* get_property */
958   NULL   /* set_property */
959 };
960
961
962 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
963 static gboolean
964 dmp_on_new_connection (GDBusServer     *server,
965                        GDBusConnection *connection,
966                        gpointer         user_data)
967 {
968   DmpData *data = user_data;
969   GDBusNodeInfo *node;
970   GError *error;
971
972   /* accept the connection */
973   data->connections = g_list_prepend (data->connections, g_object_ref (connection));
974
975   error = NULL;
976   node = g_dbus_node_info_new_for_xml ("<node>"
977                                        "  <interface name='org.gtk.GDBus.DmpInterface'>"
978                                        "    <method name='AddPair'>"
979                                        "      <arg type='i' name='first' direction='in'/>"
980                                        "      <arg type='i' name='second' direction='in'/>"
981                                        "      <arg type='i' name='sum' direction='out'/>"
982                                        "    </method>"
983                                        "  </interface>"
984                                        "</node>",
985                                        &error);
986   g_assert_no_error (error);
987
988   /* sleep 100ms before exporting an object - this is to test that
989    * G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
990    * (GDBusServer uses this feature).
991    */
992   usleep (100 * 1000);
993
994   /* export an object */
995   error = NULL;
996   g_dbus_connection_register_object (connection,
997                                      "/dmp/test",
998                                      node->interfaces[0],
999                                      &dmp_interface_vtable,
1000                                      data,
1001                                      NULL,
1002                                      &error);
1003   g_dbus_node_info_unref (node);
1004
1005   return TRUE;
1006 }
1007
1008 static gpointer
1009 dmp_thread_func (gpointer user_data)
1010 {
1011   DmpData *data = user_data;
1012   GError *error;
1013   gchar *guid;
1014
1015   data->context = g_main_context_new ();
1016   g_main_context_push_thread_default (data->context);
1017
1018   error = NULL;
1019   guid = g_dbus_generate_guid ();
1020   data->server = g_dbus_server_new_sync (tmp_address,
1021                                          G_DBUS_SERVER_FLAGS_NONE,
1022                                          guid,
1023                                          NULL, /* GDBusAuthObserver */
1024                                          NULL, /* GCancellable */
1025                                          &error);
1026   g_assert_no_error (error);
1027   g_signal_connect (data->server,
1028                     "new-connection",
1029                     G_CALLBACK (dmp_on_new_connection),
1030                     data);
1031
1032   g_dbus_server_start (data->server);
1033
1034   data->loop = g_main_loop_new (data->context, FALSE);
1035   g_main_loop_run (data->loop);
1036
1037   g_main_context_pop_thread_default (data->context);
1038
1039   g_free (guid);
1040   return NULL;
1041 }
1042
1043 static void
1044 delayed_message_processing (void)
1045 {
1046   GError *error;
1047   DmpData *data;
1048   GThread *service_thread;
1049   guint n;
1050
1051   data = g_new0 (DmpData, 1);
1052
1053   service_thread = g_thread_new ("dmp",
1054                                  dmp_thread_func,
1055                                  data);
1056   while (data->server == NULL || !g_dbus_server_is_active (data->server))
1057     g_thread_yield ();
1058
1059   for (n = 0; n < 5; n++)
1060     {
1061       GDBusConnection *c;
1062       GVariant *res;
1063       gint32 val;
1064
1065       error = NULL;
1066       c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (data->server),
1067                                                   G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1068                                                   NULL, /* GDBusAuthObserver */
1069                                                   NULL, /* GCancellable */
1070                                                   &error);
1071       g_assert_no_error (error);
1072
1073       error = NULL;
1074       res = g_dbus_connection_call_sync (c,
1075                                          NULL,    /* bus name */
1076                                          "/dmp/test",
1077                                          "org.gtk.GDBus.DmpInterface",
1078                                          "AddPair",
1079                                          g_variant_new ("(ii)", 2, n),
1080                                          G_VARIANT_TYPE ("(i)"),
1081                                          G_DBUS_CALL_FLAGS_NONE,
1082                                          -1, /* timeout_msec */
1083                                          NULL, /* GCancellable */
1084                                          &error);
1085       g_assert_no_error (error);
1086       g_variant_get (res, "(i)", &val);
1087       g_assert_cmpint (val, ==, 2 + n);
1088       g_variant_unref (res);
1089       g_object_unref (c);
1090   }
1091
1092   g_main_loop_quit (data->loop);
1093   g_thread_join (service_thread);
1094   dmp_data_free (data);
1095 }
1096
1097 /* ---------------------------------------------------------------------------------------------------- */
1098
1099 static gboolean
1100 nonce_tcp_on_authorize_authenticated_peer (GDBusAuthObserver *observer,
1101                                            GIOStream         *stream,
1102                                            GCredentials      *credentials,
1103                                            gpointer           user_data)
1104 {
1105   PeerData *data = user_data;
1106   gboolean authorized;
1107
1108   data->num_connection_attempts++;
1109
1110   authorized = TRUE;
1111   if (!data->accept_connection)
1112     {
1113       authorized = FALSE;
1114       g_main_loop_quit (loop);
1115     }
1116
1117   return authorized;
1118 }
1119
1120 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1121 static gboolean
1122 nonce_tcp_on_new_connection (GDBusServer *server,
1123                              GDBusConnection *connection,
1124                              gpointer user_data)
1125 {
1126   PeerData *data = user_data;
1127
1128   g_ptr_array_add (data->current_connections, g_object_ref (connection));
1129
1130   g_main_loop_quit (loop);
1131
1132   return TRUE;
1133 }
1134
1135 static gpointer
1136 nonce_tcp_service_thread_func (gpointer user_data)
1137 {
1138   PeerData *data = user_data;
1139   GMainContext *service_context;
1140   GDBusAuthObserver *observer;
1141   GError *error;
1142
1143   service_context = g_main_context_new ();
1144   g_main_context_push_thread_default (service_context);
1145
1146   error = NULL;
1147   observer = g_dbus_auth_observer_new ();
1148   server = g_dbus_server_new_sync ("nonce-tcp:",
1149                                    G_DBUS_SERVER_FLAGS_NONE,
1150                                    test_guid,
1151                                    observer,
1152                                    NULL, /* cancellable */
1153                                    &error);
1154   g_assert_no_error (error);
1155
1156   g_signal_connect (server,
1157                     "new-connection",
1158                     G_CALLBACK (nonce_tcp_on_new_connection),
1159                     data);
1160   g_signal_connect (observer,
1161                     "authorize-authenticated-peer",
1162                     G_CALLBACK (nonce_tcp_on_authorize_authenticated_peer),
1163                     data);
1164   g_object_unref (observer);
1165
1166   g_dbus_server_start (server);
1167
1168   service_loop = g_main_loop_new (service_context, FALSE);
1169   g_main_loop_run (service_loop);
1170
1171   g_main_context_pop_thread_default (service_context);
1172
1173   g_main_loop_unref (service_loop);
1174   g_main_context_unref (service_context);
1175
1176   /* test code specifically unrefs the server - see below */
1177   g_assert (server == NULL);
1178
1179   return NULL;
1180 }
1181
1182 static void
1183 test_nonce_tcp (void)
1184 {
1185   PeerData data;
1186   GError *error;
1187   GThread *service_thread;
1188   GDBusConnection *c;
1189   gchar *s;
1190   gchar *nonce_file;
1191   gboolean res;
1192   const gchar *address;
1193
1194   memset (&data, '\0', sizeof (PeerData));
1195   data.current_connections = g_ptr_array_new_with_free_func (g_object_unref);
1196
1197   error = NULL;
1198   server = NULL;
1199   service_loop = NULL;
1200   service_thread = g_thread_new ("nonce-tcp-service",
1201                                  nonce_tcp_service_thread_func,
1202                                  &data);
1203   while (service_loop == NULL)
1204     g_thread_yield ();
1205   g_assert (server != NULL);
1206
1207
1208   /* bring up a connection and accept it */
1209   data.accept_connection = TRUE;
1210   error = NULL;
1211   c = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1212                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1213                                               NULL, /* GDBusAuthObserver */
1214                                               NULL, /* cancellable */
1215                                               &error);
1216   g_assert_no_error (error);
1217   g_assert (c != NULL);
1218   while (data.current_connections->len < 1)
1219     g_thread_yield ();
1220   g_assert_cmpint (data.current_connections->len, ==, 1);
1221   g_assert_cmpint (data.num_connection_attempts, ==, 1);
1222   g_assert (g_dbus_connection_get_unique_name (c) == NULL);
1223   g_assert_cmpstr (g_dbus_connection_get_guid (c), ==, test_guid);
1224   g_object_unref (c);
1225
1226   /* now, try to subvert the nonce file (this assumes noncefile is the last key/value pair)
1227    */
1228
1229   address = g_dbus_server_get_client_address (server);
1230
1231   s = strstr (address, "noncefile=");
1232   g_assert (s != NULL);
1233   s += sizeof "noncefile=" - 1;
1234   nonce_file = g_strdup (s);
1235
1236   /* First try invalid data in the nonce file - this will actually
1237    * make the client send this and the server will reject it. The way
1238    * it works is that if the nonce doesn't match, the server will
1239    * simply close the connection. So, from the client point of view,
1240    * we can see a variety of errors.
1241    */
1242   error = NULL;
1243   res = g_file_set_contents (nonce_file,
1244                              "0123456789012345",
1245                              -1,
1246                              &error);
1247   g_assert_no_error (error);
1248   g_assert (res);
1249   c = g_dbus_connection_new_for_address_sync (address,
1250                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1251                                               NULL, /* GDBusAuthObserver */
1252                                               NULL, /* cancellable */
1253                                               &error);
1254   _g_assert_error_domain (error, G_IO_ERROR);
1255   g_error_free (error);
1256   g_assert (c == NULL);
1257
1258   /* Then try with a nonce-file of incorrect length - this will make
1259    * the client complain - we won't even try connecting to the server
1260    * for this
1261    */
1262   error = NULL;
1263   res = g_file_set_contents (nonce_file,
1264                              "0123456789012345_",
1265                              -1,
1266                              &error);
1267   g_assert_no_error (error);
1268   g_assert (res);
1269   c = g_dbus_connection_new_for_address_sync (address,
1270                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1271                                               NULL, /* GDBusAuthObserver */
1272                                               NULL, /* cancellable */
1273                                               &error);
1274   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1275   g_error_free (error);
1276   g_assert (c == NULL);
1277
1278   /* Finally try with no nonce-file at all */
1279   g_assert_cmpint (g_unlink (nonce_file), ==, 0);
1280   error = NULL;
1281   c = g_dbus_connection_new_for_address_sync (address,
1282                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1283                                               NULL, /* GDBusAuthObserver */
1284                                               NULL, /* cancellable */
1285                                               &error);
1286   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
1287   g_error_free (error);
1288   g_assert (c == NULL);
1289
1290   g_free (nonce_file);
1291
1292   g_dbus_server_stop (server);
1293   g_object_unref (server);
1294   server = NULL;
1295
1296   g_main_loop_quit (service_loop);
1297   g_thread_join (service_thread);
1298 }
1299
1300 static void
1301 test_credentials (void)
1302 {
1303   GCredentials *c1, *c2;
1304   GError *error;
1305   gchar *desc;
1306
1307   c1 = g_credentials_new ();
1308   c2 = g_credentials_new ();
1309
1310   error = NULL;
1311   if (g_credentials_set_unix_user (c2, getuid (), &error))
1312     g_assert_no_error (error);
1313
1314   g_clear_error (&error);
1315   g_assert (g_credentials_is_same_user (c1, c2, &error));
1316   g_assert_no_error (error);
1317
1318   desc = g_credentials_to_string (c1);
1319   g_assert (desc != NULL);
1320   g_free (desc);
1321
1322   g_object_unref (c1);
1323   g_object_unref (c2);
1324 }
1325
1326 /* ---------------------------------------------------------------------------------------------------- */
1327
1328 #ifdef G_OS_UNIX
1329
1330 /* Chosen to be big enough to overflow the socket buffer */
1331 #define OVERFLOW_NUM_SIGNALS 5000
1332 #define OVERFLOW_TIMEOUT_SEC 10
1333
1334 static GDBusMessage *
1335 overflow_filter_func (GDBusConnection *connection,
1336                       GDBusMessage    *message,
1337                       gboolean         incoming,
1338                       gpointer         user_data)
1339 {
1340   volatile gint *counter = user_data;
1341   *counter += 1;
1342   return message;
1343 }
1344
1345 static gboolean
1346 overflow_on_500ms_later_func (gpointer user_data)
1347 {
1348   g_main_loop_quit (loop);
1349   return FALSE; /* don't keep the idle */
1350 }
1351
1352 static void
1353 test_overflow (void)
1354 {
1355   gint sv[2];
1356   gint n;
1357   GSocket *socket;
1358   GSocketConnection *socket_connection;
1359   GDBusConnection *producer, *consumer;
1360   GError *error;
1361   GTimer *timer;
1362   volatile gint n_messages_received;
1363   volatile gint n_messages_sent;
1364
1365   g_assert_cmpint (socketpair (AF_UNIX, SOCK_STREAM, 0, sv), ==, 0);
1366
1367   error = NULL;
1368   socket = g_socket_new_from_fd (sv[0], &error);
1369   g_assert_no_error (error);
1370   socket_connection = g_socket_connection_factory_create_connection (socket);
1371   g_assert (socket_connection != NULL);
1372   g_object_unref (socket);
1373   producer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1374                                          NULL, /* guid */
1375                                          G_DBUS_CONNECTION_FLAGS_NONE,
1376                                          NULL, /* GDBusAuthObserver */
1377                                          NULL, /* GCancellable */
1378                                          &error);
1379   g_dbus_connection_set_exit_on_close (producer, TRUE);
1380   g_assert_no_error (error);
1381   g_object_unref (socket_connection);
1382   n_messages_sent = 0;
1383   g_dbus_connection_add_filter (producer, overflow_filter_func, (gpointer) &n_messages_sent, NULL);
1384
1385   /* send enough data that we get an EAGAIN */
1386   for (n = 0; n < OVERFLOW_NUM_SIGNALS; n++)
1387     {
1388       error = NULL;
1389       g_dbus_connection_emit_signal (producer,
1390                                      NULL, /* destination */
1391                                      "/org/foo/Object",
1392                                      "org.foo.Interface",
1393                                      "Member",
1394                                      g_variant_new ("(s)", "a string"),
1395                                      &error);
1396       g_assert_no_error (error);
1397     }
1398
1399   /* sleep for 0.5 sec (to allow the GDBus IO thread to fill up the
1400    * kernel buffers) and verify that n_messages_sent <
1401    * OVERFLOW_NUM_SIGNALS
1402    *
1403    * This is to verify that not all the submitted messages have been
1404    * sent to the underlying transport.
1405    */
1406   g_timeout_add (500, overflow_on_500ms_later_func, NULL);
1407   g_main_loop_run (loop);
1408   g_assert_cmpint (n_messages_sent, <, OVERFLOW_NUM_SIGNALS);
1409
1410   /* now suck it all out as a client, and add it up */
1411   socket = g_socket_new_from_fd (sv[1], &error);
1412   g_assert_no_error (error);
1413   socket_connection = g_socket_connection_factory_create_connection (socket);
1414   g_assert (socket_connection != NULL);
1415   g_object_unref (socket);
1416   consumer = g_dbus_connection_new_sync (G_IO_STREAM (socket_connection),
1417                                          NULL, /* guid */
1418                                          G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING,
1419                                          NULL, /* GDBusAuthObserver */
1420                                          NULL, /* GCancellable */
1421                                          &error);
1422   g_assert_no_error (error);
1423   g_object_unref (socket_connection);
1424   n_messages_received = 0;
1425   g_dbus_connection_add_filter (consumer, overflow_filter_func, (gpointer) &n_messages_received, NULL);
1426   g_dbus_connection_start_message_processing (consumer);
1427
1428   timer = g_timer_new ();
1429   g_timer_start (timer);
1430
1431   while (n_messages_received < OVERFLOW_NUM_SIGNALS && g_timer_elapsed (timer, NULL) < OVERFLOW_TIMEOUT_SEC)
1432       g_main_context_iteration (NULL, FALSE);
1433
1434   g_assert_cmpint (n_messages_sent, ==, OVERFLOW_NUM_SIGNALS);
1435   g_assert_cmpint (n_messages_received, ==, OVERFLOW_NUM_SIGNALS);
1436
1437   g_timer_destroy (timer);
1438   g_object_unref (consumer);
1439   g_object_unref (producer);
1440 }
1441 #else
1442 static void
1443 test_overflow (void)
1444 {
1445   /* TODO: test this with e.g. GWin32InputStream/GWin32OutputStream */
1446 }
1447 #endif
1448
1449 /* ---------------------------------------------------------------------------------------------------- */
1450
1451 static gboolean
1452 tcp_anonymous_on_new_connection (GDBusServer     *server,
1453                                  GDBusConnection *connection,
1454                                  gpointer         user_data)
1455 {
1456   gboolean *seen_connection = user_data;
1457   *seen_connection = TRUE;
1458   return TRUE;
1459 }
1460
1461 static gpointer
1462 tcp_anonymous_service_thread_func (gpointer user_data)
1463 {
1464   gboolean *seen_connection = user_data;
1465   GMainContext *service_context;
1466   GError *error;
1467
1468   service_context = g_main_context_new ();
1469   g_main_context_push_thread_default (service_context);
1470
1471   error = NULL;
1472   server = g_dbus_server_new_sync ("tcp:",
1473                                    G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS,
1474                                    test_guid,
1475                                    NULL, /* GDBusObserver* */
1476                                    NULL, /* GCancellable* */
1477                                    &error);
1478   g_assert_no_error (error);
1479
1480   g_signal_connect (server,
1481                     "new-connection",
1482                     G_CALLBACK (tcp_anonymous_on_new_connection),
1483                     seen_connection);
1484
1485   g_dbus_server_start (server);
1486
1487   service_loop = g_main_loop_new (service_context, FALSE);
1488   g_main_loop_run (service_loop);
1489
1490   g_main_context_pop_thread_default (service_context);
1491
1492   g_main_loop_unref (service_loop);
1493   g_main_context_unref (service_context);
1494
1495   return NULL;
1496 }
1497
1498 static void
1499 test_tcp_anonymous (void)
1500 {
1501   gboolean seen_connection;
1502   GThread *service_thread;
1503   GDBusConnection *connection;
1504   GError *error;
1505
1506   seen_connection = FALSE;
1507   service_loop = NULL;
1508   service_thread = g_thread_new ("tcp-anon-service",
1509                                  tcp_anonymous_service_thread_func,
1510                                  &seen_connection);
1511   while (service_loop == NULL)
1512     g_thread_yield ();
1513   g_assert (server != NULL);
1514
1515   error = NULL;
1516   connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (server),
1517                                                        G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1518                                                        NULL, /* GDBusAuthObserver* */
1519                                                        NULL, /* GCancellable */
1520                                                        &error);
1521   g_assert_no_error (error);
1522   g_assert (connection != NULL);
1523
1524   while (!seen_connection)
1525     g_thread_yield ();
1526
1527   g_object_unref (connection);
1528
1529   g_main_loop_quit (service_loop);
1530   g_dbus_server_stop (server);
1531   g_object_unref (server);
1532   server = NULL;
1533
1534   g_thread_join (service_thread);
1535 }
1536
1537 /* ---------------------------------------------------------------------------------------------------- */
1538
1539 static GDBusServer *codegen_server = NULL;
1540
1541 static gboolean
1542 codegen_on_animal_poke (ExampleAnimal          *animal,
1543                         GDBusMethodInvocation  *invocation,
1544                         gboolean                make_sad,
1545                         gboolean                make_happy,
1546                         gpointer                user_data)
1547 {
1548   if ((make_sad && make_happy) || (!make_sad && !make_happy))
1549     {
1550       g_main_loop_quit (service_loop);
1551
1552       g_dbus_method_invocation_return_dbus_error (invocation,
1553                                                   "org.gtk.GDBus.Examples.ObjectManager.Error.Failed",
1554                                                   "Exactly one of make_sad or make_happy must be TRUE");
1555       goto out;
1556     }
1557
1558   if (make_sad)
1559     {
1560       if (g_strcmp0 (example_animal_get_mood (animal), "Sad") == 0)
1561         {
1562           g_dbus_method_invocation_return_dbus_error (invocation,
1563                                                       "org.gtk.GDBus.Examples.ObjectManager.Error.SadAnimalIsSad",
1564                                                       "Sad animal is already sad");
1565           goto out;
1566         }
1567
1568       example_animal_set_mood (animal, "Sad");
1569       example_animal_complete_poke (animal, invocation);
1570       goto out;
1571     }
1572
1573   if (make_happy)
1574     {
1575       if (g_strcmp0 (example_animal_get_mood (animal), "Happy") == 0)
1576         {
1577           g_dbus_method_invocation_return_dbus_error (invocation,
1578                                                       "org.gtk.GDBus.Examples.ObjectManager.Error.HappyAnimalIsHappy",
1579                                                       "Happy animal is already happy");
1580           goto out;
1581         }
1582
1583       example_animal_set_mood (animal, "Happy");
1584       example_animal_complete_poke (animal, invocation);
1585       goto out;
1586     }
1587
1588   g_assert_not_reached ();
1589
1590  out:
1591   return TRUE; /* to indicate that the method was handled */
1592 }
1593
1594 /* Runs in thread we created GDBusServer in (since we didn't pass G_DBUS_SERVER_FLAGS_RUN_IN_THREAD) */
1595 static gboolean
1596 codegen_on_new_connection (GDBusServer *server,
1597                            GDBusConnection *connection,
1598                            gpointer user_data)
1599 {
1600   ExampleAnimal *animal = user_data;
1601   GError        *error = NULL;
1602
1603   /* g_print ("Client connected.\n" */
1604   /*          "Negotiated capabilities: unix-fd-passing=%d\n", */
1605   /*          g_dbus_connection_get_capabilities (connection) & G_DBUS_CAPABILITY_FLAGS_UNIX_FD_PASSING); */
1606
1607   g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (animal), connection,
1608                                     "/Example/Animals/000", &error);
1609   g_assert_no_error (error);
1610
1611   return TRUE;
1612 }
1613
1614 static gpointer
1615 codegen_service_thread_func (gpointer user_data)
1616 {
1617   GMainContext   *service_context;
1618   ExampleAnimal  *animal;
1619   GError         *error = NULL;
1620
1621   service_context = g_main_context_new ();
1622   g_main_context_push_thread_default (service_context);
1623
1624   /* Create the animal in the right thread context */
1625   animal = example_animal_skeleton_new ();
1626
1627   /* Handle Poke() D-Bus method invocations on the .Animal interface */
1628   g_signal_connect (animal, "handle-poke",
1629                     G_CALLBACK (codegen_on_animal_poke),
1630                     NULL); /* user_data */
1631
1632   codegen_server = g_dbus_server_new_sync (tmp_address,
1633                                            G_DBUS_SERVER_FLAGS_NONE,
1634                                            test_guid,
1635                                            NULL, /* observer */
1636                                            NULL, /* cancellable */
1637                                            &error);
1638   g_assert_no_error (error);
1639   g_dbus_server_start (codegen_server);
1640
1641   g_signal_connect (codegen_server, "new-connection",
1642                     G_CALLBACK (codegen_on_new_connection),
1643                     animal);
1644
1645   service_loop = g_main_loop_new (service_context, FALSE);
1646   g_main_loop_run (service_loop);
1647
1648   g_object_unref (animal);
1649
1650   g_main_context_pop_thread_default (service_context);
1651
1652   g_main_loop_unref (service_loop);
1653   g_main_context_unref (service_context);
1654
1655   g_dbus_server_stop (codegen_server);
1656   g_object_unref (codegen_server);
1657   codegen_server = NULL;
1658
1659   return NULL;
1660 }
1661
1662
1663 gboolean
1664 codegen_quit_mainloop_timeout (gpointer data)
1665 {
1666   g_main_loop_quit (loop);
1667   return FALSE;
1668 }
1669
1670 static void
1671 codegen_test_peer (void)
1672 {
1673   GDBusConnection     *connection;
1674   ExampleAnimal       *animal1, *animal2;
1675   GThread             *service_thread;
1676   GError              *error = NULL;
1677   GVariant            *value;
1678
1679   /* bring up a server - we run the server in a different thread to avoid deadlocks */
1680   service_loop = NULL;
1681   service_thread = g_thread_new ("codegen_test_peer",
1682                                  codegen_service_thread_func,
1683                                  NULL);
1684   while (service_loop == NULL)
1685     g_thread_yield ();
1686   g_assert (codegen_server != NULL);
1687
1688   /* Get an animal 1 ...  */
1689   connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (codegen_server),
1690                                                        G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1691                                                        NULL, /* GDBusAuthObserver */
1692                                                        NULL, /* cancellable */
1693                                                        &error);
1694   g_assert_no_error (error);
1695   g_assert (connection != NULL);
1696
1697   animal1 = example_animal_proxy_new_sync (connection, 0, NULL,
1698                                            "/Example/Animals/000", NULL, &error);
1699   g_assert_no_error (error);
1700   g_assert (animal1 != NULL);
1701   g_object_unref (connection);
1702
1703   /* Get animal 2 ...  */
1704   connection = g_dbus_connection_new_for_address_sync (g_dbus_server_get_client_address (codegen_server),
1705                                                        G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
1706                                                        NULL, /* GDBusAuthObserver */
1707                                                        NULL, /* cancellable */
1708                                                        &error);
1709   g_assert_no_error (error);
1710   g_assert (connection != NULL);
1711
1712   animal2 = example_animal_proxy_new_sync (connection, 0, NULL,
1713                                            "/Example/Animals/000", NULL, &error);
1714   g_assert_no_error (error);
1715   g_assert (animal2 != NULL);
1716   g_object_unref (connection);
1717
1718   /* Make animal sad via animal1  */
1719   example_animal_call_poke_sync (animal1, TRUE, FALSE, NULL, &error);
1720   g_assert_no_error (error);
1721
1722   /* Poke server and make sure animal is updated */
1723   value = g_dbus_proxy_call_sync (G_DBUS_PROXY (animal1),
1724                                   "org.freedesktop.DBus.Peer.Ping",
1725                                   NULL, G_DBUS_CALL_FLAGS_NONE, -1,
1726                                   NULL, &error);
1727   g_assert_no_error (error);
1728   g_assert (value != NULL);
1729   g_variant_unref (value);
1730
1731   /* Give the proxies a chance to refresh in the defaul main loop */
1732   g_timeout_add (100, codegen_quit_mainloop_timeout, NULL);
1733   g_main_loop_run (loop);
1734
1735   /* Assert animals are sad */
1736   g_assert_cmpstr (example_animal_get_mood (animal1), ==, "Sad");
1737   g_assert_cmpstr (example_animal_get_mood (animal2), ==, "Sad");
1738
1739   /* Make animal happy via animal2  */
1740   example_animal_call_poke_sync (animal2, FALSE, TRUE, NULL, &error);
1741   g_assert_no_error (error);
1742
1743   /* Poke server and make sure animal is updated */
1744   value = g_dbus_proxy_call_sync (G_DBUS_PROXY (animal2),
1745                                   "org.freedesktop.DBus.Peer.Ping",
1746                                   NULL, G_DBUS_CALL_FLAGS_NONE, -1,
1747                                   NULL, &error);
1748   g_assert_no_error (error);
1749   g_assert (value != NULL);
1750   g_variant_unref (value);
1751
1752   /* Give the proxies a chance to refresh in the defaul main loop */
1753   g_timeout_add (1000, codegen_quit_mainloop_timeout, NULL);
1754   g_main_loop_run (loop);
1755
1756   /* Assert animals are happy */
1757   g_assert_cmpstr (example_animal_get_mood (animal1), ==, "Happy");
1758   g_assert_cmpstr (example_animal_get_mood (animal2), ==, "Happy");
1759
1760   /* This final call making the animal happy and sad will cause
1761    * the server to quit, when the server quits we dont get property
1762    * change notifications anyway because those are done from an idle handler
1763    */
1764   example_animal_call_poke_sync (animal2, TRUE, TRUE, NULL, &error);
1765
1766   g_object_unref (animal1);
1767   g_object_unref (animal2);
1768   g_thread_join (service_thread);
1769 }
1770
1771 /* ---------------------------------------------------------------------------------------------------- */
1772
1773
1774 int
1775 main (int   argc,
1776       char *argv[])
1777 {
1778   gint ret;
1779   GDBusNodeInfo *introspection_data = NULL;
1780   gchar *tmpdir = NULL;
1781
1782   g_type_init ();
1783   g_test_init (&argc, &argv, NULL);
1784
1785   g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
1786
1787   introspection_data = g_dbus_node_info_new_for_xml (test_interface_introspection_xml, NULL);
1788   g_assert (introspection_data != NULL);
1789   test_interface_introspection_data = introspection_data->interfaces[0];
1790
1791   test_guid = g_dbus_generate_guid ();
1792
1793   if (is_unix)
1794     {
1795       if (g_unix_socket_address_abstract_names_supported ())
1796         tmp_address = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
1797       else
1798         {
1799           tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
1800           tmp_address = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
1801         }
1802     }
1803   else
1804     tmp_address = g_strdup ("nonce-tcp:");
1805
1806   /* all the tests rely on a shared main loop */
1807   loop = g_main_loop_new (NULL, FALSE);
1808
1809   g_test_add_func ("/gdbus/peer-to-peer", test_peer);
1810   g_test_add_func ("/gdbus/delayed-message-processing", delayed_message_processing);
1811   g_test_add_func ("/gdbus/nonce-tcp", test_nonce_tcp);
1812   g_test_add_func ("/gdbus/tcp-anonymous", test_tcp_anonymous);
1813   g_test_add_func ("/gdbus/credentials", test_credentials);
1814   g_test_add_func ("/gdbus/overflow", test_overflow);
1815   g_test_add_func ("/gdbus/codegen-peer-to-peer", codegen_test_peer);
1816
1817   ret = g_test_run();
1818
1819   g_main_loop_unref (loop);
1820   g_free (test_guid);
1821   g_dbus_node_info_unref (introspection_data);
1822   if (is_unix)
1823     g_free (tmp_address);
1824   if (tmpdir)
1825     {
1826       g_rmdir (tmpdir);
1827       g_free (tmpdir);
1828     }
1829
1830   return ret;
1831 }