Blame gio/tests/gdbus-connection-loss.c

Packit ae235b
/* GLib testing framework examples and tests
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2008-2010 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: David Zeuthen <davidz@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include <gio/gio.h>
Packit ae235b
#include <unistd.h>
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
#include "gdbus-tests.h"
Packit ae235b
Packit ae235b
/* all tests rely on a global connection */
Packit ae235b
static GDBusConnection *c = NULL;
Packit ae235b
Packit ae235b
/* all tests rely on a shared mainloop */
Packit ae235b
static GMainLoop *loop = NULL;
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
/* Check that pending calls fail with G_IO_ERROR_CLOSED if the connection is closed  */
Packit ae235b
/* See https://bugzilla.gnome.org/show_bug.cgi?id=660637 */
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static void
Packit ae235b
sleep_cb (GObject      *source_object,
Packit ae235b
          GAsyncResult *res,
Packit ae235b
          gpointer      user_data)
Packit ae235b
{
Packit ae235b
  GError **error = user_data;
Packit ae235b
  GVariant *result;
Packit ae235b
Packit ae235b
  result = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object), res, error);
Packit ae235b
  g_assert (result == NULL);
Packit ae235b
  g_main_loop_quit (loop);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
on_timeout (gpointer user_data)
Packit ae235b
{
Packit ae235b
  /* tear down bus */
Packit ae235b
  session_bus_stop ();
Packit ae235b
  return FALSE; /* remove source */
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_connection_loss (void)
Packit ae235b
{
Packit ae235b
  GDBusProxy *proxy;
Packit ae235b
  GError *error;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  proxy = g_dbus_proxy_new_sync (c,
Packit ae235b
                                 G_DBUS_PROXY_FLAGS_NONE,
Packit ae235b
                                 NULL,                      /* GDBusInterfaceInfo */
Packit ae235b
                                 "com.example.TestService", /* name */
Packit ae235b
                                 "/com/example/TestObject", /* object path */
Packit ae235b
                                 "com.example.Frob",        /* interface */
Packit ae235b
                                 NULL, /* GCancellable */
Packit ae235b
                                 &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  g_dbus_proxy_call (proxy,
Packit ae235b
                     "Sleep",
Packit ae235b
                     g_variant_new ("(i)", 100 * 1000 /* msec */),
Packit ae235b
                     G_DBUS_CALL_FLAGS_NONE,
Packit ae235b
                     10 * 1000 /* msec */,
Packit ae235b
                     NULL, /* GCancellable */
Packit ae235b
                     sleep_cb,
Packit ae235b
                     &error);
Packit ae235b
Packit ae235b
  /* Make sure we don't exit when the bus goes away */
Packit ae235b
  g_dbus_connection_set_exit_on_close (c, FALSE);
Packit ae235b
Packit ae235b
  /* Nuke the connection to the bus */
Packit ae235b
  g_timeout_add (100 /* ms */, on_timeout, NULL);
Packit ae235b
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
Packit ae235b
  /* If we didn't act on connection-loss we'd be getting G_IO_ERROR_TIMEOUT
Packit ae235b
   * generated locally. So if we get G_IO_ERROR_CLOSED it means that we
Packit ae235b
   * are acting correctly on connection loss.
Packit ae235b
   */
Packit ae235b
  g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CLOSED);
Packit ae235b
  g_assert (!g_dbus_error_is_remote_error (error));
Packit ae235b
  g_clear_error (&error);
Packit ae235b
Packit ae235b
  g_object_unref (proxy);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int   argc,
Packit ae235b
      char *argv[])
Packit ae235b
{
Packit ae235b
  GError *error;
Packit ae235b
  gint ret;
Packit ae235b
  gchar *path;
Packit ae235b
Packit ae235b
  g_test_init (&argc, &argv, NULL);
Packit ae235b
Packit ae235b
  /* all the tests rely on a shared main loop */
Packit ae235b
  loop = g_main_loop_new (NULL, FALSE);
Packit ae235b
Packit ae235b
  session_bus_up ();
Packit ae235b
Packit ae235b
  /* this is safe; testserver will exit once the bus goes away */
Packit ae235b
  path = g_test_build_filename (G_TEST_BUILT, "gdbus-testserver", NULL);
Packit ae235b
  g_assert (g_spawn_command_line_async (path, NULL));
Packit ae235b
  g_free (path);
Packit ae235b
Packit ae235b
  ensure_gdbus_testserver_up ();
Packit ae235b
Packit ae235b
  /* Create the connection in the main thread */
Packit ae235b
  error = NULL;
Packit ae235b
  c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (c != NULL);
Packit ae235b
Packit ae235b
  g_test_add_func ("/gdbus/connection-loss", test_connection_loss);
Packit ae235b
Packit ae235b
  ret = g_test_run();
Packit ae235b
Packit ae235b
  g_object_unref (c);
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}