Fix make check with builddir != srcdir
[platform/upstream/glib.git] / gio / tests / gdbus-connection-loss.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 #include "gdbus-tests.h"
28
29 static const gchar *binpath;
30
31 /* all tests rely on a global connection */
32 static GDBusConnection *c = NULL;
33
34 /* all tests rely on a shared mainloop */
35 static GMainLoop *loop = NULL;
36
37 /* ---------------------------------------------------------------------------------------------------- */
38 /* Check that pending calls fail with G_IO_ERROR_CLOSED if the connection is closed  */
39 /* See https://bugzilla.gnome.org/show_bug.cgi?id=660637 */
40 /* ---------------------------------------------------------------------------------------------------- */
41
42 static void
43 sleep_cb (GObject      *source_object,
44           GAsyncResult *res,
45           gpointer      user_data)
46 {
47   GError **error = user_data;
48   GVariant *result;
49
50   result = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), res, error);
51   g_assert (result == NULL);
52   g_main_loop_quit (loop);
53 }
54
55 static gboolean
56 on_timeout (gpointer user_data)
57 {
58   /* tear down bus */
59   session_bus_stop ();
60   return FALSE; /* remove source */
61 }
62
63 static void
64 test_connection_loss (void)
65 {
66   GDBusProxy *proxy;
67   GError *error;
68
69   error = NULL;
70   proxy = g_dbus_proxy_new_sync (c,
71                                  G_DBUS_PROXY_FLAGS_NONE,
72                                  NULL,                      /* GDBusInterfaceInfo */
73                                  "com.example.TestService", /* name */
74                                  "/com/example/TestObject", /* object path */
75                                  "com.example.Frob",        /* interface */
76                                  NULL, /* GCancellable */
77                                  &error);
78   g_assert_no_error (error);
79
80   error = NULL;
81   g_dbus_proxy_call (proxy,
82                      "Sleep",
83                      g_variant_new ("(i)", 100 * 1000 /* msec */),
84                      G_DBUS_CALL_FLAGS_NONE,
85                      10 * 1000 /* msec */,
86                      NULL, /* GCancellable */
87                      sleep_cb,
88                      &error);
89
90   /* Make sure we don't exit when the bus goes away */
91   g_dbus_connection_set_exit_on_close (c, FALSE);
92
93   /* Nuke the connection to the bus */
94   g_timeout_add (100 /* ms */, on_timeout, NULL);
95
96   g_main_loop_run (loop);
97
98   /* If we didn't act on connection-loss we'd be getting G_IO_ERROR_TIMEOUT
99    * generated locally. So if we get G_IO_ERROR_CLOSED it means that we
100    * are acting correctly on connection loss.
101    */
102   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
103   g_assert (!g_dbus_error_is_remote_error (error));
104   g_clear_error (&error);
105
106   g_object_unref (proxy);
107 }
108
109 /* ---------------------------------------------------------------------------------------------------- */
110
111 int
112 main (int   argc,
113       char *argv[])
114 {
115   GError *error;
116   gint ret;
117   gchar *path;
118
119   if (g_getenv ("G_TEST_DATA"))
120     binpath = g_getenv ("G_TEST_DATA");
121   else
122     binpath = BUILDDIR;
123
124   g_test_init (&argc, &argv, NULL);
125
126   /* all the tests rely on a shared main loop */
127   loop = g_main_loop_new (NULL, FALSE);
128
129   session_bus_up ();
130
131   /* this is safe; testserver will exit once the bus goes away */
132   path = g_build_filename (binpath, "gdbus-testserver", NULL);
133   g_assert (g_spawn_command_line_async (path, NULL));
134   g_free (path);
135
136   /* wait for the service to come up */
137   usleep (500 * 1000);
138
139   /* Create the connection in the main thread */
140   error = NULL;
141   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
142   g_assert_no_error (error);
143   g_assert (c != NULL);
144
145   g_test_add_func ("/gdbus/connection-loss", test_connection_loss);
146
147   ret = g_test_run();
148
149   g_object_unref (c);
150
151   return ret;
152 }