(root)/
glib-2.79.0/
gio/
tests/
gdbus-connection-loss.c
       1  /* GLib testing framework examples and tests
       2   *
       3   * Copyright (C) 2008-2010 Red Hat, Inc.
       4   *
       5   * SPDX-License-Identifier: LGPL-2.1-or-later
       6   *
       7   * This library is free software; you can redistribute it and/or
       8   * modify it under the terms of the GNU Lesser General Public
       9   * License as published by the Free Software Foundation; either
      10   * version 2.1 of the License, or (at your option) any later version.
      11   *
      12   * This library is distributed in the hope that it will be useful,
      13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15   * Lesser General Public License for more details.
      16   *
      17   * You should have received a copy of the GNU Lesser General
      18   * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      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  /* all tests rely on a global connection */
      30  static GDBusConnection *c = NULL;
      31  
      32  /* all tests rely on a shared mainloop */
      33  static GMainLoop *loop = NULL;
      34  
      35  /* ---------------------------------------------------------------------------------------------------- */
      36  /* Check that pending calls fail with G_IO_ERROR_CLOSED if the connection is closed  */
      37  /* See https://bugzilla.gnome.org/show_bug.cgi?id=660637 */
      38  /* ---------------------------------------------------------------------------------------------------- */
      39  
      40  static void
      41  sleep_cb (GObject      *source_object,
      42            GAsyncResult *res,
      43            gpointer      user_data)
      44  {
      45    GError **error = user_data;
      46    GVariant *result;
      47  
      48    result = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), res, error);
      49    g_assert (result == NULL);
      50    g_main_loop_quit (loop);
      51  }
      52  
      53  static gboolean
      54  on_timeout (gpointer user_data)
      55  {
      56    /* tear down bus */
      57    session_bus_stop ();
      58    return G_SOURCE_REMOVE;
      59  }
      60  
      61  static void
      62  test_connection_loss (void)
      63  {
      64    GDBusProxy *proxy;
      65    GError *error;
      66  
      67    error = NULL;
      68    proxy = g_dbus_proxy_new_sync (c,
      69                                   G_DBUS_PROXY_FLAGS_NONE,
      70                                   NULL,                      /* GDBusInterfaceInfo */
      71                                   "com.example.TestService", /* name */
      72                                   "/com/example/TestObject", /* object path */
      73                                   "com.example.Frob",        /* interface */
      74                                   NULL, /* GCancellable */
      75                                   &error);
      76    g_assert_no_error (error);
      77  
      78    error = NULL;
      79    g_dbus_proxy_call (proxy,
      80                       "Sleep",
      81                       g_variant_new ("(i)", 100 * 1000 /* msec */),
      82                       G_DBUS_CALL_FLAGS_NONE,
      83                       10 * 1000 /* msec */,
      84                       NULL, /* GCancellable */
      85                       sleep_cb,
      86                       &error);
      87  
      88    /* Make sure we don't exit when the bus goes away */
      89    g_dbus_connection_set_exit_on_close (c, FALSE);
      90  
      91    /* Nuke the connection to the bus */
      92    g_timeout_add (100 /* ms */, on_timeout, NULL);
      93  
      94    g_main_loop_run (loop);
      95  
      96    /* If we didn't act on connection-loss we'd be getting G_IO_ERROR_TIMEOUT
      97     * generated locally. So if we get G_IO_ERROR_CLOSED it means that we
      98     * are acting correctly on connection loss.
      99     */
     100    g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
     101    g_assert (!g_dbus_error_is_remote_error (error));
     102    g_clear_error (&error);
     103  
     104    g_object_unref (proxy);
     105  }
     106  
     107  /* ---------------------------------------------------------------------------------------------------- */
     108  
     109  int
     110  main (int   argc,
     111        char *argv[])
     112  {
     113    GError *error;
     114    gint ret;
     115    gchar *path;
     116  
     117    g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
     118  
     119    /* all the tests rely on a shared main loop */
     120    loop = g_main_loop_new (NULL, FALSE);
     121  
     122    session_bus_up ();
     123  
     124    /* this is safe; testserver will exit once the bus goes away */
     125    path = g_test_build_filename (G_TEST_BUILT, "gdbus-testserver", NULL);
     126    g_assert (g_spawn_command_line_async (path, NULL));
     127    g_free (path);
     128  
     129    /* Create the connection in the main thread */
     130    error = NULL;
     131    c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
     132    g_assert_no_error (error);
     133    g_assert (c != NULL);
     134  
     135    ensure_gdbus_testserver_up (c, NULL);
     136  
     137    g_test_add_func ("/gdbus/connection-loss", test_connection_loss);
     138  
     139    ret = g_test_run();
     140  
     141    g_object_unref (c);
     142  
     143    session_bus_down ();
     144  
     145    g_main_loop_unref (loop);
     146  
     147    return ret;
     148  }