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